make staticpro a no-op
[bpt/emacs.git] / src / alloc.c
1 /* Storage allocation and gc for GNU Emacs Lisp interpreter.
2
3 Copyright (C) 1985-1986, 1988, 1993-1995, 1997-2014 Free Software
4 Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include <config.h>
22
23 #include <stdio.h>
24
25 #ifdef ENABLE_CHECKING
26 #include <signal.h> /* For SIGABRT. */
27 #endif
28
29 #ifdef HAVE_PTHREAD
30 #include <pthread.h>
31 #endif
32
33 #include <gc.h>
34
35 #include "lisp.h"
36 #include "process.h"
37 #include "intervals.h"
38 #include "character.h"
39 #include "buffer.h"
40 #include "window.h"
41 #include "keyboard.h"
42 #include "frame.h"
43 #include "termhooks.h" /* For struct terminal. */
44 #ifdef HAVE_WINDOW_SYSTEM
45 #include TERM_HEADER
46 #endif /* HAVE_WINDOW_SYSTEM */
47
48 #include <verify.h>
49 #include <execinfo.h> /* For backtrace. */
50
51 #if (defined ENABLE_CHECKING \
52 && defined HAVE_VALGRIND_VALGRIND_H \
53 && !defined USE_VALGRIND)
54 # define USE_VALGRIND 1
55 #endif
56
57 #if USE_VALGRIND
58 #include <valgrind/valgrind.h>
59 #include <valgrind/memcheck.h>
60 static bool valgrind_p;
61 #endif
62
63 #include <unistd.h>
64 #include <fcntl.h>
65
66 #ifdef USE_GTK
67 # include "gtkutil.h"
68 #endif
69 #ifdef WINDOWSNT
70 #include "w32.h"
71 #include "w32heap.h" /* for sbrk */
72 #endif
73
74 /* Default value of gc_cons_threshold (see below). */
75
76 #define GC_DEFAULT_THRESHOLD (100000 * word_size)
77
78 /* Global variables. */
79 struct emacs_globals globals;
80
81 /* Number of bytes of consing done since the last gc. */
82
83 EMACS_INT consing_since_gc;
84
85 /* Similar minimum, computed from Vgc_cons_percentage. */
86
87 EMACS_INT gc_relative_threshold;
88
89 /* Minimum number of bytes of consing since GC before next GC,
90 when memory is full. */
91
92 EMACS_INT memory_full_cons_threshold = 1 << 10;
93
94 /* True during GC. */
95
96 bool gc_in_progress;
97
98 /* True means abort if try to GC.
99 This is for code which is written on the assumption that
100 no GC will happen, so as to verify that assumption. */
101
102 bool abort_on_gc;
103
104 /* Number of live and free conses etc. */
105
106 static EMACS_INT total_conses, total_markers, total_symbols, total_buffers;
107 static EMACS_INT total_free_conses, total_free_markers, total_free_symbols;
108 static EMACS_INT total_free_floats, total_floats;
109
110 /* Points to memory space allocated as "spare", to be freed if we run
111 out of memory. */
112
113 static void *spare_memory;
114
115 /* Amount of spare memory to keep in large reserve block, or to see
116 whether this much is available when malloc fails on a larger request. */
117
118 #define SPARE_MEMORY (1 << 15)
119
120 /* If nonzero, this is a warning delivered by malloc and not yet
121 displayed. */
122
123 const char *pending_malloc_warning;
124
125 static Lisp_Object Qgc_cons_threshold;
126 Lisp_Object Qchar_table_extra_slots;
127
128 /* Hook run after GC has finished. */
129
130 static Lisp_Object Qpost_gc_hook;
131
132 #if !defined REL_ALLOC || defined SYSTEM_MALLOC
133 static void refill_memory_reserve (void);
134 #endif
135 static Lisp_Object make_empty_string (int);
136 extern Lisp_Object which_symbols (Lisp_Object, EMACS_INT) EXTERNALLY_VISIBLE;
137
138 #ifndef DEADP
139 # define DEADP(x) 0
140 #endif
141
142 /* Recording what needs to be marked for gc. */
143
144 struct gcpro *gcprolist;
145
146 static void
147 XFLOAT_INIT (Lisp_Object f, double n)
148 {
149 XFLOAT (f)->u.data = n;
150 }
151
152 \f
153 /************************************************************************
154 Malloc
155 ************************************************************************/
156
157 /* Function malloc calls this if it finds we are near exhausting storage. */
158
159 void
160 malloc_warning (const char *str)
161 {
162 pending_malloc_warning = str;
163 }
164
165
166 /* Display an already-pending malloc warning. */
167
168 void
169 display_malloc_warning (void)
170 {
171 call3 (intern ("display-warning"),
172 intern ("alloc"),
173 build_string (pending_malloc_warning),
174 intern ("emergency"));
175 pending_malloc_warning = 0;
176 }
177 \f
178 /* Called if we can't allocate relocatable space for a buffer. */
179
180 void
181 buffer_memory_full (ptrdiff_t nbytes)
182 {
183 /* If buffers use the relocating allocator, no need to free
184 spare_memory, because we may have plenty of malloc space left
185 that we could get, and if we don't, the malloc that fails will
186 itself cause spare_memory to be freed. If buffers don't use the
187 relocating allocator, treat this like any other failing
188 malloc. */
189
190 #ifndef REL_ALLOC
191 memory_full (nbytes);
192 #else
193 /* This used to call error, but if we've run out of memory, we could
194 get infinite recursion trying to build the string. */
195 xsignal (Qnil, Vmemory_signal_data);
196 #endif
197 }
198
199 /* Like GC_MALLOC but check for no memory. */
200
201 void *
202 xmalloc (size_t size)
203 {
204 void *val = GC_MALLOC (size);
205 if (!val && size)
206 memory_full (size);
207 return val;
208 }
209
210 /* Like the above, but zeroes out the memory just allocated. */
211
212 void *
213 xzalloc (size_t size)
214 {
215 return xmalloc (size);
216 }
217
218 /* Like GC_REALLOC but check for no memory. */
219
220 void *
221 xrealloc (void *block, size_t size)
222 {
223 void *val = GC_REALLOC (block, size);
224 if (!val && size)
225 memory_full (size);
226 return val;
227 }
228
229 void
230 xfree (void *block)
231 {
232 return;
233 }
234
235
236 /* Other parts of Emacs pass large int values to allocator functions
237 expecting ptrdiff_t. This is portable in practice, but check it to
238 be safe. */
239 verify (INT_MAX <= PTRDIFF_MAX);
240
241
242 /* Allocate an array of NITEMS items, each of size ITEM_SIZE.
243 Signal an error on memory exhaustion. */
244
245 void *
246 xnmalloc (ptrdiff_t nitems, ptrdiff_t item_size)
247 {
248 eassert (0 <= nitems && 0 < item_size);
249 if (min (PTRDIFF_MAX, SIZE_MAX) / item_size < nitems)
250 memory_full (SIZE_MAX);
251 return xmalloc (nitems * item_size);
252 }
253
254
255 /* Reallocate an array PA to make it of NITEMS items, each of size ITEM_SIZE.
256 Signal an error on memory exhaustion. */
257
258 void *
259 xnrealloc (void *pa, ptrdiff_t nitems, ptrdiff_t item_size)
260 {
261 eassert (0 <= nitems && 0 < item_size);
262 if (min (PTRDIFF_MAX, SIZE_MAX) / item_size < nitems)
263 memory_full (SIZE_MAX);
264 return xrealloc (pa, nitems * item_size);
265 }
266
267
268 /* Grow PA, which points to an array of *NITEMS items, and return the
269 location of the reallocated array, updating *NITEMS to reflect its
270 new size. The new array will contain at least NITEMS_INCR_MIN more
271 items, but will not contain more than NITEMS_MAX items total.
272 ITEM_SIZE is the size of each item, in bytes.
273
274 ITEM_SIZE and NITEMS_INCR_MIN must be positive. *NITEMS must be
275 nonnegative. If NITEMS_MAX is -1, it is treated as if it were
276 infinity.
277
278 If PA is null, then allocate a new array instead of reallocating
279 the old one.
280
281 If memory exhaustion occurs, set *NITEMS to zero if PA is null, and
282 signal an error (i.e., do not return).
283
284 Thus, to grow an array A without saving its old contents, do
285 { xfree (A); A = NULL; A = xpalloc (NULL, &AITEMS, ...); }.
286 The A = NULL avoids a dangling pointer if xpalloc exhausts memory
287 and signals an error, and later this code is reexecuted and
288 attempts to free A. */
289
290 void *
291 xpalloc (void *pa, ptrdiff_t *nitems, ptrdiff_t nitems_incr_min,
292 ptrdiff_t nitems_max, ptrdiff_t item_size)
293 {
294 /* The approximate size to use for initial small allocation
295 requests. This is the largest "small" request for the GNU C
296 library malloc. */
297 enum { DEFAULT_MXFAST = 64 * sizeof (size_t) / 4 };
298
299 /* If the array is tiny, grow it to about (but no greater than)
300 DEFAULT_MXFAST bytes. Otherwise, grow it by about 50%. */
301 ptrdiff_t n = *nitems;
302 ptrdiff_t tiny_max = DEFAULT_MXFAST / item_size - n;
303 ptrdiff_t half_again = n >> 1;
304 ptrdiff_t incr_estimate = max (tiny_max, half_again);
305
306 /* Adjust the increment according to three constraints: NITEMS_INCR_MIN,
307 NITEMS_MAX, and what the C language can represent safely. */
308 ptrdiff_t C_language_max = min (PTRDIFF_MAX, SIZE_MAX) / item_size;
309 ptrdiff_t n_max = (0 <= nitems_max && nitems_max < C_language_max
310 ? nitems_max : C_language_max);
311 ptrdiff_t nitems_incr_max = n_max - n;
312 ptrdiff_t incr = max (nitems_incr_min, min (incr_estimate, nitems_incr_max));
313
314 eassert (0 < item_size && 0 < nitems_incr_min && 0 <= n && -1 <= nitems_max);
315 if (! pa)
316 *nitems = 0;
317 if (nitems_incr_max < incr)
318 memory_full (SIZE_MAX);
319 n += incr;
320 pa = xrealloc (pa, n * item_size);
321 *nitems = n;
322 return pa;
323 }
324
325
326 /* Like strdup, but uses xmalloc. */
327
328 char *
329 xstrdup (const char *s)
330 {
331 ptrdiff_t size;
332 eassert (s);
333 size = strlen (s) + 1;
334 return memcpy (xmalloc (size), s, size);
335 }
336
337 /* Like above, but duplicates Lisp string to C string. */
338
339 char *
340 xlispstrdup (Lisp_Object string)
341 {
342 ptrdiff_t size = SBYTES (string) + 1;
343 return memcpy (xmalloc (size), SSDATA (string), size);
344 }
345
346 /* Assign to *PTR a copy of STRING, freeing any storage *PTR formerly
347 pointed to. If STRING is null, assign it without copying anything.
348 Allocate before freeing, to avoid a dangling pointer if allocation
349 fails. */
350
351 void
352 dupstring (char **ptr, char const *string)
353 {
354 char *old = *ptr;
355 *ptr = string ? xstrdup (string) : 0;
356 xfree (old);
357 }
358
359
360 /* Like putenv, but (1) use the equivalent of xmalloc and (2) the
361 argument is a const pointer. */
362
363 void
364 xputenv (char const *string)
365 {
366 if (putenv ((char *) string) != 0)
367 memory_full (0);
368 }
369
370 /* Return a newly allocated memory block of SIZE bytes, remembering
371 to free it when unwinding. */
372 void *
373 record_xmalloc (size_t size)
374 {
375 void *p = xmalloc (size);
376 record_unwind_protect_ptr (xfree, p);
377 return p;
378 }
379 \f
380 /***********************************************************************
381 Interval Allocation
382 ***********************************************************************/
383
384 /* Return a new interval. */
385
386 INTERVAL
387 make_interval (void)
388 {
389 INTERVAL val = xmalloc (sizeof (struct interval));
390 RESET_INTERVAL (val);
391 return val;
392 }
393
394 /***********************************************************************
395 String Allocation
396 ***********************************************************************/
397
398 /* Initialize string allocation. Called from init_alloc_once. */
399
400 static void
401 init_strings (void)
402 {
403 empty_unibyte_string = make_empty_string (0);
404 empty_multibyte_string = make_empty_string (1);
405 }
406
407 /* Return a new Lisp_String. */
408
409 static struct Lisp_String *
410 allocate_string (void)
411 {
412 return xmalloc (sizeof (struct Lisp_String));
413 }
414
415
416 /* Set up Lisp_String S for holding NCHARS characters, NBYTES bytes,
417 plus a NUL byte at the end. Allocate an sdata structure for S, and
418 set S->data to its `u.data' member. Store a NUL byte at the end of
419 S->data. Set S->size to NCHARS and S->size_byte to NBYTES. Free
420 S->data if it was initially non-null. */
421
422 void
423 allocate_string_data (struct Lisp_String *s,
424 EMACS_INT nchars, EMACS_INT nbytes)
425 {
426 unsigned char *data;
427
428 if (STRING_BYTES_BOUND < nbytes)
429 string_overflow ();
430
431 data = GC_MALLOC_ATOMIC (nbytes + 1);
432 s->data = data;
433 s->size = nchars;
434 s->size_byte = nbytes;
435 s->data[nbytes] = '\0';
436 }
437
438 void
439 string_overflow (void)
440 {
441 error ("Maximum string size exceeded");
442 }
443
444 static Lisp_Object
445 make_empty_string (int multibyte)
446 {
447 Lisp_Object string;
448 struct Lisp_String *s;
449
450 s = allocate_string ();
451 allocate_string_data (s, 0, 0);
452 XSETSTRING (string, s);
453 if (! multibyte)
454 STRING_SET_UNIBYTE (string);
455
456 return string;
457 }
458
459 DEFUN ("make-string", Fmake_string, Smake_string, 2, 2, 0,
460 doc: /* Return a newly created string of length LENGTH, with INIT in each element.
461 LENGTH must be an integer.
462 INIT must be an integer that represents a character. */)
463 (Lisp_Object length, Lisp_Object init)
464 {
465 register Lisp_Object val;
466 int c;
467 EMACS_INT nbytes;
468
469 CHECK_NATNUM (length);
470 CHECK_CHARACTER (init);
471
472 c = XFASTINT (init);
473 if (ASCII_CHAR_P (c))
474 {
475 nbytes = XINT (length);
476 val = make_uninit_string (nbytes);
477 memset (SDATA (val), c, nbytes);
478 SDATA (val)[nbytes] = 0;
479 }
480 else
481 {
482 unsigned char str[MAX_MULTIBYTE_LENGTH];
483 ptrdiff_t len = CHAR_STRING (c, str);
484 EMACS_INT string_len = XINT (length);
485 unsigned char *p, *beg, *end;
486
487 if (string_len > STRING_BYTES_BOUND / len)
488 string_overflow ();
489 nbytes = len * string_len;
490 val = make_uninit_multibyte_string (string_len, nbytes);
491 for (beg = SDATA (val), p = beg, end = beg + nbytes; p < end; p += len)
492 {
493 /* First time we just copy `str' to the data of `val'. */
494 if (p == beg)
495 memcpy (p, str, len);
496 else
497 {
498 /* Next time we copy largest possible chunk from
499 initialized to uninitialized part of `val'. */
500 len = min (p - beg, end - p);
501 memcpy (p, beg, len);
502 }
503 }
504 *p = 0;
505 }
506
507 return val;
508 }
509
510 /* Fill A with 1 bits if INIT is non-nil, and with 0 bits otherwise.
511 Return A. */
512
513 Lisp_Object
514 bool_vector_fill (Lisp_Object a, Lisp_Object init)
515 {
516 EMACS_INT nbits = bool_vector_size (a);
517 if (0 < nbits)
518 {
519 unsigned char *data = bool_vector_uchar_data (a);
520 int pattern = NILP (init) ? 0 : (1 << BOOL_VECTOR_BITS_PER_CHAR) - 1;
521 ptrdiff_t nbytes = bool_vector_bytes (nbits);
522 int last_mask = ~ (~0u << ((nbits - 1) % BOOL_VECTOR_BITS_PER_CHAR + 1));
523 memset (data, pattern, nbytes - 1);
524 data[nbytes - 1] = pattern & last_mask;
525 }
526 return a;
527 }
528
529 /* Return a newly allocated, uninitialized bool vector of size NBITS. */
530
531 Lisp_Object
532 make_uninit_bool_vector (EMACS_INT nbits)
533 {
534 Lisp_Object val;
535 EMACS_INT words = bool_vector_words (nbits);
536 EMACS_INT word_bytes = words * sizeof (bits_word);
537 EMACS_INT needed_elements = ((bool_header_size - header_size + word_bytes
538 + word_size - 1)
539 / word_size);
540 struct Lisp_Bool_Vector *p
541 = (struct Lisp_Bool_Vector *) allocate_vector (needed_elements);
542 XSETVECTOR (val, p);
543 XSETPVECTYPESIZE (XVECTOR (val), PVEC_BOOL_VECTOR, 0, 0);
544 p->size = nbits;
545
546 /* Clear padding at the end. */
547 if (words)
548 p->data[words - 1] = 0;
549
550 return val;
551 }
552
553 DEFUN ("make-bool-vector", Fmake_bool_vector, Smake_bool_vector, 2, 2, 0,
554 doc: /* Return a new bool-vector of length LENGTH, using INIT for each element.
555 LENGTH must be a number. INIT matters only in whether it is t or nil. */)
556 (Lisp_Object length, Lisp_Object init)
557 {
558 Lisp_Object val;
559
560 CHECK_NATNUM (length);
561 val = make_uninit_bool_vector (XFASTINT (length));
562 return bool_vector_fill (val, init);
563 }
564
565 DEFUN ("bool-vector", Fbool_vector, Sbool_vector, 0, MANY, 0,
566 doc: /* Return a new bool-vector with specified arguments as elements.
567 Any number of arguments, even zero arguments, are allowed.
568 usage: (bool-vector &rest OBJECTS) */)
569 (ptrdiff_t nargs, Lisp_Object *args)
570 {
571 ptrdiff_t i;
572 Lisp_Object vector;
573
574 vector = make_uninit_bool_vector (nargs);
575 for (i = 0; i < nargs; i++)
576 bool_vector_set (vector, i, !NILP (args[i]));
577
578 return vector;
579 }
580
581 /* Make a string from NBYTES bytes at CONTENTS, and compute the number
582 of characters from the contents. This string may be unibyte or
583 multibyte, depending on the contents. */
584
585 Lisp_Object
586 make_string (const char *contents, ptrdiff_t nbytes)
587 {
588 register Lisp_Object val;
589 ptrdiff_t nchars, multibyte_nbytes;
590
591 parse_str_as_multibyte ((const unsigned char *) contents, nbytes,
592 &nchars, &multibyte_nbytes);
593 if (nbytes == nchars || nbytes != multibyte_nbytes)
594 /* CONTENTS contains no multibyte sequences or contains an invalid
595 multibyte sequence. We must make unibyte string. */
596 val = make_unibyte_string (contents, nbytes);
597 else
598 val = make_multibyte_string (contents, nchars, nbytes);
599 return val;
600 }
601
602
603 /* Make an unibyte string from LENGTH bytes at CONTENTS. */
604
605 Lisp_Object
606 make_unibyte_string (const char *contents, ptrdiff_t length)
607 {
608 register Lisp_Object val;
609 val = make_uninit_string (length);
610 memcpy (SDATA (val), contents, length);
611 return val;
612 }
613
614
615 /* Make a multibyte string from NCHARS characters occupying NBYTES
616 bytes at CONTENTS. */
617
618 Lisp_Object
619 make_multibyte_string (const char *contents,
620 ptrdiff_t nchars, ptrdiff_t nbytes)
621 {
622 register Lisp_Object val;
623 val = make_uninit_multibyte_string (nchars, nbytes);
624 memcpy (SDATA (val), contents, nbytes);
625 return val;
626 }
627
628
629 /* Make a string from NCHARS characters occupying NBYTES bytes at
630 CONTENTS. It is a multibyte string if NBYTES != NCHARS. */
631
632 Lisp_Object
633 make_string_from_bytes (const char *contents,
634 ptrdiff_t nchars, ptrdiff_t nbytes)
635 {
636 register Lisp_Object val;
637 val = make_uninit_multibyte_string (nchars, nbytes);
638 memcpy (SDATA (val), contents, nbytes);
639 if (SBYTES (val) == SCHARS (val))
640 STRING_SET_UNIBYTE (val);
641 return val;
642 }
643
644
645 /* Make a string from NCHARS characters occupying NBYTES bytes at
646 CONTENTS. The argument MULTIBYTE controls whether to label the
647 string as multibyte. If NCHARS is negative, it counts the number of
648 characters by itself. */
649
650 Lisp_Object
651 make_specified_string (const char *contents,
652 ptrdiff_t nchars, ptrdiff_t nbytes, bool multibyte)
653 {
654 Lisp_Object val;
655
656 if (nchars < 0)
657 {
658 if (multibyte)
659 nchars = multibyte_chars_in_text ((const unsigned char *) contents,
660 nbytes);
661 else
662 nchars = nbytes;
663 }
664 val = make_uninit_multibyte_string (nchars, nbytes);
665 memcpy (SDATA (val), contents, nbytes);
666 if (!multibyte)
667 STRING_SET_UNIBYTE (val);
668 return val;
669 }
670
671
672 /* Return an unibyte Lisp_String set up to hold LENGTH characters
673 occupying LENGTH bytes. */
674
675 Lisp_Object
676 make_uninit_string (EMACS_INT length)
677 {
678 Lisp_Object val;
679
680 if (!length)
681 return empty_unibyte_string;
682 val = make_uninit_multibyte_string (length, length);
683 STRING_SET_UNIBYTE (val);
684 return val;
685 }
686
687
688 /* Return a multibyte Lisp_String set up to hold NCHARS characters
689 which occupy NBYTES bytes. */
690
691 Lisp_Object
692 make_uninit_multibyte_string (EMACS_INT nchars, EMACS_INT nbytes)
693 {
694 Lisp_Object string;
695 struct Lisp_String *s;
696
697 if (nchars < 0)
698 emacs_abort ();
699 if (!nbytes)
700 return empty_multibyte_string;
701
702 s = allocate_string ();
703 s->intervals = NULL;
704 allocate_string_data (s, nchars, nbytes);
705 XSETSTRING (string, s);
706 return string;
707 }
708
709 /* Print arguments to BUF according to a FORMAT, then return
710 a Lisp_String initialized with the data from BUF. */
711
712 Lisp_Object
713 make_formatted_string (char *buf, const char *format, ...)
714 {
715 va_list ap;
716 int length;
717
718 va_start (ap, format);
719 length = vsprintf (buf, format, ap);
720 va_end (ap);
721 return make_string (buf, length);
722 }
723
724 \f
725 /***********************************************************************
726 Float Allocation
727 ***********************************************************************/
728
729 /* Return a new float object with value FLOAT_VALUE. */
730
731 Lisp_Object
732 make_float (double float_value)
733 {
734 register Lisp_Object val;
735 XSETFLOAT (val, xmalloc (sizeof (struct Lisp_Float)));
736 XFLOAT_INIT (val, float_value);
737 return val;
738 }
739
740
741 \f
742 /***********************************************************************
743 Cons Allocation
744 ***********************************************************************/
745
746 DEFUN ("cons", Fcons, Scons, 2, 2, 0,
747 doc: /* Create a new cons, give it CAR and CDR as components, and return it. */)
748 (Lisp_Object car, Lisp_Object cdr)
749 {
750 register Lisp_Object val;
751
752 XSETCONS (val, xmalloc (sizeof (struct Lisp_Cons)));
753 XSETCAR (val, car);
754 XSETCDR (val, cdr);
755 return val;
756 }
757
758 /* Make a list of 1, 2, 3, 4 or 5 specified objects. */
759
760 Lisp_Object
761 list1 (Lisp_Object arg1)
762 {
763 return Fcons (arg1, Qnil);
764 }
765
766 Lisp_Object
767 list2 (Lisp_Object arg1, Lisp_Object arg2)
768 {
769 return Fcons (arg1, Fcons (arg2, Qnil));
770 }
771
772
773 Lisp_Object
774 list3 (Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
775 {
776 return Fcons (arg1, Fcons (arg2, Fcons (arg3, Qnil)));
777 }
778
779
780 Lisp_Object
781 list4 (Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3, Lisp_Object arg4)
782 {
783 return Fcons (arg1, Fcons (arg2, Fcons (arg3, Fcons (arg4, Qnil))));
784 }
785
786
787 Lisp_Object
788 list5 (Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3, Lisp_Object arg4, Lisp_Object arg5)
789 {
790 return Fcons (arg1, Fcons (arg2, Fcons (arg3, Fcons (arg4,
791 Fcons (arg5, Qnil)))));
792 }
793
794 /* Make a list of COUNT Lisp_Objects, where ARG is the
795 first one. Allocate conses from pure space if TYPE
796 is CONSTYPE_PURE, or allocate as usual if type is CONSTYPE_HEAP. */
797
798 Lisp_Object
799 listn (enum constype type, ptrdiff_t count, Lisp_Object arg, ...)
800 {
801 va_list ap;
802 ptrdiff_t i;
803 Lisp_Object val, *objp;
804
805 /* Change to SAFE_ALLOCA if you hit this eassert. */
806 eassert (count <= MAX_ALLOCA / word_size);
807
808 objp = alloca (count * word_size);
809 objp[0] = arg;
810 va_start (ap, arg);
811 for (i = 1; i < count; i++)
812 objp[i] = va_arg (ap, Lisp_Object);
813 va_end (ap);
814
815 for (val = Qnil, i = count - 1; i >= 0; i--)
816 {
817 if (type == CONSTYPE_PURE)
818 val = pure_cons (objp[i], val);
819 else if (type == CONSTYPE_HEAP)
820 val = Fcons (objp[i], val);
821 else
822 emacs_abort ();
823 }
824 return val;
825 }
826
827 DEFUN ("list", Flist, Slist, 0, MANY, 0,
828 doc: /* Return a newly created list with specified arguments as elements.
829 Any number of arguments, even zero arguments, are allowed.
830 usage: (list &rest OBJECTS) */)
831 (ptrdiff_t nargs, Lisp_Object *args)
832 {
833 register Lisp_Object val;
834 val = Qnil;
835
836 while (nargs > 0)
837 {
838 nargs--;
839 val = Fcons (args[nargs], val);
840 }
841 return val;
842 }
843
844
845 DEFUN ("make-list", Fmake_list, Smake_list, 2, 2, 0,
846 doc: /* Return a newly created list of length LENGTH, with each element being INIT. */)
847 (register Lisp_Object length, Lisp_Object init)
848 {
849 register Lisp_Object val;
850 register EMACS_INT size;
851
852 CHECK_NATNUM (length);
853 size = XFASTINT (length);
854
855 val = Qnil;
856 while (size > 0)
857 {
858 val = Fcons (init, val);
859 --size;
860
861 if (size > 0)
862 {
863 val = Fcons (init, val);
864 --size;
865
866 if (size > 0)
867 {
868 val = Fcons (init, val);
869 --size;
870
871 if (size > 0)
872 {
873 val = Fcons (init, val);
874 --size;
875
876 if (size > 0)
877 {
878 val = Fcons (init, val);
879 --size;
880 }
881 }
882 }
883 }
884
885 QUIT;
886 }
887
888 return val;
889 }
890
891
892 \f
893 /***********************************************************************
894 Vector Allocation
895 ***********************************************************************/
896
897 /* The only vector with 0 slots, allocated from pure space. */
898
899 Lisp_Object zero_vector;
900
901 /* Called once to initialize vector allocation. */
902
903 static void
904 init_vectors (void)
905 {
906 XSETVECTOR (zero_vector, xmalloc (header_size));
907 XVECTOR (zero_vector)->header.size = 0;
908 }
909
910 /* Value is a pointer to a newly allocated Lisp_Vector structure
911 with room for LEN Lisp_Objects. */
912
913 static struct Lisp_Vector *
914 allocate_vectorlike (ptrdiff_t len)
915 {
916 if (len == 0)
917 return XVECTOR (zero_vector);
918 else
919 return xmalloc (header_size + len * word_size);
920 }
921
922
923 /* Allocate a vector with LEN slots. */
924
925 struct Lisp_Vector *
926 allocate_vector (EMACS_INT len)
927 {
928 struct Lisp_Vector *v;
929 ptrdiff_t nbytes_max = min (PTRDIFF_MAX, SIZE_MAX);
930
931 if (min ((nbytes_max - header_size) / word_size, MOST_POSITIVE_FIXNUM) < len)
932 memory_full (SIZE_MAX);
933 v = allocate_vectorlike (len);
934 v->header.size = len;
935 return v;
936 }
937
938
939 /* Allocate other vector-like structures. */
940
941 struct Lisp_Vector *
942 allocate_pseudovector (int memlen, int lisplen, enum pvec_type tag)
943 {
944 struct Lisp_Vector *v = allocate_vectorlike (memlen);
945 int i;
946
947 /* Catch bogus values. */
948 eassert (tag <= PVEC_FONT);
949 eassert (memlen - lisplen <= (1 << PSEUDOVECTOR_REST_BITS) - 1);
950 eassert (lisplen <= (1 << PSEUDOVECTOR_SIZE_BITS) - 1);
951
952 /* Only the first lisplen slots will be traced normally by the GC. */
953 for (i = 0; i < lisplen; ++i)
954 v->contents[i] = Qnil;
955
956 XSETPVECTYPESIZE (v, tag, lisplen, memlen - lisplen);
957 return v;
958 }
959
960 struct buffer *
961 allocate_buffer (void)
962 {
963 struct buffer *b = xmalloc (sizeof *b);
964
965 BUFFER_PVEC_INIT (b);
966 /* Put B on the chain of all buffers including killed ones. */
967 b->next = all_buffers;
968 all_buffers = b;
969 /* Note that the rest fields of B are not initialized. */
970 return b;
971 }
972
973 struct Lisp_Hash_Table *
974 allocate_hash_table (void)
975 {
976 return ALLOCATE_PSEUDOVECTOR (struct Lisp_Hash_Table, count, PVEC_HASH_TABLE);
977 }
978
979 struct window *
980 allocate_window (void)
981 {
982 struct window *w;
983
984 w = ALLOCATE_PSEUDOVECTOR (struct window, current_matrix, PVEC_WINDOW);
985 /* Users assumes that non-Lisp data is zeroed. */
986 memset (&w->current_matrix, 0,
987 sizeof (*w) - offsetof (struct window, current_matrix));
988 return w;
989 }
990
991 struct terminal *
992 allocate_terminal (void)
993 {
994 struct terminal *t;
995
996 t = ALLOCATE_PSEUDOVECTOR (struct terminal, next_terminal, PVEC_TERMINAL);
997 /* Users assumes that non-Lisp data is zeroed. */
998 memset (&t->next_terminal, 0,
999 sizeof (*t) - offsetof (struct terminal, next_terminal));
1000 return t;
1001 }
1002
1003 struct frame *
1004 allocate_frame (void)
1005 {
1006 struct frame *f;
1007
1008 f = ALLOCATE_PSEUDOVECTOR (struct frame, face_cache, PVEC_FRAME);
1009 /* Users assumes that non-Lisp data is zeroed. */
1010 memset (&f->face_cache, 0,
1011 sizeof (*f) - offsetof (struct frame, face_cache));
1012 return f;
1013 }
1014
1015 struct Lisp_Process *
1016 allocate_process (void)
1017 {
1018 struct Lisp_Process *p;
1019
1020 p = ALLOCATE_PSEUDOVECTOR (struct Lisp_Process, pid, PVEC_PROCESS);
1021 /* Users assumes that non-Lisp data is zeroed. */
1022 memset (&p->pid, 0,
1023 sizeof (*p) - offsetof (struct Lisp_Process, pid));
1024 return p;
1025 }
1026
1027 DEFUN ("make-vector", Fmake_vector, Smake_vector, 2, 2, 0,
1028 doc: /* Return a newly created vector of length LENGTH, with each element being INIT.
1029 See also the function `vector'. */)
1030 (register Lisp_Object length, Lisp_Object init)
1031 {
1032 Lisp_Object vector;
1033 register ptrdiff_t sizei;
1034 register ptrdiff_t i;
1035 register struct Lisp_Vector *p;
1036
1037 CHECK_NATNUM (length);
1038
1039 p = allocate_vector (XFASTINT (length));
1040 sizei = XFASTINT (length);
1041 for (i = 0; i < sizei; i++)
1042 p->contents[i] = init;
1043
1044 XSETVECTOR (vector, p);
1045 return vector;
1046 }
1047
1048
1049 DEFUN ("vector", Fvector, Svector, 0, MANY, 0,
1050 doc: /* Return a newly created vector with specified arguments as elements.
1051 Any number of arguments, even zero arguments, are allowed.
1052 usage: (vector &rest OBJECTS) */)
1053 (ptrdiff_t nargs, Lisp_Object *args)
1054 {
1055 ptrdiff_t i;
1056 register Lisp_Object val = make_uninit_vector (nargs);
1057 register struct Lisp_Vector *p = XVECTOR (val);
1058
1059 for (i = 0; i < nargs; i++)
1060 p->contents[i] = args[i];
1061 return val;
1062 }
1063
1064 void
1065 make_byte_code (struct Lisp_Vector *v)
1066 {
1067 /* Don't allow the global zero_vector to become a byte code object. */
1068 eassert (0 < v->header.size);
1069
1070 if (v->header.size > 1 && STRINGP (v->contents[1])
1071 && STRING_MULTIBYTE (v->contents[1]))
1072 /* BYTECODE-STRING must have been produced by Emacs 20.2 or the
1073 earlier because they produced a raw 8-bit string for byte-code
1074 and now such a byte-code string is loaded as multibyte while
1075 raw 8-bit characters converted to multibyte form. Thus, now we
1076 must convert them back to the original unibyte form. */
1077 v->contents[1] = Fstring_as_unibyte (v->contents[1]);
1078 XSETPVECTYPE (v, PVEC_COMPILED);
1079 }
1080
1081 DEFUN ("make-byte-code", Fmake_byte_code, Smake_byte_code, 4, MANY, 0,
1082 doc: /* Create a byte-code object with specified arguments as elements.
1083 The arguments should be the ARGLIST, bytecode-string BYTE-CODE, constant
1084 vector CONSTANTS, maximum stack size DEPTH, (optional) DOCSTRING,
1085 and (optional) INTERACTIVE-SPEC.
1086 The first four arguments are required; at most six have any
1087 significance.
1088 The ARGLIST can be either like the one of `lambda', in which case the arguments
1089 will be dynamically bound before executing the byte code, or it can be an
1090 integer of the form NNNNNNNRMMMMMMM where the 7bit MMMMMMM specifies the
1091 minimum number of arguments, the 7-bit NNNNNNN specifies the maximum number
1092 of arguments (ignoring &rest) and the R bit specifies whether there is a &rest
1093 argument to catch the left-over arguments. If such an integer is used, the
1094 arguments will not be dynamically bound but will be instead pushed on the
1095 stack before executing the byte-code.
1096 usage: (make-byte-code ARGLIST BYTE-CODE CONSTANTS DEPTH &optional DOCSTRING INTERACTIVE-SPEC &rest ELEMENTS) */)
1097 (ptrdiff_t nargs, Lisp_Object *args)
1098 {
1099 ptrdiff_t i;
1100 register Lisp_Object val = make_uninit_vector (nargs);
1101 register struct Lisp_Vector *p = XVECTOR (val);
1102
1103 /* We used to purecopy everything here, if purify-flag was set. This worked
1104 OK for Emacs-23, but with Emacs-24's lexical binding code, it can be
1105 dangerous, since make-byte-code is used during execution to build
1106 closures, so any closure built during the preload phase would end up
1107 copied into pure space, including its free variables, which is sometimes
1108 just wasteful and other times plainly wrong (e.g. those free vars may want
1109 to be setcar'd). */
1110
1111 for (i = 0; i < nargs; i++)
1112 p->contents[i] = args[i];
1113 make_byte_code (p);
1114 XSETCOMPILED (val, p);
1115 return val;
1116 }
1117
1118
1119 \f
1120 /***********************************************************************
1121 Symbol Allocation
1122 ***********************************************************************/
1123
1124 static void
1125 set_symbol_name (Lisp_Object sym, Lisp_Object name)
1126 {
1127 XSYMBOL (sym)->name = name;
1128 }
1129
1130 DEFUN ("make-symbol", Fmake_symbol, Smake_symbol, 1, 1, 0,
1131 doc: /* Return a newly allocated uninterned symbol whose name is NAME.
1132 Its value is void, and its function definition and property list are nil. */)
1133 (Lisp_Object name)
1134 {
1135 register Lisp_Object val;
1136 register struct Lisp_Symbol *p;
1137
1138 CHECK_STRING (name);
1139
1140 XSETSYMBOL (val, xmalloc (sizeof (struct Lisp_Symbol)));
1141 p = XSYMBOL (val);
1142 set_symbol_name (val, name);
1143 set_symbol_plist (val, Qnil);
1144 p->redirect = SYMBOL_PLAINVAL;
1145 SET_SYMBOL_VAL (p, Qunbound);
1146 set_symbol_function (val, Qnil);
1147 set_symbol_next (val, NULL);
1148 p->gcmarkbit = false;
1149 p->interned = SYMBOL_UNINTERNED;
1150 p->constant = 0;
1151 p->declared_special = false;
1152 p->pinned = false;
1153 return val;
1154 }
1155
1156
1157 \f
1158 /***********************************************************************
1159 Marker (Misc) Allocation
1160 ***********************************************************************/
1161
1162 /* Return a newly allocated Lisp_Misc object of specified TYPE. */
1163
1164 static Lisp_Object
1165 allocate_misc (enum Lisp_Misc_Type type)
1166 {
1167 Lisp_Object val;
1168
1169 XSETMISC (val, xmalloc (sizeof (union Lisp_Misc)));
1170 XMISCANY (val)->type = type;
1171 return val;
1172 }
1173
1174 /* Free a Lisp_Misc object. */
1175
1176 void
1177 free_misc (Lisp_Object misc)
1178 {
1179 return;
1180 }
1181
1182 /* Verify properties of Lisp_Save_Value's representation
1183 that are assumed here and elsewhere. */
1184
1185 verify (SAVE_UNUSED == 0);
1186 verify (((SAVE_INTEGER | SAVE_POINTER | SAVE_FUNCPOINTER | SAVE_OBJECT)
1187 >> SAVE_SLOT_BITS)
1188 == 0);
1189
1190 /* Return Lisp_Save_Value objects for the various combinations
1191 that callers need. */
1192
1193 Lisp_Object
1194 make_save_int_int_int (ptrdiff_t a, ptrdiff_t b, ptrdiff_t c)
1195 {
1196 Lisp_Object val = allocate_misc (Lisp_Misc_Save_Value);
1197 struct Lisp_Save_Value *p = XSAVE_VALUE (val);
1198 p->save_type = SAVE_TYPE_INT_INT_INT;
1199 p->data[0].integer = a;
1200 p->data[1].integer = b;
1201 p->data[2].integer = c;
1202 return val;
1203 }
1204
1205 Lisp_Object
1206 make_save_obj_obj_obj_obj (Lisp_Object a, Lisp_Object b, Lisp_Object c,
1207 Lisp_Object d)
1208 {
1209 Lisp_Object val = allocate_misc (Lisp_Misc_Save_Value);
1210 struct Lisp_Save_Value *p = XSAVE_VALUE (val);
1211 p->save_type = SAVE_TYPE_OBJ_OBJ_OBJ_OBJ;
1212 p->data[0].object = a;
1213 p->data[1].object = b;
1214 p->data[2].object = c;
1215 p->data[3].object = d;
1216 return val;
1217 }
1218
1219 Lisp_Object
1220 make_save_ptr (void *a)
1221 {
1222 Lisp_Object val = allocate_misc (Lisp_Misc_Save_Value);
1223 struct Lisp_Save_Value *p = XSAVE_VALUE (val);
1224 p->save_type = SAVE_POINTER;
1225 p->data[0].pointer = a;
1226 return val;
1227 }
1228
1229 Lisp_Object
1230 make_save_ptr_int (void *a, ptrdiff_t b)
1231 {
1232 Lisp_Object val = allocate_misc (Lisp_Misc_Save_Value);
1233 struct Lisp_Save_Value *p = XSAVE_VALUE (val);
1234 p->save_type = SAVE_TYPE_PTR_INT;
1235 p->data[0].pointer = a;
1236 p->data[1].integer = b;
1237 return val;
1238 }
1239
1240 #if ! (defined USE_X_TOOLKIT || defined USE_GTK)
1241 Lisp_Object
1242 make_save_ptr_ptr (void *a, void *b)
1243 {
1244 Lisp_Object val = allocate_misc (Lisp_Misc_Save_Value);
1245 struct Lisp_Save_Value *p = XSAVE_VALUE (val);
1246 p->save_type = SAVE_TYPE_PTR_PTR;
1247 p->data[0].pointer = a;
1248 p->data[1].pointer = b;
1249 return val;
1250 }
1251 #endif
1252
1253 Lisp_Object
1254 make_save_funcptr_ptr_obj (void (*a) (void), void *b, Lisp_Object c)
1255 {
1256 Lisp_Object val = allocate_misc (Lisp_Misc_Save_Value);
1257 struct Lisp_Save_Value *p = XSAVE_VALUE (val);
1258 p->save_type = SAVE_TYPE_FUNCPTR_PTR_OBJ;
1259 p->data[0].funcpointer = a;
1260 p->data[1].pointer = b;
1261 p->data[2].object = c;
1262 return val;
1263 }
1264
1265 /* Return a Lisp_Save_Value object that represents an array A
1266 of N Lisp objects. */
1267
1268 Lisp_Object
1269 make_save_memory (Lisp_Object *a, ptrdiff_t n)
1270 {
1271 Lisp_Object val = allocate_misc (Lisp_Misc_Save_Value);
1272 struct Lisp_Save_Value *p = XSAVE_VALUE (val);
1273 p->save_type = SAVE_TYPE_MEMORY;
1274 p->data[0].pointer = a;
1275 p->data[1].integer = n;
1276 return val;
1277 }
1278
1279 /* Free a Lisp_Save_Value object. Do not use this function
1280 if SAVE contains pointer other than returned by xmalloc. */
1281
1282 void
1283 free_save_value (Lisp_Object save)
1284 {
1285 xfree (XSAVE_POINTER (save, 0));
1286 free_misc (save);
1287 }
1288
1289 /* Return a Lisp_Misc_Overlay object with specified START, END and PLIST. */
1290
1291 Lisp_Object
1292 build_overlay (Lisp_Object start, Lisp_Object end, Lisp_Object plist)
1293 {
1294 register Lisp_Object overlay;
1295
1296 overlay = allocate_misc (Lisp_Misc_Overlay);
1297 OVERLAY_START (overlay) = start;
1298 OVERLAY_END (overlay) = end;
1299 set_overlay_plist (overlay, plist);
1300 XOVERLAY (overlay)->next = NULL;
1301 return overlay;
1302 }
1303
1304 DEFUN ("make-marker", Fmake_marker, Smake_marker, 0, 0, 0,
1305 doc: /* Return a newly allocated marker which does not point at any place. */)
1306 (void)
1307 {
1308 register Lisp_Object val;
1309 register struct Lisp_Marker *p;
1310
1311 val = allocate_misc (Lisp_Misc_Marker);
1312 p = XMARKER (val);
1313 p->buffer = 0;
1314 p->bytepos = 0;
1315 p->charpos = 0;
1316 p->next = NULL;
1317 p->insertion_type = 0;
1318 p->need_adjustment = 0;
1319 return val;
1320 }
1321
1322 /* Return a newly allocated marker which points into BUF
1323 at character position CHARPOS and byte position BYTEPOS. */
1324
1325 Lisp_Object
1326 build_marker (struct buffer *buf, ptrdiff_t charpos, ptrdiff_t bytepos)
1327 {
1328 Lisp_Object obj;
1329 struct Lisp_Marker *m;
1330
1331 /* No dead buffers here. */
1332 eassert (BUFFER_LIVE_P (buf));
1333
1334 /* Every character is at least one byte. */
1335 eassert (charpos <= bytepos);
1336
1337 obj = allocate_misc (Lisp_Misc_Marker);
1338 m = XMARKER (obj);
1339 m->buffer = buf;
1340 m->charpos = charpos;
1341 m->bytepos = bytepos;
1342 m->insertion_type = 0;
1343 m->need_adjustment = 0;
1344 m->next = BUF_MARKERS (buf);
1345 BUF_MARKERS (buf) = m;
1346 return obj;
1347 }
1348 \f
1349 /* Return a newly created vector or string with specified arguments as
1350 elements. If all the arguments are characters that can fit
1351 in a string of events, make a string; otherwise, make a vector.
1352
1353 Any number of arguments, even zero arguments, are allowed. */
1354
1355 Lisp_Object
1356 make_event_array (ptrdiff_t nargs, Lisp_Object *args)
1357 {
1358 ptrdiff_t i;
1359
1360 for (i = 0; i < nargs; i++)
1361 /* The things that fit in a string
1362 are characters that are in 0...127,
1363 after discarding the meta bit and all the bits above it. */
1364 if (!INTEGERP (args[i])
1365 || (XINT (args[i]) & ~(-CHAR_META)) >= 0200)
1366 return Fvector (nargs, args);
1367
1368 /* Since the loop exited, we know that all the things in it are
1369 characters, so we can make a string. */
1370 {
1371 Lisp_Object result;
1372
1373 result = Fmake_string (make_number (nargs), make_number (0));
1374 for (i = 0; i < nargs; i++)
1375 {
1376 SSET (result, i, XINT (args[i]));
1377 /* Move the meta bit to the right place for a string char. */
1378 if (XINT (args[i]) & CHAR_META)
1379 SSET (result, i, SREF (result, i) | 0x80);
1380 }
1381
1382 return result;
1383 }
1384 }
1385
1386
1387 \f
1388 /************************************************************************
1389 Memory Full Handling
1390 ************************************************************************/
1391
1392
1393 /* Called if xmalloc (NBYTES) returns zero. If NBYTES == SIZE_MAX,
1394 there may have been size_t overflow so that xmalloc was never
1395 called, or perhaps xmalloc was invoked successfully but the
1396 resulting pointer had problems fitting into a tagged EMACS_INT. In
1397 either case this counts as memory being full even though xmalloc
1398 did not fail. */
1399
1400 void
1401 memory_full (size_t nbytes)
1402 {
1403 /* Do not go into hysterics merely because a large request failed. */
1404 bool enough_free_memory = 0;
1405 if (SPARE_MEMORY < nbytes)
1406 {
1407 void *p = GC_MALLOC (SPARE_MEMORY);
1408 if (p)
1409 {
1410 xfree (p);
1411 enough_free_memory = 1;
1412 }
1413 }
1414
1415 if (! enough_free_memory)
1416 {
1417 Vmemory_full = Qt;
1418
1419 /* The first time we get here, free the spare memory. */
1420 if (spare_memory)
1421 {
1422 xfree (spare_memory);
1423 spare_memory = NULL;
1424 }
1425 }
1426
1427 /* This used to call error, but if we've run out of memory, we could
1428 get infinite recursion trying to build the string. */
1429 xsignal (Qnil, Vmemory_signal_data);
1430 }
1431
1432 /* If we released our reserve (due to running out of memory),
1433 and we have a fair amount free once again,
1434 try to set aside another reserve in case we run out once more.
1435
1436 This is called when a relocatable block is freed in ralloc.c,
1437 and also directly from this file, in case we're not using ralloc.c. */
1438
1439 void
1440 refill_memory_reserve (void)
1441 {
1442 if (spare_memory == NULL)
1443 spare_memory = GC_MALLOC (SPARE_MEMORY);
1444
1445 if (spare_memory)
1446 Vmemory_full = Qnil;
1447 }
1448 \f
1449 /* Determine whether it is safe to access memory at address P. */
1450 static int
1451 valid_pointer_p (void *p)
1452 {
1453 #ifdef WINDOWSNT
1454 return w32_valid_pointer_p (p, 16);
1455 #else
1456 int fd[2];
1457
1458 /* Obviously, we cannot just access it (we would SEGV trying), so we
1459 trick the o/s to tell us whether p is a valid pointer.
1460 Unfortunately, we cannot use NULL_DEVICE here, as emacs_write may
1461 not validate p in that case. */
1462
1463 if (emacs_pipe (fd) == 0)
1464 {
1465 bool valid = emacs_write (fd[1], p, 16) == 16;
1466 emacs_close (fd[1]);
1467 emacs_close (fd[0]);
1468 return valid;
1469 }
1470
1471 return -1;
1472 #endif
1473 }
1474
1475 /* Return 2 if OBJ is a killed or special buffer object, 1 if OBJ is a
1476 valid lisp object, 0 if OBJ is NOT a valid lisp object, or -1 if we
1477 cannot validate OBJ. This function can be quite slow, so its primary
1478 use is the manual debugging. The only exception is print_object, where
1479 we use it to check whether the memory referenced by the pointer of
1480 Lisp_Save_Value object contains valid objects. */
1481
1482 int
1483 valid_lisp_object_p (Lisp_Object obj)
1484 {
1485 void *p;
1486
1487 if (INTEGERP (obj))
1488 return 1;
1489
1490 p = (void *) XPNTR (obj);
1491
1492 if (p == &buffer_defaults || p == &buffer_local_symbols)
1493 return 2;
1494
1495 return valid_pointer_p (p);
1496 }
1497
1498 /* If GC_MARK_STACK, return 1 if STR is a relocatable data of Lisp_String
1499 (i.e. there is a non-pure Lisp_Object X so that SDATA (X) == STR) and 0
1500 if not. Otherwise we can't rely on valid_lisp_object_p and return -1.
1501 This function is slow and should be used for debugging purposes. */
1502
1503 int
1504 relocatable_string_data_p (const char *str)
1505 {
1506 return -1;
1507 }
1508
1509 /***********************************************************************
1510 Pure Storage Compatibility Functions
1511 ***********************************************************************/
1512
1513 void
1514 check_pure_size (void)
1515 {
1516 return;
1517 }
1518
1519 Lisp_Object
1520 make_pure_string (const char *data,
1521 ptrdiff_t nchars, ptrdiff_t nbytes, bool multibyte)
1522 {
1523 return make_specified_string (data, nchars, nbytes, multibyte);
1524 }
1525
1526 Lisp_Object
1527 make_pure_c_string (const char *data, ptrdiff_t nchars)
1528 {
1529 return build_string (data);
1530 }
1531
1532 Lisp_Object
1533 pure_cons (Lisp_Object car, Lisp_Object cdr)
1534 {
1535 return Fcons (car, cdr);
1536 }
1537
1538 DEFUN ("purecopy", Fpurecopy, Spurecopy, 1, 1, 0,
1539 doc: /* Return OBJ. */)
1540 (register Lisp_Object obj)
1541 {
1542 return obj;
1543 }
1544 \f
1545 /***********************************************************************
1546 Protection from GC
1547 ***********************************************************************/
1548
1549 void
1550 staticpro (Lisp_Object *varaddress)
1551 {
1552 return;
1553 }
1554 \f
1555 DEFUN ("garbage-collect", Fgarbage_collect, Sgarbage_collect, 0, 0, "",
1556 doc: /* Reclaim storage for Lisp objects no longer needed.
1557 Garbage collection happens automatically if you cons more than
1558 `gc-cons-threshold' bytes of Lisp data since previous garbage collection.
1559 `garbage-collect' normally returns a list with info on amount of space in use,
1560 where each entry has the form (NAME SIZE USED FREE), where:
1561 - NAME is a symbol describing the kind of objects this entry represents,
1562 - SIZE is the number of bytes used by each one,
1563 - USED is the number of those objects that were found live in the heap,
1564 - FREE is the number of those objects that are not live but that Emacs
1565 keeps around for future allocations (maybe because it does not know how
1566 to return them to the OS).
1567 However, if there was overflow in pure space, `garbage-collect'
1568 returns nil, because real GC can't be done.
1569 See Info node `(elisp)Garbage Collection'. */)
1570 (void)
1571 {
1572 GC_gcollect ();
1573 return Qt;
1574 }
1575 \f
1576 #ifdef ENABLE_CHECKING
1577
1578 bool suppress_checking;
1579
1580 void
1581 die (const char *msg, const char *file, int line)
1582 {
1583 fprintf (stderr, "\r\n%s:%d: Emacs fatal error: assertion failed: %s\r\n",
1584 file, line, msg);
1585 terminate_due_to_signal (SIGABRT, INT_MAX);
1586 }
1587 #endif
1588 \f
1589 /* Initialization. */
1590
1591 void
1592 init_alloc_once (void)
1593 {
1594 /* Used to do Vpurify_flag = Qt here, but Qt isn't set up yet! */
1595
1596 init_strings ();
1597 init_vectors ();
1598
1599 refill_memory_reserve ();
1600 gc_cons_threshold = GC_DEFAULT_THRESHOLD;
1601 }
1602
1603 void
1604 init_alloc (void)
1605 {
1606 gcprolist = 0;
1607 Vgc_elapsed = make_float (0.0);
1608 gcs_done = 0;
1609
1610 #if USE_VALGRIND
1611 valgrind_p = RUNNING_ON_VALGRIND != 0;
1612 #endif
1613 }
1614
1615 void
1616 syms_of_alloc (void)
1617 {
1618 DEFVAR_INT ("gc-cons-threshold", gc_cons_threshold,
1619 doc: /* Number of bytes of consing between garbage collections.
1620 Garbage collection can happen automatically once this many bytes have been
1621 allocated since the last garbage collection. All data types count.
1622
1623 Garbage collection happens automatically only when `eval' is called.
1624
1625 By binding this temporarily to a large number, you can effectively
1626 prevent garbage collection during a part of the program.
1627 See also `gc-cons-percentage'. */);
1628
1629 DEFVAR_LISP ("gc-cons-percentage", Vgc_cons_percentage,
1630 doc: /* Portion of the heap used for allocation.
1631 Garbage collection can happen automatically once this portion of the heap
1632 has been allocated since the last garbage collection.
1633 If this portion is smaller than `gc-cons-threshold', this is ignored. */);
1634 Vgc_cons_percentage = make_float (0.1);
1635
1636 DEFVAR_INT ("pure-bytes-used", pure_bytes_used,
1637 doc: /* Number of bytes of shareable Lisp data allocated so far. */);
1638
1639 DEFVAR_LISP ("purify-flag", Vpurify_flag,
1640 doc: /* Non-nil means loading Lisp code in order to dump an executable.
1641 This means that certain objects should be allocated in shared (pure) space.
1642 It can also be set to a hash-table, in which case this table is used to
1643 do hash-consing of the objects allocated to pure space. */);
1644
1645 DEFVAR_BOOL ("garbage-collection-messages", garbage_collection_messages,
1646 doc: /* Non-nil means display messages at start and end of garbage collection. */);
1647 garbage_collection_messages = 0;
1648
1649 DEFVAR_LISP ("post-gc-hook", Vpost_gc_hook,
1650 doc: /* Hook run after garbage collection has finished. */);
1651 Vpost_gc_hook = Qnil;
1652 DEFSYM (Qpost_gc_hook, "post-gc-hook");
1653
1654 DEFVAR_LISP ("memory-signal-data", Vmemory_signal_data,
1655 doc: /* Precomputed `signal' argument for memory-full error. */);
1656 /* We build this in advance because if we wait until we need it, we might
1657 not be able to allocate the memory to hold it. */
1658 Vmemory_signal_data
1659 = listn (CONSTYPE_PURE, 2, Qerror,
1660 build_pure_c_string ("Memory exhausted--use M-x save-some-buffers then exit and restart Emacs"));
1661
1662 DEFVAR_LISP ("memory-full", Vmemory_full,
1663 doc: /* Non-nil means Emacs cannot get much more Lisp memory. */);
1664 Vmemory_full = Qnil;
1665
1666 DEFSYM (Qgc_cons_threshold, "gc-cons-threshold");
1667 DEFSYM (Qchar_table_extra_slots, "char-table-extra-slots");
1668
1669 DEFVAR_LISP ("gc-elapsed", Vgc_elapsed,
1670 doc: /* Accumulated time elapsed in garbage collections.
1671 The time is in seconds as a floating point value. */);
1672 DEFVAR_INT ("gcs-done", gcs_done,
1673 doc: /* Accumulated number of garbage collections done. */);
1674
1675 defsubr (&Scons);
1676 defsubr (&Slist);
1677 defsubr (&Svector);
1678 defsubr (&Sbool_vector);
1679 defsubr (&Smake_byte_code);
1680 defsubr (&Smake_list);
1681 defsubr (&Smake_vector);
1682 defsubr (&Smake_string);
1683 defsubr (&Smake_bool_vector);
1684 defsubr (&Smake_symbol);
1685 defsubr (&Smake_marker);
1686 defsubr (&Spurecopy);
1687 defsubr (&Sgarbage_collect);
1688 }
1689
1690 /* When compiled with GCC, GDB might say "No enum type named
1691 pvec_type" if we don't have at least one symbol with that type, and
1692 then xbacktrace could fail. Similarly for the other enums and
1693 their values. Some non-GCC compilers don't like these constructs. */
1694 #ifdef __GNUC__
1695 union
1696 {
1697 enum CHARTAB_SIZE_BITS CHARTAB_SIZE_BITS;
1698 enum CHAR_TABLE_STANDARD_SLOTS CHAR_TABLE_STANDARD_SLOTS;
1699 enum char_bits char_bits;
1700 enum CHECK_LISP_OBJECT_TYPE CHECK_LISP_OBJECT_TYPE;
1701 enum DEFAULT_HASH_SIZE DEFAULT_HASH_SIZE;
1702 enum Lisp_Bits Lisp_Bits;
1703 enum Lisp_Compiled Lisp_Compiled;
1704 enum maxargs maxargs;
1705 enum MAX_ALLOCA MAX_ALLOCA;
1706 enum More_Lisp_Bits More_Lisp_Bits;
1707 enum pvec_type pvec_type;
1708 } const EXTERNALLY_VISIBLE gdb_make_enums_visible = {0};
1709 #endif /* __GNUC__ */