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