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