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