aboutsummaryrefslogtreecommitdiffstats
path: root/src/alloc.c
diff options
context:
space:
mode:
authorKen Raeburn2015-11-01 01:42:21 -0400
committerKen Raeburn2015-11-01 01:42:21 -0400
commit39372e1a1032521be74575bb06f95a3898fbae30 (patch)
tree754bd242a23d2358ea116126fcb0a629947bd9ec /src/alloc.c
parent6a3121904d76e3b2f63007341d48c5c1af55de80 (diff)
parente11aaee266da52937a3a031cb108fe13f68958c3 (diff)
downloademacs-39372e1a1032521be74575bb06f95a3898fbae30.tar.gz
emacs-39372e1a1032521be74575bb06f95a3898fbae30.zip
merge from trunk
Diffstat (limited to 'src/alloc.c')
-rw-r--r--src/alloc.c2830
1 files changed, 1668 insertions, 1162 deletions
diff --git a/src/alloc.c b/src/alloc.c
index 5de7d384a49..685d48b8770 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -1,6 +1,6 @@
1/* Storage allocation and gc for GNU Emacs Lisp interpreter. 1/* Storage allocation and gc for GNU Emacs Lisp interpreter.
2 2
3Copyright (C) 1985-1986, 1988, 1993-1995, 1997-2013 Free Software 3Copyright (C) 1985-1986, 1988, 1993-1995, 1997-2015 Free Software
4Foundation, Inc. 4Foundation, Inc.
5 5
6This file is part of GNU Emacs. 6This file is part of GNU Emacs.
@@ -20,8 +20,6 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 20
21#include <config.h> 21#include <config.h>
22 22
23#define LISP_INLINE EXTERN_INLINE
24
25#include <stdio.h> 23#include <stdio.h>
26#include <limits.h> /* For CHAR_BIT. */ 24#include <limits.h> /* For CHAR_BIT. */
27 25
@@ -34,9 +32,10 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
34#endif 32#endif
35 33
36#include "lisp.h" 34#include "lisp.h"
37#include "process.h" 35#include "dispextern.h"
38#include "intervals.h" 36#include "intervals.h"
39#include "puresize.h" 37#include "puresize.h"
38#include "systime.h"
40#include "character.h" 39#include "character.h"
41#include "buffer.h" 40#include "buffer.h"
42#include "window.h" 41#include "window.h"
@@ -44,21 +43,41 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
44#include "frame.h" 43#include "frame.h"
45#include "blockinput.h" 44#include "blockinput.h"
46#include "termhooks.h" /* For struct terminal. */ 45#include "termhooks.h" /* For struct terminal. */
46#ifdef HAVE_WINDOW_SYSTEM
47#include TERM_HEADER
48#endif /* HAVE_WINDOW_SYSTEM */
47 49
48#include <verify.h> 50#include <verify.h>
51#include <execinfo.h> /* For backtrace. */
52
53#ifdef HAVE_LINUX_SYSINFO
54#include <sys/sysinfo.h>
55#endif
56
57#ifdef MSDOS
58#include "dosfns.h" /* For dos_memory_info. */
59#endif
49 60
50/* GC_CHECK_MARKED_OBJECTS means do sanity checks on allocated objects. 61#if (defined ENABLE_CHECKING \
51 Doable only if GC_MARK_STACK. */ 62 && defined HAVE_VALGRIND_VALGRIND_H \
52#if ! GC_MARK_STACK 63 && !defined USE_VALGRIND)
53# undef GC_CHECK_MARKED_OBJECTS 64# define USE_VALGRIND 1
54#endif 65#endif
55 66
67#if USE_VALGRIND
68#include <valgrind/valgrind.h>
69#include <valgrind/memcheck.h>
70static bool valgrind_p;
71#endif
72
73/* GC_CHECK_MARKED_OBJECTS means do sanity checks on allocated objects. */
74
56/* GC_MALLOC_CHECK defined means perform validity checks of malloc'd 75/* GC_MALLOC_CHECK defined means perform validity checks of malloc'd
57 memory. Can do this only if using gmalloc.c and if not checking 76 memory. Can do this only if using gmalloc.c and if not checking
58 marked objects. */ 77 marked objects. */
59 78
60#if (defined SYSTEM_MALLOC || defined DOUG_LEA_MALLOC \ 79#if (defined SYSTEM_MALLOC || defined DOUG_LEA_MALLOC \
61 || defined GC_CHECK_MARKED_OBJECTS) 80 || defined HYBRID_MALLOC || defined GC_CHECK_MARKED_OBJECTS)
62#undef GC_MALLOC_CHECK 81#undef GC_MALLOC_CHECK
63#endif 82#endif
64 83
@@ -161,11 +180,6 @@ static ptrdiff_t pure_size;
161 180
162static ptrdiff_t pure_bytes_used_before_overflow; 181static ptrdiff_t pure_bytes_used_before_overflow;
163 182
164/* True if P points into pure space. */
165
166#define PURE_POINTER_P(P) \
167 ((uintptr_t) (P) - (uintptr_t) purebeg <= pure_size)
168
169/* Index in pure at which next pure Lisp object will be allocated.. */ 183/* Index in pure at which next pure Lisp object will be allocated.. */
170 184
171static ptrdiff_t pure_bytes_used_lisp; 185static ptrdiff_t pure_bytes_used_lisp;
@@ -179,6 +193,35 @@ static ptrdiff_t pure_bytes_used_non_lisp;
179 193
180const char *pending_malloc_warning; 194const char *pending_malloc_warning;
181 195
196#if 0 /* Normally, pointer sanity only on request... */
197#ifdef ENABLE_CHECKING
198#define SUSPICIOUS_OBJECT_CHECKING 1
199#endif
200#endif
201
202/* ... but unconditionally use SUSPICIOUS_OBJECT_CHECKING while the GC
203 bug is unresolved. */
204#define SUSPICIOUS_OBJECT_CHECKING 1
205
206#ifdef SUSPICIOUS_OBJECT_CHECKING
207struct suspicious_free_record
208{
209 void *suspicious_object;
210 void *backtrace[128];
211};
212static void *suspicious_objects[32];
213static int suspicious_object_index;
214struct suspicious_free_record suspicious_free_history[64] EXTERNALLY_VISIBLE;
215static int suspicious_free_history_index;
216/* Find the first currently-monitored suspicious pointer in range
217 [begin,end) or NULL if no such pointer exists. */
218static void *find_suspicious_object_in_range (void *begin, void *end);
219static void detect_suspicious_free (void *ptr);
220#else
221# define find_suspicious_object_in_range(begin, end) NULL
222# define detect_suspicious_free(ptr) (void)
223#endif
224
182/* Maximum amount of C stack to save when a GC happens. */ 225/* Maximum amount of C stack to save when a GC happens. */
183 226
184#ifndef MAX_SAVE_STACK 227#ifndef MAX_SAVE_STACK
@@ -190,31 +233,34 @@ const char *pending_malloc_warning;
190#if MAX_SAVE_STACK > 0 233#if MAX_SAVE_STACK > 0
191static char *stack_copy; 234static char *stack_copy;
192static ptrdiff_t stack_copy_size; 235static ptrdiff_t stack_copy_size;
193#endif
194 236
195static Lisp_Object Qconses; 237/* Copy to DEST a block of memory from SRC of size SIZE bytes,
196static Lisp_Object Qsymbols; 238 avoiding any address sanitization. */
197static Lisp_Object Qmiscs;
198static Lisp_Object Qstrings;
199static Lisp_Object Qvectors;
200static Lisp_Object Qfloats;
201static Lisp_Object Qintervals;
202static Lisp_Object Qbuffers;
203static Lisp_Object Qstring_bytes, Qvector_slots, Qheap;
204static Lisp_Object Qgc_cons_threshold;
205Lisp_Object Qautomatic_gc;
206Lisp_Object Qchar_table_extra_slots;
207 239
208/* Hook run after GC has finished. */ 240static void * ATTRIBUTE_NO_SANITIZE_ADDRESS
241no_sanitize_memcpy (void *dest, void const *src, size_t size)
242{
243 if (! ADDRESS_SANITIZER)
244 return memcpy (dest, src, size);
245 else
246 {
247 size_t i;
248 char *d = dest;
249 char const *s = src;
250 for (i = 0; i < size; i++)
251 d[i] = s[i];
252 return dest;
253 }
254}
209 255
210static Lisp_Object Qpost_gc_hook; 256#endif /* MAX_SAVE_STACK > 0 */
211 257
212static void mark_terminals (void); 258static void mark_terminals (void);
213static void gc_sweep (void); 259static void gc_sweep (void);
214static Lisp_Object make_pure_vector (ptrdiff_t); 260static Lisp_Object make_pure_vector (ptrdiff_t);
215static void mark_buffer (struct buffer *); 261static void mark_buffer (struct buffer *);
216 262
217#if !defined REL_ALLOC || defined SYSTEM_MALLOC 263#if !defined REL_ALLOC || defined SYSTEM_MALLOC || defined HYBRID_MALLOC
218static void refill_memory_reserve (void); 264static void refill_memory_reserve (void);
219#endif 265#endif
220static void compact_small_strings (void); 266static void compact_small_strings (void);
@@ -244,8 +290,6 @@ enum mem_type
244 MEM_TYPE_SPARE 290 MEM_TYPE_SPARE
245}; 291};
246 292
247#if GC_MARK_STACK || defined GC_MALLOC_CHECK
248
249/* A unique object in pure space used to make some Lisp objects 293/* A unique object in pure space used to make some Lisp objects
250 on free lists recognizable in O(1). */ 294 on free lists recognizable in O(1). */
251 295
@@ -322,8 +366,6 @@ static void mem_delete (struct mem_node *);
322static void mem_delete_fixup (struct mem_node *); 366static void mem_delete_fixup (struct mem_node *);
323static struct mem_node *mem_find (void *); 367static struct mem_node *mem_find (void *);
324 368
325#endif /* GC_MARK_STACK || GC_MALLOC_CHECK */
326
327#ifndef DEADP 369#ifndef DEADP
328# define DEADP(x) 0 370# define DEADP(x) 0
329#endif 371#endif
@@ -340,13 +382,43 @@ static int staticidx;
340 382
341static void *pure_alloc (size_t, int); 383static void *pure_alloc (size_t, int);
342 384
385/* Return X rounded to the next multiple of Y. Arguments should not
386 have side effects, as they are evaluated more than once. Assume X
387 + Y - 1 does not overflow. Tune for Y being a power of 2. */
388
389#define ROUNDUP(x, y) ((y) & ((y) - 1) \
390 ? ((x) + (y) - 1) - ((x) + (y) - 1) % (y) \
391 : ((x) + (y) - 1) & ~ ((y) - 1))
343 392
344/* Value is SZ rounded up to the next multiple of ALIGNMENT. 393/* Return PTR rounded up to the next multiple of ALIGNMENT. */
345 ALIGNMENT must be a power of 2. */ 394
395static void *
396ALIGN (void *ptr, int alignment)
397{
398 return (void *) ROUNDUP ((uintptr_t) ptr, alignment);
399}
346 400
347#define ALIGN(ptr, ALIGNMENT) \ 401/* Extract the pointer hidden within A, if A is not a symbol.
348 ((void *) (((uintptr_t) (ptr) + (ALIGNMENT) - 1) \ 402 If A is a symbol, extract the hidden pointer's offset from lispsym,
349 & ~ ((ALIGNMENT) - 1))) 403 converted to void *. */
404
405static void *
406XPNTR_OR_SYMBOL_OFFSET (Lisp_Object a)
407{
408 intptr_t i = USE_LSB_TAG ? XLI (a) - XTYPE (a) : XLI (a) & VALMASK;
409 return (void *) i;
410}
411
412/* Extract the pointer hidden within A. */
413
414static void *
415XPNTR (Lisp_Object a)
416{
417 void *p = XPNTR_OR_SYMBOL_OFFSET (a);
418 if (SYMBOLP (a))
419 p = (intptr_t) p + (char *) lispsym;
420 return p;
421}
350 422
351static void 423static void
352XFLOAT_INIT (Lisp_Object f, double n) 424XFLOAT_INIT (Lisp_Object f, double n)
@@ -354,6 +426,32 @@ XFLOAT_INIT (Lisp_Object f, double n)
354 XFLOAT (f)->u.data = n; 426 XFLOAT (f)->u.data = n;
355} 427}
356 428
429static bool
430pointers_fit_in_lispobj_p (void)
431{
432 return (UINTPTR_MAX <= VAL_MAX) || USE_LSB_TAG;
433}
434
435static bool
436mmap_lisp_allowed_p (void)
437{
438 /* If we can't store all memory addresses in our lisp objects, it's
439 risky to let the heap use mmap and give us addresses from all
440 over our address space. We also can't use mmap for lisp objects
441 if we might dump: unexec doesn't preserve the contents of mmapped
442 regions. */
443 return pointers_fit_in_lispobj_p () && !might_dump;
444}
445
446/* Head of a circularly-linked list of extant finalizers. */
447static struct Lisp_Finalizer finalizers;
448
449/* Head of a circularly-linked list of finalizers that must be invoked
450 because we deemed them unreachable. This list must be global, and
451 not a local inside garbage_collect_1, in case we GC again while
452 running finalizers. */
453static struct Lisp_Finalizer doomed_finalizers;
454
357 455
358/************************************************************************ 456/************************************************************************
359 Malloc 457 Malloc
@@ -430,15 +528,10 @@ buffer_memory_full (ptrdiff_t nbytes)
430/* Define XMALLOC_OVERRUN_SIZE_SIZE so that (1) it's large enough to 528/* Define XMALLOC_OVERRUN_SIZE_SIZE so that (1) it's large enough to
431 hold a size_t value and (2) the header size is a multiple of the 529 hold a size_t value and (2) the header size is a multiple of the
432 alignment that Emacs needs for C types and for USE_LSB_TAG. */ 530 alignment that Emacs needs for C types and for USE_LSB_TAG. */
433#define XMALLOC_BASE_ALIGNMENT \ 531#define XMALLOC_BASE_ALIGNMENT alignof (max_align_t)
434 alignof (union { long double d; intmax_t i; void *p; })
435 532
436#if USE_LSB_TAG 533#define XMALLOC_HEADER_ALIGNMENT \
437# define XMALLOC_HEADER_ALIGNMENT \ 534 COMMON_MULTIPLE (GCALIGNMENT, XMALLOC_BASE_ALIGNMENT)
438 COMMON_MULTIPLE (GCALIGNMENT, XMALLOC_BASE_ALIGNMENT)
439#else
440# define XMALLOC_HEADER_ALIGNMENT XMALLOC_BASE_ALIGNMENT
441#endif
442#define XMALLOC_OVERRUN_SIZE_SIZE \ 535#define XMALLOC_OVERRUN_SIZE_SIZE \
443 (((XMALLOC_OVERRUN_CHECK_SIZE + sizeof (size_t) \ 536 (((XMALLOC_OVERRUN_CHECK_SIZE + sizeof (size_t) \
444 + XMALLOC_HEADER_ALIGNMENT - 1) \ 537 + XMALLOC_HEADER_ALIGNMENT - 1) \
@@ -801,6 +894,20 @@ xlispstrdup (Lisp_Object string)
801 return memcpy (xmalloc (size), SSDATA (string), size); 894 return memcpy (xmalloc (size), SSDATA (string), size);
802} 895}
803 896
897/* Assign to *PTR a copy of STRING, freeing any storage *PTR formerly
898 pointed to. If STRING is null, assign it without copying anything.
899 Allocate before freeing, to avoid a dangling pointer if allocation
900 fails. */
901
902void
903dupstring (char **ptr, char const *string)
904{
905 char *old = *ptr;
906 *ptr = string ? xstrdup (string) : 0;
907 xfree (old);
908}
909
910
804/* Like putenv, but (1) use the equivalent of xmalloc and (2) the 911/* Like putenv, but (1) use the equivalent of xmalloc and (2) the
805 argument is a const pointer. */ 912 argument is a const pointer. */
806 913
@@ -860,7 +967,7 @@ lisp_malloc (size_t nbytes, enum mem_type type)
860 } 967 }
861#endif 968#endif
862 969
863#if GC_MARK_STACK && !defined GC_MALLOC_CHECK 970#ifndef GC_MALLOC_CHECK
864 if (val && type != MEM_TYPE_NON_LISP) 971 if (val && type != MEM_TYPE_NON_LISP)
865 mem_insert (val, (char *) val + nbytes, type); 972 mem_insert (val, (char *) val + nbytes, type);
866#endif 973#endif
@@ -880,7 +987,7 @@ lisp_free (void *block)
880{ 987{
881 MALLOC_BLOCK_INPUT; 988 MALLOC_BLOCK_INPUT;
882 free (block); 989 free (block);
883#if GC_MARK_STACK && !defined GC_MALLOC_CHECK 990#ifndef GC_MALLOC_CHECK
884 mem_delete (mem_find (block)); 991 mem_delete (mem_find (block));
885#endif 992#endif
886 MALLOC_UNBLOCK_INPUT; 993 MALLOC_UNBLOCK_INPUT;
@@ -891,8 +998,33 @@ lisp_free (void *block)
891/* The entry point is lisp_align_malloc which returns blocks of at most 998/* The entry point is lisp_align_malloc which returns blocks of at most
892 BLOCK_BYTES and guarantees they are aligned on a BLOCK_ALIGN boundary. */ 999 BLOCK_BYTES and guarantees they are aligned on a BLOCK_ALIGN boundary. */
893 1000
894#if defined (HAVE_POSIX_MEMALIGN) && defined (SYSTEM_MALLOC) 1001/* Use aligned_alloc if it or a simple substitute is available.
895#define USE_POSIX_MEMALIGN 1 1002 Address sanitization breaks aligned allocation, as of gcc 4.8.2 and
1003 clang 3.3 anyway. */
1004
1005#if ! ADDRESS_SANITIZER
1006# if !defined SYSTEM_MALLOC && !defined DOUG_LEA_MALLOC && !defined HYBRID_MALLOC
1007# define USE_ALIGNED_ALLOC 1
1008/* Defined in gmalloc.c. */
1009void *aligned_alloc (size_t, size_t);
1010# elif defined HYBRID_MALLOC
1011# if defined ALIGNED_ALLOC || defined HAVE_POSIX_MEMALIGN
1012# define USE_ALIGNED_ALLOC 1
1013# define aligned_alloc hybrid_aligned_alloc
1014/* Defined in gmalloc.c. */
1015void *aligned_alloc (size_t, size_t);
1016# endif
1017# elif defined HAVE_ALIGNED_ALLOC
1018# define USE_ALIGNED_ALLOC 1
1019# elif defined HAVE_POSIX_MEMALIGN
1020# define USE_ALIGNED_ALLOC 1
1021static void *
1022aligned_alloc (size_t alignment, size_t size)
1023{
1024 void *p;
1025 return posix_memalign (&p, alignment, size) == 0 ? p : 0;
1026}
1027# endif
896#endif 1028#endif
897 1029
898/* BLOCK_ALIGN has to be a power of 2. */ 1030/* BLOCK_ALIGN has to be a power of 2. */
@@ -902,7 +1034,7 @@ lisp_free (void *block)
902 malloc a chance to minimize the amount of memory wasted to alignment. 1034 malloc a chance to minimize the amount of memory wasted to alignment.
903 It should be tuned to the particular malloc library used. 1035 It should be tuned to the particular malloc library used.
904 On glibc-2.3.2, malloc never tries to align, so a padding of 0 is best. 1036 On glibc-2.3.2, malloc never tries to align, so a padding of 0 is best.
905 posix_memalign on the other hand would ideally prefer a value of 4 1037 aligned_alloc on the other hand would ideally prefer a value of 4
906 because otherwise, there's 1020 bytes wasted between each ablocks. 1038 because otherwise, there's 1020 bytes wasted between each ablocks.
907 In Emacs, testing shows that those 1020 can most of the time be 1039 In Emacs, testing shows that those 1020 can most of the time be
908 efficiently used by malloc to place other objects, so a value of 0 can 1040 efficiently used by malloc to place other objects, so a value of 0 can
@@ -947,7 +1079,7 @@ struct ablocks
947 struct ablock blocks[ABLOCKS_SIZE]; 1079 struct ablock blocks[ABLOCKS_SIZE];
948}; 1080};
949 1081
950/* Size of the block requested from malloc or posix_memalign. */ 1082/* Size of the block requested from malloc or aligned_alloc. */
951#define ABLOCKS_BYTES (sizeof (struct ablocks) - BLOCK_PADDING) 1083#define ABLOCKS_BYTES (sizeof (struct ablocks) - BLOCK_PADDING)
952 1084
953#define ABLOCK_ABASE(block) \ 1085#define ABLOCK_ABASE(block) \
@@ -959,11 +1091,11 @@ struct ablocks
959#define ABLOCKS_BUSY(abase) ((abase)->blocks[0].abase) 1091#define ABLOCKS_BUSY(abase) ((abase)->blocks[0].abase)
960 1092
961/* Pointer to the (not necessarily aligned) malloc block. */ 1093/* Pointer to the (not necessarily aligned) malloc block. */
962#ifdef USE_POSIX_MEMALIGN 1094#ifdef USE_ALIGNED_ALLOC
963#define ABLOCKS_BASE(abase) (abase) 1095#define ABLOCKS_BASE(abase) (abase)
964#else 1096#else
965#define ABLOCKS_BASE(abase) \ 1097#define ABLOCKS_BASE(abase) \
966 (1 & (intptr_t) ABLOCKS_BUSY (abase) ? abase : ((void**)abase)[-1]) 1098 (1 & (intptr_t) ABLOCKS_BUSY (abase) ? abase : ((void **)abase)[-1])
967#endif 1099#endif
968 1100
969/* The list of free ablock. */ 1101/* The list of free ablock. */
@@ -992,19 +1124,12 @@ lisp_align_malloc (size_t nbytes, enum mem_type type)
992 intptr_t aligned; /* int gets warning casting to 64-bit pointer. */ 1124 intptr_t aligned; /* int gets warning casting to 64-bit pointer. */
993 1125
994#ifdef DOUG_LEA_MALLOC 1126#ifdef DOUG_LEA_MALLOC
995 /* Prevent mmap'ing the chunk. Lisp data may not be mmap'ed 1127 if (!mmap_lisp_allowed_p ())
996 because mapped region contents are not preserved in 1128 mallopt (M_MMAP_MAX, 0);
997 a dumped Emacs. */
998 mallopt (M_MMAP_MAX, 0);
999#endif 1129#endif
1000 1130
1001#ifdef USE_POSIX_MEMALIGN 1131#ifdef USE_ALIGNED_ALLOC
1002 { 1132 abase = base = aligned_alloc (BLOCK_ALIGN, ABLOCKS_BYTES);
1003 int err = posix_memalign (&base, BLOCK_ALIGN, ABLOCKS_BYTES);
1004 if (err)
1005 base = NULL;
1006 abase = base;
1007 }
1008#else 1133#else
1009 base = malloc (ABLOCKS_BYTES); 1134 base = malloc (ABLOCKS_BYTES);
1010 abase = ALIGN (base, BLOCK_ALIGN); 1135 abase = ALIGN (base, BLOCK_ALIGN);
@@ -1018,11 +1143,11 @@ lisp_align_malloc (size_t nbytes, enum mem_type type)
1018 1143
1019 aligned = (base == abase); 1144 aligned = (base == abase);
1020 if (!aligned) 1145 if (!aligned)
1021 ((void**)abase)[-1] = base; 1146 ((void **) abase)[-1] = base;
1022 1147
1023#ifdef DOUG_LEA_MALLOC 1148#ifdef DOUG_LEA_MALLOC
1024 /* Back to a reasonable maximum of mmap'ed areas. */ 1149 if (!mmap_lisp_allowed_p ())
1025 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS); 1150 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS);
1026#endif 1151#endif
1027 1152
1028#if ! USE_LSB_TAG 1153#if ! USE_LSB_TAG
@@ -1062,12 +1187,12 @@ lisp_align_malloc (size_t nbytes, enum mem_type type)
1062 } 1187 }
1063 1188
1064 abase = ABLOCK_ABASE (free_ablock); 1189 abase = ABLOCK_ABASE (free_ablock);
1065 ABLOCKS_BUSY (abase) = 1190 ABLOCKS_BUSY (abase)
1066 (struct ablocks *) (2 + (intptr_t) ABLOCKS_BUSY (abase)); 1191 = (struct ablocks *) (2 + (intptr_t) ABLOCKS_BUSY (abase));
1067 val = free_ablock; 1192 val = free_ablock;
1068 free_ablock = free_ablock->x.next_free; 1193 free_ablock = free_ablock->x.next_free;
1069 1194
1070#if GC_MARK_STACK && !defined GC_MALLOC_CHECK 1195#ifndef GC_MALLOC_CHECK
1071 if (type != MEM_TYPE_NON_LISP) 1196 if (type != MEM_TYPE_NON_LISP)
1072 mem_insert (val, (char *) val + nbytes, type); 1197 mem_insert (val, (char *) val + nbytes, type);
1073#endif 1198#endif
@@ -1087,7 +1212,7 @@ lisp_align_free (void *block)
1087 struct ablocks *abase = ABLOCK_ABASE (ablock); 1212 struct ablocks *abase = ABLOCK_ABASE (ablock);
1088 1213
1089 MALLOC_BLOCK_INPUT; 1214 MALLOC_BLOCK_INPUT;
1090#if GC_MARK_STACK && !defined GC_MALLOC_CHECK 1215#ifndef GC_MALLOC_CHECK
1091 mem_delete (mem_find (block)); 1216 mem_delete (mem_find (block));
1092#endif 1217#endif
1093 /* Put on free list. */ 1218 /* Put on free list. */
@@ -1259,28 +1384,32 @@ mark_interval (register INTERVAL i, Lisp_Object dummy)
1259 1384
1260#define LARGE_STRING_BYTES 1024 1385#define LARGE_STRING_BYTES 1024
1261 1386
1262/* Struct or union describing string memory sub-allocated from an sblock. 1387/* The SDATA typedef is a struct or union describing string memory
1263 This is where the contents of Lisp strings are stored. */ 1388 sub-allocated from an sblock. This is where the contents of Lisp
1264 1389 strings are stored. */
1265#ifdef GC_CHECK_STRING_BYTES
1266 1390
1267typedef struct 1391struct sdata
1268{ 1392{
1269 /* Back-pointer to the string this sdata belongs to. If null, this 1393 /* Back-pointer to the string this sdata belongs to. If null, this
1270 structure is free, and the NBYTES member of the union below 1394 structure is free, and NBYTES (in this structure or in the union below)
1271 contains the string's byte size (the same value that STRING_BYTES 1395 contains the string's byte size (the same value that STRING_BYTES
1272 would return if STRING were non-null). If non-null, STRING_BYTES 1396 would return if STRING were non-null). If non-null, STRING_BYTES
1273 (STRING) is the size of the data, and DATA contains the string's 1397 (STRING) is the size of the data, and DATA contains the string's
1274 contents. */ 1398 contents. */
1275 struct Lisp_String *string; 1399 struct Lisp_String *string;
1276 1400
1401#ifdef GC_CHECK_STRING_BYTES
1277 ptrdiff_t nbytes; 1402 ptrdiff_t nbytes;
1403#endif
1404
1278 unsigned char data[FLEXIBLE_ARRAY_MEMBER]; 1405 unsigned char data[FLEXIBLE_ARRAY_MEMBER];
1279} sdata; 1406};
1407
1408#ifdef GC_CHECK_STRING_BYTES
1280 1409
1410typedef struct sdata sdata;
1281#define SDATA_NBYTES(S) (S)->nbytes 1411#define SDATA_NBYTES(S) (S)->nbytes
1282#define SDATA_DATA(S) (S)->data 1412#define SDATA_DATA(S) (S)->data
1283#define SDATA_SELECTOR(member) member
1284 1413
1285#else 1414#else
1286 1415
@@ -1288,12 +1417,16 @@ typedef union
1288{ 1417{
1289 struct Lisp_String *string; 1418 struct Lisp_String *string;
1290 1419
1291 /* When STRING is non-null. */ 1420 /* When STRING is nonnull, this union is actually of type 'struct sdata',
1292 struct 1421 which has a flexible array member. However, if implemented by
1293 { 1422 giving this union a member of type 'struct sdata', the union
1294 struct Lisp_String *string; 1423 could not be the last (flexible) member of 'struct sblock',
1295 unsigned char data[FLEXIBLE_ARRAY_MEMBER]; 1424 because C99 prohibits a flexible array member from having a type
1296 } u; 1425 that is itself a flexible array. So, comment this member out here,
1426 but remember that the option's there when using this union. */
1427#if 0
1428 struct sdata u;
1429#endif
1297 1430
1298 /* When STRING is null. */ 1431 /* When STRING is null. */
1299 struct 1432 struct
@@ -1304,13 +1437,11 @@ typedef union
1304} sdata; 1437} sdata;
1305 1438
1306#define SDATA_NBYTES(S) (S)->n.nbytes 1439#define SDATA_NBYTES(S) (S)->n.nbytes
1307#define SDATA_DATA(S) (S)->u.data 1440#define SDATA_DATA(S) ((struct sdata *) (S))->data
1308#define SDATA_SELECTOR(member) u.member
1309 1441
1310#endif /* not GC_CHECK_STRING_BYTES */ 1442#endif /* not GC_CHECK_STRING_BYTES */
1311 1443
1312#define SDATA_DATA_OFFSET offsetof (sdata, SDATA_SELECTOR (data)) 1444enum { SDATA_DATA_OFFSET = offsetof (struct sdata, data) };
1313
1314 1445
1315/* Structure describing a block of memory which is sub-allocated to 1446/* Structure describing a block of memory which is sub-allocated to
1316 obtain string data memory for strings. Blocks for small strings 1447 obtain string data memory for strings. Blocks for small strings
@@ -1326,8 +1457,8 @@ struct sblock
1326 of the sblock if there isn't any space left in this block. */ 1457 of the sblock if there isn't any space left in this block. */
1327 sdata *next_free; 1458 sdata *next_free;
1328 1459
1329 /* Start of data. */ 1460 /* String data. */
1330 sdata first_data; 1461 sdata data[FLEXIBLE_ARRAY_MEMBER];
1331}; 1462};
1332 1463
1333/* Number of Lisp strings in a string_block structure. The 1020 is 1464/* Number of Lisp strings in a string_block structure. The 1020 is
@@ -1443,7 +1574,7 @@ static ptrdiff_t const STRING_BYTES_MAX =
1443 min (STRING_BYTES_BOUND, 1574 min (STRING_BYTES_BOUND,
1444 ((SIZE_MAX - XMALLOC_OVERRUN_CHECK_OVERHEAD 1575 ((SIZE_MAX - XMALLOC_OVERRUN_CHECK_OVERHEAD
1445 - GC_STRING_EXTRA 1576 - GC_STRING_EXTRA
1446 - offsetof (struct sblock, first_data) 1577 - offsetof (struct sblock, data)
1447 - SDATA_DATA_OFFSET) 1578 - SDATA_DATA_OFFSET)
1448 & ~(sizeof (EMACS_INT) - 1))); 1579 & ~(sizeof (EMACS_INT) - 1)));
1449 1580
@@ -1470,9 +1601,7 @@ string_bytes (struct Lisp_String *s)
1470 ptrdiff_t nbytes = 1601 ptrdiff_t nbytes =
1471 (s->size_byte < 0 ? s->size & ~ARRAY_MARK_FLAG : s->size_byte); 1602 (s->size_byte < 0 ? s->size & ~ARRAY_MARK_FLAG : s->size_byte);
1472 1603
1473 if (!PURE_POINTER_P (s) 1604 if (!PURE_P (s) && s->data && nbytes != SDATA_NBYTES (SDATA_OF_STRING (s)))
1474 && s->data
1475 && nbytes != SDATA_NBYTES (SDATA_OF_STRING (s)))
1476 emacs_abort (); 1605 emacs_abort ();
1477 return nbytes; 1606 return nbytes;
1478} 1607}
@@ -1486,7 +1615,7 @@ check_sblock (struct sblock *b)
1486 1615
1487 end = b->next_free; 1616 end = b->next_free;
1488 1617
1489 for (from = &b->first_data; from < end; from = from_end) 1618 for (from = b->data; from < end; from = from_end)
1490 { 1619 {
1491 /* Compute the next FROM here because copying below may 1620 /* Compute the next FROM here because copying below may
1492 overwrite data we need to compute it. */ 1621 overwrite data we need to compute it. */
@@ -1514,7 +1643,7 @@ check_string_bytes (bool all_p)
1514 1643
1515 for (b = large_sblocks; b; b = b->next) 1644 for (b = large_sblocks; b; b = b->next)
1516 { 1645 {
1517 struct Lisp_String *s = b->first_data.string; 1646 struct Lisp_String *s = b->data[0].string;
1518 if (s) 1647 if (s)
1519 string_bytes (s); 1648 string_bytes (s);
1520 } 1649 }
@@ -1648,30 +1777,22 @@ allocate_string_data (struct Lisp_String *s,
1648 1777
1649 if (nbytes > LARGE_STRING_BYTES) 1778 if (nbytes > LARGE_STRING_BYTES)
1650 { 1779 {
1651 size_t size = offsetof (struct sblock, first_data) + needed; 1780 size_t size = offsetof (struct sblock, data) + needed;
1652 1781
1653#ifdef DOUG_LEA_MALLOC 1782#ifdef DOUG_LEA_MALLOC
1654 /* Prevent mmap'ing the chunk. Lisp data may not be mmap'ed 1783 if (!mmap_lisp_allowed_p ())
1655 because mapped region contents are not preserved in 1784 mallopt (M_MMAP_MAX, 0);
1656 a dumped Emacs.
1657
1658 In case you think of allowing it in a dumped Emacs at the
1659 cost of not being able to re-dump, there's another reason:
1660 mmap'ed data typically have an address towards the top of the
1661 address space, which won't fit into an EMACS_INT (at least on
1662 32-bit systems with the current tagging scheme). --fx */
1663 mallopt (M_MMAP_MAX, 0);
1664#endif 1785#endif
1665 1786
1666 b = lisp_malloc (size + GC_STRING_EXTRA, MEM_TYPE_NON_LISP); 1787 b = lisp_malloc (size + GC_STRING_EXTRA, MEM_TYPE_NON_LISP);
1667 1788
1668#ifdef DOUG_LEA_MALLOC 1789#ifdef DOUG_LEA_MALLOC
1669 /* Back to a reasonable maximum of mmap'ed areas. */ 1790 if (!mmap_lisp_allowed_p ())
1670 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS); 1791 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS);
1671#endif 1792#endif
1672 1793
1673 b->next_free = &b->first_data; 1794 b->next_free = b->data;
1674 b->first_data.string = NULL; 1795 b->data[0].string = NULL;
1675 b->next = large_sblocks; 1796 b->next = large_sblocks;
1676 large_sblocks = b; 1797 large_sblocks = b;
1677 } 1798 }
@@ -1682,8 +1803,8 @@ allocate_string_data (struct Lisp_String *s,
1682 { 1803 {
1683 /* Not enough room in the current sblock. */ 1804 /* Not enough room in the current sblock. */
1684 b = lisp_malloc (SBLOCK_SIZE, MEM_TYPE_NON_LISP); 1805 b = lisp_malloc (SBLOCK_SIZE, MEM_TYPE_NON_LISP);
1685 b->next_free = &b->first_data; 1806 b->next_free = b->data;
1686 b->first_data.string = NULL; 1807 b->data[0].string = NULL;
1687 b->next = NULL; 1808 b->next = NULL;
1688 1809
1689 if (current_sblock) 1810 if (current_sblock)
@@ -1728,6 +1849,7 @@ allocate_string_data (struct Lisp_String *s,
1728 1849
1729/* Sweep and compact strings. */ 1850/* Sweep and compact strings. */
1730 1851
1852NO_INLINE /* For better stack traces */
1731static void 1853static void
1732sweep_strings (void) 1854sweep_strings (void)
1733{ 1855{
@@ -1837,7 +1959,7 @@ free_large_strings (void)
1837 { 1959 {
1838 next = b->next; 1960 next = b->next;
1839 1961
1840 if (b->first_data.string == NULL) 1962 if (b->data[0].string == NULL)
1841 lisp_free (b); 1963 lisp_free (b);
1842 else 1964 else
1843 { 1965 {
@@ -1864,7 +1986,7 @@ compact_small_strings (void)
1864 to, and TB_END is the end of TB. */ 1986 to, and TB_END is the end of TB. */
1865 tb = oldest_sblock; 1987 tb = oldest_sblock;
1866 tb_end = (sdata *) ((char *) tb + SBLOCK_SIZE); 1988 tb_end = (sdata *) ((char *) tb + SBLOCK_SIZE);
1867 to = &tb->first_data; 1989 to = tb->data;
1868 1990
1869 /* Step through the blocks from the oldest to the youngest. We 1991 /* Step through the blocks from the oldest to the youngest. We
1870 expect that old blocks will stabilize over time, so that less 1992 expect that old blocks will stabilize over time, so that less
@@ -1874,7 +1996,7 @@ compact_small_strings (void)
1874 end = b->next_free; 1996 end = b->next_free;
1875 eassert ((char *) end <= (char *) b + SBLOCK_SIZE); 1997 eassert ((char *) end <= (char *) b + SBLOCK_SIZE);
1876 1998
1877 for (from = &b->first_data; from < end; from = from_end) 1999 for (from = b->data; from < end; from = from_end)
1878 { 2000 {
1879 /* Compute the next FROM here because copying below may 2001 /* Compute the next FROM here because copying below may
1880 overwrite data we need to compute it. */ 2002 overwrite data we need to compute it. */
@@ -1911,7 +2033,7 @@ compact_small_strings (void)
1911 tb->next_free = to; 2033 tb->next_free = to;
1912 tb = tb->next; 2034 tb = tb->next;
1913 tb_end = (sdata *) ((char *) tb + SBLOCK_SIZE); 2035 tb_end = (sdata *) ((char *) tb + SBLOCK_SIZE);
1914 to = &tb->first_data; 2036 to = tb->data;
1915 to_end = (sdata *) ((char *) to + nbytes + GC_STRING_EXTRA); 2037 to_end = (sdata *) ((char *) to + nbytes + GC_STRING_EXTRA);
1916 } 2038 }
1917 2039
@@ -1955,7 +2077,6 @@ INIT must be an integer that represents a character. */)
1955 (Lisp_Object length, Lisp_Object init) 2077 (Lisp_Object length, Lisp_Object init)
1956{ 2078{
1957 register Lisp_Object val; 2079 register Lisp_Object val;
1958 register unsigned char *p, *end;
1959 int c; 2080 int c;
1960 EMACS_INT nbytes; 2081 EMACS_INT nbytes;
1961 2082
@@ -1967,77 +2088,110 @@ INIT must be an integer that represents a character. */)
1967 { 2088 {
1968 nbytes = XINT (length); 2089 nbytes = XINT (length);
1969 val = make_uninit_string (nbytes); 2090 val = make_uninit_string (nbytes);
1970 p = SDATA (val); 2091 memset (SDATA (val), c, nbytes);
1971 end = p + SCHARS (val); 2092 SDATA (val)[nbytes] = 0;
1972 while (p != end)
1973 *p++ = c;
1974 } 2093 }
1975 else 2094 else
1976 { 2095 {
1977 unsigned char str[MAX_MULTIBYTE_LENGTH]; 2096 unsigned char str[MAX_MULTIBYTE_LENGTH];
1978 int len = CHAR_STRING (c, str); 2097 ptrdiff_t len = CHAR_STRING (c, str);
1979 EMACS_INT string_len = XINT (length); 2098 EMACS_INT string_len = XINT (length);
2099 unsigned char *p, *beg, *end;
1980 2100
1981 if (string_len > STRING_BYTES_MAX / len) 2101 if (string_len > STRING_BYTES_MAX / len)
1982 string_overflow (); 2102 string_overflow ();
1983 nbytes = len * string_len; 2103 nbytes = len * string_len;
1984 val = make_uninit_multibyte_string (string_len, nbytes); 2104 val = make_uninit_multibyte_string (string_len, nbytes);
1985 p = SDATA (val); 2105 for (beg = SDATA (val), p = beg, end = beg + nbytes; p < end; p += len)
1986 end = p + nbytes;
1987 while (p != end)
1988 { 2106 {
1989 memcpy (p, str, len); 2107 /* First time we just copy `str' to the data of `val'. */
1990 p += len; 2108 if (p == beg)
2109 memcpy (p, str, len);
2110 else
2111 {
2112 /* Next time we copy largest possible chunk from
2113 initialized to uninitialized part of `val'. */
2114 len = min (p - beg, end - p);
2115 memcpy (p, beg, len);
2116 }
1991 } 2117 }
2118 *p = 0;
1992 } 2119 }
1993 2120
1994 *p = 0;
1995 return val; 2121 return val;
1996} 2122}
1997 2123
2124/* Fill A with 1 bits if INIT is non-nil, and with 0 bits otherwise.
2125 Return A. */
1998 2126
1999DEFUN ("make-bool-vector", Fmake_bool_vector, Smake_bool_vector, 2, 2, 0, 2127Lisp_Object
2000 doc: /* Return a new bool-vector of length LENGTH, using INIT for each element. 2128bool_vector_fill (Lisp_Object a, Lisp_Object init)
2001LENGTH must be a number. INIT matters only in whether it is t or nil. */)
2002 (Lisp_Object length, Lisp_Object init)
2003{ 2129{
2004 register Lisp_Object val; 2130 EMACS_INT nbits = bool_vector_size (a);
2005 struct Lisp_Bool_Vector *p; 2131 if (0 < nbits)
2006 ptrdiff_t length_in_chars; 2132 {
2007 EMACS_INT length_in_elts; 2133 unsigned char *data = bool_vector_uchar_data (a);
2008 int bits_per_value; 2134 int pattern = NILP (init) ? 0 : (1 << BOOL_VECTOR_BITS_PER_CHAR) - 1;
2009 int extra_bool_elts = ((bool_header_size - header_size + word_size - 1) 2135 ptrdiff_t nbytes = bool_vector_bytes (nbits);
2010 / word_size); 2136 int last_mask = ~ (~0u << ((nbits - 1) % BOOL_VECTOR_BITS_PER_CHAR + 1));
2137 memset (data, pattern, nbytes - 1);
2138 data[nbytes - 1] = pattern & last_mask;
2139 }
2140 return a;
2141}
2011 2142
2012 CHECK_NATNUM (length); 2143/* Return a newly allocated, uninitialized bool vector of size NBITS. */
2013 2144
2014 bits_per_value = sizeof (EMACS_INT) * BOOL_VECTOR_BITS_PER_CHAR; 2145Lisp_Object
2146make_uninit_bool_vector (EMACS_INT nbits)
2147{
2148 Lisp_Object val;
2149 EMACS_INT words = bool_vector_words (nbits);
2150 EMACS_INT word_bytes = words * sizeof (bits_word);
2151 EMACS_INT needed_elements = ((bool_header_size - header_size + word_bytes
2152 + word_size - 1)
2153 / word_size);
2154 struct Lisp_Bool_Vector *p
2155 = (struct Lisp_Bool_Vector *) allocate_vector (needed_elements);
2156 XSETVECTOR (val, p);
2157 XSETPVECTYPESIZE (XVECTOR (val), PVEC_BOOL_VECTOR, 0, 0);
2158 p->size = nbits;
2015 2159
2016 length_in_elts = (XFASTINT (length) + bits_per_value - 1) / bits_per_value; 2160 /* Clear padding at the end. */
2161 if (words)
2162 p->data[words - 1] = 0;
2017 2163
2018 val = Fmake_vector (make_number (length_in_elts + extra_bool_elts), Qnil); 2164 return val;
2165}
2019 2166
2020 /* No Lisp_Object to trace in there. */ 2167DEFUN ("make-bool-vector", Fmake_bool_vector, Smake_bool_vector, 2, 2, 0,
2021 XSETPVECTYPESIZE (XVECTOR (val), PVEC_BOOL_VECTOR, 0, 0); 2168 doc: /* Return a new bool-vector of length LENGTH, using INIT for each element.
2169LENGTH must be a number. INIT matters only in whether it is t or nil. */)
2170 (Lisp_Object length, Lisp_Object init)
2171{
2172 Lisp_Object val;
2022 2173
2023 p = XBOOL_VECTOR (val); 2174 CHECK_NATNUM (length);
2024 p->size = XFASTINT (length); 2175 val = make_uninit_bool_vector (XFASTINT (length));
2176 return bool_vector_fill (val, init);
2177}
2025 2178
2026 length_in_chars = ((XFASTINT (length) + BOOL_VECTOR_BITS_PER_CHAR - 1) 2179DEFUN ("bool-vector", Fbool_vector, Sbool_vector, 0, MANY, 0,
2027 / BOOL_VECTOR_BITS_PER_CHAR); 2180 doc: /* Return a new bool-vector with specified arguments as elements.
2028 if (length_in_chars) 2181Any number of arguments, even zero arguments, are allowed.
2029 { 2182usage: (bool-vector &rest OBJECTS) */)
2030 memset (p->data, ! NILP (init) ? -1 : 0, length_in_chars); 2183 (ptrdiff_t nargs, Lisp_Object *args)
2184{
2185 ptrdiff_t i;
2186 Lisp_Object vector;
2031 2187
2032 /* Clear any extraneous bits in the last byte. */ 2188 vector = make_uninit_bool_vector (nargs);
2033 p->data[length_in_chars - 1] 2189 for (i = 0; i < nargs; i++)
2034 &= (1 << ((XFASTINT (length) - 1) % BOOL_VECTOR_BITS_PER_CHAR + 1)) - 1; 2190 bool_vector_set (vector, i, !NILP (args[i]));
2035 }
2036 2191
2037 return val; 2192 return vector;
2038} 2193}
2039 2194
2040
2041/* Make a string from NBYTES bytes at CONTENTS, and compute the number 2195/* Make a string from NBYTES bytes at CONTENTS, and compute the number
2042 of characters from the contents. This string may be unibyte or 2196 of characters from the contents. This string may be unibyte or
2043 multibyte, depending on the contents. */ 2197 multibyte, depending on the contents. */
@@ -2059,8 +2213,7 @@ make_string (const char *contents, ptrdiff_t nbytes)
2059 return val; 2213 return val;
2060} 2214}
2061 2215
2062 2216/* Make a unibyte string from LENGTH bytes at CONTENTS. */
2063/* Make an unibyte string from LENGTH bytes at CONTENTS. */
2064 2217
2065Lisp_Object 2218Lisp_Object
2066make_unibyte_string (const char *contents, ptrdiff_t length) 2219make_unibyte_string (const char *contents, ptrdiff_t length)
@@ -2129,7 +2282,7 @@ make_specified_string (const char *contents,
2129} 2282}
2130 2283
2131 2284
2132/* Return an unibyte Lisp_String set up to hold LENGTH characters 2285/* Return a unibyte Lisp_String set up to hold LENGTH characters
2133 occupying LENGTH bytes. */ 2286 occupying LENGTH bytes. */
2134 2287
2135Lisp_Object 2288Lisp_Object
@@ -2195,21 +2348,21 @@ make_formatted_string (char *buf, const char *format, ...)
2195#define FLOAT_BLOCK_SIZE \ 2348#define FLOAT_BLOCK_SIZE \
2196 (((BLOCK_BYTES - sizeof (struct float_block *) \ 2349 (((BLOCK_BYTES - sizeof (struct float_block *) \
2197 /* The compiler might add padding at the end. */ \ 2350 /* The compiler might add padding at the end. */ \
2198 - (sizeof (struct Lisp_Float) - sizeof (int))) * CHAR_BIT) \ 2351 - (sizeof (struct Lisp_Float) - sizeof (bits_word))) * CHAR_BIT) \
2199 / (sizeof (struct Lisp_Float) * CHAR_BIT + 1)) 2352 / (sizeof (struct Lisp_Float) * CHAR_BIT + 1))
2200 2353
2201#define GETMARKBIT(block,n) \ 2354#define GETMARKBIT(block,n) \
2202 (((block)->gcmarkbits[(n) / (sizeof (int) * CHAR_BIT)] \ 2355 (((block)->gcmarkbits[(n) / BITS_PER_BITS_WORD] \
2203 >> ((n) % (sizeof (int) * CHAR_BIT))) \ 2356 >> ((n) % BITS_PER_BITS_WORD)) \
2204 & 1) 2357 & 1)
2205 2358
2206#define SETMARKBIT(block,n) \ 2359#define SETMARKBIT(block,n) \
2207 (block)->gcmarkbits[(n) / (sizeof (int) * CHAR_BIT)] \ 2360 ((block)->gcmarkbits[(n) / BITS_PER_BITS_WORD] \
2208 |= 1 << ((n) % (sizeof (int) * CHAR_BIT)) 2361 |= (bits_word) 1 << ((n) % BITS_PER_BITS_WORD))
2209 2362
2210#define UNSETMARKBIT(block,n) \ 2363#define UNSETMARKBIT(block,n) \
2211 (block)->gcmarkbits[(n) / (sizeof (int) * CHAR_BIT)] \ 2364 ((block)->gcmarkbits[(n) / BITS_PER_BITS_WORD] \
2212 &= ~(1 << ((n) % (sizeof (int) * CHAR_BIT))) 2365 &= ~((bits_word) 1 << ((n) % BITS_PER_BITS_WORD)))
2213 2366
2214#define FLOAT_BLOCK(fptr) \ 2367#define FLOAT_BLOCK(fptr) \
2215 ((struct float_block *) (((uintptr_t) (fptr)) & ~(BLOCK_ALIGN - 1))) 2368 ((struct float_block *) (((uintptr_t) (fptr)) & ~(BLOCK_ALIGN - 1)))
@@ -2221,7 +2374,7 @@ struct float_block
2221{ 2374{
2222 /* Place `floats' at the beginning, to ease up FLOAT_INDEX's job. */ 2375 /* Place `floats' at the beginning, to ease up FLOAT_INDEX's job. */
2223 struct Lisp_Float floats[FLOAT_BLOCK_SIZE]; 2376 struct Lisp_Float floats[FLOAT_BLOCK_SIZE];
2224 int gcmarkbits[1 + FLOAT_BLOCK_SIZE / (sizeof (int) * CHAR_BIT)]; 2377 bits_word gcmarkbits[1 + FLOAT_BLOCK_SIZE / BITS_PER_BITS_WORD];
2225 struct float_block *next; 2378 struct float_block *next;
2226}; 2379};
2227 2380
@@ -2302,7 +2455,7 @@ make_float (double float_value)
2302#define CONS_BLOCK_SIZE \ 2455#define CONS_BLOCK_SIZE \
2303 (((BLOCK_BYTES - sizeof (struct cons_block *) \ 2456 (((BLOCK_BYTES - sizeof (struct cons_block *) \
2304 /* The compiler might add padding at the end. */ \ 2457 /* The compiler might add padding at the end. */ \
2305 - (sizeof (struct Lisp_Cons) - sizeof (int))) * CHAR_BIT) \ 2458 - (sizeof (struct Lisp_Cons) - sizeof (bits_word))) * CHAR_BIT) \
2306 / (sizeof (struct Lisp_Cons) * CHAR_BIT + 1)) 2459 / (sizeof (struct Lisp_Cons) * CHAR_BIT + 1))
2307 2460
2308#define CONS_BLOCK(fptr) \ 2461#define CONS_BLOCK(fptr) \
@@ -2315,7 +2468,7 @@ struct cons_block
2315{ 2468{
2316 /* Place `conses' at the beginning, to ease up CONS_INDEX's job. */ 2469 /* Place `conses' at the beginning, to ease up CONS_INDEX's job. */
2317 struct Lisp_Cons conses[CONS_BLOCK_SIZE]; 2470 struct Lisp_Cons conses[CONS_BLOCK_SIZE];
2318 int gcmarkbits[1 + CONS_BLOCK_SIZE / (sizeof (int) * CHAR_BIT)]; 2471 bits_word gcmarkbits[1 + CONS_BLOCK_SIZE / BITS_PER_BITS_WORD];
2319 struct cons_block *next; 2472 struct cons_block *next;
2320}; 2473};
2321 2474
@@ -2346,9 +2499,7 @@ void
2346free_cons (struct Lisp_Cons *ptr) 2499free_cons (struct Lisp_Cons *ptr)
2347{ 2500{
2348 ptr->u.chain = cons_free_list; 2501 ptr->u.chain = cons_free_list;
2349#if GC_MARK_STACK
2350 ptr->car = Vdead; 2502 ptr->car = Vdead;
2351#endif
2352 cons_free_list = ptr; 2503 cons_free_list = ptr;
2353 consing_since_gc -= sizeof *ptr; 2504 consing_since_gc -= sizeof *ptr;
2354 total_free_conses++; 2505 total_free_conses++;
@@ -2451,29 +2602,28 @@ list5 (Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3, Lisp_Object arg4, L
2451Lisp_Object 2602Lisp_Object
2452listn (enum constype type, ptrdiff_t count, Lisp_Object arg, ...) 2603listn (enum constype type, ptrdiff_t count, Lisp_Object arg, ...)
2453{ 2604{
2454 va_list ap; 2605 Lisp_Object (*cons) (Lisp_Object, Lisp_Object);
2455 ptrdiff_t i; 2606 switch (type)
2456 Lisp_Object val, *objp; 2607 {
2608 case CONSTYPE_PURE: cons = pure_cons; break;
2609 case CONSTYPE_HEAP: cons = Fcons; break;
2610 default: emacs_abort ();
2611 }
2457 2612
2458 /* Change to SAFE_ALLOCA if you hit this eassert. */ 2613 eassume (0 < count);
2459 eassert (count <= MAX_ALLOCA / word_size); 2614 Lisp_Object val = cons (arg, Qnil);
2615 Lisp_Object tail = val;
2460 2616
2461 objp = alloca (count * word_size); 2617 va_list ap;
2462 objp[0] = arg;
2463 va_start (ap, arg); 2618 va_start (ap, arg);
2464 for (i = 1; i < count; i++) 2619 for (ptrdiff_t i = 1; i < count; i++)
2465 objp[i] = va_arg (ap, Lisp_Object);
2466 va_end (ap);
2467
2468 for (val = Qnil, i = count - 1; i >= 0; i--)
2469 { 2620 {
2470 if (type == CONSTYPE_PURE) 2621 Lisp_Object elem = cons (va_arg (ap, Lisp_Object), Qnil);
2471 val = pure_cons (objp[i], val); 2622 XSETCDR (tail, elem);
2472 else if (type == CONSTYPE_HEAP) 2623 tail = elem;
2473 val = Fcons (objp[i], val);
2474 else
2475 emacs_abort ();
2476 } 2624 }
2625 va_end (ap);
2626
2477 return val; 2627 return val;
2478} 2628}
2479 2629
@@ -2547,36 +2697,55 @@ DEFUN ("make-list", Fmake_list, Smake_list, 2, 2, 0,
2547 Vector Allocation 2697 Vector Allocation
2548 ***********************************************************************/ 2698 ***********************************************************************/
2549 2699
2700/* Sometimes a vector's contents are merely a pointer internally used
2701 in vector allocation code. On the rare platforms where a null
2702 pointer cannot be tagged, represent it with a Lisp 0.
2703 Usually you don't want to touch this. */
2704
2705static struct Lisp_Vector *
2706next_vector (struct Lisp_Vector *v)
2707{
2708 return XUNTAG (v->contents[0], Lisp_Int0);
2709}
2710
2711static void
2712set_next_vector (struct Lisp_Vector *v, struct Lisp_Vector *p)
2713{
2714 v->contents[0] = make_lisp_ptr (p, Lisp_Int0);
2715}
2716
2550/* This value is balanced well enough to avoid too much internal overhead 2717/* This value is balanced well enough to avoid too much internal overhead
2551 for the most common cases; it's not required to be a power of two, but 2718 for the most common cases; it's not required to be a power of two, but
2552 it's expected to be a mult-of-ROUNDUP_SIZE (see below). */ 2719 it's expected to be a mult-of-ROUNDUP_SIZE (see below). */
2553 2720
2554#define VECTOR_BLOCK_SIZE 4096 2721#define VECTOR_BLOCK_SIZE 4096
2555 2722
2556/* Align allocation request sizes to be a multiple of ROUNDUP_SIZE. */
2557enum 2723enum
2558 { 2724 {
2559 roundup_size = COMMON_MULTIPLE (word_size, USE_LSB_TAG ? GCALIGNMENT : 1) 2725 /* Alignment of struct Lisp_Vector objects. */
2560 }; 2726 vector_alignment = COMMON_MULTIPLE (ALIGNOF_STRUCT_LISP_VECTOR,
2727 GCALIGNMENT),
2561 2728
2562/* ROUNDUP_SIZE must be a power of 2. */ 2729 /* Vector size requests are a multiple of this. */
2563verify ((roundup_size & (roundup_size - 1)) == 0); 2730 roundup_size = COMMON_MULTIPLE (vector_alignment, word_size)
2731 };
2564 2732
2565/* Verify assumptions described above. */ 2733/* Verify assumptions described above. */
2566verify ((VECTOR_BLOCK_SIZE % roundup_size) == 0); 2734verify ((VECTOR_BLOCK_SIZE % roundup_size) == 0);
2567verify (VECTOR_BLOCK_SIZE <= (1 << PSEUDOVECTOR_SIZE_BITS)); 2735verify (VECTOR_BLOCK_SIZE <= (1 << PSEUDOVECTOR_SIZE_BITS));
2568 2736
2569/* Round up X to nearest mult-of-ROUNDUP_SIZE. */ 2737/* Round up X to nearest mult-of-ROUNDUP_SIZE --- use at compile time. */
2570 2738#define vroundup_ct(x) ROUNDUP (x, roundup_size)
2571#define vroundup(x) (((x) + (roundup_size - 1)) & ~(roundup_size - 1)) 2739/* Round up X to nearest mult-of-ROUNDUP_SIZE --- use at runtime. */
2740#define vroundup(x) (eassume ((x) >= 0), vroundup_ct (x))
2572 2741
2573/* Rounding helps to maintain alignment constraints if USE_LSB_TAG. */ 2742/* Rounding helps to maintain alignment constraints if USE_LSB_TAG. */
2574 2743
2575#define VECTOR_BLOCK_BYTES (VECTOR_BLOCK_SIZE - vroundup (sizeof (void *))) 2744#define VECTOR_BLOCK_BYTES (VECTOR_BLOCK_SIZE - vroundup_ct (sizeof (void *)))
2576 2745
2577/* Size of the minimal vector allocated from block. */ 2746/* Size of the minimal vector allocated from block. */
2578 2747
2579#define VBLOCK_BYTES_MIN vroundup (header_size + sizeof (Lisp_Object)) 2748#define VBLOCK_BYTES_MIN vroundup_ct (header_size + sizeof (Lisp_Object))
2580 2749
2581/* Size of the largest vector allocated from block. */ 2750/* Size of the largest vector allocated from block. */
2582 2751
@@ -2597,22 +2766,6 @@ verify (VECTOR_BLOCK_SIZE <= (1 << PSEUDOVECTOR_SIZE_BITS));
2597 2766
2598#define VINDEX(nbytes) (((nbytes) - VBLOCK_BYTES_MIN) / roundup_size) 2767#define VINDEX(nbytes) (((nbytes) - VBLOCK_BYTES_MIN) / roundup_size)
2599 2768
2600/* Get and set the next field in block-allocated vectorlike objects on
2601 the free list. Doing it this way respects C's aliasing rules.
2602 We could instead make 'contents' a union, but that would mean
2603 changes everywhere that the code uses 'contents'. */
2604static struct Lisp_Vector *
2605next_in_free_list (struct Lisp_Vector *v)
2606{
2607 intptr_t i = XLI (v->contents[0]);
2608 return (struct Lisp_Vector *) i;
2609}
2610static void
2611set_next_in_free_list (struct Lisp_Vector *v, struct Lisp_Vector *next)
2612{
2613 v->contents[0] = XIL ((intptr_t) next);
2614}
2615
2616/* Common shortcut to setup vector on a free list. */ 2769/* Common shortcut to setup vector on a free list. */
2617 2770
2618#define SETUP_ON_FREE_LIST(v, nbytes, tmp) \ 2771#define SETUP_ON_FREE_LIST(v, nbytes, tmp) \
@@ -2622,26 +2775,37 @@ set_next_in_free_list (struct Lisp_Vector *v, struct Lisp_Vector *next)
2622 eassert ((nbytes) % roundup_size == 0); \ 2775 eassert ((nbytes) % roundup_size == 0); \
2623 (tmp) = VINDEX (nbytes); \ 2776 (tmp) = VINDEX (nbytes); \
2624 eassert ((tmp) < VECTOR_MAX_FREE_LIST_INDEX); \ 2777 eassert ((tmp) < VECTOR_MAX_FREE_LIST_INDEX); \
2625 set_next_in_free_list (v, vector_free_lists[tmp]); \ 2778 set_next_vector (v, vector_free_lists[tmp]); \
2626 vector_free_lists[tmp] = (v); \ 2779 vector_free_lists[tmp] = (v); \
2627 total_free_vector_slots += (nbytes) / word_size; \ 2780 total_free_vector_slots += (nbytes) / word_size; \
2628 } while (0) 2781 } while (0)
2629 2782
2630/* This internal type is used to maintain the list of large vectors 2783/* This internal type is used to maintain the list of large vectors
2631 which are allocated at their own, e.g. outside of vector blocks. */ 2784 which are allocated at their own, e.g. outside of vector blocks.
2785
2786 struct large_vector itself cannot contain a struct Lisp_Vector, as
2787 the latter contains a flexible array member and C99 does not allow
2788 such structs to be nested. Instead, each struct large_vector
2789 object LV is followed by a struct Lisp_Vector, which is at offset
2790 large_vector_offset from LV, and whose address is therefore
2791 large_vector_vec (&LV). */
2632 2792
2633struct large_vector 2793struct large_vector
2634{ 2794{
2635 union { 2795 struct large_vector *next;
2636 struct large_vector *vector;
2637#if USE_LSB_TAG
2638 /* We need to maintain ROUNDUP_SIZE alignment for the vector member. */
2639 unsigned char c[vroundup (sizeof (struct large_vector *))];
2640#endif
2641 } next;
2642 struct Lisp_Vector v;
2643}; 2796};
2644 2797
2798enum
2799{
2800 large_vector_offset = ROUNDUP (sizeof (struct large_vector), vector_alignment)
2801};
2802
2803static struct Lisp_Vector *
2804large_vector_vec (struct large_vector *p)
2805{
2806 return (struct Lisp_Vector *) ((char *) p + large_vector_offset);
2807}
2808
2645/* This internal type is used to maintain an underlying storage 2809/* This internal type is used to maintain an underlying storage
2646 for small vectors. */ 2810 for small vectors. */
2647 2811
@@ -2683,7 +2847,7 @@ allocate_vector_block (void)
2683{ 2847{
2684 struct vector_block *block = xmalloc (sizeof *block); 2848 struct vector_block *block = xmalloc (sizeof *block);
2685 2849
2686#if GC_MARK_STACK && !defined GC_MALLOC_CHECK 2850#ifndef GC_MALLOC_CHECK
2687 mem_insert (block->data, block->data + VECTOR_BLOCK_BYTES, 2851 mem_insert (block->data, block->data + VECTOR_BLOCK_BYTES,
2688 MEM_TYPE_VECTOR_BLOCK); 2852 MEM_TYPE_VECTOR_BLOCK);
2689#endif 2853#endif
@@ -2719,7 +2883,7 @@ allocate_vector_from_block (size_t nbytes)
2719 if (vector_free_lists[index]) 2883 if (vector_free_lists[index])
2720 { 2884 {
2721 vector = vector_free_lists[index]; 2885 vector = vector_free_lists[index];
2722 vector_free_lists[index] = next_in_free_list (vector); 2886 vector_free_lists[index] = next_vector (vector);
2723 total_free_vector_slots -= nbytes / word_size; 2887 total_free_vector_slots -= nbytes / word_size;
2724 return vector; 2888 return vector;
2725 } 2889 }
@@ -2733,7 +2897,7 @@ allocate_vector_from_block (size_t nbytes)
2733 { 2897 {
2734 /* This vector is larger than requested. */ 2898 /* This vector is larger than requested. */
2735 vector = vector_free_lists[index]; 2899 vector = vector_free_lists[index];
2736 vector_free_lists[index] = next_in_free_list (vector); 2900 vector_free_lists[index] = next_vector (vector);
2737 total_free_vector_slots -= nbytes / word_size; 2901 total_free_vector_slots -= nbytes / word_size;
2738 2902
2739 /* Excess bytes are used for the smaller vector, 2903 /* Excess bytes are used for the smaller vector,
@@ -2773,31 +2937,67 @@ static ptrdiff_t
2773vector_nbytes (struct Lisp_Vector *v) 2937vector_nbytes (struct Lisp_Vector *v)
2774{ 2938{
2775 ptrdiff_t size = v->header.size & ~ARRAY_MARK_FLAG; 2939 ptrdiff_t size = v->header.size & ~ARRAY_MARK_FLAG;
2940 ptrdiff_t nwords;
2776 2941
2777 if (size & PSEUDOVECTOR_FLAG) 2942 if (size & PSEUDOVECTOR_FLAG)
2778 { 2943 {
2779 if (PSEUDOVECTOR_TYPEP (&v->header, PVEC_BOOL_VECTOR)) 2944 if (PSEUDOVECTOR_TYPEP (&v->header, PVEC_BOOL_VECTOR))
2780 size = (bool_header_size 2945 {
2781 + (((struct Lisp_Bool_Vector *) v)->size 2946 struct Lisp_Bool_Vector *bv = (struct Lisp_Bool_Vector *) v;
2782 + BOOL_VECTOR_BITS_PER_CHAR - 1) 2947 ptrdiff_t word_bytes = (bool_vector_words (bv->size)
2783 / BOOL_VECTOR_BITS_PER_CHAR); 2948 * sizeof (bits_word));
2949 ptrdiff_t boolvec_bytes = bool_header_size + word_bytes;
2950 verify (header_size <= bool_header_size);
2951 nwords = (boolvec_bytes - header_size + word_size - 1) / word_size;
2952 }
2784 else 2953 else
2785 size = (header_size 2954 nwords = ((size & PSEUDOVECTOR_SIZE_MASK)
2786 + ((size & PSEUDOVECTOR_SIZE_MASK) 2955 + ((size & PSEUDOVECTOR_REST_MASK)
2787 + ((size & PSEUDOVECTOR_REST_MASK) 2956 >> PSEUDOVECTOR_SIZE_BITS));
2788 >> PSEUDOVECTOR_SIZE_BITS)) * word_size);
2789 } 2957 }
2790 else 2958 else
2791 size = header_size + size * word_size; 2959 nwords = size;
2792 return vroundup (size); 2960 return vroundup (header_size + word_size * nwords);
2961}
2962
2963/* Release extra resources still in use by VECTOR, which may be any
2964 vector-like object. */
2965
2966static void
2967cleanup_vector (struct Lisp_Vector *vector)
2968{
2969 detect_suspicious_free (vector);
2970 if (PSEUDOVECTOR_TYPEP (&vector->header, PVEC_FONT)
2971 && ((vector->header.size & PSEUDOVECTOR_SIZE_MASK)
2972 == FONT_OBJECT_MAX))
2973 {
2974 struct font_driver *drv = ((struct font *) vector)->driver;
2975
2976 /* The font driver might sometimes be NULL, e.g. if Emacs was
2977 interrupted before it had time to set it up. */
2978 if (drv)
2979 {
2980 /* Attempt to catch subtle bugs like Bug#16140. */
2981 eassert (valid_font_driver (drv));
2982 drv->close ((struct font *) vector);
2983 }
2984 }
2985
2986 if (PSEUDOVECTOR_TYPEP (&vector->header, PVEC_THREAD))
2987 finalize_one_thread ((struct thread_state *) vector);
2988 else if (PSEUDOVECTOR_TYPEP (&vector->header, PVEC_MUTEX))
2989 finalize_one_mutex ((struct Lisp_Mutex *) vector);
2990 else if (PSEUDOVECTOR_TYPEP (&vector->header, PVEC_CONDVAR))
2991 finalize_one_condvar ((struct Lisp_CondVar *) vector);
2793} 2992}
2794 2993
2795/* Reclaim space used by unmarked vectors. */ 2994/* Reclaim space used by unmarked vectors. */
2796 2995
2996NO_INLINE /* For better stack traces */
2797static void 2997static void
2798sweep_vectors (void) 2998sweep_vectors (void)
2799{ 2999{
2800 struct vector_block *block = vector_blocks, **bprev = &vector_blocks; 3000 struct vector_block *block, **bprev = &vector_blocks;
2801 struct large_vector *lv, **lvprev = &large_vectors; 3001 struct large_vector *lv, **lvprev = &large_vectors;
2802 struct Lisp_Vector *vector, *next; 3002 struct Lisp_Vector *vector, *next;
2803 3003
@@ -2826,13 +3026,7 @@ sweep_vectors (void)
2826 { 3026 {
2827 ptrdiff_t total_bytes; 3027 ptrdiff_t total_bytes;
2828 3028
2829 if (PSEUDOVECTOR_TYPEP (&vector->header, PVEC_THREAD)) 3029 cleanup_vector (vector);
2830 finalize_one_thread ((struct thread_state *) vector);
2831 else if (PSEUDOVECTOR_TYPEP (&vector->header, PVEC_MUTEX))
2832 finalize_one_mutex ((struct Lisp_Mutex *) vector);
2833 else if (PSEUDOVECTOR_TYPEP (&vector->header, PVEC_CONDVAR))
2834 finalize_one_condvar ((struct Lisp_CondVar *) vector);
2835
2836 nbytes = vector_nbytes (vector); 3030 nbytes = vector_nbytes (vector);
2837 total_bytes = nbytes; 3031 total_bytes = nbytes;
2838 next = ADVANCE (vector, nbytes); 3032 next = ADVANCE (vector, nbytes);
@@ -2844,6 +3038,7 @@ sweep_vectors (void)
2844 { 3038 {
2845 if (VECTOR_MARKED_P (next)) 3039 if (VECTOR_MARKED_P (next))
2846 break; 3040 break;
3041 cleanup_vector (next);
2847 nbytes = vector_nbytes (next); 3042 nbytes = vector_nbytes (next);
2848 total_bytes += nbytes; 3043 total_bytes += nbytes;
2849 next = ADVANCE (next, nbytes); 3044 next = ADVANCE (next, nbytes);
@@ -2853,12 +3048,12 @@ sweep_vectors (void)
2853 3048
2854 if (vector == (struct Lisp_Vector *) block->data 3049 if (vector == (struct Lisp_Vector *) block->data
2855 && !VECTOR_IN_BLOCK (next, block)) 3050 && !VECTOR_IN_BLOCK (next, block))
2856 /* This block should be freed because all of it's 3051 /* This block should be freed because all of its
2857 space was coalesced into the only free vector. */ 3052 space was coalesced into the only free vector. */
2858 free_this_block = 1; 3053 free_this_block = 1;
2859 else 3054 else
2860 { 3055 {
2861 int tmp; 3056 size_t tmp;
2862 SETUP_ON_FREE_LIST (vector, total_bytes, tmp); 3057 SETUP_ON_FREE_LIST (vector, total_bytes, tmp);
2863 } 3058 }
2864 } 3059 }
@@ -2867,7 +3062,7 @@ sweep_vectors (void)
2867 if (free_this_block) 3062 if (free_this_block)
2868 { 3063 {
2869 *bprev = block->next; 3064 *bprev = block->next;
2870#if GC_MARK_STACK && !defined GC_MALLOC_CHECK 3065#ifndef GC_MALLOC_CHECK
2871 mem_delete (mem_find (block->data)); 3066 mem_delete (mem_find (block->data));
2872#endif 3067#endif
2873 xfree (block); 3068 xfree (block);
@@ -2880,33 +3075,27 @@ sweep_vectors (void)
2880 3075
2881 for (lv = large_vectors; lv; lv = *lvprev) 3076 for (lv = large_vectors; lv; lv = *lvprev)
2882 { 3077 {
2883 vector = &lv->v; 3078 vector = large_vector_vec (lv);
2884 if (VECTOR_MARKED_P (vector)) 3079 if (VECTOR_MARKED_P (vector))
2885 { 3080 {
2886 VECTOR_UNMARK (vector); 3081 VECTOR_UNMARK (vector);
2887 total_vectors++; 3082 total_vectors++;
2888 if (vector->header.size & PSEUDOVECTOR_FLAG) 3083 if (vector->header.size & PSEUDOVECTOR_FLAG)
2889 { 3084 {
2890 struct Lisp_Bool_Vector *b = (struct Lisp_Bool_Vector *) vector;
2891
2892 /* All non-bool pseudovectors are small enough to be allocated 3085 /* All non-bool pseudovectors are small enough to be allocated
2893 from vector blocks. This code should be redesigned if some 3086 from vector blocks. This code should be redesigned if some
2894 pseudovector type grows beyond VBLOCK_BYTES_MAX. */ 3087 pseudovector type grows beyond VBLOCK_BYTES_MAX. */
2895 eassert (PSEUDOVECTOR_TYPEP (&vector->header, PVEC_BOOL_VECTOR)); 3088 eassert (PSEUDOVECTOR_TYPEP (&vector->header, PVEC_BOOL_VECTOR));
2896 3089 total_vector_slots += vector_nbytes (vector) / word_size;
2897 total_vector_slots
2898 += (bool_header_size
2899 + ((b->size + BOOL_VECTOR_BITS_PER_CHAR - 1)
2900 / BOOL_VECTOR_BITS_PER_CHAR)) / word_size;
2901 } 3090 }
2902 else 3091 else
2903 total_vector_slots 3092 total_vector_slots
2904 += header_size / word_size + vector->header.size; 3093 += header_size / word_size + vector->header.size;
2905 lvprev = &lv->next.vector; 3094 lvprev = &lv->next;
2906 } 3095 }
2907 else 3096 else
2908 { 3097 {
2909 *lvprev = lv->next.vector; 3098 *lvprev = lv->next;
2910 lisp_free (lv); 3099 lisp_free (lv);
2911 } 3100 }
2912 } 3101 }
@@ -2929,10 +3118,8 @@ allocate_vectorlike (ptrdiff_t len)
2929 size_t nbytes = header_size + len * word_size; 3118 size_t nbytes = header_size + len * word_size;
2930 3119
2931#ifdef DOUG_LEA_MALLOC 3120#ifdef DOUG_LEA_MALLOC
2932 /* Prevent mmap'ing the chunk. Lisp data may not be mmap'ed 3121 if (!mmap_lisp_allowed_p ())
2933 because mapped region contents are not preserved in 3122 mallopt (M_MMAP_MAX, 0);
2934 a dumped Emacs. */
2935 mallopt (M_MMAP_MAX, 0);
2936#endif 3123#endif
2937 3124
2938 if (nbytes <= VBLOCK_BYTES_MAX) 3125 if (nbytes <= VBLOCK_BYTES_MAX)
@@ -2940,19 +3127,22 @@ allocate_vectorlike (ptrdiff_t len)
2940 else 3127 else
2941 { 3128 {
2942 struct large_vector *lv 3129 struct large_vector *lv
2943 = lisp_malloc ((offsetof (struct large_vector, v.contents) 3130 = lisp_malloc ((large_vector_offset + header_size
2944 + len * word_size), 3131 + len * word_size),
2945 MEM_TYPE_VECTORLIKE); 3132 MEM_TYPE_VECTORLIKE);
2946 lv->next.vector = large_vectors; 3133 lv->next = large_vectors;
2947 large_vectors = lv; 3134 large_vectors = lv;
2948 p = &lv->v; 3135 p = large_vector_vec (lv);
2949 } 3136 }
2950 3137
2951#ifdef DOUG_LEA_MALLOC 3138#ifdef DOUG_LEA_MALLOC
2952 /* Back to a reasonable maximum of mmap'ed areas. */ 3139 if (!mmap_lisp_allowed_p ())
2953 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS); 3140 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS);
2954#endif 3141#endif
2955 3142
3143 if (find_suspicious_object_in_range (p, (char *) p + nbytes))
3144 emacs_abort ();
3145
2956 consing_since_gc += nbytes; 3146 consing_since_gc += nbytes;
2957 vector_cells_consed += len; 3147 vector_cells_consed += len;
2958 } 3148 }
@@ -2982,20 +3172,19 @@ allocate_vector (EMACS_INT len)
2982/* Allocate other vector-like structures. */ 3172/* Allocate other vector-like structures. */
2983 3173
2984struct Lisp_Vector * 3174struct Lisp_Vector *
2985allocate_pseudovector (int memlen, int lisplen, enum pvec_type tag) 3175allocate_pseudovector (int memlen, int lisplen,
3176 int zerolen, enum pvec_type tag)
2986{ 3177{
2987 struct Lisp_Vector *v = allocate_vectorlike (memlen); 3178 struct Lisp_Vector *v = allocate_vectorlike (memlen);
2988 int i;
2989 3179
2990 /* Catch bogus values. */ 3180 /* Catch bogus values. */
2991 eassert (tag <= PVEC_FONT); 3181 eassert (0 <= tag && tag <= PVEC_FONT);
3182 eassert (0 <= lisplen && lisplen <= zerolen && zerolen <= memlen);
2992 eassert (memlen - lisplen <= (1 << PSEUDOVECTOR_REST_BITS) - 1); 3183 eassert (memlen - lisplen <= (1 << PSEUDOVECTOR_REST_BITS) - 1);
2993 eassert (lisplen <= (1 << PSEUDOVECTOR_SIZE_BITS) - 1); 3184 eassert (lisplen <= (1 << PSEUDOVECTOR_SIZE_BITS) - 1);
2994 3185
2995 /* Only the first lisplen slots will be traced normally by the GC. */ 3186 /* Only the first LISPLEN slots will be traced normally by the GC. */
2996 for (i = 0; i < lisplen; ++i) 3187 memclear (v->contents, zerolen * word_size);
2997 v->contents[i] = Qnil;
2998
2999 XSETPVECTYPESIZE (v, tag, lisplen, memlen - lisplen); 3188 XSETPVECTYPESIZE (v, tag, lisplen, memlen - lisplen);
3000 return v; 3189 return v;
3001} 3190}
@@ -3013,60 +3202,6 @@ allocate_buffer (void)
3013 return b; 3202 return b;
3014} 3203}
3015 3204
3016struct Lisp_Hash_Table *
3017allocate_hash_table (void)
3018{
3019 return ALLOCATE_PSEUDOVECTOR (struct Lisp_Hash_Table, count, PVEC_HASH_TABLE);
3020}
3021
3022struct window *
3023allocate_window (void)
3024{
3025 struct window *w;
3026
3027 w = ALLOCATE_PSEUDOVECTOR (struct window, current_matrix, PVEC_WINDOW);
3028 /* Users assumes that non-Lisp data is zeroed. */
3029 memset (&w->current_matrix, 0,
3030 sizeof (*w) - offsetof (struct window, current_matrix));
3031 return w;
3032}
3033
3034struct terminal *
3035allocate_terminal (void)
3036{
3037 struct terminal *t;
3038
3039 t = ALLOCATE_PSEUDOVECTOR (struct terminal, next_terminal, PVEC_TERMINAL);
3040 /* Users assumes that non-Lisp data is zeroed. */
3041 memset (&t->next_terminal, 0,
3042 sizeof (*t) - offsetof (struct terminal, next_terminal));
3043 return t;
3044}
3045
3046struct frame *
3047allocate_frame (void)
3048{
3049 struct frame *f;
3050
3051 f = ALLOCATE_PSEUDOVECTOR (struct frame, face_cache, PVEC_FRAME);
3052 /* Users assumes that non-Lisp data is zeroed. */
3053 memset (&f->face_cache, 0,
3054 sizeof (*f) - offsetof (struct frame, face_cache));
3055 return f;
3056}
3057
3058struct Lisp_Process *
3059allocate_process (void)
3060{
3061 struct Lisp_Process *p;
3062
3063 p = ALLOCATE_PSEUDOVECTOR (struct Lisp_Process, pid, PVEC_PROCESS);
3064 /* Users assumes that non-Lisp data is zeroed. */
3065 memset (&p->pid, 0,
3066 sizeof (*p) - offsetof (struct Lisp_Process, pid));
3067 return p;
3068}
3069
3070DEFUN ("make-vector", Fmake_vector, Smake_vector, 2, 2, 0, 3205DEFUN ("make-vector", Fmake_vector, Smake_vector, 2, 2, 0,
3071 doc: /* Return a newly created vector of length LENGTH, with each element being INIT. 3206 doc: /* Return a newly created vector of length LENGTH, with each element being INIT.
3072See also the function `vector'. */) 3207See also the function `vector'. */)
@@ -3088,7 +3223,6 @@ See also the function `vector'. */)
3088 return vector; 3223 return vector;
3089} 3224}
3090 3225
3091
3092DEFUN ("vector", Fvector, Svector, 0, MANY, 0, 3226DEFUN ("vector", Fvector, Svector, 0, MANY, 0,
3093 doc: /* Return a newly created vector with specified arguments as elements. 3227 doc: /* Return a newly created vector with specified arguments as elements.
3094Any number of arguments, even zero arguments, are allowed. 3228Any number of arguments, even zero arguments, are allowed.
@@ -3107,6 +3241,9 @@ usage: (vector &rest OBJECTS) */)
3107void 3241void
3108make_byte_code (struct Lisp_Vector *v) 3242make_byte_code (struct Lisp_Vector *v)
3109{ 3243{
3244 /* Don't allow the global zero_vector to become a byte code object. */
3245 eassert (0 < v->header.size);
3246
3110 if (v->header.size > 1 && STRINGP (v->contents[1]) 3247 if (v->header.size > 1 && STRINGP (v->contents[1])
3111 && STRING_MULTIBYTE (v->contents[1])) 3248 && STRING_MULTIBYTE (v->contents[1]))
3112 /* BYTECODE-STRING must have been produced by Emacs 20.2 or the 3249 /* BYTECODE-STRING must have been produced by Emacs 20.2 or the
@@ -3162,15 +3299,13 @@ usage: (make-byte-code ARGLIST BYTE-CODE CONSTANTS DEPTH &optional DOCSTRING INT
3162 ***********************************************************************/ 3299 ***********************************************************************/
3163 3300
3164/* Like struct Lisp_Symbol, but padded so that the size is a multiple 3301/* Like struct Lisp_Symbol, but padded so that the size is a multiple
3165 of the required alignment if LSB tags are used. */ 3302 of the required alignment. */
3166 3303
3167union aligned_Lisp_Symbol 3304union aligned_Lisp_Symbol
3168{ 3305{
3169 struct Lisp_Symbol s; 3306 struct Lisp_Symbol s;
3170#if USE_LSB_TAG
3171 unsigned char c[(sizeof (struct Lisp_Symbol) + GCALIGNMENT - 1) 3307 unsigned char c[(sizeof (struct Lisp_Symbol) + GCALIGNMENT - 1)
3172 & -GCALIGNMENT]; 3308 & -GCALIGNMENT];
3173#endif
3174}; 3309};
3175 3310
3176/* Each symbol_block is just under 1020 bytes long, since malloc 3311/* Each symbol_block is just under 1020 bytes long, since malloc
@@ -3192,6 +3327,13 @@ struct symbol_block
3192 3327
3193static struct symbol_block *symbol_block; 3328static struct symbol_block *symbol_block;
3194static int symbol_block_index = SYMBOL_BLOCK_SIZE; 3329static int symbol_block_index = SYMBOL_BLOCK_SIZE;
3330/* Pointer to the first symbol_block that contains pinned symbols.
3331 Tests for 24.4 showed that at dump-time, Emacs contains about 15K symbols,
3332 10K of which are pinned (and all but 250 of them are interned in obarray),
3333 whereas a "typical session" has in the order of 30K symbols.
3334 `symbol_block_pinned' lets mark_pinned_symbols scan only 15K symbols rather
3335 than 30K to find the 10K symbols we need to mark. */
3336static struct symbol_block *symbol_block_pinned;
3195 3337
3196/* List of free symbols. */ 3338/* List of free symbols. */
3197 3339
@@ -3203,13 +3345,29 @@ set_symbol_name (Lisp_Object sym, Lisp_Object name)
3203 XSYMBOL (sym)->name = name; 3345 XSYMBOL (sym)->name = name;
3204} 3346}
3205 3347
3348void
3349init_symbol (Lisp_Object val, Lisp_Object name)
3350{
3351 struct Lisp_Symbol *p = XSYMBOL (val);
3352 set_symbol_name (val, name);
3353 set_symbol_plist (val, Qnil);
3354 p->redirect = SYMBOL_PLAINVAL;
3355 SET_SYMBOL_VAL (p, Qunbound);
3356 set_symbol_function (val, Qnil);
3357 set_symbol_next (val, NULL);
3358 p->gcmarkbit = false;
3359 p->interned = SYMBOL_UNINTERNED;
3360 p->constant = 0;
3361 p->declared_special = false;
3362 p->pinned = false;
3363}
3364
3206DEFUN ("make-symbol", Fmake_symbol, Smake_symbol, 1, 1, 0, 3365DEFUN ("make-symbol", Fmake_symbol, Smake_symbol, 1, 1, 0,
3207 doc: /* Return a newly allocated uninterned symbol whose name is NAME. 3366 doc: /* Return a newly allocated uninterned symbol whose name is NAME.
3208Its value is void, and its function definition and property list are nil. */) 3367Its value is void, and its function definition and property list are nil. */)
3209 (Lisp_Object name) 3368 (Lisp_Object name)
3210{ 3369{
3211 register Lisp_Object val; 3370 Lisp_Object val;
3212 register struct Lisp_Symbol *p;
3213 3371
3214 CHECK_STRING (name); 3372 CHECK_STRING (name);
3215 3373
@@ -3237,17 +3395,7 @@ Its value is void, and its function definition and property list are nil. */)
3237 3395
3238 MALLOC_UNBLOCK_INPUT; 3396 MALLOC_UNBLOCK_INPUT;
3239 3397
3240 p = XSYMBOL (val); 3398 init_symbol (val, name);
3241 set_symbol_name (val, name);
3242 set_symbol_plist (val, Qnil);
3243 p->redirect = SYMBOL_PLAINVAL;
3244 SET_SYMBOL_VAL (p, Qunbound);
3245 set_symbol_function (val, Qnil);
3246 set_symbol_next (val, NULL);
3247 p->gcmarkbit = 0;
3248 p->interned = SYMBOL_UNINTERNED;
3249 p->constant = 0;
3250 p->declared_special = 0;
3251 consing_since_gc += sizeof (struct Lisp_Symbol); 3399 consing_since_gc += sizeof (struct Lisp_Symbol);
3252 symbols_consed++; 3400 symbols_consed++;
3253 total_free_symbols--; 3401 total_free_symbols--;
@@ -3261,19 +3409,17 @@ Its value is void, and its function definition and property list are nil. */)
3261 ***********************************************************************/ 3409 ***********************************************************************/
3262 3410
3263/* Like union Lisp_Misc, but padded so that its size is a multiple of 3411/* Like union Lisp_Misc, but padded so that its size is a multiple of
3264 the required alignment when LSB tags are used. */ 3412 the required alignment. */
3265 3413
3266union aligned_Lisp_Misc 3414union aligned_Lisp_Misc
3267{ 3415{
3268 union Lisp_Misc m; 3416 union Lisp_Misc m;
3269#if USE_LSB_TAG
3270 unsigned char c[(sizeof (union Lisp_Misc) + GCALIGNMENT - 1) 3417 unsigned char c[(sizeof (union Lisp_Misc) + GCALIGNMENT - 1)
3271 & -GCALIGNMENT]; 3418 & -GCALIGNMENT];
3272#endif
3273}; 3419};
3274 3420
3275/* Allocation of markers and other objects that share that structure. 3421/* Allocation of markers and other objects that share that structure.
3276 Works like allocation of conses. */ 3422 Works like allocation of conses. */
3277 3423
3278#define MARKER_BLOCK_SIZE \ 3424#define MARKER_BLOCK_SIZE \
3279 ((1020 - sizeof (struct marker_block *)) / sizeof (union aligned_Lisp_Misc)) 3425 ((1020 - sizeof (struct marker_block *)) / sizeof (union aligned_Lisp_Misc))
@@ -3377,7 +3523,6 @@ make_save_obj_obj_obj_obj (Lisp_Object a, Lisp_Object b, Lisp_Object c,
3377 return val; 3523 return val;
3378} 3524}
3379 3525
3380#if defined HAVE_NS || defined HAVE_NTGUI
3381Lisp_Object 3526Lisp_Object
3382make_save_ptr (void *a) 3527make_save_ptr (void *a)
3383{ 3528{
@@ -3387,7 +3532,6 @@ make_save_ptr (void *a)
3387 p->data[0].pointer = a; 3532 p->data[0].pointer = a;
3388 return val; 3533 return val;
3389} 3534}
3390#endif
3391 3535
3392Lisp_Object 3536Lisp_Object
3393make_save_ptr_int (void *a, ptrdiff_t b) 3537make_save_ptr_int (void *a, ptrdiff_t b)
@@ -3400,7 +3544,7 @@ make_save_ptr_int (void *a, ptrdiff_t b)
3400 return val; 3544 return val;
3401} 3545}
3402 3546
3403#if defined HAVE_MENUS && ! (defined USE_X_TOOLKIT || defined USE_GTK) 3547#if ! (defined USE_X_TOOLKIT || defined USE_GTK)
3404Lisp_Object 3548Lisp_Object
3405make_save_ptr_ptr (void *a, void *b) 3549make_save_ptr_ptr (void *a, void *b)
3406{ 3550{
@@ -3478,6 +3622,7 @@ DEFUN ("make-marker", Fmake_marker, Smake_marker, 0, 0, 0,
3478 p->charpos = 0; 3622 p->charpos = 0;
3479 p->next = NULL; 3623 p->next = NULL;
3480 p->insertion_type = 0; 3624 p->insertion_type = 0;
3625 p->need_adjustment = 0;
3481 return val; 3626 return val;
3482} 3627}
3483 3628
@@ -3502,6 +3647,7 @@ build_marker (struct buffer *buf, ptrdiff_t charpos, ptrdiff_t bytepos)
3502 m->charpos = charpos; 3647 m->charpos = charpos;
3503 m->bytepos = bytepos; 3648 m->bytepos = bytepos;
3504 m->insertion_type = 0; 3649 m->insertion_type = 0;
3650 m->need_adjustment = 0;
3505 m->next = BUF_MARKERS (buf); 3651 m->next = BUF_MARKERS (buf);
3506 BUF_MARKERS (buf) = m; 3652 BUF_MARKERS (buf) = m;
3507 return obj; 3653 return obj;
@@ -3524,9 +3670,9 @@ free_marker (Lisp_Object marker)
3524 Any number of arguments, even zero arguments, are allowed. */ 3670 Any number of arguments, even zero arguments, are allowed. */
3525 3671
3526Lisp_Object 3672Lisp_Object
3527make_event_array (register int nargs, Lisp_Object *args) 3673make_event_array (ptrdiff_t nargs, Lisp_Object *args)
3528{ 3674{
3529 int i; 3675 ptrdiff_t i;
3530 3676
3531 for (i = 0; i < nargs; i++) 3677 for (i = 0; i < nargs; i++)
3532 /* The things that fit in a string 3678 /* The things that fit in a string
@@ -3554,6 +3700,125 @@ make_event_array (register int nargs, Lisp_Object *args)
3554 } 3700 }
3555} 3701}
3556 3702
3703static void
3704init_finalizer_list (struct Lisp_Finalizer *head)
3705{
3706 head->prev = head->next = head;
3707}
3708
3709/* Insert FINALIZER before ELEMENT. */
3710
3711static void
3712finalizer_insert (struct Lisp_Finalizer *element,
3713 struct Lisp_Finalizer *finalizer)
3714{
3715 eassert (finalizer->prev == NULL);
3716 eassert (finalizer->next == NULL);
3717 finalizer->next = element;
3718 finalizer->prev = element->prev;
3719 finalizer->prev->next = finalizer;
3720 element->prev = finalizer;
3721}
3722
3723static void
3724unchain_finalizer (struct Lisp_Finalizer *finalizer)
3725{
3726 if (finalizer->prev != NULL)
3727 {
3728 eassert (finalizer->next != NULL);
3729 finalizer->prev->next = finalizer->next;
3730 finalizer->next->prev = finalizer->prev;
3731 finalizer->prev = finalizer->next = NULL;
3732 }
3733}
3734
3735static void
3736mark_finalizer_list (struct Lisp_Finalizer *head)
3737{
3738 for (struct Lisp_Finalizer *finalizer = head->next;
3739 finalizer != head;
3740 finalizer = finalizer->next)
3741 {
3742 finalizer->base.gcmarkbit = true;
3743 mark_object (finalizer->function);
3744 }
3745}
3746
3747/* Move doomed finalizers to list DEST from list SRC. A doomed
3748 finalizer is one that is not GC-reachable and whose
3749 finalizer->function is non-nil. */
3750
3751static void
3752queue_doomed_finalizers (struct Lisp_Finalizer *dest,
3753 struct Lisp_Finalizer *src)
3754{
3755 struct Lisp_Finalizer *finalizer = src->next;
3756 while (finalizer != src)
3757 {
3758 struct Lisp_Finalizer *next = finalizer->next;
3759 if (!finalizer->base.gcmarkbit && !NILP (finalizer->function))
3760 {
3761 unchain_finalizer (finalizer);
3762 finalizer_insert (dest, finalizer);
3763 }
3764
3765 finalizer = next;
3766 }
3767}
3768
3769static Lisp_Object
3770run_finalizer_handler (Lisp_Object args)
3771{
3772 add_to_log ("finalizer failed: %S", args);
3773 return Qnil;
3774}
3775
3776static void
3777run_finalizer_function (Lisp_Object function)
3778{
3779 ptrdiff_t count = SPECPDL_INDEX ();
3780
3781 specbind (Qinhibit_quit, Qt);
3782 internal_condition_case_1 (call0, function, Qt, run_finalizer_handler);
3783 unbind_to (count, Qnil);
3784}
3785
3786static void
3787run_finalizers (struct Lisp_Finalizer *finalizers)
3788{
3789 struct Lisp_Finalizer *finalizer;
3790 Lisp_Object function;
3791
3792 while (finalizers->next != finalizers)
3793 {
3794 finalizer = finalizers->next;
3795 eassert (finalizer->base.type == Lisp_Misc_Finalizer);
3796 unchain_finalizer (finalizer);
3797 function = finalizer->function;
3798 if (!NILP (function))
3799 {
3800 finalizer->function = Qnil;
3801 run_finalizer_function (function);
3802 }
3803 }
3804}
3805
3806DEFUN ("make-finalizer", Fmake_finalizer, Smake_finalizer, 1, 1, 0,
3807 doc: /* Make a finalizer that will run FUNCTION.
3808FUNCTION will be called after garbage collection when the returned
3809finalizer object becomes unreachable. If the finalizer object is
3810reachable only through references from finalizer objects, it does not
3811count as reachable for the purpose of deciding whether to run
3812FUNCTION. FUNCTION will be run once per finalizer object. */)
3813 (Lisp_Object function)
3814{
3815 Lisp_Object val = allocate_misc (Lisp_Misc_Finalizer);
3816 struct Lisp_Finalizer *finalizer = XFINALIZER (val);
3817 finalizer->function = function;
3818 finalizer->prev = finalizer->next = NULL;
3819 finalizer_insert (&finalizers, finalizer);
3820 return val;
3821}
3557 3822
3558 3823
3559/************************************************************************ 3824/************************************************************************
@@ -3596,7 +3861,7 @@ memory_full (size_t nbytes)
3596 memory_full_cons_threshold = sizeof (struct cons_block); 3861 memory_full_cons_threshold = sizeof (struct cons_block);
3597 3862
3598 /* The first time we get here, free the spare memory. */ 3863 /* The first time we get here, free the spare memory. */
3599 for (i = 0; i < sizeof (spare_memory) / sizeof (char *); i++) 3864 for (i = 0; i < ARRAYELTS (spare_memory); i++)
3600 if (spare_memory[i]) 3865 if (spare_memory[i])
3601 { 3866 {
3602 if (i == 0) 3867 if (i == 0)
@@ -3624,7 +3889,7 @@ memory_full (size_t nbytes)
3624void 3889void
3625refill_memory_reserve (void) 3890refill_memory_reserve (void)
3626{ 3891{
3627#ifndef SYSTEM_MALLOC 3892#if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
3628 if (spare_memory[0] == 0) 3893 if (spare_memory[0] == 0)
3629 spare_memory[0] = malloc (SPARE_MEMORY); 3894 spare_memory[0] = malloc (SPARE_MEMORY);
3630 if (spare_memory[1] == 0) 3895 if (spare_memory[1] == 0)
@@ -3654,8 +3919,6 @@ refill_memory_reserve (void)
3654 C Stack Marking 3919 C Stack Marking
3655 ************************************************************************/ 3920 ************************************************************************/
3656 3921
3657#if GC_MARK_STACK || defined GC_MALLOC_CHECK
3658
3659/* Conservative C stack marking requires a method to identify possibly 3922/* Conservative C stack marking requires a method to identify possibly
3660 live Lisp objects given a pointer value. We do this by keeping 3923 live Lisp objects given a pointer value. We do this by keeping
3661 track of blocks of Lisp data that are allocated in a red-black tree 3924 track of blocks of Lisp data that are allocated in a red-black tree
@@ -3722,26 +3985,12 @@ mem_insert (void *start, void *end, enum mem_type type)
3722 c = mem_root; 3985 c = mem_root;
3723 parent = NULL; 3986 parent = NULL;
3724 3987
3725#if GC_MARK_STACK != GC_MAKE_GCPROS_NOOPS
3726
3727 while (c != MEM_NIL) 3988 while (c != MEM_NIL)
3728 { 3989 {
3729 if (start >= c->start && start < c->end)
3730 emacs_abort ();
3731 parent = c; 3990 parent = c;
3732 c = start < c->start ? c->left : c->right; 3991 c = start < c->start ? c->left : c->right;
3733 } 3992 }
3734 3993
3735#else /* GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS */
3736
3737 while (c != MEM_NIL)
3738 {
3739 parent = c;
3740 c = start < c->start ? c->left : c->right;
3741 }
3742
3743#endif /* GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS */
3744
3745 /* Create a new node. */ 3994 /* Create a new node. */
3746#ifdef GC_MALLOC_CHECK 3995#ifdef GC_MALLOC_CHECK
3747 x = malloc (sizeof *x); 3996 x = malloc (sizeof *x);
@@ -4207,9 +4456,7 @@ live_vector_p (struct mem_node *m, void *p)
4207 vector = ADVANCE (vector, vector_nbytes (vector)); 4456 vector = ADVANCE (vector, vector_nbytes (vector));
4208 } 4457 }
4209 } 4458 }
4210 else if (m->type == MEM_TYPE_VECTORLIKE 4459 else if (m->type == MEM_TYPE_VECTORLIKE && p == large_vector_vec (m->start))
4211 && (char *) p == ((char *) m->start
4212 + offsetof (struct large_vector, v)))
4213 /* This memory node corresponds to a large vector. */ 4460 /* This memory node corresponds to a large vector. */
4214 return 1; 4461 return 1;
4215 return 0; 4462 return 0;
@@ -4226,84 +4473,28 @@ live_buffer_p (struct mem_node *m, void *p)
4226 must not have been killed. */ 4473 must not have been killed. */
4227 return (m->type == MEM_TYPE_BUFFER 4474 return (m->type == MEM_TYPE_BUFFER
4228 && p == m->start 4475 && p == m->start
4229 && !NILP (((struct buffer *) p)->INTERNAL_FIELD (name))); 4476 && !NILP (((struct buffer *) p)->name_));
4230}
4231
4232#endif /* GC_MARK_STACK || defined GC_MALLOC_CHECK */
4233
4234#if GC_MARK_STACK
4235
4236#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
4237
4238/* Currently not used, but may be called from gdb. */
4239
4240void dump_zombies (void) EXTERNALLY_VISIBLE;
4241
4242/* Array of objects that are kept alive because the C stack contains
4243 a pattern that looks like a reference to them . */
4244
4245#define MAX_ZOMBIES 10
4246static Lisp_Object zombies[MAX_ZOMBIES];
4247
4248/* Number of zombie objects. */
4249
4250static EMACS_INT nzombies;
4251
4252/* Number of garbage collections. */
4253
4254static EMACS_INT ngcs;
4255
4256/* Average percentage of zombies per collection. */
4257
4258static double avg_zombies;
4259
4260/* Max. number of live and zombie objects. */
4261
4262static EMACS_INT max_live, max_zombies;
4263
4264/* Average number of live objects per GC. */
4265
4266static double avg_live;
4267
4268DEFUN ("gc-status", Fgc_status, Sgc_status, 0, 0, "",
4269 doc: /* Show information about live and zombie objects. */)
4270 (void)
4271{
4272 Lisp_Object args[8], zombie_list = Qnil;
4273 EMACS_INT i;
4274 for (i = 0; i < min (MAX_ZOMBIES, nzombies); i++)
4275 zombie_list = Fcons (zombies[i], zombie_list);
4276 args[0] = build_string ("%d GCs, avg live/zombies = %.2f/%.2f (%f%%), max %d/%d\nzombies: %S");
4277 args[1] = make_number (ngcs);
4278 args[2] = make_float (avg_live);
4279 args[3] = make_float (avg_zombies);
4280 args[4] = make_float (avg_zombies / avg_live / 100);
4281 args[5] = make_number (max_live);
4282 args[6] = make_number (max_zombies);
4283 args[7] = zombie_list;
4284 return Fmessage (8, args);
4285} 4477}
4286 4478
4287#endif /* GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES */
4288
4289
4290/* Mark OBJ if we can prove it's a Lisp_Object. */ 4479/* Mark OBJ if we can prove it's a Lisp_Object. */
4291 4480
4292static void 4481static void
4293mark_maybe_object (Lisp_Object obj) 4482mark_maybe_object (Lisp_Object obj)
4294{ 4483{
4295 void *po; 4484#if USE_VALGRIND
4296 struct mem_node *m; 4485 if (valgrind_p)
4486 VALGRIND_MAKE_MEM_DEFINED (&obj, sizeof (obj));
4487#endif
4297 4488
4298 if (INTEGERP (obj)) 4489 if (INTEGERP (obj))
4299 return; 4490 return;
4300 4491
4301 po = (void *) XPNTR (obj); 4492 void *po = XPNTR (obj);
4302 m = mem_find (po); 4493 struct mem_node *m = mem_find (po);
4303 4494
4304 if (m != MEM_NIL) 4495 if (m != MEM_NIL)
4305 { 4496 {
4306 bool mark_p = 0; 4497 bool mark_p = false;
4307 4498
4308 switch (XTYPE (obj)) 4499 switch (XTYPE (obj))
4309 { 4500 {
@@ -4343,17 +4534,19 @@ mark_maybe_object (Lisp_Object obj)
4343 } 4534 }
4344 4535
4345 if (mark_p) 4536 if (mark_p)
4346 { 4537 mark_object (obj);
4347#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
4348 if (nzombies < MAX_ZOMBIES)
4349 zombies[nzombies] = obj;
4350 ++nzombies;
4351#endif
4352 mark_object (obj);
4353 }
4354 } 4538 }
4355} 4539}
4356 4540
4541/* Return true if P can point to Lisp data, and false otherwise.
4542 Symbols are implemented via offsets not pointers, but the offsets
4543 are also multiples of GCALIGNMENT. */
4544
4545static bool
4546maybe_lisp_pointer (void *p)
4547{
4548 return (uintptr_t) p % GCALIGNMENT == 0;
4549}
4357 4550
4358/* If P points to Lisp data, mark that as live if it isn't already 4551/* If P points to Lisp data, mark that as live if it isn't already
4359 marked. */ 4552 marked. */
@@ -4363,10 +4556,12 @@ mark_maybe_pointer (void *p)
4363{ 4556{
4364 struct mem_node *m; 4557 struct mem_node *m;
4365 4558
4366 /* Quickly rule out some values which can't point to Lisp data. 4559#if USE_VALGRIND
4367 USE_LSB_TAG needs Lisp data to be aligned on multiples of GCALIGNMENT. 4560 if (valgrind_p)
4368 Otherwise, assume that Lisp data is aligned on even addresses. */ 4561 VALGRIND_MAKE_MEM_DEFINED (&p, sizeof (p));
4369 if ((intptr_t) p % (USE_LSB_TAG ? GCALIGNMENT : 2)) 4562#endif
4563
4564 if (!maybe_lisp_pointer (p))
4370 return; 4565 return;
4371 4566
4372 m = mem_find (p); 4567 m = mem_find (p);
@@ -4438,48 +4633,15 @@ mark_maybe_pointer (void *p)
4438 miss objects if __alignof__ were used. */ 4633 miss objects if __alignof__ were used. */
4439#define GC_POINTER_ALIGNMENT alignof (void *) 4634#define GC_POINTER_ALIGNMENT alignof (void *)
4440 4635
4441/* Define POINTERS_MIGHT_HIDE_IN_OBJECTS to 1 if marking via C pointers does
4442 not suffice, which is the typical case. A host where a Lisp_Object is
4443 wider than a pointer might allocate a Lisp_Object in non-adjacent halves.
4444 If USE_LSB_TAG, the bottom half is not a valid pointer, but it should
4445 suffice to widen it to to a Lisp_Object and check it that way. */
4446#if USE_LSB_TAG || VAL_MAX < UINTPTR_MAX
4447# if !USE_LSB_TAG && VAL_MAX < UINTPTR_MAX >> GCTYPEBITS
4448 /* If tag bits straddle pointer-word boundaries, neither mark_maybe_pointer
4449 nor mark_maybe_object can follow the pointers. This should not occur on
4450 any practical porting target. */
4451# error "MSB type bits straddle pointer-word boundaries"
4452# endif
4453 /* Marking via C pointers does not suffice, because Lisp_Objects contain
4454 pointer words that hold pointers ORed with type bits. */
4455# define POINTERS_MIGHT_HIDE_IN_OBJECTS 1
4456#else
4457 /* Marking via C pointers suffices, because Lisp_Objects contain pointer
4458 words that hold unmodified pointers. */
4459# define POINTERS_MIGHT_HIDE_IN_OBJECTS 0
4460#endif
4461
4462/* Mark Lisp objects referenced from the address range START+OFFSET..END 4636/* Mark Lisp objects referenced from the address range START+OFFSET..END
4463 or END+OFFSET..START. */ 4637 or END+OFFSET..START. */
4464 4638
4465static void 4639static void ATTRIBUTE_NO_SANITIZE_ADDRESS
4466mark_memory (void *start, void *end) 4640mark_memory (void *start, void *end)
4467#if defined (__clang__) && defined (__has_feature)
4468#if __has_feature(address_sanitizer)
4469 /* Do not allow -faddress-sanitizer to check this function, since it
4470 crosses the function stack boundary, and thus would yield many
4471 false positives. */
4472 __attribute__((no_address_safety_analysis))
4473#endif
4474#endif
4475{ 4641{
4476 void **pp; 4642 void **pp;
4477 int i; 4643 int i;
4478 4644
4479#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
4480 nzombies = 0;
4481#endif
4482
4483 /* Make START the pointer to the start of the memory region, 4645 /* Make START the pointer to the start of the memory region,
4484 if it isn't already. */ 4646 if it isn't already. */
4485 if (end < start) 4647 if (end < start)
@@ -4499,7 +4661,7 @@ mark_memory (void *start, void *end)
4499 Lisp_Object obj = build_string ("test"); 4661 Lisp_Object obj = build_string ("test");
4500 struct Lisp_String *s = XSTRING (obj); 4662 struct Lisp_String *s = XSTRING (obj);
4501 Fgarbage_collect (); 4663 Fgarbage_collect ();
4502 fprintf (stderr, "test `%s'\n", s->data); 4664 fprintf (stderr, "test '%s'\n", s->data);
4503 return Qnil; 4665 return Qnil;
4504 } 4666 }
4505 4667
@@ -4512,8 +4674,7 @@ mark_memory (void *start, void *end)
4512 { 4674 {
4513 void *p = *(void **) ((char *) pp + i); 4675 void *p = *(void **) ((char *) pp + i);
4514 mark_maybe_pointer (p); 4676 mark_maybe_pointer (p);
4515 if (POINTERS_MIGHT_HIDE_IN_OBJECTS) 4677 mark_maybe_object (XIL ((intptr_t) p));
4516 mark_maybe_object (XIL ((intptr_t) p));
4517 } 4678 }
4518} 4679}
4519 4680
@@ -4601,42 +4762,6 @@ test_setjmp (void)
4601#endif /* not GC_SAVE_REGISTERS_ON_STACK && not GC_SETJMP_WORKS */ 4762#endif /* not GC_SAVE_REGISTERS_ON_STACK && not GC_SETJMP_WORKS */
4602 4763
4603 4764
4604#if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
4605
4606/* Abort if anything GCPRO'd doesn't survive the GC. */
4607
4608static void
4609check_gcpros (void)
4610{
4611 struct gcpro *p;
4612 ptrdiff_t i;
4613
4614 for (p = gcprolist; p; p = p->next)
4615 for (i = 0; i < p->nvars; ++i)
4616 if (!survives_gc_p (p->var[i]))
4617 /* FIXME: It's not necessarily a bug. It might just be that the
4618 GCPRO is unnecessary or should release the object sooner. */
4619 emacs_abort ();
4620}
4621
4622#elif GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
4623
4624void
4625dump_zombies (void)
4626{
4627 int i;
4628
4629 fprintf (stderr, "\nZombies kept alive = %"pI"d:\n", nzombies);
4630 for (i = 0; i < min (MAX_ZOMBIES, nzombies); ++i)
4631 {
4632 fprintf (stderr, " %d = ", i);
4633 debug_print (zombies[i]);
4634 }
4635}
4636
4637#endif /* GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES */
4638
4639
4640/* Mark live Lisp objects on the C stack. 4765/* Mark live Lisp objects on the C stack.
4641 4766
4642 There are several system-dependent problems to consider when 4767 There are several system-dependent problems to consider when
@@ -4698,10 +4823,6 @@ mark_stack (char *bottom, char *end)
4698#ifdef GC_MARK_SECONDARY_STACK 4823#ifdef GC_MARK_SECONDARY_STACK
4699 GC_MARK_SECONDARY_STACK (); 4824 GC_MARK_SECONDARY_STACK ();
4700#endif 4825#endif
4701
4702#if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
4703 check_gcpros ();
4704#endif
4705} 4826}
4706 4827
4707/* This is a trampoline function that flushes registers to the stack, 4828/* This is a trampoline function that flushes registers to the stack,
@@ -4711,7 +4832,7 @@ mark_stack (char *bottom, char *end)
4711 global interpreter lock. This lets the garbage collector easily 4832 global interpreter lock. This lets the garbage collector easily
4712 find roots in registers on threads that are not actively running 4833 find roots in registers on threads that are not actively running
4713 Lisp. 4834 Lisp.
4714 4835
4715 It is invalid to run any Lisp code or to allocate any GC memory 4836 It is invalid to run any Lisp code or to allocate any GC memory
4716 from FUNC. */ 4837 from FUNC. */
4717 4838
@@ -4779,12 +4900,14 @@ flush_stack_call_func (void (*func) (void *arg), void *arg)
4779 eassert (current_thread == self); 4900 eassert (current_thread == self);
4780} 4901}
4781 4902
4782#else /* GC_MARK_STACK == 0 */ 4903static bool
4783 4904c_symbol_p (struct Lisp_Symbol *sym)
4784#define mark_maybe_object(obj) emacs_abort () 4905{
4785 4906 char *lispsym_ptr = (char *) lispsym;
4786#endif /* GC_MARK_STACK != 0 */ 4907 char *sym_ptr = (char *) sym;
4787 4908 ptrdiff_t lispsym_offset = sym_ptr - lispsym_ptr;
4909 return 0 <= lispsym_offset && lispsym_offset < sizeof lispsym;
4910}
4788 4911
4789/* Determine whether it is safe to access memory at address P. */ 4912/* Determine whether it is safe to access memory at address P. */
4790static int 4913static int
@@ -4793,6 +4916,10 @@ valid_pointer_p (void *p)
4793#ifdef WINDOWSNT 4916#ifdef WINDOWSNT
4794 return w32_valid_pointer_p (p, 16); 4917 return w32_valid_pointer_p (p, 16);
4795#else 4918#else
4919
4920 if (ADDRESS_SANITIZER)
4921 return p ? -1 : 0;
4922
4796 int fd[2]; 4923 int fd[2];
4797 4924
4798 /* Obviously, we cannot just access it (we would SEGV trying), so we 4925 /* Obviously, we cannot just access it (we would SEGV trying), so we
@@ -4802,13 +4929,13 @@ valid_pointer_p (void *p)
4802 4929
4803 if (emacs_pipe (fd) == 0) 4930 if (emacs_pipe (fd) == 0)
4804 { 4931 {
4805 bool valid = emacs_write (fd[1], (char *) p, 16) == 16; 4932 bool valid = emacs_write (fd[1], p, 16) == 16;
4806 emacs_close (fd[1]); 4933 emacs_close (fd[1]);
4807 emacs_close (fd[0]); 4934 emacs_close (fd[0]);
4808 return valid; 4935 return valid;
4809 } 4936 }
4810 4937
4811 return -1; 4938 return -1;
4812#endif 4939#endif
4813} 4940}
4814 4941
@@ -4822,26 +4949,20 @@ valid_pointer_p (void *p)
4822int 4949int
4823valid_lisp_object_p (Lisp_Object obj) 4950valid_lisp_object_p (Lisp_Object obj)
4824{ 4951{
4825 void *p;
4826#if GC_MARK_STACK
4827 struct mem_node *m;
4828#endif
4829
4830 if (INTEGERP (obj)) 4952 if (INTEGERP (obj))
4831 return 1; 4953 return 1;
4832 4954
4833 p = (void *) XPNTR (obj); 4955 void *p = XPNTR (obj);
4834 if (PURE_POINTER_P (p)) 4956 if (PURE_P (p))
4835 return 1; 4957 return 1;
4836 4958
4959 if (SYMBOLP (obj) && c_symbol_p (p))
4960 return ((char *) p - (char *) lispsym) % sizeof lispsym[0] == 0;
4961
4837 if (p == &buffer_defaults || p == &buffer_local_symbols) 4962 if (p == &buffer_defaults || p == &buffer_local_symbols)
4838 return 2; 4963 return 2;
4839 4964
4840#if !GC_MARK_STACK 4965 struct mem_node *m = mem_find (p);
4841 return valid_pointer_p (p);
4842#else
4843
4844 m = mem_find (p);
4845 4966
4846 if (m == MEM_NIL) 4967 if (m == MEM_NIL)
4847 { 4968 {
@@ -4888,12 +5009,8 @@ valid_lisp_object_p (Lisp_Object obj)
4888 } 5009 }
4889 5010
4890 return 0; 5011 return 0;
4891#endif
4892} 5012}
4893 5013
4894
4895
4896
4897/*********************************************************************** 5014/***********************************************************************
4898 Pure Storage Management 5015 Pure Storage Management
4899 ***********************************************************************/ 5016 ***********************************************************************/
@@ -4906,22 +5023,13 @@ static void *
4906pure_alloc (size_t size, int type) 5023pure_alloc (size_t size, int type)
4907{ 5024{
4908 void *result; 5025 void *result;
4909#if USE_LSB_TAG
4910 size_t alignment = GCALIGNMENT;
4911#else
4912 size_t alignment = alignof (EMACS_INT);
4913
4914 /* Give Lisp_Floats an extra alignment. */
4915 if (type == Lisp_Float)
4916 alignment = alignof (struct Lisp_Float);
4917#endif
4918 5026
4919 again: 5027 again:
4920 if (type >= 0) 5028 if (type >= 0)
4921 { 5029 {
4922 /* Allocate space for a Lisp object from the beginning of the free 5030 /* Allocate space for a Lisp object from the beginning of the free
4923 space with taking account of alignment. */ 5031 space with taking account of alignment. */
4924 result = ALIGN (purebeg + pure_bytes_used_lisp, alignment); 5032 result = ALIGN (purebeg + pure_bytes_used_lisp, GCALIGNMENT);
4925 pure_bytes_used_lisp = ((char *)result - (char *)purebeg) + size; 5033 pure_bytes_used_lisp = ((char *)result - (char *)purebeg) + size;
4926 } 5034 }
4927 else 5035 else
@@ -5070,6 +5178,8 @@ make_pure_c_string (const char *data, ptrdiff_t nchars)
5070 return string; 5178 return string;
5071} 5179}
5072 5180
5181static Lisp_Object purecopy (Lisp_Object obj);
5182
5073/* Return a cons allocated from pure space. Give it pure copies 5183/* Return a cons allocated from pure space. Give it pure copies
5074 of CAR as car and CDR as cdr. */ 5184 of CAR as car and CDR as cdr. */
5075 5185
@@ -5079,8 +5189,8 @@ pure_cons (Lisp_Object car, Lisp_Object cdr)
5079 Lisp_Object new; 5189 Lisp_Object new;
5080 struct Lisp_Cons *p = pure_alloc (sizeof *p, Lisp_Cons); 5190 struct Lisp_Cons *p = pure_alloc (sizeof *p, Lisp_Cons);
5081 XSETCONS (new, p); 5191 XSETCONS (new, p);
5082 XSETCAR (new, Fpurecopy (car)); 5192 XSETCAR (new, purecopy (car));
5083 XSETCDR (new, Fpurecopy (cdr)); 5193 XSETCDR (new, purecopy (cdr));
5084 return new; 5194 return new;
5085} 5195}
5086 5196
@@ -5112,7 +5222,6 @@ make_pure_vector (ptrdiff_t len)
5112 return new; 5222 return new;
5113} 5223}
5114 5224
5115
5116DEFUN ("purecopy", Fpurecopy, Spurecopy, 1, 1, 0, 5225DEFUN ("purecopy", Fpurecopy, Spurecopy, 1, 1, 0,
5117 doc: /* Make a copy of object OBJ in pure storage. 5226 doc: /* Make a copy of object OBJ in pure storage.
5118Recursively copies contents of vectors and cons cells. 5227Recursively copies contents of vectors and cons cells.
@@ -5121,9 +5230,25 @@ Does not copy symbols. Copies strings without text properties. */)
5121{ 5230{
5122 if (NILP (Vpurify_flag)) 5231 if (NILP (Vpurify_flag))
5123 return obj; 5232 return obj;
5124 5233 else if (MARKERP (obj) || OVERLAYP (obj)
5125 if (PURE_POINTER_P (XPNTR (obj))) 5234 || HASH_TABLE_P (obj) || SYMBOLP (obj))
5235 /* Can't purify those. */
5126 return obj; 5236 return obj;
5237 else
5238 return purecopy (obj);
5239}
5240
5241static Lisp_Object
5242purecopy (Lisp_Object obj)
5243{
5244 if (INTEGERP (obj)
5245 || (! SYMBOLP (obj) && PURE_P (XPNTR_OR_SYMBOL_OFFSET (obj)))
5246 || SUBRP (obj))
5247 return obj; /* Already pure. */
5248
5249 if (STRINGP (obj) && XSTRING (obj)->intervals)
5250 message_with_string ("Dropping text-properties while making string `%s' pure",
5251 obj, true);
5127 5252
5128 if (HASH_TABLE_P (Vpurify_flag)) /* Hash consing. */ 5253 if (HASH_TABLE_P (Vpurify_flag)) /* Hash consing. */
5129 { 5254 {
@@ -5140,31 +5265,36 @@ Does not copy symbols. Copies strings without text properties. */)
5140 obj = make_pure_string (SSDATA (obj), SCHARS (obj), 5265 obj = make_pure_string (SSDATA (obj), SCHARS (obj),
5141 SBYTES (obj), 5266 SBYTES (obj),
5142 STRING_MULTIBYTE (obj)); 5267 STRING_MULTIBYTE (obj));
5143 else if (COMPILEDP (obj) || VECTORP (obj)) 5268 else if (COMPILEDP (obj) || VECTORP (obj) || HASH_TABLE_P (obj))
5144 { 5269 {
5145 register struct Lisp_Vector *vec; 5270 struct Lisp_Vector *objp = XVECTOR (obj);
5271 ptrdiff_t nbytes = vector_nbytes (objp);
5272 struct Lisp_Vector *vec = pure_alloc (nbytes, Lisp_Vectorlike);
5146 register ptrdiff_t i; 5273 register ptrdiff_t i;
5147 ptrdiff_t size; 5274 ptrdiff_t size = ASIZE (obj);
5148
5149 size = ASIZE (obj);
5150 if (size & PSEUDOVECTOR_FLAG) 5275 if (size & PSEUDOVECTOR_FLAG)
5151 size &= PSEUDOVECTOR_SIZE_MASK; 5276 size &= PSEUDOVECTOR_SIZE_MASK;
5152 vec = XVECTOR (make_pure_vector (size)); 5277 memcpy (vec, objp, nbytes);
5153 for (i = 0; i < size; i++) 5278 for (i = 0; i < size; i++)
5154 vec->contents[i] = Fpurecopy (AREF (obj, i)); 5279 vec->contents[i] = purecopy (vec->contents[i]);
5155 if (COMPILEDP (obj)) 5280 XSETVECTOR (obj, vec);
5156 { 5281 }
5157 XSETPVECTYPE (vec, PVEC_COMPILED); 5282 else if (SYMBOLP (obj))
5158 XSETCOMPILED (obj, vec); 5283 {
5284 if (!XSYMBOL (obj)->pinned && !c_symbol_p (XSYMBOL (obj)))
5285 { /* We can't purify them, but they appear in many pure objects.
5286 Mark them as `pinned' so we know to mark them at every GC cycle. */
5287 XSYMBOL (obj)->pinned = true;
5288 symbol_block_pinned = symbol_block;
5159 } 5289 }
5160 else 5290 /* Don't hash-cons it. */
5161 XSETVECTOR (obj, vec); 5291 return obj;
5162 } 5292 }
5163 else if (MARKERP (obj))
5164 error ("Attempt to copy a marker to pure storage");
5165 else 5293 else
5166 /* Not purified, don't hash-cons. */ 5294 {
5167 return obj; 5295 Lisp_Object fmt = build_pure_c_string ("Don't know how to purify: %S");
5296 Fsignal (Qerror, list1 (CALLN (Fformat, fmt, obj)));
5297 }
5168 5298
5169 if (HASH_TABLE_P (Vpurify_flag)) /* Hash consing. */ 5299 if (HASH_TABLE_P (Vpurify_flag)) /* Hash consing. */
5170 Fputhash (obj, obj, Vpurify_flag); 5300 Fputhash (obj, obj, Vpurify_flag);
@@ -5231,29 +5361,136 @@ total_bytes_of_live_objects (void)
5231 return tot; 5361 return tot;
5232} 5362}
5233 5363
5234DEFUN ("garbage-collect", Fgarbage_collect, Sgarbage_collect, 0, 0, "", 5364#ifdef HAVE_WINDOW_SYSTEM
5235 doc: /* Reclaim storage for Lisp objects no longer needed. 5365
5236Garbage collection happens automatically if you cons more than 5366/* This code has a few issues on MS-Windows, see Bug#15876 and Bug#16140. */
5237`gc-cons-threshold' bytes of Lisp data since previous garbage collection. 5367
5238`garbage-collect' normally returns a list with info on amount of space in use, 5368#if !defined (HAVE_NTGUI)
5239where each entry has the form (NAME SIZE USED FREE), where: 5369
5240- NAME is a symbol describing the kind of objects this entry represents, 5370/* Remove unmarked font-spec and font-entity objects from ENTRY, which is
5241- SIZE is the number of bytes used by each one, 5371 (DRIVER-TYPE NUM-FRAMES FONT-CACHE-DATA ...), and return changed entry. */
5242- USED is the number of those objects that were found live in the heap, 5372
5243- FREE is the number of those objects that are not live but that Emacs 5373static Lisp_Object
5244 keeps around for future allocations (maybe because it does not know how 5374compact_font_cache_entry (Lisp_Object entry)
5245 to return them to the OS). 5375{
5246However, if there was overflow in pure space, `garbage-collect' 5376 Lisp_Object tail, *prev = &entry;
5247returns nil, because real GC can't be done. 5377
5248See Info node `(elisp)Garbage Collection'. */) 5378 for (tail = entry; CONSP (tail); tail = XCDR (tail))
5249 (void) 5379 {
5380 bool drop = 0;
5381 Lisp_Object obj = XCAR (tail);
5382
5383 /* Consider OBJ if it is (font-spec . [font-entity font-entity ...]). */
5384 if (CONSP (obj) && FONT_SPEC_P (XCAR (obj))
5385 && !VECTOR_MARKED_P (XFONT_SPEC (XCAR (obj)))
5386 && VECTORP (XCDR (obj)))
5387 {
5388 ptrdiff_t i, size = ASIZE (XCDR (obj)) & ~ARRAY_MARK_FLAG;
5389
5390 /* If font-spec is not marked, most likely all font-entities
5391 are not marked too. But we must be sure that nothing is
5392 marked within OBJ before we really drop it. */
5393 for (i = 0; i < size; i++)
5394 if (VECTOR_MARKED_P (XFONT_ENTITY (AREF (XCDR (obj), i))))
5395 break;
5396
5397 if (i == size)
5398 drop = 1;
5399 }
5400 if (drop)
5401 *prev = XCDR (tail);
5402 else
5403 prev = xcdr_addr (tail);
5404 }
5405 return entry;
5406}
5407
5408#endif /* not HAVE_NTGUI */
5409
5410/* Compact font caches on all terminals and mark
5411 everything which is still here after compaction. */
5412
5413static void
5414compact_font_caches (void)
5415{
5416 struct terminal *t;
5417
5418 for (t = terminal_list; t; t = t->next_terminal)
5419 {
5420 Lisp_Object cache = TERMINAL_FONT_CACHE (t);
5421#if !defined (HAVE_NTGUI)
5422 if (CONSP (cache))
5423 {
5424 Lisp_Object entry;
5425
5426 for (entry = XCDR (cache); CONSP (entry); entry = XCDR (entry))
5427 XSETCAR (entry, compact_font_cache_entry (XCAR (entry)));
5428 }
5429#endif /* not HAVE_NTGUI */
5430 mark_object (cache);
5431 }
5432}
5433
5434#else /* not HAVE_WINDOW_SYSTEM */
5435
5436#define compact_font_caches() (void)(0)
5437
5438#endif /* HAVE_WINDOW_SYSTEM */
5439
5440/* Remove (MARKER . DATA) entries with unmarked MARKER
5441 from buffer undo LIST and return changed list. */
5442
5443static Lisp_Object
5444compact_undo_list (Lisp_Object list)
5445{
5446 Lisp_Object tail, *prev = &list;
5447
5448 for (tail = list; CONSP (tail); tail = XCDR (tail))
5449 {
5450 if (CONSP (XCAR (tail))
5451 && MARKERP (XCAR (XCAR (tail)))
5452 && !XMARKER (XCAR (XCAR (tail)))->gcmarkbit)
5453 *prev = XCDR (tail);
5454 else
5455 prev = xcdr_addr (tail);
5456 }
5457 return list;
5458}
5459
5460static void
5461mark_pinned_symbols (void)
5462{
5463 struct symbol_block *sblk;
5464 int lim = (symbol_block_pinned == symbol_block
5465 ? symbol_block_index : SYMBOL_BLOCK_SIZE);
5466
5467 for (sblk = symbol_block_pinned; sblk; sblk = sblk->next)
5468 {
5469 union aligned_Lisp_Symbol *sym = sblk->symbols, *end = sym + lim;
5470 for (; sym < end; ++sym)
5471 if (sym->s.pinned)
5472 mark_object (make_lisp_symbol (&sym->s));
5473
5474 lim = SYMBOL_BLOCK_SIZE;
5475 }
5476}
5477
5478/* Subroutine of Fgarbage_collect that does most of the work. It is a
5479 separate function so that we could limit mark_stack in searching
5480 the stack frames below this function, thus avoiding the rare cases
5481 where mark_stack finds values that look like live Lisp objects on
5482 portions of stack that couldn't possibly contain such live objects.
5483 For more details of this, see the discussion at
5484 http://lists.gnu.org/archive/html/emacs-devel/2014-05/msg00270.html. */
5485static Lisp_Object
5486garbage_collect_1 (void *end)
5250{ 5487{
5251 struct buffer *nextb; 5488 struct buffer *nextb;
5252 char stack_top_variable; 5489 char stack_top_variable;
5253 ptrdiff_t i; 5490 ptrdiff_t i;
5254 bool message_p; 5491 bool message_p;
5255 ptrdiff_t count = SPECPDL_INDEX (); 5492 ptrdiff_t count = SPECPDL_INDEX ();
5256 EMACS_TIME start; 5493 struct timespec start;
5257 Lisp_Object retval = Qnil; 5494 Lisp_Object retval = Qnil;
5258 size_t tot_before = 0; 5495 size_t tot_before = 0;
5259 5496
@@ -5266,7 +5503,7 @@ See Info node `(elisp)Garbage Collection'. */)
5266 return Qnil; 5503 return Qnil;
5267 5504
5268 /* Record this function, so it appears on the profiler's backtraces. */ 5505 /* Record this function, so it appears on the profiler's backtraces. */
5269 record_in_backtrace (Qautomatic_gc, &Qnil, 0); 5506 record_in_backtrace (Qautomatic_gc, 0, 0);
5270 5507
5271 check_cons_list (); 5508 check_cons_list ();
5272 5509
@@ -5278,7 +5515,7 @@ See Info node `(elisp)Garbage Collection'. */)
5278 if (profiler_memory_running) 5515 if (profiler_memory_running)
5279 tot_before = total_bytes_of_live_objects (); 5516 tot_before = total_bytes_of_live_objects ();
5280 5517
5281 start = current_emacs_time (); 5518 start = current_timespec ();
5282 5519
5283 /* In case user calls debug_print during GC, 5520 /* In case user calls debug_print during GC,
5284 don't let that cause a recursive GC. */ 5521 don't let that cause a recursive GC. */
@@ -5311,7 +5548,7 @@ See Info node `(elisp)Garbage Collection'. */)
5311 stack_copy = xrealloc (stack_copy, stack_size); 5548 stack_copy = xrealloc (stack_copy, stack_size);
5312 stack_copy_size = stack_size; 5549 stack_copy_size = stack_size;
5313 } 5550 }
5314 memcpy (stack_copy, stack, stack_size); 5551 no_sanitize_memcpy (stack_copy, stack, stack_size);
5315 } 5552 }
5316 } 5553 }
5317#endif /* MAX_SAVE_STACK > 0 */ 5554#endif /* MAX_SAVE_STACK > 0 */
@@ -5330,12 +5567,16 @@ See Info node `(elisp)Garbage Collection'. */)
5330 mark_buffer (&buffer_defaults); 5567 mark_buffer (&buffer_defaults);
5331 mark_buffer (&buffer_local_symbols); 5568 mark_buffer (&buffer_local_symbols);
5332 5569
5570 for (i = 0; i < ARRAYELTS (lispsym); i++)
5571 mark_object (builtin_lisp_symbol (i));
5572
5333 for (i = 0; i < staticidx; i++) 5573 for (i = 0; i < staticidx; i++)
5334 mark_object (*staticvec[i]); 5574 mark_object (*staticvec[i]);
5335 5575
5336 mark_threads (); 5576 mark_pinned_symbols ();
5337 mark_terminals (); 5577 mark_terminals ();
5338 mark_kboards (); 5578 mark_kboards ();
5579 mark_threads ();
5339 5580
5340#ifdef USE_GTK 5581#ifdef USE_GTK
5341 xg_mark_data (); 5582 xg_mark_data ();
@@ -5345,65 +5586,39 @@ See Info node `(elisp)Garbage Collection'. */)
5345 mark_fringe_data (); 5586 mark_fringe_data ();
5346#endif 5587#endif
5347 5588
5348#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES 5589 /* Everything is now marked, except for the data in font caches,
5349 FIXME; 5590 undo lists, and finalizers. The first two are compacted by
5350 mark_stack (); 5591 removing an items which aren't reachable otherwise. */
5351#endif 5592
5593 compact_font_caches ();
5352 5594
5353 /* Everything is now marked, except for the things that require special
5354 finalization, i.e. the undo_list.
5355 Look thru every buffer's undo list
5356 for elements that update markers that were not marked,
5357 and delete them. */
5358 FOR_EACH_BUFFER (nextb) 5595 FOR_EACH_BUFFER (nextb)
5359 { 5596 {
5360 /* If a buffer's undo list is Qt, that means that undo is 5597 if (!EQ (BVAR (nextb, undo_list), Qt))
5361 turned off in that buffer. Calling truncate_undo_list on 5598 bset_undo_list (nextb, compact_undo_list (BVAR (nextb, undo_list)));
5362 Qt tends to return NULL, which effectively turns undo back on. 5599 /* Now that we have stripped the elements that need not be
5363 So don't call truncate_undo_list if undo_list is Qt. */ 5600 in the undo_list any more, we can finally mark the list. */
5364 if (! EQ (nextb->INTERNAL_FIELD (undo_list), Qt)) 5601 mark_object (BVAR (nextb, undo_list));
5365 {
5366 Lisp_Object tail, prev;
5367 tail = nextb->INTERNAL_FIELD (undo_list);
5368 prev = Qnil;
5369 while (CONSP (tail))
5370 {
5371 if (CONSP (XCAR (tail))
5372 && MARKERP (XCAR (XCAR (tail)))
5373 && !XMARKER (XCAR (XCAR (tail)))->gcmarkbit)
5374 {
5375 if (NILP (prev))
5376 nextb->INTERNAL_FIELD (undo_list) = tail = XCDR (tail);
5377 else
5378 {
5379 tail = XCDR (tail);
5380 XSETCDR (prev, tail);
5381 }
5382 }
5383 else
5384 {
5385 prev = tail;
5386 tail = XCDR (tail);
5387 }
5388 }
5389 }
5390 /* Now that we have stripped the elements that need not be in the
5391 undo_list any more, we can finally mark the list. */
5392 mark_object (nextb->INTERNAL_FIELD (undo_list));
5393 } 5602 }
5394 5603
5395 gc_sweep (); 5604 /* Now pre-sweep finalizers. Here, we add any unmarked finalizers
5605 to doomed_finalizers so we can run their associated functions
5606 after GC. It's important to scan finalizers at this stage so
5607 that we can be sure that unmarked finalizers are really
5608 unreachable except for references from their associated functions
5609 and from other finalizers. */
5396 5610
5397 /* Clear the mark bits that we set in certain root slots. */ 5611 queue_doomed_finalizers (&doomed_finalizers, &finalizers);
5612 mark_finalizer_list (&doomed_finalizers);
5613
5614 gc_sweep ();
5398 5615
5399 unmark_threads (); 5616 unmark_threads ();
5617
5618 /* Clear the mark bits that we set in certain root slots. */
5400 VECTOR_UNMARK (&buffer_defaults); 5619 VECTOR_UNMARK (&buffer_defaults);
5401 VECTOR_UNMARK (&buffer_local_symbols); 5620 VECTOR_UNMARK (&buffer_local_symbols);
5402 5621
5403#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES && 0
5404 dump_zombies ();
5405#endif
5406
5407 check_cons_list (); 5622 check_cons_list ();
5408 5623
5409 gc_in_progress = 0; 5624 gc_in_progress = 0;
@@ -5438,71 +5653,47 @@ See Info node `(elisp)Garbage Collection'. */)
5438 } 5653 }
5439 5654
5440 unbind_to (count, Qnil); 5655 unbind_to (count, Qnil);
5441 {
5442 Lisp_Object total[11];
5443 int total_size = 10;
5444
5445 total[0] = list4 (Qconses, make_number (sizeof (struct Lisp_Cons)),
5446 bounded_number (total_conses),
5447 bounded_number (total_free_conses));
5448
5449 total[1] = list4 (Qsymbols, make_number (sizeof (struct Lisp_Symbol)),
5450 bounded_number (total_symbols),
5451 bounded_number (total_free_symbols));
5452
5453 total[2] = list4 (Qmiscs, make_number (sizeof (union Lisp_Misc)),
5454 bounded_number (total_markers),
5455 bounded_number (total_free_markers));
5456
5457 total[3] = list4 (Qstrings, make_number (sizeof (struct Lisp_String)),
5458 bounded_number (total_strings),
5459 bounded_number (total_free_strings));
5460 5656
5461 total[4] = list3 (Qstring_bytes, make_number (1), 5657 Lisp_Object total[] = {
5462 bounded_number (total_string_bytes)); 5658 list4 (Qconses, make_number (sizeof (struct Lisp_Cons)),
5463 5659 bounded_number (total_conses),
5464 total[5] = list3 (Qvectors, 5660 bounded_number (total_free_conses)),
5465 make_number (header_size + sizeof (Lisp_Object)), 5661 list4 (Qsymbols, make_number (sizeof (struct Lisp_Symbol)),
5466 bounded_number (total_vectors)); 5662 bounded_number (total_symbols),
5467 5663 bounded_number (total_free_symbols)),
5468 total[6] = list4 (Qvector_slots, make_number (word_size), 5664 list4 (Qmiscs, make_number (sizeof (union Lisp_Misc)),
5469 bounded_number (total_vector_slots), 5665 bounded_number (total_markers),
5470 bounded_number (total_free_vector_slots)); 5666 bounded_number (total_free_markers)),
5471 5667 list4 (Qstrings, make_number (sizeof (struct Lisp_String)),
5472 total[7] = list4 (Qfloats, make_number (sizeof (struct Lisp_Float)), 5668 bounded_number (total_strings),
5473 bounded_number (total_floats), 5669 bounded_number (total_free_strings)),
5474 bounded_number (total_free_floats)); 5670 list3 (Qstring_bytes, make_number (1),
5475 5671 bounded_number (total_string_bytes)),
5476 total[8] = list4 (Qintervals, make_number (sizeof (struct interval)), 5672 list3 (Qvectors,
5477 bounded_number (total_intervals), 5673 make_number (header_size + sizeof (Lisp_Object)),
5478 bounded_number (total_free_intervals)); 5674 bounded_number (total_vectors)),
5479 5675 list4 (Qvector_slots, make_number (word_size),
5480 total[9] = list3 (Qbuffers, make_number (sizeof (struct buffer)), 5676 bounded_number (total_vector_slots),
5481 bounded_number (total_buffers)); 5677 bounded_number (total_free_vector_slots)),
5678 list4 (Qfloats, make_number (sizeof (struct Lisp_Float)),
5679 bounded_number (total_floats),
5680 bounded_number (total_free_floats)),
5681 list4 (Qintervals, make_number (sizeof (struct interval)),
5682 bounded_number (total_intervals),
5683 bounded_number (total_free_intervals)),
5684 list3 (Qbuffers, make_number (sizeof (struct buffer)),
5685 bounded_number (total_buffers)),
5482 5686
5483#ifdef DOUG_LEA_MALLOC 5687#ifdef DOUG_LEA_MALLOC
5484 total_size++; 5688 list4 (Qheap, make_number (1024),
5485 total[10] = list4 (Qheap, make_number (1024), 5689 bounded_number ((mallinfo ().uordblks + 1023) >> 10),
5486 bounded_number ((mallinfo ().uordblks + 1023) >> 10), 5690 bounded_number ((mallinfo ().fordblks + 1023) >> 10)),
5487 bounded_number ((mallinfo ().fordblks + 1023) >> 10));
5488#endif 5691#endif
5489 retval = Flist (total_size, total); 5692 };
5490 } 5693 retval = CALLMANY (Flist, total);
5491 5694
5492#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES 5695 /* GC is complete: now we can run our finalizer callbacks. */
5493 { 5696 run_finalizers (&doomed_finalizers);
5494 /* Compute average percentage of zombies. */
5495 double nlive
5496 = (total_conses + total_symbols + total_markers + total_strings
5497 + total_vectors + total_floats + total_intervals + total_buffers);
5498
5499 avg_live = (avg_live * ngcs + nlive) / (ngcs + 1);
5500 max_live = max (nlive, max_live);
5501 avg_zombies = (avg_zombies * ngcs + nzombies) / (ngcs + 1);
5502 max_zombies = max (nzombies, max_zombies);
5503 ++ngcs;
5504 }
5505#endif
5506 5697
5507 if (!NILP (Vpost_gc_hook)) 5698 if (!NILP (Vpost_gc_hook))
5508 { 5699 {
@@ -5514,9 +5705,9 @@ See Info node `(elisp)Garbage Collection'. */)
5514 /* Accumulate statistics. */ 5705 /* Accumulate statistics. */
5515 if (FLOATP (Vgc_elapsed)) 5706 if (FLOATP (Vgc_elapsed))
5516 { 5707 {
5517 EMACS_TIME since_start = sub_emacs_time (current_emacs_time (), start); 5708 struct timespec since_start = timespec_sub (current_timespec (), start);
5518 Vgc_elapsed = make_float (XFLOAT_DATA (Vgc_elapsed) 5709 Vgc_elapsed = make_float (XFLOAT_DATA (Vgc_elapsed)
5519 + EMACS_TIME_TO_DOUBLE (since_start)); 5710 + timespectod (since_start));
5520 } 5711 }
5521 5712
5522 gcs_done++; 5713 gcs_done++;
@@ -5534,6 +5725,78 @@ See Info node `(elisp)Garbage Collection'. */)
5534 return retval; 5725 return retval;
5535} 5726}
5536 5727
5728DEFUN ("garbage-collect", Fgarbage_collect, Sgarbage_collect, 0, 0, "",
5729 doc: /* Reclaim storage for Lisp objects no longer needed.
5730Garbage collection happens automatically if you cons more than
5731`gc-cons-threshold' bytes of Lisp data since previous garbage collection.
5732`garbage-collect' normally returns a list with info on amount of space in use,
5733where each entry has the form (NAME SIZE USED FREE), where:
5734- NAME is a symbol describing the kind of objects this entry represents,
5735- SIZE is the number of bytes used by each one,
5736- USED is the number of those objects that were found live in the heap,
5737- FREE is the number of those objects that are not live but that Emacs
5738 keeps around for future allocations (maybe because it does not know how
5739 to return them to the OS).
5740However, if there was overflow in pure space, `garbage-collect'
5741returns nil, because real GC can't be done.
5742See Info node `(elisp)Garbage Collection'. */)
5743 (void)
5744{
5745 void *end;
5746
5747#ifdef HAVE___BUILTIN_UNWIND_INIT
5748 /* Force callee-saved registers and register windows onto the stack.
5749 This is the preferred method if available, obviating the need for
5750 machine dependent methods. */
5751 __builtin_unwind_init ();
5752 end = &end;
5753#else /* not HAVE___BUILTIN_UNWIND_INIT */
5754#ifndef GC_SAVE_REGISTERS_ON_STACK
5755 /* jmp_buf may not be aligned enough on darwin-ppc64 */
5756 union aligned_jmpbuf {
5757 Lisp_Object o;
5758 sys_jmp_buf j;
5759 } j;
5760 volatile bool stack_grows_down_p = (char *) &j > (char *) stack_base;
5761#endif
5762 /* This trick flushes the register windows so that all the state of
5763 the process is contained in the stack. */
5764 /* Fixme: Code in the Boehm GC suggests flushing (with `flushrs') is
5765 needed on ia64 too. See mach_dep.c, where it also says inline
5766 assembler doesn't work with relevant proprietary compilers. */
5767#ifdef __sparc__
5768#if defined (__sparc64__) && defined (__FreeBSD__)
5769 /* FreeBSD does not have a ta 3 handler. */
5770 asm ("flushw");
5771#else
5772 asm ("ta 3");
5773#endif
5774#endif
5775
5776 /* Save registers that we need to see on the stack. We need to see
5777 registers used to hold register variables and registers used to
5778 pass parameters. */
5779#ifdef GC_SAVE_REGISTERS_ON_STACK
5780 GC_SAVE_REGISTERS_ON_STACK (end);
5781#else /* not GC_SAVE_REGISTERS_ON_STACK */
5782
5783#ifndef GC_SETJMP_WORKS /* If it hasn't been checked yet that
5784 setjmp will definitely work, test it
5785 and print a message with the result
5786 of the test. */
5787 if (!setjmp_tested_p)
5788 {
5789 setjmp_tested_p = 1;
5790 test_setjmp ();
5791 }
5792#endif /* GC_SETJMP_WORKS */
5793
5794 sys_setjmp (j.j);
5795 end = stack_grows_down_p ? (char *) &j + sizeof j : (char *) &j;
5796#endif /* not GC_SAVE_REGISTERS_ON_STACK */
5797#endif /* not HAVE___BUILTIN_UNWIND_INIT */
5798 return garbage_collect_1 (end);
5799}
5537 5800
5538/* Mark Lisp objects in glyph matrix MATRIX. Currently the 5801/* Mark Lisp objects in glyph matrix MATRIX. Currently the
5539 only interesting objects referenced from glyphs are strings. */ 5802 only interesting objects referenced from glyphs are strings. */
@@ -5561,30 +5824,6 @@ mark_glyph_matrix (struct glyph_matrix *matrix)
5561 } 5824 }
5562} 5825}
5563 5826
5564
5565/* Mark Lisp faces in the face cache C. */
5566
5567static void
5568mark_face_cache (struct face_cache *c)
5569{
5570 if (c)
5571 {
5572 int i, j;
5573 for (i = 0; i < c->used; ++i)
5574 {
5575 struct face *face = FACE_FROM_ID (c->f, i);
5576
5577 if (face)
5578 {
5579 for (j = 0; j < LFACE_VECTOR_SIZE; ++j)
5580 mark_object (face->lface[j]);
5581 }
5582 }
5583 }
5584}
5585
5586
5587
5588/* Mark reference to a Lisp_Object. 5827/* Mark reference to a Lisp_Object.
5589 If the object referred to has not been seen yet, recursively mark 5828 If the object referred to has not been seen yet, recursively mark
5590 all the references contained in it. */ 5829 all the references contained in it. */
@@ -5623,14 +5862,15 @@ mark_vectorlike (struct Lisp_Vector *ptr)
5623 symbols. */ 5862 symbols. */
5624 5863
5625static void 5864static void
5626mark_char_table (struct Lisp_Vector *ptr) 5865mark_char_table (struct Lisp_Vector *ptr, enum pvec_type pvectype)
5627{ 5866{
5628 int size = ptr->header.size & PSEUDOVECTOR_SIZE_MASK; 5867 int size = ptr->header.size & PSEUDOVECTOR_SIZE_MASK;
5629 int i; 5868 /* Consult the Lisp_Sub_Char_Table layout before changing this. */
5869 int i, idx = (pvectype == PVEC_SUB_CHAR_TABLE ? SUB_CHAR_TABLE_OFFSET : 0);
5630 5870
5631 eassert (!VECTOR_MARKED_P (ptr)); 5871 eassert (!VECTOR_MARKED_P (ptr));
5632 VECTOR_MARK (ptr); 5872 VECTOR_MARK (ptr);
5633 for (i = 0; i < size; i++) 5873 for (i = idx; i < size; i++)
5634 { 5874 {
5635 Lisp_Object val = ptr->contents[i]; 5875 Lisp_Object val = ptr->contents[i];
5636 5876
@@ -5639,13 +5879,26 @@ mark_char_table (struct Lisp_Vector *ptr)
5639 if (SUB_CHAR_TABLE_P (val)) 5879 if (SUB_CHAR_TABLE_P (val))
5640 { 5880 {
5641 if (! VECTOR_MARKED_P (XVECTOR (val))) 5881 if (! VECTOR_MARKED_P (XVECTOR (val)))
5642 mark_char_table (XVECTOR (val)); 5882 mark_char_table (XVECTOR (val), PVEC_SUB_CHAR_TABLE);
5643 } 5883 }
5644 else 5884 else
5645 mark_object (val); 5885 mark_object (val);
5646 } 5886 }
5647} 5887}
5648 5888
5889NO_INLINE /* To reduce stack depth in mark_object. */
5890static Lisp_Object
5891mark_compiled (struct Lisp_Vector *ptr)
5892{
5893 int i, size = ptr->header.size & PSEUDOVECTOR_SIZE_MASK;
5894
5895 VECTOR_MARK (ptr);
5896 for (i = 0; i < size; i++)
5897 if (i != COMPILED_CONSTANTS)
5898 mark_object (ptr->contents[i]);
5899 return size > COMPILED_CONSTANTS ? ptr->contents[COMPILED_CONSTANTS] : Qnil;
5900}
5901
5649/* Mark the chain of overlays starting at PTR. */ 5902/* Mark the chain of overlays starting at PTR. */
5650 5903
5651static void 5904static void
@@ -5654,8 +5907,9 @@ mark_overlay (struct Lisp_Overlay *ptr)
5654 for (; ptr && !ptr->gcmarkbit; ptr = ptr->next) 5907 for (; ptr && !ptr->gcmarkbit; ptr = ptr->next)
5655 { 5908 {
5656 ptr->gcmarkbit = 1; 5909 ptr->gcmarkbit = 1;
5657 mark_object (ptr->start); 5910 /* These two are always markers and can be marked fast. */
5658 mark_object (ptr->end); 5911 XMARKER (ptr->start)->gcmarkbit = 1;
5912 XMARKER (ptr->end)->gcmarkbit = 1;
5659 mark_object (ptr->plist); 5913 mark_object (ptr->plist);
5660 } 5914 }
5661} 5915}
@@ -5684,6 +5938,73 @@ mark_buffer (struct buffer *buffer)
5684 mark_buffer (buffer->base_buffer); 5938 mark_buffer (buffer->base_buffer);
5685} 5939}
5686 5940
5941/* Mark Lisp faces in the face cache C. */
5942
5943NO_INLINE /* To reduce stack depth in mark_object. */
5944static void
5945mark_face_cache (struct face_cache *c)
5946{
5947 if (c)
5948 {
5949 int i, j;
5950 for (i = 0; i < c->used; ++i)
5951 {
5952 struct face *face = FACE_FROM_ID (c->f, i);
5953
5954 if (face)
5955 {
5956 if (face->font && !VECTOR_MARKED_P (face->font))
5957 mark_vectorlike ((struct Lisp_Vector *) face->font);
5958
5959 for (j = 0; j < LFACE_VECTOR_SIZE; ++j)
5960 mark_object (face->lface[j]);
5961 }
5962 }
5963 }
5964}
5965
5966NO_INLINE /* To reduce stack depth in mark_object. */
5967static void
5968mark_localized_symbol (struct Lisp_Symbol *ptr)
5969{
5970 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (ptr);
5971 Lisp_Object where = blv->where;
5972 /* If the value is set up for a killed buffer or deleted
5973 frame, restore its global binding. If the value is
5974 forwarded to a C variable, either it's not a Lisp_Object
5975 var, or it's staticpro'd already. */
5976 if ((BUFFERP (where) && !BUFFER_LIVE_P (XBUFFER (where)))
5977 || (FRAMEP (where) && !FRAME_LIVE_P (XFRAME (where))))
5978 swap_in_global_binding (ptr);
5979 mark_object (blv->where);
5980 mark_object (blv->valcell);
5981 mark_object (blv->defcell);
5982}
5983
5984NO_INLINE /* To reduce stack depth in mark_object. */
5985static void
5986mark_save_value (struct Lisp_Save_Value *ptr)
5987{
5988 /* If `save_type' is zero, `data[0].pointer' is the address
5989 of a memory area containing `data[1].integer' potential
5990 Lisp_Objects. */
5991 if (ptr->save_type == SAVE_TYPE_MEMORY)
5992 {
5993 Lisp_Object *p = ptr->data[0].pointer;
5994 ptrdiff_t nelt;
5995 for (nelt = ptr->data[1].integer; nelt > 0; nelt--, p++)
5996 mark_maybe_object (*p);
5997 }
5998 else
5999 {
6000 /* Find Lisp_Objects in `data[N]' slots and mark them. */
6001 int i;
6002 for (i = 0; i < SAVE_VALUE_SLOTS; i++)
6003 if (save_type (ptr, i) == SAVE_OBJECT)
6004 mark_object (ptr->data[i].object);
6005 }
6006}
6007
5687/* Remove killed buffers or items whose car is a killed buffer from 6008/* Remove killed buffers or items whose car is a killed buffer from
5688 LIST, and mark other items. Return changed LIST, which is marked. */ 6009 LIST, and mark other items. Return changed LIST, which is marked. */
5689 6010
@@ -5711,21 +6032,29 @@ mark_discard_killed_buffers (Lisp_Object list)
5711 return list; 6032 return list;
5712} 6033}
5713 6034
5714/* Determine type of generic Lisp_Object and mark it accordingly. */ 6035/* Determine type of generic Lisp_Object and mark it accordingly.
6036
6037 This function implements a straightforward depth-first marking
6038 algorithm and so the recursion depth may be very high (a few
6039 tens of thousands is not uncommon). To minimize stack usage,
6040 a few cold paths are moved out to NO_INLINE functions above.
6041 In general, inlining them doesn't help you to gain more speed. */
5715 6042
5716void 6043void
5717mark_object (Lisp_Object arg) 6044mark_object (Lisp_Object arg)
5718{ 6045{
5719 register Lisp_Object obj = arg; 6046 register Lisp_Object obj;
5720#ifdef GC_CHECK_MARKED_OBJECTS
5721 void *po; 6047 void *po;
6048#ifdef GC_CHECK_MARKED_OBJECTS
5722 struct mem_node *m; 6049 struct mem_node *m;
5723#endif 6050#endif
5724 ptrdiff_t cdr_count = 0; 6051 ptrdiff_t cdr_count = 0;
5725 6052
6053 obj = arg;
5726 loop: 6054 loop:
5727 6055
5728 if (PURE_POINTER_P (XPNTR (obj))) 6056 po = XPNTR (obj);
6057 if (PURE_P (po))
5729 return; 6058 return;
5730 6059
5731 last_marked[last_marked_index++] = obj; 6060 last_marked[last_marked_index++] = obj;
@@ -5734,11 +6063,9 @@ mark_object (Lisp_Object arg)
5734 6063
5735 /* Perform some sanity checks on the objects marked here. Abort if 6064 /* Perform some sanity checks on the objects marked here. Abort if
5736 we encounter an object we know is bogus. This increases GC time 6065 we encounter an object we know is bogus. This increases GC time
5737 by ~80%, and requires compilation with GC_MARK_STACK != 0. */ 6066 by ~80%. */
5738#ifdef GC_CHECK_MARKED_OBJECTS 6067#ifdef GC_CHECK_MARKED_OBJECTS
5739 6068
5740 po = (void *) XPNTR (obj);
5741
5742 /* Check that the object pointed to by PO is known to be a Lisp 6069 /* Check that the object pointed to by PO is known to be a Lisp
5743 structure allocated from the heap. */ 6070 structure allocated from the heap. */
5744#define CHECK_ALLOCATED() \ 6071#define CHECK_ALLOCATED() \
@@ -5756,17 +6083,28 @@ mark_object (Lisp_Object arg)
5756 emacs_abort (); \ 6083 emacs_abort (); \
5757 } while (0) 6084 } while (0)
5758 6085
5759 /* Check both of the above conditions. */ 6086 /* Check both of the above conditions, for non-symbols. */
5760#define CHECK_ALLOCATED_AND_LIVE(LIVEP) \ 6087#define CHECK_ALLOCATED_AND_LIVE(LIVEP) \
5761 do { \ 6088 do { \
5762 CHECK_ALLOCATED (); \ 6089 CHECK_ALLOCATED (); \
5763 CHECK_LIVE (LIVEP); \ 6090 CHECK_LIVE (LIVEP); \
5764 } while (0) \ 6091 } while (0) \
5765 6092
6093 /* Check both of the above conditions, for symbols. */
6094#define CHECK_ALLOCATED_AND_LIVE_SYMBOL() \
6095 do { \
6096 if (!c_symbol_p (ptr)) \
6097 { \
6098 CHECK_ALLOCATED (); \
6099 CHECK_LIVE (live_symbol_p); \
6100 } \
6101 } while (0) \
6102
5766#else /* not GC_CHECK_MARKED_OBJECTS */ 6103#else /* not GC_CHECK_MARKED_OBJECTS */
5767 6104
5768#define CHECK_LIVE(LIVEP) (void) 0 6105#define CHECK_LIVE(LIVEP) ((void) 0)
5769#define CHECK_ALLOCATED_AND_LIVE(LIVEP) (void) 0 6106#define CHECK_ALLOCATED_AND_LIVE(LIVEP) ((void) 0)
6107#define CHECK_ALLOCATED_AND_LIVE_SYMBOL() ((void) 0)
5770 6108
5771#endif /* not GC_CHECK_MARKED_OBJECTS */ 6109#endif /* not GC_CHECK_MARKED_OBJECTS */
5772 6110
@@ -5828,27 +6166,31 @@ mark_object (Lisp_Object arg)
5828 break; 6166 break;
5829 6167
5830 case PVEC_COMPILED: 6168 case PVEC_COMPILED:
5831 { /* We could treat this just like a vector, but it is better 6169 /* Although we could treat this just like a vector, mark_compiled
5832 to save the COMPILED_CONSTANTS element for last and avoid 6170 returns the COMPILED_CONSTANTS element, which is marked at the
5833 recursion there. */ 6171 next iteration of goto-loop here. This is done to avoid a few
5834 int size = ptr->header.size & PSEUDOVECTOR_SIZE_MASK; 6172 recursive calls to mark_object. */
5835 int i; 6173 obj = mark_compiled (ptr);
5836 6174 if (!NILP (obj))
5837 VECTOR_MARK (ptr); 6175 goto loop;
5838 for (i = 0; i < size; i++)
5839 if (i != COMPILED_CONSTANTS)
5840 mark_object (ptr->contents[i]);
5841 if (size > COMPILED_CONSTANTS)
5842 {
5843 obj = ptr->contents[COMPILED_CONSTANTS];
5844 goto loop;
5845 }
5846 }
5847 break; 6176 break;
5848 6177
5849 case PVEC_FRAME: 6178 case PVEC_FRAME:
5850 mark_vectorlike (ptr); 6179 {
5851 mark_face_cache (((struct frame *) ptr)->face_cache); 6180 struct frame *f = (struct frame *) ptr;
6181
6182 mark_vectorlike (ptr);
6183 mark_face_cache (f->face_cache);
6184#ifdef HAVE_WINDOW_SYSTEM
6185 if (FRAME_WINDOW_P (f) && FRAME_X_OUTPUT (f))
6186 {
6187 struct font *font = FRAME_FONT (f);
6188
6189 if (font && !VECTOR_MARKED_P (font))
6190 mark_vectorlike ((struct Lisp_Vector *) font);
6191 }
6192#endif
6193 }
5852 break; 6194 break;
5853 6195
5854 case PVEC_WINDOW: 6196 case PVEC_WINDOW:
@@ -5895,7 +6237,8 @@ mark_object (Lisp_Object arg)
5895 break; 6237 break;
5896 6238
5897 case PVEC_CHAR_TABLE: 6239 case PVEC_CHAR_TABLE:
5898 mark_char_table (ptr); 6240 case PVEC_SUB_CHAR_TABLE:
6241 mark_char_table (ptr, (enum pvec_type) pvectype);
5899 break; 6242 break;
5900 6243
5901 case PVEC_BOOL_VECTOR: 6244 case PVEC_BOOL_VECTOR:
@@ -5918,12 +6261,13 @@ mark_object (Lisp_Object arg)
5918 case Lisp_Symbol: 6261 case Lisp_Symbol:
5919 { 6262 {
5920 register struct Lisp_Symbol *ptr = XSYMBOL (obj); 6263 register struct Lisp_Symbol *ptr = XSYMBOL (obj);
5921 struct Lisp_Symbol *ptrx; 6264 nextsym:
5922
5923 if (ptr->gcmarkbit) 6265 if (ptr->gcmarkbit)
5924 break; 6266 break;
5925 CHECK_ALLOCATED_AND_LIVE (live_symbol_p); 6267 CHECK_ALLOCATED_AND_LIVE_SYMBOL ();
5926 ptr->gcmarkbit = 1; 6268 ptr->gcmarkbit = 1;
6269 /* Attempt to catch bogus objects. */
6270 eassert (valid_lisp_object_p (ptr->function));
5927 mark_object (ptr->function); 6271 mark_object (ptr->function);
5928 mark_object (ptr->plist); 6272 mark_object (ptr->plist);
5929 switch (ptr->redirect) 6273 switch (ptr->redirect)
@@ -5937,21 +6281,8 @@ mark_object (Lisp_Object arg)
5937 break; 6281 break;
5938 } 6282 }
5939 case SYMBOL_LOCALIZED: 6283 case SYMBOL_LOCALIZED:
5940 { 6284 mark_localized_symbol (ptr);
5941 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (ptr); 6285 break;
5942 Lisp_Object where = blv->where;
5943 /* If the value is set up for a killed buffer or deleted
5944 frame, restore it's global binding. If the value is
5945 forwarded to a C variable, either it's not a Lisp_Object
5946 var, or it's staticpro'd already. */
5947 if ((BUFFERP (where) && !BUFFER_LIVE_P (XBUFFER (where)))
5948 || (FRAMEP (where) && !FRAME_LIVE_P (XFRAME (where))))
5949 swap_in_global_binding (ptr);
5950 mark_object (blv->where);
5951 mark_object (blv->valcell);
5952 mark_object (blv->defcell);
5953 break;
5954 }
5955 case SYMBOL_FORWARDED: 6286 case SYMBOL_FORWARDED:
5956 /* If the value is forwarded to a buffer or keyboard field, 6287 /* If the value is forwarded to a buffer or keyboard field,
5957 these are marked when we see the corresponding object. 6288 these are marked when we see the corresponding object.
@@ -5960,17 +6291,13 @@ mark_object (Lisp_Object arg)
5960 break; 6291 break;
5961 default: emacs_abort (); 6292 default: emacs_abort ();
5962 } 6293 }
5963 if (!PURE_POINTER_P (XSTRING (ptr->name))) 6294 if (!PURE_P (XSTRING (ptr->name)))
5964 MARK_STRING (XSTRING (ptr->name)); 6295 MARK_STRING (XSTRING (ptr->name));
5965 MARK_INTERVAL_TREE (string_intervals (ptr->name)); 6296 MARK_INTERVAL_TREE (string_intervals (ptr->name));
5966 6297 /* Inner loop to mark next symbol in this bucket, if any. */
5967 ptr = ptr->next; 6298 po = ptr = ptr->next;
5968 if (ptr) 6299 if (ptr)
5969 { 6300 goto nextsym;
5970 ptrx = ptr; /* Use of ptrx avoids compiler bug on Sun. */
5971 XSETSYMBOL (obj, ptrx);
5972 goto loop;
5973 }
5974 } 6301 }
5975 break; 6302 break;
5976 6303
@@ -5991,32 +6318,17 @@ mark_object (Lisp_Object arg)
5991 6318
5992 case Lisp_Misc_Save_Value: 6319 case Lisp_Misc_Save_Value:
5993 XMISCANY (obj)->gcmarkbit = 1; 6320 XMISCANY (obj)->gcmarkbit = 1;
5994 { 6321 mark_save_value (XSAVE_VALUE (obj));
5995 struct Lisp_Save_Value *ptr = XSAVE_VALUE (obj);
5996 /* If `save_type' is zero, `data[0].pointer' is the address
5997 of a memory area containing `data[1].integer' potential
5998 Lisp_Objects. */
5999 if (GC_MARK_STACK && ptr->save_type == SAVE_TYPE_MEMORY)
6000 {
6001 Lisp_Object *p = ptr->data[0].pointer;
6002 ptrdiff_t nelt;
6003 for (nelt = ptr->data[1].integer; nelt > 0; nelt--, p++)
6004 mark_maybe_object (*p);
6005 }
6006 else
6007 {
6008 /* Find Lisp_Objects in `data[N]' slots and mark them. */
6009 int i;
6010 for (i = 0; i < SAVE_VALUE_SLOTS; i++)
6011 if (save_type (ptr, i) == SAVE_OBJECT)
6012 mark_object (ptr->data[i].object);
6013 }
6014 }
6015 break; 6322 break;
6016 6323
6017 case Lisp_Misc_Overlay: 6324 case Lisp_Misc_Overlay:
6018 mark_overlay (XOVERLAY (obj)); 6325 mark_overlay (XOVERLAY (obj));
6019 break; 6326 break;
6327
6328 case Lisp_Misc_Finalizer:
6329 XMISCANY (obj)->gcmarkbit = true;
6330 mark_object (XFINALIZER (obj)->function);
6331 break;
6020 6332
6021 default: 6333 default:
6022 emacs_abort (); 6334 emacs_abort ();
@@ -6126,343 +6438,403 @@ survives_gc_p (Lisp_Object obj)
6126 emacs_abort (); 6438 emacs_abort ();
6127 } 6439 }
6128 6440
6129 return survives_p || PURE_POINTER_P ((void *) XPNTR (obj)); 6441 return survives_p || PURE_P (XPNTR (obj));
6130} 6442}
6131 6443
6132 6444
6133 6445
6134/* Sweep: find all structures not marked, and free them. */
6135 6446
6447NO_INLINE /* For better stack traces */
6136static void 6448static void
6137gc_sweep (void) 6449sweep_conses (void)
6138{ 6450{
6139 /* Remove or mark entries in weak hash tables. 6451 struct cons_block *cblk;
6140 This must be done before any object is unmarked. */ 6452 struct cons_block **cprev = &cons_block;
6141 sweep_weak_hash_tables (); 6453 int lim = cons_block_index;
6454 EMACS_INT num_free = 0, num_used = 0;
6142 6455
6143 sweep_strings (); 6456 cons_free_list = 0;
6144 check_string_bytes (!noninteractive);
6145 6457
6146 /* Put all unmarked conses on free list */ 6458 for (cblk = cons_block; cblk; cblk = *cprev)
6147 { 6459 {
6148 register struct cons_block *cblk; 6460 int i = 0;
6149 struct cons_block **cprev = &cons_block; 6461 int this_free = 0;
6150 register int lim = cons_block_index; 6462 int ilim = (lim + BITS_PER_BITS_WORD - 1) / BITS_PER_BITS_WORD;
6151 EMACS_INT num_free = 0, num_used = 0; 6463
6464 /* Scan the mark bits an int at a time. */
6465 for (i = 0; i < ilim; i++)
6466 {
6467 if (cblk->gcmarkbits[i] == BITS_WORD_MAX)
6468 {
6469 /* Fast path - all cons cells for this int are marked. */
6470 cblk->gcmarkbits[i] = 0;
6471 num_used += BITS_PER_BITS_WORD;
6472 }
6473 else
6474 {
6475 /* Some cons cells for this int are not marked.
6476 Find which ones, and free them. */
6477 int start, pos, stop;
6478
6479 start = i * BITS_PER_BITS_WORD;
6480 stop = lim - start;
6481 if (stop > BITS_PER_BITS_WORD)
6482 stop = BITS_PER_BITS_WORD;
6483 stop += start;
6484
6485 for (pos = start; pos < stop; pos++)
6486 {
6487 if (!CONS_MARKED_P (&cblk->conses[pos]))
6488 {
6489 this_free++;
6490 cblk->conses[pos].u.chain = cons_free_list;
6491 cons_free_list = &cblk->conses[pos];
6492 cons_free_list->car = Vdead;
6493 }
6494 else
6495 {
6496 num_used++;
6497 CONS_UNMARK (&cblk->conses[pos]);
6498 }
6499 }
6500 }
6501 }
6152 6502
6153 cons_free_list = 0; 6503 lim = CONS_BLOCK_SIZE;
6504 /* If this block contains only free conses and we have already
6505 seen more than two blocks worth of free conses then deallocate
6506 this block. */
6507 if (this_free == CONS_BLOCK_SIZE && num_free > CONS_BLOCK_SIZE)
6508 {
6509 *cprev = cblk->next;
6510 /* Unhook from the free list. */
6511 cons_free_list = cblk->conses[0].u.chain;
6512 lisp_align_free (cblk);
6513 }
6514 else
6515 {
6516 num_free += this_free;
6517 cprev = &cblk->next;
6518 }
6519 }
6520 total_conses = num_used;
6521 total_free_conses = num_free;
6522}
6154 6523
6155 for (cblk = cons_block; cblk; cblk = *cprev) 6524NO_INLINE /* For better stack traces */
6156 { 6525static void
6157 register int i = 0; 6526sweep_floats (void)
6158 int this_free = 0; 6527{
6159 int ilim = (lim + BITS_PER_INT - 1) / BITS_PER_INT; 6528 register struct float_block *fblk;
6529 struct float_block **fprev = &float_block;
6530 register int lim = float_block_index;
6531 EMACS_INT num_free = 0, num_used = 0;
6160 6532
6161 /* Scan the mark bits an int at a time. */ 6533 float_free_list = 0;
6162 for (i = 0; i < ilim; i++)
6163 {
6164 if (cblk->gcmarkbits[i] == -1)
6165 {
6166 /* Fast path - all cons cells for this int are marked. */
6167 cblk->gcmarkbits[i] = 0;
6168 num_used += BITS_PER_INT;
6169 }
6170 else
6171 {
6172 /* Some cons cells for this int are not marked.
6173 Find which ones, and free them. */
6174 int start, pos, stop;
6175
6176 start = i * BITS_PER_INT;
6177 stop = lim - start;
6178 if (stop > BITS_PER_INT)
6179 stop = BITS_PER_INT;
6180 stop += start;
6181
6182 for (pos = start; pos < stop; pos++)
6183 {
6184 if (!CONS_MARKED_P (&cblk->conses[pos]))
6185 {
6186 this_free++;
6187 cblk->conses[pos].u.chain = cons_free_list;
6188 cons_free_list = &cblk->conses[pos];
6189#if GC_MARK_STACK
6190 cons_free_list->car = Vdead;
6191#endif
6192 }
6193 else
6194 {
6195 num_used++;
6196 CONS_UNMARK (&cblk->conses[pos]);
6197 }
6198 }
6199 }
6200 }
6201 6534
6202 lim = CONS_BLOCK_SIZE; 6535 for (fblk = float_block; fblk; fblk = *fprev)
6203 /* If this block contains only free conses and we have already 6536 {
6204 seen more than two blocks worth of free conses then deallocate 6537 register int i;
6205 this block. */ 6538 int this_free = 0;
6206 if (this_free == CONS_BLOCK_SIZE && num_free > CONS_BLOCK_SIZE) 6539 for (i = 0; i < lim; i++)
6207 { 6540 if (!FLOAT_MARKED_P (&fblk->floats[i]))
6208 *cprev = cblk->next; 6541 {
6209 /* Unhook from the free list. */ 6542 this_free++;
6210 cons_free_list = cblk->conses[0].u.chain; 6543 fblk->floats[i].u.chain = float_free_list;
6211 lisp_align_free (cblk); 6544 float_free_list = &fblk->floats[i];
6212 } 6545 }
6213 else 6546 else
6214 { 6547 {
6215 num_free += this_free; 6548 num_used++;
6216 cprev = &cblk->next; 6549 FLOAT_UNMARK (&fblk->floats[i]);
6217 } 6550 }
6218 } 6551 lim = FLOAT_BLOCK_SIZE;
6219 total_conses = num_used; 6552 /* If this block contains only free floats and we have already
6220 total_free_conses = num_free; 6553 seen more than two blocks worth of free floats then deallocate
6221 } 6554 this block. */
6555 if (this_free == FLOAT_BLOCK_SIZE && num_free > FLOAT_BLOCK_SIZE)
6556 {
6557 *fprev = fblk->next;
6558 /* Unhook from the free list. */
6559 float_free_list = fblk->floats[0].u.chain;
6560 lisp_align_free (fblk);
6561 }
6562 else
6563 {
6564 num_free += this_free;
6565 fprev = &fblk->next;
6566 }
6567 }
6568 total_floats = num_used;
6569 total_free_floats = num_free;
6570}
6222 6571
6223 /* Put all unmarked floats on free list */ 6572NO_INLINE /* For better stack traces */
6224 { 6573static void
6225 register struct float_block *fblk; 6574sweep_intervals (void)
6226 struct float_block **fprev = &float_block; 6575{
6227 register int lim = float_block_index; 6576 register struct interval_block *iblk;
6228 EMACS_INT num_free = 0, num_used = 0; 6577 struct interval_block **iprev = &interval_block;
6578 register int lim = interval_block_index;
6579 EMACS_INT num_free = 0, num_used = 0;
6229 6580
6230 float_free_list = 0; 6581 interval_free_list = 0;
6231 6582
6232 for (fblk = float_block; fblk; fblk = *fprev) 6583 for (iblk = interval_block; iblk; iblk = *iprev)
6233 { 6584 {
6234 register int i; 6585 register int i;
6235 int this_free = 0; 6586 int this_free = 0;
6236 for (i = 0; i < lim; i++) 6587
6237 if (!FLOAT_MARKED_P (&fblk->floats[i])) 6588 for (i = 0; i < lim; i++)
6238 { 6589 {
6239 this_free++; 6590 if (!iblk->intervals[i].gcmarkbit)
6240 fblk->floats[i].u.chain = float_free_list; 6591 {
6241 float_free_list = &fblk->floats[i]; 6592 set_interval_parent (&iblk->intervals[i], interval_free_list);
6242 } 6593 interval_free_list = &iblk->intervals[i];
6243 else 6594 this_free++;
6244 { 6595 }
6245 num_used++; 6596 else
6246 FLOAT_UNMARK (&fblk->floats[i]); 6597 {
6247 } 6598 num_used++;
6248 lim = FLOAT_BLOCK_SIZE; 6599 iblk->intervals[i].gcmarkbit = 0;
6249 /* If this block contains only free floats and we have already 6600 }
6250 seen more than two blocks worth of free floats then deallocate 6601 }
6251 this block. */ 6602 lim = INTERVAL_BLOCK_SIZE;
6252 if (this_free == FLOAT_BLOCK_SIZE && num_free > FLOAT_BLOCK_SIZE) 6603 /* If this block contains only free intervals and we have already
6253 { 6604 seen more than two blocks worth of free intervals then
6254 *fprev = fblk->next; 6605 deallocate this block. */
6255 /* Unhook from the free list. */ 6606 if (this_free == INTERVAL_BLOCK_SIZE && num_free > INTERVAL_BLOCK_SIZE)
6256 float_free_list = fblk->floats[0].u.chain; 6607 {
6257 lisp_align_free (fblk); 6608 *iprev = iblk->next;
6258 } 6609 /* Unhook from the free list. */
6259 else 6610 interval_free_list = INTERVAL_PARENT (&iblk->intervals[0]);
6260 { 6611 lisp_free (iblk);
6261 num_free += this_free; 6612 }
6262 fprev = &fblk->next; 6613 else
6263 } 6614 {
6264 } 6615 num_free += this_free;
6265 total_floats = num_used; 6616 iprev = &iblk->next;
6266 total_free_floats = num_free; 6617 }
6267 } 6618 }
6619 total_intervals = num_used;
6620 total_free_intervals = num_free;
6621}
6268 6622
6269 /* Put all unmarked intervals on free list */ 6623NO_INLINE /* For better stack traces */
6270 { 6624static void
6271 register struct interval_block *iblk; 6625sweep_symbols (void)
6272 struct interval_block **iprev = &interval_block; 6626{
6273 register int lim = interval_block_index; 6627 struct symbol_block *sblk;
6274 EMACS_INT num_free = 0, num_used = 0; 6628 struct symbol_block **sprev = &symbol_block;
6629 int lim = symbol_block_index;
6630 EMACS_INT num_free = 0, num_used = ARRAYELTS (lispsym);
6275 6631
6276 interval_free_list = 0; 6632 symbol_free_list = NULL;
6277 6633
6278 for (iblk = interval_block; iblk; iblk = *iprev) 6634 for (int i = 0; i < ARRAYELTS (lispsym); i++)
6279 { 6635 lispsym[i].gcmarkbit = 0;
6280 register int i;
6281 int this_free = 0;
6282 6636
6283 for (i = 0; i < lim; i++) 6637 for (sblk = symbol_block; sblk; sblk = *sprev)
6284 { 6638 {
6285 if (!iblk->intervals[i].gcmarkbit) 6639 int this_free = 0;
6286 { 6640 union aligned_Lisp_Symbol *sym = sblk->symbols;
6287 set_interval_parent (&iblk->intervals[i], interval_free_list); 6641 union aligned_Lisp_Symbol *end = sym + lim;
6288 interval_free_list = &iblk->intervals[i]; 6642
6289 this_free++; 6643 for (; sym < end; ++sym)
6290 } 6644 {
6291 else 6645 if (!sym->s.gcmarkbit)
6292 { 6646 {
6293 num_used++; 6647 if (sym->s.redirect == SYMBOL_LOCALIZED)
6294 iblk->intervals[i].gcmarkbit = 0; 6648 xfree (SYMBOL_BLV (&sym->s));
6295 } 6649 sym->s.next = symbol_free_list;
6296 } 6650 symbol_free_list = &sym->s;
6297 lim = INTERVAL_BLOCK_SIZE; 6651 symbol_free_list->function = Vdead;
6298 /* If this block contains only free intervals and we have already 6652 ++this_free;
6299 seen more than two blocks worth of free intervals then 6653 }
6300 deallocate this block. */ 6654 else
6301 if (this_free == INTERVAL_BLOCK_SIZE && num_free > INTERVAL_BLOCK_SIZE) 6655 {
6302 { 6656 ++num_used;
6303 *iprev = iblk->next; 6657 sym->s.gcmarkbit = 0;
6304 /* Unhook from the free list. */ 6658 /* Attempt to catch bogus objects. */
6305 interval_free_list = INTERVAL_PARENT (&iblk->intervals[0]); 6659 eassert (valid_lisp_object_p (sym->s.function));
6306 lisp_free (iblk); 6660 }
6307 } 6661 }
6308 else
6309 {
6310 num_free += this_free;
6311 iprev = &iblk->next;
6312 }
6313 }
6314 total_intervals = num_used;
6315 total_free_intervals = num_free;
6316 }
6317 6662
6318 /* Put all unmarked symbols on free list */ 6663 lim = SYMBOL_BLOCK_SIZE;
6319 { 6664 /* If this block contains only free symbols and we have already
6320 register struct symbol_block *sblk; 6665 seen more than two blocks worth of free symbols then deallocate
6321 struct symbol_block **sprev = &symbol_block; 6666 this block. */
6322 register int lim = symbol_block_index; 6667 if (this_free == SYMBOL_BLOCK_SIZE && num_free > SYMBOL_BLOCK_SIZE)
6323 EMACS_INT num_free = 0, num_used = 0; 6668 {
6669 *sprev = sblk->next;
6670 /* Unhook from the free list. */
6671 symbol_free_list = sblk->symbols[0].s.next;
6672 lisp_free (sblk);
6673 }
6674 else
6675 {
6676 num_free += this_free;
6677 sprev = &sblk->next;
6678 }
6679 }
6680 total_symbols = num_used;
6681 total_free_symbols = num_free;
6682}
6324 6683
6325 symbol_free_list = NULL; 6684NO_INLINE /* For better stack traces. */
6685static void
6686sweep_misc (void)
6687{
6688 register struct marker_block *mblk;
6689 struct marker_block **mprev = &marker_block;
6690 register int lim = marker_block_index;
6691 EMACS_INT num_free = 0, num_used = 0;
6326 6692
6327 for (sblk = symbol_block; sblk; sblk = *sprev) 6693 /* Put all unmarked misc's on free list. For a marker, first
6328 { 6694 unchain it from the buffer it points into. */
6329 int this_free = 0;
6330 union aligned_Lisp_Symbol *sym = sblk->symbols;
6331 union aligned_Lisp_Symbol *end = sym + lim;
6332 6695
6333 for (; sym < end; ++sym) 6696 marker_free_list = 0;
6334 {
6335 /* Check if the symbol was created during loadup. In such a case
6336 it might be pointed to by pure bytecode which we don't trace,
6337 so we conservatively assume that it is live. */
6338 bool pure_p = PURE_POINTER_P (XSTRING (sym->s.name));
6339
6340 if (!sym->s.gcmarkbit && !pure_p)
6341 {
6342 if (sym->s.redirect == SYMBOL_LOCALIZED)
6343 xfree (SYMBOL_BLV (&sym->s));
6344 sym->s.next = symbol_free_list;
6345 symbol_free_list = &sym->s;
6346#if GC_MARK_STACK
6347 symbol_free_list->function = Vdead;
6348#endif
6349 ++this_free;
6350 }
6351 else
6352 {
6353 ++num_used;
6354 if (!pure_p)
6355 UNMARK_STRING (XSTRING (sym->s.name));
6356 sym->s.gcmarkbit = 0;
6357 }
6358 }
6359 6697
6360 lim = SYMBOL_BLOCK_SIZE; 6698 for (mblk = marker_block; mblk; mblk = *mprev)
6361 /* If this block contains only free symbols and we have already 6699 {
6362 seen more than two blocks worth of free symbols then deallocate 6700 register int i;
6363 this block. */ 6701 int this_free = 0;
6364 if (this_free == SYMBOL_BLOCK_SIZE && num_free > SYMBOL_BLOCK_SIZE) 6702
6365 { 6703 for (i = 0; i < lim; i++)
6366 *sprev = sblk->next; 6704 {
6367 /* Unhook from the free list. */ 6705 if (!mblk->markers[i].m.u_any.gcmarkbit)
6368 symbol_free_list = sblk->symbols[0].s.next; 6706 {
6369 lisp_free (sblk); 6707 if (mblk->markers[i].m.u_any.type == Lisp_Misc_Marker)
6370 } 6708 unchain_marker (&mblk->markers[i].m.u_marker);
6371 else 6709 if (mblk->markers[i].m.u_any.type == Lisp_Misc_Finalizer)
6372 { 6710 unchain_finalizer (&mblk->markers[i].m.u_finalizer);
6373 num_free += this_free; 6711 /* Set the type of the freed object to Lisp_Misc_Free.
6374 sprev = &sblk->next; 6712 We could leave the type alone, since nobody checks it,
6375 } 6713 but this might catch bugs faster. */
6376 } 6714 mblk->markers[i].m.u_marker.type = Lisp_Misc_Free;
6377 total_symbols = num_used; 6715 mblk->markers[i].m.u_free.chain = marker_free_list;
6378 total_free_symbols = num_free; 6716 marker_free_list = &mblk->markers[i].m;
6379 } 6717 this_free++;
6718 }
6719 else
6720 {
6721 num_used++;
6722 mblk->markers[i].m.u_any.gcmarkbit = 0;
6723 }
6724 }
6725 lim = MARKER_BLOCK_SIZE;
6726 /* If this block contains only free markers and we have already
6727 seen more than two blocks worth of free markers then deallocate
6728 this block. */
6729 if (this_free == MARKER_BLOCK_SIZE && num_free > MARKER_BLOCK_SIZE)
6730 {
6731 *mprev = mblk->next;
6732 /* Unhook from the free list. */
6733 marker_free_list = mblk->markers[0].m.u_free.chain;
6734 lisp_free (mblk);
6735 }
6736 else
6737 {
6738 num_free += this_free;
6739 mprev = &mblk->next;
6740 }
6741 }
6380 6742
6381 /* Put all unmarked misc's on free list. 6743 total_markers = num_used;
6382 For a marker, first unchain it from the buffer it points into. */ 6744 total_free_markers = num_free;
6383 { 6745}
6384 register struct marker_block *mblk;
6385 struct marker_block **mprev = &marker_block;
6386 register int lim = marker_block_index;
6387 EMACS_INT num_free = 0, num_used = 0;
6388 6746
6389 marker_free_list = 0; 6747NO_INLINE /* For better stack traces */
6748static void
6749sweep_buffers (void)
6750{
6751 register struct buffer *buffer, **bprev = &all_buffers;
6390 6752
6391 for (mblk = marker_block; mblk; mblk = *mprev) 6753 total_buffers = 0;
6754 for (buffer = all_buffers; buffer; buffer = *bprev)
6755 if (!VECTOR_MARKED_P (buffer))
6392 { 6756 {
6393 register int i; 6757 *bprev = buffer->next;
6394 int this_free = 0; 6758 lisp_free (buffer);
6395
6396 for (i = 0; i < lim; i++)
6397 {
6398 if (!mblk->markers[i].m.u_any.gcmarkbit)
6399 {
6400 if (mblk->markers[i].m.u_any.type == Lisp_Misc_Marker)
6401 unchain_marker (&mblk->markers[i].m.u_marker);
6402 /* Set the type of the freed object to Lisp_Misc_Free.
6403 We could leave the type alone, since nobody checks it,
6404 but this might catch bugs faster. */
6405 mblk->markers[i].m.u_marker.type = Lisp_Misc_Free;
6406 mblk->markers[i].m.u_free.chain = marker_free_list;
6407 marker_free_list = &mblk->markers[i].m;
6408 this_free++;
6409 }
6410 else
6411 {
6412 num_used++;
6413 mblk->markers[i].m.u_any.gcmarkbit = 0;
6414 }
6415 }
6416 lim = MARKER_BLOCK_SIZE;
6417 /* If this block contains only free markers and we have already
6418 seen more than two blocks worth of free markers then deallocate
6419 this block. */
6420 if (this_free == MARKER_BLOCK_SIZE && num_free > MARKER_BLOCK_SIZE)
6421 {
6422 *mprev = mblk->next;
6423 /* Unhook from the free list. */
6424 marker_free_list = mblk->markers[0].m.u_free.chain;
6425 lisp_free (mblk);
6426 }
6427 else
6428 {
6429 num_free += this_free;
6430 mprev = &mblk->next;
6431 }
6432 } 6759 }
6760 else
6761 {
6762 VECTOR_UNMARK (buffer);
6763 /* Do not use buffer_(set|get)_intervals here. */
6764 buffer->text->intervals = balance_intervals (buffer->text->intervals);
6765 total_buffers++;
6766 bprev = &buffer->next;
6767 }
6768}
6433 6769
6434 total_markers = num_used; 6770/* Sweep: find all structures not marked, and free them. */
6435 total_free_markers = num_free; 6771static void
6436 } 6772gc_sweep (void)
6437 6773{
6438 /* Free all unmarked buffers */ 6774 /* Remove or mark entries in weak hash tables.
6439 { 6775 This must be done before any object is unmarked. */
6440 register struct buffer *buffer, **bprev = &all_buffers; 6776 sweep_weak_hash_tables ();
6441
6442 total_buffers = 0;
6443 for (buffer = all_buffers; buffer; buffer = *bprev)
6444 if (!VECTOR_MARKED_P (buffer))
6445 {
6446 *bprev = buffer->next;
6447 lisp_free (buffer);
6448 }
6449 else
6450 {
6451 VECTOR_UNMARK (buffer);
6452 /* Do not use buffer_(set|get)_intervals here. */
6453 buffer->text->intervals = balance_intervals (buffer->text->intervals);
6454 total_buffers++;
6455 bprev = &buffer->next;
6456 }
6457 }
6458 6777
6778 sweep_strings ();
6779 check_string_bytes (!noninteractive);
6780 sweep_conses ();
6781 sweep_floats ();
6782 sweep_intervals ();
6783 sweep_symbols ();
6784 sweep_misc ();
6785 sweep_buffers ();
6459 sweep_vectors (); 6786 sweep_vectors ();
6460 check_string_bytes (!noninteractive); 6787 check_string_bytes (!noninteractive);
6461} 6788}
6462 6789
6790DEFUN ("memory-info", Fmemory_info, Smemory_info, 0, 0, 0,
6791 doc: /* Return a list of (TOTAL-RAM FREE-RAM TOTAL-SWAP FREE-SWAP).
6792All values are in Kbytes. If there is no swap space,
6793last two values are zero. If the system is not supported
6794or memory information can't be obtained, return nil. */)
6795 (void)
6796{
6797#if defined HAVE_LINUX_SYSINFO
6798 struct sysinfo si;
6799 uintmax_t units;
6463 6800
6801 if (sysinfo (&si))
6802 return Qnil;
6803#ifdef LINUX_SYSINFO_UNIT
6804 units = si.mem_unit;
6805#else
6806 units = 1;
6807#endif
6808 return list4i ((uintmax_t) si.totalram * units / 1024,
6809 (uintmax_t) si.freeram * units / 1024,
6810 (uintmax_t) si.totalswap * units / 1024,
6811 (uintmax_t) si.freeswap * units / 1024);
6812#elif defined WINDOWSNT
6813 unsigned long long totalram, freeram, totalswap, freeswap;
6814
6815 if (w32_memory_info (&totalram, &freeram, &totalswap, &freeswap) == 0)
6816 return list4i ((uintmax_t) totalram / 1024,
6817 (uintmax_t) freeram / 1024,
6818 (uintmax_t) totalswap / 1024,
6819 (uintmax_t) freeswap / 1024);
6820 else
6821 return Qnil;
6822#elif defined MSDOS
6823 unsigned long totalram, freeram, totalswap, freeswap;
6824
6825 if (dos_memory_info (&totalram, &freeram, &totalswap, &freeswap) == 0)
6826 return list4i ((uintmax_t) totalram / 1024,
6827 (uintmax_t) freeram / 1024,
6828 (uintmax_t) totalswap / 1024,
6829 (uintmax_t) freeswap / 1024);
6830 else
6831 return Qnil;
6832#else /* not HAVE_LINUX_SYSINFO, not WINDOWSNT, not MSDOS */
6833 /* FIXME: add more systems. */
6834 return Qnil;
6835#endif /* HAVE_LINUX_SYSINFO, not WINDOWSNT, not MSDOS */
6836}
6464 6837
6465
6466/* Debugging aids. */ 6838/* Debugging aids. */
6467 6839
6468DEFUN ("memory-limit", Fmemory_limit, Smemory_limit, 0, 0, 0, 6840DEFUN ("memory-limit", Fmemory_limit, Smemory_limit, 0, 0, 0,
@@ -6473,7 +6845,12 @@ We divide the value by 1024 to make sure it fits in a Lisp integer. */)
6473{ 6845{
6474 Lisp_Object end; 6846 Lisp_Object end;
6475 6847
6848#ifdef HAVE_NS
6849 /* Avoid warning. sbrk has no relation to memory allocated anyway. */
6850 XSETINT (end, 0);
6851#else
6476 XSETINT (end, (intptr_t) (char *) sbrk (0) / 1024); 6852 XSETINT (end, (intptr_t) (char *) sbrk (0) / 1024);
6853#endif
6477 6854
6478 return end; 6855 return end;
6479} 6856}
@@ -6504,6 +6881,21 @@ Frames, windows, buffers, and subprocesses count as vectors
6504 bounded_number (strings_consed)); 6881 bounded_number (strings_consed));
6505} 6882}
6506 6883
6884static bool
6885symbol_uses_obj (Lisp_Object symbol, Lisp_Object obj)
6886{
6887 struct Lisp_Symbol *sym = XSYMBOL (symbol);
6888 Lisp_Object val = find_symbol_value (symbol);
6889 return (EQ (val, obj)
6890 || EQ (sym->function, obj)
6891 || (!NILP (sym->function)
6892 && COMPILEDP (sym->function)
6893 && EQ (AREF (sym->function, COMPILED_BYTECODE), obj))
6894 || (!NILP (val)
6895 && COMPILEDP (val)
6896 && EQ (AREF (val, COMPILED_BYTECODE), obj)));
6897}
6898
6507/* Find at most FIND_MAX symbols which have OBJ as their value or 6899/* Find at most FIND_MAX symbols which have OBJ as their value or
6508 function. This is used in gdbinit's `xwhichsymbols' command. */ 6900 function. This is used in gdbinit's `xwhichsymbols' command. */
6509 6901
@@ -6516,6 +6908,17 @@ which_symbols (Lisp_Object obj, EMACS_INT find_max)
6516 6908
6517 if (! DEADP (obj)) 6909 if (! DEADP (obj))
6518 { 6910 {
6911 for (int i = 0; i < ARRAYELTS (lispsym); i++)
6912 {
6913 Lisp_Object sym = builtin_lisp_symbol (i);
6914 if (symbol_uses_obj (sym, obj))
6915 {
6916 found = Fcons (sym, found);
6917 if (--find_max == 0)
6918 goto out;
6919 }
6920 }
6921
6519 for (sblk = symbol_block; sblk; sblk = sblk->next) 6922 for (sblk = symbol_block; sblk; sblk = sblk->next)
6520 { 6923 {
6521 union aligned_Lisp_Symbol *aligned_sym = sblk->symbols; 6924 union aligned_Lisp_Symbol *aligned_sym = sblk->symbols;
@@ -6523,25 +6926,13 @@ which_symbols (Lisp_Object obj, EMACS_INT find_max)
6523 6926
6524 for (bn = 0; bn < SYMBOL_BLOCK_SIZE; bn++, aligned_sym++) 6927 for (bn = 0; bn < SYMBOL_BLOCK_SIZE; bn++, aligned_sym++)
6525 { 6928 {
6526 struct Lisp_Symbol *sym = &aligned_sym->s;
6527 Lisp_Object val;
6528 Lisp_Object tem;
6529
6530 if (sblk == symbol_block && bn >= symbol_block_index) 6929 if (sblk == symbol_block && bn >= symbol_block_index)
6531 break; 6930 break;
6532 6931
6533 XSETSYMBOL (tem, sym); 6932 Lisp_Object sym = make_lisp_symbol (&aligned_sym->s);
6534 val = find_symbol_value (tem); 6933 if (symbol_uses_obj (sym, obj))
6535 if (EQ (val, obj)
6536 || EQ (sym->function, obj)
6537 || (!NILP (sym->function)
6538 && COMPILEDP (sym->function)
6539 && EQ (AREF (sym->function, COMPILED_BYTECODE), obj))
6540 || (!NILP (val)
6541 && COMPILEDP (val)
6542 && EQ (AREF (val, COMPILED_BYTECODE), obj)))
6543 { 6934 {
6544 found = Fcons (tem, found); 6935 found = Fcons (sym, found);
6545 if (--find_max == 0) 6936 if (--find_max == 0)
6546 goto out; 6937 goto out;
6547 } 6938 }
@@ -6554,6 +6945,78 @@ which_symbols (Lisp_Object obj, EMACS_INT find_max)
6554 return found; 6945 return found;
6555} 6946}
6556 6947
6948#ifdef SUSPICIOUS_OBJECT_CHECKING
6949
6950static void *
6951find_suspicious_object_in_range (void *begin, void *end)
6952{
6953 char *begin_a = begin;
6954 char *end_a = end;
6955 int i;
6956
6957 for (i = 0; i < ARRAYELTS (suspicious_objects); ++i)
6958 {
6959 char *suspicious_object = suspicious_objects[i];
6960 if (begin_a <= suspicious_object && suspicious_object < end_a)
6961 return suspicious_object;
6962 }
6963
6964 return NULL;
6965}
6966
6967static void
6968note_suspicious_free (void* ptr)
6969{
6970 struct suspicious_free_record* rec;
6971
6972 rec = &suspicious_free_history[suspicious_free_history_index++];
6973 if (suspicious_free_history_index ==
6974 ARRAYELTS (suspicious_free_history))
6975 {
6976 suspicious_free_history_index = 0;
6977 }
6978
6979 memset (rec, 0, sizeof (*rec));
6980 rec->suspicious_object = ptr;
6981 backtrace (&rec->backtrace[0], ARRAYELTS (rec->backtrace));
6982}
6983
6984static void
6985detect_suspicious_free (void* ptr)
6986{
6987 int i;
6988
6989 eassert (ptr != NULL);
6990
6991 for (i = 0; i < ARRAYELTS (suspicious_objects); ++i)
6992 if (suspicious_objects[i] == ptr)
6993 {
6994 note_suspicious_free (ptr);
6995 suspicious_objects[i] = NULL;
6996 }
6997}
6998
6999#endif /* SUSPICIOUS_OBJECT_CHECKING */
7000
7001DEFUN ("suspicious-object", Fsuspicious_object, Ssuspicious_object, 1, 1, 0,
7002 doc: /* Return OBJ, maybe marking it for extra scrutiny.
7003If Emacs is compiled with suspicious object checking, capture
7004a stack trace when OBJ is freed in order to help track down
7005garbage collection bugs. Otherwise, do nothing and return OBJ. */)
7006 (Lisp_Object obj)
7007{
7008#ifdef SUSPICIOUS_OBJECT_CHECKING
7009 /* Right now, we care only about vectors. */
7010 if (VECTORLIKEP (obj))
7011 {
7012 suspicious_objects[suspicious_object_index++] = XVECTOR (obj);
7013 if (suspicious_object_index == ARRAYELTS (suspicious_objects))
7014 suspicious_object_index = 0;
7015 }
7016#endif
7017 return obj;
7018}
7019
6557#ifdef ENABLE_CHECKING 7020#ifdef ENABLE_CHECKING
6558 7021
6559bool suppress_checking; 7022bool suppress_checking;
@@ -6565,21 +7028,65 @@ die (const char *msg, const char *file, int line)
6565 file, line, msg); 7028 file, line, msg);
6566 terminate_due_to_signal (SIGABRT, INT_MAX); 7029 terminate_due_to_signal (SIGABRT, INT_MAX);
6567} 7030}
6568#endif 7031
6569 7032#endif /* ENABLE_CHECKING */
7033
7034#if defined (ENABLE_CHECKING) && USE_STACK_LISP_OBJECTS
7035
7036/* Debugging check whether STR is ASCII-only. */
7037
7038const char *
7039verify_ascii (const char *str)
7040{
7041 const unsigned char *ptr = (unsigned char *) str, *end = ptr + strlen (str);
7042 while (ptr < end)
7043 {
7044 int c = STRING_CHAR_ADVANCE (ptr);
7045 if (!ASCII_CHAR_P (c))
7046 emacs_abort ();
7047 }
7048 return str;
7049}
7050
7051/* Stress alloca with inconveniently sized requests and check
7052 whether all allocated areas may be used for Lisp_Object. */
7053
7054NO_INLINE static void
7055verify_alloca (void)
7056{
7057 int i;
7058 enum { ALLOCA_CHECK_MAX = 256 };
7059 /* Start from size of the smallest Lisp object. */
7060 for (i = sizeof (struct Lisp_Cons); i <= ALLOCA_CHECK_MAX; i++)
7061 {
7062 void *ptr = alloca (i);
7063 make_lisp_ptr (ptr, Lisp_Cons);
7064 }
7065}
7066
7067#else /* not ENABLE_CHECKING && USE_STACK_LISP_OBJECTS */
7068
7069#define verify_alloca() ((void) 0)
7070
7071#endif /* ENABLE_CHECKING && USE_STACK_LISP_OBJECTS */
7072
6570/* Initialization. */ 7073/* Initialization. */
6571 7074
6572void 7075void
6573init_alloc_once (void) 7076init_alloc_once (void)
6574{ 7077{
6575 /* Used to do Vpurify_flag = Qt here, but Qt isn't set up yet! */ 7078 /* Even though Qt's contents are not set up, its address is known. */
7079 Vpurify_flag = Qt;
7080
6576 purebeg = PUREBEG; 7081 purebeg = PUREBEG;
6577 pure_size = PURESIZE; 7082 pure_size = PURESIZE;
6578 7083
6579#if GC_MARK_STACK || defined GC_MALLOC_CHECK 7084 verify_alloca ();
7085 init_finalizer_list (&finalizers);
7086 init_finalizer_list (&doomed_finalizers);
7087
6580 mem_init (); 7088 mem_init ();
6581 Vdead = make_pure_string ("DEAD", 4, 4, 0); 7089 Vdead = make_pure_string ("DEAD", 4, 4, 0);
6582#endif
6583 7090
6584#ifdef DOUG_LEA_MALLOC 7091#ifdef DOUG_LEA_MALLOC
6585 mallopt (M_TRIM_THRESHOLD, 128 * 1024); /* Trim threshold. */ 7092 mallopt (M_TRIM_THRESHOLD, 128 * 1024); /* Trim threshold. */
@@ -6596,15 +7103,15 @@ init_alloc_once (void)
6596void 7103void
6597init_alloc (void) 7104init_alloc (void)
6598{ 7105{
6599 gcprolist = 0;
6600 byte_stack_list = 0;
6601#if GC_MARK_STACK
6602#if !defined GC_SAVE_REGISTERS_ON_STACK && !defined GC_SETJMP_WORKS 7106#if !defined GC_SAVE_REGISTERS_ON_STACK && !defined GC_SETJMP_WORKS
6603 setjmp_tested_p = longjmps_done = 0; 7107 setjmp_tested_p = longjmps_done = 0;
6604#endif 7108#endif
6605#endif
6606 Vgc_elapsed = make_float (0.0); 7109 Vgc_elapsed = make_float (0.0);
6607 gcs_done = 0; 7110 gcs_done = 0;
7111
7112#if USE_VALGRIND
7113 valgrind_p = RUNNING_ON_VALGRIND != 0;
7114#endif
6608} 7115}
6609 7116
6610void 7117void
@@ -6642,6 +7149,7 @@ If this portion is smaller than `gc-cons-threshold', this is ignored. */);
6642 7149
6643 DEFVAR_INT ("symbols-consed", symbols_consed, 7150 DEFVAR_INT ("symbols-consed", symbols_consed,
6644 doc: /* Number of symbols that have been consed so far. */); 7151 doc: /* Number of symbols that have been consed so far. */);
7152 symbols_consed += ARRAYELTS (lispsym);
6645 7153
6646 DEFVAR_INT ("string-chars-consed", string_chars_consed, 7154 DEFVAR_INT ("string-chars-consed", string_chars_consed,
6647 doc: /* Number of string characters that have been consed so far. */); 7155 doc: /* Number of string characters that have been consed so far. */);
@@ -6704,11 +7212,12 @@ do hash-consing of the objects allocated to pure space. */);
6704 doc: /* Accumulated time elapsed in garbage collections. 7212 doc: /* Accumulated time elapsed in garbage collections.
6705The time is in seconds as a floating point value. */); 7213The time is in seconds as a floating point value. */);
6706 DEFVAR_INT ("gcs-done", gcs_done, 7214 DEFVAR_INT ("gcs-done", gcs_done,
6707 doc: /* Accumulated number of garbage collections done. */); 7215 doc: /* Accumulated number of garbage collections done. */);
6708 7216
6709 defsubr (&Scons); 7217 defsubr (&Scons);
6710 defsubr (&Slist); 7218 defsubr (&Slist);
6711 defsubr (&Svector); 7219 defsubr (&Svector);
7220 defsubr (&Sbool_vector);
6712 defsubr (&Smake_byte_code); 7221 defsubr (&Smake_byte_code);
6713 defsubr (&Smake_list); 7222 defsubr (&Smake_list);
6714 defsubr (&Smake_vector); 7223 defsubr (&Smake_vector);
@@ -6716,14 +7225,13 @@ The time is in seconds as a floating point value. */);
6716 defsubr (&Smake_bool_vector); 7225 defsubr (&Smake_bool_vector);
6717 defsubr (&Smake_symbol); 7226 defsubr (&Smake_symbol);
6718 defsubr (&Smake_marker); 7227 defsubr (&Smake_marker);
7228 defsubr (&Smake_finalizer);
6719 defsubr (&Spurecopy); 7229 defsubr (&Spurecopy);
6720 defsubr (&Sgarbage_collect); 7230 defsubr (&Sgarbage_collect);
6721 defsubr (&Smemory_limit); 7231 defsubr (&Smemory_limit);
7232 defsubr (&Smemory_info);
6722 defsubr (&Smemory_use_counts); 7233 defsubr (&Smemory_use_counts);
6723 7234 defsubr (&Ssuspicious_object);
6724#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
6725 defsubr (&Sgc_status);
6726#endif
6727} 7235}
6728 7236
6729/* When compiled with GCC, GDB might say "No enum type named 7237/* When compiled with GCC, GDB might say "No enum type named
@@ -6734,12 +7242,10 @@ The time is in seconds as a floating point value. */);
6734union 7242union
6735{ 7243{
6736 enum CHARTAB_SIZE_BITS CHARTAB_SIZE_BITS; 7244 enum CHARTAB_SIZE_BITS CHARTAB_SIZE_BITS;
6737 enum CHAR_TABLE_STANDARD_SLOTS CHAR_TABLE_STANDARD_SLOTS; 7245 enum char_table_specials char_table_specials;
6738 enum char_bits char_bits; 7246 enum char_bits char_bits;
6739 enum CHECK_LISP_OBJECT_TYPE CHECK_LISP_OBJECT_TYPE; 7247 enum CHECK_LISP_OBJECT_TYPE CHECK_LISP_OBJECT_TYPE;
6740 enum DEFAULT_HASH_SIZE DEFAULT_HASH_SIZE; 7248 enum DEFAULT_HASH_SIZE DEFAULT_HASH_SIZE;
6741 enum enum_USE_LSB_TAG enum_USE_LSB_TAG;
6742 enum FLOAT_TO_STRING_BUFSIZE FLOAT_TO_STRING_BUFSIZE;
6743 enum Lisp_Bits Lisp_Bits; 7249 enum Lisp_Bits Lisp_Bits;
6744 enum Lisp_Compiled Lisp_Compiled; 7250 enum Lisp_Compiled Lisp_Compiled;
6745 enum maxargs maxargs; 7251 enum maxargs maxargs;