Commit | Line | Data |
---|---|---|
7146af97 | 1 | /* Storage allocation and gc for GNU Emacs Lisp interpreter. |
999dd333 GM |
2 | |
3 | Copyright (C) 1985-1986, 1988, 1993-1995, 1997-2012 | |
4 | Free Software Foundation, Inc. | |
7146af97 JB |
5 | |
6 | This file is part of GNU Emacs. | |
7 | ||
9ec0b715 | 8 | GNU Emacs is free software: you can redistribute it and/or modify |
7146af97 | 9 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
10 | the Free Software Foundation, either version 3 of the License, or |
11 | (at your option) any later version. | |
7146af97 JB |
12 | |
13 | GNU Emacs is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
9ec0b715 | 19 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
7146af97 | 20 | |
18160b98 | 21 | #include <config.h> |
f162bcc3 PE |
22 | |
23 | #define LISP_INLINE EXTERN_INLINE | |
24 | ||
e9b309ac | 25 | #include <stdio.h> |
ab6780cd | 26 | #include <limits.h> /* For CHAR_BIT. */ |
92939d31 | 27 | |
bc8000ff EZ |
28 | #ifdef ENABLE_CHECKING |
29 | #include <signal.h> /* For SIGABRT. */ | |
30 | #endif | |
31 | ||
ae9e757a | 32 | #ifdef HAVE_PTHREAD |
aa477689 JD |
33 | #include <pthread.h> |
34 | #endif | |
35 | ||
7146af97 | 36 | #include "lisp.h" |
ece93c02 | 37 | #include "process.h" |
d5e35230 | 38 | #include "intervals.h" |
4c0be5f4 | 39 | #include "puresize.h" |
e5560ff7 | 40 | #include "character.h" |
7146af97 JB |
41 | #include "buffer.h" |
42 | #include "window.h" | |
2538fae4 | 43 | #include "keyboard.h" |
502b9b64 | 44 | #include "frame.h" |
9ac0d9e0 | 45 | #include "blockinput.h" |
4a729fd8 | 46 | #include "termhooks.h" /* For struct terminal. */ |
0328b6de | 47 | |
0065d054 | 48 | #include <verify.h> |
e065a56e | 49 | |
52828e02 PE |
50 | /* GC_CHECK_MARKED_OBJECTS means do sanity checks on allocated objects. |
51 | Doable only if GC_MARK_STACK. */ | |
52 | #if ! GC_MARK_STACK | |
53 | # undef GC_CHECK_MARKED_OBJECTS | |
54 | #endif | |
55 | ||
6b61353c | 56 | /* GC_MALLOC_CHECK defined means perform validity checks of malloc'd |
52828e02 PE |
57 | memory. Can do this only if using gmalloc.c and if not checking |
58 | marked objects. */ | |
6b61353c | 59 | |
52828e02 PE |
60 | #if (defined SYSTEM_MALLOC || defined DOUG_LEA_MALLOC \ |
61 | || defined GC_CHECK_MARKED_OBJECTS) | |
6b61353c KH |
62 | #undef GC_MALLOC_CHECK |
63 | #endif | |
64 | ||
bf952fb6 | 65 | #include <unistd.h> |
4004364e | 66 | #ifndef HAVE_UNISTD_H |
261cb4bb | 67 | extern void *sbrk (); |
bf952fb6 | 68 | #endif |
ee1eea5c | 69 | |
de7124a7 | 70 | #include <fcntl.h> |
de7124a7 | 71 | |
a411ac43 PE |
72 | #ifdef USE_GTK |
73 | # include "gtkutil.h" | |
74 | #endif | |
69666f77 | 75 | #ifdef WINDOWSNT |
f892cf9c | 76 | #include "w32.h" |
62aba0d4 | 77 | #include "w32heap.h" /* for sbrk */ |
69666f77 EZ |
78 | #endif |
79 | ||
d1658221 | 80 | #ifdef DOUG_LEA_MALLOC |
2e471eb5 | 81 | |
d1658221 | 82 | #include <malloc.h> |
81d492d5 | 83 | |
2e471eb5 GM |
84 | /* Specify maximum number of areas to mmap. It would be nice to use a |
85 | value that explicitly means "no limit". */ | |
86 | ||
81d492d5 RS |
87 | #define MMAP_MAX_AREAS 100000000 |
88 | ||
2e471eb5 | 89 | #endif /* not DOUG_LEA_MALLOC */ |
276cbe5a | 90 | |
2e471eb5 GM |
91 | /* Mark, unmark, query mark bit of a Lisp string. S must be a pointer |
92 | to a struct Lisp_String. */ | |
93 | ||
7cdee936 SM |
94 | #define MARK_STRING(S) ((S)->size |= ARRAY_MARK_FLAG) |
95 | #define UNMARK_STRING(S) ((S)->size &= ~ARRAY_MARK_FLAG) | |
b059de99 | 96 | #define STRING_MARKED_P(S) (((S)->size & ARRAY_MARK_FLAG) != 0) |
2e471eb5 | 97 | |
eab3844f PE |
98 | #define VECTOR_MARK(V) ((V)->header.size |= ARRAY_MARK_FLAG) |
99 | #define VECTOR_UNMARK(V) ((V)->header.size &= ~ARRAY_MARK_FLAG) | |
100 | #define VECTOR_MARKED_P(V) (((V)->header.size & ARRAY_MARK_FLAG) != 0) | |
3ef06d12 | 101 | |
0dd6d66d DA |
102 | /* Default value of gc_cons_threshold (see below). */ |
103 | ||
663e2b3f | 104 | #define GC_DEFAULT_THRESHOLD (100000 * word_size) |
0dd6d66d | 105 | |
29208e82 TT |
106 | /* Global variables. */ |
107 | struct emacs_globals globals; | |
108 | ||
2e471eb5 GM |
109 | /* Number of bytes of consing done since the last gc. */ |
110 | ||
dac616ff | 111 | EMACS_INT consing_since_gc; |
7146af97 | 112 | |
974aae61 RS |
113 | /* Similar minimum, computed from Vgc_cons_percentage. */ |
114 | ||
dac616ff | 115 | EMACS_INT gc_relative_threshold; |
310ea200 | 116 | |
24d8a105 RS |
117 | /* Minimum number of bytes of consing since GC before next GC, |
118 | when memory is full. */ | |
119 | ||
dac616ff | 120 | EMACS_INT memory_full_cons_threshold; |
24d8a105 | 121 | |
fce31d69 | 122 | /* True during GC. */ |
2e471eb5 | 123 | |
fce31d69 | 124 | bool gc_in_progress; |
7146af97 | 125 | |
fce31d69 | 126 | /* True means abort if try to GC. |
3de0effb RS |
127 | This is for code which is written on the assumption that |
128 | no GC will happen, so as to verify that assumption. */ | |
129 | ||
fce31d69 | 130 | bool abort_on_gc; |
3de0effb | 131 | |
34400008 GM |
132 | /* Number of live and free conses etc. */ |
133 | ||
3ab6e069 | 134 | static EMACS_INT total_conses, total_markers, total_symbols, total_buffers; |
c0c5c8ae | 135 | static EMACS_INT total_free_conses, total_free_markers, total_free_symbols; |
3ab6e069 | 136 | static EMACS_INT total_free_floats, total_floats; |
fd27a537 | 137 | |
2e471eb5 | 138 | /* Points to memory space allocated as "spare", to be freed if we run |
24d8a105 RS |
139 | out of memory. We keep one large block, four cons-blocks, and |
140 | two string blocks. */ | |
2e471eb5 | 141 | |
d3d47262 | 142 | static char *spare_memory[7]; |
276cbe5a | 143 | |
2b6148e4 PE |
144 | /* Amount of spare memory to keep in large reserve block, or to see |
145 | whether this much is available when malloc fails on a larger request. */ | |
2e471eb5 | 146 | |
276cbe5a | 147 | #define SPARE_MEMORY (1 << 14) |
4d09bcf6 | 148 | |
1b8950e5 RS |
149 | /* Initialize it to a nonzero value to force it into data space |
150 | (rather than bss space). That way unexec will remap it into text | |
151 | space (pure), on some systems. We have not implemented the | |
152 | remapping on more recent systems because this is less important | |
153 | nowadays than in the days of small memories and timesharing. */ | |
2e471eb5 | 154 | |
2c4685ee | 155 | EMACS_INT pure[(PURESIZE + sizeof (EMACS_INT) - 1) / sizeof (EMACS_INT)] = {1,}; |
7146af97 | 156 | #define PUREBEG (char *) pure |
2e471eb5 | 157 | |
9e713715 | 158 | /* Pointer to the pure area, and its size. */ |
2e471eb5 | 159 | |
9e713715 | 160 | static char *purebeg; |
903fe15d | 161 | static ptrdiff_t pure_size; |
9e713715 GM |
162 | |
163 | /* Number of bytes of pure storage used before pure storage overflowed. | |
164 | If this is non-zero, this implies that an overflow occurred. */ | |
165 | ||
903fe15d | 166 | static ptrdiff_t pure_bytes_used_before_overflow; |
7146af97 | 167 | |
fce31d69 | 168 | /* True if P points into pure space. */ |
34400008 GM |
169 | |
170 | #define PURE_POINTER_P(P) \ | |
6a0bf43d | 171 | ((uintptr_t) (P) - (uintptr_t) purebeg <= pure_size) |
34400008 | 172 | |
fecbd8ff | 173 | /* Index in pure at which next pure Lisp object will be allocated.. */ |
e5bc14d4 | 174 | |
d311d28c | 175 | static ptrdiff_t pure_bytes_used_lisp; |
e5bc14d4 YM |
176 | |
177 | /* Number of bytes allocated for non-Lisp objects in pure storage. */ | |
178 | ||
d311d28c | 179 | static ptrdiff_t pure_bytes_used_non_lisp; |
e5bc14d4 | 180 | |
2e471eb5 GM |
181 | /* If nonzero, this is a warning delivered by malloc and not yet |
182 | displayed. */ | |
183 | ||
a8fe7202 | 184 | const char *pending_malloc_warning; |
7146af97 JB |
185 | |
186 | /* Maximum amount of C stack to save when a GC happens. */ | |
187 | ||
188 | #ifndef MAX_SAVE_STACK | |
189 | #define MAX_SAVE_STACK 16000 | |
190 | #endif | |
191 | ||
192 | /* Buffer in which we save a copy of the C stack at each GC. */ | |
193 | ||
dd3f25f7 | 194 | #if MAX_SAVE_STACK > 0 |
d3d47262 | 195 | static char *stack_copy; |
903fe15d | 196 | static ptrdiff_t stack_copy_size; |
dd3f25f7 | 197 | #endif |
7146af97 | 198 | |
fecbd8ff SM |
199 | static Lisp_Object Qconses; |
200 | static Lisp_Object Qsymbols; | |
201 | static Lisp_Object Qmiscs; | |
202 | static Lisp_Object Qstrings; | |
203 | static Lisp_Object Qvectors; | |
204 | static Lisp_Object Qfloats; | |
205 | static Lisp_Object Qintervals; | |
206 | static Lisp_Object Qbuffers; | |
f8643a6b | 207 | static Lisp_Object Qstring_bytes, Qvector_slots, Qheap; |
955cbe7b | 208 | static Lisp_Object Qgc_cons_threshold; |
3d80c99f | 209 | Lisp_Object Qautomatic_gc; |
955cbe7b | 210 | Lisp_Object Qchar_table_extra_slots; |
e8197642 | 211 | |
9e713715 GM |
212 | /* Hook run after GC has finished. */ |
213 | ||
955cbe7b | 214 | static Lisp_Object Qpost_gc_hook; |
2c5bd608 | 215 | |
f57e2426 | 216 | static void mark_terminals (void); |
f57e2426 | 217 | static void gc_sweep (void); |
72cb32cf | 218 | static Lisp_Object make_pure_vector (ptrdiff_t); |
f57e2426 J |
219 | static void mark_glyph_matrix (struct glyph_matrix *); |
220 | static void mark_face_cache (struct face_cache *); | |
c752cfa9 | 221 | static void mark_buffer (struct buffer *); |
41c28a37 | 222 | |
69003fd8 PE |
223 | #if !defined REL_ALLOC || defined SYSTEM_MALLOC |
224 | static void refill_memory_reserve (void); | |
225 | #endif | |
f57e2426 J |
226 | static struct Lisp_String *allocate_string (void); |
227 | static void compact_small_strings (void); | |
228 | static void free_large_strings (void); | |
229 | static void sweep_strings (void); | |
244ed907 | 230 | static void free_misc (Lisp_Object); |
196e41e4 | 231 | extern Lisp_Object which_symbols (Lisp_Object, EMACS_INT) EXTERNALLY_VISIBLE; |
34400008 | 232 | |
34400008 GM |
233 | /* When scanning the C stack for live Lisp objects, Emacs keeps track |
234 | of what memory allocated via lisp_malloc is intended for what | |
235 | purpose. This enumeration specifies the type of memory. */ | |
236 | ||
237 | enum mem_type | |
238 | { | |
239 | MEM_TYPE_NON_LISP, | |
240 | MEM_TYPE_BUFFER, | |
241 | MEM_TYPE_CONS, | |
242 | MEM_TYPE_STRING, | |
243 | MEM_TYPE_MISC, | |
244 | MEM_TYPE_SYMBOL, | |
245 | MEM_TYPE_FLOAT, | |
9c545a55 SM |
246 | /* We used to keep separate mem_types for subtypes of vectors such as |
247 | process, hash_table, frame, terminal, and window, but we never made | |
248 | use of the distinction, so it only caused source-code complexity | |
249 | and runtime slowdown. Minor but pointless. */ | |
f3372c87 DA |
250 | MEM_TYPE_VECTORLIKE, |
251 | /* Special type to denote vector blocks. */ | |
35aaa1ea DA |
252 | MEM_TYPE_VECTOR_BLOCK, |
253 | /* Special type to denote reserved memory. */ | |
254 | MEM_TYPE_SPARE | |
34400008 GM |
255 | }; |
256 | ||
261cb4bb | 257 | static void *lisp_malloc (size_t, enum mem_type); |
225ccad6 | 258 | |
24d8a105 | 259 | |
877935b1 | 260 | #if GC_MARK_STACK || defined GC_MALLOC_CHECK |
0b378936 GM |
261 | |
262 | #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES | |
263 | #include <stdio.h> /* For fprintf. */ | |
264 | #endif | |
265 | ||
266 | /* A unique object in pure space used to make some Lisp objects | |
267 | on free lists recognizable in O(1). */ | |
268 | ||
d3d47262 | 269 | static Lisp_Object Vdead; |
ca78dc43 | 270 | #define DEADP(x) EQ (x, Vdead) |
0b378936 | 271 | |
877935b1 GM |
272 | #ifdef GC_MALLOC_CHECK |
273 | ||
274 | enum mem_type allocated_mem_type; | |
877935b1 GM |
275 | |
276 | #endif /* GC_MALLOC_CHECK */ | |
277 | ||
278 | /* A node in the red-black tree describing allocated memory containing | |
279 | Lisp data. Each such block is recorded with its start and end | |
280 | address when it is allocated, and removed from the tree when it | |
281 | is freed. | |
282 | ||
283 | A red-black tree is a balanced binary tree with the following | |
284 | properties: | |
285 | ||
286 | 1. Every node is either red or black. | |
287 | 2. Every leaf is black. | |
288 | 3. If a node is red, then both of its children are black. | |
289 | 4. Every simple path from a node to a descendant leaf contains | |
290 | the same number of black nodes. | |
291 | 5. The root is always black. | |
292 | ||
293 | When nodes are inserted into the tree, or deleted from the tree, | |
294 | the tree is "fixed" so that these properties are always true. | |
295 | ||
296 | A red-black tree with N internal nodes has height at most 2 | |
297 | log(N+1). Searches, insertions and deletions are done in O(log N). | |
298 | Please see a text book about data structures for a detailed | |
299 | description of red-black trees. Any book worth its salt should | |
300 | describe them. */ | |
301 | ||
302 | struct mem_node | |
303 | { | |
9f7d9210 RS |
304 | /* Children of this node. These pointers are never NULL. When there |
305 | is no child, the value is MEM_NIL, which points to a dummy node. */ | |
306 | struct mem_node *left, *right; | |
307 | ||
308 | /* The parent of this node. In the root node, this is NULL. */ | |
309 | struct mem_node *parent; | |
877935b1 GM |
310 | |
311 | /* Start and end of allocated region. */ | |
312 | void *start, *end; | |
313 | ||
314 | /* Node color. */ | |
315 | enum {MEM_BLACK, MEM_RED} color; | |
177c0ea7 | 316 | |
877935b1 GM |
317 | /* Memory type. */ |
318 | enum mem_type type; | |
319 | }; | |
320 | ||
321 | /* Base address of stack. Set in main. */ | |
322 | ||
323 | Lisp_Object *stack_base; | |
324 | ||
325 | /* Root of the tree describing allocated Lisp memory. */ | |
326 | ||
327 | static struct mem_node *mem_root; | |
328 | ||
ece93c02 GM |
329 | /* Lowest and highest known address in the heap. */ |
330 | ||
331 | static void *min_heap_address, *max_heap_address; | |
332 | ||
877935b1 GM |
333 | /* Sentinel node of the tree. */ |
334 | ||
335 | static struct mem_node mem_z; | |
336 | #define MEM_NIL &mem_z | |
337 | ||
d311d28c | 338 | static struct Lisp_Vector *allocate_vectorlike (ptrdiff_t); |
261cb4bb | 339 | static void lisp_free (void *); |
f57e2426 | 340 | static void mark_stack (void); |
fce31d69 PE |
341 | static bool live_vector_p (struct mem_node *, void *); |
342 | static bool live_buffer_p (struct mem_node *, void *); | |
343 | static bool live_string_p (struct mem_node *, void *); | |
344 | static bool live_cons_p (struct mem_node *, void *); | |
345 | static bool live_symbol_p (struct mem_node *, void *); | |
346 | static bool live_float_p (struct mem_node *, void *); | |
347 | static bool live_misc_p (struct mem_node *, void *); | |
f57e2426 | 348 | static void mark_maybe_object (Lisp_Object); |
3164aeac | 349 | static void mark_memory (void *, void *); |
52828e02 | 350 | #if GC_MARK_STACK || defined GC_MALLOC_CHECK |
f57e2426 J |
351 | static void mem_init (void); |
352 | static struct mem_node *mem_insert (void *, void *, enum mem_type); | |
353 | static void mem_insert_fixup (struct mem_node *); | |
354 | static void mem_rotate_left (struct mem_node *); | |
355 | static void mem_rotate_right (struct mem_node *); | |
356 | static void mem_delete (struct mem_node *); | |
357 | static void mem_delete_fixup (struct mem_node *); | |
b0ab8123 | 358 | static struct mem_node *mem_find (void *); |
0caaedb1 | 359 | #endif |
34400008 | 360 | |
34400008 GM |
361 | |
362 | #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS | |
f57e2426 | 363 | static void check_gcpros (void); |
34400008 GM |
364 | #endif |
365 | ||
877935b1 | 366 | #endif /* GC_MARK_STACK || GC_MALLOC_CHECK */ |
34400008 | 367 | |
ca78dc43 PE |
368 | #ifndef DEADP |
369 | # define DEADP(x) 0 | |
370 | #endif | |
371 | ||
1f0b3fd2 GM |
372 | /* Recording what needs to be marked for gc. */ |
373 | ||
374 | struct gcpro *gcprolist; | |
375 | ||
379b98b1 PE |
376 | /* Addresses of staticpro'd variables. Initialize it to a nonzero |
377 | value; otherwise some compilers put it into BSS. */ | |
1f0b3fd2 | 378 | |
d2824928 | 379 | #define NSTATICS 0x800 |
d3d47262 | 380 | static Lisp_Object *staticvec[NSTATICS] = {&Vpurify_flag}; |
1f0b3fd2 GM |
381 | |
382 | /* Index of next unused slot in staticvec. */ | |
383 | ||
fff62aa9 | 384 | static int staticidx; |
1f0b3fd2 | 385 | |
261cb4bb | 386 | static void *pure_alloc (size_t, int); |
1f0b3fd2 GM |
387 | |
388 | ||
389 | /* Value is SZ rounded up to the next multiple of ALIGNMENT. | |
390 | ALIGNMENT must be a power of 2. */ | |
391 | ||
ab6780cd | 392 | #define ALIGN(ptr, ALIGNMENT) \ |
261cb4bb PE |
393 | ((void *) (((uintptr_t) (ptr) + (ALIGNMENT) - 1) \ |
394 | & ~ ((ALIGNMENT) - 1))) | |
1f0b3fd2 | 395 | |
ece93c02 | 396 | |
7146af97 | 397 | \f |
34400008 GM |
398 | /************************************************************************ |
399 | Malloc | |
400 | ************************************************************************/ | |
401 | ||
4455ad75 | 402 | /* Function malloc calls this if it finds we are near exhausting storage. */ |
d457598b AS |
403 | |
404 | void | |
a8fe7202 | 405 | malloc_warning (const char *str) |
7146af97 JB |
406 | { |
407 | pending_malloc_warning = str; | |
408 | } | |
409 | ||
34400008 | 410 | |
4455ad75 | 411 | /* Display an already-pending malloc warning. */ |
34400008 | 412 | |
d457598b | 413 | void |
971de7fb | 414 | display_malloc_warning (void) |
7146af97 | 415 | { |
4455ad75 RS |
416 | call3 (intern ("display-warning"), |
417 | intern ("alloc"), | |
418 | build_string (pending_malloc_warning), | |
419 | intern ("emergency")); | |
7146af97 | 420 | pending_malloc_warning = 0; |
7146af97 | 421 | } |
49efed3a | 422 | \f |
276cbe5a RS |
423 | /* Called if we can't allocate relocatable space for a buffer. */ |
424 | ||
425 | void | |
d311d28c | 426 | buffer_memory_full (ptrdiff_t nbytes) |
276cbe5a | 427 | { |
2e471eb5 GM |
428 | /* If buffers use the relocating allocator, no need to free |
429 | spare_memory, because we may have plenty of malloc space left | |
430 | that we could get, and if we don't, the malloc that fails will | |
431 | itself cause spare_memory to be freed. If buffers don't use the | |
432 | relocating allocator, treat this like any other failing | |
433 | malloc. */ | |
276cbe5a RS |
434 | |
435 | #ifndef REL_ALLOC | |
531b0165 | 436 | memory_full (nbytes); |
276cbe5a RS |
437 | #endif |
438 | ||
2e471eb5 GM |
439 | /* This used to call error, but if we've run out of memory, we could |
440 | get infinite recursion trying to build the string. */ | |
9b306d37 | 441 | xsignal (Qnil, Vmemory_signal_data); |
7146af97 JB |
442 | } |
443 | ||
f3372c87 DA |
444 | /* A common multiple of the positive integers A and B. Ideally this |
445 | would be the least common multiple, but there's no way to do that | |
446 | as a constant expression in C, so do the best that we can easily do. */ | |
447 | #define COMMON_MULTIPLE(a, b) \ | |
448 | ((a) % (b) == 0 ? (a) : (b) % (a) == 0 ? (b) : (a) * (b)) | |
34400008 | 449 | |
c9d624c6 | 450 | #ifndef XMALLOC_OVERRUN_CHECK |
903fe15d | 451 | #define XMALLOC_OVERRUN_CHECK_OVERHEAD 0 |
c9d624c6 | 452 | #else |
212f33f1 | 453 | |
903fe15d PE |
454 | /* Check for overrun in malloc'ed buffers by wrapping a header and trailer |
455 | around each block. | |
bdbed949 | 456 | |
f701dc2a PE |
457 | The header consists of XMALLOC_OVERRUN_CHECK_SIZE fixed bytes |
458 | followed by XMALLOC_OVERRUN_SIZE_SIZE bytes containing the original | |
459 | block size in little-endian order. The trailer consists of | |
460 | XMALLOC_OVERRUN_CHECK_SIZE fixed bytes. | |
bdbed949 KS |
461 | |
462 | The header is used to detect whether this block has been allocated | |
f701dc2a PE |
463 | through these functions, as some low-level libc functions may |
464 | bypass the malloc hooks. */ | |
bdbed949 | 465 | |
212f33f1 | 466 | #define XMALLOC_OVERRUN_CHECK_SIZE 16 |
903fe15d | 467 | #define XMALLOC_OVERRUN_CHECK_OVERHEAD \ |
38532ce6 PE |
468 | (2 * XMALLOC_OVERRUN_CHECK_SIZE + XMALLOC_OVERRUN_SIZE_SIZE) |
469 | ||
470 | /* Define XMALLOC_OVERRUN_SIZE_SIZE so that (1) it's large enough to | |
f701dc2a PE |
471 | hold a size_t value and (2) the header size is a multiple of the |
472 | alignment that Emacs needs for C types and for USE_LSB_TAG. */ | |
473 | #define XMALLOC_BASE_ALIGNMENT \ | |
e32a5799 | 474 | alignof (union { long double d; intmax_t i; void *p; }) |
f3372c87 | 475 | |
bfe3e0a2 | 476 | #if USE_LSB_TAG |
f701dc2a | 477 | # define XMALLOC_HEADER_ALIGNMENT \ |
2b90362b | 478 | COMMON_MULTIPLE (GCALIGNMENT, XMALLOC_BASE_ALIGNMENT) |
38532ce6 PE |
479 | #else |
480 | # define XMALLOC_HEADER_ALIGNMENT XMALLOC_BASE_ALIGNMENT | |
481 | #endif | |
482 | #define XMALLOC_OVERRUN_SIZE_SIZE \ | |
f701dc2a PE |
483 | (((XMALLOC_OVERRUN_CHECK_SIZE + sizeof (size_t) \ |
484 | + XMALLOC_HEADER_ALIGNMENT - 1) \ | |
485 | / XMALLOC_HEADER_ALIGNMENT * XMALLOC_HEADER_ALIGNMENT) \ | |
486 | - XMALLOC_OVERRUN_CHECK_SIZE) | |
bdbed949 | 487 | |
903fe15d PE |
488 | static char const xmalloc_overrun_check_header[XMALLOC_OVERRUN_CHECK_SIZE] = |
489 | { '\x9a', '\x9b', '\xae', '\xaf', | |
490 | '\xbf', '\xbe', '\xce', '\xcf', | |
491 | '\xea', '\xeb', '\xec', '\xed', | |
492 | '\xdf', '\xde', '\x9c', '\x9d' }; | |
212f33f1 | 493 | |
903fe15d PE |
494 | static char const xmalloc_overrun_check_trailer[XMALLOC_OVERRUN_CHECK_SIZE] = |
495 | { '\xaa', '\xab', '\xac', '\xad', | |
496 | '\xba', '\xbb', '\xbc', '\xbd', | |
497 | '\xca', '\xcb', '\xcc', '\xcd', | |
498 | '\xda', '\xdb', '\xdc', '\xdd' }; | |
212f33f1 | 499 | |
903fe15d | 500 | /* Insert and extract the block size in the header. */ |
bdbed949 | 501 | |
903fe15d PE |
502 | static void |
503 | xmalloc_put_size (unsigned char *ptr, size_t size) | |
504 | { | |
505 | int i; | |
38532ce6 | 506 | for (i = 0; i < XMALLOC_OVERRUN_SIZE_SIZE; i++) |
903fe15d | 507 | { |
38532ce6 | 508 | *--ptr = size & ((1 << CHAR_BIT) - 1); |
903fe15d PE |
509 | size >>= CHAR_BIT; |
510 | } | |
511 | } | |
bdbed949 | 512 | |
903fe15d PE |
513 | static size_t |
514 | xmalloc_get_size (unsigned char *ptr) | |
515 | { | |
516 | size_t size = 0; | |
517 | int i; | |
38532ce6 PE |
518 | ptr -= XMALLOC_OVERRUN_SIZE_SIZE; |
519 | for (i = 0; i < XMALLOC_OVERRUN_SIZE_SIZE; i++) | |
903fe15d PE |
520 | { |
521 | size <<= CHAR_BIT; | |
522 | size += *ptr++; | |
523 | } | |
524 | return size; | |
525 | } | |
bdbed949 KS |
526 | |
527 | ||
528 | /* Like malloc, but wraps allocated block with header and trailer. */ | |
529 | ||
261cb4bb | 530 | static void * |
e7974947 | 531 | overrun_check_malloc (size_t size) |
212f33f1 | 532 | { |
bdbed949 | 533 | register unsigned char *val; |
0caaedb1 | 534 | if (SIZE_MAX - XMALLOC_OVERRUN_CHECK_OVERHEAD < size) |
1088b922 | 535 | emacs_abort (); |
212f33f1 | 536 | |
0caaedb1 PE |
537 | val = malloc (size + XMALLOC_OVERRUN_CHECK_OVERHEAD); |
538 | if (val) | |
212f33f1 | 539 | { |
903fe15d | 540 | memcpy (val, xmalloc_overrun_check_header, XMALLOC_OVERRUN_CHECK_SIZE); |
38532ce6 | 541 | val += XMALLOC_OVERRUN_CHECK_SIZE + XMALLOC_OVERRUN_SIZE_SIZE; |
903fe15d | 542 | xmalloc_put_size (val, size); |
72af86bd AS |
543 | memcpy (val + size, xmalloc_overrun_check_trailer, |
544 | XMALLOC_OVERRUN_CHECK_SIZE); | |
212f33f1 | 545 | } |
261cb4bb | 546 | return val; |
212f33f1 KS |
547 | } |
548 | ||
bdbed949 KS |
549 | |
550 | /* Like realloc, but checks old block for overrun, and wraps new block | |
551 | with header and trailer. */ | |
552 | ||
261cb4bb PE |
553 | static void * |
554 | overrun_check_realloc (void *block, size_t size) | |
212f33f1 | 555 | { |
e7974947 | 556 | register unsigned char *val = (unsigned char *) block; |
0caaedb1 | 557 | if (SIZE_MAX - XMALLOC_OVERRUN_CHECK_OVERHEAD < size) |
1088b922 | 558 | emacs_abort (); |
212f33f1 KS |
559 | |
560 | if (val | |
72af86bd | 561 | && memcmp (xmalloc_overrun_check_header, |
38532ce6 | 562 | val - XMALLOC_OVERRUN_CHECK_SIZE - XMALLOC_OVERRUN_SIZE_SIZE, |
903fe15d | 563 | XMALLOC_OVERRUN_CHECK_SIZE) == 0) |
212f33f1 | 564 | { |
903fe15d | 565 | size_t osize = xmalloc_get_size (val); |
72af86bd AS |
566 | if (memcmp (xmalloc_overrun_check_trailer, val + osize, |
567 | XMALLOC_OVERRUN_CHECK_SIZE)) | |
1088b922 | 568 | emacs_abort (); |
72af86bd | 569 | memset (val + osize, 0, XMALLOC_OVERRUN_CHECK_SIZE); |
38532ce6 PE |
570 | val -= XMALLOC_OVERRUN_CHECK_SIZE + XMALLOC_OVERRUN_SIZE_SIZE; |
571 | memset (val, 0, XMALLOC_OVERRUN_CHECK_SIZE + XMALLOC_OVERRUN_SIZE_SIZE); | |
212f33f1 KS |
572 | } |
573 | ||
0caaedb1 | 574 | val = realloc (val, size + XMALLOC_OVERRUN_CHECK_OVERHEAD); |
212f33f1 | 575 | |
0caaedb1 | 576 | if (val) |
212f33f1 | 577 | { |
903fe15d | 578 | memcpy (val, xmalloc_overrun_check_header, XMALLOC_OVERRUN_CHECK_SIZE); |
38532ce6 | 579 | val += XMALLOC_OVERRUN_CHECK_SIZE + XMALLOC_OVERRUN_SIZE_SIZE; |
903fe15d | 580 | xmalloc_put_size (val, size); |
72af86bd AS |
581 | memcpy (val + size, xmalloc_overrun_check_trailer, |
582 | XMALLOC_OVERRUN_CHECK_SIZE); | |
212f33f1 | 583 | } |
261cb4bb | 584 | return val; |
212f33f1 KS |
585 | } |
586 | ||
bdbed949 KS |
587 | /* Like free, but checks block for overrun. */ |
588 | ||
2538aa2f | 589 | static void |
261cb4bb | 590 | overrun_check_free (void *block) |
212f33f1 | 591 | { |
e7974947 | 592 | unsigned char *val = (unsigned char *) block; |
212f33f1 KS |
593 | |
594 | if (val | |
72af86bd | 595 | && memcmp (xmalloc_overrun_check_header, |
38532ce6 | 596 | val - XMALLOC_OVERRUN_CHECK_SIZE - XMALLOC_OVERRUN_SIZE_SIZE, |
903fe15d | 597 | XMALLOC_OVERRUN_CHECK_SIZE) == 0) |
212f33f1 | 598 | { |
903fe15d | 599 | size_t osize = xmalloc_get_size (val); |
72af86bd AS |
600 | if (memcmp (xmalloc_overrun_check_trailer, val + osize, |
601 | XMALLOC_OVERRUN_CHECK_SIZE)) | |
1088b922 | 602 | emacs_abort (); |
454d7973 | 603 | #ifdef XMALLOC_CLEAR_FREE_MEMORY |
38532ce6 | 604 | val -= XMALLOC_OVERRUN_CHECK_SIZE + XMALLOC_OVERRUN_SIZE_SIZE; |
903fe15d | 605 | memset (val, 0xff, osize + XMALLOC_OVERRUN_CHECK_OVERHEAD); |
454d7973 | 606 | #else |
72af86bd | 607 | memset (val + osize, 0, XMALLOC_OVERRUN_CHECK_SIZE); |
38532ce6 PE |
608 | val -= XMALLOC_OVERRUN_CHECK_SIZE + XMALLOC_OVERRUN_SIZE_SIZE; |
609 | memset (val, 0, XMALLOC_OVERRUN_CHECK_SIZE + XMALLOC_OVERRUN_SIZE_SIZE); | |
454d7973 | 610 | #endif |
212f33f1 KS |
611 | } |
612 | ||
613 | free (val); | |
614 | } | |
615 | ||
616 | #undef malloc | |
617 | #undef realloc | |
618 | #undef free | |
619 | #define malloc overrun_check_malloc | |
620 | #define realloc overrun_check_realloc | |
621 | #define free overrun_check_free | |
622 | #endif | |
623 | ||
0caaedb1 PE |
624 | /* If compiled with XMALLOC_BLOCK_INPUT_CHECK, define a symbol |
625 | BLOCK_INPUT_IN_MEMORY_ALLOCATORS that is visible to the debugger. | |
626 | If that variable is set, block input while in one of Emacs's memory | |
627 | allocation functions. There should be no need for this debugging | |
628 | option, since signal handlers do not allocate memory, but Emacs | |
629 | formerly allocated memory in signal handlers and this compile-time | |
630 | option remains as a way to help debug the issue should it rear its | |
631 | ugly head again. */ | |
632 | #ifdef XMALLOC_BLOCK_INPUT_CHECK | |
633 | bool block_input_in_memory_allocators EXTERNALLY_VISIBLE; | |
634 | static void | |
635 | malloc_block_input (void) | |
636 | { | |
637 | if (block_input_in_memory_allocators) | |
4d7e6e51 | 638 | block_input (); |
0caaedb1 PE |
639 | } |
640 | static void | |
641 | malloc_unblock_input (void) | |
642 | { | |
643 | if (block_input_in_memory_allocators) | |
4d7e6e51 | 644 | unblock_input (); |
0caaedb1 PE |
645 | } |
646 | # define MALLOC_BLOCK_INPUT malloc_block_input () | |
647 | # define MALLOC_UNBLOCK_INPUT malloc_unblock_input () | |
dafc79fa | 648 | #else |
0caaedb1 PE |
649 | # define MALLOC_BLOCK_INPUT ((void) 0) |
650 | # define MALLOC_UNBLOCK_INPUT ((void) 0) | |
dafc79fa | 651 | #endif |
bdbed949 | 652 | |
3a880af4 SM |
653 | #define MALLOC_PROBE(size) \ |
654 | do { \ | |
655 | if (profiler_memory_running) \ | |
656 | malloc_probe (size); \ | |
657 | } while (0) | |
658 | ||
659 | ||
34400008 | 660 | /* Like malloc but check for no memory and block interrupt input.. */ |
7146af97 | 661 | |
261cb4bb | 662 | void * |
971de7fb | 663 | xmalloc (size_t size) |
7146af97 | 664 | { |
261cb4bb | 665 | void *val; |
7146af97 | 666 | |
dafc79fa | 667 | MALLOC_BLOCK_INPUT; |
261cb4bb | 668 | val = malloc (size); |
dafc79fa | 669 | MALLOC_UNBLOCK_INPUT; |
7146af97 | 670 | |
2e471eb5 | 671 | if (!val && size) |
531b0165 | 672 | memory_full (size); |
c2d7786e | 673 | MALLOC_PROBE (size); |
7146af97 JB |
674 | return val; |
675 | } | |
676 | ||
23f86fce DA |
677 | /* Like the above, but zeroes out the memory just allocated. */ |
678 | ||
679 | void * | |
680 | xzalloc (size_t size) | |
681 | { | |
682 | void *val; | |
683 | ||
684 | MALLOC_BLOCK_INPUT; | |
685 | val = malloc (size); | |
686 | MALLOC_UNBLOCK_INPUT; | |
687 | ||
688 | if (!val && size) | |
689 | memory_full (size); | |
690 | memset (val, 0, size); | |
c2d7786e | 691 | MALLOC_PROBE (size); |
23f86fce DA |
692 | return val; |
693 | } | |
34400008 GM |
694 | |
695 | /* Like realloc but check for no memory and block interrupt input.. */ | |
696 | ||
261cb4bb PE |
697 | void * |
698 | xrealloc (void *block, size_t size) | |
7146af97 | 699 | { |
261cb4bb | 700 | void *val; |
7146af97 | 701 | |
dafc79fa | 702 | MALLOC_BLOCK_INPUT; |
56d2031b JB |
703 | /* We must call malloc explicitly when BLOCK is 0, since some |
704 | reallocs don't do this. */ | |
705 | if (! block) | |
261cb4bb | 706 | val = malloc (size); |
f048679d | 707 | else |
261cb4bb | 708 | val = realloc (block, size); |
dafc79fa | 709 | MALLOC_UNBLOCK_INPUT; |
7146af97 | 710 | |
531b0165 PE |
711 | if (!val && size) |
712 | memory_full (size); | |
c2d7786e | 713 | MALLOC_PROBE (size); |
7146af97 JB |
714 | return val; |
715 | } | |
9ac0d9e0 | 716 | |
34400008 | 717 | |
005ca5c7 | 718 | /* Like free but block interrupt input. */ |
34400008 | 719 | |
9ac0d9e0 | 720 | void |
261cb4bb | 721 | xfree (void *block) |
9ac0d9e0 | 722 | { |
70fdbb46 JM |
723 | if (!block) |
724 | return; | |
dafc79fa | 725 | MALLOC_BLOCK_INPUT; |
9ac0d9e0 | 726 | free (block); |
dafc79fa | 727 | MALLOC_UNBLOCK_INPUT; |
24d8a105 | 728 | /* We don't call refill_memory_reserve here |
0caaedb1 | 729 | because in practice the call in r_alloc_free seems to suffice. */ |
9ac0d9e0 JB |
730 | } |
731 | ||
c8099634 | 732 | |
0065d054 PE |
733 | /* Other parts of Emacs pass large int values to allocator functions |
734 | expecting ptrdiff_t. This is portable in practice, but check it to | |
735 | be safe. */ | |
736 | verify (INT_MAX <= PTRDIFF_MAX); | |
737 | ||
738 | ||
739 | /* Allocate an array of NITEMS items, each of size ITEM_SIZE. | |
740 | Signal an error on memory exhaustion, and block interrupt input. */ | |
741 | ||
742 | void * | |
743 | xnmalloc (ptrdiff_t nitems, ptrdiff_t item_size) | |
744 | { | |
a54e2c05 | 745 | eassert (0 <= nitems && 0 < item_size); |
0065d054 PE |
746 | if (min (PTRDIFF_MAX, SIZE_MAX) / item_size < nitems) |
747 | memory_full (SIZE_MAX); | |
748 | return xmalloc (nitems * item_size); | |
749 | } | |
750 | ||
751 | ||
752 | /* Reallocate an array PA to make it of NITEMS items, each of size ITEM_SIZE. | |
753 | Signal an error on memory exhaustion, and block interrupt input. */ | |
754 | ||
755 | void * | |
756 | xnrealloc (void *pa, ptrdiff_t nitems, ptrdiff_t item_size) | |
757 | { | |
a54e2c05 | 758 | eassert (0 <= nitems && 0 < item_size); |
0065d054 PE |
759 | if (min (PTRDIFF_MAX, SIZE_MAX) / item_size < nitems) |
760 | memory_full (SIZE_MAX); | |
761 | return xrealloc (pa, nitems * item_size); | |
762 | } | |
763 | ||
764 | ||
765 | /* Grow PA, which points to an array of *NITEMS items, and return the | |
766 | location of the reallocated array, updating *NITEMS to reflect its | |
767 | new size. The new array will contain at least NITEMS_INCR_MIN more | |
768 | items, but will not contain more than NITEMS_MAX items total. | |
769 | ITEM_SIZE is the size of each item, in bytes. | |
770 | ||
771 | ITEM_SIZE and NITEMS_INCR_MIN must be positive. *NITEMS must be | |
772 | nonnegative. If NITEMS_MAX is -1, it is treated as if it were | |
773 | infinity. | |
774 | ||
775 | If PA is null, then allocate a new array instead of reallocating | |
776 | the old one. Thus, to grow an array A without saving its old | |
777 | contents, invoke xfree (A) immediately followed by xgrowalloc (0, | |
778 | &NITEMS, ...). | |
779 | ||
780 | Block interrupt input as needed. If memory exhaustion occurs, set | |
781 | *NITEMS to zero if PA is null, and signal an error (i.e., do not | |
782 | return). */ | |
783 | ||
784 | void * | |
785 | xpalloc (void *pa, ptrdiff_t *nitems, ptrdiff_t nitems_incr_min, | |
786 | ptrdiff_t nitems_max, ptrdiff_t item_size) | |
787 | { | |
788 | /* The approximate size to use for initial small allocation | |
789 | requests. This is the largest "small" request for the GNU C | |
790 | library malloc. */ | |
791 | enum { DEFAULT_MXFAST = 64 * sizeof (size_t) / 4 }; | |
792 | ||
793 | /* If the array is tiny, grow it to about (but no greater than) | |
794 | DEFAULT_MXFAST bytes. Otherwise, grow it by about 50%. */ | |
795 | ptrdiff_t n = *nitems; | |
796 | ptrdiff_t tiny_max = DEFAULT_MXFAST / item_size - n; | |
797 | ptrdiff_t half_again = n >> 1; | |
798 | ptrdiff_t incr_estimate = max (tiny_max, half_again); | |
799 | ||
800 | /* Adjust the increment according to three constraints: NITEMS_INCR_MIN, | |
801 | NITEMS_MAX, and what the C language can represent safely. */ | |
802 | ptrdiff_t C_language_max = min (PTRDIFF_MAX, SIZE_MAX) / item_size; | |
803 | ptrdiff_t n_max = (0 <= nitems_max && nitems_max < C_language_max | |
804 | ? nitems_max : C_language_max); | |
805 | ptrdiff_t nitems_incr_max = n_max - n; | |
806 | ptrdiff_t incr = max (nitems_incr_min, min (incr_estimate, nitems_incr_max)); | |
807 | ||
a54e2c05 | 808 | eassert (0 < item_size && 0 < nitems_incr_min && 0 <= n && -1 <= nitems_max); |
0065d054 PE |
809 | if (! pa) |
810 | *nitems = 0; | |
811 | if (nitems_incr_max < incr) | |
812 | memory_full (SIZE_MAX); | |
813 | n += incr; | |
814 | pa = xrealloc (pa, n * item_size); | |
815 | *nitems = n; | |
816 | return pa; | |
817 | } | |
818 | ||
819 | ||
dca7c6a8 GM |
820 | /* Like strdup, but uses xmalloc. */ |
821 | ||
822 | char * | |
971de7fb | 823 | xstrdup (const char *s) |
dca7c6a8 | 824 | { |
675d5130 | 825 | size_t len = strlen (s) + 1; |
23f86fce | 826 | char *p = xmalloc (len); |
72af86bd | 827 | memcpy (p, s, len); |
dca7c6a8 GM |
828 | return p; |
829 | } | |
830 | ||
831 | ||
f61bef8b KS |
832 | /* Unwind for SAFE_ALLOCA */ |
833 | ||
834 | Lisp_Object | |
971de7fb | 835 | safe_alloca_unwind (Lisp_Object arg) |
f61bef8b | 836 | { |
b766f870 KS |
837 | register struct Lisp_Save_Value *p = XSAVE_VALUE (arg); |
838 | ||
839 | p->dogc = 0; | |
840 | xfree (p->pointer); | |
841 | p->pointer = 0; | |
7b7990cc | 842 | free_misc (arg); |
f61bef8b KS |
843 | return Qnil; |
844 | } | |
845 | ||
98c6f1e3 PE |
846 | /* Return a newly allocated memory block of SIZE bytes, remembering |
847 | to free it when unwinding. */ | |
848 | void * | |
849 | record_xmalloc (size_t size) | |
850 | { | |
851 | void *p = xmalloc (size); | |
852 | record_unwind_protect (safe_alloca_unwind, make_save_value (p, 0)); | |
853 | return p; | |
854 | } | |
855 | ||
f61bef8b | 856 | |
34400008 GM |
857 | /* Like malloc but used for allocating Lisp data. NBYTES is the |
858 | number of bytes to allocate, TYPE describes the intended use of the | |
91af3942 | 859 | allocated memory block (for strings, for conses, ...). */ |
34400008 | 860 | |
bfe3e0a2 PE |
861 | #if ! USE_LSB_TAG |
862 | void *lisp_malloc_loser EXTERNALLY_VISIBLE; | |
212f33f1 | 863 | #endif |
918a23a7 | 864 | |
261cb4bb | 865 | static void * |
971de7fb | 866 | lisp_malloc (size_t nbytes, enum mem_type type) |
c8099634 | 867 | { |
34400008 | 868 | register void *val; |
c8099634 | 869 | |
dafc79fa | 870 | MALLOC_BLOCK_INPUT; |
877935b1 GM |
871 | |
872 | #ifdef GC_MALLOC_CHECK | |
873 | allocated_mem_type = type; | |
874 | #endif | |
177c0ea7 | 875 | |
38182d90 | 876 | val = malloc (nbytes); |
c8099634 | 877 | |
bfe3e0a2 | 878 | #if ! USE_LSB_TAG |
918a23a7 RS |
879 | /* If the memory just allocated cannot be addressed thru a Lisp |
880 | object's pointer, and it needs to be, | |
881 | that's equivalent to running out of memory. */ | |
882 | if (val && type != MEM_TYPE_NON_LISP) | |
883 | { | |
884 | Lisp_Object tem; | |
885 | XSETCONS (tem, (char *) val + nbytes - 1); | |
886 | if ((char *) XCONS (tem) != (char *) val + nbytes - 1) | |
887 | { | |
888 | lisp_malloc_loser = val; | |
889 | free (val); | |
890 | val = 0; | |
891 | } | |
892 | } | |
6b61353c | 893 | #endif |
918a23a7 | 894 | |
877935b1 | 895 | #if GC_MARK_STACK && !defined GC_MALLOC_CHECK |
dca7c6a8 | 896 | if (val && type != MEM_TYPE_NON_LISP) |
34400008 GM |
897 | mem_insert (val, (char *) val + nbytes, type); |
898 | #endif | |
177c0ea7 | 899 | |
dafc79fa | 900 | MALLOC_UNBLOCK_INPUT; |
dca7c6a8 | 901 | if (!val && nbytes) |
531b0165 | 902 | memory_full (nbytes); |
c2d7786e | 903 | MALLOC_PROBE (nbytes); |
c8099634 RS |
904 | return val; |
905 | } | |
906 | ||
34400008 GM |
907 | /* Free BLOCK. This must be called to free memory allocated with a |
908 | call to lisp_malloc. */ | |
909 | ||
bf952fb6 | 910 | static void |
261cb4bb | 911 | lisp_free (void *block) |
c8099634 | 912 | { |
dafc79fa | 913 | MALLOC_BLOCK_INPUT; |
c8099634 | 914 | free (block); |
877935b1 | 915 | #if GC_MARK_STACK && !defined GC_MALLOC_CHECK |
34400008 GM |
916 | mem_delete (mem_find (block)); |
917 | #endif | |
dafc79fa | 918 | MALLOC_UNBLOCK_INPUT; |
c8099634 | 919 | } |
34400008 | 920 | |
453b951e SM |
921 | /***** Allocation of aligned blocks of memory to store Lisp data. *****/ |
922 | ||
923 | /* The entry point is lisp_align_malloc which returns blocks of at most | |
924 | BLOCK_BYTES and guarantees they are aligned on a BLOCK_ALIGN boundary. */ | |
ab6780cd | 925 | |
b4181b01 KS |
926 | #if defined (HAVE_POSIX_MEMALIGN) && defined (SYSTEM_MALLOC) |
927 | #define USE_POSIX_MEMALIGN 1 | |
928 | #endif | |
ab6780cd SM |
929 | |
930 | /* BLOCK_ALIGN has to be a power of 2. */ | |
931 | #define BLOCK_ALIGN (1 << 10) | |
ab6780cd SM |
932 | |
933 | /* Padding to leave at the end of a malloc'd block. This is to give | |
934 | malloc a chance to minimize the amount of memory wasted to alignment. | |
935 | It should be tuned to the particular malloc library used. | |
19bcad1f SM |
936 | On glibc-2.3.2, malloc never tries to align, so a padding of 0 is best. |
937 | posix_memalign on the other hand would ideally prefer a value of 4 | |
938 | because otherwise, there's 1020 bytes wasted between each ablocks. | |
f501ccb4 SM |
939 | In Emacs, testing shows that those 1020 can most of the time be |
940 | efficiently used by malloc to place other objects, so a value of 0 can | |
941 | still preferable unless you have a lot of aligned blocks and virtually | |
942 | nothing else. */ | |
19bcad1f SM |
943 | #define BLOCK_PADDING 0 |
944 | #define BLOCK_BYTES \ | |
0b432f21 | 945 | (BLOCK_ALIGN - sizeof (struct ablocks *) - BLOCK_PADDING) |
19bcad1f SM |
946 | |
947 | /* Internal data structures and constants. */ | |
948 | ||
ab6780cd SM |
949 | #define ABLOCKS_SIZE 16 |
950 | ||
951 | /* An aligned block of memory. */ | |
952 | struct ablock | |
953 | { | |
954 | union | |
955 | { | |
956 | char payload[BLOCK_BYTES]; | |
957 | struct ablock *next_free; | |
958 | } x; | |
959 | /* `abase' is the aligned base of the ablocks. */ | |
960 | /* It is overloaded to hold the virtual `busy' field that counts | |
961 | the number of used ablock in the parent ablocks. | |
962 | The first ablock has the `busy' field, the others have the `abase' | |
963 | field. To tell the difference, we assume that pointers will have | |
964 | integer values larger than 2 * ABLOCKS_SIZE. The lowest bit of `busy' | |
965 | is used to tell whether the real base of the parent ablocks is `abase' | |
966 | (if not, the word before the first ablock holds a pointer to the | |
967 | real base). */ | |
968 | struct ablocks *abase; | |
969 | /* The padding of all but the last ablock is unused. The padding of | |
970 | the last ablock in an ablocks is not allocated. */ | |
19bcad1f SM |
971 | #if BLOCK_PADDING |
972 | char padding[BLOCK_PADDING]; | |
ebb8d410 | 973 | #endif |
ab6780cd SM |
974 | }; |
975 | ||
976 | /* A bunch of consecutive aligned blocks. */ | |
977 | struct ablocks | |
978 | { | |
979 | struct ablock blocks[ABLOCKS_SIZE]; | |
980 | }; | |
981 | ||
4b5afbb0 | 982 | /* Size of the block requested from malloc or posix_memalign. */ |
19bcad1f | 983 | #define ABLOCKS_BYTES (sizeof (struct ablocks) - BLOCK_PADDING) |
ab6780cd SM |
984 | |
985 | #define ABLOCK_ABASE(block) \ | |
d01a7826 | 986 | (((uintptr_t) (block)->abase) <= (1 + 2 * ABLOCKS_SIZE) \ |
ab6780cd SM |
987 | ? (struct ablocks *)(block) \ |
988 | : (block)->abase) | |
989 | ||
990 | /* Virtual `busy' field. */ | |
991 | #define ABLOCKS_BUSY(abase) ((abase)->blocks[0].abase) | |
992 | ||
993 | /* Pointer to the (not necessarily aligned) malloc block. */ | |
349a4500 | 994 | #ifdef USE_POSIX_MEMALIGN |
19bcad1f SM |
995 | #define ABLOCKS_BASE(abase) (abase) |
996 | #else | |
ab6780cd | 997 | #define ABLOCKS_BASE(abase) \ |
d01a7826 | 998 | (1 & (intptr_t) ABLOCKS_BUSY (abase) ? abase : ((void**)abase)[-1]) |
19bcad1f | 999 | #endif |
ab6780cd SM |
1000 | |
1001 | /* The list of free ablock. */ | |
1002 | static struct ablock *free_ablock; | |
1003 | ||
1004 | /* Allocate an aligned block of nbytes. | |
1005 | Alignment is on a multiple of BLOCK_ALIGN and `nbytes' has to be | |
1006 | smaller or equal to BLOCK_BYTES. */ | |
261cb4bb | 1007 | static void * |
971de7fb | 1008 | lisp_align_malloc (size_t nbytes, enum mem_type type) |
ab6780cd SM |
1009 | { |
1010 | void *base, *val; | |
1011 | struct ablocks *abase; | |
1012 | ||
1013 | eassert (nbytes <= BLOCK_BYTES); | |
1014 | ||
dafc79fa | 1015 | MALLOC_BLOCK_INPUT; |
ab6780cd SM |
1016 | |
1017 | #ifdef GC_MALLOC_CHECK | |
1018 | allocated_mem_type = type; | |
1019 | #endif | |
1020 | ||
1021 | if (!free_ablock) | |
1022 | { | |
005ca5c7 | 1023 | int i; |
d01a7826 | 1024 | intptr_t aligned; /* int gets warning casting to 64-bit pointer. */ |
ab6780cd SM |
1025 | |
1026 | #ifdef DOUG_LEA_MALLOC | |
1027 | /* Prevent mmap'ing the chunk. Lisp data may not be mmap'ed | |
1028 | because mapped region contents are not preserved in | |
1029 | a dumped Emacs. */ | |
1030 | mallopt (M_MMAP_MAX, 0); | |
1031 | #endif | |
1032 | ||
349a4500 | 1033 | #ifdef USE_POSIX_MEMALIGN |
19bcad1f SM |
1034 | { |
1035 | int err = posix_memalign (&base, BLOCK_ALIGN, ABLOCKS_BYTES); | |
ab349c19 RS |
1036 | if (err) |
1037 | base = NULL; | |
1038 | abase = base; | |
19bcad1f SM |
1039 | } |
1040 | #else | |
ab6780cd SM |
1041 | base = malloc (ABLOCKS_BYTES); |
1042 | abase = ALIGN (base, BLOCK_ALIGN); | |
ab349c19 RS |
1043 | #endif |
1044 | ||
6b61353c KH |
1045 | if (base == 0) |
1046 | { | |
dafc79fa | 1047 | MALLOC_UNBLOCK_INPUT; |
531b0165 | 1048 | memory_full (ABLOCKS_BYTES); |
6b61353c | 1049 | } |
ab6780cd SM |
1050 | |
1051 | aligned = (base == abase); | |
1052 | if (!aligned) | |
1053 | ((void**)abase)[-1] = base; | |
1054 | ||
1055 | #ifdef DOUG_LEA_MALLOC | |
1056 | /* Back to a reasonable maximum of mmap'ed areas. */ | |
1057 | mallopt (M_MMAP_MAX, MMAP_MAX_AREAS); | |
1058 | #endif | |
1059 | ||
bfe3e0a2 | 1060 | #if ! USE_LSB_TAG |
8f924df7 KH |
1061 | /* If the memory just allocated cannot be addressed thru a Lisp |
1062 | object's pointer, and it needs to be, that's equivalent to | |
1063 | running out of memory. */ | |
1064 | if (type != MEM_TYPE_NON_LISP) | |
1065 | { | |
1066 | Lisp_Object tem; | |
1067 | char *end = (char *) base + ABLOCKS_BYTES - 1; | |
1068 | XSETCONS (tem, end); | |
1069 | if ((char *) XCONS (tem) != end) | |
1070 | { | |
1071 | lisp_malloc_loser = base; | |
1072 | free (base); | |
dafc79fa | 1073 | MALLOC_UNBLOCK_INPUT; |
531b0165 | 1074 | memory_full (SIZE_MAX); |
8f924df7 KH |
1075 | } |
1076 | } | |
6b61353c | 1077 | #endif |
8f924df7 | 1078 | |
ab6780cd | 1079 | /* Initialize the blocks and put them on the free list. |
453b951e | 1080 | If `base' was not properly aligned, we can't use the last block. */ |
ab6780cd SM |
1081 | for (i = 0; i < (aligned ? ABLOCKS_SIZE : ABLOCKS_SIZE - 1); i++) |
1082 | { | |
1083 | abase->blocks[i].abase = abase; | |
1084 | abase->blocks[i].x.next_free = free_ablock; | |
1085 | free_ablock = &abase->blocks[i]; | |
1086 | } | |
8ac068ac | 1087 | ABLOCKS_BUSY (abase) = (struct ablocks *) aligned; |
ab6780cd | 1088 | |
d01a7826 | 1089 | eassert (0 == ((uintptr_t) abase) % BLOCK_ALIGN); |
ab6780cd SM |
1090 | eassert (ABLOCK_ABASE (&abase->blocks[3]) == abase); /* 3 is arbitrary */ |
1091 | eassert (ABLOCK_ABASE (&abase->blocks[0]) == abase); | |
1092 | eassert (ABLOCKS_BASE (abase) == base); | |
d01a7826 | 1093 | eassert (aligned == (intptr_t) ABLOCKS_BUSY (abase)); |
ab6780cd SM |
1094 | } |
1095 | ||
1096 | abase = ABLOCK_ABASE (free_ablock); | |
8ac068ac | 1097 | ABLOCKS_BUSY (abase) = |
d01a7826 | 1098 | (struct ablocks *) (2 + (intptr_t) ABLOCKS_BUSY (abase)); |
ab6780cd SM |
1099 | val = free_ablock; |
1100 | free_ablock = free_ablock->x.next_free; | |
1101 | ||
ab6780cd | 1102 | #if GC_MARK_STACK && !defined GC_MALLOC_CHECK |
3687c2ef | 1103 | if (type != MEM_TYPE_NON_LISP) |
ab6780cd SM |
1104 | mem_insert (val, (char *) val + nbytes, type); |
1105 | #endif | |
1106 | ||
dafc79fa | 1107 | MALLOC_UNBLOCK_INPUT; |
ab6780cd | 1108 | |
c2d7786e TM |
1109 | MALLOC_PROBE (nbytes); |
1110 | ||
d01a7826 | 1111 | eassert (0 == ((uintptr_t) val) % BLOCK_ALIGN); |
ab6780cd SM |
1112 | return val; |
1113 | } | |
1114 | ||
1115 | static void | |
261cb4bb | 1116 | lisp_align_free (void *block) |
ab6780cd SM |
1117 | { |
1118 | struct ablock *ablock = block; | |
1119 | struct ablocks *abase = ABLOCK_ABASE (ablock); | |
1120 | ||
dafc79fa | 1121 | MALLOC_BLOCK_INPUT; |
ab6780cd SM |
1122 | #if GC_MARK_STACK && !defined GC_MALLOC_CHECK |
1123 | mem_delete (mem_find (block)); | |
1124 | #endif | |
1125 | /* Put on free list. */ | |
1126 | ablock->x.next_free = free_ablock; | |
1127 | free_ablock = ablock; | |
1128 | /* Update busy count. */ | |
453b951e SM |
1129 | ABLOCKS_BUSY (abase) |
1130 | = (struct ablocks *) (-2 + (intptr_t) ABLOCKS_BUSY (abase)); | |
d2db1c32 | 1131 | |
d01a7826 | 1132 | if (2 > (intptr_t) ABLOCKS_BUSY (abase)) |
ab6780cd | 1133 | { /* All the blocks are free. */ |
d01a7826 | 1134 | int i = 0, aligned = (intptr_t) ABLOCKS_BUSY (abase); |
ab6780cd SM |
1135 | struct ablock **tem = &free_ablock; |
1136 | struct ablock *atop = &abase->blocks[aligned ? ABLOCKS_SIZE : ABLOCKS_SIZE - 1]; | |
1137 | ||
1138 | while (*tem) | |
1139 | { | |
1140 | if (*tem >= (struct ablock *) abase && *tem < atop) | |
1141 | { | |
1142 | i++; | |
1143 | *tem = (*tem)->x.next_free; | |
1144 | } | |
1145 | else | |
1146 | tem = &(*tem)->x.next_free; | |
1147 | } | |
1148 | eassert ((aligned & 1) == aligned); | |
1149 | eassert (i == (aligned ? ABLOCKS_SIZE : ABLOCKS_SIZE - 1)); | |
349a4500 | 1150 | #ifdef USE_POSIX_MEMALIGN |
d01a7826 | 1151 | eassert ((uintptr_t) ABLOCKS_BASE (abase) % BLOCK_ALIGN == 0); |
cfb2f32e | 1152 | #endif |
ab6780cd SM |
1153 | free (ABLOCKS_BASE (abase)); |
1154 | } | |
dafc79fa | 1155 | MALLOC_UNBLOCK_INPUT; |
ab6780cd | 1156 | } |
3ef06d12 | 1157 | |
9ac0d9e0 | 1158 | \f |
2e471eb5 GM |
1159 | /*********************************************************************** |
1160 | Interval Allocation | |
1161 | ***********************************************************************/ | |
1a4f1e2c | 1162 | |
34400008 GM |
1163 | /* Number of intervals allocated in an interval_block structure. |
1164 | The 1020 is 1024 minus malloc overhead. */ | |
1165 | ||
d5e35230 JA |
1166 | #define INTERVAL_BLOCK_SIZE \ |
1167 | ((1020 - sizeof (struct interval_block *)) / sizeof (struct interval)) | |
1168 | ||
34400008 GM |
1169 | /* Intervals are allocated in chunks in form of an interval_block |
1170 | structure. */ | |
1171 | ||
d5e35230 | 1172 | struct interval_block |
2e471eb5 | 1173 | { |
6b61353c | 1174 | /* Place `intervals' first, to preserve alignment. */ |
2e471eb5 | 1175 | struct interval intervals[INTERVAL_BLOCK_SIZE]; |
6b61353c | 1176 | struct interval_block *next; |
2e471eb5 | 1177 | }; |
d5e35230 | 1178 | |
34400008 GM |
1179 | /* Current interval block. Its `next' pointer points to older |
1180 | blocks. */ | |
1181 | ||
d3d47262 | 1182 | static struct interval_block *interval_block; |
34400008 GM |
1183 | |
1184 | /* Index in interval_block above of the next unused interval | |
1185 | structure. */ | |
1186 | ||
fff62aa9 | 1187 | static int interval_block_index = INTERVAL_BLOCK_SIZE; |
34400008 GM |
1188 | |
1189 | /* Number of free and live intervals. */ | |
1190 | ||
c0c5c8ae | 1191 | static EMACS_INT total_free_intervals, total_intervals; |
d5e35230 | 1192 | |
34400008 GM |
1193 | /* List of free intervals. */ |
1194 | ||
244ed907 | 1195 | static INTERVAL interval_free_list; |
d5e35230 | 1196 | |
34400008 | 1197 | /* Return a new interval. */ |
d5e35230 JA |
1198 | |
1199 | INTERVAL | |
971de7fb | 1200 | make_interval (void) |
d5e35230 JA |
1201 | { |
1202 | INTERVAL val; | |
1203 | ||
dafc79fa | 1204 | MALLOC_BLOCK_INPUT; |
cfb2f32e | 1205 | |
d5e35230 JA |
1206 | if (interval_free_list) |
1207 | { | |
1208 | val = interval_free_list; | |
439d5cb4 | 1209 | interval_free_list = INTERVAL_PARENT (interval_free_list); |
d5e35230 JA |
1210 | } |
1211 | else | |
1212 | { | |
1213 | if (interval_block_index == INTERVAL_BLOCK_SIZE) | |
1214 | { | |
38182d90 PE |
1215 | struct interval_block *newi |
1216 | = lisp_malloc (sizeof *newi, MEM_TYPE_NON_LISP); | |
d5e35230 | 1217 | |
d5e35230 JA |
1218 | newi->next = interval_block; |
1219 | interval_block = newi; | |
1220 | interval_block_index = 0; | |
3900d5de | 1221 | total_free_intervals += INTERVAL_BLOCK_SIZE; |
d5e35230 JA |
1222 | } |
1223 | val = &interval_block->intervals[interval_block_index++]; | |
1224 | } | |
e2984df0 | 1225 | |
dafc79fa | 1226 | MALLOC_UNBLOCK_INPUT; |
e2984df0 | 1227 | |
d5e35230 | 1228 | consing_since_gc += sizeof (struct interval); |
310ea200 | 1229 | intervals_consed++; |
3900d5de | 1230 | total_free_intervals--; |
d5e35230 | 1231 | RESET_INTERVAL (val); |
2336fe58 | 1232 | val->gcmarkbit = 0; |
d5e35230 JA |
1233 | return val; |
1234 | } | |
1235 | ||
34400008 | 1236 | |
ee28be33 | 1237 | /* Mark Lisp objects in interval I. */ |
d5e35230 JA |
1238 | |
1239 | static void | |
971de7fb | 1240 | mark_interval (register INTERVAL i, Lisp_Object dummy) |
d5e35230 | 1241 | { |
cce7fefc DA |
1242 | /* Intervals should never be shared. So, if extra internal checking is |
1243 | enabled, GC aborts if it seems to have visited an interval twice. */ | |
1244 | eassert (!i->gcmarkbit); | |
2336fe58 | 1245 | i->gcmarkbit = 1; |
49723c04 | 1246 | mark_object (i->plist); |
d5e35230 JA |
1247 | } |
1248 | ||
34400008 GM |
1249 | /* Mark the interval tree rooted in I. */ |
1250 | ||
8707c1e5 DA |
1251 | #define MARK_INTERVAL_TREE(i) \ |
1252 | do { \ | |
1253 | if (i && !i->gcmarkbit) \ | |
1254 | traverse_intervals_noorder (i, mark_interval, Qnil); \ | |
2e471eb5 | 1255 | } while (0) |
77c7bcb1 | 1256 | |
2e471eb5 GM |
1257 | /*********************************************************************** |
1258 | String Allocation | |
1259 | ***********************************************************************/ | |
1a4f1e2c | 1260 | |
2e471eb5 GM |
1261 | /* Lisp_Strings are allocated in string_block structures. When a new |
1262 | string_block is allocated, all the Lisp_Strings it contains are | |
e0fead5d | 1263 | added to a free-list string_free_list. When a new Lisp_String is |
2e471eb5 GM |
1264 | needed, it is taken from that list. During the sweep phase of GC, |
1265 | string_blocks that are entirely free are freed, except two which | |
1266 | we keep. | |
7146af97 | 1267 | |
2e471eb5 GM |
1268 | String data is allocated from sblock structures. Strings larger |
1269 | than LARGE_STRING_BYTES, get their own sblock, data for smaller | |
1270 | strings is sub-allocated out of sblocks of size SBLOCK_SIZE. | |
7146af97 | 1271 | |
2e471eb5 GM |
1272 | Sblocks consist internally of sdata structures, one for each |
1273 | Lisp_String. The sdata structure points to the Lisp_String it | |
1274 | belongs to. The Lisp_String points back to the `u.data' member of | |
1275 | its sdata structure. | |
7146af97 | 1276 | |
2e471eb5 GM |
1277 | When a Lisp_String is freed during GC, it is put back on |
1278 | string_free_list, and its `data' member and its sdata's `string' | |
1279 | pointer is set to null. The size of the string is recorded in the | |
1280 | `u.nbytes' member of the sdata. So, sdata structures that are no | |
1281 | longer used, can be easily recognized, and it's easy to compact the | |
1282 | sblocks of small strings which we do in compact_small_strings. */ | |
7146af97 | 1283 | |
2e471eb5 GM |
1284 | /* Size in bytes of an sblock structure used for small strings. This |
1285 | is 8192 minus malloc overhead. */ | |
7146af97 | 1286 | |
2e471eb5 | 1287 | #define SBLOCK_SIZE 8188 |
c8099634 | 1288 | |
2e471eb5 GM |
1289 | /* Strings larger than this are considered large strings. String data |
1290 | for large strings is allocated from individual sblocks. */ | |
7146af97 | 1291 | |
2e471eb5 GM |
1292 | #define LARGE_STRING_BYTES 1024 |
1293 | ||
1294 | /* Structure describing string memory sub-allocated from an sblock. | |
1295 | This is where the contents of Lisp strings are stored. */ | |
1296 | ||
1297 | struct sdata | |
7146af97 | 1298 | { |
2e471eb5 GM |
1299 | /* Back-pointer to the string this sdata belongs to. If null, this |
1300 | structure is free, and the NBYTES member of the union below | |
34400008 | 1301 | contains the string's byte size (the same value that STRING_BYTES |
2e471eb5 GM |
1302 | would return if STRING were non-null). If non-null, STRING_BYTES |
1303 | (STRING) is the size of the data, and DATA contains the string's | |
1304 | contents. */ | |
1305 | struct Lisp_String *string; | |
7146af97 | 1306 | |
31d929e5 | 1307 | #ifdef GC_CHECK_STRING_BYTES |
177c0ea7 | 1308 | |
d311d28c | 1309 | ptrdiff_t nbytes; |
31d929e5 | 1310 | unsigned char data[1]; |
177c0ea7 | 1311 | |
31d929e5 GM |
1312 | #define SDATA_NBYTES(S) (S)->nbytes |
1313 | #define SDATA_DATA(S) (S)->data | |
36372bf9 | 1314 | #define SDATA_SELECTOR(member) member |
177c0ea7 | 1315 | |
31d929e5 GM |
1316 | #else /* not GC_CHECK_STRING_BYTES */ |
1317 | ||
2e471eb5 GM |
1318 | union |
1319 | { | |
83998b7a | 1320 | /* When STRING is non-null. */ |
2e471eb5 GM |
1321 | unsigned char data[1]; |
1322 | ||
1323 | /* When STRING is null. */ | |
d311d28c | 1324 | ptrdiff_t nbytes; |
2e471eb5 | 1325 | } u; |
177c0ea7 | 1326 | |
31d929e5 GM |
1327 | #define SDATA_NBYTES(S) (S)->u.nbytes |
1328 | #define SDATA_DATA(S) (S)->u.data | |
36372bf9 | 1329 | #define SDATA_SELECTOR(member) u.member |
31d929e5 GM |
1330 | |
1331 | #endif /* not GC_CHECK_STRING_BYTES */ | |
36372bf9 PE |
1332 | |
1333 | #define SDATA_DATA_OFFSET offsetof (struct sdata, SDATA_SELECTOR (data)) | |
2e471eb5 GM |
1334 | }; |
1335 | ||
31d929e5 | 1336 | |
2e471eb5 GM |
1337 | /* Structure describing a block of memory which is sub-allocated to |
1338 | obtain string data memory for strings. Blocks for small strings | |
1339 | are of fixed size SBLOCK_SIZE. Blocks for large strings are made | |
1340 | as large as needed. */ | |
1341 | ||
1342 | struct sblock | |
7146af97 | 1343 | { |
2e471eb5 GM |
1344 | /* Next in list. */ |
1345 | struct sblock *next; | |
7146af97 | 1346 | |
2e471eb5 GM |
1347 | /* Pointer to the next free sdata block. This points past the end |
1348 | of the sblock if there isn't any space left in this block. */ | |
1349 | struct sdata *next_free; | |
1350 | ||
1351 | /* Start of data. */ | |
1352 | struct sdata first_data; | |
1353 | }; | |
1354 | ||
1355 | /* Number of Lisp strings in a string_block structure. The 1020 is | |
1356 | 1024 minus malloc overhead. */ | |
1357 | ||
19bcad1f | 1358 | #define STRING_BLOCK_SIZE \ |
2e471eb5 GM |
1359 | ((1020 - sizeof (struct string_block *)) / sizeof (struct Lisp_String)) |
1360 | ||
1361 | /* Structure describing a block from which Lisp_String structures | |
1362 | are allocated. */ | |
1363 | ||
1364 | struct string_block | |
7146af97 | 1365 | { |
6b61353c | 1366 | /* Place `strings' first, to preserve alignment. */ |
19bcad1f | 1367 | struct Lisp_String strings[STRING_BLOCK_SIZE]; |
6b61353c | 1368 | struct string_block *next; |
2e471eb5 | 1369 | }; |
7146af97 | 1370 | |
2e471eb5 GM |
1371 | /* Head and tail of the list of sblock structures holding Lisp string |
1372 | data. We always allocate from current_sblock. The NEXT pointers | |
1373 | in the sblock structures go from oldest_sblock to current_sblock. */ | |
3c06d205 | 1374 | |
2e471eb5 | 1375 | static struct sblock *oldest_sblock, *current_sblock; |
7146af97 | 1376 | |
2e471eb5 | 1377 | /* List of sblocks for large strings. */ |
7146af97 | 1378 | |
2e471eb5 | 1379 | static struct sblock *large_sblocks; |
7146af97 | 1380 | |
5a25e253 | 1381 | /* List of string_block structures. */ |
7146af97 | 1382 | |
2e471eb5 | 1383 | static struct string_block *string_blocks; |
7146af97 | 1384 | |
2e471eb5 | 1385 | /* Free-list of Lisp_Strings. */ |
7146af97 | 1386 | |
2e471eb5 | 1387 | static struct Lisp_String *string_free_list; |
7146af97 | 1388 | |
2e471eb5 | 1389 | /* Number of live and free Lisp_Strings. */ |
c8099634 | 1390 | |
c0c5c8ae | 1391 | static EMACS_INT total_strings, total_free_strings; |
7146af97 | 1392 | |
2e471eb5 GM |
1393 | /* Number of bytes used by live strings. */ |
1394 | ||
3ab6e069 | 1395 | static EMACS_INT total_string_bytes; |
2e471eb5 GM |
1396 | |
1397 | /* Given a pointer to a Lisp_String S which is on the free-list | |
1398 | string_free_list, return a pointer to its successor in the | |
1399 | free-list. */ | |
1400 | ||
1401 | #define NEXT_FREE_LISP_STRING(S) (*(struct Lisp_String **) (S)) | |
1402 | ||
1403 | /* Return a pointer to the sdata structure belonging to Lisp string S. | |
1404 | S must be live, i.e. S->data must not be null. S->data is actually | |
1405 | a pointer to the `u.data' member of its sdata structure; the | |
1406 | structure starts at a constant offset in front of that. */ | |
177c0ea7 | 1407 | |
36372bf9 | 1408 | #define SDATA_OF_STRING(S) ((struct sdata *) ((S)->data - SDATA_DATA_OFFSET)) |
31d929e5 | 1409 | |
212f33f1 KS |
1410 | |
1411 | #ifdef GC_CHECK_STRING_OVERRUN | |
bdbed949 KS |
1412 | |
1413 | /* We check for overrun in string data blocks by appending a small | |
1414 | "cookie" after each allocated string data block, and check for the | |
8349069c | 1415 | presence of this cookie during GC. */ |
bdbed949 KS |
1416 | |
1417 | #define GC_STRING_OVERRUN_COOKIE_SIZE 4 | |
bfd1c781 PE |
1418 | static char const string_overrun_cookie[GC_STRING_OVERRUN_COOKIE_SIZE] = |
1419 | { '\xde', '\xad', '\xbe', '\xef' }; | |
bdbed949 | 1420 | |
212f33f1 | 1421 | #else |
bdbed949 | 1422 | #define GC_STRING_OVERRUN_COOKIE_SIZE 0 |
212f33f1 KS |
1423 | #endif |
1424 | ||
2e471eb5 GM |
1425 | /* Value is the size of an sdata structure large enough to hold NBYTES |
1426 | bytes of string data. The value returned includes a terminating | |
1427 | NUL byte, the size of the sdata structure, and padding. */ | |
1428 | ||
31d929e5 GM |
1429 | #ifdef GC_CHECK_STRING_BYTES |
1430 | ||
2e471eb5 | 1431 | #define SDATA_SIZE(NBYTES) \ |
36372bf9 | 1432 | ((SDATA_DATA_OFFSET \ |
2e471eb5 | 1433 | + (NBYTES) + 1 \ |
d311d28c PE |
1434 | + sizeof (ptrdiff_t) - 1) \ |
1435 | & ~(sizeof (ptrdiff_t) - 1)) | |
2e471eb5 | 1436 | |
31d929e5 GM |
1437 | #else /* not GC_CHECK_STRING_BYTES */ |
1438 | ||
f2d3008d PE |
1439 | /* The 'max' reserves space for the nbytes union member even when NBYTES + 1 is |
1440 | less than the size of that member. The 'max' is not needed when | |
d311d28c | 1441 | SDATA_DATA_OFFSET is a multiple of sizeof (ptrdiff_t), because then the |
f2d3008d PE |
1442 | alignment code reserves enough space. */ |
1443 | ||
1444 | #define SDATA_SIZE(NBYTES) \ | |
1445 | ((SDATA_DATA_OFFSET \ | |
d311d28c | 1446 | + (SDATA_DATA_OFFSET % sizeof (ptrdiff_t) == 0 \ |
f2d3008d | 1447 | ? NBYTES \ |
d311d28c | 1448 | : max (NBYTES, sizeof (ptrdiff_t) - 1)) \ |
f2d3008d | 1449 | + 1 \ |
d311d28c PE |
1450 | + sizeof (ptrdiff_t) - 1) \ |
1451 | & ~(sizeof (ptrdiff_t) - 1)) | |
31d929e5 GM |
1452 | |
1453 | #endif /* not GC_CHECK_STRING_BYTES */ | |
2e471eb5 | 1454 | |
bdbed949 KS |
1455 | /* Extra bytes to allocate for each string. */ |
1456 | ||
1457 | #define GC_STRING_EXTRA (GC_STRING_OVERRUN_COOKIE_SIZE) | |
1458 | ||
c9d624c6 PE |
1459 | /* Exact bound on the number of bytes in a string, not counting the |
1460 | terminating null. A string cannot contain more bytes than | |
1461 | STRING_BYTES_BOUND, nor can it be so long that the size_t | |
1462 | arithmetic in allocate_string_data would overflow while it is | |
1463 | calculating a value to be passed to malloc. */ | |
03a660a6 PE |
1464 | static ptrdiff_t const STRING_BYTES_MAX = |
1465 | min (STRING_BYTES_BOUND, | |
1466 | ((SIZE_MAX - XMALLOC_OVERRUN_CHECK_OVERHEAD | |
1467 | - GC_STRING_EXTRA | |
1468 | - offsetof (struct sblock, first_data) | |
1469 | - SDATA_DATA_OFFSET) | |
1470 | & ~(sizeof (EMACS_INT) - 1))); | |
c9d624c6 | 1471 | |
2e471eb5 | 1472 | /* Initialize string allocation. Called from init_alloc_once. */ |
d457598b | 1473 | |
d3d47262 | 1474 | static void |
971de7fb | 1475 | init_strings (void) |
7146af97 | 1476 | { |
4d774b0f JB |
1477 | empty_unibyte_string = make_pure_string ("", 0, 0, 0); |
1478 | empty_multibyte_string = make_pure_string ("", 0, 0, 1); | |
7146af97 JB |
1479 | } |
1480 | ||
2e471eb5 | 1481 | |
361b097f GM |
1482 | #ifdef GC_CHECK_STRING_BYTES |
1483 | ||
361b097f GM |
1484 | static int check_string_bytes_count; |
1485 | ||
e499d0ee DA |
1486 | /* Like STRING_BYTES, but with debugging check. Can be |
1487 | called during GC, so pay attention to the mark bit. */ | |
676a7251 | 1488 | |
d311d28c | 1489 | ptrdiff_t |
14162469 | 1490 | string_bytes (struct Lisp_String *s) |
676a7251 | 1491 | { |
d311d28c | 1492 | ptrdiff_t nbytes = |
14162469 EZ |
1493 | (s->size_byte < 0 ? s->size & ~ARRAY_MARK_FLAG : s->size_byte); |
1494 | ||
676a7251 GM |
1495 | if (!PURE_POINTER_P (s) |
1496 | && s->data | |
1497 | && nbytes != SDATA_NBYTES (SDATA_OF_STRING (s))) | |
1088b922 | 1498 | emacs_abort (); |
676a7251 GM |
1499 | return nbytes; |
1500 | } | |
177c0ea7 | 1501 | |
2c5bd608 | 1502 | /* Check validity of Lisp strings' string_bytes member in B. */ |
676a7251 | 1503 | |
d3d47262 | 1504 | static void |
d0f4e1f5 | 1505 | check_sblock (struct sblock *b) |
361b097f | 1506 | { |
676a7251 | 1507 | struct sdata *from, *end, *from_end; |
177c0ea7 | 1508 | |
676a7251 | 1509 | end = b->next_free; |
177c0ea7 | 1510 | |
676a7251 | 1511 | for (from = &b->first_data; from < end; from = from_end) |
361b097f | 1512 | { |
676a7251 GM |
1513 | /* Compute the next FROM here because copying below may |
1514 | overwrite data we need to compute it. */ | |
d311d28c | 1515 | ptrdiff_t nbytes; |
177c0ea7 | 1516 | |
676a7251 | 1517 | /* Check that the string size recorded in the string is the |
ee28be33 | 1518 | same as the one recorded in the sdata structure. */ |
e499d0ee DA |
1519 | nbytes = SDATA_SIZE (from->string ? string_bytes (from->string) |
1520 | : SDATA_NBYTES (from)); | |
212f33f1 | 1521 | from_end = (struct sdata *) ((char *) from + nbytes + GC_STRING_EXTRA); |
676a7251 GM |
1522 | } |
1523 | } | |
361b097f | 1524 | |
676a7251 GM |
1525 | |
1526 | /* Check validity of Lisp strings' string_bytes member. ALL_P | |
fce31d69 | 1527 | means check all strings, otherwise check only most |
676a7251 GM |
1528 | recently allocated strings. Used for hunting a bug. */ |
1529 | ||
d3d47262 | 1530 | static void |
fce31d69 | 1531 | check_string_bytes (bool all_p) |
676a7251 GM |
1532 | { |
1533 | if (all_p) | |
1534 | { | |
1535 | struct sblock *b; | |
1536 | ||
1537 | for (b = large_sblocks; b; b = b->next) | |
1538 | { | |
1539 | struct Lisp_String *s = b->first_data.string; | |
1540 | if (s) | |
e499d0ee | 1541 | string_bytes (s); |
361b097f | 1542 | } |
177c0ea7 | 1543 | |
676a7251 GM |
1544 | for (b = oldest_sblock; b; b = b->next) |
1545 | check_sblock (b); | |
361b097f | 1546 | } |
296094c3 | 1547 | else if (current_sblock) |
676a7251 | 1548 | check_sblock (current_sblock); |
361b097f GM |
1549 | } |
1550 | ||
e499d0ee DA |
1551 | #else /* not GC_CHECK_STRING_BYTES */ |
1552 | ||
1553 | #define check_string_bytes(all) ((void) 0) | |
1554 | ||
361b097f GM |
1555 | #endif /* GC_CHECK_STRING_BYTES */ |
1556 | ||
212f33f1 KS |
1557 | #ifdef GC_CHECK_STRING_FREE_LIST |
1558 | ||
bdbed949 KS |
1559 | /* Walk through the string free list looking for bogus next pointers. |
1560 | This may catch buffer overrun from a previous string. */ | |
1561 | ||
212f33f1 | 1562 | static void |
d0f4e1f5 | 1563 | check_string_free_list (void) |
212f33f1 KS |
1564 | { |
1565 | struct Lisp_String *s; | |
1566 | ||
1567 | /* Pop a Lisp_String off the free-list. */ | |
1568 | s = string_free_list; | |
1569 | while (s != NULL) | |
1570 | { | |
d01a7826 | 1571 | if ((uintptr_t) s < 1024) |
1088b922 | 1572 | emacs_abort (); |
212f33f1 KS |
1573 | s = NEXT_FREE_LISP_STRING (s); |
1574 | } | |
1575 | } | |
1576 | #else | |
1577 | #define check_string_free_list() | |
1578 | #endif | |
361b097f | 1579 | |
2e471eb5 GM |
1580 | /* Return a new Lisp_String. */ |
1581 | ||
1582 | static struct Lisp_String * | |
971de7fb | 1583 | allocate_string (void) |
7146af97 | 1584 | { |
2e471eb5 | 1585 | struct Lisp_String *s; |
7146af97 | 1586 | |
dafc79fa | 1587 | MALLOC_BLOCK_INPUT; |
cfb2f32e | 1588 | |
2e471eb5 GM |
1589 | /* If the free-list is empty, allocate a new string_block, and |
1590 | add all the Lisp_Strings in it to the free-list. */ | |
1591 | if (string_free_list == NULL) | |
7146af97 | 1592 | { |
38182d90 | 1593 | struct string_block *b = lisp_malloc (sizeof *b, MEM_TYPE_STRING); |
2e471eb5 GM |
1594 | int i; |
1595 | ||
2e471eb5 GM |
1596 | b->next = string_blocks; |
1597 | string_blocks = b; | |
2e471eb5 | 1598 | |
19bcad1f | 1599 | for (i = STRING_BLOCK_SIZE - 1; i >= 0; --i) |
7146af97 | 1600 | { |
2e471eb5 | 1601 | s = b->strings + i; |
3fe6dd74 DA |
1602 | /* Every string on a free list should have NULL data pointer. */ |
1603 | s->data = NULL; | |
2e471eb5 GM |
1604 | NEXT_FREE_LISP_STRING (s) = string_free_list; |
1605 | string_free_list = s; | |
7146af97 | 1606 | } |
2e471eb5 | 1607 | |
19bcad1f | 1608 | total_free_strings += STRING_BLOCK_SIZE; |
7146af97 | 1609 | } |
c0f51373 | 1610 | |
bdbed949 | 1611 | check_string_free_list (); |
212f33f1 | 1612 | |
2e471eb5 GM |
1613 | /* Pop a Lisp_String off the free-list. */ |
1614 | s = string_free_list; | |
1615 | string_free_list = NEXT_FREE_LISP_STRING (s); | |
c0f51373 | 1616 | |
dafc79fa | 1617 | MALLOC_UNBLOCK_INPUT; |
e2984df0 | 1618 | |
2e471eb5 GM |
1619 | --total_free_strings; |
1620 | ++total_strings; | |
1621 | ++strings_consed; | |
1622 | consing_since_gc += sizeof *s; | |
c0f51373 | 1623 | |
361b097f | 1624 | #ifdef GC_CHECK_STRING_BYTES |
e39a993c | 1625 | if (!noninteractive) |
361b097f | 1626 | { |
676a7251 GM |
1627 | if (++check_string_bytes_count == 200) |
1628 | { | |
1629 | check_string_bytes_count = 0; | |
1630 | check_string_bytes (1); | |
1631 | } | |
1632 | else | |
1633 | check_string_bytes (0); | |
361b097f | 1634 | } |
676a7251 | 1635 | #endif /* GC_CHECK_STRING_BYTES */ |
361b097f | 1636 | |
2e471eb5 | 1637 | return s; |
c0f51373 | 1638 | } |
7146af97 | 1639 | |
7146af97 | 1640 | |
2e471eb5 GM |
1641 | /* Set up Lisp_String S for holding NCHARS characters, NBYTES bytes, |
1642 | plus a NUL byte at the end. Allocate an sdata structure for S, and | |
1643 | set S->data to its `u.data' member. Store a NUL byte at the end of | |
1644 | S->data. Set S->size to NCHARS and S->size_byte to NBYTES. Free | |
1645 | S->data if it was initially non-null. */ | |
7146af97 | 1646 | |
2e471eb5 | 1647 | void |
413d18e7 EZ |
1648 | allocate_string_data (struct Lisp_String *s, |
1649 | EMACS_INT nchars, EMACS_INT nbytes) | |
7146af97 | 1650 | { |
b7ffe040 | 1651 | struct sdata *data, *old_data; |
2e471eb5 | 1652 | struct sblock *b; |
b7ffe040 | 1653 | ptrdiff_t needed, old_nbytes; |
7146af97 | 1654 | |
c9d624c6 PE |
1655 | if (STRING_BYTES_MAX < nbytes) |
1656 | string_overflow (); | |
1657 | ||
2e471eb5 GM |
1658 | /* Determine the number of bytes needed to store NBYTES bytes |
1659 | of string data. */ | |
1660 | needed = SDATA_SIZE (nbytes); | |
b7ffe040 DA |
1661 | if (s->data) |
1662 | { | |
1663 | old_data = SDATA_OF_STRING (s); | |
e499d0ee | 1664 | old_nbytes = STRING_BYTES (s); |
b7ffe040 DA |
1665 | } |
1666 | else | |
1667 | old_data = NULL; | |
e2984df0 | 1668 | |
dafc79fa | 1669 | MALLOC_BLOCK_INPUT; |
7146af97 | 1670 | |
2e471eb5 GM |
1671 | if (nbytes > LARGE_STRING_BYTES) |
1672 | { | |
36372bf9 | 1673 | size_t size = offsetof (struct sblock, first_data) + needed; |
2e471eb5 GM |
1674 | |
1675 | #ifdef DOUG_LEA_MALLOC | |
f8608968 GM |
1676 | /* Prevent mmap'ing the chunk. Lisp data may not be mmap'ed |
1677 | because mapped region contents are not preserved in | |
d36b182f DL |
1678 | a dumped Emacs. |
1679 | ||
1680 | In case you think of allowing it in a dumped Emacs at the | |
1681 | cost of not being able to re-dump, there's another reason: | |
1682 | mmap'ed data typically have an address towards the top of the | |
1683 | address space, which won't fit into an EMACS_INT (at least on | |
1684 | 32-bit systems with the current tagging scheme). --fx */ | |
2e471eb5 GM |
1685 | mallopt (M_MMAP_MAX, 0); |
1686 | #endif | |
1687 | ||
38182d90 | 1688 | b = lisp_malloc (size + GC_STRING_EXTRA, MEM_TYPE_NON_LISP); |
177c0ea7 | 1689 | |
2e471eb5 GM |
1690 | #ifdef DOUG_LEA_MALLOC |
1691 | /* Back to a reasonable maximum of mmap'ed areas. */ | |
1692 | mallopt (M_MMAP_MAX, MMAP_MAX_AREAS); | |
1693 | #endif | |
177c0ea7 | 1694 | |
2e471eb5 GM |
1695 | b->next_free = &b->first_data; |
1696 | b->first_data.string = NULL; | |
1697 | b->next = large_sblocks; | |
1698 | large_sblocks = b; | |
1699 | } | |
1700 | else if (current_sblock == NULL | |
1701 | || (((char *) current_sblock + SBLOCK_SIZE | |
1702 | - (char *) current_sblock->next_free) | |
212f33f1 | 1703 | < (needed + GC_STRING_EXTRA))) |
2e471eb5 GM |
1704 | { |
1705 | /* Not enough room in the current sblock. */ | |
38182d90 | 1706 | b = lisp_malloc (SBLOCK_SIZE, MEM_TYPE_NON_LISP); |
2e471eb5 GM |
1707 | b->next_free = &b->first_data; |
1708 | b->first_data.string = NULL; | |
1709 | b->next = NULL; | |
1710 | ||
1711 | if (current_sblock) | |
1712 | current_sblock->next = b; | |
1713 | else | |
1714 | oldest_sblock = b; | |
1715 | current_sblock = b; | |
1716 | } | |
1717 | else | |
1718 | b = current_sblock; | |
5c5fecb3 | 1719 | |
2e471eb5 | 1720 | data = b->next_free; |
a0b08700 CY |
1721 | b->next_free = (struct sdata *) ((char *) data + needed + GC_STRING_EXTRA); |
1722 | ||
dafc79fa | 1723 | MALLOC_UNBLOCK_INPUT; |
e2984df0 | 1724 | |
2e471eb5 | 1725 | data->string = s; |
31d929e5 GM |
1726 | s->data = SDATA_DATA (data); |
1727 | #ifdef GC_CHECK_STRING_BYTES | |
1728 | SDATA_NBYTES (data) = nbytes; | |
1729 | #endif | |
2e471eb5 GM |
1730 | s->size = nchars; |
1731 | s->size_byte = nbytes; | |
1732 | s->data[nbytes] = '\0'; | |
212f33f1 | 1733 | #ifdef GC_CHECK_STRING_OVERRUN |
000098c1 PE |
1734 | memcpy ((char *) data + needed, string_overrun_cookie, |
1735 | GC_STRING_OVERRUN_COOKIE_SIZE); | |
212f33f1 | 1736 | #endif |
b7ffe040 DA |
1737 | |
1738 | /* Note that Faset may call to this function when S has already data | |
1739 | assigned. In this case, mark data as free by setting it's string | |
1740 | back-pointer to null, and record the size of the data in it. */ | |
1741 | if (old_data) | |
1742 | { | |
1743 | SDATA_NBYTES (old_data) = old_nbytes; | |
1744 | old_data->string = NULL; | |
1745 | } | |
1746 | ||
2e471eb5 GM |
1747 | consing_since_gc += needed; |
1748 | } | |
1749 | ||
1750 | ||
1751 | /* Sweep and compact strings. */ | |
1752 | ||
1753 | static void | |
971de7fb | 1754 | sweep_strings (void) |
2e471eb5 GM |
1755 | { |
1756 | struct string_block *b, *next; | |
1757 | struct string_block *live_blocks = NULL; | |
177c0ea7 | 1758 | |
2e471eb5 GM |
1759 | string_free_list = NULL; |
1760 | total_strings = total_free_strings = 0; | |
3ab6e069 | 1761 | total_string_bytes = 0; |
2e471eb5 GM |
1762 | |
1763 | /* Scan strings_blocks, free Lisp_Strings that aren't marked. */ | |
1764 | for (b = string_blocks; b; b = next) | |
1765 | { | |
1766 | int i, nfree = 0; | |
1767 | struct Lisp_String *free_list_before = string_free_list; | |
1768 | ||
1769 | next = b->next; | |
1770 | ||
19bcad1f | 1771 | for (i = 0; i < STRING_BLOCK_SIZE; ++i) |
2e471eb5 GM |
1772 | { |
1773 | struct Lisp_String *s = b->strings + i; | |
1774 | ||
1775 | if (s->data) | |
1776 | { | |
1777 | /* String was not on free-list before. */ | |
1778 | if (STRING_MARKED_P (s)) | |
1779 | { | |
1780 | /* String is live; unmark it and its intervals. */ | |
1781 | UNMARK_STRING (s); | |
177c0ea7 | 1782 | |
8707c1e5 DA |
1783 | /* Do not use string_(set|get)_intervals here. */ |
1784 | s->intervals = balance_intervals (s->intervals); | |
2e471eb5 GM |
1785 | |
1786 | ++total_strings; | |
3ab6e069 | 1787 | total_string_bytes += STRING_BYTES (s); |
2e471eb5 GM |
1788 | } |
1789 | else | |
1790 | { | |
1791 | /* String is dead. Put it on the free-list. */ | |
1792 | struct sdata *data = SDATA_OF_STRING (s); | |
1793 | ||
1794 | /* Save the size of S in its sdata so that we know | |
1795 | how large that is. Reset the sdata's string | |
1796 | back-pointer so that we know it's free. */ | |
31d929e5 | 1797 | #ifdef GC_CHECK_STRING_BYTES |
e499d0ee | 1798 | if (string_bytes (s) != SDATA_NBYTES (data)) |
1088b922 | 1799 | emacs_abort (); |
31d929e5 | 1800 | #else |
e499d0ee | 1801 | data->u.nbytes = STRING_BYTES (s); |
31d929e5 | 1802 | #endif |
2e471eb5 GM |
1803 | data->string = NULL; |
1804 | ||
1805 | /* Reset the strings's `data' member so that we | |
1806 | know it's free. */ | |
1807 | s->data = NULL; | |
1808 | ||
1809 | /* Put the string on the free-list. */ | |
1810 | NEXT_FREE_LISP_STRING (s) = string_free_list; | |
1811 | string_free_list = s; | |
1812 | ++nfree; | |
1813 | } | |
1814 | } | |
1815 | else | |
1816 | { | |
1817 | /* S was on the free-list before. Put it there again. */ | |
1818 | NEXT_FREE_LISP_STRING (s) = string_free_list; | |
1819 | string_free_list = s; | |
1820 | ++nfree; | |
1821 | } | |
1822 | } | |
1823 | ||
34400008 | 1824 | /* Free blocks that contain free Lisp_Strings only, except |
2e471eb5 | 1825 | the first two of them. */ |
19bcad1f SM |
1826 | if (nfree == STRING_BLOCK_SIZE |
1827 | && total_free_strings > STRING_BLOCK_SIZE) | |
2e471eb5 GM |
1828 | { |
1829 | lisp_free (b); | |
2e471eb5 GM |
1830 | string_free_list = free_list_before; |
1831 | } | |
1832 | else | |
1833 | { | |
1834 | total_free_strings += nfree; | |
1835 | b->next = live_blocks; | |
1836 | live_blocks = b; | |
1837 | } | |
1838 | } | |
1839 | ||
bdbed949 | 1840 | check_string_free_list (); |
212f33f1 | 1841 | |
2e471eb5 GM |
1842 | string_blocks = live_blocks; |
1843 | free_large_strings (); | |
1844 | compact_small_strings (); | |
212f33f1 | 1845 | |
bdbed949 | 1846 | check_string_free_list (); |
2e471eb5 GM |
1847 | } |
1848 | ||
1849 | ||
1850 | /* Free dead large strings. */ | |
1851 | ||
1852 | static void | |
971de7fb | 1853 | free_large_strings (void) |
2e471eb5 GM |
1854 | { |
1855 | struct sblock *b, *next; | |
1856 | struct sblock *live_blocks = NULL; | |
177c0ea7 | 1857 | |
2e471eb5 GM |
1858 | for (b = large_sblocks; b; b = next) |
1859 | { | |
1860 | next = b->next; | |
1861 | ||
1862 | if (b->first_data.string == NULL) | |
1863 | lisp_free (b); | |
1864 | else | |
1865 | { | |
1866 | b->next = live_blocks; | |
1867 | live_blocks = b; | |
1868 | } | |
1869 | } | |
1870 | ||
1871 | large_sblocks = live_blocks; | |
1872 | } | |
1873 | ||
1874 | ||
1875 | /* Compact data of small strings. Free sblocks that don't contain | |
1876 | data of live strings after compaction. */ | |
1877 | ||
1878 | static void | |
971de7fb | 1879 | compact_small_strings (void) |
2e471eb5 GM |
1880 | { |
1881 | struct sblock *b, *tb, *next; | |
1882 | struct sdata *from, *to, *end, *tb_end; | |
1883 | struct sdata *to_end, *from_end; | |
1884 | ||
1885 | /* TB is the sblock we copy to, TO is the sdata within TB we copy | |
1886 | to, and TB_END is the end of TB. */ | |
1887 | tb = oldest_sblock; | |
1888 | tb_end = (struct sdata *) ((char *) tb + SBLOCK_SIZE); | |
1889 | to = &tb->first_data; | |
1890 | ||
1891 | /* Step through the blocks from the oldest to the youngest. We | |
1892 | expect that old blocks will stabilize over time, so that less | |
1893 | copying will happen this way. */ | |
1894 | for (b = oldest_sblock; b; b = b->next) | |
1895 | { | |
1896 | end = b->next_free; | |
a54e2c05 | 1897 | eassert ((char *) end <= (char *) b + SBLOCK_SIZE); |
177c0ea7 | 1898 | |
2e471eb5 GM |
1899 | for (from = &b->first_data; from < end; from = from_end) |
1900 | { | |
1901 | /* Compute the next FROM here because copying below may | |
1902 | overwrite data we need to compute it. */ | |
d311d28c | 1903 | ptrdiff_t nbytes; |
e499d0ee | 1904 | struct Lisp_String *s = from->string; |
2e471eb5 | 1905 | |
31d929e5 GM |
1906 | #ifdef GC_CHECK_STRING_BYTES |
1907 | /* Check that the string size recorded in the string is the | |
1908 | same as the one recorded in the sdata structure. */ | |
e499d0ee | 1909 | if (s && string_bytes (s) != SDATA_NBYTES (from)) |
1088b922 | 1910 | emacs_abort (); |
31d929e5 | 1911 | #endif /* GC_CHECK_STRING_BYTES */ |
177c0ea7 | 1912 | |
e499d0ee DA |
1913 | nbytes = s ? STRING_BYTES (s) : SDATA_NBYTES (from); |
1914 | eassert (nbytes <= LARGE_STRING_BYTES); | |
212f33f1 | 1915 | |
2e471eb5 | 1916 | nbytes = SDATA_SIZE (nbytes); |
212f33f1 KS |
1917 | from_end = (struct sdata *) ((char *) from + nbytes + GC_STRING_EXTRA); |
1918 | ||
1919 | #ifdef GC_CHECK_STRING_OVERRUN | |
72af86bd AS |
1920 | if (memcmp (string_overrun_cookie, |
1921 | (char *) from_end - GC_STRING_OVERRUN_COOKIE_SIZE, | |
1922 | GC_STRING_OVERRUN_COOKIE_SIZE)) | |
1088b922 | 1923 | emacs_abort (); |
212f33f1 | 1924 | #endif |
177c0ea7 | 1925 | |
e499d0ee DA |
1926 | /* Non-NULL S means it's alive. Copy its data. */ |
1927 | if (s) | |
2e471eb5 GM |
1928 | { |
1929 | /* If TB is full, proceed with the next sblock. */ | |
212f33f1 | 1930 | to_end = (struct sdata *) ((char *) to + nbytes + GC_STRING_EXTRA); |
2e471eb5 GM |
1931 | if (to_end > tb_end) |
1932 | { | |
1933 | tb->next_free = to; | |
1934 | tb = tb->next; | |
1935 | tb_end = (struct sdata *) ((char *) tb + SBLOCK_SIZE); | |
1936 | to = &tb->first_data; | |
212f33f1 | 1937 | to_end = (struct sdata *) ((char *) to + nbytes + GC_STRING_EXTRA); |
2e471eb5 | 1938 | } |
177c0ea7 | 1939 | |
2e471eb5 GM |
1940 | /* Copy, and update the string's `data' pointer. */ |
1941 | if (from != to) | |
1942 | { | |
a54e2c05 | 1943 | eassert (tb != b || to < from); |
72af86bd | 1944 | memmove (to, from, nbytes + GC_STRING_EXTRA); |
31d929e5 | 1945 | to->string->data = SDATA_DATA (to); |
2e471eb5 GM |
1946 | } |
1947 | ||
1948 | /* Advance past the sdata we copied to. */ | |
1949 | to = to_end; | |
1950 | } | |
1951 | } | |
1952 | } | |
1953 | ||
1954 | /* The rest of the sblocks following TB don't contain live data, so | |
1955 | we can free them. */ | |
1956 | for (b = tb->next; b; b = next) | |
1957 | { | |
1958 | next = b->next; | |
1959 | lisp_free (b); | |
1960 | } | |
1961 | ||
1962 | tb->next_free = to; | |
1963 | tb->next = NULL; | |
1964 | current_sblock = tb; | |
1965 | } | |
1966 | ||
cb93f9be PE |
1967 | void |
1968 | string_overflow (void) | |
1969 | { | |
1970 | error ("Maximum string size exceeded"); | |
1971 | } | |
2e471eb5 | 1972 | |
a7ca3326 | 1973 | DEFUN ("make-string", Fmake_string, Smake_string, 2, 2, 0, |
69623621 RS |
1974 | doc: /* Return a newly created string of length LENGTH, with INIT in each element. |
1975 | LENGTH must be an integer. | |
1976 | INIT must be an integer that represents a character. */) | |
5842a27b | 1977 | (Lisp_Object length, Lisp_Object init) |
2e471eb5 GM |
1978 | { |
1979 | register Lisp_Object val; | |
1980 | register unsigned char *p, *end; | |
14162469 EZ |
1981 | int c; |
1982 | EMACS_INT nbytes; | |
2e471eb5 | 1983 | |
b7826503 | 1984 | CHECK_NATNUM (length); |
2bccce07 | 1985 | CHECK_CHARACTER (init); |
2e471eb5 | 1986 | |
2bccce07 | 1987 | c = XFASTINT (init); |
830ff83b | 1988 | if (ASCII_CHAR_P (c)) |
2e471eb5 GM |
1989 | { |
1990 | nbytes = XINT (length); | |
1991 | val = make_uninit_string (nbytes); | |
d5db4077 KR |
1992 | p = SDATA (val); |
1993 | end = p + SCHARS (val); | |
2e471eb5 GM |
1994 | while (p != end) |
1995 | *p++ = c; | |
1996 | } | |
1997 | else | |
1998 | { | |
d942b71c | 1999 | unsigned char str[MAX_MULTIBYTE_LENGTH]; |
2e471eb5 | 2000 | int len = CHAR_STRING (c, str); |
14162469 | 2001 | EMACS_INT string_len = XINT (length); |
2e471eb5 | 2002 | |
d1f3d2af | 2003 | if (string_len > STRING_BYTES_MAX / len) |
cb93f9be | 2004 | string_overflow (); |
14162469 EZ |
2005 | nbytes = len * string_len; |
2006 | val = make_uninit_multibyte_string (string_len, nbytes); | |
d5db4077 | 2007 | p = SDATA (val); |
2e471eb5 GM |
2008 | end = p + nbytes; |
2009 | while (p != end) | |
2010 | { | |
72af86bd | 2011 | memcpy (p, str, len); |
2e471eb5 GM |
2012 | p += len; |
2013 | } | |
2014 | } | |
177c0ea7 | 2015 | |
2e471eb5 GM |
2016 | *p = 0; |
2017 | return val; | |
2018 | } | |
2019 | ||
2020 | ||
a7ca3326 | 2021 | DEFUN ("make-bool-vector", Fmake_bool_vector, Smake_bool_vector, 2, 2, 0, |
909e3b33 | 2022 | doc: /* Return a new bool-vector of length LENGTH, using INIT for each element. |
7ee72033 | 2023 | LENGTH must be a number. INIT matters only in whether it is t or nil. */) |
5842a27b | 2024 | (Lisp_Object length, Lisp_Object init) |
2e471eb5 GM |
2025 | { |
2026 | register Lisp_Object val; | |
2027 | struct Lisp_Bool_Vector *p; | |
d311d28c PE |
2028 | ptrdiff_t length_in_chars; |
2029 | EMACS_INT length_in_elts; | |
14162469 | 2030 | int bits_per_value; |
d06714cb PE |
2031 | int extra_bool_elts = ((bool_header_size - header_size + word_size - 1) |
2032 | / word_size); | |
2e471eb5 | 2033 | |
b7826503 | 2034 | CHECK_NATNUM (length); |
2e471eb5 | 2035 | |
a097329f | 2036 | bits_per_value = sizeof (EMACS_INT) * BOOL_VECTOR_BITS_PER_CHAR; |
2e471eb5 GM |
2037 | |
2038 | length_in_elts = (XFASTINT (length) + bits_per_value - 1) / bits_per_value; | |
2e471eb5 | 2039 | |
d06714cb | 2040 | val = Fmake_vector (make_number (length_in_elts + extra_bool_elts), Qnil); |
177c0ea7 | 2041 | |
eab3844f PE |
2042 | /* No Lisp_Object to trace in there. */ |
2043 | XSETPVECTYPESIZE (XVECTOR (val), PVEC_BOOL_VECTOR, 0); | |
d2029e5b SM |
2044 | |
2045 | p = XBOOL_VECTOR (val); | |
2e471eb5 | 2046 | p->size = XFASTINT (length); |
177c0ea7 | 2047 | |
d311d28c PE |
2048 | length_in_chars = ((XFASTINT (length) + BOOL_VECTOR_BITS_PER_CHAR - 1) |
2049 | / BOOL_VECTOR_BITS_PER_CHAR); | |
c0c1ee9f PE |
2050 | if (length_in_chars) |
2051 | { | |
2052 | memset (p->data, ! NILP (init) ? -1 : 0, length_in_chars); | |
177c0ea7 | 2053 | |
c0c1ee9f PE |
2054 | /* Clear any extraneous bits in the last byte. */ |
2055 | p->data[length_in_chars - 1] | |
83713154 | 2056 | &= (1 << ((XFASTINT (length) - 1) % BOOL_VECTOR_BITS_PER_CHAR + 1)) - 1; |
c0c1ee9f | 2057 | } |
2e471eb5 GM |
2058 | |
2059 | return val; | |
2060 | } | |
2061 | ||
2062 | ||
2063 | /* Make a string from NBYTES bytes at CONTENTS, and compute the number | |
2064 | of characters from the contents. This string may be unibyte or | |
2065 | multibyte, depending on the contents. */ | |
2066 | ||
2067 | Lisp_Object | |
d311d28c | 2068 | make_string (const char *contents, ptrdiff_t nbytes) |
2e471eb5 GM |
2069 | { |
2070 | register Lisp_Object val; | |
d311d28c | 2071 | ptrdiff_t nchars, multibyte_nbytes; |
9eac9d59 | 2072 | |
90256841 PE |
2073 | parse_str_as_multibyte ((const unsigned char *) contents, nbytes, |
2074 | &nchars, &multibyte_nbytes); | |
9eac9d59 KH |
2075 | if (nbytes == nchars || nbytes != multibyte_nbytes) |
2076 | /* CONTENTS contains no multibyte sequences or contains an invalid | |
2077 | multibyte sequence. We must make unibyte string. */ | |
495a6df3 KH |
2078 | val = make_unibyte_string (contents, nbytes); |
2079 | else | |
2080 | val = make_multibyte_string (contents, nchars, nbytes); | |
2e471eb5 GM |
2081 | return val; |
2082 | } | |
2083 | ||
2084 | ||
2085 | /* Make an unibyte string from LENGTH bytes at CONTENTS. */ | |
2086 | ||
2087 | Lisp_Object | |
d311d28c | 2088 | make_unibyte_string (const char *contents, ptrdiff_t length) |
2e471eb5 GM |
2089 | { |
2090 | register Lisp_Object val; | |
2091 | val = make_uninit_string (length); | |
72af86bd | 2092 | memcpy (SDATA (val), contents, length); |
2e471eb5 GM |
2093 | return val; |
2094 | } | |
2095 | ||
2096 | ||
2097 | /* Make a multibyte string from NCHARS characters occupying NBYTES | |
2098 | bytes at CONTENTS. */ | |
2099 | ||
2100 | Lisp_Object | |
14162469 | 2101 | make_multibyte_string (const char *contents, |
d311d28c | 2102 | ptrdiff_t nchars, ptrdiff_t nbytes) |
2e471eb5 GM |
2103 | { |
2104 | register Lisp_Object val; | |
2105 | val = make_uninit_multibyte_string (nchars, nbytes); | |
72af86bd | 2106 | memcpy (SDATA (val), contents, nbytes); |
2e471eb5 GM |
2107 | return val; |
2108 | } | |
2109 | ||
2110 | ||
2111 | /* Make a string from NCHARS characters occupying NBYTES bytes at | |
2112 | CONTENTS. It is a multibyte string if NBYTES != NCHARS. */ | |
2113 | ||
2114 | Lisp_Object | |
14162469 | 2115 | make_string_from_bytes (const char *contents, |
d311d28c | 2116 | ptrdiff_t nchars, ptrdiff_t nbytes) |
2e471eb5 GM |
2117 | { |
2118 | register Lisp_Object val; | |
2119 | val = make_uninit_multibyte_string (nchars, nbytes); | |
72af86bd | 2120 | memcpy (SDATA (val), contents, nbytes); |
d5db4077 KR |
2121 | if (SBYTES (val) == SCHARS (val)) |
2122 | STRING_SET_UNIBYTE (val); | |
2e471eb5 GM |
2123 | return val; |
2124 | } | |
2125 | ||
2126 | ||
2127 | /* Make a string from NCHARS characters occupying NBYTES bytes at | |
2128 | CONTENTS. The argument MULTIBYTE controls whether to label the | |
229b28c4 KH |
2129 | string as multibyte. If NCHARS is negative, it counts the number of |
2130 | characters by itself. */ | |
2e471eb5 GM |
2131 | |
2132 | Lisp_Object | |
14162469 | 2133 | make_specified_string (const char *contents, |
fce31d69 | 2134 | ptrdiff_t nchars, ptrdiff_t nbytes, bool multibyte) |
2e471eb5 | 2135 | { |
fce31d69 | 2136 | Lisp_Object val; |
229b28c4 KH |
2137 | |
2138 | if (nchars < 0) | |
2139 | { | |
2140 | if (multibyte) | |
90256841 PE |
2141 | nchars = multibyte_chars_in_text ((const unsigned char *) contents, |
2142 | nbytes); | |
229b28c4 KH |
2143 | else |
2144 | nchars = nbytes; | |
2145 | } | |
2e471eb5 | 2146 | val = make_uninit_multibyte_string (nchars, nbytes); |
72af86bd | 2147 | memcpy (SDATA (val), contents, nbytes); |
2e471eb5 | 2148 | if (!multibyte) |
d5db4077 | 2149 | STRING_SET_UNIBYTE (val); |
2e471eb5 GM |
2150 | return val; |
2151 | } | |
2152 | ||
2153 | ||
2e471eb5 GM |
2154 | /* Return an unibyte Lisp_String set up to hold LENGTH characters |
2155 | occupying LENGTH bytes. */ | |
2156 | ||
2157 | Lisp_Object | |
413d18e7 | 2158 | make_uninit_string (EMACS_INT length) |
2e471eb5 GM |
2159 | { |
2160 | Lisp_Object val; | |
4d774b0f JB |
2161 | |
2162 | if (!length) | |
2163 | return empty_unibyte_string; | |
2e471eb5 | 2164 | val = make_uninit_multibyte_string (length, length); |
d5db4077 | 2165 | STRING_SET_UNIBYTE (val); |
2e471eb5 GM |
2166 | return val; |
2167 | } | |
2168 | ||
2169 | ||
2170 | /* Return a multibyte Lisp_String set up to hold NCHARS characters | |
2171 | which occupy NBYTES bytes. */ | |
2172 | ||
2173 | Lisp_Object | |
413d18e7 | 2174 | make_uninit_multibyte_string (EMACS_INT nchars, EMACS_INT nbytes) |
2e471eb5 GM |
2175 | { |
2176 | Lisp_Object string; | |
2177 | struct Lisp_String *s; | |
2178 | ||
2179 | if (nchars < 0) | |
1088b922 | 2180 | emacs_abort (); |
4d774b0f JB |
2181 | if (!nbytes) |
2182 | return empty_multibyte_string; | |
2e471eb5 GM |
2183 | |
2184 | s = allocate_string (); | |
77c7bcb1 | 2185 | s->intervals = NULL; |
2e471eb5 GM |
2186 | allocate_string_data (s, nchars, nbytes); |
2187 | XSETSTRING (string, s); | |
2188 | string_chars_consed += nbytes; | |
2189 | return string; | |
2190 | } | |
2191 | ||
a8290ec3 DA |
2192 | /* Print arguments to BUF according to a FORMAT, then return |
2193 | a Lisp_String initialized with the data from BUF. */ | |
2194 | ||
2195 | Lisp_Object | |
2196 | make_formatted_string (char *buf, const char *format, ...) | |
2197 | { | |
2198 | va_list ap; | |
26bccfae | 2199 | int length; |
a8290ec3 DA |
2200 | |
2201 | va_start (ap, format); | |
2202 | length = vsprintf (buf, format, ap); | |
2203 | va_end (ap); | |
2204 | return make_string (buf, length); | |
2205 | } | |
2e471eb5 GM |
2206 | |
2207 | \f | |
2208 | /*********************************************************************** | |
2209 | Float Allocation | |
2210 | ***********************************************************************/ | |
2211 | ||
2e471eb5 GM |
2212 | /* We store float cells inside of float_blocks, allocating a new |
2213 | float_block with malloc whenever necessary. Float cells reclaimed | |
2214 | by GC are put on a free list to be reallocated before allocating | |
ab6780cd | 2215 | any new float cells from the latest float_block. */ |
2e471eb5 | 2216 | |
6b61353c KH |
2217 | #define FLOAT_BLOCK_SIZE \ |
2218 | (((BLOCK_BYTES - sizeof (struct float_block *) \ | |
2219 | /* The compiler might add padding at the end. */ \ | |
2220 | - (sizeof (struct Lisp_Float) - sizeof (int))) * CHAR_BIT) \ | |
ab6780cd SM |
2221 | / (sizeof (struct Lisp_Float) * CHAR_BIT + 1)) |
2222 | ||
2223 | #define GETMARKBIT(block,n) \ | |
5e617bc2 JB |
2224 | (((block)->gcmarkbits[(n) / (sizeof (int) * CHAR_BIT)] \ |
2225 | >> ((n) % (sizeof (int) * CHAR_BIT))) \ | |
ab6780cd SM |
2226 | & 1) |
2227 | ||
2228 | #define SETMARKBIT(block,n) \ | |
5e617bc2 JB |
2229 | (block)->gcmarkbits[(n) / (sizeof (int) * CHAR_BIT)] \ |
2230 | |= 1 << ((n) % (sizeof (int) * CHAR_BIT)) | |
ab6780cd SM |
2231 | |
2232 | #define UNSETMARKBIT(block,n) \ | |
5e617bc2 JB |
2233 | (block)->gcmarkbits[(n) / (sizeof (int) * CHAR_BIT)] \ |
2234 | &= ~(1 << ((n) % (sizeof (int) * CHAR_BIT))) | |
ab6780cd SM |
2235 | |
2236 | #define FLOAT_BLOCK(fptr) \ | |
d01a7826 | 2237 | ((struct float_block *) (((uintptr_t) (fptr)) & ~(BLOCK_ALIGN - 1))) |
ab6780cd SM |
2238 | |
2239 | #define FLOAT_INDEX(fptr) \ | |
d01a7826 | 2240 | ((((uintptr_t) (fptr)) & (BLOCK_ALIGN - 1)) / sizeof (struct Lisp_Float)) |
2e471eb5 GM |
2241 | |
2242 | struct float_block | |
2243 | { | |
ab6780cd | 2244 | /* Place `floats' at the beginning, to ease up FLOAT_INDEX's job. */ |
2e471eb5 | 2245 | struct Lisp_Float floats[FLOAT_BLOCK_SIZE]; |
5e617bc2 | 2246 | int gcmarkbits[1 + FLOAT_BLOCK_SIZE / (sizeof (int) * CHAR_BIT)]; |
ab6780cd | 2247 | struct float_block *next; |
2e471eb5 GM |
2248 | }; |
2249 | ||
ab6780cd SM |
2250 | #define FLOAT_MARKED_P(fptr) \ |
2251 | GETMARKBIT (FLOAT_BLOCK (fptr), FLOAT_INDEX ((fptr))) | |
2252 | ||
2253 | #define FLOAT_MARK(fptr) \ | |
2254 | SETMARKBIT (FLOAT_BLOCK (fptr), FLOAT_INDEX ((fptr))) | |
2255 | ||
2256 | #define FLOAT_UNMARK(fptr) \ | |
2257 | UNSETMARKBIT (FLOAT_BLOCK (fptr), FLOAT_INDEX ((fptr))) | |
2258 | ||
34400008 GM |
2259 | /* Current float_block. */ |
2260 | ||
244ed907 | 2261 | static struct float_block *float_block; |
34400008 GM |
2262 | |
2263 | /* Index of first unused Lisp_Float in the current float_block. */ | |
2264 | ||
fff62aa9 | 2265 | static int float_block_index = FLOAT_BLOCK_SIZE; |
2e471eb5 | 2266 | |
34400008 GM |
2267 | /* Free-list of Lisp_Floats. */ |
2268 | ||
244ed907 | 2269 | static struct Lisp_Float *float_free_list; |
2e471eb5 | 2270 | |
34400008 GM |
2271 | /* Return a new float object with value FLOAT_VALUE. */ |
2272 | ||
2e471eb5 | 2273 | Lisp_Object |
971de7fb | 2274 | make_float (double float_value) |
2e471eb5 GM |
2275 | { |
2276 | register Lisp_Object val; | |
2277 | ||
dafc79fa | 2278 | MALLOC_BLOCK_INPUT; |
cfb2f32e | 2279 | |
2e471eb5 GM |
2280 | if (float_free_list) |
2281 | { | |
2282 | /* We use the data field for chaining the free list | |
2283 | so that we won't use the same field that has the mark bit. */ | |
2284 | XSETFLOAT (val, float_free_list); | |
28a099a4 | 2285 | float_free_list = float_free_list->u.chain; |
2e471eb5 GM |
2286 | } |
2287 | else | |
2288 | { | |
2289 | if (float_block_index == FLOAT_BLOCK_SIZE) | |
2290 | { | |
38182d90 PE |
2291 | struct float_block *new |
2292 | = lisp_align_malloc (sizeof *new, MEM_TYPE_FLOAT); | |
2e471eb5 | 2293 | new->next = float_block; |
72af86bd | 2294 | memset (new->gcmarkbits, 0, sizeof new->gcmarkbits); |
2e471eb5 GM |
2295 | float_block = new; |
2296 | float_block_index = 0; | |
3900d5de | 2297 | total_free_floats += FLOAT_BLOCK_SIZE; |
2e471eb5 | 2298 | } |
6b61353c KH |
2299 | XSETFLOAT (val, &float_block->floats[float_block_index]); |
2300 | float_block_index++; | |
2e471eb5 | 2301 | } |
177c0ea7 | 2302 | |
dafc79fa | 2303 | MALLOC_UNBLOCK_INPUT; |
e2984df0 | 2304 | |
f601cdf3 | 2305 | XFLOAT_INIT (val, float_value); |
6b61353c | 2306 | eassert (!FLOAT_MARKED_P (XFLOAT (val))); |
2e471eb5 GM |
2307 | consing_since_gc += sizeof (struct Lisp_Float); |
2308 | floats_consed++; | |
3900d5de | 2309 | total_free_floats--; |
2e471eb5 GM |
2310 | return val; |
2311 | } | |
2312 | ||
2e471eb5 GM |
2313 | |
2314 | \f | |
2315 | /*********************************************************************** | |
2316 | Cons Allocation | |
2317 | ***********************************************************************/ | |
2318 | ||
2319 | /* We store cons cells inside of cons_blocks, allocating a new | |
2320 | cons_block with malloc whenever necessary. Cons cells reclaimed by | |
2321 | GC are put on a free list to be reallocated before allocating | |
08b7c2cb | 2322 | any new cons cells from the latest cons_block. */ |
2e471eb5 | 2323 | |
a2821611 AS |
2324 | #define CONS_BLOCK_SIZE \ |
2325 | (((BLOCK_BYTES - sizeof (struct cons_block *) \ | |
2326 | /* The compiler might add padding at the end. */ \ | |
2327 | - (sizeof (struct Lisp_Cons) - sizeof (int))) * CHAR_BIT) \ | |
08b7c2cb SM |
2328 | / (sizeof (struct Lisp_Cons) * CHAR_BIT + 1)) |
2329 | ||
2330 | #define CONS_BLOCK(fptr) \ | |
d01a7826 | 2331 | ((struct cons_block *) ((uintptr_t) (fptr) & ~(BLOCK_ALIGN - 1))) |
08b7c2cb SM |
2332 | |
2333 | #define CONS_INDEX(fptr) \ | |
d01a7826 | 2334 | (((uintptr_t) (fptr) & (BLOCK_ALIGN - 1)) / sizeof (struct Lisp_Cons)) |
2e471eb5 GM |
2335 | |
2336 | struct cons_block | |
2337 | { | |
08b7c2cb | 2338 | /* Place `conses' at the beginning, to ease up CONS_INDEX's job. */ |
2e471eb5 | 2339 | struct Lisp_Cons conses[CONS_BLOCK_SIZE]; |
5e617bc2 | 2340 | int gcmarkbits[1 + CONS_BLOCK_SIZE / (sizeof (int) * CHAR_BIT)]; |
08b7c2cb | 2341 | struct cons_block *next; |
2e471eb5 GM |
2342 | }; |
2343 | ||
08b7c2cb SM |
2344 | #define CONS_MARKED_P(fptr) \ |
2345 | GETMARKBIT (CONS_BLOCK (fptr), CONS_INDEX ((fptr))) | |
2346 | ||
2347 | #define CONS_MARK(fptr) \ | |
2348 | SETMARKBIT (CONS_BLOCK (fptr), CONS_INDEX ((fptr))) | |
2349 | ||
2350 | #define CONS_UNMARK(fptr) \ | |
2351 | UNSETMARKBIT (CONS_BLOCK (fptr), CONS_INDEX ((fptr))) | |
2352 | ||
34400008 GM |
2353 | /* Current cons_block. */ |
2354 | ||
244ed907 | 2355 | static struct cons_block *cons_block; |
34400008 GM |
2356 | |
2357 | /* Index of first unused Lisp_Cons in the current block. */ | |
2358 | ||
fff62aa9 | 2359 | static int cons_block_index = CONS_BLOCK_SIZE; |
2e471eb5 | 2360 | |
34400008 GM |
2361 | /* Free-list of Lisp_Cons structures. */ |
2362 | ||
244ed907 | 2363 | static struct Lisp_Cons *cons_free_list; |
2e471eb5 | 2364 | |
34400008 | 2365 | /* Explicitly free a cons cell by putting it on the free-list. */ |
2e471eb5 GM |
2366 | |
2367 | void | |
971de7fb | 2368 | free_cons (struct Lisp_Cons *ptr) |
2e471eb5 | 2369 | { |
28a099a4 | 2370 | ptr->u.chain = cons_free_list; |
34400008 | 2371 | #if GC_MARK_STACK |
c644523b | 2372 | ptr->car = Vdead; |
34400008 | 2373 | #endif |
2e471eb5 | 2374 | cons_free_list = ptr; |
0dd6d66d | 2375 | consing_since_gc -= sizeof *ptr; |
3900d5de | 2376 | total_free_conses++; |
2e471eb5 GM |
2377 | } |
2378 | ||
a7ca3326 | 2379 | DEFUN ("cons", Fcons, Scons, 2, 2, 0, |
a6266d23 | 2380 | doc: /* Create a new cons, give it CAR and CDR as components, and return it. */) |
5842a27b | 2381 | (Lisp_Object car, Lisp_Object cdr) |
2e471eb5 GM |
2382 | { |
2383 | register Lisp_Object val; | |
2384 | ||
dafc79fa | 2385 | MALLOC_BLOCK_INPUT; |
cfb2f32e | 2386 | |
2e471eb5 GM |
2387 | if (cons_free_list) |
2388 | { | |
2389 | /* We use the cdr for chaining the free list | |
2390 | so that we won't use the same field that has the mark bit. */ | |
2391 | XSETCONS (val, cons_free_list); | |
28a099a4 | 2392 | cons_free_list = cons_free_list->u.chain; |
2e471eb5 GM |
2393 | } |
2394 | else | |
2395 | { | |
2396 | if (cons_block_index == CONS_BLOCK_SIZE) | |
2397 | { | |
38182d90 PE |
2398 | struct cons_block *new |
2399 | = lisp_align_malloc (sizeof *new, MEM_TYPE_CONS); | |
72af86bd | 2400 | memset (new->gcmarkbits, 0, sizeof new->gcmarkbits); |
2e471eb5 GM |
2401 | new->next = cons_block; |
2402 | cons_block = new; | |
2403 | cons_block_index = 0; | |
3900d5de | 2404 | total_free_conses += CONS_BLOCK_SIZE; |
2e471eb5 | 2405 | } |
6b61353c KH |
2406 | XSETCONS (val, &cons_block->conses[cons_block_index]); |
2407 | cons_block_index++; | |
2e471eb5 | 2408 | } |
177c0ea7 | 2409 | |
dafc79fa | 2410 | MALLOC_UNBLOCK_INPUT; |
e2984df0 | 2411 | |
f3fbd155 KR |
2412 | XSETCAR (val, car); |
2413 | XSETCDR (val, cdr); | |
6b61353c | 2414 | eassert (!CONS_MARKED_P (XCONS (val))); |
2e471eb5 | 2415 | consing_since_gc += sizeof (struct Lisp_Cons); |
3900d5de | 2416 | total_free_conses--; |
2e471eb5 GM |
2417 | cons_cells_consed++; |
2418 | return val; | |
2419 | } | |
2420 | ||
e5aab7e7 | 2421 | #ifdef GC_CHECK_CONS_LIST |
e3e56238 RS |
2422 | /* Get an error now if there's any junk in the cons free list. */ |
2423 | void | |
971de7fb | 2424 | check_cons_list (void) |
e3e56238 RS |
2425 | { |
2426 | struct Lisp_Cons *tail = cons_free_list; | |
2427 | ||
e3e56238 | 2428 | while (tail) |
28a099a4 | 2429 | tail = tail->u.chain; |
e3e56238 | 2430 | } |
e5aab7e7 | 2431 | #endif |
34400008 | 2432 | |
9b306d37 KS |
2433 | /* Make a list of 1, 2, 3, 4 or 5 specified objects. */ |
2434 | ||
2435 | Lisp_Object | |
971de7fb | 2436 | list1 (Lisp_Object arg1) |
9b306d37 KS |
2437 | { |
2438 | return Fcons (arg1, Qnil); | |
2439 | } | |
2e471eb5 GM |
2440 | |
2441 | Lisp_Object | |
971de7fb | 2442 | list2 (Lisp_Object arg1, Lisp_Object arg2) |
2e471eb5 GM |
2443 | { |
2444 | return Fcons (arg1, Fcons (arg2, Qnil)); | |
2445 | } | |
2446 | ||
34400008 | 2447 | |
2e471eb5 | 2448 | Lisp_Object |
971de7fb | 2449 | list3 (Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3) |
2e471eb5 GM |
2450 | { |
2451 | return Fcons (arg1, Fcons (arg2, Fcons (arg3, Qnil))); | |
2452 | } | |
2453 | ||
34400008 | 2454 | |
2e471eb5 | 2455 | Lisp_Object |
971de7fb | 2456 | list4 (Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3, Lisp_Object arg4) |
2e471eb5 GM |
2457 | { |
2458 | return Fcons (arg1, Fcons (arg2, Fcons (arg3, Fcons (arg4, Qnil)))); | |
2459 | } | |
2460 | ||
34400008 | 2461 | |
2e471eb5 | 2462 | Lisp_Object |
971de7fb | 2463 | list5 (Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3, Lisp_Object arg4, Lisp_Object arg5) |
2e471eb5 GM |
2464 | { |
2465 | return Fcons (arg1, Fcons (arg2, Fcons (arg3, Fcons (arg4, | |
2466 | Fcons (arg5, Qnil))))); | |
2467 | } | |
2468 | ||
694b6c97 DA |
2469 | /* Make a list of COUNT Lisp_Objects, where ARG is the |
2470 | first one. Allocate conses from pure space if TYPE | |
3438fe21 | 2471 | is CONSTYPE_PURE, or allocate as usual if type is CONSTYPE_HEAP. */ |
694b6c97 DA |
2472 | |
2473 | Lisp_Object | |
2474 | listn (enum constype type, ptrdiff_t count, Lisp_Object arg, ...) | |
2475 | { | |
2476 | va_list ap; | |
2477 | ptrdiff_t i; | |
2478 | Lisp_Object val, *objp; | |
2479 | ||
2480 | /* Change to SAFE_ALLOCA if you hit this eassert. */ | |
663e2b3f | 2481 | eassert (count <= MAX_ALLOCA / word_size); |
694b6c97 | 2482 | |
663e2b3f | 2483 | objp = alloca (count * word_size); |
694b6c97 DA |
2484 | objp[0] = arg; |
2485 | va_start (ap, arg); | |
2486 | for (i = 1; i < count; i++) | |
2487 | objp[i] = va_arg (ap, Lisp_Object); | |
2488 | va_end (ap); | |
2489 | ||
bcfbc9de | 2490 | for (val = Qnil, i = count - 1; i >= 0; i--) |
694b6c97 | 2491 | { |
3438fe21 | 2492 | if (type == CONSTYPE_PURE) |
694b6c97 | 2493 | val = pure_cons (objp[i], val); |
3438fe21 | 2494 | else if (type == CONSTYPE_HEAP) |
694b6c97 DA |
2495 | val = Fcons (objp[i], val); |
2496 | else | |
1088b922 | 2497 | emacs_abort (); |
694b6c97 DA |
2498 | } |
2499 | return val; | |
2500 | } | |
34400008 | 2501 | |
a7ca3326 | 2502 | DEFUN ("list", Flist, Slist, 0, MANY, 0, |
eae936e2 | 2503 | doc: /* Return a newly created list with specified arguments as elements. |
ae8e8122 MB |
2504 | Any number of arguments, even zero arguments, are allowed. |
2505 | usage: (list &rest OBJECTS) */) | |
f66c7cf8 | 2506 | (ptrdiff_t nargs, Lisp_Object *args) |
2e471eb5 GM |
2507 | { |
2508 | register Lisp_Object val; | |
2509 | val = Qnil; | |
2510 | ||
2511 | while (nargs > 0) | |
2512 | { | |
2513 | nargs--; | |
2514 | val = Fcons (args[nargs], val); | |
2515 | } | |
2516 | return val; | |
2517 | } | |
2518 | ||
34400008 | 2519 | |
a7ca3326 | 2520 | DEFUN ("make-list", Fmake_list, Smake_list, 2, 2, 0, |
a6266d23 | 2521 | doc: /* Return a newly created list of length LENGTH, with each element being INIT. */) |
5842a27b | 2522 | (register Lisp_Object length, Lisp_Object init) |
2e471eb5 GM |
2523 | { |
2524 | register Lisp_Object val; | |
14162469 | 2525 | register EMACS_INT size; |
2e471eb5 | 2526 | |
b7826503 | 2527 | CHECK_NATNUM (length); |
2e471eb5 GM |
2528 | size = XFASTINT (length); |
2529 | ||
2530 | val = Qnil; | |
ce070307 GM |
2531 | while (size > 0) |
2532 | { | |
2533 | val = Fcons (init, val); | |
2534 | --size; | |
2535 | ||
2536 | if (size > 0) | |
2537 | { | |
2538 | val = Fcons (init, val); | |
2539 | --size; | |
177c0ea7 | 2540 | |
ce070307 GM |
2541 | if (size > 0) |
2542 | { | |
2543 | val = Fcons (init, val); | |
2544 | --size; | |
177c0ea7 | 2545 | |
ce070307 GM |
2546 | if (size > 0) |
2547 | { | |
2548 | val = Fcons (init, val); | |
2549 | --size; | |
177c0ea7 | 2550 | |
ce070307 GM |
2551 | if (size > 0) |
2552 | { | |
2553 | val = Fcons (init, val); | |
2554 | --size; | |
2555 | } | |
2556 | } | |
2557 | } | |
2558 | } | |
2559 | ||
2560 | QUIT; | |
2561 | } | |
177c0ea7 | 2562 | |
7146af97 JB |
2563 | return val; |
2564 | } | |
2e471eb5 GM |
2565 | |
2566 | ||
7146af97 | 2567 | \f |
2e471eb5 GM |
2568 | /*********************************************************************** |
2569 | Vector Allocation | |
2570 | ***********************************************************************/ | |
7146af97 | 2571 | |
f3372c87 DA |
2572 | /* This value is balanced well enough to avoid too much internal overhead |
2573 | for the most common cases; it's not required to be a power of two, but | |
2574 | it's expected to be a mult-of-ROUNDUP_SIZE (see below). */ | |
34400008 | 2575 | |
f3372c87 | 2576 | #define VECTOR_BLOCK_SIZE 4096 |
7146af97 | 2577 | |
d06714cb | 2578 | /* Align allocation request sizes to be a multiple of ROUNDUP_SIZE. */ |
dd0b0efb PE |
2579 | enum |
2580 | { | |
2b90362b | 2581 | roundup_size = COMMON_MULTIPLE (word_size, USE_LSB_TAG ? GCALIGNMENT : 1) |
dd0b0efb | 2582 | }; |
34400008 | 2583 | |
bfe3e0a2 PE |
2584 | /* ROUNDUP_SIZE must be a power of 2. */ |
2585 | verify ((roundup_size & (roundup_size - 1)) == 0); | |
2586 | ||
ca95b3eb DA |
2587 | /* Verify assumptions described above. */ |
2588 | verify ((VECTOR_BLOCK_SIZE % roundup_size) == 0); | |
ee28be33 | 2589 | verify (VECTOR_BLOCK_SIZE <= (1 << PSEUDOVECTOR_SIZE_BITS)); |
ca95b3eb | 2590 | |
bfe3e0a2 | 2591 | /* Round up X to nearest mult-of-ROUNDUP_SIZE. */ |
f3372c87 DA |
2592 | |
2593 | #define vroundup(x) (((x) + (roundup_size - 1)) & ~(roundup_size - 1)) | |
2594 | ||
2595 | /* Rounding helps to maintain alignment constraints if USE_LSB_TAG. */ | |
2596 | ||
2597 | #define VECTOR_BLOCK_BYTES (VECTOR_BLOCK_SIZE - vroundup (sizeof (void *))) | |
2598 | ||
2599 | /* Size of the minimal vector allocated from block. */ | |
2600 | ||
2601 | #define VBLOCK_BYTES_MIN vroundup (sizeof (struct Lisp_Vector)) | |
2602 | ||
2603 | /* Size of the largest vector allocated from block. */ | |
2604 | ||
2605 | #define VBLOCK_BYTES_MAX \ | |
d06714cb | 2606 | vroundup ((VECTOR_BLOCK_BYTES / 2) - word_size) |
f3372c87 DA |
2607 | |
2608 | /* We maintain one free list for each possible block-allocated | |
2609 | vector size, and this is the number of free lists we have. */ | |
2610 | ||
2611 | #define VECTOR_MAX_FREE_LIST_INDEX \ | |
2612 | ((VECTOR_BLOCK_BYTES - VBLOCK_BYTES_MIN) / roundup_size + 1) | |
2613 | ||
f3372c87 DA |
2614 | /* Common shortcut to advance vector pointer over a block data. */ |
2615 | ||
2616 | #define ADVANCE(v, nbytes) ((struct Lisp_Vector *) ((char *) (v) + (nbytes))) | |
2617 | ||
2618 | /* Common shortcut to calculate NBYTES-vector index in VECTOR_FREE_LISTS. */ | |
2619 | ||
2620 | #define VINDEX(nbytes) (((nbytes) - VBLOCK_BYTES_MIN) / roundup_size) | |
2621 | ||
2622 | /* Common shortcut to setup vector on a free list. */ | |
2623 | ||
2624 | #define SETUP_ON_FREE_LIST(v, nbytes, index) \ | |
2625 | do { \ | |
ee28be33 | 2626 | XSETPVECTYPESIZE (v, PVEC_FREE, nbytes); \ |
f3372c87 DA |
2627 | eassert ((nbytes) % roundup_size == 0); \ |
2628 | (index) = VINDEX (nbytes); \ | |
2629 | eassert ((index) < VECTOR_MAX_FREE_LIST_INDEX); \ | |
2630 | (v)->header.next.vector = vector_free_lists[index]; \ | |
2631 | vector_free_lists[index] = (v); \ | |
5b835e1d | 2632 | total_free_vector_slots += (nbytes) / word_size; \ |
f3372c87 DA |
2633 | } while (0) |
2634 | ||
2635 | struct vector_block | |
2636 | { | |
2637 | char data[VECTOR_BLOCK_BYTES]; | |
2638 | struct vector_block *next; | |
2639 | }; | |
2640 | ||
2641 | /* Chain of vector blocks. */ | |
2642 | ||
2643 | static struct vector_block *vector_blocks; | |
2644 | ||
2645 | /* Vector free lists, where NTH item points to a chain of free | |
2646 | vectors of the same NBYTES size, so NTH == VINDEX (NBYTES). */ | |
2647 | ||
2648 | static struct Lisp_Vector *vector_free_lists[VECTOR_MAX_FREE_LIST_INDEX]; | |
2649 | ||
2650 | /* Singly-linked list of large vectors. */ | |
2651 | ||
2652 | static struct Lisp_Vector *large_vectors; | |
2653 | ||
2654 | /* The only vector with 0 slots, allocated from pure space. */ | |
2655 | ||
9730daca | 2656 | Lisp_Object zero_vector; |
f3372c87 | 2657 | |
3ab6e069 DA |
2658 | /* Number of live vectors. */ |
2659 | ||
2660 | static EMACS_INT total_vectors; | |
2661 | ||
5b835e1d | 2662 | /* Total size of live and free vectors, in Lisp_Object units. */ |
3ab6e069 | 2663 | |
5b835e1d | 2664 | static EMACS_INT total_vector_slots, total_free_vector_slots; |
3ab6e069 | 2665 | |
f3372c87 DA |
2666 | /* Get a new vector block. */ |
2667 | ||
2668 | static struct vector_block * | |
2669 | allocate_vector_block (void) | |
2670 | { | |
38182d90 | 2671 | struct vector_block *block = xmalloc (sizeof *block); |
f3372c87 DA |
2672 | |
2673 | #if GC_MARK_STACK && !defined GC_MALLOC_CHECK | |
2674 | mem_insert (block->data, block->data + VECTOR_BLOCK_BYTES, | |
2675 | MEM_TYPE_VECTOR_BLOCK); | |
2676 | #endif | |
2677 | ||
2678 | block->next = vector_blocks; | |
2679 | vector_blocks = block; | |
2680 | return block; | |
2681 | } | |
2682 | ||
2683 | /* Called once to initialize vector allocation. */ | |
2684 | ||
2685 | static void | |
2686 | init_vectors (void) | |
2687 | { | |
9730daca | 2688 | zero_vector = make_pure_vector (0); |
f3372c87 DA |
2689 | } |
2690 | ||
2691 | /* Allocate vector from a vector block. */ | |
2692 | ||
2693 | static struct Lisp_Vector * | |
2694 | allocate_vector_from_block (size_t nbytes) | |
2695 | { | |
2696 | struct Lisp_Vector *vector, *rest; | |
2697 | struct vector_block *block; | |
2698 | size_t index, restbytes; | |
2699 | ||
2700 | eassert (VBLOCK_BYTES_MIN <= nbytes && nbytes <= VBLOCK_BYTES_MAX); | |
2701 | eassert (nbytes % roundup_size == 0); | |
2702 | ||
2703 | /* First, try to allocate from a free list | |
2704 | containing vectors of the requested size. */ | |
2705 | index = VINDEX (nbytes); | |
2706 | if (vector_free_lists[index]) | |
2707 | { | |
2708 | vector = vector_free_lists[index]; | |
2709 | vector_free_lists[index] = vector->header.next.vector; | |
2710 | vector->header.next.nbytes = nbytes; | |
5b835e1d | 2711 | total_free_vector_slots -= nbytes / word_size; |
f3372c87 DA |
2712 | return vector; |
2713 | } | |
2714 | ||
2715 | /* Next, check free lists containing larger vectors. Since | |
2716 | we will split the result, we should have remaining space | |
2717 | large enough to use for one-slot vector at least. */ | |
2718 | for (index = VINDEX (nbytes + VBLOCK_BYTES_MIN); | |
2719 | index < VECTOR_MAX_FREE_LIST_INDEX; index++) | |
2720 | if (vector_free_lists[index]) | |
2721 | { | |
2722 | /* This vector is larger than requested. */ | |
2723 | vector = vector_free_lists[index]; | |
2724 | vector_free_lists[index] = vector->header.next.vector; | |
2725 | vector->header.next.nbytes = nbytes; | |
5b835e1d | 2726 | total_free_vector_slots -= nbytes / word_size; |
f3372c87 DA |
2727 | |
2728 | /* Excess bytes are used for the smaller vector, | |
2729 | which should be set on an appropriate free list. */ | |
2730 | restbytes = index * roundup_size + VBLOCK_BYTES_MIN - nbytes; | |
2731 | eassert (restbytes % roundup_size == 0); | |
2732 | rest = ADVANCE (vector, nbytes); | |
2733 | SETUP_ON_FREE_LIST (rest, restbytes, index); | |
2734 | return vector; | |
2735 | } | |
2736 | ||
2737 | /* Finally, need a new vector block. */ | |
2738 | block = allocate_vector_block (); | |
2739 | ||
2740 | /* New vector will be at the beginning of this block. */ | |
2741 | vector = (struct Lisp_Vector *) block->data; | |
2742 | vector->header.next.nbytes = nbytes; | |
2743 | ||
2744 | /* If the rest of space from this block is large enough | |
2745 | for one-slot vector at least, set up it on a free list. */ | |
2746 | restbytes = VECTOR_BLOCK_BYTES - nbytes; | |
2747 | if (restbytes >= VBLOCK_BYTES_MIN) | |
2748 | { | |
2749 | eassert (restbytes % roundup_size == 0); | |
2750 | rest = ADVANCE (vector, nbytes); | |
2751 | SETUP_ON_FREE_LIST (rest, restbytes, index); | |
2752 | } | |
2753 | return vector; | |
2754 | } | |
2755 | ||
f3372c87 DA |
2756 | /* Nonzero if VECTOR pointer is valid pointer inside BLOCK. */ |
2757 | ||
2758 | #define VECTOR_IN_BLOCK(vector, block) \ | |
2759 | ((char *) (vector) <= (block)->data \ | |
2760 | + VECTOR_BLOCK_BYTES - VBLOCK_BYTES_MIN) | |
2761 | ||
ee28be33 SM |
2762 | /* Number of bytes used by vector-block-allocated object. This is the only |
2763 | place where we actually use the `nbytes' field of the vector-header. | |
2764 | I.e. we could get rid of the `nbytes' field by computing it based on the | |
2765 | vector-type. */ | |
2766 | ||
2767 | #define PSEUDOVECTOR_NBYTES(vector) \ | |
2768 | (PSEUDOVECTOR_TYPEP (&vector->header, PVEC_FREE) \ | |
2769 | ? vector->header.size & PSEUDOVECTOR_SIZE_MASK \ | |
356e7178 | 2770 | : vector->header.next.nbytes) |
ee28be33 | 2771 | |
f3372c87 DA |
2772 | /* Reclaim space used by unmarked vectors. */ |
2773 | ||
2774 | static void | |
2775 | sweep_vectors (void) | |
2776 | { | |
2777 | struct vector_block *block = vector_blocks, **bprev = &vector_blocks; | |
2778 | struct Lisp_Vector *vector, *next, **vprev = &large_vectors; | |
2779 | ||
5b835e1d | 2780 | total_vectors = total_vector_slots = total_free_vector_slots = 0; |
f3372c87 DA |
2781 | memset (vector_free_lists, 0, sizeof (vector_free_lists)); |
2782 | ||
2783 | /* Looking through vector blocks. */ | |
2784 | ||
2785 | for (block = vector_blocks; block; block = *bprev) | |
2786 | { | |
fce31d69 | 2787 | bool free_this_block = 0; |
f3372c87 DA |
2788 | |
2789 | for (vector = (struct Lisp_Vector *) block->data; | |
2790 | VECTOR_IN_BLOCK (vector, block); vector = next) | |
2791 | { | |
2792 | if (VECTOR_MARKED_P (vector)) | |
2793 | { | |
2794 | VECTOR_UNMARK (vector); | |
3ab6e069 | 2795 | total_vectors++; |
5b835e1d | 2796 | total_vector_slots += vector->header.next.nbytes / word_size; |
f3372c87 DA |
2797 | next = ADVANCE (vector, vector->header.next.nbytes); |
2798 | } | |
2799 | else | |
2800 | { | |
ee28be33 SM |
2801 | ptrdiff_t nbytes = PSEUDOVECTOR_NBYTES (vector); |
2802 | ptrdiff_t total_bytes = nbytes; | |
f3372c87 | 2803 | |
ee28be33 | 2804 | next = ADVANCE (vector, nbytes); |
f3372c87 DA |
2805 | |
2806 | /* While NEXT is not marked, try to coalesce with VECTOR, | |
2807 | thus making VECTOR of the largest possible size. */ | |
2808 | ||
2809 | while (VECTOR_IN_BLOCK (next, block)) | |
2810 | { | |
2811 | if (VECTOR_MARKED_P (next)) | |
2812 | break; | |
ee28be33 SM |
2813 | nbytes = PSEUDOVECTOR_NBYTES (next); |
2814 | total_bytes += nbytes; | |
f3372c87 DA |
2815 | next = ADVANCE (next, nbytes); |
2816 | } | |
bfe3e0a2 | 2817 | |
ee28be33 | 2818 | eassert (total_bytes % roundup_size == 0); |
f3372c87 DA |
2819 | |
2820 | if (vector == (struct Lisp_Vector *) block->data | |
2821 | && !VECTOR_IN_BLOCK (next, block)) | |
2822 | /* This block should be freed because all of it's | |
2823 | space was coalesced into the only free vector. */ | |
2824 | free_this_block = 1; | |
2825 | else | |
ee28be33 SM |
2826 | { |
2827 | int tmp; | |
2828 | SETUP_ON_FREE_LIST (vector, total_bytes, tmp); | |
2829 | } | |
f3372c87 DA |
2830 | } |
2831 | } | |
2832 | ||
2833 | if (free_this_block) | |
2834 | { | |
2835 | *bprev = block->next; | |
2836 | #if GC_MARK_STACK && !defined GC_MALLOC_CHECK | |
2837 | mem_delete (mem_find (block->data)); | |
2838 | #endif | |
2839 | xfree (block); | |
2840 | } | |
2841 | else | |
2842 | bprev = &block->next; | |
2843 | } | |
2844 | ||
2845 | /* Sweep large vectors. */ | |
2846 | ||
2847 | for (vector = large_vectors; vector; vector = *vprev) | |
2848 | { | |
2849 | if (VECTOR_MARKED_P (vector)) | |
2850 | { | |
2851 | VECTOR_UNMARK (vector); | |
3ab6e069 | 2852 | total_vectors++; |
169925ec DA |
2853 | if (vector->header.size & PSEUDOVECTOR_FLAG) |
2854 | { | |
d06714cb PE |
2855 | struct Lisp_Bool_Vector *b = (struct Lisp_Bool_Vector *) vector; |
2856 | ||
2857 | /* All non-bool pseudovectors are small enough to be allocated | |
2858 | from vector blocks. This code should be redesigned if some | |
2859 | pseudovector type grows beyond VBLOCK_BYTES_MAX. */ | |
2860 | eassert (PSEUDOVECTOR_TYPEP (&vector->header, PVEC_BOOL_VECTOR)); | |
2861 | ||
5b835e1d | 2862 | total_vector_slots |
d06714cb PE |
2863 | += (bool_header_size |
2864 | + ((b->size + BOOL_VECTOR_BITS_PER_CHAR - 1) | |
5b835e1d | 2865 | / BOOL_VECTOR_BITS_PER_CHAR)) / word_size; |
169925ec DA |
2866 | } |
2867 | else | |
5b835e1d DA |
2868 | total_vector_slots |
2869 | += header_size / word_size + vector->header.size; | |
f3372c87 DA |
2870 | vprev = &vector->header.next.vector; |
2871 | } | |
2872 | else | |
2873 | { | |
2874 | *vprev = vector->header.next.vector; | |
2875 | lisp_free (vector); | |
2876 | } | |
2877 | } | |
2878 | } | |
2879 | ||
34400008 GM |
2880 | /* Value is a pointer to a newly allocated Lisp_Vector structure |
2881 | with room for LEN Lisp_Objects. */ | |
2882 | ||
ece93c02 | 2883 | static struct Lisp_Vector * |
d311d28c | 2884 | allocate_vectorlike (ptrdiff_t len) |
1825c68d KH |
2885 | { |
2886 | struct Lisp_Vector *p; | |
2887 | ||
dafc79fa SM |
2888 | MALLOC_BLOCK_INPUT; |
2889 | ||
f3372c87 | 2890 | if (len == 0) |
9730daca | 2891 | p = XVECTOR (zero_vector); |
d12e8f5a | 2892 | else |
8bbbc977 | 2893 | { |
d12e8f5a | 2894 | size_t nbytes = header_size + len * word_size; |
f3372c87 | 2895 | |
d12e8f5a DA |
2896 | #ifdef DOUG_LEA_MALLOC |
2897 | /* Prevent mmap'ing the chunk. Lisp data may not be mmap'ed | |
2898 | because mapped region contents are not preserved in | |
2899 | a dumped Emacs. */ | |
2900 | mallopt (M_MMAP_MAX, 0); | |
2901 | #endif | |
f3372c87 | 2902 | |
d12e8f5a DA |
2903 | if (nbytes <= VBLOCK_BYTES_MAX) |
2904 | p = allocate_vector_from_block (vroundup (nbytes)); | |
2905 | else | |
2906 | { | |
38182d90 | 2907 | p = lisp_malloc (nbytes, MEM_TYPE_VECTORLIKE); |
d12e8f5a DA |
2908 | p->header.next.vector = large_vectors; |
2909 | large_vectors = p; | |
2910 | } | |
177c0ea7 | 2911 | |
d1658221 | 2912 | #ifdef DOUG_LEA_MALLOC |
d12e8f5a DA |
2913 | /* Back to a reasonable maximum of mmap'ed areas. */ |
2914 | mallopt (M_MMAP_MAX, MMAP_MAX_AREAS); | |
d1658221 | 2915 | #endif |
177c0ea7 | 2916 | |
d12e8f5a DA |
2917 | consing_since_gc += nbytes; |
2918 | vector_cells_consed += len; | |
2919 | } | |
1825c68d | 2920 | |
dafc79fa | 2921 | MALLOC_UNBLOCK_INPUT; |
e2984df0 | 2922 | |
1825c68d KH |
2923 | return p; |
2924 | } | |
2925 | ||
34400008 | 2926 | |
dd0b0efb | 2927 | /* Allocate a vector with LEN slots. */ |
ece93c02 GM |
2928 | |
2929 | struct Lisp_Vector * | |
dd0b0efb | 2930 | allocate_vector (EMACS_INT len) |
ece93c02 | 2931 | { |
dd0b0efb PE |
2932 | struct Lisp_Vector *v; |
2933 | ptrdiff_t nbytes_max = min (PTRDIFF_MAX, SIZE_MAX); | |
2934 | ||
2935 | if (min ((nbytes_max - header_size) / word_size, MOST_POSITIVE_FIXNUM) < len) | |
2936 | memory_full (SIZE_MAX); | |
2937 | v = allocate_vectorlike (len); | |
2938 | v->header.size = len; | |
ece93c02 GM |
2939 | return v; |
2940 | } | |
2941 | ||
2942 | ||
2943 | /* Allocate other vector-like structures. */ | |
2944 | ||
30f95089 | 2945 | struct Lisp_Vector * |
d311d28c | 2946 | allocate_pseudovector (int memlen, int lisplen, int tag) |
ece93c02 | 2947 | { |
d2029e5b | 2948 | struct Lisp_Vector *v = allocate_vectorlike (memlen); |
e46bb31a | 2949 | int i; |
177c0ea7 | 2950 | |
d2029e5b | 2951 | /* Only the first lisplen slots will be traced normally by the GC. */ |
d2029e5b | 2952 | for (i = 0; i < lisplen; ++i) |
ece93c02 | 2953 | v->contents[i] = Qnil; |
177c0ea7 | 2954 | |
eab3844f | 2955 | XSETPVECTYPESIZE (v, tag, lisplen); |
d2029e5b SM |
2956 | return v; |
2957 | } | |
d2029e5b | 2958 | |
36429c89 DA |
2959 | struct buffer * |
2960 | allocate_buffer (void) | |
2961 | { | |
38182d90 | 2962 | struct buffer *b = lisp_malloc (sizeof *b, MEM_TYPE_BUFFER); |
36429c89 DA |
2963 | |
2964 | XSETPVECTYPESIZE (b, PVEC_BUFFER, (offsetof (struct buffer, own_text) | |
2965 | - header_size) / word_size); | |
c752cfa9 DA |
2966 | /* Put B on the chain of all buffers including killed ones. */ |
2967 | b->header.next.buffer = all_buffers; | |
2968 | all_buffers = b; | |
2969 | /* Note that the rest fields of B are not initialized. */ | |
36429c89 DA |
2970 | return b; |
2971 | } | |
2972 | ||
ece93c02 | 2973 | struct Lisp_Hash_Table * |
878f97ff | 2974 | allocate_hash_table (void) |
ece93c02 | 2975 | { |
878f97ff | 2976 | return ALLOCATE_PSEUDOVECTOR (struct Lisp_Hash_Table, count, PVEC_HASH_TABLE); |
ece93c02 GM |
2977 | } |
2978 | ||
ece93c02 | 2979 | struct window * |
971de7fb | 2980 | allocate_window (void) |
ece93c02 | 2981 | { |
62efea5e | 2982 | struct window *w; |
177c0ea7 | 2983 | |
62efea5e DA |
2984 | w = ALLOCATE_PSEUDOVECTOR (struct window, current_matrix, PVEC_WINDOW); |
2985 | /* Users assumes that non-Lisp data is zeroed. */ | |
2986 | memset (&w->current_matrix, 0, | |
2987 | sizeof (*w) - offsetof (struct window, current_matrix)); | |
2988 | return w; | |
2989 | } | |
177c0ea7 | 2990 | |
4a729fd8 | 2991 | struct terminal * |
971de7fb | 2992 | allocate_terminal (void) |
4a729fd8 | 2993 | { |
62efea5e | 2994 | struct terminal *t; |
ece93c02 | 2995 | |
62efea5e DA |
2996 | t = ALLOCATE_PSEUDOVECTOR (struct terminal, next_terminal, PVEC_TERMINAL); |
2997 | /* Users assumes that non-Lisp data is zeroed. */ | |
2998 | memset (&t->next_terminal, 0, | |
2999 | sizeof (*t) - offsetof (struct terminal, next_terminal)); | |
d2029e5b | 3000 | return t; |
4a729fd8 | 3001 | } |
ece93c02 GM |
3002 | |
3003 | struct frame * | |
971de7fb | 3004 | allocate_frame (void) |
ece93c02 | 3005 | { |
62efea5e DA |
3006 | struct frame *f; |
3007 | ||
3008 | f = ALLOCATE_PSEUDOVECTOR (struct frame, face_cache, PVEC_FRAME); | |
3009 | /* Users assumes that non-Lisp data is zeroed. */ | |
72af86bd | 3010 | memset (&f->face_cache, 0, |
62efea5e | 3011 | sizeof (*f) - offsetof (struct frame, face_cache)); |
d2029e5b | 3012 | return f; |
ece93c02 GM |
3013 | } |
3014 | ||
ece93c02 | 3015 | struct Lisp_Process * |
971de7fb | 3016 | allocate_process (void) |
ece93c02 | 3017 | { |
62efea5e | 3018 | struct Lisp_Process *p; |
ece93c02 | 3019 | |
62efea5e DA |
3020 | p = ALLOCATE_PSEUDOVECTOR (struct Lisp_Process, pid, PVEC_PROCESS); |
3021 | /* Users assumes that non-Lisp data is zeroed. */ | |
3022 | memset (&p->pid, 0, | |
3023 | sizeof (*p) - offsetof (struct Lisp_Process, pid)); | |
3024 | return p; | |
3025 | } | |
ece93c02 | 3026 | |
a7ca3326 | 3027 | DEFUN ("make-vector", Fmake_vector, Smake_vector, 2, 2, 0, |
a6266d23 | 3028 | doc: /* Return a newly created vector of length LENGTH, with each element being INIT. |
7ee72033 | 3029 | See also the function `vector'. */) |
5842a27b | 3030 | (register Lisp_Object length, Lisp_Object init) |
7146af97 | 3031 | { |
1825c68d | 3032 | Lisp_Object vector; |
d311d28c PE |
3033 | register ptrdiff_t sizei; |
3034 | register ptrdiff_t i; | |
7146af97 JB |
3035 | register struct Lisp_Vector *p; |
3036 | ||
b7826503 | 3037 | CHECK_NATNUM (length); |
7146af97 | 3038 | |
d311d28c PE |
3039 | p = allocate_vector (XFASTINT (length)); |
3040 | sizei = XFASTINT (length); | |
ae35e756 PE |
3041 | for (i = 0; i < sizei; i++) |
3042 | p->contents[i] = init; | |
7146af97 | 3043 | |
1825c68d | 3044 | XSETVECTOR (vector, p); |
7146af97 JB |
3045 | return vector; |
3046 | } | |
3047 | ||
34400008 | 3048 | |
a7ca3326 | 3049 | DEFUN ("vector", Fvector, Svector, 0, MANY, 0, |
eae936e2 | 3050 | doc: /* Return a newly created vector with specified arguments as elements. |
ae8e8122 MB |
3051 | Any number of arguments, even zero arguments, are allowed. |
3052 | usage: (vector &rest OBJECTS) */) | |
f66c7cf8 | 3053 | (ptrdiff_t nargs, Lisp_Object *args) |
7146af97 JB |
3054 | { |
3055 | register Lisp_Object len, val; | |
f66c7cf8 | 3056 | ptrdiff_t i; |
7146af97 JB |
3057 | register struct Lisp_Vector *p; |
3058 | ||
67ba9986 | 3059 | XSETFASTINT (len, nargs); |
7146af97 JB |
3060 | val = Fmake_vector (len, Qnil); |
3061 | p = XVECTOR (val); | |
ae35e756 PE |
3062 | for (i = 0; i < nargs; i++) |
3063 | p->contents[i] = args[i]; | |
7146af97 JB |
3064 | return val; |
3065 | } | |
3066 | ||
3017f87f SM |
3067 | void |
3068 | make_byte_code (struct Lisp_Vector *v) | |
3069 | { | |
3070 | if (v->header.size > 1 && STRINGP (v->contents[1]) | |
3071 | && STRING_MULTIBYTE (v->contents[1])) | |
3072 | /* BYTECODE-STRING must have been produced by Emacs 20.2 or the | |
3073 | earlier because they produced a raw 8-bit string for byte-code | |
3074 | and now such a byte-code string is loaded as multibyte while | |
3075 | raw 8-bit characters converted to multibyte form. Thus, now we | |
3076 | must convert them back to the original unibyte form. */ | |
3077 | v->contents[1] = Fstring_as_unibyte (v->contents[1]); | |
3078 | XSETPVECTYPE (v, PVEC_COMPILED); | |
3079 | } | |
34400008 | 3080 | |
a7ca3326 | 3081 | DEFUN ("make-byte-code", Fmake_byte_code, Smake_byte_code, 4, MANY, 0, |
a6266d23 | 3082 | doc: /* Create a byte-code object with specified arguments as elements. |
e2abe5a1 SM |
3083 | The arguments should be the ARGLIST, bytecode-string BYTE-CODE, constant |
3084 | vector CONSTANTS, maximum stack size DEPTH, (optional) DOCSTRING, | |
3085 | and (optional) INTERACTIVE-SPEC. | |
228299fa | 3086 | The first four arguments are required; at most six have any |
ae8e8122 | 3087 | significance. |
e2abe5a1 SM |
3088 | The ARGLIST can be either like the one of `lambda', in which case the arguments |
3089 | will be dynamically bound before executing the byte code, or it can be an | |
3090 | integer of the form NNNNNNNRMMMMMMM where the 7bit MMMMMMM specifies the | |
3091 | minimum number of arguments, the 7-bit NNNNNNN specifies the maximum number | |
3092 | of arguments (ignoring &rest) and the R bit specifies whether there is a &rest | |
3093 | argument to catch the left-over arguments. If such an integer is used, the | |
3094 | arguments will not be dynamically bound but will be instead pushed on the | |
3095 | stack before executing the byte-code. | |
92cc28b2 | 3096 | usage: (make-byte-code ARGLIST BYTE-CODE CONSTANTS DEPTH &optional DOCSTRING INTERACTIVE-SPEC &rest ELEMENTS) */) |
f66c7cf8 | 3097 | (ptrdiff_t nargs, Lisp_Object *args) |
7146af97 JB |
3098 | { |
3099 | register Lisp_Object len, val; | |
f66c7cf8 | 3100 | ptrdiff_t i; |
7146af97 JB |
3101 | register struct Lisp_Vector *p; |
3102 | ||
12fbe755 | 3103 | /* We used to purecopy everything here, if purify-flag was set. This worked |
3017f87f SM |
3104 | OK for Emacs-23, but with Emacs-24's lexical binding code, it can be |
3105 | dangerous, since make-byte-code is used during execution to build | |
3106 | closures, so any closure built during the preload phase would end up | |
3107 | copied into pure space, including its free variables, which is sometimes | |
3108 | just wasteful and other times plainly wrong (e.g. those free vars may want | |
3109 | to be setcar'd). */ | |
9eac9d59 | 3110 | |
3017f87f SM |
3111 | XSETFASTINT (len, nargs); |
3112 | val = Fmake_vector (len, Qnil); | |
9eac9d59 | 3113 | |
7146af97 | 3114 | p = XVECTOR (val); |
ae35e756 | 3115 | for (i = 0; i < nargs; i++) |
3017f87f SM |
3116 | p->contents[i] = args[i]; |
3117 | make_byte_code (p); | |
876c194c | 3118 | XSETCOMPILED (val, p); |
7146af97 JB |
3119 | return val; |
3120 | } | |
2e471eb5 | 3121 | |
34400008 | 3122 | |
7146af97 | 3123 | \f |
2e471eb5 GM |
3124 | /*********************************************************************** |
3125 | Symbol Allocation | |
3126 | ***********************************************************************/ | |
7146af97 | 3127 | |
d55c12ed AS |
3128 | /* Like struct Lisp_Symbol, but padded so that the size is a multiple |
3129 | of the required alignment if LSB tags are used. */ | |
3130 | ||
3131 | union aligned_Lisp_Symbol | |
3132 | { | |
3133 | struct Lisp_Symbol s; | |
bfe3e0a2 | 3134 | #if USE_LSB_TAG |
2b90362b DA |
3135 | unsigned char c[(sizeof (struct Lisp_Symbol) + GCALIGNMENT - 1) |
3136 | & -GCALIGNMENT]; | |
d55c12ed AS |
3137 | #endif |
3138 | }; | |
3139 | ||
2e471eb5 GM |
3140 | /* Each symbol_block is just under 1020 bytes long, since malloc |
3141 | really allocates in units of powers of two and uses 4 bytes for its | |
3017f87f | 3142 | own overhead. */ |
7146af97 JB |
3143 | |
3144 | #define SYMBOL_BLOCK_SIZE \ | |
d55c12ed | 3145 | ((1020 - sizeof (struct symbol_block *)) / sizeof (union aligned_Lisp_Symbol)) |
7146af97 JB |
3146 | |
3147 | struct symbol_block | |
2e471eb5 | 3148 | { |
6b61353c | 3149 | /* Place `symbols' first, to preserve alignment. */ |
d55c12ed | 3150 | union aligned_Lisp_Symbol symbols[SYMBOL_BLOCK_SIZE]; |
6b61353c | 3151 | struct symbol_block *next; |
2e471eb5 | 3152 | }; |
7146af97 | 3153 | |
34400008 GM |
3154 | /* Current symbol block and index of first unused Lisp_Symbol |
3155 | structure in it. */ | |
3156 | ||
d3d47262 | 3157 | static struct symbol_block *symbol_block; |
fff62aa9 | 3158 | static int symbol_block_index = SYMBOL_BLOCK_SIZE; |
7146af97 | 3159 | |
34400008 GM |
3160 | /* List of free symbols. */ |
3161 | ||
d3d47262 | 3162 | static struct Lisp_Symbol *symbol_free_list; |
7146af97 | 3163 | |
a7ca3326 | 3164 | DEFUN ("make-symbol", Fmake_symbol, Smake_symbol, 1, 1, 0, |
a6266d23 | 3165 | doc: /* Return a newly allocated uninterned symbol whose name is NAME. |
7ee72033 | 3166 | Its value and function definition are void, and its property list is nil. */) |
5842a27b | 3167 | (Lisp_Object name) |
7146af97 JB |
3168 | { |
3169 | register Lisp_Object val; | |
3170 | register struct Lisp_Symbol *p; | |
3171 | ||
b7826503 | 3172 | CHECK_STRING (name); |
7146af97 | 3173 | |
dafc79fa | 3174 | MALLOC_BLOCK_INPUT; |
e2984df0 | 3175 | |
7146af97 JB |
3176 | if (symbol_free_list) |
3177 | { | |
45d12a89 | 3178 | XSETSYMBOL (val, symbol_free_list); |
28a099a4 | 3179 | symbol_free_list = symbol_free_list->next; |
7146af97 JB |
3180 | } |
3181 | else | |
3182 | { | |
3183 | if (symbol_block_index == SYMBOL_BLOCK_SIZE) | |
3184 | { | |
38182d90 PE |
3185 | struct symbol_block *new |
3186 | = lisp_malloc (sizeof *new, MEM_TYPE_SYMBOL); | |
7146af97 JB |
3187 | new->next = symbol_block; |
3188 | symbol_block = new; | |
3189 | symbol_block_index = 0; | |
3900d5de | 3190 | total_free_symbols += SYMBOL_BLOCK_SIZE; |
7146af97 | 3191 | } |
d55c12ed | 3192 | XSETSYMBOL (val, &symbol_block->symbols[symbol_block_index].s); |
6b61353c | 3193 | symbol_block_index++; |
7146af97 | 3194 | } |
177c0ea7 | 3195 | |
dafc79fa | 3196 | MALLOC_UNBLOCK_INPUT; |
e2984df0 | 3197 | |
7146af97 | 3198 | p = XSYMBOL (val); |
c644523b DA |
3199 | set_symbol_name (val, name); |
3200 | set_symbol_plist (val, Qnil); | |
ce5b453a SM |
3201 | p->redirect = SYMBOL_PLAINVAL; |
3202 | SET_SYMBOL_VAL (p, Qunbound); | |
c644523b DA |
3203 | set_symbol_function (val, Qunbound); |
3204 | set_symbol_next (val, NULL); | |
2336fe58 | 3205 | p->gcmarkbit = 0; |
9e713715 GM |
3206 | p->interned = SYMBOL_UNINTERNED; |
3207 | p->constant = 0; | |
b9598260 | 3208 | p->declared_special = 0; |
2e471eb5 GM |
3209 | consing_since_gc += sizeof (struct Lisp_Symbol); |
3210 | symbols_consed++; | |
3900d5de | 3211 | total_free_symbols--; |
7146af97 JB |
3212 | return val; |
3213 | } | |
3214 | ||
3f25e183 | 3215 | |
2e471eb5 GM |
3216 | \f |
3217 | /*********************************************************************** | |
34400008 | 3218 | Marker (Misc) Allocation |
2e471eb5 | 3219 | ***********************************************************************/ |
3f25e183 | 3220 | |
d55c12ed AS |
3221 | /* Like union Lisp_Misc, but padded so that its size is a multiple of |
3222 | the required alignment when LSB tags are used. */ | |
3223 | ||
3224 | union aligned_Lisp_Misc | |
3225 | { | |
3226 | union Lisp_Misc m; | |
bfe3e0a2 | 3227 | #if USE_LSB_TAG |
2b90362b DA |
3228 | unsigned char c[(sizeof (union Lisp_Misc) + GCALIGNMENT - 1) |
3229 | & -GCALIGNMENT]; | |
d55c12ed AS |
3230 | #endif |
3231 | }; | |
3232 | ||
2e471eb5 GM |
3233 | /* Allocation of markers and other objects that share that structure. |
3234 | Works like allocation of conses. */ | |
c0696668 | 3235 | |
2e471eb5 | 3236 | #define MARKER_BLOCK_SIZE \ |
d55c12ed | 3237 | ((1020 - sizeof (struct marker_block *)) / sizeof (union aligned_Lisp_Misc)) |
2e471eb5 GM |
3238 | |
3239 | struct marker_block | |
c0696668 | 3240 | { |
6b61353c | 3241 | /* Place `markers' first, to preserve alignment. */ |
d55c12ed | 3242 | union aligned_Lisp_Misc markers[MARKER_BLOCK_SIZE]; |
6b61353c | 3243 | struct marker_block *next; |
2e471eb5 | 3244 | }; |
c0696668 | 3245 | |
d3d47262 | 3246 | static struct marker_block *marker_block; |
fff62aa9 | 3247 | static int marker_block_index = MARKER_BLOCK_SIZE; |
c0696668 | 3248 | |
d3d47262 | 3249 | static union Lisp_Misc *marker_free_list; |
c0696668 | 3250 | |
d7a7fda3 | 3251 | /* Return a newly allocated Lisp_Misc object of specified TYPE. */ |
2e471eb5 | 3252 | |
d7a7fda3 DA |
3253 | static Lisp_Object |
3254 | allocate_misc (enum Lisp_Misc_Type type) | |
7146af97 | 3255 | { |
2e471eb5 | 3256 | Lisp_Object val; |
7146af97 | 3257 | |
dafc79fa | 3258 | MALLOC_BLOCK_INPUT; |
cfb2f32e | 3259 | |
2e471eb5 | 3260 | if (marker_free_list) |
7146af97 | 3261 | { |
2e471eb5 GM |
3262 | XSETMISC (val, marker_free_list); |
3263 | marker_free_list = marker_free_list->u_free.chain; | |
7146af97 JB |
3264 | } |
3265 | else | |
7146af97 | 3266 | { |
2e471eb5 GM |
3267 | if (marker_block_index == MARKER_BLOCK_SIZE) |
3268 | { | |
38182d90 | 3269 | struct marker_block *new = lisp_malloc (sizeof *new, MEM_TYPE_MISC); |
2e471eb5 GM |
3270 | new->next = marker_block; |
3271 | marker_block = new; | |
3272 | marker_block_index = 0; | |
7b7990cc | 3273 | total_free_markers += MARKER_BLOCK_SIZE; |
2e471eb5 | 3274 | } |
d55c12ed | 3275 | XSETMISC (val, &marker_block->markers[marker_block_index].m); |
6b61353c | 3276 | marker_block_index++; |
7146af97 | 3277 | } |
177c0ea7 | 3278 | |
dafc79fa | 3279 | MALLOC_UNBLOCK_INPUT; |
e2984df0 | 3280 | |
7b7990cc | 3281 | --total_free_markers; |
2e471eb5 GM |
3282 | consing_since_gc += sizeof (union Lisp_Misc); |
3283 | misc_objects_consed++; | |
d7a7fda3 | 3284 | XMISCTYPE (val) = type; |
67ee9f6e | 3285 | XMISCANY (val)->gcmarkbit = 0; |
2e471eb5 GM |
3286 | return val; |
3287 | } | |
3288 | ||
7b7990cc KS |
3289 | /* Free a Lisp_Misc object */ |
3290 | ||
244ed907 | 3291 | static void |
971de7fb | 3292 | free_misc (Lisp_Object misc) |
7b7990cc | 3293 | { |
d314756e | 3294 | XMISCTYPE (misc) = Lisp_Misc_Free; |
7b7990cc KS |
3295 | XMISC (misc)->u_free.chain = marker_free_list; |
3296 | marker_free_list = XMISC (misc); | |
0dd6d66d | 3297 | consing_since_gc -= sizeof (union Lisp_Misc); |
7b7990cc KS |
3298 | total_free_markers++; |
3299 | } | |
3300 | ||
42172a6b RS |
3301 | /* Return a Lisp_Misc_Save_Value object containing POINTER and |
3302 | INTEGER. This is used to package C values to call record_unwind_protect. | |
3303 | The unwind function can get the C values back using XSAVE_VALUE. */ | |
3304 | ||
3305 | Lisp_Object | |
9c4c5f81 | 3306 | make_save_value (void *pointer, ptrdiff_t integer) |
42172a6b RS |
3307 | { |
3308 | register Lisp_Object val; | |
3309 | register struct Lisp_Save_Value *p; | |
3310 | ||
d7a7fda3 | 3311 | val = allocate_misc (Lisp_Misc_Save_Value); |
42172a6b RS |
3312 | p = XSAVE_VALUE (val); |
3313 | p->pointer = pointer; | |
3314 | p->integer = integer; | |
b766f870 | 3315 | p->dogc = 0; |
42172a6b RS |
3316 | return val; |
3317 | } | |
3318 | ||
d7a7fda3 DA |
3319 | /* Return a Lisp_Misc_Overlay object with specified START, END and PLIST. */ |
3320 | ||
3321 | Lisp_Object | |
3322 | build_overlay (Lisp_Object start, Lisp_Object end, Lisp_Object plist) | |
3323 | { | |
3324 | register Lisp_Object overlay; | |
3325 | ||
3326 | overlay = allocate_misc (Lisp_Misc_Overlay); | |
3327 | OVERLAY_START (overlay) = start; | |
3328 | OVERLAY_END (overlay) = end; | |
c644523b | 3329 | set_overlay_plist (overlay, plist); |
d7a7fda3 DA |
3330 | XOVERLAY (overlay)->next = NULL; |
3331 | return overlay; | |
3332 | } | |
3333 | ||
a7ca3326 | 3334 | DEFUN ("make-marker", Fmake_marker, Smake_marker, 0, 0, 0, |
a6266d23 | 3335 | doc: /* Return a newly allocated marker which does not point at any place. */) |
5842a27b | 3336 | (void) |
2e471eb5 | 3337 | { |
eeaea515 DA |
3338 | register Lisp_Object val; |
3339 | register struct Lisp_Marker *p; | |
7146af97 | 3340 | |
eeaea515 DA |
3341 | val = allocate_misc (Lisp_Misc_Marker); |
3342 | p = XMARKER (val); | |
3343 | p->buffer = 0; | |
3344 | p->bytepos = 0; | |
3345 | p->charpos = 0; | |
3346 | p->next = NULL; | |
3347 | p->insertion_type = 0; | |
3348 | return val; | |
7146af97 | 3349 | } |
2e471eb5 | 3350 | |
657924ff DA |
3351 | /* Return a newly allocated marker which points into BUF |
3352 | at character position CHARPOS and byte position BYTEPOS. */ | |
3353 | ||
3354 | Lisp_Object | |
3355 | build_marker (struct buffer *buf, ptrdiff_t charpos, ptrdiff_t bytepos) | |
3356 | { | |
eeaea515 DA |
3357 | Lisp_Object obj; |
3358 | struct Lisp_Marker *m; | |
657924ff DA |
3359 | |
3360 | /* No dead buffers here. */ | |
e578f381 | 3361 | eassert (BUFFER_LIVE_P (buf)); |
657924ff | 3362 | |
eeaea515 DA |
3363 | /* Every character is at least one byte. */ |
3364 | eassert (charpos <= bytepos); | |
3365 | ||
3366 | obj = allocate_misc (Lisp_Misc_Marker); | |
3367 | m = XMARKER (obj); | |
3368 | m->buffer = buf; | |
3369 | m->charpos = charpos; | |
3370 | m->bytepos = bytepos; | |
3371 | m->insertion_type = 0; | |
3372 | m->next = BUF_MARKERS (buf); | |
3373 | BUF_MARKERS (buf) = m; | |
3374 | return obj; | |
657924ff DA |
3375 | } |
3376 | ||
2e471eb5 GM |
3377 | /* Put MARKER back on the free list after using it temporarily. */ |
3378 | ||
3379 | void | |
971de7fb | 3380 | free_marker (Lisp_Object marker) |
2e471eb5 | 3381 | { |
ef89c2ce | 3382 | unchain_marker (XMARKER (marker)); |
7b7990cc | 3383 | free_misc (marker); |
2e471eb5 GM |
3384 | } |
3385 | ||
c0696668 | 3386 | \f |
7146af97 | 3387 | /* Return a newly created vector or string with specified arguments as |
736471d1 RS |
3388 | elements. If all the arguments are characters that can fit |
3389 | in a string of events, make a string; otherwise, make a vector. | |
3390 | ||
3391 | Any number of arguments, even zero arguments, are allowed. */ | |
7146af97 JB |
3392 | |
3393 | Lisp_Object | |
971de7fb | 3394 | make_event_array (register int nargs, Lisp_Object *args) |
7146af97 JB |
3395 | { |
3396 | int i; | |
3397 | ||
3398 | for (i = 0; i < nargs; i++) | |
736471d1 | 3399 | /* The things that fit in a string |
c9ca4659 RS |
3400 | are characters that are in 0...127, |
3401 | after discarding the meta bit and all the bits above it. */ | |
e687453f | 3402 | if (!INTEGERP (args[i]) |
c11285dc | 3403 | || (XINT (args[i]) & ~(-CHAR_META)) >= 0200) |
7146af97 JB |
3404 | return Fvector (nargs, args); |
3405 | ||
3406 | /* Since the loop exited, we know that all the things in it are | |
3407 | characters, so we can make a string. */ | |
3408 | { | |
c13ccad2 | 3409 | Lisp_Object result; |
177c0ea7 | 3410 | |
50aee051 | 3411 | result = Fmake_string (make_number (nargs), make_number (0)); |
7146af97 | 3412 | for (i = 0; i < nargs; i++) |
736471d1 | 3413 | { |
46e7e6b0 | 3414 | SSET (result, i, XINT (args[i])); |
736471d1 RS |
3415 | /* Move the meta bit to the right place for a string char. */ |
3416 | if (XINT (args[i]) & CHAR_META) | |
46e7e6b0 | 3417 | SSET (result, i, SREF (result, i) | 0x80); |
736471d1 | 3418 | } |
177c0ea7 | 3419 | |
7146af97 JB |
3420 | return result; |
3421 | } | |
3422 | } | |
2e471eb5 GM |
3423 | |
3424 | ||
7146af97 | 3425 | \f |
24d8a105 RS |
3426 | /************************************************************************ |
3427 | Memory Full Handling | |
3428 | ************************************************************************/ | |
3429 | ||
3430 | ||
531b0165 PE |
3431 | /* Called if malloc (NBYTES) returns zero. If NBYTES == SIZE_MAX, |
3432 | there may have been size_t overflow so that malloc was never | |
3433 | called, or perhaps malloc was invoked successfully but the | |
3434 | resulting pointer had problems fitting into a tagged EMACS_INT. In | |
3435 | either case this counts as memory being full even though malloc did | |
3436 | not fail. */ | |
24d8a105 RS |
3437 | |
3438 | void | |
531b0165 | 3439 | memory_full (size_t nbytes) |
24d8a105 | 3440 | { |
531b0165 | 3441 | /* Do not go into hysterics merely because a large request failed. */ |
fce31d69 | 3442 | bool enough_free_memory = 0; |
2b6148e4 | 3443 | if (SPARE_MEMORY < nbytes) |
531b0165 | 3444 | { |
66606eea PE |
3445 | void *p; |
3446 | ||
3447 | MALLOC_BLOCK_INPUT; | |
3448 | p = malloc (SPARE_MEMORY); | |
531b0165 PE |
3449 | if (p) |
3450 | { | |
4d09bcf6 | 3451 | free (p); |
531b0165 PE |
3452 | enough_free_memory = 1; |
3453 | } | |
66606eea | 3454 | MALLOC_UNBLOCK_INPUT; |
531b0165 | 3455 | } |
24d8a105 | 3456 | |
531b0165 PE |
3457 | if (! enough_free_memory) |
3458 | { | |
3459 | int i; | |
24d8a105 | 3460 | |
531b0165 PE |
3461 | Vmemory_full = Qt; |
3462 | ||
3463 | memory_full_cons_threshold = sizeof (struct cons_block); | |
3464 | ||
3465 | /* The first time we get here, free the spare memory. */ | |
3466 | for (i = 0; i < sizeof (spare_memory) / sizeof (char *); i++) | |
3467 | if (spare_memory[i]) | |
3468 | { | |
3469 | if (i == 0) | |
3470 | free (spare_memory[i]); | |
3471 | else if (i >= 1 && i <= 4) | |
3472 | lisp_align_free (spare_memory[i]); | |
3473 | else | |
3474 | lisp_free (spare_memory[i]); | |
3475 | spare_memory[i] = 0; | |
3476 | } | |
531b0165 | 3477 | } |
24d8a105 RS |
3478 | |
3479 | /* This used to call error, but if we've run out of memory, we could | |
3480 | get infinite recursion trying to build the string. */ | |
9b306d37 | 3481 | xsignal (Qnil, Vmemory_signal_data); |
24d8a105 RS |
3482 | } |
3483 | ||
3484 | /* If we released our reserve (due to running out of memory), | |
3485 | and we have a fair amount free once again, | |
3486 | try to set aside another reserve in case we run out once more. | |
3487 | ||
3488 | This is called when a relocatable block is freed in ralloc.c, | |
3489 | and also directly from this file, in case we're not using ralloc.c. */ | |
3490 | ||
3491 | void | |
971de7fb | 3492 | refill_memory_reserve (void) |
24d8a105 RS |
3493 | { |
3494 | #ifndef SYSTEM_MALLOC | |
3495 | if (spare_memory[0] == 0) | |
38182d90 | 3496 | spare_memory[0] = malloc (SPARE_MEMORY); |
24d8a105 | 3497 | if (spare_memory[1] == 0) |
38182d90 | 3498 | spare_memory[1] = lisp_align_malloc (sizeof (struct cons_block), |
35aaa1ea | 3499 | MEM_TYPE_SPARE); |
24d8a105 | 3500 | if (spare_memory[2] == 0) |
38182d90 | 3501 | spare_memory[2] = lisp_align_malloc (sizeof (struct cons_block), |
35aaa1ea | 3502 | MEM_TYPE_SPARE); |
24d8a105 | 3503 | if (spare_memory[3] == 0) |
38182d90 | 3504 | spare_memory[3] = lisp_align_malloc (sizeof (struct cons_block), |
35aaa1ea | 3505 | MEM_TYPE_SPARE); |
24d8a105 | 3506 | if (spare_memory[4] == 0) |
38182d90 | 3507 | spare_memory[4] = lisp_align_malloc (sizeof (struct cons_block), |
35aaa1ea | 3508 | MEM_TYPE_SPARE); |
24d8a105 | 3509 | if (spare_memory[5] == 0) |
38182d90 | 3510 | spare_memory[5] = lisp_malloc (sizeof (struct string_block), |
35aaa1ea | 3511 | MEM_TYPE_SPARE); |
24d8a105 | 3512 | if (spare_memory[6] == 0) |
38182d90 | 3513 | spare_memory[6] = lisp_malloc (sizeof (struct string_block), |
35aaa1ea | 3514 | MEM_TYPE_SPARE); |
24d8a105 RS |
3515 | if (spare_memory[0] && spare_memory[1] && spare_memory[5]) |
3516 | Vmemory_full = Qnil; | |
3517 | #endif | |
3518 | } | |
3519 | \f | |
34400008 GM |
3520 | /************************************************************************ |
3521 | C Stack Marking | |
3522 | ************************************************************************/ | |
3523 | ||
13c844fb GM |
3524 | #if GC_MARK_STACK || defined GC_MALLOC_CHECK |
3525 | ||
71cf5fa0 GM |
3526 | /* Conservative C stack marking requires a method to identify possibly |
3527 | live Lisp objects given a pointer value. We do this by keeping | |
3528 | track of blocks of Lisp data that are allocated in a red-black tree | |
3529 | (see also the comment of mem_node which is the type of nodes in | |
3530 | that tree). Function lisp_malloc adds information for an allocated | |
3531 | block to the red-black tree with calls to mem_insert, and function | |
3532 | lisp_free removes it with mem_delete. Functions live_string_p etc | |
3533 | call mem_find to lookup information about a given pointer in the | |
3534 | tree, and use that to determine if the pointer points to a Lisp | |
3535 | object or not. */ | |
3536 | ||
34400008 GM |
3537 | /* Initialize this part of alloc.c. */ |
3538 | ||
3539 | static void | |
971de7fb | 3540 | mem_init (void) |
34400008 GM |
3541 | { |
3542 | mem_z.left = mem_z.right = MEM_NIL; | |
3543 | mem_z.parent = NULL; | |
3544 | mem_z.color = MEM_BLACK; | |
3545 | mem_z.start = mem_z.end = NULL; | |
3546 | mem_root = MEM_NIL; | |
3547 | } | |
3548 | ||
3549 | ||
3550 | /* Value is a pointer to the mem_node containing START. Value is | |
3551 | MEM_NIL if there is no node in the tree containing START. */ | |
3552 | ||
b0ab8123 | 3553 | static struct mem_node * |
971de7fb | 3554 | mem_find (void *start) |
34400008 GM |
3555 | { |
3556 | struct mem_node *p; | |
3557 | ||
ece93c02 GM |
3558 | if (start < min_heap_address || start > max_heap_address) |
3559 | return MEM_NIL; | |
3560 | ||
34400008 GM |
3561 | /* Make the search always successful to speed up the loop below. */ |
3562 | mem_z.start = start; | |
3563 | mem_z.end = (char *) start + 1; | |
3564 | ||
3565 | p = mem_root; | |
3566 | while (start < p->start || start >= p->end) | |
3567 | p = start < p->start ? p->left : p->right; | |
3568 | return p; | |
3569 | } | |
3570 | ||
3571 | ||
3572 | /* Insert a new node into the tree for a block of memory with start | |
3573 | address START, end address END, and type TYPE. Value is a | |
3574 | pointer to the node that was inserted. */ | |
3575 | ||
3576 | static struct mem_node * | |
971de7fb | 3577 | mem_insert (void *start, void *end, enum mem_type type) |
34400008 GM |
3578 | { |
3579 | struct mem_node *c, *parent, *x; | |
3580 | ||
add3c3ea | 3581 | if (min_heap_address == NULL || start < min_heap_address) |
ece93c02 | 3582 | min_heap_address = start; |
add3c3ea | 3583 | if (max_heap_address == NULL || end > max_heap_address) |
ece93c02 GM |
3584 | max_heap_address = end; |
3585 | ||
34400008 GM |
3586 | /* See where in the tree a node for START belongs. In this |
3587 | particular application, it shouldn't happen that a node is already | |
3588 | present. For debugging purposes, let's check that. */ | |
3589 | c = mem_root; | |
3590 | parent = NULL; | |
3591 | ||
3592 | #if GC_MARK_STACK != GC_MAKE_GCPROS_NOOPS | |
177c0ea7 | 3593 | |
34400008 GM |
3594 | while (c != MEM_NIL) |
3595 | { | |
3596 | if (start >= c->start && start < c->end) | |
1088b922 | 3597 | emacs_abort (); |
34400008 GM |
3598 | parent = c; |
3599 | c = start < c->start ? c->left : c->right; | |
3600 | } | |
177c0ea7 | 3601 | |
34400008 | 3602 | #else /* GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS */ |
177c0ea7 | 3603 | |
34400008 GM |
3604 | while (c != MEM_NIL) |
3605 | { | |
3606 | parent = c; | |
3607 | c = start < c->start ? c->left : c->right; | |
3608 | } | |
177c0ea7 | 3609 | |
34400008 GM |
3610 | #endif /* GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS */ |
3611 | ||
3612 | /* Create a new node. */ | |
877935b1 | 3613 | #ifdef GC_MALLOC_CHECK |
0caaedb1 | 3614 | x = malloc (sizeof *x); |
877935b1 | 3615 | if (x == NULL) |
1088b922 | 3616 | emacs_abort (); |
877935b1 | 3617 | #else |
23f86fce | 3618 | x = xmalloc (sizeof *x); |
877935b1 | 3619 | #endif |
34400008 GM |
3620 | x->start = start; |
3621 | x->end = end; | |
3622 | x->type = type; | |
3623 | x->parent = parent; | |
3624 | x->left = x->right = MEM_NIL; | |
3625 | x->color = MEM_RED; | |
3626 | ||
3627 | /* Insert it as child of PARENT or install it as root. */ | |
3628 | if (parent) | |
3629 | { | |
3630 | if (start < parent->start) | |
3631 | parent->left = x; | |
3632 | else | |
3633 | parent->right = x; | |
3634 | } | |
177c0ea7 | 3635 | else |
34400008 GM |
3636 | mem_root = x; |
3637 | ||
3638 | /* Re-establish red-black tree properties. */ | |
3639 | mem_insert_fixup (x); | |
877935b1 | 3640 | |
34400008 GM |
3641 | return x; |
3642 | } | |
3643 | ||
3644 | ||
3645 | /* Re-establish the red-black properties of the tree, and thereby | |
3646 | balance the tree, after node X has been inserted; X is always red. */ | |
3647 | ||
3648 | static void | |
971de7fb | 3649 | mem_insert_fixup (struct mem_node *x) |
34400008 GM |
3650 | { |
3651 | while (x != mem_root && x->parent->color == MEM_RED) | |
3652 | { | |
3653 | /* X is red and its parent is red. This is a violation of | |
3654 | red-black tree property #3. */ | |
177c0ea7 | 3655 | |
34400008 GM |
3656 | if (x->parent == x->parent->parent->left) |
3657 | { | |
3658 | /* We're on the left side of our grandparent, and Y is our | |
3659 | "uncle". */ | |
3660 | struct mem_node *y = x->parent->parent->right; | |
177c0ea7 | 3661 | |
34400008 GM |
3662 | if (y->color == MEM_RED) |
3663 | { | |
3664 | /* Uncle and parent are red but should be black because | |
3665 | X is red. Change the colors accordingly and proceed | |
3666 | with the grandparent. */ | |
3667 | x->parent->color = MEM_BLACK; | |
3668 | y->color = MEM_BLACK; | |
3669 | x->parent->parent->color = MEM_RED; | |
3670 | x = x->parent->parent; | |
3671 | } | |
3672 | else | |
3673 | { | |
3674 | /* Parent and uncle have different colors; parent is | |
3675 | red, uncle is black. */ | |
3676 | if (x == x->parent->right) | |
3677 | { | |
3678 | x = x->parent; | |
3679 | mem_rotate_left (x); | |
3680 | } | |
3681 | ||
3682 | x->parent->color = MEM_BLACK; | |
3683 | x->parent->parent->color = MEM_RED; | |
3684 | mem_rotate_right (x->parent->parent); | |
3685 | } | |
3686 | } | |
3687 | else | |
3688 | { | |
3689 | /* This is the symmetrical case of above. */ | |
3690 | struct mem_node *y = x->parent->parent->left; | |
177c0ea7 | 3691 | |
34400008 GM |
3692 | if (y->color == MEM_RED) |
3693 | { | |
3694 | x->parent->color = MEM_BLACK; | |
3695 | y->color = MEM_BLACK; | |
3696 | x->parent->parent->color = MEM_RED; | |
3697 | x = x->parent->parent; | |
3698 | } | |
3699 | else | |
3700 | { | |
3701 | if (x == x->parent->left) | |
3702 | { | |
3703 | x = x->parent; | |
3704 | mem_rotate_right (x); | |
3705 | } | |
177c0ea7 | 3706 | |
34400008 GM |
3707 | x->parent->color = MEM_BLACK; |
3708 | x->parent->parent->color = MEM_RED; | |
3709 | mem_rotate_left (x->parent->parent); | |
3710 | } | |
3711 | } | |
3712 | } | |
3713 | ||
3714 | /* The root may have been changed to red due to the algorithm. Set | |
3715 | it to black so that property #5 is satisfied. */ | |
3716 | mem_root->color = MEM_BLACK; | |
3717 | } | |
3718 | ||
3719 | ||
177c0ea7 JB |
3720 | /* (x) (y) |
3721 | / \ / \ | |
34400008 GM |
3722 | a (y) ===> (x) c |
3723 | / \ / \ | |
3724 | b c a b */ | |
3725 | ||
3726 | static void | |
971de7fb | 3727 | mem_rotate_left (struct mem_node *x) |
34400008 GM |
3728 | { |
3729 | struct mem_node *y; | |
3730 | ||
3731 | /* Turn y's left sub-tree into x's right sub-tree. */ | |
3732 | y = x->right; | |
3733 | x->right = y->left; | |
3734 | if (y->left != MEM_NIL) | |
3735 | y->left->parent = x; | |
3736 | ||
3737 | /* Y's parent was x's parent. */ | |
3738 | if (y != MEM_NIL) | |
3739 | y->parent = x->parent; | |
3740 | ||
3741 | /* Get the parent to point to y instead of x. */ | |
3742 | if (x->parent) | |
3743 | { | |
3744 | if (x == x->parent->left) | |
3745 | x->parent->left = y; | |
3746 | else | |
3747 | x->parent->right = y; | |
3748 | } | |
3749 | else | |
3750 | mem_root = y; | |
3751 | ||
3752 | /* Put x on y's left. */ | |
3753 | y->left = x; | |
3754 | if (x != MEM_NIL) | |
3755 | x->parent = y; | |
3756 | } | |
3757 | ||
3758 | ||
177c0ea7 JB |
3759 | /* (x) (Y) |
3760 | / \ / \ | |
3761 | (y) c ===> a (x) | |
3762 | / \ / \ | |
34400008 GM |
3763 | a b b c */ |
3764 | ||
3765 | static void | |
971de7fb | 3766 | mem_rotate_right (struct mem_node *x) |
34400008 GM |
3767 | { |
3768 | struct mem_node *y = x->left; | |
3769 | ||
3770 | x->left = y->right; | |
3771 | if (y->right != MEM_NIL) | |
3772 | y->right->parent = x; | |
177c0ea7 | 3773 | |
34400008 GM |
3774 | if (y != MEM_NIL) |
3775 | y->parent = x->parent; | |
3776 | if (x->parent) | |
3777 | { | |
3778 | if (x == x->parent->right) | |
3779 | x->parent->right = y; | |
3780 | else | |
3781 | x->parent->left = y; | |
3782 | } | |
3783 | else | |
3784 | mem_root = y; | |
177c0ea7 | 3785 | |
34400008 GM |
3786 | y->right = x; |
3787 | if (x != MEM_NIL) | |
3788 | x->parent = y; | |
3789 | } | |
3790 | ||
3791 | ||
3792 | /* Delete node Z from the tree. If Z is null or MEM_NIL, do nothing. */ | |
3793 | ||
3794 | static void | |
971de7fb | 3795 | mem_delete (struct mem_node *z) |
34400008 GM |
3796 | { |
3797 | struct mem_node *x, *y; | |
3798 | ||
3799 | if (!z || z == MEM_NIL) | |
3800 | return; | |
3801 | ||
3802 | if (z->left == MEM_NIL || z->right == MEM_NIL) | |
3803 | y = z; | |
3804 | else | |
3805 | { | |
3806 | y = z->right; | |
3807 | while (y->left != MEM_NIL) | |
3808 | y = y->left; | |
3809 | } | |
3810 | ||
3811 | if (y->left != MEM_NIL) | |
3812 | x = y->left; | |
3813 | else | |
3814 | x = y->right; | |
3815 | ||
3816 | x->parent = y->parent; | |
3817 | if (y->parent) | |
3818 | { | |
3819 | if (y == y->parent->left) | |
3820 | y->parent->left = x; | |
3821 | else | |
3822 | y->parent->right = x; | |
3823 | } | |
3824 | else | |
3825 | mem_root = x; | |
3826 | ||
3827 | if (y != z) | |
3828 | { | |
3829 | z->start = y->start; | |
3830 | z->end = y->end; | |
3831 | z->type = y->type; | |
3832 | } | |
177c0ea7 | 3833 | |
34400008 GM |
3834 | if (y->color == MEM_BLACK) |
3835 | mem_delete_fixup (x); | |
877935b1 GM |
3836 | |
3837 | #ifdef GC_MALLOC_CHECK | |
0caaedb1 | 3838 | free (y); |
877935b1 | 3839 | #else |
34400008 | 3840 | xfree (y); |
877935b1 | 3841 | #endif |
34400008 GM |
3842 | } |
3843 | ||
3844 | ||
3845 | /* Re-establish the red-black properties of the tree, after a | |
3846 | deletion. */ | |
3847 | ||
3848 | static void | |
971de7fb | 3849 | mem_delete_fixup (struct mem_node *x) |
34400008 GM |
3850 | { |
3851 | while (x != mem_root && x->color == MEM_BLACK) | |
3852 | { | |
3853 | if (x == x->parent->left) | |
3854 | { | |
3855 | struct mem_node *w = x->parent->right; | |
177c0ea7 | 3856 | |
34400008 GM |
3857 | if (w->color == MEM_RED) |
3858 | { | |
3859 | w->color = MEM_BLACK; | |
3860 | x->parent->color = MEM_RED; | |
3861 | mem_rotate_left (x->parent); | |
3862 | w = x->parent->right; | |
3863 | } | |
177c0ea7 | 3864 | |
34400008 GM |
3865 | if (w->left->color == MEM_BLACK && w->right->color == MEM_BLACK) |
3866 | { | |
3867 | w->color = MEM_RED; | |
3868 | x = x->parent; | |
3869 | } | |
3870 | else | |
3871 | { | |
3872 | if (w->right->color == MEM_BLACK) | |
3873 | { | |
3874 | w->left->color = MEM_BLACK; | |
3875 | w->color = MEM_RED; | |
3876 | mem_rotate_right (w); | |
3877 | w = x->parent->right; | |
3878 | } | |
3879 | w->color = x->parent->color; | |
3880 | x->parent->color = MEM_BLACK; | |
3881 | w->right->color = MEM_BLACK; | |
3882 | mem_rotate_left (x->parent); | |
3883 | x = mem_root; | |
3884 | } | |
3885 | } | |
3886 | else | |
3887 | { | |
3888 | struct mem_node *w = x->parent->left; | |
177c0ea7 | 3889 | |
34400008 GM |
3890 | if (w->color == MEM_RED) |
3891 | { | |
3892 | w->color = MEM_BLACK; | |
3893 | x->parent->color = MEM_RED; | |
3894 | mem_rotate_right (x->parent); | |
3895 | w = x->parent->left; | |
3896 | } | |
177c0ea7 | 3897 | |
34400008 GM |
3898 | if (w->right->color == MEM_BLACK && w->left->color == MEM_BLACK) |
3899 | { | |
3900 | w->color = MEM_RED; | |
3901 | x = x->parent; | |
3902 | } | |
3903 | else | |
3904 | { | |
3905 | if (w->left->color == MEM_BLACK) | |
3906 | { | |
3907 | w->right->color = MEM_BLACK; | |
3908 | w->color = MEM_RED; | |
3909 | mem_rotate_left (w); | |
3910 | w = x->parent->left; | |
3911 | } | |
177c0ea7 | 3912 | |
34400008 GM |
3913 | w->color = x->parent->color; |
3914 | x->parent->color = MEM_BLACK; | |
3915 | w->left->color = MEM_BLACK; | |
3916 | mem_rotate_right (x->parent); | |
3917 | x = mem_root; | |
3918 | } | |
3919 | } | |
3920 | } | |
177c0ea7 | 3921 | |
34400008 GM |
3922 | x->color = MEM_BLACK; |
3923 | } | |
3924 | ||
3925 | ||
3926 | /* Value is non-zero if P is a pointer to a live Lisp string on | |
3927 | the heap. M is a pointer to the mem_block for P. */ | |
3928 | ||
b0ab8123 | 3929 | static bool |
971de7fb | 3930 | live_string_p (struct mem_node *m, void *p) |
34400008 GM |
3931 | { |
3932 | if (m->type == MEM_TYPE_STRING) | |
3933 | { | |
3934 | struct string_block *b = (struct string_block *) m->start; | |
14162469 | 3935 | ptrdiff_t offset = (char *) p - (char *) &b->strings[0]; |
34400008 GM |
3936 | |
3937 | /* P must point to the start of a Lisp_String structure, and it | |
3938 | must not be on the free-list. */ | |
176bc847 GM |
3939 | return (offset >= 0 |
3940 | && offset % sizeof b->strings[0] == 0 | |
6b61353c | 3941 | && offset < (STRING_BLOCK_SIZE * sizeof b->strings[0]) |
34400008 GM |
3942 | && ((struct Lisp_String *) p)->data != NULL); |
3943 | } | |
3944 | else | |
3945 | return 0; | |
3946 | } | |
3947 | ||
3948 | ||
3949 | /* Value is non-zero if P is a pointer to a live Lisp cons on | |
3950 | the heap. M is a pointer to the mem_block for P. */ | |
3951 | ||
b0ab8123 | 3952 | static bool |
971de7fb | 3953 | live_cons_p (struct mem_node *m, void *p) |
34400008 GM |
3954 | { |
3955 | if (m->type == MEM_TYPE_CONS) | |
3956 | { | |
3957 | struct cons_block *b = (struct cons_block *) m->start; | |
14162469 | 3958 | ptrdiff_t offset = (char *) p - (char *) &b->conses[0]; |
34400008 GM |
3959 | |
3960 | /* P must point to the start of a Lisp_Cons, not be | |
3961 | one of the unused cells in the current cons block, | |
3962 | and not be on the free-list. */ | |
176bc847 GM |
3963 | return (offset >= 0 |
3964 | && offset % sizeof b->conses[0] == 0 | |
6b61353c | 3965 | && offset < (CONS_BLOCK_SIZE * sizeof b->conses[0]) |
34400008 GM |
3966 | && (b != cons_block |
3967 | || offset / sizeof b->conses[0] < cons_block_index) | |
c644523b | 3968 | && !EQ (((struct Lisp_Cons *) p)->car, Vdead)); |
34400008 GM |
3969 | } |
3970 | else | |
3971 | return 0; | |
3972 | } | |
3973 | ||
3974 | ||
3975 | /* Value is non-zero if P is a pointer to a live Lisp symbol on | |
3976 | the heap. M is a pointer to the mem_block for P. */ | |
3977 | ||
b0ab8123 | 3978 | static bool |
971de7fb | 3979 | live_symbol_p (struct mem_node *m, void *p) |
34400008 GM |
3980 | { |
3981 | if (m->type == MEM_TYPE_SYMBOL) | |
3982 | { | |
3983 | struct symbol_block *b = (struct symbol_block *) m->start; | |
14162469 | 3984 | ptrdiff_t offset = (char *) p - (char *) &b->symbols[0]; |
177c0ea7 | 3985 | |
34400008 GM |
3986 | /* P must point to the start of a Lisp_Symbol, not be |
3987 | one of the unused cells in the current symbol block, | |
3988 | and not be on the free-list. */ | |
176bc847 GM |
3989 | return (offset >= 0 |
3990 | && offset % sizeof b->symbols[0] == 0 | |
6b61353c | 3991 | && offset < (SYMBOL_BLOCK_SIZE * sizeof b->symbols[0]) |
34400008 GM |
3992 | && (b != symbol_block |
3993 | || offset / sizeof b->symbols[0] < symbol_block_index) | |
c644523b | 3994 | && !EQ (((struct Lisp_Symbol *)p)->function, Vdead)); |
34400008 GM |
3995 | } |
3996 | else | |
3997 | return 0; | |
3998 | } | |
3999 | ||
4000 | ||
4001 | /* Value is non-zero if P is a pointer to a live Lisp float on | |
4002 | the heap. M is a pointer to the mem_block for P. */ | |
4003 | ||
b0ab8123 | 4004 | static bool |
971de7fb | 4005 | live_float_p (struct mem_node *m, void *p) |
34400008 GM |
4006 | { |
4007 | if (m->type == MEM_TYPE_FLOAT) | |
4008 | { | |
4009 | struct float_block *b = (struct float_block *) m->start; | |
14162469 | 4010 | ptrdiff_t offset = (char *) p - (char *) &b->floats[0]; |
177c0ea7 | 4011 | |
ab6780cd SM |
4012 | /* P must point to the start of a Lisp_Float and not be |
4013 | one of the unused cells in the current float block. */ | |
176bc847 GM |
4014 | return (offset >= 0 |
4015 | && offset % sizeof b->floats[0] == 0 | |
6b61353c | 4016 | && offset < (FLOAT_BLOCK_SIZE * sizeof b->floats[0]) |
34400008 | 4017 | && (b != float_block |
ab6780cd | 4018 | || offset / sizeof b->floats[0] < float_block_index)); |
34400008 GM |
4019 | } |
4020 | else | |
4021 | return 0; | |
4022 | } | |
4023 | ||
4024 | ||
4025 | /* Value is non-zero if P is a pointer to a live Lisp Misc on | |
4026 | the heap. M is a pointer to the mem_block for P. */ | |
4027 | ||
b0ab8123 | 4028 | static bool |
971de7fb | 4029 | live_misc_p (struct mem_node *m, void *p) |
34400008 GM |
4030 | { |
4031 | if (m->type == MEM_TYPE_MISC) | |
4032 | { | |
4033 | struct marker_block *b = (struct marker_block *) m->start; | |
14162469 | 4034 | ptrdiff_t offset = (char *) p - (char *) &b->markers[0]; |
177c0ea7 | 4035 | |
34400008 GM |
4036 | /* P must point to the start of a Lisp_Misc, not be |
4037 | one of the unused cells in the current misc block, | |
4038 | and not be on the free-list. */ | |
176bc847 GM |
4039 | return (offset >= 0 |
4040 | && offset % sizeof b->markers[0] == 0 | |
6b61353c | 4041 | && offset < (MARKER_BLOCK_SIZE * sizeof b->markers[0]) |
34400008 GM |
4042 | && (b != marker_block |
4043 | || offset / sizeof b->markers[0] < marker_block_index) | |
d314756e | 4044 | && ((union Lisp_Misc *) p)->u_any.type != Lisp_Misc_Free); |
34400008 GM |
4045 | } |
4046 | else | |
4047 | return 0; | |
4048 | } | |
4049 | ||
4050 | ||
4051 | /* Value is non-zero if P is a pointer to a live vector-like object. | |
4052 | M is a pointer to the mem_block for P. */ | |
4053 | ||
b0ab8123 | 4054 | static bool |
971de7fb | 4055 | live_vector_p (struct mem_node *m, void *p) |
34400008 | 4056 | { |
f3372c87 DA |
4057 | if (m->type == MEM_TYPE_VECTOR_BLOCK) |
4058 | { | |
4059 | /* This memory node corresponds to a vector block. */ | |
4060 | struct vector_block *block = (struct vector_block *) m->start; | |
4061 | struct Lisp_Vector *vector = (struct Lisp_Vector *) block->data; | |
4062 | ||
4063 | /* P is in the block's allocation range. Scan the block | |
4064 | up to P and see whether P points to the start of some | |
4065 | vector which is not on a free list. FIXME: check whether | |
4066 | some allocation patterns (probably a lot of short vectors) | |
4067 | may cause a substantial overhead of this loop. */ | |
4068 | while (VECTOR_IN_BLOCK (vector, block) | |
4069 | && vector <= (struct Lisp_Vector *) p) | |
4070 | { | |
ee28be33 | 4071 | if (PSEUDOVECTOR_TYPEP (&vector->header, PVEC_FREE)) |
f3372c87 | 4072 | vector = ADVANCE (vector, (vector->header.size |
ee28be33 | 4073 | & PSEUDOVECTOR_SIZE_MASK)); |
f3372c87 DA |
4074 | else if (vector == p) |
4075 | return 1; | |
4076 | else | |
4077 | vector = ADVANCE (vector, vector->header.next.nbytes); | |
4078 | } | |
4079 | } | |
4080 | else if (m->type == MEM_TYPE_VECTORLIKE && p == m->start) | |
4081 | /* This memory node corresponds to a large vector. */ | |
4082 | return 1; | |
4083 | return 0; | |
34400008 GM |
4084 | } |
4085 | ||
4086 | ||
2336fe58 | 4087 | /* Value is non-zero if P is a pointer to a live buffer. M is a |
34400008 GM |
4088 | pointer to the mem_block for P. */ |
4089 | ||
b0ab8123 | 4090 | static bool |
971de7fb | 4091 | live_buffer_p (struct mem_node *m, void *p) |
34400008 GM |
4092 | { |
4093 | /* P must point to the start of the block, and the buffer | |
4094 | must not have been killed. */ | |
4095 | return (m->type == MEM_TYPE_BUFFER | |
4096 | && p == m->start | |
e34f7f79 | 4097 | && !NILP (((struct buffer *) p)->INTERNAL_FIELD (name))); |
34400008 GM |
4098 | } |
4099 | ||
13c844fb GM |
4100 | #endif /* GC_MARK_STACK || defined GC_MALLOC_CHECK */ |
4101 | ||
4102 | #if GC_MARK_STACK | |
4103 | ||
34400008 GM |
4104 | #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES |
4105 | ||
4106 | /* Array of objects that are kept alive because the C stack contains | |
4107 | a pattern that looks like a reference to them . */ | |
4108 | ||
4109 | #define MAX_ZOMBIES 10 | |
4110 | static Lisp_Object zombies[MAX_ZOMBIES]; | |
4111 | ||
4112 | /* Number of zombie objects. */ | |
4113 | ||
211a0b2a | 4114 | static EMACS_INT nzombies; |
34400008 GM |
4115 | |
4116 | /* Number of garbage collections. */ | |
4117 | ||
211a0b2a | 4118 | static EMACS_INT ngcs; |
34400008 GM |
4119 | |
4120 | /* Average percentage of zombies per collection. */ | |
4121 | ||
4122 | static double avg_zombies; | |
4123 | ||
4124 | /* Max. number of live and zombie objects. */ | |
4125 | ||
211a0b2a | 4126 | static EMACS_INT max_live, max_zombies; |
34400008 GM |
4127 | |
4128 | /* Average number of live objects per GC. */ | |
4129 | ||
4130 | static double avg_live; | |
4131 | ||
a7ca3326 | 4132 | DEFUN ("gc-status", Fgc_status, Sgc_status, 0, 0, "", |
7ee72033 | 4133 | doc: /* Show information about live and zombie objects. */) |
5842a27b | 4134 | (void) |
34400008 | 4135 | { |
83fc9c63 | 4136 | Lisp_Object args[8], zombie_list = Qnil; |
211a0b2a | 4137 | EMACS_INT i; |
6e4b3fbe | 4138 | for (i = 0; i < min (MAX_ZOMBIES, nzombies); i++) |
83fc9c63 DL |
4139 | zombie_list = Fcons (zombies[i], zombie_list); |
4140 | args[0] = build_string ("%d GCs, avg live/zombies = %.2f/%.2f (%f%%), max %d/%d\nzombies: %S"); | |
34400008 GM |
4141 | args[1] = make_number (ngcs); |
4142 | args[2] = make_float (avg_live); | |
4143 | args[3] = make_float (avg_zombies); | |
4144 | args[4] = make_float (avg_zombies / avg_live / 100); | |
4145 | args[5] = make_number (max_live); | |
4146 | args[6] = make_number (max_zombies); | |
83fc9c63 DL |
4147 | args[7] = zombie_list; |
4148 | return Fmessage (8, args); | |
34400008 GM |
4149 | } |
4150 | ||
4151 | #endif /* GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES */ | |
4152 | ||
4153 | ||
182ff242 GM |
4154 | /* Mark OBJ if we can prove it's a Lisp_Object. */ |
4155 | ||
b0ab8123 | 4156 | static void |
971de7fb | 4157 | mark_maybe_object (Lisp_Object obj) |
182ff242 | 4158 | { |
b609f591 YM |
4159 | void *po; |
4160 | struct mem_node *m; | |
4161 | ||
4162 | if (INTEGERP (obj)) | |
4163 | return; | |
4164 | ||
4165 | po = (void *) XPNTR (obj); | |
4166 | m = mem_find (po); | |
177c0ea7 | 4167 | |
182ff242 GM |
4168 | if (m != MEM_NIL) |
4169 | { | |
fce31d69 | 4170 | bool mark_p = 0; |
182ff242 | 4171 | |
8e50cc2d | 4172 | switch (XTYPE (obj)) |
182ff242 GM |
4173 | { |
4174 | case Lisp_String: | |
4175 | mark_p = (live_string_p (m, po) | |
4176 | && !STRING_MARKED_P ((struct Lisp_String *) po)); | |
4177 | break; | |
4178 | ||
4179 | case Lisp_Cons: | |
08b7c2cb | 4180 | mark_p = (live_cons_p (m, po) && !CONS_MARKED_P (XCONS (obj))); |
182ff242 GM |
4181 | break; |
4182 | ||
4183 | case Lisp_Symbol: | |
2336fe58 | 4184 | mark_p = (live_symbol_p (m, po) && !XSYMBOL (obj)->gcmarkbit); |
182ff242 GM |
4185 | break; |
4186 | ||
4187 | case Lisp_Float: | |
ab6780cd | 4188 | mark_p = (live_float_p (m, po) && !FLOAT_MARKED_P (XFLOAT (obj))); |
182ff242 GM |
4189 | break; |
4190 | ||
4191 | case Lisp_Vectorlike: | |
8e50cc2d | 4192 | /* Note: can't check BUFFERP before we know it's a |
182ff242 GM |
4193 | buffer because checking that dereferences the pointer |
4194 | PO which might point anywhere. */ | |
4195 | if (live_vector_p (m, po)) | |
8e50cc2d | 4196 | mark_p = !SUBRP (obj) && !VECTOR_MARKED_P (XVECTOR (obj)); |
182ff242 | 4197 | else if (live_buffer_p (m, po)) |
8e50cc2d | 4198 | mark_p = BUFFERP (obj) && !VECTOR_MARKED_P (XBUFFER (obj)); |
182ff242 GM |
4199 | break; |
4200 | ||
4201 | case Lisp_Misc: | |
67ee9f6e | 4202 | mark_p = (live_misc_p (m, po) && !XMISCANY (obj)->gcmarkbit); |
182ff242 | 4203 | break; |
6bbd7a29 | 4204 | |
2de9f71c | 4205 | default: |
6bbd7a29 | 4206 | break; |
182ff242 GM |
4207 | } |
4208 | ||
4209 | if (mark_p) | |
4210 | { | |
4211 | #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES | |
4212 | if (nzombies < MAX_ZOMBIES) | |
83fc9c63 | 4213 | zombies[nzombies] = obj; |
182ff242 GM |
4214 | ++nzombies; |
4215 | #endif | |
49723c04 | 4216 | mark_object (obj); |
182ff242 GM |
4217 | } |
4218 | } | |
4219 | } | |
ece93c02 GM |
4220 | |
4221 | ||
4222 | /* If P points to Lisp data, mark that as live if it isn't already | |
4223 | marked. */ | |
4224 | ||
b0ab8123 | 4225 | static void |
971de7fb | 4226 | mark_maybe_pointer (void *p) |
ece93c02 GM |
4227 | { |
4228 | struct mem_node *m; | |
4229 | ||
bfe3e0a2 | 4230 | /* Quickly rule out some values which can't point to Lisp data. |
2b90362b | 4231 | USE_LSB_TAG needs Lisp data to be aligned on multiples of GCALIGNMENT. |
bfe3e0a2 | 4232 | Otherwise, assume that Lisp data is aligned on even addresses. */ |
2b90362b | 4233 | if ((intptr_t) p % (USE_LSB_TAG ? GCALIGNMENT : 2)) |
ece93c02 | 4234 | return; |
177c0ea7 | 4235 | |
ece93c02 GM |
4236 | m = mem_find (p); |
4237 | if (m != MEM_NIL) | |
4238 | { | |
4239 | Lisp_Object obj = Qnil; | |
177c0ea7 | 4240 | |
ece93c02 GM |
4241 | switch (m->type) |
4242 | { | |
4243 | case MEM_TYPE_NON_LISP: | |
5474c384 | 4244 | case MEM_TYPE_SPARE: |
2fe50224 | 4245 | /* Nothing to do; not a pointer to Lisp memory. */ |
ece93c02 | 4246 | break; |
177c0ea7 | 4247 | |
ece93c02 | 4248 | case MEM_TYPE_BUFFER: |
5e617bc2 | 4249 | if (live_buffer_p (m, p) && !VECTOR_MARKED_P ((struct buffer *)p)) |
ece93c02 GM |
4250 | XSETVECTOR (obj, p); |
4251 | break; | |
177c0ea7 | 4252 | |
ece93c02 | 4253 | case MEM_TYPE_CONS: |
08b7c2cb | 4254 | if (live_cons_p (m, p) && !CONS_MARKED_P ((struct Lisp_Cons *) p)) |
ece93c02 GM |
4255 | XSETCONS (obj, p); |
4256 | break; | |
177c0ea7 | 4257 | |
ece93c02 GM |
4258 | case MEM_TYPE_STRING: |
4259 | if (live_string_p (m, p) | |
4260 | && !STRING_MARKED_P ((struct Lisp_String *) p)) | |
4261 | XSETSTRING (obj, p); | |
4262 | break; | |
4263 | ||
4264 | case MEM_TYPE_MISC: | |
2336fe58 SM |
4265 | if (live_misc_p (m, p) && !((struct Lisp_Free *) p)->gcmarkbit) |
4266 | XSETMISC (obj, p); | |
ece93c02 | 4267 | break; |
177c0ea7 | 4268 | |
ece93c02 | 4269 | case MEM_TYPE_SYMBOL: |
2336fe58 | 4270 | if (live_symbol_p (m, p) && !((struct Lisp_Symbol *) p)->gcmarkbit) |
ece93c02 GM |
4271 | XSETSYMBOL (obj, p); |
4272 | break; | |
177c0ea7 | 4273 | |
ece93c02 | 4274 | case MEM_TYPE_FLOAT: |
ab6780cd | 4275 | if (live_float_p (m, p) && !FLOAT_MARKED_P (p)) |
ece93c02 GM |
4276 | XSETFLOAT (obj, p); |
4277 | break; | |
177c0ea7 | 4278 | |
9c545a55 | 4279 | case MEM_TYPE_VECTORLIKE: |
f3372c87 | 4280 | case MEM_TYPE_VECTOR_BLOCK: |
ece93c02 GM |
4281 | if (live_vector_p (m, p)) |
4282 | { | |
4283 | Lisp_Object tem; | |
4284 | XSETVECTOR (tem, p); | |
8e50cc2d | 4285 | if (!SUBRP (tem) && !VECTOR_MARKED_P (XVECTOR (tem))) |
ece93c02 GM |
4286 | obj = tem; |
4287 | } | |
4288 | break; | |
4289 | ||
4290 | default: | |
1088b922 | 4291 | emacs_abort (); |
ece93c02 GM |
4292 | } |
4293 | ||
8e50cc2d | 4294 | if (!NILP (obj)) |
49723c04 | 4295 | mark_object (obj); |
ece93c02 GM |
4296 | } |
4297 | } | |
4298 | ||
4299 | ||
e32a5799 | 4300 | /* Alignment of pointer values. Use alignof, as it sometimes returns |
e3fb2efb PE |
4301 | a smaller alignment than GCC's __alignof__ and mark_memory might |
4302 | miss objects if __alignof__ were used. */ | |
e32a5799 | 4303 | #define GC_POINTER_ALIGNMENT alignof (void *) |
3164aeac | 4304 | |
e3fb2efb PE |
4305 | /* Define POINTERS_MIGHT_HIDE_IN_OBJECTS to 1 if marking via C pointers does |
4306 | not suffice, which is the typical case. A host where a Lisp_Object is | |
4307 | wider than a pointer might allocate a Lisp_Object in non-adjacent halves. | |
4308 | If USE_LSB_TAG, the bottom half is not a valid pointer, but it should | |
4309 | suffice to widen it to to a Lisp_Object and check it that way. */ | |
bfe3e0a2 PE |
4310 | #if USE_LSB_TAG || VAL_MAX < UINTPTR_MAX |
4311 | # if !USE_LSB_TAG && VAL_MAX < UINTPTR_MAX >> GCTYPEBITS | |
e3fb2efb PE |
4312 | /* If tag bits straddle pointer-word boundaries, neither mark_maybe_pointer |
4313 | nor mark_maybe_object can follow the pointers. This should not occur on | |
4314 | any practical porting target. */ | |
4315 | # error "MSB type bits straddle pointer-word boundaries" | |
4316 | # endif | |
4317 | /* Marking via C pointers does not suffice, because Lisp_Objects contain | |
4318 | pointer words that hold pointers ORed with type bits. */ | |
4319 | # define POINTERS_MIGHT_HIDE_IN_OBJECTS 1 | |
4320 | #else | |
4321 | /* Marking via C pointers suffices, because Lisp_Objects contain pointer | |
4322 | words that hold unmodified pointers. */ | |
4323 | # define POINTERS_MIGHT_HIDE_IN_OBJECTS 0 | |
4324 | #endif | |
4325 | ||
55a314a5 YM |
4326 | /* Mark Lisp objects referenced from the address range START+OFFSET..END |
4327 | or END+OFFSET..START. */ | |
34400008 | 4328 | |
177c0ea7 | 4329 | static void |
3164aeac | 4330 | mark_memory (void *start, void *end) |
b41253a3 JW |
4331 | #if defined (__clang__) && defined (__has_feature) |
4332 | #if __has_feature(address_sanitizer) | |
ed6b3510 JW |
4333 | /* Do not allow -faddress-sanitizer to check this function, since it |
4334 | crosses the function stack boundary, and thus would yield many | |
4335 | false positives. */ | |
4336 | __attribute__((no_address_safety_analysis)) | |
4337 | #endif | |
b41253a3 | 4338 | #endif |
34400008 | 4339 | { |
ece93c02 | 4340 | void **pp; |
3164aeac | 4341 | int i; |
34400008 GM |
4342 | |
4343 | #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES | |
4344 | nzombies = 0; | |
4345 | #endif | |
4346 | ||
4347 | /* Make START the pointer to the start of the memory region, | |
4348 | if it isn't already. */ | |
4349 | if (end < start) | |
4350 | { | |
4351 | void *tem = start; | |
4352 | start = end; | |
4353 | end = tem; | |
4354 | } | |
ece93c02 | 4355 | |
ece93c02 GM |
4356 | /* Mark Lisp data pointed to. This is necessary because, in some |
4357 | situations, the C compiler optimizes Lisp objects away, so that | |
4358 | only a pointer to them remains. Example: | |
4359 | ||
4360 | DEFUN ("testme", Ftestme, Stestme, 0, 0, 0, "") | |
7ee72033 | 4361 | () |
ece93c02 GM |
4362 | { |
4363 | Lisp_Object obj = build_string ("test"); | |
4364 | struct Lisp_String *s = XSTRING (obj); | |
4365 | Fgarbage_collect (); | |
4366 | fprintf (stderr, "test `%s'\n", s->data); | |
4367 | return Qnil; | |
4368 | } | |
4369 | ||
4370 | Here, `obj' isn't really used, and the compiler optimizes it | |
4371 | away. The only reference to the life string is through the | |
4372 | pointer `s'. */ | |
177c0ea7 | 4373 | |
3164aeac PE |
4374 | for (pp = start; (void *) pp < end; pp++) |
4375 | for (i = 0; i < sizeof *pp; i += GC_POINTER_ALIGNMENT) | |
27f3c637 | 4376 | { |
e3fb2efb PE |
4377 | void *p = *(void **) ((char *) pp + i); |
4378 | mark_maybe_pointer (p); | |
4379 | if (POINTERS_MIGHT_HIDE_IN_OBJECTS) | |
646b5f55 | 4380 | mark_maybe_object (XIL ((intptr_t) p)); |
27f3c637 | 4381 | } |
182ff242 GM |
4382 | } |
4383 | ||
30f637f8 DL |
4384 | /* setjmp will work with GCC unless NON_SAVING_SETJMP is defined in |
4385 | the GCC system configuration. In gcc 3.2, the only systems for | |
4386 | which this is so are i386-sco5 non-ELF, i386-sysv3 (maybe included | |
4387 | by others?) and ns32k-pc532-min. */ | |
182ff242 GM |
4388 | |
4389 | #if !defined GC_SAVE_REGISTERS_ON_STACK && !defined GC_SETJMP_WORKS | |
4390 | ||
fce31d69 PE |
4391 | static bool setjmp_tested_p; |
4392 | static int longjmps_done; | |
182ff242 GM |
4393 | |
4394 | #define SETJMP_WILL_LIKELY_WORK "\ | |
4395 | \n\ | |
4396 | Emacs garbage collector has been changed to use conservative stack\n\ | |
4397 | marking. Emacs has determined that the method it uses to do the\n\ | |
4398 | marking will likely work on your system, but this isn't sure.\n\ | |
4399 | \n\ | |
4400 | If you are a system-programmer, or can get the help of a local wizard\n\ | |
4401 | who is, please take a look at the function mark_stack in alloc.c, and\n\ | |
4402 | verify that the methods used are appropriate for your system.\n\ | |
4403 | \n\ | |
d191623b | 4404 | Please mail the result to <emacs-devel@gnu.org>.\n\ |
182ff242 GM |
4405 | " |
4406 | ||
4407 | #define SETJMP_WILL_NOT_WORK "\ | |
4408 | \n\ | |
4409 | Emacs garbage collector has been changed to use conservative stack\n\ | |
4410 | marking. Emacs has determined that the default method it uses to do the\n\ | |
4411 | marking will not work on your system. We will need a system-dependent\n\ | |
4412 | solution for your system.\n\ | |
4413 | \n\ | |
4414 | Please take a look at the function mark_stack in alloc.c, and\n\ | |
4415 | try to find a way to make it work on your system.\n\ | |
30f637f8 DL |
4416 | \n\ |
4417 | Note that you may get false negatives, depending on the compiler.\n\ | |
4418 | In particular, you need to use -O with GCC for this test.\n\ | |
4419 | \n\ | |
d191623b | 4420 | Please mail the result to <emacs-devel@gnu.org>.\n\ |
182ff242 GM |
4421 | " |
4422 | ||
4423 | ||
4424 | /* Perform a quick check if it looks like setjmp saves registers in a | |
4425 | jmp_buf. Print a message to stderr saying so. When this test | |
4426 | succeeds, this is _not_ a proof that setjmp is sufficient for | |
4427 | conservative stack marking. Only the sources or a disassembly | |
4428 | can prove that. */ | |
4429 | ||
4430 | static void | |
2018939f | 4431 | test_setjmp (void) |
182ff242 GM |
4432 | { |
4433 | char buf[10]; | |
4434 | register int x; | |
0328b6de | 4435 | sys_jmp_buf jbuf; |
182ff242 GM |
4436 | |
4437 | /* Arrange for X to be put in a register. */ | |
4438 | sprintf (buf, "1"); | |
4439 | x = strlen (buf); | |
4440 | x = 2 * x - 1; | |
4441 | ||
0328b6de | 4442 | sys_setjmp (jbuf); |
182ff242 | 4443 | if (longjmps_done == 1) |
34400008 | 4444 | { |
182ff242 | 4445 | /* Came here after the longjmp at the end of the function. |
34400008 | 4446 | |
182ff242 GM |
4447 | If x == 1, the longjmp has restored the register to its |
4448 | value before the setjmp, and we can hope that setjmp | |
4449 | saves all such registers in the jmp_buf, although that | |
4450 | isn't sure. | |
34400008 | 4451 | |
182ff242 GM |
4452 | For other values of X, either something really strange is |
4453 | taking place, or the setjmp just didn't save the register. */ | |
4454 | ||
4455 | if (x == 1) | |
4456 | fprintf (stderr, SETJMP_WILL_LIKELY_WORK); | |
4457 | else | |
4458 | { | |
4459 | fprintf (stderr, SETJMP_WILL_NOT_WORK); | |
4460 | exit (1); | |
34400008 GM |
4461 | } |
4462 | } | |
182ff242 GM |
4463 | |
4464 | ++longjmps_done; | |
4465 | x = 2; | |
4466 | if (longjmps_done == 1) | |
0328b6de | 4467 | sys_longjmp (jbuf, 1); |
34400008 GM |
4468 | } |
4469 | ||
182ff242 GM |
4470 | #endif /* not GC_SAVE_REGISTERS_ON_STACK && not GC_SETJMP_WORKS */ |
4471 | ||
34400008 GM |
4472 | |
4473 | #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS | |
4474 | ||
4475 | /* Abort if anything GCPRO'd doesn't survive the GC. */ | |
4476 | ||
4477 | static void | |
2018939f | 4478 | check_gcpros (void) |
34400008 GM |
4479 | { |
4480 | struct gcpro *p; | |
f66c7cf8 | 4481 | ptrdiff_t i; |
34400008 GM |
4482 | |
4483 | for (p = gcprolist; p; p = p->next) | |
4484 | for (i = 0; i < p->nvars; ++i) | |
4485 | if (!survives_gc_p (p->var[i])) | |
92cc28b2 SM |
4486 | /* FIXME: It's not necessarily a bug. It might just be that the |
4487 | GCPRO is unnecessary or should release the object sooner. */ | |
1088b922 | 4488 | emacs_abort (); |
34400008 GM |
4489 | } |
4490 | ||
4491 | #elif GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES | |
4492 | ||
4493 | static void | |
2018939f | 4494 | dump_zombies (void) |
34400008 GM |
4495 | { |
4496 | int i; | |
4497 | ||
6e4b3fbe | 4498 | fprintf (stderr, "\nZombies kept alive = %"pI"d:\n", nzombies); |
34400008 GM |
4499 | for (i = 0; i < min (MAX_ZOMBIES, nzombies); ++i) |
4500 | { | |
4501 | fprintf (stderr, " %d = ", i); | |
4502 | debug_print (zombies[i]); | |
4503 | } | |
4504 | } | |
4505 | ||
4506 | #endif /* GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES */ | |
4507 | ||
4508 | ||
182ff242 GM |
4509 | /* Mark live Lisp objects on the C stack. |
4510 | ||
4511 | There are several system-dependent problems to consider when | |
4512 | porting this to new architectures: | |
4513 | ||
4514 | Processor Registers | |
4515 | ||
4516 | We have to mark Lisp objects in CPU registers that can hold local | |
4517 | variables or are used to pass parameters. | |
4518 | ||
4519 | If GC_SAVE_REGISTERS_ON_STACK is defined, it should expand to | |
4520 | something that either saves relevant registers on the stack, or | |
4521 | calls mark_maybe_object passing it each register's contents. | |
4522 | ||
4523 | If GC_SAVE_REGISTERS_ON_STACK is not defined, the current | |
4524 | implementation assumes that calling setjmp saves registers we need | |
4525 | to see in a jmp_buf which itself lies on the stack. This doesn't | |
4526 | have to be true! It must be verified for each system, possibly | |
4527 | by taking a look at the source code of setjmp. | |
4528 | ||
2018939f AS |
4529 | If __builtin_unwind_init is available (defined by GCC >= 2.8) we |
4530 | can use it as a machine independent method to store all registers | |
4531 | to the stack. In this case the macros described in the previous | |
4532 | two paragraphs are not used. | |
4533 | ||
182ff242 GM |
4534 | Stack Layout |
4535 | ||
4536 | Architectures differ in the way their processor stack is organized. | |
4537 | For example, the stack might look like this | |
4538 | ||
4539 | +----------------+ | |
4540 | | Lisp_Object | size = 4 | |
4541 | +----------------+ | |
4542 | | something else | size = 2 | |
4543 | +----------------+ | |
4544 | | Lisp_Object | size = 4 | |
4545 | +----------------+ | |
4546 | | ... | | |
4547 | ||
4548 | In such a case, not every Lisp_Object will be aligned equally. To | |
4549 | find all Lisp_Object on the stack it won't be sufficient to walk | |
4550 | the stack in steps of 4 bytes. Instead, two passes will be | |
4551 | necessary, one starting at the start of the stack, and a second | |
4552 | pass starting at the start of the stack + 2. Likewise, if the | |
4553 | minimal alignment of Lisp_Objects on the stack is 1, four passes | |
4554 | would be necessary, each one starting with one byte more offset | |
c9af454e | 4555 | from the stack start. */ |
34400008 GM |
4556 | |
4557 | static void | |
971de7fb | 4558 | mark_stack (void) |
34400008 | 4559 | { |
34400008 GM |
4560 | void *end; |
4561 | ||
2018939f AS |
4562 | #ifdef HAVE___BUILTIN_UNWIND_INIT |
4563 | /* Force callee-saved registers and register windows onto the stack. | |
4564 | This is the preferred method if available, obviating the need for | |
4565 | machine dependent methods. */ | |
4566 | __builtin_unwind_init (); | |
4567 | end = &end; | |
4568 | #else /* not HAVE___BUILTIN_UNWIND_INIT */ | |
dff45157 PE |
4569 | #ifndef GC_SAVE_REGISTERS_ON_STACK |
4570 | /* jmp_buf may not be aligned enough on darwin-ppc64 */ | |
4571 | union aligned_jmpbuf { | |
4572 | Lisp_Object o; | |
0328b6de | 4573 | sys_jmp_buf j; |
dff45157 | 4574 | } j; |
fce31d69 | 4575 | volatile bool stack_grows_down_p = (char *) &j > (char *) stack_base; |
dff45157 | 4576 | #endif |
34400008 GM |
4577 | /* This trick flushes the register windows so that all the state of |
4578 | the process is contained in the stack. */ | |
ab6780cd | 4579 | /* Fixme: Code in the Boehm GC suggests flushing (with `flushrs') is |
422eec7e DL |
4580 | needed on ia64 too. See mach_dep.c, where it also says inline |
4581 | assembler doesn't work with relevant proprietary compilers. */ | |
4a00783e | 4582 | #ifdef __sparc__ |
4d18a7a2 DN |
4583 | #if defined (__sparc64__) && defined (__FreeBSD__) |
4584 | /* FreeBSD does not have a ta 3 handler. */ | |
4c1616be CY |
4585 | asm ("flushw"); |
4586 | #else | |
34400008 | 4587 | asm ("ta 3"); |
4c1616be | 4588 | #endif |
34400008 | 4589 | #endif |
177c0ea7 | 4590 | |
34400008 GM |
4591 | /* Save registers that we need to see on the stack. We need to see |
4592 | registers used to hold register variables and registers used to | |
4593 | pass parameters. */ | |
4594 | #ifdef GC_SAVE_REGISTERS_ON_STACK | |
4595 | GC_SAVE_REGISTERS_ON_STACK (end); | |
182ff242 | 4596 | #else /* not GC_SAVE_REGISTERS_ON_STACK */ |
177c0ea7 | 4597 | |
182ff242 GM |
4598 | #ifndef GC_SETJMP_WORKS /* If it hasn't been checked yet that |
4599 | setjmp will definitely work, test it | |
4600 | and print a message with the result | |
4601 | of the test. */ | |
4602 | if (!setjmp_tested_p) | |
4603 | { | |
4604 | setjmp_tested_p = 1; | |
4605 | test_setjmp (); | |
4606 | } | |
4607 | #endif /* GC_SETJMP_WORKS */ | |
177c0ea7 | 4608 | |
0328b6de | 4609 | sys_setjmp (j.j); |
34400008 | 4610 | end = stack_grows_down_p ? (char *) &j + sizeof j : (char *) &j; |
182ff242 | 4611 | #endif /* not GC_SAVE_REGISTERS_ON_STACK */ |
2018939f | 4612 | #endif /* not HAVE___BUILTIN_UNWIND_INIT */ |
34400008 GM |
4613 | |
4614 | /* This assumes that the stack is a contiguous region in memory. If | |
182ff242 GM |
4615 | that's not the case, something has to be done here to iterate |
4616 | over the stack segments. */ | |
3164aeac PE |
4617 | mark_memory (stack_base, end); |
4618 | ||
4dec23ff AS |
4619 | /* Allow for marking a secondary stack, like the register stack on the |
4620 | ia64. */ | |
4621 | #ifdef GC_MARK_SECONDARY_STACK | |
4622 | GC_MARK_SECONDARY_STACK (); | |
4623 | #endif | |
34400008 GM |
4624 | |
4625 | #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS | |
4626 | check_gcpros (); | |
4627 | #endif | |
4628 | } | |
4629 | ||
34400008 GM |
4630 | #endif /* GC_MARK_STACK != 0 */ |
4631 | ||
4632 | ||
7ffb6955 | 4633 | /* Determine whether it is safe to access memory at address P. */ |
d3d47262 | 4634 | static int |
971de7fb | 4635 | valid_pointer_p (void *p) |
7ffb6955 | 4636 | { |
f892cf9c EZ |
4637 | #ifdef WINDOWSNT |
4638 | return w32_valid_pointer_p (p, 16); | |
4639 | #else | |
41bed37d | 4640 | int fd[2]; |
7ffb6955 KS |
4641 | |
4642 | /* Obviously, we cannot just access it (we would SEGV trying), so we | |
4643 | trick the o/s to tell us whether p is a valid pointer. | |
4644 | Unfortunately, we cannot use NULL_DEVICE here, as emacs_write may | |
4645 | not validate p in that case. */ | |
4646 | ||
41bed37d | 4647 | if (pipe (fd) == 0) |
7ffb6955 | 4648 | { |
fce31d69 | 4649 | bool valid = emacs_write (fd[1], (char *) p, 16) == 16; |
41bed37d PE |
4650 | emacs_close (fd[1]); |
4651 | emacs_close (fd[0]); | |
7ffb6955 KS |
4652 | return valid; |
4653 | } | |
4654 | ||
4655 | return -1; | |
f892cf9c | 4656 | #endif |
7ffb6955 | 4657 | } |
3cd55735 | 4658 | |
c1ca42ca DA |
4659 | /* Return 2 if OBJ is a killed or special buffer object. |
4660 | Return 1 if OBJ is a valid lisp object. | |
3cd55735 KS |
4661 | Return 0 if OBJ is NOT a valid lisp object. |
4662 | Return -1 if we cannot validate OBJ. | |
7c0ab7d9 RS |
4663 | This function can be quite slow, |
4664 | so it should only be used in code for manual debugging. */ | |
3cd55735 KS |
4665 | |
4666 | int | |
971de7fb | 4667 | valid_lisp_object_p (Lisp_Object obj) |
3cd55735 | 4668 | { |
de7124a7 | 4669 | void *p; |
7ffb6955 | 4670 | #if GC_MARK_STACK |
3cd55735 | 4671 | struct mem_node *m; |
de7124a7 | 4672 | #endif |
3cd55735 KS |
4673 | |
4674 | if (INTEGERP (obj)) | |
4675 | return 1; | |
4676 | ||
4677 | p = (void *) XPNTR (obj); | |
3cd55735 KS |
4678 | if (PURE_POINTER_P (p)) |
4679 | return 1; | |
4680 | ||
c1ca42ca DA |
4681 | if (p == &buffer_defaults || p == &buffer_local_symbols) |
4682 | return 2; | |
4683 | ||
de7124a7 | 4684 | #if !GC_MARK_STACK |
7ffb6955 | 4685 | return valid_pointer_p (p); |
de7124a7 KS |
4686 | #else |
4687 | ||
3cd55735 KS |
4688 | m = mem_find (p); |
4689 | ||
4690 | if (m == MEM_NIL) | |
7ffb6955 KS |
4691 | { |
4692 | int valid = valid_pointer_p (p); | |
4693 | if (valid <= 0) | |
4694 | return valid; | |
4695 | ||
4696 | if (SUBRP (obj)) | |
4697 | return 1; | |
4698 | ||
4699 | return 0; | |
4700 | } | |
3cd55735 KS |
4701 | |
4702 | switch (m->type) | |
4703 | { | |
4704 | case MEM_TYPE_NON_LISP: | |
5474c384 | 4705 | case MEM_TYPE_SPARE: |
3cd55735 KS |
4706 | return 0; |
4707 | ||
4708 | case MEM_TYPE_BUFFER: | |
c1ca42ca | 4709 | return live_buffer_p (m, p) ? 1 : 2; |
3cd55735 KS |
4710 | |
4711 | case MEM_TYPE_CONS: | |
4712 | return live_cons_p (m, p); | |
4713 | ||
4714 | case MEM_TYPE_STRING: | |
4715 | return live_string_p (m, p); | |
4716 | ||
4717 | case MEM_TYPE_MISC: | |
4718 | return live_misc_p (m, p); | |
4719 | ||
4720 | case MEM_TYPE_SYMBOL: | |
4721 | return live_symbol_p (m, p); | |
4722 | ||
4723 | case MEM_TYPE_FLOAT: | |
4724 | return live_float_p (m, p); | |
4725 | ||
9c545a55 | 4726 | case MEM_TYPE_VECTORLIKE: |
f3372c87 | 4727 | case MEM_TYPE_VECTOR_BLOCK: |
3cd55735 KS |
4728 | return live_vector_p (m, p); |
4729 | ||
4730 | default: | |
4731 | break; | |
4732 | } | |
4733 | ||
4734 | return 0; | |
4735 | #endif | |
4736 | } | |
4737 | ||
4738 | ||
4739 | ||
34400008 | 4740 | \f |
2e471eb5 GM |
4741 | /*********************************************************************** |
4742 | Pure Storage Management | |
4743 | ***********************************************************************/ | |
4744 | ||
1f0b3fd2 GM |
4745 | /* Allocate room for SIZE bytes from pure Lisp storage and return a |
4746 | pointer to it. TYPE is the Lisp type for which the memory is | |
e5bc14d4 | 4747 | allocated. TYPE < 0 means it's not used for a Lisp object. */ |
1f0b3fd2 | 4748 | |
261cb4bb | 4749 | static void * |
971de7fb | 4750 | pure_alloc (size_t size, int type) |
1f0b3fd2 | 4751 | { |
261cb4bb | 4752 | void *result; |
bfe3e0a2 | 4753 | #if USE_LSB_TAG |
2b90362b | 4754 | size_t alignment = GCALIGNMENT; |
6b61353c | 4755 | #else |
e32a5799 | 4756 | size_t alignment = alignof (EMACS_INT); |
1f0b3fd2 GM |
4757 | |
4758 | /* Give Lisp_Floats an extra alignment. */ | |
4759 | if (type == Lisp_Float) | |
e32a5799 | 4760 | alignment = alignof (struct Lisp_Float); |
6b61353c | 4761 | #endif |
1f0b3fd2 | 4762 | |
44117420 | 4763 | again: |
e5bc14d4 YM |
4764 | if (type >= 0) |
4765 | { | |
4766 | /* Allocate space for a Lisp object from the beginning of the free | |
4767 | space with taking account of alignment. */ | |
4768 | result = ALIGN (purebeg + pure_bytes_used_lisp, alignment); | |
4769 | pure_bytes_used_lisp = ((char *)result - (char *)purebeg) + size; | |
4770 | } | |
4771 | else | |
4772 | { | |
4773 | /* Allocate space for a non-Lisp object from the end of the free | |
4774 | space. */ | |
4775 | pure_bytes_used_non_lisp += size; | |
4776 | result = purebeg + pure_size - pure_bytes_used_non_lisp; | |
4777 | } | |
4778 | pure_bytes_used = pure_bytes_used_lisp + pure_bytes_used_non_lisp; | |
44117420 KS |
4779 | |
4780 | if (pure_bytes_used <= pure_size) | |
4781 | return result; | |
4782 | ||
4783 | /* Don't allocate a large amount here, | |
4784 | because it might get mmap'd and then its address | |
4785 | might not be usable. */ | |
23f86fce | 4786 | purebeg = xmalloc (10000); |
44117420 KS |
4787 | pure_size = 10000; |
4788 | pure_bytes_used_before_overflow += pure_bytes_used - size; | |
4789 | pure_bytes_used = 0; | |
e5bc14d4 | 4790 | pure_bytes_used_lisp = pure_bytes_used_non_lisp = 0; |
44117420 | 4791 | goto again; |
1f0b3fd2 GM |
4792 | } |
4793 | ||
4794 | ||
852f8cdc | 4795 | /* Print a warning if PURESIZE is too small. */ |
9e713715 GM |
4796 | |
4797 | void | |
971de7fb | 4798 | check_pure_size (void) |
9e713715 GM |
4799 | { |
4800 | if (pure_bytes_used_before_overflow) | |
c2982e87 PE |
4801 | message (("emacs:0:Pure Lisp storage overflow (approx. %"pI"d" |
4802 | " bytes needed)"), | |
4803 | pure_bytes_used + pure_bytes_used_before_overflow); | |
9e713715 GM |
4804 | } |
4805 | ||
4806 | ||
79fd0489 YM |
4807 | /* Find the byte sequence {DATA[0], ..., DATA[NBYTES-1], '\0'} from |
4808 | the non-Lisp data pool of the pure storage, and return its start | |
4809 | address. Return NULL if not found. */ | |
4810 | ||
4811 | static char * | |
d311d28c | 4812 | find_string_data_in_pure (const char *data, ptrdiff_t nbytes) |
79fd0489 | 4813 | { |
14162469 | 4814 | int i; |
d311d28c | 4815 | ptrdiff_t skip, bm_skip[256], last_char_skip, infinity, start, start_max; |
2aff7c53 | 4816 | const unsigned char *p; |
79fd0489 YM |
4817 | char *non_lisp_beg; |
4818 | ||
d311d28c | 4819 | if (pure_bytes_used_non_lisp <= nbytes) |
79fd0489 YM |
4820 | return NULL; |
4821 | ||
4822 | /* Set up the Boyer-Moore table. */ | |
4823 | skip = nbytes + 1; | |
4824 | for (i = 0; i < 256; i++) | |
4825 | bm_skip[i] = skip; | |
4826 | ||
2aff7c53 | 4827 | p = (const unsigned char *) data; |
79fd0489 YM |
4828 | while (--skip > 0) |
4829 | bm_skip[*p++] = skip; | |
4830 | ||
4831 | last_char_skip = bm_skip['\0']; | |
4832 | ||
4833 | non_lisp_beg = purebeg + pure_size - pure_bytes_used_non_lisp; | |
4834 | start_max = pure_bytes_used_non_lisp - (nbytes + 1); | |
4835 | ||
4836 | /* See the comments in the function `boyer_moore' (search.c) for the | |
4837 | use of `infinity'. */ | |
4838 | infinity = pure_bytes_used_non_lisp + 1; | |
4839 | bm_skip['\0'] = infinity; | |
4840 | ||
2aff7c53 | 4841 | p = (const unsigned char *) non_lisp_beg + nbytes; |
79fd0489 YM |
4842 | start = 0; |
4843 | do | |
4844 | { | |
4845 | /* Check the last character (== '\0'). */ | |
4846 | do | |
4847 | { | |
4848 | start += bm_skip[*(p + start)]; | |
4849 | } | |
4850 | while (start <= start_max); | |
4851 | ||
4852 | if (start < infinity) | |
4853 | /* Couldn't find the last character. */ | |
4854 | return NULL; | |
4855 | ||
4856 | /* No less than `infinity' means we could find the last | |
4857 | character at `p[start - infinity]'. */ | |
4858 | start -= infinity; | |
4859 | ||
4860 | /* Check the remaining characters. */ | |
4861 | if (memcmp (data, non_lisp_beg + start, nbytes) == 0) | |
4862 | /* Found. */ | |
4863 | return non_lisp_beg + start; | |
4864 | ||
4865 | start += last_char_skip; | |
4866 | } | |
4867 | while (start <= start_max); | |
4868 | ||
4869 | return NULL; | |
4870 | } | |
4871 | ||
4872 | ||
2e471eb5 GM |
4873 | /* Return a string allocated in pure space. DATA is a buffer holding |
4874 | NCHARS characters, and NBYTES bytes of string data. MULTIBYTE | |
fce31d69 | 4875 | means make the result string multibyte. |
1a4f1e2c | 4876 | |
2e471eb5 GM |
4877 | Must get an error if pure storage is full, since if it cannot hold |
4878 | a large string it may be able to hold conses that point to that | |
4879 | string; then the string is not protected from gc. */ | |
7146af97 JB |
4880 | |
4881 | Lisp_Object | |
14162469 | 4882 | make_pure_string (const char *data, |
fce31d69 | 4883 | ptrdiff_t nchars, ptrdiff_t nbytes, bool multibyte) |
7146af97 | 4884 | { |
2e471eb5 | 4885 | Lisp_Object string; |
98c6f1e3 | 4886 | struct Lisp_String *s = pure_alloc (sizeof *s, Lisp_String); |
90256841 | 4887 | s->data = (unsigned char *) find_string_data_in_pure (data, nbytes); |
79fd0489 YM |
4888 | if (s->data == NULL) |
4889 | { | |
98c6f1e3 | 4890 | s->data = pure_alloc (nbytes + 1, -1); |
72af86bd | 4891 | memcpy (s->data, data, nbytes); |
79fd0489 YM |
4892 | s->data[nbytes] = '\0'; |
4893 | } | |
2e471eb5 GM |
4894 | s->size = nchars; |
4895 | s->size_byte = multibyte ? nbytes : -1; | |
77c7bcb1 | 4896 | s->intervals = NULL; |
2e471eb5 GM |
4897 | XSETSTRING (string, s); |
4898 | return string; | |
7146af97 JB |
4899 | } |
4900 | ||
2a0213a6 DA |
4901 | /* Return a string allocated in pure space. Do not |
4902 | allocate the string data, just point to DATA. */ | |
a56eaaef DN |
4903 | |
4904 | Lisp_Object | |
2a0213a6 | 4905 | make_pure_c_string (const char *data, ptrdiff_t nchars) |
a56eaaef DN |
4906 | { |
4907 | Lisp_Object string; | |
98c6f1e3 | 4908 | struct Lisp_String *s = pure_alloc (sizeof *s, Lisp_String); |
a56eaaef DN |
4909 | s->size = nchars; |
4910 | s->size_byte = -1; | |
323637a2 | 4911 | s->data = (unsigned char *) data; |
77c7bcb1 | 4912 | s->intervals = NULL; |
a56eaaef DN |
4913 | XSETSTRING (string, s); |
4914 | return string; | |
4915 | } | |
2e471eb5 | 4916 | |
34400008 GM |
4917 | /* Return a cons allocated from pure space. Give it pure copies |
4918 | of CAR as car and CDR as cdr. */ | |
4919 | ||
7146af97 | 4920 | Lisp_Object |
971de7fb | 4921 | pure_cons (Lisp_Object car, Lisp_Object cdr) |
7146af97 | 4922 | { |
98c6f1e3 PE |
4923 | Lisp_Object new; |
4924 | struct Lisp_Cons *p = pure_alloc (sizeof *p, Lisp_Cons); | |
1f0b3fd2 | 4925 | XSETCONS (new, p); |
f3fbd155 KR |
4926 | XSETCAR (new, Fpurecopy (car)); |
4927 | XSETCDR (new, Fpurecopy (cdr)); | |
7146af97 JB |
4928 | return new; |
4929 | } | |
4930 | ||
7146af97 | 4931 | |
34400008 GM |
4932 | /* Value is a float object with value NUM allocated from pure space. */ |
4933 | ||
d3d47262 | 4934 | static Lisp_Object |
971de7fb | 4935 | make_pure_float (double num) |
7146af97 | 4936 | { |
98c6f1e3 PE |
4937 | Lisp_Object new; |
4938 | struct Lisp_Float *p = pure_alloc (sizeof *p, Lisp_Float); | |
1f0b3fd2 | 4939 | XSETFLOAT (new, p); |
f601cdf3 | 4940 | XFLOAT_INIT (new, num); |
7146af97 JB |
4941 | return new; |
4942 | } | |
4943 | ||
34400008 GM |
4944 | |
4945 | /* Return a vector with room for LEN Lisp_Objects allocated from | |
4946 | pure space. */ | |
4947 | ||
72cb32cf | 4948 | static Lisp_Object |
d311d28c | 4949 | make_pure_vector (ptrdiff_t len) |
7146af97 | 4950 | { |
1f0b3fd2 | 4951 | Lisp_Object new; |
d06714cb | 4952 | size_t size = header_size + len * word_size; |
98c6f1e3 | 4953 | struct Lisp_Vector *p = pure_alloc (size, Lisp_Vectorlike); |
1f0b3fd2 | 4954 | XSETVECTOR (new, p); |
eab3844f | 4955 | XVECTOR (new)->header.size = len; |
7146af97 JB |
4956 | return new; |
4957 | } | |
4958 | ||
34400008 | 4959 | |
a7ca3326 | 4960 | DEFUN ("purecopy", Fpurecopy, Spurecopy, 1, 1, 0, |
909e3b33 | 4961 | doc: /* Make a copy of object OBJ in pure storage. |
228299fa | 4962 | Recursively copies contents of vectors and cons cells. |
7ee72033 | 4963 | Does not copy symbols. Copies strings without text properties. */) |
5842a27b | 4964 | (register Lisp_Object obj) |
7146af97 | 4965 | { |
265a9e55 | 4966 | if (NILP (Vpurify_flag)) |
7146af97 JB |
4967 | return obj; |
4968 | ||
1f0b3fd2 | 4969 | if (PURE_POINTER_P (XPNTR (obj))) |
7146af97 JB |
4970 | return obj; |
4971 | ||
e9515805 SM |
4972 | if (HASH_TABLE_P (Vpurify_flag)) /* Hash consing. */ |
4973 | { | |
4974 | Lisp_Object tmp = Fgethash (obj, Vpurify_flag, Qnil); | |
4975 | if (!NILP (tmp)) | |
4976 | return tmp; | |
4977 | } | |
4978 | ||
d6dd74bb | 4979 | if (CONSP (obj)) |
e9515805 | 4980 | obj = pure_cons (XCAR (obj), XCDR (obj)); |
d6dd74bb | 4981 | else if (FLOATP (obj)) |
e9515805 | 4982 | obj = make_pure_float (XFLOAT_DATA (obj)); |
d6dd74bb | 4983 | else if (STRINGP (obj)) |
42a5b22f | 4984 | obj = make_pure_string (SSDATA (obj), SCHARS (obj), |
e9515805 SM |
4985 | SBYTES (obj), |
4986 | STRING_MULTIBYTE (obj)); | |
876c194c | 4987 | else if (COMPILEDP (obj) || VECTORP (obj)) |
d6dd74bb KH |
4988 | { |
4989 | register struct Lisp_Vector *vec; | |
d311d28c PE |
4990 | register ptrdiff_t i; |
4991 | ptrdiff_t size; | |
d6dd74bb | 4992 | |
77b37c05 | 4993 | size = ASIZE (obj); |
7d535c68 KH |
4994 | if (size & PSEUDOVECTOR_FLAG) |
4995 | size &= PSEUDOVECTOR_SIZE_MASK; | |
6b61353c | 4996 | vec = XVECTOR (make_pure_vector (size)); |
d6dd74bb | 4997 | for (i = 0; i < size; i++) |
28be1ada | 4998 | vec->contents[i] = Fpurecopy (AREF (obj, i)); |
876c194c | 4999 | if (COMPILEDP (obj)) |
985773c9 | 5000 | { |
876c194c SM |
5001 | XSETPVECTYPE (vec, PVEC_COMPILED); |
5002 | XSETCOMPILED (obj, vec); | |
985773c9 | 5003 | } |
d6dd74bb KH |
5004 | else |
5005 | XSETVECTOR (obj, vec); | |
7146af97 | 5006 | } |
d6dd74bb KH |
5007 | else if (MARKERP (obj)) |
5008 | error ("Attempt to copy a marker to pure storage"); | |
e9515805 SM |
5009 | else |
5010 | /* Not purified, don't hash-cons. */ | |
5011 | return obj; | |
5012 | ||
5013 | if (HASH_TABLE_P (Vpurify_flag)) /* Hash consing. */ | |
5014 | Fputhash (obj, obj, Vpurify_flag); | |
6bbd7a29 GM |
5015 | |
5016 | return obj; | |
7146af97 | 5017 | } |
2e471eb5 | 5018 | |
34400008 | 5019 | |
7146af97 | 5020 | \f |
34400008 GM |
5021 | /*********************************************************************** |
5022 | Protection from GC | |
5023 | ***********************************************************************/ | |
5024 | ||
2e471eb5 GM |
5025 | /* Put an entry in staticvec, pointing at the variable with address |
5026 | VARADDRESS. */ | |
7146af97 JB |
5027 | |
5028 | void | |
971de7fb | 5029 | staticpro (Lisp_Object *varaddress) |
7146af97 JB |
5030 | { |
5031 | staticvec[staticidx++] = varaddress; | |
5032 | if (staticidx >= NSTATICS) | |
afb8aa24 | 5033 | fatal ("NSTATICS too small; try increasing and recompiling Emacs."); |
7146af97 JB |
5034 | } |
5035 | ||
7146af97 | 5036 | \f |
34400008 GM |
5037 | /*********************************************************************** |
5038 | Protection from GC | |
5039 | ***********************************************************************/ | |
1a4f1e2c | 5040 | |
e8197642 RS |
5041 | /* Temporarily prevent garbage collection. */ |
5042 | ||
d311d28c | 5043 | ptrdiff_t |
971de7fb | 5044 | inhibit_garbage_collection (void) |
e8197642 | 5045 | { |
d311d28c | 5046 | ptrdiff_t count = SPECPDL_INDEX (); |
54defd0d | 5047 | |
6349ae4d | 5048 | specbind (Qgc_cons_threshold, make_number (MOST_POSITIVE_FIXNUM)); |
e8197642 RS |
5049 | return count; |
5050 | } | |
5051 | ||
3ab6e069 DA |
5052 | /* Used to avoid possible overflows when |
5053 | converting from C to Lisp integers. */ | |
5054 | ||
b0ab8123 | 5055 | static Lisp_Object |
3ab6e069 DA |
5056 | bounded_number (EMACS_INT number) |
5057 | { | |
5058 | return make_number (min (MOST_POSITIVE_FIXNUM, number)); | |
5059 | } | |
34400008 | 5060 | |
12b3895d TM |
5061 | /* Calculate total bytes of live objects. */ |
5062 | ||
5063 | static size_t | |
5064 | total_bytes_of_live_objects (void) | |
5065 | { | |
5066 | size_t tot = 0; | |
5067 | tot += total_conses * sizeof (struct Lisp_Cons); | |
5068 | tot += total_symbols * sizeof (struct Lisp_Symbol); | |
5069 | tot += total_markers * sizeof (union Lisp_Misc); | |
5070 | tot += total_string_bytes; | |
5071 | tot += total_vector_slots * word_size; | |
5072 | tot += total_floats * sizeof (struct Lisp_Float); | |
5073 | tot += total_intervals * sizeof (struct interval); | |
5074 | tot += total_strings * sizeof (struct Lisp_String); | |
5075 | return tot; | |
5076 | } | |
5077 | ||
a7ca3326 | 5078 | DEFUN ("garbage-collect", Fgarbage_collect, Sgarbage_collect, 0, 0, "", |
7ee72033 | 5079 | doc: /* Reclaim storage for Lisp objects no longer needed. |
e1e37596 RS |
5080 | Garbage collection happens automatically if you cons more than |
5081 | `gc-cons-threshold' bytes of Lisp data since previous garbage collection. | |
5db81e33 SM |
5082 | `garbage-collect' normally returns a list with info on amount of space in use, |
5083 | where each entry has the form (NAME SIZE USED FREE), where: | |
5084 | - NAME is a symbol describing the kind of objects this entry represents, | |
5085 | - SIZE is the number of bytes used by each one, | |
5086 | - USED is the number of those objects that were found live in the heap, | |
5087 | - FREE is the number of those objects that are not live but that Emacs | |
5088 | keeps around for future allocations (maybe because it does not know how | |
5089 | to return them to the OS). | |
e1e37596 | 5090 | However, if there was overflow in pure space, `garbage-collect' |
999dd333 GM |
5091 | returns nil, because real GC can't be done. |
5092 | See Info node `(elisp)Garbage Collection'. */) | |
5842a27b | 5093 | (void) |
7146af97 | 5094 | { |
fce31d69 PE |
5095 | struct specbinding *bind; |
5096 | struct buffer *nextb; | |
7146af97 | 5097 | char stack_top_variable; |
f66c7cf8 | 5098 | ptrdiff_t i; |
fce31d69 | 5099 | bool message_p; |
d311d28c | 5100 | ptrdiff_t count = SPECPDL_INDEX (); |
dbcf001c | 5101 | EMACS_TIME start; |
fecbd8ff | 5102 | Lisp_Object retval = Qnil; |
12b3895d | 5103 | size_t tot_before = 0; |
3d80c99f | 5104 | struct backtrace backtrace; |
2c5bd608 | 5105 | |
3de0effb | 5106 | if (abort_on_gc) |
1088b922 | 5107 | emacs_abort (); |
3de0effb | 5108 | |
9e713715 GM |
5109 | /* Can't GC if pure storage overflowed because we can't determine |
5110 | if something is a pure object or not. */ | |
5111 | if (pure_bytes_used_before_overflow) | |
5112 | return Qnil; | |
5113 | ||
3d80c99f SM |
5114 | /* Record this function, so it appears on the profiler's backtraces. */ |
5115 | backtrace.next = backtrace_list; | |
e7c1b6ef SM |
5116 | backtrace.function = Qautomatic_gc; |
5117 | backtrace.args = &Qnil; | |
3d80c99f SM |
5118 | backtrace.nargs = 0; |
5119 | backtrace.debug_on_exit = 0; | |
5120 | backtrace_list = &backtrace; | |
5121 | ||
7e63e0c3 | 5122 | check_cons_list (); |
bbc012e0 | 5123 | |
3c7e66a8 RS |
5124 | /* Don't keep undo information around forever. |
5125 | Do this early on, so it is no problem if the user quits. */ | |
52b852c7 | 5126 | FOR_EACH_BUFFER (nextb) |
9cd47b72 | 5127 | compact_buffer (nextb); |
3c7e66a8 | 5128 | |
6521894d | 5129 | if (profiler_memory_running) |
12b3895d TM |
5130 | tot_before = total_bytes_of_live_objects (); |
5131 | ||
dbcf001c | 5132 | start = current_emacs_time (); |
3c7e66a8 | 5133 | |
58595309 KH |
5134 | /* In case user calls debug_print during GC, |
5135 | don't let that cause a recursive GC. */ | |
5136 | consing_since_gc = 0; | |
5137 | ||
6efc7df7 GM |
5138 | /* Save what's currently displayed in the echo area. */ |
5139 | message_p = push_message (); | |
c55b0da6 | 5140 | record_unwind_protect (pop_message_unwind, Qnil); |
41c28a37 | 5141 | |
7146af97 JB |
5142 | /* Save a copy of the contents of the stack, for debugging. */ |
5143 | #if MAX_SAVE_STACK > 0 | |
265a9e55 | 5144 | if (NILP (Vpurify_flag)) |
7146af97 | 5145 | { |
dd3f25f7 | 5146 | char *stack; |
903fe15d | 5147 | ptrdiff_t stack_size; |
dd3f25f7 | 5148 | if (&stack_top_variable < stack_bottom) |
7146af97 | 5149 | { |
dd3f25f7 PE |
5150 | stack = &stack_top_variable; |
5151 | stack_size = stack_bottom - &stack_top_variable; | |
5152 | } | |
5153 | else | |
5154 | { | |
5155 | stack = stack_bottom; | |
5156 | stack_size = &stack_top_variable - stack_bottom; | |
5157 | } | |
5158 | if (stack_size <= MAX_SAVE_STACK) | |
7146af97 | 5159 | { |
dd3f25f7 | 5160 | if (stack_copy_size < stack_size) |
7146af97 | 5161 | { |
38182d90 | 5162 | stack_copy = xrealloc (stack_copy, stack_size); |
dd3f25f7 | 5163 | stack_copy_size = stack_size; |
7146af97 | 5164 | } |
dd3f25f7 | 5165 | memcpy (stack_copy, stack, stack_size); |
7146af97 JB |
5166 | } |
5167 | } | |
5168 | #endif /* MAX_SAVE_STACK > 0 */ | |
5169 | ||
299585ee | 5170 | if (garbage_collection_messages) |
691c4285 | 5171 | message1_nolog ("Garbage collecting..."); |
7146af97 | 5172 | |
4d7e6e51 | 5173 | block_input (); |
6e0fca1d | 5174 | |
eec7b73d RS |
5175 | shrink_regexp_cache (); |
5176 | ||
7146af97 JB |
5177 | gc_in_progress = 1; |
5178 | ||
005ca5c7 | 5179 | /* Mark all the special slots that serve as the roots of accessibility. */ |
7146af97 | 5180 | |
c752cfa9 DA |
5181 | mark_buffer (&buffer_defaults); |
5182 | mark_buffer (&buffer_local_symbols); | |
5183 | ||
7146af97 | 5184 | for (i = 0; i < staticidx; i++) |
49723c04 | 5185 | mark_object (*staticvec[i]); |
34400008 | 5186 | |
126f9c02 SM |
5187 | for (bind = specpdl; bind != specpdl_ptr; bind++) |
5188 | { | |
5189 | mark_object (bind->symbol); | |
5190 | mark_object (bind->old_value); | |
5191 | } | |
6ed8eeff | 5192 | mark_terminals (); |
126f9c02 SM |
5193 | mark_kboards (); |
5194 | ||
5195 | #ifdef USE_GTK | |
a411ac43 | 5196 | xg_mark_data (); |
126f9c02 SM |
5197 | #endif |
5198 | ||
34400008 GM |
5199 | #if (GC_MARK_STACK == GC_MAKE_GCPROS_NOOPS \ |
5200 | || GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS) | |
5201 | mark_stack (); | |
5202 | #else | |
acf5f7d3 SM |
5203 | { |
5204 | register struct gcpro *tail; | |
5205 | for (tail = gcprolist; tail; tail = tail->next) | |
5206 | for (i = 0; i < tail->nvars; i++) | |
005ca5c7 | 5207 | mark_object (tail->var[i]); |
acf5f7d3 | 5208 | } |
3e21b6a7 | 5209 | mark_byte_stack (); |
b286858c SM |
5210 | { |
5211 | struct catchtag *catch; | |
5212 | struct handler *handler; | |
177c0ea7 | 5213 | |
7146af97 JB |
5214 | for (catch = catchlist; catch; catch = catch->next) |
5215 | { | |
49723c04 SM |
5216 | mark_object (catch->tag); |
5217 | mark_object (catch->val); | |
177c0ea7 | 5218 | } |
7146af97 JB |
5219 | for (handler = handlerlist; handler; handler = handler->next) |
5220 | { | |
49723c04 SM |
5221 | mark_object (handler->handler); |
5222 | mark_object (handler->var); | |
177c0ea7 | 5223 | } |
b286858c | 5224 | } |
b40ea20a | 5225 | mark_backtrace (); |
b286858c | 5226 | #endif |
7146af97 | 5227 | |
454d7973 KS |
5228 | #ifdef HAVE_WINDOW_SYSTEM |
5229 | mark_fringe_data (); | |
5230 | #endif | |
5231 | ||
74c35a48 SM |
5232 | #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES |
5233 | mark_stack (); | |
5234 | #endif | |
5235 | ||
c37adf23 SM |
5236 | /* Everything is now marked, except for the things that require special |
5237 | finalization, i.e. the undo_list. | |
5238 | Look thru every buffer's undo list | |
4c315bda RS |
5239 | for elements that update markers that were not marked, |
5240 | and delete them. */ | |
52b852c7 | 5241 | FOR_EACH_BUFFER (nextb) |
d17337e5 DA |
5242 | { |
5243 | /* If a buffer's undo list is Qt, that means that undo is | |
5244 | turned off in that buffer. Calling truncate_undo_list on | |
5245 | Qt tends to return NULL, which effectively turns undo back on. | |
5246 | So don't call truncate_undo_list if undo_list is Qt. */ | |
e34f7f79 | 5247 | if (! EQ (nextb->INTERNAL_FIELD (undo_list), Qt)) |
d17337e5 DA |
5248 | { |
5249 | Lisp_Object tail, prev; | |
e34f7f79 | 5250 | tail = nextb->INTERNAL_FIELD (undo_list); |
d17337e5 DA |
5251 | prev = Qnil; |
5252 | while (CONSP (tail)) | |
5253 | { | |
5254 | if (CONSP (XCAR (tail)) | |
5255 | && MARKERP (XCAR (XCAR (tail))) | |
5256 | && !XMARKER (XCAR (XCAR (tail)))->gcmarkbit) | |
5257 | { | |
5258 | if (NILP (prev)) | |
e34f7f79 | 5259 | nextb->INTERNAL_FIELD (undo_list) = tail = XCDR (tail); |
d17337e5 DA |
5260 | else |
5261 | { | |
5262 | tail = XCDR (tail); | |
5263 | XSETCDR (prev, tail); | |
5264 | } | |
5265 | } | |
5266 | else | |
5267 | { | |
5268 | prev = tail; | |
5269 | tail = XCDR (tail); | |
5270 | } | |
5271 | } | |
5272 | } | |
5273 | /* Now that we have stripped the elements that need not be in the | |
5274 | undo_list any more, we can finally mark the list. */ | |
e34f7f79 | 5275 | mark_object (nextb->INTERNAL_FIELD (undo_list)); |
d17337e5 | 5276 | } |
4c315bda | 5277 | |
7146af97 JB |
5278 | gc_sweep (); |
5279 | ||
5280 | /* Clear the mark bits that we set in certain root slots. */ | |
5281 | ||
033a5fa3 | 5282 | unmark_byte_stack (); |
3ef06d12 SM |
5283 | VECTOR_UNMARK (&buffer_defaults); |
5284 | VECTOR_UNMARK (&buffer_local_symbols); | |
7146af97 | 5285 | |
34400008 GM |
5286 | #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES && 0 |
5287 | dump_zombies (); | |
5288 | #endif | |
5289 | ||
4d7e6e51 | 5290 | unblock_input (); |
6e0fca1d | 5291 | |
7e63e0c3 | 5292 | check_cons_list (); |
bbc012e0 | 5293 | |
7146af97 JB |
5294 | gc_in_progress = 0; |
5295 | ||
5296 | consing_since_gc = 0; | |
0dd6d66d DA |
5297 | if (gc_cons_threshold < GC_DEFAULT_THRESHOLD / 10) |
5298 | gc_cons_threshold = GC_DEFAULT_THRESHOLD / 10; | |
7146af97 | 5299 | |
c0c5c8ae | 5300 | gc_relative_threshold = 0; |
96f077ad SM |
5301 | if (FLOATP (Vgc_cons_percentage)) |
5302 | { /* Set gc_cons_combined_threshold. */ | |
12b3895d | 5303 | double tot = total_bytes_of_live_objects (); |
ae35e756 | 5304 | |
c0c5c8ae PE |
5305 | tot *= XFLOAT_DATA (Vgc_cons_percentage); |
5306 | if (0 < tot) | |
5307 | { | |
5308 | if (tot < TYPE_MAXIMUM (EMACS_INT)) | |
5309 | gc_relative_threshold = tot; | |
5310 | else | |
5311 | gc_relative_threshold = TYPE_MAXIMUM (EMACS_INT); | |
5312 | } | |
96f077ad SM |
5313 | } |
5314 | ||
299585ee RS |
5315 | if (garbage_collection_messages) |
5316 | { | |
6efc7df7 GM |
5317 | if (message_p || minibuf_level > 0) |
5318 | restore_message (); | |
299585ee RS |
5319 | else |
5320 | message1_nolog ("Garbage collecting...done"); | |
5321 | } | |
7146af97 | 5322 | |
98edb5ff | 5323 | unbind_to (count, Qnil); |
fecbd8ff SM |
5324 | { |
5325 | Lisp_Object total[11]; | |
5326 | int total_size = 10; | |
2e471eb5 | 5327 | |
fecbd8ff SM |
5328 | total[0] = list4 (Qconses, make_number (sizeof (struct Lisp_Cons)), |
5329 | bounded_number (total_conses), | |
5330 | bounded_number (total_free_conses)); | |
3ab6e069 | 5331 | |
fecbd8ff SM |
5332 | total[1] = list4 (Qsymbols, make_number (sizeof (struct Lisp_Symbol)), |
5333 | bounded_number (total_symbols), | |
5334 | bounded_number (total_free_symbols)); | |
3ab6e069 | 5335 | |
fecbd8ff SM |
5336 | total[2] = list4 (Qmiscs, make_number (sizeof (union Lisp_Misc)), |
5337 | bounded_number (total_markers), | |
5338 | bounded_number (total_free_markers)); | |
3ab6e069 | 5339 | |
fecbd8ff SM |
5340 | total[3] = list4 (Qstrings, make_number (sizeof (struct Lisp_String)), |
5341 | bounded_number (total_strings), | |
5342 | bounded_number (total_free_strings)); | |
3ab6e069 | 5343 | |
fecbd8ff SM |
5344 | total[4] = list3 (Qstring_bytes, make_number (1), |
5345 | bounded_number (total_string_bytes)); | |
3ab6e069 | 5346 | |
fecbd8ff SM |
5347 | total[5] = list3 (Qvectors, make_number (sizeof (struct Lisp_Vector)), |
5348 | bounded_number (total_vectors)); | |
5b835e1d | 5349 | |
fecbd8ff SM |
5350 | total[6] = list4 (Qvector_slots, make_number (word_size), |
5351 | bounded_number (total_vector_slots), | |
5352 | bounded_number (total_free_vector_slots)); | |
5b835e1d | 5353 | |
fecbd8ff SM |
5354 | total[7] = list4 (Qfloats, make_number (sizeof (struct Lisp_Float)), |
5355 | bounded_number (total_floats), | |
5356 | bounded_number (total_free_floats)); | |
3ab6e069 | 5357 | |
fecbd8ff SM |
5358 | total[8] = list4 (Qintervals, make_number (sizeof (struct interval)), |
5359 | bounded_number (total_intervals), | |
5360 | bounded_number (total_free_intervals)); | |
3ab6e069 | 5361 | |
fecbd8ff SM |
5362 | total[9] = list3 (Qbuffers, make_number (sizeof (struct buffer)), |
5363 | bounded_number (total_buffers)); | |
2e471eb5 | 5364 | |
f8643a6b | 5365 | #ifdef DOUG_LEA_MALLOC |
fecbd8ff SM |
5366 | total_size++; |
5367 | total[10] = list4 (Qheap, make_number (1024), | |
5368 | bounded_number ((mallinfo ().uordblks + 1023) >> 10), | |
5369 | bounded_number ((mallinfo ().fordblks + 1023) >> 10)); | |
f8643a6b | 5370 | #endif |
fecbd8ff SM |
5371 | retval = Flist (total_size, total); |
5372 | } | |
f8643a6b | 5373 | |
34400008 | 5374 | #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES |
7146af97 | 5375 | { |
34400008 | 5376 | /* Compute average percentage of zombies. */ |
fecbd8ff SM |
5377 | double nlive |
5378 | = (total_conses + total_symbols + total_markers + total_strings | |
5379 | + total_vectors + total_floats + total_intervals + total_buffers); | |
34400008 GM |
5380 | |
5381 | avg_live = (avg_live * ngcs + nlive) / (ngcs + 1); | |
5382 | max_live = max (nlive, max_live); | |
5383 | avg_zombies = (avg_zombies * ngcs + nzombies) / (ngcs + 1); | |
5384 | max_zombies = max (nzombies, max_zombies); | |
5385 | ++ngcs; | |
dbcf001c | 5386 | } |
34400008 | 5387 | #endif |
7146af97 | 5388 | |
9e713715 GM |
5389 | if (!NILP (Vpost_gc_hook)) |
5390 | { | |
d311d28c | 5391 | ptrdiff_t gc_count = inhibit_garbage_collection (); |
9e713715 | 5392 | safe_run_hooks (Qpost_gc_hook); |
ae35e756 | 5393 | unbind_to (gc_count, Qnil); |
9e713715 | 5394 | } |
2c5bd608 DL |
5395 | |
5396 | /* Accumulate statistics. */ | |
2c5bd608 | 5397 | if (FLOATP (Vgc_elapsed)) |
387d4d92 PE |
5398 | { |
5399 | EMACS_TIME since_start = sub_emacs_time (current_emacs_time (), start); | |
5400 | Vgc_elapsed = make_float (XFLOAT_DATA (Vgc_elapsed) | |
5401 | + EMACS_TIME_TO_DOUBLE (since_start)); | |
5402 | } | |
d35af63c | 5403 | |
2c5bd608 DL |
5404 | gcs_done++; |
5405 | ||
12b3895d | 5406 | /* Collect profiling data. */ |
6521894d | 5407 | if (profiler_memory_running) |
12b3895d TM |
5408 | { |
5409 | size_t swept = 0; | |
6521894d SM |
5410 | size_t tot_after = total_bytes_of_live_objects (); |
5411 | if (tot_before > tot_after) | |
5412 | swept = tot_before - tot_after; | |
3d80c99f | 5413 | malloc_probe (swept); |
12b3895d TM |
5414 | } |
5415 | ||
3d80c99f | 5416 | backtrace_list = backtrace.next; |
fecbd8ff | 5417 | return retval; |
7146af97 | 5418 | } |
34400008 | 5419 | |
41c28a37 | 5420 | |
3770920e GM |
5421 | /* Mark Lisp objects in glyph matrix MATRIX. Currently the |
5422 | only interesting objects referenced from glyphs are strings. */ | |
41c28a37 GM |
5423 | |
5424 | static void | |
971de7fb | 5425 | mark_glyph_matrix (struct glyph_matrix *matrix) |
41c28a37 GM |
5426 | { |
5427 | struct glyph_row *row = matrix->rows; | |
5428 | struct glyph_row *end = row + matrix->nrows; | |
5429 | ||
2e471eb5 GM |
5430 | for (; row < end; ++row) |
5431 | if (row->enabled_p) | |
5432 | { | |
5433 | int area; | |
5434 | for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area) | |
5435 | { | |
5436 | struct glyph *glyph = row->glyphs[area]; | |
5437 | struct glyph *end_glyph = glyph + row->used[area]; | |
177c0ea7 | 5438 | |
2e471eb5 | 5439 | for (; glyph < end_glyph; ++glyph) |
8e50cc2d | 5440 | if (STRINGP (glyph->object) |
2e471eb5 | 5441 | && !STRING_MARKED_P (XSTRING (glyph->object))) |
49723c04 | 5442 | mark_object (glyph->object); |
2e471eb5 GM |
5443 | } |
5444 | } | |
41c28a37 GM |
5445 | } |
5446 | ||
34400008 | 5447 | |
41c28a37 GM |
5448 | /* Mark Lisp faces in the face cache C. */ |
5449 | ||
5450 | static void | |
971de7fb | 5451 | mark_face_cache (struct face_cache *c) |
41c28a37 GM |
5452 | { |
5453 | if (c) | |
5454 | { | |
5455 | int i, j; | |
5456 | for (i = 0; i < c->used; ++i) | |
5457 | { | |
5458 | struct face *face = FACE_FROM_ID (c->f, i); | |
5459 | ||
5460 | if (face) | |
5461 | { | |
5462 | for (j = 0; j < LFACE_VECTOR_SIZE; ++j) | |
49723c04 | 5463 | mark_object (face->lface[j]); |
41c28a37 GM |
5464 | } |
5465 | } | |
5466 | } | |
5467 | } | |
5468 | ||
5469 | ||
7146af97 | 5470 | \f |
1a4f1e2c | 5471 | /* Mark reference to a Lisp_Object. |
2e471eb5 GM |
5472 | If the object referred to has not been seen yet, recursively mark |
5473 | all the references contained in it. */ | |
7146af97 | 5474 | |
785cd37f | 5475 | #define LAST_MARKED_SIZE 500 |
d3d47262 | 5476 | static Lisp_Object last_marked[LAST_MARKED_SIZE]; |
244ed907 | 5477 | static int last_marked_index; |
785cd37f | 5478 | |
1342fc6f RS |
5479 | /* For debugging--call abort when we cdr down this many |
5480 | links of a list, in mark_object. In debugging, | |
5481 | the call to abort will hit a breakpoint. | |
5482 | Normally this is zero and the check never goes off. */ | |
903fe15d | 5483 | ptrdiff_t mark_object_loop_halt EXTERNALLY_VISIBLE; |
1342fc6f | 5484 | |
8f11f7ec | 5485 | static void |
971de7fb | 5486 | mark_vectorlike (struct Lisp_Vector *ptr) |
d2029e5b | 5487 | { |
d311d28c PE |
5488 | ptrdiff_t size = ptr->header.size; |
5489 | ptrdiff_t i; | |
d2029e5b | 5490 | |
8f11f7ec | 5491 | eassert (!VECTOR_MARKED_P (ptr)); |
7555c33f | 5492 | VECTOR_MARK (ptr); /* Else mark it. */ |
d2029e5b SM |
5493 | if (size & PSEUDOVECTOR_FLAG) |
5494 | size &= PSEUDOVECTOR_SIZE_MASK; | |
d3d47262 | 5495 | |
d2029e5b SM |
5496 | /* Note that this size is not the memory-footprint size, but only |
5497 | the number of Lisp_Object fields that we should trace. | |
5498 | The distinction is used e.g. by Lisp_Process which places extra | |
7555c33f SM |
5499 | non-Lisp_Object fields at the end of the structure... */ |
5500 | for (i = 0; i < size; i++) /* ...and then mark its elements. */ | |
d2029e5b | 5501 | mark_object (ptr->contents[i]); |
d2029e5b SM |
5502 | } |
5503 | ||
58026347 KH |
5504 | /* Like mark_vectorlike but optimized for char-tables (and |
5505 | sub-char-tables) assuming that the contents are mostly integers or | |
5506 | symbols. */ | |
5507 | ||
5508 | static void | |
971de7fb | 5509 | mark_char_table (struct Lisp_Vector *ptr) |
58026347 | 5510 | { |
b6439961 PE |
5511 | int size = ptr->header.size & PSEUDOVECTOR_SIZE_MASK; |
5512 | int i; | |
58026347 | 5513 | |
8f11f7ec | 5514 | eassert (!VECTOR_MARKED_P (ptr)); |
58026347 KH |
5515 | VECTOR_MARK (ptr); |
5516 | for (i = 0; i < size; i++) | |
5517 | { | |
5518 | Lisp_Object val = ptr->contents[i]; | |
5519 | ||
ef1b0ba7 | 5520 | if (INTEGERP (val) || (SYMBOLP (val) && XSYMBOL (val)->gcmarkbit)) |
58026347 KH |
5521 | continue; |
5522 | if (SUB_CHAR_TABLE_P (val)) | |
5523 | { | |
5524 | if (! VECTOR_MARKED_P (XVECTOR (val))) | |
5525 | mark_char_table (XVECTOR (val)); | |
5526 | } | |
5527 | else | |
5528 | mark_object (val); | |
5529 | } | |
5530 | } | |
5531 | ||
36429c89 DA |
5532 | /* Mark the chain of overlays starting at PTR. */ |
5533 | ||
5534 | static void | |
5535 | mark_overlay (struct Lisp_Overlay *ptr) | |
5536 | { | |
5537 | for (; ptr && !ptr->gcmarkbit; ptr = ptr->next) | |
5538 | { | |
5539 | ptr->gcmarkbit = 1; | |
c644523b DA |
5540 | mark_object (ptr->start); |
5541 | mark_object (ptr->end); | |
5542 | mark_object (ptr->plist); | |
36429c89 DA |
5543 | } |
5544 | } | |
5545 | ||
5546 | /* Mark Lisp_Objects and special pointers in BUFFER. */ | |
cf5c0175 DA |
5547 | |
5548 | static void | |
5549 | mark_buffer (struct buffer *buffer) | |
5550 | { | |
b4fa72f2 DA |
5551 | /* This is handled much like other pseudovectors... */ |
5552 | mark_vectorlike ((struct Lisp_Vector *) buffer); | |
cf5c0175 | 5553 | |
b4fa72f2 | 5554 | /* ...but there are some buffer-specific things. */ |
cf5c0175 | 5555 | |
b4fa72f2 | 5556 | MARK_INTERVAL_TREE (buffer_intervals (buffer)); |
cf5c0175 | 5557 | |
b4fa72f2 DA |
5558 | /* For now, we just don't mark the undo_list. It's done later in |
5559 | a special way just before the sweep phase, and after stripping | |
5560 | some of its elements that are not needed any more. */ | |
cf5c0175 | 5561 | |
b4fa72f2 DA |
5562 | mark_overlay (buffer->overlays_before); |
5563 | mark_overlay (buffer->overlays_after); | |
cf5c0175 | 5564 | |
b4fa72f2 DA |
5565 | /* If this is an indirect buffer, mark its base buffer. */ |
5566 | if (buffer->base_buffer && !VECTOR_MARKED_P (buffer->base_buffer)) | |
5567 | mark_buffer (buffer->base_buffer); | |
cf5c0175 DA |
5568 | } |
5569 | ||
d59a1afb | 5570 | /* Remove killed buffers or items whose car is a killed buffer from |
e99f70c8 | 5571 | LIST, and mark other items. Return changed LIST, which is marked. */ |
d73e321c | 5572 | |
5779a1dc | 5573 | static Lisp_Object |
d59a1afb | 5574 | mark_discard_killed_buffers (Lisp_Object list) |
d73e321c | 5575 | { |
d59a1afb | 5576 | Lisp_Object tail, *prev = &list; |
d73e321c | 5577 | |
d59a1afb DA |
5578 | for (tail = list; CONSP (tail) && !CONS_MARKED_P (XCONS (tail)); |
5579 | tail = XCDR (tail)) | |
d73e321c | 5580 | { |
5779a1dc | 5581 | Lisp_Object tem = XCAR (tail); |
d73e321c DA |
5582 | if (CONSP (tem)) |
5583 | tem = XCAR (tem); | |
5584 | if (BUFFERP (tem) && !BUFFER_LIVE_P (XBUFFER (tem))) | |
5779a1dc | 5585 | *prev = XCDR (tail); |
d73e321c | 5586 | else |
d59a1afb DA |
5587 | { |
5588 | CONS_MARK (XCONS (tail)); | |
5589 | mark_object (XCAR (tail)); | |
5590 | prev = &XCDR_AS_LVALUE (tail); | |
5591 | } | |
d73e321c | 5592 | } |
e99f70c8 | 5593 | mark_object (tail); |
d73e321c DA |
5594 | return list; |
5595 | } | |
5596 | ||
cf5c0175 DA |
5597 | /* Determine type of generic Lisp_Object and mark it accordingly. */ |
5598 | ||
41c28a37 | 5599 | void |
971de7fb | 5600 | mark_object (Lisp_Object arg) |
7146af97 | 5601 | { |
49723c04 | 5602 | register Lisp_Object obj = arg; |
4f5c1376 GM |
5603 | #ifdef GC_CHECK_MARKED_OBJECTS |
5604 | void *po; | |
5605 | struct mem_node *m; | |
5606 | #endif | |
903fe15d | 5607 | ptrdiff_t cdr_count = 0; |
7146af97 | 5608 | |
9149e743 | 5609 | loop: |
7146af97 | 5610 | |
1f0b3fd2 | 5611 | if (PURE_POINTER_P (XPNTR (obj))) |
7146af97 JB |
5612 | return; |
5613 | ||
49723c04 | 5614 | last_marked[last_marked_index++] = obj; |
785cd37f RS |
5615 | if (last_marked_index == LAST_MARKED_SIZE) |
5616 | last_marked_index = 0; | |
5617 | ||
4f5c1376 GM |
5618 | /* Perform some sanity checks on the objects marked here. Abort if |
5619 | we encounter an object we know is bogus. This increases GC time | |
5620 | by ~80%, and requires compilation with GC_MARK_STACK != 0. */ | |
5621 | #ifdef GC_CHECK_MARKED_OBJECTS | |
5622 | ||
5623 | po = (void *) XPNTR (obj); | |
5624 | ||
5625 | /* Check that the object pointed to by PO is known to be a Lisp | |
5626 | structure allocated from the heap. */ | |
5627 | #define CHECK_ALLOCATED() \ | |
5628 | do { \ | |
5629 | m = mem_find (po); \ | |
5630 | if (m == MEM_NIL) \ | |
1088b922 | 5631 | emacs_abort (); \ |
4f5c1376 GM |
5632 | } while (0) |
5633 | ||
5634 | /* Check that the object pointed to by PO is live, using predicate | |
5635 | function LIVEP. */ | |
5636 | #define CHECK_LIVE(LIVEP) \ | |
5637 | do { \ | |
5638 | if (!LIVEP (m, po)) \ | |
1088b922 | 5639 | emacs_abort (); \ |
4f5c1376 GM |
5640 | } while (0) |
5641 | ||
5642 | /* Check both of the above conditions. */ | |
5643 | #define CHECK_ALLOCATED_AND_LIVE(LIVEP) \ | |
5644 | do { \ | |
5645 | CHECK_ALLOCATED (); \ | |
5646 | CHECK_LIVE (LIVEP); \ | |
5647 | } while (0) \ | |
177c0ea7 | 5648 | |
4f5c1376 | 5649 | #else /* not GC_CHECK_MARKED_OBJECTS */ |
177c0ea7 | 5650 | |
4f5c1376 GM |
5651 | #define CHECK_LIVE(LIVEP) (void) 0 |
5652 | #define CHECK_ALLOCATED_AND_LIVE(LIVEP) (void) 0 | |
177c0ea7 | 5653 | |
4f5c1376 GM |
5654 | #endif /* not GC_CHECK_MARKED_OBJECTS */ |
5655 | ||
7393bcbb | 5656 | switch (XTYPE (obj)) |
7146af97 JB |
5657 | { |
5658 | case Lisp_String: | |
5659 | { | |
5660 | register struct Lisp_String *ptr = XSTRING (obj); | |
8f11f7ec SM |
5661 | if (STRING_MARKED_P (ptr)) |
5662 | break; | |
4f5c1376 | 5663 | CHECK_ALLOCATED_AND_LIVE (live_string_p); |
2e471eb5 | 5664 | MARK_STRING (ptr); |
7555c33f | 5665 | MARK_INTERVAL_TREE (ptr->intervals); |
361b097f | 5666 | #ifdef GC_CHECK_STRING_BYTES |
676a7251 | 5667 | /* Check that the string size recorded in the string is the |
7555c33f | 5668 | same as the one recorded in the sdata structure. */ |
e499d0ee | 5669 | string_bytes (ptr); |
361b097f | 5670 | #endif /* GC_CHECK_STRING_BYTES */ |
7146af97 JB |
5671 | } |
5672 | break; | |
5673 | ||
76437631 | 5674 | case Lisp_Vectorlike: |
cf5c0175 DA |
5675 | { |
5676 | register struct Lisp_Vector *ptr = XVECTOR (obj); | |
5677 | register ptrdiff_t pvectype; | |
5678 | ||
5679 | if (VECTOR_MARKED_P (ptr)) | |
5680 | break; | |
5681 | ||
4f5c1376 | 5682 | #ifdef GC_CHECK_MARKED_OBJECTS |
cf5c0175 | 5683 | m = mem_find (po); |
c752cfa9 | 5684 | if (m == MEM_NIL && !SUBRP (obj)) |
1088b922 | 5685 | emacs_abort (); |
4f5c1376 | 5686 | #endif /* GC_CHECK_MARKED_OBJECTS */ |
177c0ea7 | 5687 | |
cf5c0175 | 5688 | if (ptr->header.size & PSEUDOVECTOR_FLAG) |
ee28be33 SM |
5689 | pvectype = ((ptr->header.size & PVEC_TYPE_MASK) |
5690 | >> PSEUDOVECTOR_SIZE_BITS); | |
cf5c0175 | 5691 | else |
6aea7528 | 5692 | pvectype = PVEC_NORMAL_VECTOR; |
cf5c0175 | 5693 | |
cf5c0175 DA |
5694 | if (pvectype != PVEC_SUBR && pvectype != PVEC_BUFFER) |
5695 | CHECK_LIVE (live_vector_p); | |
169ee243 | 5696 | |
ee28be33 | 5697 | switch (pvectype) |
cf5c0175 | 5698 | { |
ee28be33 | 5699 | case PVEC_BUFFER: |
cf5c0175 | 5700 | #ifdef GC_CHECK_MARKED_OBJECTS |
c752cfa9 DA |
5701 | { |
5702 | struct buffer *b; | |
5703 | FOR_EACH_BUFFER (b) | |
5704 | if (b == po) | |
5705 | break; | |
5706 | if (b == NULL) | |
5707 | emacs_abort (); | |
5708 | } | |
cf5c0175 DA |
5709 | #endif /* GC_CHECK_MARKED_OBJECTS */ |
5710 | mark_buffer ((struct buffer *) ptr); | |
ee28be33 SM |
5711 | break; |
5712 | ||
5713 | case PVEC_COMPILED: | |
5714 | { /* We could treat this just like a vector, but it is better | |
5715 | to save the COMPILED_CONSTANTS element for last and avoid | |
5716 | recursion there. */ | |
5717 | int size = ptr->header.size & PSEUDOVECTOR_SIZE_MASK; | |
5718 | int i; | |
5719 | ||
5720 | VECTOR_MARK (ptr); | |
5721 | for (i = 0; i < size; i++) | |
5722 | if (i != COMPILED_CONSTANTS) | |
5723 | mark_object (ptr->contents[i]); | |
5724 | if (size > COMPILED_CONSTANTS) | |
5725 | { | |
5726 | obj = ptr->contents[COMPILED_CONSTANTS]; | |
5727 | goto loop; | |
5728 | } | |
5729 | } | |
5730 | break; | |
cf5c0175 | 5731 | |
ee28be33 | 5732 | case PVEC_FRAME: |
d59a1afb DA |
5733 | mark_vectorlike (ptr); |
5734 | mark_face_cache (((struct frame *) ptr)->face_cache); | |
ee28be33 | 5735 | break; |
cf5c0175 | 5736 | |
ee28be33 SM |
5737 | case PVEC_WINDOW: |
5738 | { | |
5739 | struct window *w = (struct window *) ptr; | |
d73e321c | 5740 | bool leaf = NILP (w->hchild) && NILP (w->vchild); |
5779a1dc | 5741 | |
0699fc18 | 5742 | mark_vectorlike (ptr); |
e99f70c8 | 5743 | |
0699fc18 DA |
5744 | /* Mark glyphs for leaf windows. Marking window |
5745 | matrices is sufficient because frame matrices | |
5746 | use the same glyph memory. */ | |
d73e321c | 5747 | if (leaf && w->current_matrix) |
ee28be33 | 5748 | { |
0699fc18 DA |
5749 | mark_glyph_matrix (w->current_matrix); |
5750 | mark_glyph_matrix (w->desired_matrix); | |
ee28be33 | 5751 | } |
e99f70c8 SM |
5752 | |
5753 | /* Filter out killed buffers from both buffer lists | |
5754 | in attempt to help GC to reclaim killed buffers faster. | |
5755 | We can do it elsewhere for live windows, but this is the | |
5756 | best place to do it for dead windows. */ | |
5757 | wset_prev_buffers | |
5758 | (w, mark_discard_killed_buffers (w->prev_buffers)); | |
5759 | wset_next_buffers | |
5760 | (w, mark_discard_killed_buffers (w->next_buffers)); | |
ee28be33 SM |
5761 | } |
5762 | break; | |
cf5c0175 | 5763 | |
ee28be33 SM |
5764 | case PVEC_HASH_TABLE: |
5765 | { | |
5766 | struct Lisp_Hash_Table *h = (struct Lisp_Hash_Table *) ptr; | |
cf5c0175 | 5767 | |
ee28be33 SM |
5768 | mark_vectorlike (ptr); |
5769 | /* If hash table is not weak, mark all keys and values. | |
5770 | For weak tables, mark only the vector. */ | |
5771 | if (NILP (h->weak)) | |
5772 | mark_object (h->key_and_value); | |
5773 | else | |
5774 | VECTOR_MARK (XVECTOR (h->key_and_value)); | |
5775 | } | |
5776 | break; | |
cf5c0175 | 5777 | |
ee28be33 SM |
5778 | case PVEC_CHAR_TABLE: |
5779 | mark_char_table (ptr); | |
5780 | break; | |
cf5c0175 | 5781 | |
ee28be33 SM |
5782 | case PVEC_BOOL_VECTOR: |
5783 | /* No Lisp_Objects to mark in a bool vector. */ | |
5784 | VECTOR_MARK (ptr); | |
5785 | break; | |
cf5c0175 | 5786 | |
ee28be33 SM |
5787 | case PVEC_SUBR: |
5788 | break; | |
cf5c0175 | 5789 | |
ee28be33 | 5790 | case PVEC_FREE: |
1088b922 | 5791 | emacs_abort (); |
cf5c0175 | 5792 | |
ee28be33 SM |
5793 | default: |
5794 | mark_vectorlike (ptr); | |
5795 | } | |
cf5c0175 | 5796 | } |
169ee243 | 5797 | break; |
7146af97 | 5798 | |
7146af97 JB |
5799 | case Lisp_Symbol: |
5800 | { | |
c70bbf06 | 5801 | register struct Lisp_Symbol *ptr = XSYMBOL (obj); |
7146af97 JB |
5802 | struct Lisp_Symbol *ptrx; |
5803 | ||
8f11f7ec SM |
5804 | if (ptr->gcmarkbit) |
5805 | break; | |
4f5c1376 | 5806 | CHECK_ALLOCATED_AND_LIVE (live_symbol_p); |
2336fe58 | 5807 | ptr->gcmarkbit = 1; |
c644523b DA |
5808 | mark_object (ptr->function); |
5809 | mark_object (ptr->plist); | |
ce5b453a SM |
5810 | switch (ptr->redirect) |
5811 | { | |
5812 | case SYMBOL_PLAINVAL: mark_object (SYMBOL_VAL (ptr)); break; | |
5813 | case SYMBOL_VARALIAS: | |
5814 | { | |
5815 | Lisp_Object tem; | |
5816 | XSETSYMBOL (tem, SYMBOL_ALIAS (ptr)); | |
5817 | mark_object (tem); | |
5818 | break; | |
5819 | } | |
5820 | case SYMBOL_LOCALIZED: | |
5821 | { | |
5822 | struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (ptr); | |
d73e321c DA |
5823 | Lisp_Object where = blv->where; |
5824 | /* If the value is set up for a killed buffer or deleted | |
5825 | frame, restore it's global binding. If the value is | |
5826 | forwarded to a C variable, either it's not a Lisp_Object | |
5827 | var, or it's staticpro'd already. */ | |
5828 | if ((BUFFERP (where) && !BUFFER_LIVE_P (XBUFFER (where))) | |
5829 | || (FRAMEP (where) && !FRAME_LIVE_P (XFRAME (where)))) | |
5830 | swap_in_global_binding (ptr); | |
ce5b453a SM |
5831 | mark_object (blv->where); |
5832 | mark_object (blv->valcell); | |
5833 | mark_object (blv->defcell); | |
5834 | break; | |
5835 | } | |
5836 | case SYMBOL_FORWARDED: | |
5837 | /* If the value is forwarded to a buffer or keyboard field, | |
5838 | these are marked when we see the corresponding object. | |
5839 | And if it's forwarded to a C variable, either it's not | |
5840 | a Lisp_Object var, or it's staticpro'd already. */ | |
5841 | break; | |
1088b922 | 5842 | default: emacs_abort (); |
ce5b453a | 5843 | } |
c644523b DA |
5844 | if (!PURE_POINTER_P (XSTRING (ptr->name))) |
5845 | MARK_STRING (XSTRING (ptr->name)); | |
0c94c8d6 | 5846 | MARK_INTERVAL_TREE (string_intervals (ptr->name)); |
177c0ea7 | 5847 | |
7146af97 JB |
5848 | ptr = ptr->next; |
5849 | if (ptr) | |
5850 | { | |
7555c33f | 5851 | ptrx = ptr; /* Use of ptrx avoids compiler bug on Sun. */ |
7146af97 | 5852 | XSETSYMBOL (obj, ptrx); |
49723c04 | 5853 | goto loop; |
7146af97 JB |
5854 | } |
5855 | } | |
5856 | break; | |
5857 | ||
a0a38eb7 | 5858 | case Lisp_Misc: |
4f5c1376 | 5859 | CHECK_ALLOCATED_AND_LIVE (live_misc_p); |
b766f870 | 5860 | |
7555c33f SM |
5861 | if (XMISCANY (obj)->gcmarkbit) |
5862 | break; | |
5863 | ||
5864 | switch (XMISCTYPE (obj)) | |
a0a38eb7 | 5865 | { |
7555c33f SM |
5866 | case Lisp_Misc_Marker: |
5867 | /* DO NOT mark thru the marker's chain. | |
5868 | The buffer's markers chain does not preserve markers from gc; | |
5869 | instead, markers are removed from the chain when freed by gc. */ | |
36429c89 | 5870 | XMISCANY (obj)->gcmarkbit = 1; |
7555c33f | 5871 | break; |
465edf35 | 5872 | |
7555c33f SM |
5873 | case Lisp_Misc_Save_Value: |
5874 | XMISCANY (obj)->gcmarkbit = 1; | |
9ea306d1 | 5875 | #if GC_MARK_STACK |
7555c33f SM |
5876 | { |
5877 | register struct Lisp_Save_Value *ptr = XSAVE_VALUE (obj); | |
5878 | /* If DOGC is set, POINTER is the address of a memory | |
5879 | area containing INTEGER potential Lisp_Objects. */ | |
5880 | if (ptr->dogc) | |
5881 | { | |
5882 | Lisp_Object *p = (Lisp_Object *) ptr->pointer; | |
5883 | ptrdiff_t nelt; | |
5884 | for (nelt = ptr->integer; nelt > 0; nelt--, p++) | |
5885 | mark_maybe_object (*p); | |
5886 | } | |
5887 | } | |
9ea306d1 | 5888 | #endif |
7555c33f SM |
5889 | break; |
5890 | ||
5891 | case Lisp_Misc_Overlay: | |
5892 | mark_overlay (XOVERLAY (obj)); | |
5893 | break; | |
5894 | ||
5895 | default: | |
1088b922 | 5896 | emacs_abort (); |
a0a38eb7 | 5897 | } |
7146af97 JB |
5898 | break; |
5899 | ||
5900 | case Lisp_Cons: | |
7146af97 JB |
5901 | { |
5902 | register struct Lisp_Cons *ptr = XCONS (obj); | |
8f11f7ec SM |
5903 | if (CONS_MARKED_P (ptr)) |
5904 | break; | |
4f5c1376 | 5905 | CHECK_ALLOCATED_AND_LIVE (live_cons_p); |
08b7c2cb | 5906 | CONS_MARK (ptr); |
c54ca951 | 5907 | /* If the cdr is nil, avoid recursion for the car. */ |
c644523b | 5908 | if (EQ (ptr->u.cdr, Qnil)) |
c54ca951 | 5909 | { |
c644523b | 5910 | obj = ptr->car; |
1342fc6f | 5911 | cdr_count = 0; |
c54ca951 RS |
5912 | goto loop; |
5913 | } | |
c644523b DA |
5914 | mark_object (ptr->car); |
5915 | obj = ptr->u.cdr; | |
1342fc6f RS |
5916 | cdr_count++; |
5917 | if (cdr_count == mark_object_loop_halt) | |
1088b922 | 5918 | emacs_abort (); |
7146af97 JB |
5919 | goto loop; |
5920 | } | |
5921 | ||
7146af97 | 5922 | case Lisp_Float: |
4f5c1376 | 5923 | CHECK_ALLOCATED_AND_LIVE (live_float_p); |
ab6780cd | 5924 | FLOAT_MARK (XFLOAT (obj)); |
7146af97 | 5925 | break; |
7146af97 | 5926 | |
2de9f71c | 5927 | case_Lisp_Int: |
7146af97 JB |
5928 | break; |
5929 | ||
5930 | default: | |
1088b922 | 5931 | emacs_abort (); |
7146af97 | 5932 | } |
4f5c1376 GM |
5933 | |
5934 | #undef CHECK_LIVE | |
5935 | #undef CHECK_ALLOCATED | |
5936 | #undef CHECK_ALLOCATED_AND_LIVE | |
7146af97 | 5937 | } |
4a729fd8 | 5938 | /* Mark the Lisp pointers in the terminal objects. |
0ba2624f | 5939 | Called by Fgarbage_collect. */ |
4a729fd8 | 5940 | |
4a729fd8 SM |
5941 | static void |
5942 | mark_terminals (void) | |
5943 | { | |
5944 | struct terminal *t; | |
5945 | for (t = terminal_list; t; t = t->next_terminal) | |
5946 | { | |
5947 | eassert (t->name != NULL); | |
354884c4 | 5948 | #ifdef HAVE_WINDOW_SYSTEM |
96ad0af7 YM |
5949 | /* If a terminal object is reachable from a stacpro'ed object, |
5950 | it might have been marked already. Make sure the image cache | |
5951 | gets marked. */ | |
5952 | mark_image_cache (t->image_cache); | |
354884c4 | 5953 | #endif /* HAVE_WINDOW_SYSTEM */ |
96ad0af7 YM |
5954 | if (!VECTOR_MARKED_P (t)) |
5955 | mark_vectorlike ((struct Lisp_Vector *)t); | |
4a729fd8 SM |
5956 | } |
5957 | } | |
5958 | ||
5959 | ||
084b1a0c | 5960 | |
41c28a37 GM |
5961 | /* Value is non-zero if OBJ will survive the current GC because it's |
5962 | either marked or does not need to be marked to survive. */ | |
5963 | ||
fce31d69 | 5964 | bool |
971de7fb | 5965 | survives_gc_p (Lisp_Object obj) |
41c28a37 | 5966 | { |
fce31d69 | 5967 | bool survives_p; |
177c0ea7 | 5968 | |
8e50cc2d | 5969 | switch (XTYPE (obj)) |
41c28a37 | 5970 | { |
2de9f71c | 5971 | case_Lisp_Int: |
41c28a37 GM |
5972 | survives_p = 1; |
5973 | break; | |
5974 | ||
5975 | case Lisp_Symbol: | |
2336fe58 | 5976 | survives_p = XSYMBOL (obj)->gcmarkbit; |
41c28a37 GM |
5977 | break; |
5978 | ||
5979 | case Lisp_Misc: | |
67ee9f6e | 5980 | survives_p = XMISCANY (obj)->gcmarkbit; |
41c28a37 GM |
5981 | break; |
5982 | ||
5983 | case Lisp_String: | |
08b7c2cb | 5984 | survives_p = STRING_MARKED_P (XSTRING (obj)); |
41c28a37 GM |
5985 | break; |
5986 | ||
5987 | case Lisp_Vectorlike: | |
8e50cc2d | 5988 | survives_p = SUBRP (obj) || VECTOR_MARKED_P (XVECTOR (obj)); |
41c28a37 GM |
5989 | break; |
5990 | ||
5991 | case Lisp_Cons: | |
08b7c2cb | 5992 | survives_p = CONS_MARKED_P (XCONS (obj)); |
41c28a37 GM |
5993 | break; |
5994 | ||
41c28a37 | 5995 | case Lisp_Float: |
ab6780cd | 5996 | survives_p = FLOAT_MARKED_P (XFLOAT (obj)); |
41c28a37 | 5997 | break; |
41c28a37 GM |
5998 | |
5999 | default: | |
1088b922 | 6000 | emacs_abort (); |
41c28a37 GM |
6001 | } |
6002 | ||
34400008 | 6003 | return survives_p || PURE_POINTER_P ((void *) XPNTR (obj)); |
41c28a37 GM |
6004 | } |
6005 | ||
6006 | ||
7146af97 | 6007 | \f |
1a4f1e2c | 6008 | /* Sweep: find all structures not marked, and free them. */ |
7146af97 JB |
6009 | |
6010 | static void | |
971de7fb | 6011 | gc_sweep (void) |
7146af97 | 6012 | { |
41c28a37 GM |
6013 | /* Remove or mark entries in weak hash tables. |
6014 | This must be done before any object is unmarked. */ | |
6015 | sweep_weak_hash_tables (); | |
6016 | ||
2e471eb5 | 6017 | sweep_strings (); |
e499d0ee | 6018 | check_string_bytes (!noninteractive); |
7146af97 JB |
6019 | |
6020 | /* Put all unmarked conses on free list */ | |
6021 | { | |
6022 | register struct cons_block *cblk; | |
6ca94ac9 | 6023 | struct cons_block **cprev = &cons_block; |
7146af97 | 6024 | register int lim = cons_block_index; |
c0c5c8ae | 6025 | EMACS_INT num_free = 0, num_used = 0; |
7146af97 JB |
6026 | |
6027 | cons_free_list = 0; | |
177c0ea7 | 6028 | |
6ca94ac9 | 6029 | for (cblk = cons_block; cblk; cblk = *cprev) |
7146af97 | 6030 | { |
3ae2e3a3 | 6031 | register int i = 0; |
6ca94ac9 | 6032 | int this_free = 0; |
3ae2e3a3 RS |
6033 | int ilim = (lim + BITS_PER_INT - 1) / BITS_PER_INT; |
6034 | ||
6035 | /* Scan the mark bits an int at a time. */ | |
47ea7f44 | 6036 | for (i = 0; i < ilim; i++) |
3ae2e3a3 RS |
6037 | { |
6038 | if (cblk->gcmarkbits[i] == -1) | |
6039 | { | |
6040 | /* Fast path - all cons cells for this int are marked. */ | |
6041 | cblk->gcmarkbits[i] = 0; | |
6042 | num_used += BITS_PER_INT; | |
6043 | } | |
6044 | else | |
6045 | { | |
6046 | /* Some cons cells for this int are not marked. | |
6047 | Find which ones, and free them. */ | |
6048 | int start, pos, stop; | |
6049 | ||
6050 | start = i * BITS_PER_INT; | |
6051 | stop = lim - start; | |
6052 | if (stop > BITS_PER_INT) | |
6053 | stop = BITS_PER_INT; | |
6054 | stop += start; | |
6055 | ||
6056 | for (pos = start; pos < stop; pos++) | |
6057 | { | |
6058 | if (!CONS_MARKED_P (&cblk->conses[pos])) | |
6059 | { | |
6060 | this_free++; | |
6061 | cblk->conses[pos].u.chain = cons_free_list; | |
6062 | cons_free_list = &cblk->conses[pos]; | |
34400008 | 6063 | #if GC_MARK_STACK |
c644523b | 6064 | cons_free_list->car = Vdead; |
34400008 | 6065 | #endif |
3ae2e3a3 RS |
6066 | } |
6067 | else | |
6068 | { | |
6069 | num_used++; | |
6070 | CONS_UNMARK (&cblk->conses[pos]); | |
6071 | } | |
6072 | } | |
6073 | } | |
6074 | } | |
6075 | ||
7146af97 | 6076 | lim = CONS_BLOCK_SIZE; |
6ca94ac9 KH |
6077 | /* If this block contains only free conses and we have already |
6078 | seen more than two blocks worth of free conses then deallocate | |
6079 | this block. */ | |
6feef451 | 6080 | if (this_free == CONS_BLOCK_SIZE && num_free > CONS_BLOCK_SIZE) |
6ca94ac9 | 6081 | { |
6ca94ac9 KH |
6082 | *cprev = cblk->next; |
6083 | /* Unhook from the free list. */ | |
28a099a4 | 6084 | cons_free_list = cblk->conses[0].u.chain; |
08b7c2cb | 6085 | lisp_align_free (cblk); |
6ca94ac9 KH |
6086 | } |
6087 | else | |
6feef451 AS |
6088 | { |
6089 | num_free += this_free; | |
6090 | cprev = &cblk->next; | |
6091 | } | |
7146af97 JB |
6092 | } |
6093 | total_conses = num_used; | |
6094 | total_free_conses = num_free; | |
6095 | } | |
6096 | ||
7146af97 JB |
6097 | /* Put all unmarked floats on free list */ |
6098 | { | |
6099 | register struct float_block *fblk; | |
6ca94ac9 | 6100 | struct float_block **fprev = &float_block; |
7146af97 | 6101 | register int lim = float_block_index; |
c0c5c8ae | 6102 | EMACS_INT num_free = 0, num_used = 0; |
7146af97 JB |
6103 | |
6104 | float_free_list = 0; | |
177c0ea7 | 6105 | |
6ca94ac9 | 6106 | for (fblk = float_block; fblk; fblk = *fprev) |
7146af97 JB |
6107 | { |
6108 | register int i; | |
6ca94ac9 | 6109 | int this_free = 0; |
7146af97 | 6110 | for (i = 0; i < lim; i++) |
ab6780cd | 6111 | if (!FLOAT_MARKED_P (&fblk->floats[i])) |
7146af97 | 6112 | { |
6ca94ac9 | 6113 | this_free++; |
28a099a4 | 6114 | fblk->floats[i].u.chain = float_free_list; |
7146af97 JB |
6115 | float_free_list = &fblk->floats[i]; |
6116 | } | |
6117 | else | |
6118 | { | |
6119 | num_used++; | |
ab6780cd | 6120 | FLOAT_UNMARK (&fblk->floats[i]); |
7146af97 JB |
6121 | } |
6122 | lim = FLOAT_BLOCK_SIZE; | |
6ca94ac9 KH |
6123 | /* If this block contains only free floats and we have already |
6124 | seen more than two blocks worth of free floats then deallocate | |
6125 | this block. */ | |
6feef451 | 6126 | if (this_free == FLOAT_BLOCK_SIZE && num_free > FLOAT_BLOCK_SIZE) |
6ca94ac9 | 6127 | { |
6ca94ac9 KH |
6128 | *fprev = fblk->next; |
6129 | /* Unhook from the free list. */ | |
28a099a4 | 6130 | float_free_list = fblk->floats[0].u.chain; |
ab6780cd | 6131 | lisp_align_free (fblk); |
6ca94ac9 KH |
6132 | } |
6133 | else | |
6feef451 AS |
6134 | { |
6135 | num_free += this_free; | |
6136 | fprev = &fblk->next; | |
6137 | } | |
7146af97 JB |
6138 | } |
6139 | total_floats = num_used; | |
6140 | total_free_floats = num_free; | |
6141 | } | |
7146af97 | 6142 | |
d5e35230 JA |
6143 | /* Put all unmarked intervals on free list */ |
6144 | { | |
6145 | register struct interval_block *iblk; | |
6ca94ac9 | 6146 | struct interval_block **iprev = &interval_block; |
d5e35230 | 6147 | register int lim = interval_block_index; |
c0c5c8ae | 6148 | EMACS_INT num_free = 0, num_used = 0; |
d5e35230 JA |
6149 | |
6150 | interval_free_list = 0; | |
6151 | ||
6ca94ac9 | 6152 | for (iblk = interval_block; iblk; iblk = *iprev) |
d5e35230 JA |
6153 | { |
6154 | register int i; | |
6ca94ac9 | 6155 | int this_free = 0; |
d5e35230 JA |
6156 | |
6157 | for (i = 0; i < lim; i++) | |
6158 | { | |
2336fe58 | 6159 | if (!iblk->intervals[i].gcmarkbit) |
d5e35230 | 6160 | { |
0c94c8d6 | 6161 | set_interval_parent (&iblk->intervals[i], interval_free_list); |
d5e35230 | 6162 | interval_free_list = &iblk->intervals[i]; |
6ca94ac9 | 6163 | this_free++; |
d5e35230 JA |
6164 | } |
6165 | else | |
6166 | { | |
6167 | num_used++; | |
2336fe58 | 6168 | iblk->intervals[i].gcmarkbit = 0; |
d5e35230 JA |
6169 | } |
6170 | } | |
6171 | lim = INTERVAL_BLOCK_SIZE; | |
6ca94ac9 KH |
6172 | /* If this block contains only free intervals and we have already |
6173 | seen more than two blocks worth of free intervals then | |
6174 | deallocate this block. */ | |
6feef451 | 6175 | if (this_free == INTERVAL_BLOCK_SIZE && num_free > INTERVAL_BLOCK_SIZE) |
6ca94ac9 | 6176 | { |
6ca94ac9 KH |
6177 | *iprev = iblk->next; |
6178 | /* Unhook from the free list. */ | |
439d5cb4 | 6179 | interval_free_list = INTERVAL_PARENT (&iblk->intervals[0]); |
c8099634 | 6180 | lisp_free (iblk); |
6ca94ac9 KH |
6181 | } |
6182 | else | |
6feef451 AS |
6183 | { |
6184 | num_free += this_free; | |
6185 | iprev = &iblk->next; | |
6186 | } | |
d5e35230 JA |
6187 | } |
6188 | total_intervals = num_used; | |
6189 | total_free_intervals = num_free; | |
6190 | } | |
d5e35230 | 6191 | |
7146af97 JB |
6192 | /* Put all unmarked symbols on free list */ |
6193 | { | |
6194 | register struct symbol_block *sblk; | |
6ca94ac9 | 6195 | struct symbol_block **sprev = &symbol_block; |
7146af97 | 6196 | register int lim = symbol_block_index; |
c0c5c8ae | 6197 | EMACS_INT num_free = 0, num_used = 0; |
7146af97 | 6198 | |
d285b373 | 6199 | symbol_free_list = NULL; |
177c0ea7 | 6200 | |
6ca94ac9 | 6201 | for (sblk = symbol_block; sblk; sblk = *sprev) |
7146af97 | 6202 | { |
6ca94ac9 | 6203 | int this_free = 0; |
d55c12ed AS |
6204 | union aligned_Lisp_Symbol *sym = sblk->symbols; |
6205 | union aligned_Lisp_Symbol *end = sym + lim; | |
d285b373 GM |
6206 | |
6207 | for (; sym < end; ++sym) | |
6208 | { | |
20035321 SM |
6209 | /* Check if the symbol was created during loadup. In such a case |
6210 | it might be pointed to by pure bytecode which we don't trace, | |
6211 | so we conservatively assume that it is live. */ | |
fce31d69 | 6212 | bool pure_p = PURE_POINTER_P (XSTRING (sym->s.name)); |
177c0ea7 | 6213 | |
d55c12ed | 6214 | if (!sym->s.gcmarkbit && !pure_p) |
d285b373 | 6215 | { |
d55c12ed AS |
6216 | if (sym->s.redirect == SYMBOL_LOCALIZED) |
6217 | xfree (SYMBOL_BLV (&sym->s)); | |
6218 | sym->s.next = symbol_free_list; | |
6219 | symbol_free_list = &sym->s; | |
34400008 | 6220 | #if GC_MARK_STACK |
c644523b | 6221 | symbol_free_list->function = Vdead; |
34400008 | 6222 | #endif |
d285b373 GM |
6223 | ++this_free; |
6224 | } | |
6225 | else | |
6226 | { | |
6227 | ++num_used; | |
6228 | if (!pure_p) | |
c644523b | 6229 | UNMARK_STRING (XSTRING (sym->s.name)); |
d55c12ed | 6230 | sym->s.gcmarkbit = 0; |
d285b373 GM |
6231 | } |
6232 | } | |
177c0ea7 | 6233 | |
7146af97 | 6234 | lim = SYMBOL_BLOCK_SIZE; |
6ca94ac9 KH |
6235 | /* If this block contains only free symbols and we have already |
6236 | seen more than two blocks worth of free symbols then deallocate | |
6237 | this block. */ | |
6feef451 | 6238 | if (this_free == SYMBOL_BLOCK_SIZE && num_free > SYMBOL_BLOCK_SIZE) |
6ca94ac9 | 6239 | { |
6ca94ac9 KH |
6240 | *sprev = sblk->next; |
6241 | /* Unhook from the free list. */ | |
d55c12ed | 6242 | symbol_free_list = sblk->symbols[0].s.next; |
c8099634 | 6243 | lisp_free (sblk); |
6ca94ac9 KH |
6244 | } |
6245 | else | |
6feef451 AS |
6246 | { |
6247 | num_free += this_free; | |
6248 | sprev = &sblk->next; | |
6249 | } | |
7146af97 JB |
6250 | } |
6251 | total_symbols = num_used; | |
6252 | total_free_symbols = num_free; | |
6253 | } | |
6254 | ||
a9faeabe RS |
6255 | /* Put all unmarked misc's on free list. |
6256 | For a marker, first unchain it from the buffer it points into. */ | |
7146af97 JB |
6257 | { |
6258 | register struct marker_block *mblk; | |
6ca94ac9 | 6259 | struct marker_block **mprev = &marker_block; |
7146af97 | 6260 | register int lim = marker_block_index; |
c0c5c8ae | 6261 | EMACS_INT num_free = 0, num_used = 0; |
7146af97 JB |
6262 | |
6263 | marker_free_list = 0; | |
177c0ea7 | 6264 | |
6ca94ac9 | 6265 | for (mblk = marker_block; mblk; mblk = *mprev) |
7146af97 JB |
6266 | { |
6267 | register int i; | |
6ca94ac9 | 6268 | int this_free = 0; |
fa05e253 | 6269 | |
7146af97 | 6270 | for (i = 0; i < lim; i++) |
465edf35 | 6271 | { |
d55c12ed | 6272 | if (!mblk->markers[i].m.u_any.gcmarkbit) |
465edf35 | 6273 | { |
d55c12ed AS |
6274 | if (mblk->markers[i].m.u_any.type == Lisp_Misc_Marker) |
6275 | unchain_marker (&mblk->markers[i].m.u_marker); | |
fa05e253 RS |
6276 | /* Set the type of the freed object to Lisp_Misc_Free. |
6277 | We could leave the type alone, since nobody checks it, | |
465edf35 | 6278 | but this might catch bugs faster. */ |
d55c12ed AS |
6279 | mblk->markers[i].m.u_marker.type = Lisp_Misc_Free; |
6280 | mblk->markers[i].m.u_free.chain = marker_free_list; | |
6281 | marker_free_list = &mblk->markers[i].m; | |
6ca94ac9 | 6282 | this_free++; |
465edf35 KH |
6283 | } |
6284 | else | |
6285 | { | |
6286 | num_used++; | |
d55c12ed | 6287 | mblk->markers[i].m.u_any.gcmarkbit = 0; |
465edf35 KH |
6288 | } |
6289 | } | |
7146af97 | 6290 | lim = MARKER_BLOCK_SIZE; |
6ca94ac9 KH |
6291 | /* If this block contains only free markers and we have already |
6292 | seen more than two blocks worth of free markers then deallocate | |
6293 | this block. */ | |
6feef451 | 6294 | if (this_free == MARKER_BLOCK_SIZE && num_free > MARKER_BLOCK_SIZE) |
6ca94ac9 | 6295 | { |
6ca94ac9 KH |
6296 | *mprev = mblk->next; |
6297 | /* Unhook from the free list. */ | |
d55c12ed | 6298 | marker_free_list = mblk->markers[0].m.u_free.chain; |
c8099634 | 6299 | lisp_free (mblk); |
6ca94ac9 KH |
6300 | } |
6301 | else | |
6feef451 AS |
6302 | { |
6303 | num_free += this_free; | |
6304 | mprev = &mblk->next; | |
6305 | } | |
7146af97 JB |
6306 | } |
6307 | ||
6308 | total_markers = num_used; | |
6309 | total_free_markers = num_free; | |
6310 | } | |
6311 | ||
6312 | /* Free all unmarked buffers */ | |
6313 | { | |
3e98c68e | 6314 | register struct buffer *buffer, **bprev = &all_buffers; |
7146af97 | 6315 | |
3ab6e069 | 6316 | total_buffers = 0; |
3e98c68e | 6317 | for (buffer = all_buffers; buffer; buffer = *bprev) |
3ef06d12 | 6318 | if (!VECTOR_MARKED_P (buffer)) |
7146af97 | 6319 | { |
3e98c68e | 6320 | *bprev = buffer->header.next.buffer; |
34400008 | 6321 | lisp_free (buffer); |
7146af97 JB |
6322 | } |
6323 | else | |
6324 | { | |
3ef06d12 | 6325 | VECTOR_UNMARK (buffer); |
8707c1e5 DA |
6326 | /* Do not use buffer_(set|get)_intervals here. */ |
6327 | buffer->text->intervals = balance_intervals (buffer->text->intervals); | |
3ab6e069 | 6328 | total_buffers++; |
3e98c68e | 6329 | bprev = &buffer->header.next.buffer; |
7146af97 JB |
6330 | } |
6331 | } | |
6332 | ||
f3372c87 | 6333 | sweep_vectors (); |
e499d0ee | 6334 | check_string_bytes (!noninteractive); |
7146af97 | 6335 | } |
7146af97 | 6336 | |
7146af97 | 6337 | |
7146af97 | 6338 | |
7146af97 | 6339 | \f |
20d24714 JB |
6340 | /* Debugging aids. */ |
6341 | ||
31ce1c91 | 6342 | DEFUN ("memory-limit", Fmemory_limit, Smemory_limit, 0, 0, 0, |
a6266d23 | 6343 | doc: /* Return the address of the last byte Emacs has allocated, divided by 1024. |
228299fa | 6344 | This may be helpful in debugging Emacs's memory usage. |
7ee72033 | 6345 | We divide the value by 1024 to make sure it fits in a Lisp integer. */) |
5842a27b | 6346 | (void) |
20d24714 JB |
6347 | { |
6348 | Lisp_Object end; | |
6349 | ||
d01a7826 | 6350 | XSETINT (end, (intptr_t) (char *) sbrk (0) / 1024); |
20d24714 JB |
6351 | |
6352 | return end; | |
6353 | } | |
6354 | ||
310ea200 | 6355 | DEFUN ("memory-use-counts", Fmemory_use_counts, Smemory_use_counts, 0, 0, 0, |
a6266d23 | 6356 | doc: /* Return a list of counters that measure how much consing there has been. |
228299fa GM |
6357 | Each of these counters increments for a certain kind of object. |
6358 | The counters wrap around from the largest positive integer to zero. | |
6359 | Garbage collection does not decrease them. | |
6360 | The elements of the value are as follows: | |
6361 | (CONSES FLOATS VECTOR-CELLS SYMBOLS STRING-CHARS MISCS INTERVALS STRINGS) | |
6362 | All are in units of 1 = one object consed | |
6363 | except for VECTOR-CELLS and STRING-CHARS, which count the total length of | |
6364 | objects consed. | |
6365 | MISCS include overlays, markers, and some internal types. | |
6366 | Frames, windows, buffers, and subprocesses count as vectors | |
7ee72033 | 6367 | (but the contents of a buffer's text do not count here). */) |
5842a27b | 6368 | (void) |
310ea200 | 6369 | { |
3438fe21 | 6370 | return listn (CONSTYPE_HEAP, 8, |
694b6c97 DA |
6371 | bounded_number (cons_cells_consed), |
6372 | bounded_number (floats_consed), | |
6373 | bounded_number (vector_cells_consed), | |
6374 | bounded_number (symbols_consed), | |
6375 | bounded_number (string_chars_consed), | |
6376 | bounded_number (misc_objects_consed), | |
6377 | bounded_number (intervals_consed), | |
6378 | bounded_number (strings_consed)); | |
310ea200 | 6379 | } |
e0b8c689 | 6380 | |
8b058d44 EZ |
6381 | /* Find at most FIND_MAX symbols which have OBJ as their value or |
6382 | function. This is used in gdbinit's `xwhichsymbols' command. */ | |
6383 | ||
6384 | Lisp_Object | |
196e41e4 | 6385 | which_symbols (Lisp_Object obj, EMACS_INT find_max) |
8b058d44 EZ |
6386 | { |
6387 | struct symbol_block *sblk; | |
8d0eb4c2 | 6388 | ptrdiff_t gc_count = inhibit_garbage_collection (); |
8b058d44 EZ |
6389 | Lisp_Object found = Qnil; |
6390 | ||
ca78dc43 | 6391 | if (! DEADP (obj)) |
8b058d44 EZ |
6392 | { |
6393 | for (sblk = symbol_block; sblk; sblk = sblk->next) | |
6394 | { | |
9426aba4 | 6395 | union aligned_Lisp_Symbol *aligned_sym = sblk->symbols; |
8b058d44 EZ |
6396 | int bn; |
6397 | ||
9426aba4 | 6398 | for (bn = 0; bn < SYMBOL_BLOCK_SIZE; bn++, aligned_sym++) |
8b058d44 | 6399 | { |
9426aba4 | 6400 | struct Lisp_Symbol *sym = &aligned_sym->s; |
8b058d44 EZ |
6401 | Lisp_Object val; |
6402 | Lisp_Object tem; | |
6403 | ||
6404 | if (sblk == symbol_block && bn >= symbol_block_index) | |
6405 | break; | |
6406 | ||
6407 | XSETSYMBOL (tem, sym); | |
6408 | val = find_symbol_value (tem); | |
6409 | if (EQ (val, obj) | |
c644523b DA |
6410 | || EQ (sym->function, obj) |
6411 | || (!NILP (sym->function) | |
6412 | && COMPILEDP (sym->function) | |
6413 | && EQ (AREF (sym->function, COMPILED_BYTECODE), obj)) | |
8b058d44 EZ |
6414 | || (!NILP (val) |
6415 | && COMPILEDP (val) | |
6416 | && EQ (AREF (val, COMPILED_BYTECODE), obj))) | |
6417 | { | |
6418 | found = Fcons (tem, found); | |
6419 | if (--find_max == 0) | |
6420 | goto out; | |
6421 | } | |
6422 | } | |
6423 | } | |
6424 | } | |
6425 | ||
6426 | out: | |
6427 | unbind_to (gc_count, Qnil); | |
6428 | return found; | |
6429 | } | |
6430 | ||
244ed907 | 6431 | #ifdef ENABLE_CHECKING |
f4a681b0 | 6432 | |
fce31d69 | 6433 | bool suppress_checking; |
d3d47262 | 6434 | |
e0b8c689 | 6435 | void |
971de7fb | 6436 | die (const char *msg, const char *file, int line) |
e0b8c689 | 6437 | { |
67ee9f6e | 6438 | fprintf (stderr, "\r\n%s:%d: Emacs fatal error: %s\r\n", |
e0b8c689 | 6439 | file, line, msg); |
4d7e6e51 | 6440 | terminate_due_to_signal (SIGABRT, INT_MAX); |
e0b8c689 | 6441 | } |
244ed907 | 6442 | #endif |
20d24714 | 6443 | \f |
7146af97 JB |
6444 | /* Initialization */ |
6445 | ||
dfcf069d | 6446 | void |
971de7fb | 6447 | init_alloc_once (void) |
7146af97 JB |
6448 | { |
6449 | /* Used to do Vpurify_flag = Qt here, but Qt isn't set up yet! */ | |
9e713715 GM |
6450 | purebeg = PUREBEG; |
6451 | pure_size = PURESIZE; | |
ab6780cd | 6452 | |
877935b1 | 6453 | #if GC_MARK_STACK || defined GC_MALLOC_CHECK |
34400008 GM |
6454 | mem_init (); |
6455 | Vdead = make_pure_string ("DEAD", 4, 4, 0); | |
6456 | #endif | |
9e713715 | 6457 | |
d1658221 RS |
6458 | #ifdef DOUG_LEA_MALLOC |
6459 | mallopt (M_TRIM_THRESHOLD, 128*1024); /* trim threshold */ | |
6460 | mallopt (M_MMAP_THRESHOLD, 64*1024); /* mmap threshold */ | |
81d492d5 | 6461 | mallopt (M_MMAP_MAX, MMAP_MAX_AREAS); /* max. number of mmap'ed areas */ |
d1658221 | 6462 | #endif |
7146af97 | 6463 | init_strings (); |
f3372c87 | 6464 | init_vectors (); |
d5e35230 | 6465 | |
24d8a105 | 6466 | refill_memory_reserve (); |
0dd6d66d | 6467 | gc_cons_threshold = GC_DEFAULT_THRESHOLD; |
7146af97 JB |
6468 | } |
6469 | ||
dfcf069d | 6470 | void |
971de7fb | 6471 | init_alloc (void) |
7146af97 JB |
6472 | { |
6473 | gcprolist = 0; | |
630686c8 | 6474 | byte_stack_list = 0; |
182ff242 GM |
6475 | #if GC_MARK_STACK |
6476 | #if !defined GC_SAVE_REGISTERS_ON_STACK && !defined GC_SETJMP_WORKS | |
6477 | setjmp_tested_p = longjmps_done = 0; | |
6478 | #endif | |
6479 | #endif | |
2c5bd608 DL |
6480 | Vgc_elapsed = make_float (0.0); |
6481 | gcs_done = 0; | |
7146af97 JB |
6482 | } |
6483 | ||
6484 | void | |
971de7fb | 6485 | syms_of_alloc (void) |
7146af97 | 6486 | { |
29208e82 | 6487 | DEFVAR_INT ("gc-cons-threshold", gc_cons_threshold, |
fb7ada5f | 6488 | doc: /* Number of bytes of consing between garbage collections. |
228299fa GM |
6489 | Garbage collection can happen automatically once this many bytes have been |
6490 | allocated since the last garbage collection. All data types count. | |
7146af97 | 6491 | |
228299fa | 6492 | Garbage collection happens automatically only when `eval' is called. |
7146af97 | 6493 | |
228299fa | 6494 | By binding this temporarily to a large number, you can effectively |
96f077ad SM |
6495 | prevent garbage collection during a part of the program. |
6496 | See also `gc-cons-percentage'. */); | |
6497 | ||
29208e82 | 6498 | DEFVAR_LISP ("gc-cons-percentage", Vgc_cons_percentage, |
fb7ada5f | 6499 | doc: /* Portion of the heap used for allocation. |
96f077ad SM |
6500 | Garbage collection can happen automatically once this portion of the heap |
6501 | has been allocated since the last garbage collection. | |
6502 | If this portion is smaller than `gc-cons-threshold', this is ignored. */); | |
6503 | Vgc_cons_percentage = make_float (0.1); | |
0819585c | 6504 | |
29208e82 | 6505 | DEFVAR_INT ("pure-bytes-used", pure_bytes_used, |
333f9019 | 6506 | doc: /* Number of bytes of shareable Lisp data allocated so far. */); |
0819585c | 6507 | |
29208e82 | 6508 | DEFVAR_INT ("cons-cells-consed", cons_cells_consed, |
a6266d23 | 6509 | doc: /* Number of cons cells that have been consed so far. */); |
0819585c | 6510 | |
29208e82 | 6511 | DEFVAR_INT ("floats-consed", floats_consed, |
a6266d23 | 6512 | doc: /* Number of floats that have been consed so far. */); |
0819585c | 6513 | |
29208e82 | 6514 | DEFVAR_INT ("vector-cells-consed", vector_cells_consed, |
a6266d23 | 6515 | doc: /* Number of vector cells that have been consed so far. */); |
0819585c | 6516 | |
29208e82 | 6517 | DEFVAR_INT ("symbols-consed", symbols_consed, |
a6266d23 | 6518 | doc: /* Number of symbols that have been consed so far. */); |
0819585c | 6519 | |
29208e82 | 6520 | DEFVAR_INT ("string-chars-consed", string_chars_consed, |
a6266d23 | 6521 | doc: /* Number of string characters that have been consed so far. */); |
0819585c | 6522 | |
29208e82 | 6523 | DEFVAR_INT ("misc-objects-consed", misc_objects_consed, |
01a6dcc8 GM |
6524 | doc: /* Number of miscellaneous objects that have been consed so far. |
6525 | These include markers and overlays, plus certain objects not visible | |
6526 | to users. */); | |
2e471eb5 | 6527 | |
29208e82 | 6528 | DEFVAR_INT ("intervals-consed", intervals_consed, |
a6266d23 | 6529 | doc: /* Number of intervals that have been consed so far. */); |
7146af97 | 6530 | |
29208e82 | 6531 | DEFVAR_INT ("strings-consed", strings_consed, |
a6266d23 | 6532 | doc: /* Number of strings that have been consed so far. */); |
228299fa | 6533 | |
29208e82 | 6534 | DEFVAR_LISP ("purify-flag", Vpurify_flag, |
a6266d23 | 6535 | doc: /* Non-nil means loading Lisp code in order to dump an executable. |
e9515805 SM |
6536 | This means that certain objects should be allocated in shared (pure) space. |
6537 | It can also be set to a hash-table, in which case this table is used to | |
6538 | do hash-consing of the objects allocated to pure space. */); | |
228299fa | 6539 | |
29208e82 | 6540 | DEFVAR_BOOL ("garbage-collection-messages", garbage_collection_messages, |
a6266d23 | 6541 | doc: /* Non-nil means display messages at start and end of garbage collection. */); |
299585ee RS |
6542 | garbage_collection_messages = 0; |
6543 | ||
29208e82 | 6544 | DEFVAR_LISP ("post-gc-hook", Vpost_gc_hook, |
a6266d23 | 6545 | doc: /* Hook run after garbage collection has finished. */); |
9e713715 | 6546 | Vpost_gc_hook = Qnil; |
cd3520a4 | 6547 | DEFSYM (Qpost_gc_hook, "post-gc-hook"); |
9e713715 | 6548 | |
29208e82 | 6549 | DEFVAR_LISP ("memory-signal-data", Vmemory_signal_data, |
74a54b04 | 6550 | doc: /* Precomputed `signal' argument for memory-full error. */); |
bcb61d60 KH |
6551 | /* We build this in advance because if we wait until we need it, we might |
6552 | not be able to allocate the memory to hold it. */ | |
74a54b04 | 6553 | Vmemory_signal_data |
3438fe21 | 6554 | = listn (CONSTYPE_PURE, 2, Qerror, |
694b6c97 | 6555 | build_pure_c_string ("Memory exhausted--use M-x save-some-buffers then exit and restart Emacs")); |
74a54b04 | 6556 | |
29208e82 | 6557 | DEFVAR_LISP ("memory-full", Vmemory_full, |
24d8a105 | 6558 | doc: /* Non-nil means Emacs cannot get much more Lisp memory. */); |
74a54b04 | 6559 | Vmemory_full = Qnil; |
bcb61d60 | 6560 | |
fecbd8ff SM |
6561 | DEFSYM (Qconses, "conses"); |
6562 | DEFSYM (Qsymbols, "symbols"); | |
6563 | DEFSYM (Qmiscs, "miscs"); | |
6564 | DEFSYM (Qstrings, "strings"); | |
6565 | DEFSYM (Qvectors, "vectors"); | |
6566 | DEFSYM (Qfloats, "floats"); | |
6567 | DEFSYM (Qintervals, "intervals"); | |
6568 | DEFSYM (Qbuffers, "buffers"); | |
5b835e1d DA |
6569 | DEFSYM (Qstring_bytes, "string-bytes"); |
6570 | DEFSYM (Qvector_slots, "vector-slots"); | |
f8643a6b | 6571 | DEFSYM (Qheap, "heap"); |
3d80c99f | 6572 | DEFSYM (Qautomatic_gc, "Automatic GC"); |
5b835e1d | 6573 | |
cd3520a4 JB |
6574 | DEFSYM (Qgc_cons_threshold, "gc-cons-threshold"); |
6575 | DEFSYM (Qchar_table_extra_slots, "char-table-extra-slots"); | |
a59de17b | 6576 | |
29208e82 | 6577 | DEFVAR_LISP ("gc-elapsed", Vgc_elapsed, |
2c5bd608 | 6578 | doc: /* Accumulated time elapsed in garbage collections. |
e7415487 | 6579 | The time is in seconds as a floating point value. */); |
29208e82 | 6580 | DEFVAR_INT ("gcs-done", gcs_done, |
e7415487 | 6581 | doc: /* Accumulated number of garbage collections done. */); |
2c5bd608 | 6582 | |
7146af97 JB |
6583 | defsubr (&Scons); |
6584 | defsubr (&Slist); | |
6585 | defsubr (&Svector); | |
6586 | defsubr (&Smake_byte_code); | |
6587 | defsubr (&Smake_list); | |
6588 | defsubr (&Smake_vector); | |
6589 | defsubr (&Smake_string); | |
7b07587b | 6590 | defsubr (&Smake_bool_vector); |
7146af97 JB |
6591 | defsubr (&Smake_symbol); |
6592 | defsubr (&Smake_marker); | |
6593 | defsubr (&Spurecopy); | |
6594 | defsubr (&Sgarbage_collect); | |
20d24714 | 6595 | defsubr (&Smemory_limit); |
310ea200 | 6596 | defsubr (&Smemory_use_counts); |
34400008 GM |
6597 | |
6598 | #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES | |
6599 | defsubr (&Sgc_status); | |
6600 | #endif | |
7146af97 | 6601 | } |
5eceb8fb | 6602 | |
4706125e PE |
6603 | /* When compiled with GCC, GDB might say "No enum type named |
6604 | pvec_type" if we don't have at least one symbol with that type, and | |
6605 | then xbacktrace could fail. Similarly for the other enums and | |
62aba0d4 FP |
6606 | their values. Some non-GCC compilers don't like these constructs. */ |
6607 | #ifdef __GNUC__ | |
4706125e PE |
6608 | union |
6609 | { | |
03a660a6 PE |
6610 | enum CHARTAB_SIZE_BITS CHARTAB_SIZE_BITS; |
6611 | enum CHAR_TABLE_STANDARD_SLOTS CHAR_TABLE_STANDARD_SLOTS; | |
6612 | enum char_bits char_bits; | |
4706125e | 6613 | enum CHECK_LISP_OBJECT_TYPE CHECK_LISP_OBJECT_TYPE; |
03a660a6 | 6614 | enum DEFAULT_HASH_SIZE DEFAULT_HASH_SIZE; |
4706125e | 6615 | enum enum_USE_LSB_TAG enum_USE_LSB_TAG; |
03a660a6 | 6616 | enum FLOAT_TO_STRING_BUFSIZE FLOAT_TO_STRING_BUFSIZE; |
4706125e | 6617 | enum Lisp_Bits Lisp_Bits; |
03a660a6 PE |
6618 | enum Lisp_Compiled Lisp_Compiled; |
6619 | enum maxargs maxargs; | |
6620 | enum MAX_ALLOCA MAX_ALLOCA; | |
4706125e PE |
6621 | enum More_Lisp_Bits More_Lisp_Bits; |
6622 | enum pvec_type pvec_type; | |
c32af1e4 PE |
6623 | #if USE_LSB_TAG |
6624 | enum lsb_bits lsb_bits; | |
6625 | #endif | |
4706125e | 6626 | } const EXTERNALLY_VISIBLE gdb_make_enums_visible = {0}; |
62aba0d4 | 6627 | #endif /* __GNUC__ */ |