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