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