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