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