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 | |
229b28c4 KH |
1822 | string as multibyte. If NCHARS is negative, it counts the number of |
1823 | characters by itself. */ | |
2e471eb5 GM |
1824 | |
1825 | Lisp_Object | |
1826 | make_specified_string (contents, nchars, nbytes, multibyte) | |
1827 | char *contents; | |
1828 | int nchars, nbytes; | |
1829 | int multibyte; | |
1830 | { | |
1831 | register Lisp_Object val; | |
229b28c4 KH |
1832 | |
1833 | if (nchars < 0) | |
1834 | { | |
1835 | if (multibyte) | |
1836 | nchars = multibyte_chars_in_text (contents, nbytes); | |
1837 | else | |
1838 | nchars = nbytes; | |
1839 | } | |
2e471eb5 | 1840 | val = make_uninit_multibyte_string (nchars, nbytes); |
d5db4077 | 1841 | bcopy (contents, SDATA (val), nbytes); |
2e471eb5 | 1842 | if (!multibyte) |
d5db4077 | 1843 | STRING_SET_UNIBYTE (val); |
2e471eb5 GM |
1844 | return val; |
1845 | } | |
1846 | ||
1847 | ||
1848 | /* Make a string from the data at STR, treating it as multibyte if the | |
1849 | data warrants. */ | |
1850 | ||
1851 | Lisp_Object | |
1852 | build_string (str) | |
943b873e | 1853 | const char *str; |
2e471eb5 GM |
1854 | { |
1855 | return make_string (str, strlen (str)); | |
1856 | } | |
1857 | ||
1858 | ||
1859 | /* Return an unibyte Lisp_String set up to hold LENGTH characters | |
1860 | occupying LENGTH bytes. */ | |
1861 | ||
1862 | Lisp_Object | |
1863 | make_uninit_string (length) | |
1864 | int length; | |
1865 | { | |
1866 | Lisp_Object val; | |
1867 | val = make_uninit_multibyte_string (length, length); | |
d5db4077 | 1868 | STRING_SET_UNIBYTE (val); |
2e471eb5 GM |
1869 | return val; |
1870 | } | |
1871 | ||
1872 | ||
1873 | /* Return a multibyte Lisp_String set up to hold NCHARS characters | |
1874 | which occupy NBYTES bytes. */ | |
1875 | ||
1876 | Lisp_Object | |
1877 | make_uninit_multibyte_string (nchars, nbytes) | |
1878 | int nchars, nbytes; | |
1879 | { | |
1880 | Lisp_Object string; | |
1881 | struct Lisp_String *s; | |
1882 | ||
1883 | if (nchars < 0) | |
1884 | abort (); | |
1885 | ||
1886 | s = allocate_string (); | |
1887 | allocate_string_data (s, nchars, nbytes); | |
1888 | XSETSTRING (string, s); | |
1889 | string_chars_consed += nbytes; | |
1890 | return string; | |
1891 | } | |
1892 | ||
1893 | ||
1894 | \f | |
1895 | /*********************************************************************** | |
1896 | Float Allocation | |
1897 | ***********************************************************************/ | |
1898 | ||
2e471eb5 GM |
1899 | /* We store float cells inside of float_blocks, allocating a new |
1900 | float_block with malloc whenever necessary. Float cells reclaimed | |
1901 | by GC are put on a free list to be reallocated before allocating | |
1902 | any new float cells from the latest float_block. | |
1903 | ||
1904 | Each float_block is just under 1020 bytes long, since malloc really | |
1905 | allocates in units of powers of two and uses 4 bytes for its own | |
1906 | overhead. */ | |
1907 | ||
1908 | #define FLOAT_BLOCK_SIZE \ | |
1909 | ((1020 - sizeof (struct float_block *)) / sizeof (struct Lisp_Float)) | |
1910 | ||
1911 | struct float_block | |
1912 | { | |
1913 | struct float_block *next; | |
1914 | struct Lisp_Float floats[FLOAT_BLOCK_SIZE]; | |
1915 | }; | |
1916 | ||
34400008 GM |
1917 | /* Current float_block. */ |
1918 | ||
2e471eb5 | 1919 | struct float_block *float_block; |
34400008 GM |
1920 | |
1921 | /* Index of first unused Lisp_Float in the current float_block. */ | |
1922 | ||
2e471eb5 GM |
1923 | int float_block_index; |
1924 | ||
1925 | /* Total number of float blocks now in use. */ | |
1926 | ||
1927 | int n_float_blocks; | |
1928 | ||
34400008 GM |
1929 | /* Free-list of Lisp_Floats. */ |
1930 | ||
2e471eb5 GM |
1931 | struct Lisp_Float *float_free_list; |
1932 | ||
34400008 | 1933 | |
966533c9 | 1934 | /* Initialize float allocation. */ |
34400008 | 1935 | |
2e471eb5 GM |
1936 | void |
1937 | init_float () | |
1938 | { | |
34400008 GM |
1939 | float_block = (struct float_block *) lisp_malloc (sizeof *float_block, |
1940 | MEM_TYPE_FLOAT); | |
2e471eb5 GM |
1941 | float_block->next = 0; |
1942 | bzero ((char *) float_block->floats, sizeof float_block->floats); | |
1943 | float_block_index = 0; | |
1944 | float_free_list = 0; | |
1945 | n_float_blocks = 1; | |
1946 | } | |
1947 | ||
34400008 GM |
1948 | |
1949 | /* Explicitly free a float cell by putting it on the free-list. */ | |
2e471eb5 GM |
1950 | |
1951 | void | |
1952 | free_float (ptr) | |
1953 | struct Lisp_Float *ptr; | |
1954 | { | |
1955 | *(struct Lisp_Float **)&ptr->data = float_free_list; | |
34400008 GM |
1956 | #if GC_MARK_STACK |
1957 | ptr->type = Vdead; | |
1958 | #endif | |
2e471eb5 GM |
1959 | float_free_list = ptr; |
1960 | } | |
1961 | ||
34400008 GM |
1962 | |
1963 | /* Return a new float object with value FLOAT_VALUE. */ | |
1964 | ||
2e471eb5 GM |
1965 | Lisp_Object |
1966 | make_float (float_value) | |
1967 | double float_value; | |
1968 | { | |
1969 | register Lisp_Object val; | |
1970 | ||
1971 | if (float_free_list) | |
1972 | { | |
1973 | /* We use the data field for chaining the free list | |
1974 | so that we won't use the same field that has the mark bit. */ | |
1975 | XSETFLOAT (val, float_free_list); | |
1976 | float_free_list = *(struct Lisp_Float **)&float_free_list->data; | |
1977 | } | |
1978 | else | |
1979 | { | |
1980 | if (float_block_index == FLOAT_BLOCK_SIZE) | |
1981 | { | |
1982 | register struct float_block *new; | |
1983 | ||
34400008 GM |
1984 | new = (struct float_block *) lisp_malloc (sizeof *new, |
1985 | MEM_TYPE_FLOAT); | |
2e471eb5 GM |
1986 | VALIDATE_LISP_STORAGE (new, sizeof *new); |
1987 | new->next = float_block; | |
1988 | float_block = new; | |
1989 | float_block_index = 0; | |
1990 | n_float_blocks++; | |
1991 | } | |
1992 | XSETFLOAT (val, &float_block->floats[float_block_index++]); | |
1993 | } | |
177c0ea7 | 1994 | |
2e471eb5 GM |
1995 | XFLOAT_DATA (val) = float_value; |
1996 | XSETFASTINT (XFLOAT (val)->type, 0); /* bug chasing -wsr */ | |
1997 | consing_since_gc += sizeof (struct Lisp_Float); | |
1998 | floats_consed++; | |
1999 | return val; | |
2000 | } | |
2001 | ||
2e471eb5 GM |
2002 | |
2003 | \f | |
2004 | /*********************************************************************** | |
2005 | Cons Allocation | |
2006 | ***********************************************************************/ | |
2007 | ||
2008 | /* We store cons cells inside of cons_blocks, allocating a new | |
2009 | cons_block with malloc whenever necessary. Cons cells reclaimed by | |
2010 | GC are put on a free list to be reallocated before allocating | |
2011 | any new cons cells from the latest cons_block. | |
2012 | ||
2013 | Each cons_block is just under 1020 bytes long, | |
2014 | since malloc really allocates in units of powers of two | |
2015 | and uses 4 bytes for its own overhead. */ | |
2016 | ||
2017 | #define CONS_BLOCK_SIZE \ | |
2018 | ((1020 - sizeof (struct cons_block *)) / sizeof (struct Lisp_Cons)) | |
2019 | ||
2020 | struct cons_block | |
2021 | { | |
2022 | struct cons_block *next; | |
2023 | struct Lisp_Cons conses[CONS_BLOCK_SIZE]; | |
2024 | }; | |
2025 | ||
34400008 GM |
2026 | /* Current cons_block. */ |
2027 | ||
2e471eb5 | 2028 | struct cons_block *cons_block; |
34400008 GM |
2029 | |
2030 | /* Index of first unused Lisp_Cons in the current block. */ | |
2031 | ||
2e471eb5 GM |
2032 | int cons_block_index; |
2033 | ||
34400008 GM |
2034 | /* Free-list of Lisp_Cons structures. */ |
2035 | ||
2e471eb5 GM |
2036 | struct Lisp_Cons *cons_free_list; |
2037 | ||
2038 | /* Total number of cons blocks now in use. */ | |
2039 | ||
2040 | int n_cons_blocks; | |
2041 | ||
34400008 GM |
2042 | |
2043 | /* Initialize cons allocation. */ | |
2044 | ||
2e471eb5 GM |
2045 | void |
2046 | init_cons () | |
2047 | { | |
34400008 GM |
2048 | cons_block = (struct cons_block *) lisp_malloc (sizeof *cons_block, |
2049 | MEM_TYPE_CONS); | |
2e471eb5 GM |
2050 | cons_block->next = 0; |
2051 | bzero ((char *) cons_block->conses, sizeof cons_block->conses); | |
2052 | cons_block_index = 0; | |
2053 | cons_free_list = 0; | |
2054 | n_cons_blocks = 1; | |
2055 | } | |
2056 | ||
34400008 GM |
2057 | |
2058 | /* Explicitly free a cons cell by putting it on the free-list. */ | |
2e471eb5 GM |
2059 | |
2060 | void | |
2061 | free_cons (ptr) | |
2062 | struct Lisp_Cons *ptr; | |
2063 | { | |
2064 | *(struct Lisp_Cons **)&ptr->cdr = cons_free_list; | |
34400008 GM |
2065 | #if GC_MARK_STACK |
2066 | ptr->car = Vdead; | |
2067 | #endif | |
2e471eb5 GM |
2068 | cons_free_list = ptr; |
2069 | } | |
2070 | ||
34400008 | 2071 | |
2e471eb5 | 2072 | DEFUN ("cons", Fcons, Scons, 2, 2, 0, |
a6266d23 | 2073 | doc: /* Create a new cons, give it CAR and CDR as components, and return it. */) |
7ee72033 | 2074 | (car, cdr) |
2e471eb5 GM |
2075 | Lisp_Object car, cdr; |
2076 | { | |
2077 | register Lisp_Object val; | |
2078 | ||
2079 | if (cons_free_list) | |
2080 | { | |
2081 | /* We use the cdr for chaining the free list | |
2082 | so that we won't use the same field that has the mark bit. */ | |
2083 | XSETCONS (val, cons_free_list); | |
2084 | cons_free_list = *(struct Lisp_Cons **)&cons_free_list->cdr; | |
2085 | } | |
2086 | else | |
2087 | { | |
2088 | if (cons_block_index == CONS_BLOCK_SIZE) | |
2089 | { | |
2090 | register struct cons_block *new; | |
34400008 GM |
2091 | new = (struct cons_block *) lisp_malloc (sizeof *new, |
2092 | MEM_TYPE_CONS); | |
2e471eb5 GM |
2093 | VALIDATE_LISP_STORAGE (new, sizeof *new); |
2094 | new->next = cons_block; | |
2095 | cons_block = new; | |
2096 | cons_block_index = 0; | |
2097 | n_cons_blocks++; | |
2098 | } | |
2099 | XSETCONS (val, &cons_block->conses[cons_block_index++]); | |
2100 | } | |
177c0ea7 | 2101 | |
f3fbd155 KR |
2102 | XSETCAR (val, car); |
2103 | XSETCDR (val, cdr); | |
2e471eb5 GM |
2104 | consing_since_gc += sizeof (struct Lisp_Cons); |
2105 | cons_cells_consed++; | |
2106 | return val; | |
2107 | } | |
2108 | ||
34400008 | 2109 | |
2e471eb5 GM |
2110 | /* Make a list of 2, 3, 4 or 5 specified objects. */ |
2111 | ||
2112 | Lisp_Object | |
2113 | list2 (arg1, arg2) | |
2114 | Lisp_Object arg1, arg2; | |
2115 | { | |
2116 | return Fcons (arg1, Fcons (arg2, Qnil)); | |
2117 | } | |
2118 | ||
34400008 | 2119 | |
2e471eb5 GM |
2120 | Lisp_Object |
2121 | list3 (arg1, arg2, arg3) | |
2122 | Lisp_Object arg1, arg2, arg3; | |
2123 | { | |
2124 | return Fcons (arg1, Fcons (arg2, Fcons (arg3, Qnil))); | |
2125 | } | |
2126 | ||
34400008 | 2127 | |
2e471eb5 GM |
2128 | Lisp_Object |
2129 | list4 (arg1, arg2, arg3, arg4) | |
2130 | Lisp_Object arg1, arg2, arg3, arg4; | |
2131 | { | |
2132 | return Fcons (arg1, Fcons (arg2, Fcons (arg3, Fcons (arg4, Qnil)))); | |
2133 | } | |
2134 | ||
34400008 | 2135 | |
2e471eb5 GM |
2136 | Lisp_Object |
2137 | list5 (arg1, arg2, arg3, arg4, arg5) | |
2138 | Lisp_Object arg1, arg2, arg3, arg4, arg5; | |
2139 | { | |
2140 | return Fcons (arg1, Fcons (arg2, Fcons (arg3, Fcons (arg4, | |
2141 | Fcons (arg5, Qnil))))); | |
2142 | } | |
2143 | ||
34400008 | 2144 | |
2e471eb5 | 2145 | DEFUN ("list", Flist, Slist, 0, MANY, 0, |
eae936e2 | 2146 | doc: /* Return a newly created list with specified arguments as elements. |
ae8e8122 MB |
2147 | Any number of arguments, even zero arguments, are allowed. |
2148 | usage: (list &rest OBJECTS) */) | |
7ee72033 | 2149 | (nargs, args) |
2e471eb5 GM |
2150 | int nargs; |
2151 | register Lisp_Object *args; | |
2152 | { | |
2153 | register Lisp_Object val; | |
2154 | val = Qnil; | |
2155 | ||
2156 | while (nargs > 0) | |
2157 | { | |
2158 | nargs--; | |
2159 | val = Fcons (args[nargs], val); | |
2160 | } | |
2161 | return val; | |
2162 | } | |
2163 | ||
34400008 | 2164 | |
2e471eb5 | 2165 | DEFUN ("make-list", Fmake_list, Smake_list, 2, 2, 0, |
a6266d23 | 2166 | doc: /* Return a newly created list of length LENGTH, with each element being INIT. */) |
7ee72033 | 2167 | (length, init) |
2e471eb5 GM |
2168 | register Lisp_Object length, init; |
2169 | { | |
2170 | register Lisp_Object val; | |
2171 | register int size; | |
2172 | ||
b7826503 | 2173 | CHECK_NATNUM (length); |
2e471eb5 GM |
2174 | size = XFASTINT (length); |
2175 | ||
2176 | val = Qnil; | |
ce070307 GM |
2177 | while (size > 0) |
2178 | { | |
2179 | val = Fcons (init, val); | |
2180 | --size; | |
2181 | ||
2182 | if (size > 0) | |
2183 | { | |
2184 | val = Fcons (init, val); | |
2185 | --size; | |
177c0ea7 | 2186 | |
ce070307 GM |
2187 | if (size > 0) |
2188 | { | |
2189 | val = Fcons (init, val); | |
2190 | --size; | |
177c0ea7 | 2191 | |
ce070307 GM |
2192 | if (size > 0) |
2193 | { | |
2194 | val = Fcons (init, val); | |
2195 | --size; | |
177c0ea7 | 2196 | |
ce070307 GM |
2197 | if (size > 0) |
2198 | { | |
2199 | val = Fcons (init, val); | |
2200 | --size; | |
2201 | } | |
2202 | } | |
2203 | } | |
2204 | } | |
2205 | ||
2206 | QUIT; | |
2207 | } | |
177c0ea7 | 2208 | |
7146af97 JB |
2209 | return val; |
2210 | } | |
2e471eb5 GM |
2211 | |
2212 | ||
7146af97 | 2213 | \f |
2e471eb5 GM |
2214 | /*********************************************************************** |
2215 | Vector Allocation | |
2216 | ***********************************************************************/ | |
7146af97 | 2217 | |
34400008 GM |
2218 | /* Singly-linked list of all vectors. */ |
2219 | ||
7146af97 JB |
2220 | struct Lisp_Vector *all_vectors; |
2221 | ||
2e471eb5 GM |
2222 | /* Total number of vector-like objects now in use. */ |
2223 | ||
c8099634 RS |
2224 | int n_vectors; |
2225 | ||
34400008 GM |
2226 | |
2227 | /* Value is a pointer to a newly allocated Lisp_Vector structure | |
2228 | with room for LEN Lisp_Objects. */ | |
2229 | ||
ece93c02 GM |
2230 | static struct Lisp_Vector * |
2231 | allocate_vectorlike (len, type) | |
1825c68d | 2232 | EMACS_INT len; |
ece93c02 | 2233 | enum mem_type type; |
1825c68d KH |
2234 | { |
2235 | struct Lisp_Vector *p; | |
675d5130 | 2236 | size_t nbytes; |
1825c68d | 2237 | |
d1658221 | 2238 | #ifdef DOUG_LEA_MALLOC |
f8608968 GM |
2239 | /* Prevent mmap'ing the chunk. Lisp data may not be mmap'ed |
2240 | because mapped region contents are not preserved in | |
2241 | a dumped Emacs. */ | |
d1658221 RS |
2242 | mallopt (M_MMAP_MAX, 0); |
2243 | #endif | |
177c0ea7 | 2244 | |
34400008 | 2245 | nbytes = sizeof *p + (len - 1) * sizeof p->contents[0]; |
ece93c02 | 2246 | p = (struct Lisp_Vector *) lisp_malloc (nbytes, type); |
177c0ea7 | 2247 | |
d1658221 | 2248 | #ifdef DOUG_LEA_MALLOC |
34400008 | 2249 | /* Back to a reasonable maximum of mmap'ed areas. */ |
81d492d5 | 2250 | mallopt (M_MMAP_MAX, MMAP_MAX_AREAS); |
d1658221 | 2251 | #endif |
177c0ea7 | 2252 | |
1825c68d | 2253 | VALIDATE_LISP_STORAGE (p, 0); |
34400008 | 2254 | consing_since_gc += nbytes; |
310ea200 | 2255 | vector_cells_consed += len; |
1825c68d KH |
2256 | |
2257 | p->next = all_vectors; | |
2258 | all_vectors = p; | |
34400008 | 2259 | ++n_vectors; |
1825c68d KH |
2260 | return p; |
2261 | } | |
2262 | ||
34400008 | 2263 | |
ece93c02 GM |
2264 | /* Allocate a vector with NSLOTS slots. */ |
2265 | ||
2266 | struct Lisp_Vector * | |
2267 | allocate_vector (nslots) | |
2268 | EMACS_INT nslots; | |
2269 | { | |
2270 | struct Lisp_Vector *v = allocate_vectorlike (nslots, MEM_TYPE_VECTOR); | |
2271 | v->size = nslots; | |
2272 | return v; | |
2273 | } | |
2274 | ||
2275 | ||
2276 | /* Allocate other vector-like structures. */ | |
2277 | ||
2278 | struct Lisp_Hash_Table * | |
2279 | allocate_hash_table () | |
2280 | { | |
2281 | EMACS_INT len = VECSIZE (struct Lisp_Hash_Table); | |
2282 | struct Lisp_Vector *v = allocate_vectorlike (len, MEM_TYPE_HASH_TABLE); | |
2283 | EMACS_INT i; | |
177c0ea7 | 2284 | |
ece93c02 GM |
2285 | v->size = len; |
2286 | for (i = 0; i < len; ++i) | |
2287 | v->contents[i] = Qnil; | |
177c0ea7 | 2288 | |
ece93c02 GM |
2289 | return (struct Lisp_Hash_Table *) v; |
2290 | } | |
2291 | ||
2292 | ||
2293 | struct window * | |
2294 | allocate_window () | |
2295 | { | |
2296 | EMACS_INT len = VECSIZE (struct window); | |
2297 | struct Lisp_Vector *v = allocate_vectorlike (len, MEM_TYPE_WINDOW); | |
2298 | EMACS_INT i; | |
177c0ea7 | 2299 | |
ece93c02 GM |
2300 | for (i = 0; i < len; ++i) |
2301 | v->contents[i] = Qnil; | |
2302 | v->size = len; | |
177c0ea7 | 2303 | |
ece93c02 GM |
2304 | return (struct window *) v; |
2305 | } | |
2306 | ||
2307 | ||
2308 | struct frame * | |
2309 | allocate_frame () | |
2310 | { | |
2311 | EMACS_INT len = VECSIZE (struct frame); | |
2312 | struct Lisp_Vector *v = allocate_vectorlike (len, MEM_TYPE_FRAME); | |
2313 | EMACS_INT i; | |
177c0ea7 | 2314 | |
ece93c02 GM |
2315 | for (i = 0; i < len; ++i) |
2316 | v->contents[i] = make_number (0); | |
2317 | v->size = len; | |
2318 | return (struct frame *) v; | |
2319 | } | |
2320 | ||
2321 | ||
2322 | struct Lisp_Process * | |
2323 | allocate_process () | |
2324 | { | |
2325 | EMACS_INT len = VECSIZE (struct Lisp_Process); | |
2326 | struct Lisp_Vector *v = allocate_vectorlike (len, MEM_TYPE_PROCESS); | |
2327 | EMACS_INT i; | |
177c0ea7 | 2328 | |
ece93c02 GM |
2329 | for (i = 0; i < len; ++i) |
2330 | v->contents[i] = Qnil; | |
2331 | v->size = len; | |
177c0ea7 | 2332 | |
ece93c02 GM |
2333 | return (struct Lisp_Process *) v; |
2334 | } | |
2335 | ||
2336 | ||
2337 | struct Lisp_Vector * | |
2338 | allocate_other_vector (len) | |
2339 | EMACS_INT len; | |
2340 | { | |
2341 | struct Lisp_Vector *v = allocate_vectorlike (len, MEM_TYPE_VECTOR); | |
2342 | EMACS_INT i; | |
177c0ea7 | 2343 | |
ece93c02 GM |
2344 | for (i = 0; i < len; ++i) |
2345 | v->contents[i] = Qnil; | |
2346 | v->size = len; | |
177c0ea7 | 2347 | |
ece93c02 GM |
2348 | return v; |
2349 | } | |
2350 | ||
2351 | ||
7146af97 | 2352 | DEFUN ("make-vector", Fmake_vector, Smake_vector, 2, 2, 0, |
a6266d23 | 2353 | doc: /* Return a newly created vector of length LENGTH, with each element being INIT. |
7ee72033 MB |
2354 | See also the function `vector'. */) |
2355 | (length, init) | |
7146af97 JB |
2356 | register Lisp_Object length, init; |
2357 | { | |
1825c68d KH |
2358 | Lisp_Object vector; |
2359 | register EMACS_INT sizei; | |
2360 | register int index; | |
7146af97 JB |
2361 | register struct Lisp_Vector *p; |
2362 | ||
b7826503 | 2363 | CHECK_NATNUM (length); |
c9dad5ed | 2364 | sizei = XFASTINT (length); |
7146af97 | 2365 | |
ece93c02 | 2366 | p = allocate_vector (sizei); |
7146af97 JB |
2367 | for (index = 0; index < sizei; index++) |
2368 | p->contents[index] = init; | |
2369 | ||
1825c68d | 2370 | XSETVECTOR (vector, p); |
7146af97 JB |
2371 | return vector; |
2372 | } | |
2373 | ||
34400008 | 2374 | |
a59de17b | 2375 | DEFUN ("make-char-table", Fmake_char_table, Smake_char_table, 1, 2, 0, |
a6266d23 | 2376 | doc: /* Return a newly created char-table, with purpose PURPOSE. |
228299fa GM |
2377 | Each element is initialized to INIT, which defaults to nil. |
2378 | PURPOSE should be a symbol which has a `char-table-extra-slots' property. | |
7ee72033 MB |
2379 | The property's value should be an integer between 0 and 10. */) |
2380 | (purpose, init) | |
a59de17b | 2381 | register Lisp_Object purpose, init; |
7b07587b RS |
2382 | { |
2383 | Lisp_Object vector; | |
a59de17b | 2384 | Lisp_Object n; |
b7826503 | 2385 | CHECK_SYMBOL (purpose); |
0551bde3 | 2386 | n = Fget (purpose, Qchar_table_extra_slots); |
b7826503 | 2387 | CHECK_NUMBER (n); |
7b07587b RS |
2388 | if (XINT (n) < 0 || XINT (n) > 10) |
2389 | args_out_of_range (n, Qnil); | |
2390 | /* Add 2 to the size for the defalt and parent slots. */ | |
2391 | vector = Fmake_vector (make_number (CHAR_TABLE_STANDARD_SLOTS + XINT (n)), | |
2392 | init); | |
0551bde3 | 2393 | XCHAR_TABLE (vector)->top = Qt; |
c96a008c | 2394 | XCHAR_TABLE (vector)->parent = Qnil; |
a59de17b | 2395 | XCHAR_TABLE (vector)->purpose = purpose; |
7b07587b RS |
2396 | XSETCHAR_TABLE (vector, XCHAR_TABLE (vector)); |
2397 | return vector; | |
2398 | } | |
2399 | ||
34400008 | 2400 | |
0551bde3 KH |
2401 | /* Return a newly created sub char table with default value DEFALT. |
2402 | Since a sub char table does not appear as a top level Emacs Lisp | |
2403 | object, we don't need a Lisp interface to make it. */ | |
2404 | ||
2405 | Lisp_Object | |
2406 | make_sub_char_table (defalt) | |
2407 | Lisp_Object defalt; | |
2408 | { | |
2409 | Lisp_Object vector | |
2410 | = Fmake_vector (make_number (SUB_CHAR_TABLE_STANDARD_SLOTS), Qnil); | |
2411 | XCHAR_TABLE (vector)->top = Qnil; | |
2412 | XCHAR_TABLE (vector)->defalt = defalt; | |
2413 | XSETCHAR_TABLE (vector, XCHAR_TABLE (vector)); | |
2414 | return vector; | |
2415 | } | |
2416 | ||
34400008 | 2417 | |
7146af97 | 2418 | DEFUN ("vector", Fvector, Svector, 0, MANY, 0, |
eae936e2 | 2419 | doc: /* Return a newly created vector with specified arguments as elements. |
ae8e8122 MB |
2420 | Any number of arguments, even zero arguments, are allowed. |
2421 | usage: (vector &rest OBJECTS) */) | |
7ee72033 | 2422 | (nargs, args) |
7146af97 JB |
2423 | register int nargs; |
2424 | Lisp_Object *args; | |
2425 | { | |
2426 | register Lisp_Object len, val; | |
2427 | register int index; | |
2428 | register struct Lisp_Vector *p; | |
2429 | ||
67ba9986 | 2430 | XSETFASTINT (len, nargs); |
7146af97 JB |
2431 | val = Fmake_vector (len, Qnil); |
2432 | p = XVECTOR (val); | |
2433 | for (index = 0; index < nargs; index++) | |
2434 | p->contents[index] = args[index]; | |
2435 | return val; | |
2436 | } | |
2437 | ||
34400008 | 2438 | |
7146af97 | 2439 | DEFUN ("make-byte-code", Fmake_byte_code, Smake_byte_code, 4, MANY, 0, |
a6266d23 | 2440 | doc: /* Create a byte-code object with specified arguments as elements. |
228299fa GM |
2441 | The arguments should be the arglist, bytecode-string, constant vector, |
2442 | stack size, (optional) doc string, and (optional) interactive spec. | |
2443 | The first four arguments are required; at most six have any | |
ae8e8122 MB |
2444 | significance. |
2445 | usage: (make-byte-code &rest ELEMENTS) */) | |
7ee72033 | 2446 | (nargs, args) |
7146af97 JB |
2447 | register int nargs; |
2448 | Lisp_Object *args; | |
2449 | { | |
2450 | register Lisp_Object len, val; | |
2451 | register int index; | |
2452 | register struct Lisp_Vector *p; | |
2453 | ||
67ba9986 | 2454 | XSETFASTINT (len, nargs); |
265a9e55 | 2455 | if (!NILP (Vpurify_flag)) |
5a053ea9 | 2456 | val = make_pure_vector ((EMACS_INT) nargs); |
7146af97 JB |
2457 | else |
2458 | val = Fmake_vector (len, Qnil); | |
9eac9d59 KH |
2459 | |
2460 | if (STRINGP (args[1]) && STRING_MULTIBYTE (args[1])) | |
2461 | /* BYTECODE-STRING must have been produced by Emacs 20.2 or the | |
2462 | earlier because they produced a raw 8-bit string for byte-code | |
2463 | and now such a byte-code string is loaded as multibyte while | |
2464 | raw 8-bit characters converted to multibyte form. Thus, now we | |
2465 | must convert them back to the original unibyte form. */ | |
2466 | args[1] = Fstring_as_unibyte (args[1]); | |
2467 | ||
7146af97 JB |
2468 | p = XVECTOR (val); |
2469 | for (index = 0; index < nargs; index++) | |
2470 | { | |
265a9e55 | 2471 | if (!NILP (Vpurify_flag)) |
7146af97 JB |
2472 | args[index] = Fpurecopy (args[index]); |
2473 | p->contents[index] = args[index]; | |
2474 | } | |
50aee051 | 2475 | XSETCOMPILED (val, p); |
7146af97 JB |
2476 | return val; |
2477 | } | |
2e471eb5 | 2478 | |
34400008 | 2479 | |
7146af97 | 2480 | \f |
2e471eb5 GM |
2481 | /*********************************************************************** |
2482 | Symbol Allocation | |
2483 | ***********************************************************************/ | |
7146af97 | 2484 | |
2e471eb5 GM |
2485 | /* Each symbol_block is just under 1020 bytes long, since malloc |
2486 | really allocates in units of powers of two and uses 4 bytes for its | |
2487 | own overhead. */ | |
7146af97 JB |
2488 | |
2489 | #define SYMBOL_BLOCK_SIZE \ | |
2490 | ((1020 - sizeof (struct symbol_block *)) / sizeof (struct Lisp_Symbol)) | |
2491 | ||
2492 | struct symbol_block | |
2e471eb5 GM |
2493 | { |
2494 | struct symbol_block *next; | |
2495 | struct Lisp_Symbol symbols[SYMBOL_BLOCK_SIZE]; | |
2496 | }; | |
7146af97 | 2497 | |
34400008 GM |
2498 | /* Current symbol block and index of first unused Lisp_Symbol |
2499 | structure in it. */ | |
2500 | ||
7146af97 JB |
2501 | struct symbol_block *symbol_block; |
2502 | int symbol_block_index; | |
2503 | ||
34400008 GM |
2504 | /* List of free symbols. */ |
2505 | ||
7146af97 JB |
2506 | struct Lisp_Symbol *symbol_free_list; |
2507 | ||
c8099634 | 2508 | /* Total number of symbol blocks now in use. */ |
2e471eb5 | 2509 | |
c8099634 RS |
2510 | int n_symbol_blocks; |
2511 | ||
34400008 GM |
2512 | |
2513 | /* Initialize symbol allocation. */ | |
2514 | ||
7146af97 JB |
2515 | void |
2516 | init_symbol () | |
2517 | { | |
34400008 GM |
2518 | symbol_block = (struct symbol_block *) lisp_malloc (sizeof *symbol_block, |
2519 | MEM_TYPE_SYMBOL); | |
7146af97 | 2520 | symbol_block->next = 0; |
290c8f1e | 2521 | bzero ((char *) symbol_block->symbols, sizeof symbol_block->symbols); |
7146af97 JB |
2522 | symbol_block_index = 0; |
2523 | symbol_free_list = 0; | |
c8099634 | 2524 | n_symbol_blocks = 1; |
7146af97 JB |
2525 | } |
2526 | ||
34400008 | 2527 | |
7146af97 | 2528 | DEFUN ("make-symbol", Fmake_symbol, Smake_symbol, 1, 1, 0, |
a6266d23 | 2529 | doc: /* Return a newly allocated uninterned symbol whose name is NAME. |
7ee72033 MB |
2530 | Its value and function definition are void, and its property list is nil. */) |
2531 | (name) | |
54ee42dd | 2532 | Lisp_Object name; |
7146af97 JB |
2533 | { |
2534 | register Lisp_Object val; | |
2535 | register struct Lisp_Symbol *p; | |
2536 | ||
b7826503 | 2537 | CHECK_STRING (name); |
7146af97 JB |
2538 | |
2539 | if (symbol_free_list) | |
2540 | { | |
45d12a89 | 2541 | XSETSYMBOL (val, symbol_free_list); |
85481507 | 2542 | symbol_free_list = *(struct Lisp_Symbol **)&symbol_free_list->value; |
7146af97 JB |
2543 | } |
2544 | else | |
2545 | { | |
2546 | if (symbol_block_index == SYMBOL_BLOCK_SIZE) | |
2547 | { | |
3c06d205 | 2548 | struct symbol_block *new; |
34400008 GM |
2549 | new = (struct symbol_block *) lisp_malloc (sizeof *new, |
2550 | MEM_TYPE_SYMBOL); | |
7146af97 JB |
2551 | VALIDATE_LISP_STORAGE (new, sizeof *new); |
2552 | new->next = symbol_block; | |
2553 | symbol_block = new; | |
2554 | symbol_block_index = 0; | |
c8099634 | 2555 | n_symbol_blocks++; |
7146af97 | 2556 | } |
45d12a89 | 2557 | XSETSYMBOL (val, &symbol_block->symbols[symbol_block_index++]); |
7146af97 | 2558 | } |
177c0ea7 | 2559 | |
7146af97 | 2560 | p = XSYMBOL (val); |
8fe5665d | 2561 | p->xname = name; |
7146af97 | 2562 | p->plist = Qnil; |
2e471eb5 GM |
2563 | p->value = Qunbound; |
2564 | p->function = Qunbound; | |
9e713715 GM |
2565 | p->next = NULL; |
2566 | p->interned = SYMBOL_UNINTERNED; | |
2567 | p->constant = 0; | |
2568 | p->indirect_variable = 0; | |
2e471eb5 GM |
2569 | consing_since_gc += sizeof (struct Lisp_Symbol); |
2570 | symbols_consed++; | |
7146af97 JB |
2571 | return val; |
2572 | } | |
2573 | ||
3f25e183 | 2574 | |
2e471eb5 GM |
2575 | \f |
2576 | /*********************************************************************** | |
34400008 | 2577 | Marker (Misc) Allocation |
2e471eb5 | 2578 | ***********************************************************************/ |
3f25e183 | 2579 | |
2e471eb5 GM |
2580 | /* Allocation of markers and other objects that share that structure. |
2581 | Works like allocation of conses. */ | |
c0696668 | 2582 | |
2e471eb5 GM |
2583 | #define MARKER_BLOCK_SIZE \ |
2584 | ((1020 - sizeof (struct marker_block *)) / sizeof (union Lisp_Misc)) | |
2585 | ||
2586 | struct marker_block | |
c0696668 | 2587 | { |
2e471eb5 GM |
2588 | struct marker_block *next; |
2589 | union Lisp_Misc markers[MARKER_BLOCK_SIZE]; | |
2590 | }; | |
c0696668 | 2591 | |
2e471eb5 GM |
2592 | struct marker_block *marker_block; |
2593 | int marker_block_index; | |
c0696668 | 2594 | |
2e471eb5 | 2595 | union Lisp_Misc *marker_free_list; |
c0696668 | 2596 | |
2e471eb5 | 2597 | /* Total number of marker blocks now in use. */ |
3f25e183 | 2598 | |
2e471eb5 GM |
2599 | int n_marker_blocks; |
2600 | ||
2601 | void | |
2602 | init_marker () | |
3f25e183 | 2603 | { |
34400008 GM |
2604 | marker_block = (struct marker_block *) lisp_malloc (sizeof *marker_block, |
2605 | MEM_TYPE_MISC); | |
2e471eb5 GM |
2606 | marker_block->next = 0; |
2607 | bzero ((char *) marker_block->markers, sizeof marker_block->markers); | |
2608 | marker_block_index = 0; | |
2609 | marker_free_list = 0; | |
2610 | n_marker_blocks = 1; | |
3f25e183 RS |
2611 | } |
2612 | ||
2e471eb5 GM |
2613 | /* Return a newly allocated Lisp_Misc object, with no substructure. */ |
2614 | ||
3f25e183 | 2615 | Lisp_Object |
2e471eb5 | 2616 | allocate_misc () |
7146af97 | 2617 | { |
2e471eb5 | 2618 | Lisp_Object val; |
7146af97 | 2619 | |
2e471eb5 | 2620 | if (marker_free_list) |
7146af97 | 2621 | { |
2e471eb5 GM |
2622 | XSETMISC (val, marker_free_list); |
2623 | marker_free_list = marker_free_list->u_free.chain; | |
7146af97 JB |
2624 | } |
2625 | else | |
7146af97 | 2626 | { |
2e471eb5 GM |
2627 | if (marker_block_index == MARKER_BLOCK_SIZE) |
2628 | { | |
2629 | struct marker_block *new; | |
34400008 GM |
2630 | new = (struct marker_block *) lisp_malloc (sizeof *new, |
2631 | MEM_TYPE_MISC); | |
2e471eb5 GM |
2632 | VALIDATE_LISP_STORAGE (new, sizeof *new); |
2633 | new->next = marker_block; | |
2634 | marker_block = new; | |
2635 | marker_block_index = 0; | |
2636 | n_marker_blocks++; | |
2637 | } | |
2638 | XSETMISC (val, &marker_block->markers[marker_block_index++]); | |
7146af97 | 2639 | } |
177c0ea7 | 2640 | |
2e471eb5 GM |
2641 | consing_since_gc += sizeof (union Lisp_Misc); |
2642 | misc_objects_consed++; | |
2643 | return val; | |
2644 | } | |
2645 | ||
42172a6b RS |
2646 | /* Return a Lisp_Misc_Save_Value object containing POINTER and |
2647 | INTEGER. This is used to package C values to call record_unwind_protect. | |
2648 | The unwind function can get the C values back using XSAVE_VALUE. */ | |
2649 | ||
2650 | Lisp_Object | |
2651 | make_save_value (pointer, integer) | |
2652 | void *pointer; | |
2653 | int integer; | |
2654 | { | |
2655 | register Lisp_Object val; | |
2656 | register struct Lisp_Save_Value *p; | |
2657 | ||
2658 | val = allocate_misc (); | |
2659 | XMISCTYPE (val) = Lisp_Misc_Save_Value; | |
2660 | p = XSAVE_VALUE (val); | |
2661 | p->pointer = pointer; | |
2662 | p->integer = integer; | |
2663 | return val; | |
2664 | } | |
2665 | ||
2e471eb5 | 2666 | DEFUN ("make-marker", Fmake_marker, Smake_marker, 0, 0, 0, |
a6266d23 | 2667 | doc: /* Return a newly allocated marker which does not point at any place. */) |
7ee72033 | 2668 | () |
2e471eb5 GM |
2669 | { |
2670 | register Lisp_Object val; | |
2671 | register struct Lisp_Marker *p; | |
7146af97 | 2672 | |
2e471eb5 GM |
2673 | val = allocate_misc (); |
2674 | XMISCTYPE (val) = Lisp_Misc_Marker; | |
2675 | p = XMARKER (val); | |
2676 | p->buffer = 0; | |
2677 | p->bytepos = 0; | |
2678 | p->charpos = 0; | |
2679 | p->chain = Qnil; | |
2680 | p->insertion_type = 0; | |
7146af97 JB |
2681 | return val; |
2682 | } | |
2e471eb5 GM |
2683 | |
2684 | /* Put MARKER back on the free list after using it temporarily. */ | |
2685 | ||
2686 | void | |
2687 | free_marker (marker) | |
2688 | Lisp_Object marker; | |
2689 | { | |
2690 | unchain_marker (marker); | |
2691 | ||
2692 | XMISC (marker)->u_marker.type = Lisp_Misc_Free; | |
2693 | XMISC (marker)->u_free.chain = marker_free_list; | |
2694 | marker_free_list = XMISC (marker); | |
2695 | ||
2696 | total_free_markers++; | |
2697 | } | |
2698 | ||
c0696668 | 2699 | \f |
7146af97 | 2700 | /* Return a newly created vector or string with specified arguments as |
736471d1 RS |
2701 | elements. If all the arguments are characters that can fit |
2702 | in a string of events, make a string; otherwise, make a vector. | |
2703 | ||
2704 | Any number of arguments, even zero arguments, are allowed. */ | |
7146af97 JB |
2705 | |
2706 | Lisp_Object | |
736471d1 | 2707 | make_event_array (nargs, args) |
7146af97 JB |
2708 | register int nargs; |
2709 | Lisp_Object *args; | |
2710 | { | |
2711 | int i; | |
2712 | ||
2713 | for (i = 0; i < nargs; i++) | |
736471d1 | 2714 | /* The things that fit in a string |
c9ca4659 RS |
2715 | are characters that are in 0...127, |
2716 | after discarding the meta bit and all the bits above it. */ | |
e687453f | 2717 | if (!INTEGERP (args[i]) |
c9ca4659 | 2718 | || (XUINT (args[i]) & ~(-CHAR_META)) >= 0200) |
7146af97 JB |
2719 | return Fvector (nargs, args); |
2720 | ||
2721 | /* Since the loop exited, we know that all the things in it are | |
2722 | characters, so we can make a string. */ | |
2723 | { | |
c13ccad2 | 2724 | Lisp_Object result; |
177c0ea7 | 2725 | |
50aee051 | 2726 | result = Fmake_string (make_number (nargs), make_number (0)); |
7146af97 | 2727 | for (i = 0; i < nargs; i++) |
736471d1 | 2728 | { |
46e7e6b0 | 2729 | SSET (result, i, XINT (args[i])); |
736471d1 RS |
2730 | /* Move the meta bit to the right place for a string char. */ |
2731 | if (XINT (args[i]) & CHAR_META) | |
46e7e6b0 | 2732 | SSET (result, i, SREF (result, i) | 0x80); |
736471d1 | 2733 | } |
177c0ea7 | 2734 | |
7146af97 JB |
2735 | return result; |
2736 | } | |
2737 | } | |
2e471eb5 GM |
2738 | |
2739 | ||
7146af97 | 2740 | \f |
34400008 GM |
2741 | /************************************************************************ |
2742 | C Stack Marking | |
2743 | ************************************************************************/ | |
2744 | ||
13c844fb GM |
2745 | #if GC_MARK_STACK || defined GC_MALLOC_CHECK |
2746 | ||
71cf5fa0 GM |
2747 | /* Conservative C stack marking requires a method to identify possibly |
2748 | live Lisp objects given a pointer value. We do this by keeping | |
2749 | track of blocks of Lisp data that are allocated in a red-black tree | |
2750 | (see also the comment of mem_node which is the type of nodes in | |
2751 | that tree). Function lisp_malloc adds information for an allocated | |
2752 | block to the red-black tree with calls to mem_insert, and function | |
2753 | lisp_free removes it with mem_delete. Functions live_string_p etc | |
2754 | call mem_find to lookup information about a given pointer in the | |
2755 | tree, and use that to determine if the pointer points to a Lisp | |
2756 | object or not. */ | |
2757 | ||
34400008 GM |
2758 | /* Initialize this part of alloc.c. */ |
2759 | ||
2760 | static void | |
2761 | mem_init () | |
2762 | { | |
2763 | mem_z.left = mem_z.right = MEM_NIL; | |
2764 | mem_z.parent = NULL; | |
2765 | mem_z.color = MEM_BLACK; | |
2766 | mem_z.start = mem_z.end = NULL; | |
2767 | mem_root = MEM_NIL; | |
2768 | } | |
2769 | ||
2770 | ||
2771 | /* Value is a pointer to the mem_node containing START. Value is | |
2772 | MEM_NIL if there is no node in the tree containing START. */ | |
2773 | ||
2774 | static INLINE struct mem_node * | |
2775 | mem_find (start) | |
2776 | void *start; | |
2777 | { | |
2778 | struct mem_node *p; | |
2779 | ||
ece93c02 GM |
2780 | if (start < min_heap_address || start > max_heap_address) |
2781 | return MEM_NIL; | |
2782 | ||
34400008 GM |
2783 | /* Make the search always successful to speed up the loop below. */ |
2784 | mem_z.start = start; | |
2785 | mem_z.end = (char *) start + 1; | |
2786 | ||
2787 | p = mem_root; | |
2788 | while (start < p->start || start >= p->end) | |
2789 | p = start < p->start ? p->left : p->right; | |
2790 | return p; | |
2791 | } | |
2792 | ||
2793 | ||
2794 | /* Insert a new node into the tree for a block of memory with start | |
2795 | address START, end address END, and type TYPE. Value is a | |
2796 | pointer to the node that was inserted. */ | |
2797 | ||
2798 | static struct mem_node * | |
2799 | mem_insert (start, end, type) | |
2800 | void *start, *end; | |
2801 | enum mem_type type; | |
2802 | { | |
2803 | struct mem_node *c, *parent, *x; | |
2804 | ||
ece93c02 GM |
2805 | if (start < min_heap_address) |
2806 | min_heap_address = start; | |
2807 | if (end > max_heap_address) | |
2808 | max_heap_address = end; | |
2809 | ||
34400008 GM |
2810 | /* See where in the tree a node for START belongs. In this |
2811 | particular application, it shouldn't happen that a node is already | |
2812 | present. For debugging purposes, let's check that. */ | |
2813 | c = mem_root; | |
2814 | parent = NULL; | |
2815 | ||
2816 | #if GC_MARK_STACK != GC_MAKE_GCPROS_NOOPS | |
177c0ea7 | 2817 | |
34400008 GM |
2818 | while (c != MEM_NIL) |
2819 | { | |
2820 | if (start >= c->start && start < c->end) | |
2821 | abort (); | |
2822 | parent = c; | |
2823 | c = start < c->start ? c->left : c->right; | |
2824 | } | |
177c0ea7 | 2825 | |
34400008 | 2826 | #else /* GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS */ |
177c0ea7 | 2827 | |
34400008 GM |
2828 | while (c != MEM_NIL) |
2829 | { | |
2830 | parent = c; | |
2831 | c = start < c->start ? c->left : c->right; | |
2832 | } | |
177c0ea7 | 2833 | |
34400008 GM |
2834 | #endif /* GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS */ |
2835 | ||
2836 | /* Create a new node. */ | |
877935b1 GM |
2837 | #ifdef GC_MALLOC_CHECK |
2838 | x = (struct mem_node *) _malloc_internal (sizeof *x); | |
2839 | if (x == NULL) | |
2840 | abort (); | |
2841 | #else | |
34400008 | 2842 | x = (struct mem_node *) xmalloc (sizeof *x); |
877935b1 | 2843 | #endif |
34400008 GM |
2844 | x->start = start; |
2845 | x->end = end; | |
2846 | x->type = type; | |
2847 | x->parent = parent; | |
2848 | x->left = x->right = MEM_NIL; | |
2849 | x->color = MEM_RED; | |
2850 | ||
2851 | /* Insert it as child of PARENT or install it as root. */ | |
2852 | if (parent) | |
2853 | { | |
2854 | if (start < parent->start) | |
2855 | parent->left = x; | |
2856 | else | |
2857 | parent->right = x; | |
2858 | } | |
177c0ea7 | 2859 | else |
34400008 GM |
2860 | mem_root = x; |
2861 | ||
2862 | /* Re-establish red-black tree properties. */ | |
2863 | mem_insert_fixup (x); | |
877935b1 | 2864 | |
34400008 GM |
2865 | return x; |
2866 | } | |
2867 | ||
2868 | ||
2869 | /* Re-establish the red-black properties of the tree, and thereby | |
2870 | balance the tree, after node X has been inserted; X is always red. */ | |
2871 | ||
2872 | static void | |
2873 | mem_insert_fixup (x) | |
2874 | struct mem_node *x; | |
2875 | { | |
2876 | while (x != mem_root && x->parent->color == MEM_RED) | |
2877 | { | |
2878 | /* X is red and its parent is red. This is a violation of | |
2879 | red-black tree property #3. */ | |
177c0ea7 | 2880 | |
34400008 GM |
2881 | if (x->parent == x->parent->parent->left) |
2882 | { | |
2883 | /* We're on the left side of our grandparent, and Y is our | |
2884 | "uncle". */ | |
2885 | struct mem_node *y = x->parent->parent->right; | |
177c0ea7 | 2886 | |
34400008 GM |
2887 | if (y->color == MEM_RED) |
2888 | { | |
2889 | /* Uncle and parent are red but should be black because | |
2890 | X is red. Change the colors accordingly and proceed | |
2891 | with the grandparent. */ | |
2892 | x->parent->color = MEM_BLACK; | |
2893 | y->color = MEM_BLACK; | |
2894 | x->parent->parent->color = MEM_RED; | |
2895 | x = x->parent->parent; | |
2896 | } | |
2897 | else | |
2898 | { | |
2899 | /* Parent and uncle have different colors; parent is | |
2900 | red, uncle is black. */ | |
2901 | if (x == x->parent->right) | |
2902 | { | |
2903 | x = x->parent; | |
2904 | mem_rotate_left (x); | |
2905 | } | |
2906 | ||
2907 | x->parent->color = MEM_BLACK; | |
2908 | x->parent->parent->color = MEM_RED; | |
2909 | mem_rotate_right (x->parent->parent); | |
2910 | } | |
2911 | } | |
2912 | else | |
2913 | { | |
2914 | /* This is the symmetrical case of above. */ | |
2915 | struct mem_node *y = x->parent->parent->left; | |
177c0ea7 | 2916 | |
34400008 GM |
2917 | if (y->color == MEM_RED) |
2918 | { | |
2919 | x->parent->color = MEM_BLACK; | |
2920 | y->color = MEM_BLACK; | |
2921 | x->parent->parent->color = MEM_RED; | |
2922 | x = x->parent->parent; | |
2923 | } | |
2924 | else | |
2925 | { | |
2926 | if (x == x->parent->left) | |
2927 | { | |
2928 | x = x->parent; | |
2929 | mem_rotate_right (x); | |
2930 | } | |
177c0ea7 | 2931 | |
34400008 GM |
2932 | x->parent->color = MEM_BLACK; |
2933 | x->parent->parent->color = MEM_RED; | |
2934 | mem_rotate_left (x->parent->parent); | |
2935 | } | |
2936 | } | |
2937 | } | |
2938 | ||
2939 | /* The root may have been changed to red due to the algorithm. Set | |
2940 | it to black so that property #5 is satisfied. */ | |
2941 | mem_root->color = MEM_BLACK; | |
2942 | } | |
2943 | ||
2944 | ||
177c0ea7 JB |
2945 | /* (x) (y) |
2946 | / \ / \ | |
34400008 GM |
2947 | a (y) ===> (x) c |
2948 | / \ / \ | |
2949 | b c a b */ | |
2950 | ||
2951 | static void | |
2952 | mem_rotate_left (x) | |
2953 | struct mem_node *x; | |
2954 | { | |
2955 | struct mem_node *y; | |
2956 | ||
2957 | /* Turn y's left sub-tree into x's right sub-tree. */ | |
2958 | y = x->right; | |
2959 | x->right = y->left; | |
2960 | if (y->left != MEM_NIL) | |
2961 | y->left->parent = x; | |
2962 | ||
2963 | /* Y's parent was x's parent. */ | |
2964 | if (y != MEM_NIL) | |
2965 | y->parent = x->parent; | |
2966 | ||
2967 | /* Get the parent to point to y instead of x. */ | |
2968 | if (x->parent) | |
2969 | { | |
2970 | if (x == x->parent->left) | |
2971 | x->parent->left = y; | |
2972 | else | |
2973 | x->parent->right = y; | |
2974 | } | |
2975 | else | |
2976 | mem_root = y; | |
2977 | ||
2978 | /* Put x on y's left. */ | |
2979 | y->left = x; | |
2980 | if (x != MEM_NIL) | |
2981 | x->parent = y; | |
2982 | } | |
2983 | ||
2984 | ||
177c0ea7 JB |
2985 | /* (x) (Y) |
2986 | / \ / \ | |
2987 | (y) c ===> a (x) | |
2988 | / \ / \ | |
34400008 GM |
2989 | a b b c */ |
2990 | ||
2991 | static void | |
2992 | mem_rotate_right (x) | |
2993 | struct mem_node *x; | |
2994 | { | |
2995 | struct mem_node *y = x->left; | |
2996 | ||
2997 | x->left = y->right; | |
2998 | if (y->right != MEM_NIL) | |
2999 | y->right->parent = x; | |
177c0ea7 | 3000 | |
34400008 GM |
3001 | if (y != MEM_NIL) |
3002 | y->parent = x->parent; | |
3003 | if (x->parent) | |
3004 | { | |
3005 | if (x == x->parent->right) | |
3006 | x->parent->right = y; | |
3007 | else | |
3008 | x->parent->left = y; | |
3009 | } | |
3010 | else | |
3011 | mem_root = y; | |
177c0ea7 | 3012 | |
34400008 GM |
3013 | y->right = x; |
3014 | if (x != MEM_NIL) | |
3015 | x->parent = y; | |
3016 | } | |
3017 | ||
3018 | ||
3019 | /* Delete node Z from the tree. If Z is null or MEM_NIL, do nothing. */ | |
3020 | ||
3021 | static void | |
3022 | mem_delete (z) | |
3023 | struct mem_node *z; | |
3024 | { | |
3025 | struct mem_node *x, *y; | |
3026 | ||
3027 | if (!z || z == MEM_NIL) | |
3028 | return; | |
3029 | ||
3030 | if (z->left == MEM_NIL || z->right == MEM_NIL) | |
3031 | y = z; | |
3032 | else | |
3033 | { | |
3034 | y = z->right; | |
3035 | while (y->left != MEM_NIL) | |
3036 | y = y->left; | |
3037 | } | |
3038 | ||
3039 | if (y->left != MEM_NIL) | |
3040 | x = y->left; | |
3041 | else | |
3042 | x = y->right; | |
3043 | ||
3044 | x->parent = y->parent; | |
3045 | if (y->parent) | |
3046 | { | |
3047 | if (y == y->parent->left) | |
3048 | y->parent->left = x; | |
3049 | else | |
3050 | y->parent->right = x; | |
3051 | } | |
3052 | else | |
3053 | mem_root = x; | |
3054 | ||
3055 | if (y != z) | |
3056 | { | |
3057 | z->start = y->start; | |
3058 | z->end = y->end; | |
3059 | z->type = y->type; | |
3060 | } | |
177c0ea7 | 3061 | |
34400008 GM |
3062 | if (y->color == MEM_BLACK) |
3063 | mem_delete_fixup (x); | |
877935b1 GM |
3064 | |
3065 | #ifdef GC_MALLOC_CHECK | |
3066 | _free_internal (y); | |
3067 | #else | |
34400008 | 3068 | xfree (y); |
877935b1 | 3069 | #endif |
34400008 GM |
3070 | } |
3071 | ||
3072 | ||
3073 | /* Re-establish the red-black properties of the tree, after a | |
3074 | deletion. */ | |
3075 | ||
3076 | static void | |
3077 | mem_delete_fixup (x) | |
3078 | struct mem_node *x; | |
3079 | { | |
3080 | while (x != mem_root && x->color == MEM_BLACK) | |
3081 | { | |
3082 | if (x == x->parent->left) | |
3083 | { | |
3084 | struct mem_node *w = x->parent->right; | |
177c0ea7 | 3085 | |
34400008 GM |
3086 | if (w->color == MEM_RED) |
3087 | { | |
3088 | w->color = MEM_BLACK; | |
3089 | x->parent->color = MEM_RED; | |
3090 | mem_rotate_left (x->parent); | |
3091 | w = x->parent->right; | |
3092 | } | |
177c0ea7 | 3093 | |
34400008 GM |
3094 | if (w->left->color == MEM_BLACK && w->right->color == MEM_BLACK) |
3095 | { | |
3096 | w->color = MEM_RED; | |
3097 | x = x->parent; | |
3098 | } | |
3099 | else | |
3100 | { | |
3101 | if (w->right->color == MEM_BLACK) | |
3102 | { | |
3103 | w->left->color = MEM_BLACK; | |
3104 | w->color = MEM_RED; | |
3105 | mem_rotate_right (w); | |
3106 | w = x->parent->right; | |
3107 | } | |
3108 | w->color = x->parent->color; | |
3109 | x->parent->color = MEM_BLACK; | |
3110 | w->right->color = MEM_BLACK; | |
3111 | mem_rotate_left (x->parent); | |
3112 | x = mem_root; | |
3113 | } | |
3114 | } | |
3115 | else | |
3116 | { | |
3117 | struct mem_node *w = x->parent->left; | |
177c0ea7 | 3118 | |
34400008 GM |
3119 | if (w->color == MEM_RED) |
3120 | { | |
3121 | w->color = MEM_BLACK; | |
3122 | x->parent->color = MEM_RED; | |
3123 | mem_rotate_right (x->parent); | |
3124 | w = x->parent->left; | |
3125 | } | |
177c0ea7 | 3126 | |
34400008 GM |
3127 | if (w->right->color == MEM_BLACK && w->left->color == MEM_BLACK) |
3128 | { | |
3129 | w->color = MEM_RED; | |
3130 | x = x->parent; | |
3131 | } | |
3132 | else | |
3133 | { | |
3134 | if (w->left->color == MEM_BLACK) | |
3135 | { | |
3136 | w->right->color = MEM_BLACK; | |
3137 | w->color = MEM_RED; | |
3138 | mem_rotate_left (w); | |
3139 | w = x->parent->left; | |
3140 | } | |
177c0ea7 | 3141 | |
34400008 GM |
3142 | w->color = x->parent->color; |
3143 | x->parent->color = MEM_BLACK; | |
3144 | w->left->color = MEM_BLACK; | |
3145 | mem_rotate_right (x->parent); | |
3146 | x = mem_root; | |
3147 | } | |
3148 | } | |
3149 | } | |
177c0ea7 | 3150 | |
34400008 GM |
3151 | x->color = MEM_BLACK; |
3152 | } | |
3153 | ||
3154 | ||
3155 | /* Value is non-zero if P is a pointer to a live Lisp string on | |
3156 | the heap. M is a pointer to the mem_block for P. */ | |
3157 | ||
3158 | static INLINE int | |
3159 | live_string_p (m, p) | |
3160 | struct mem_node *m; | |
3161 | void *p; | |
3162 | { | |
3163 | if (m->type == MEM_TYPE_STRING) | |
3164 | { | |
3165 | struct string_block *b = (struct string_block *) m->start; | |
3166 | int offset = (char *) p - (char *) &b->strings[0]; | |
3167 | ||
3168 | /* P must point to the start of a Lisp_String structure, and it | |
3169 | must not be on the free-list. */ | |
176bc847 GM |
3170 | return (offset >= 0 |
3171 | && offset % sizeof b->strings[0] == 0 | |
34400008 GM |
3172 | && ((struct Lisp_String *) p)->data != NULL); |
3173 | } | |
3174 | else | |
3175 | return 0; | |
3176 | } | |
3177 | ||
3178 | ||
3179 | /* Value is non-zero if P is a pointer to a live Lisp cons on | |
3180 | the heap. M is a pointer to the mem_block for P. */ | |
3181 | ||
3182 | static INLINE int | |
3183 | live_cons_p (m, p) | |
3184 | struct mem_node *m; | |
3185 | void *p; | |
3186 | { | |
3187 | if (m->type == MEM_TYPE_CONS) | |
3188 | { | |
3189 | struct cons_block *b = (struct cons_block *) m->start; | |
3190 | int offset = (char *) p - (char *) &b->conses[0]; | |
3191 | ||
3192 | /* P must point to the start of a Lisp_Cons, not be | |
3193 | one of the unused cells in the current cons block, | |
3194 | and not be on the free-list. */ | |
176bc847 GM |
3195 | return (offset >= 0 |
3196 | && offset % sizeof b->conses[0] == 0 | |
34400008 GM |
3197 | && (b != cons_block |
3198 | || offset / sizeof b->conses[0] < cons_block_index) | |
3199 | && !EQ (((struct Lisp_Cons *) p)->car, Vdead)); | |
3200 | } | |
3201 | else | |
3202 | return 0; | |
3203 | } | |
3204 | ||
3205 | ||
3206 | /* Value is non-zero if P is a pointer to a live Lisp symbol on | |
3207 | the heap. M is a pointer to the mem_block for P. */ | |
3208 | ||
3209 | static INLINE int | |
3210 | live_symbol_p (m, p) | |
3211 | struct mem_node *m; | |
3212 | void *p; | |
3213 | { | |
3214 | if (m->type == MEM_TYPE_SYMBOL) | |
3215 | { | |
3216 | struct symbol_block *b = (struct symbol_block *) m->start; | |
3217 | int offset = (char *) p - (char *) &b->symbols[0]; | |
177c0ea7 | 3218 | |
34400008 GM |
3219 | /* P must point to the start of a Lisp_Symbol, not be |
3220 | one of the unused cells in the current symbol block, | |
3221 | and not be on the free-list. */ | |
176bc847 GM |
3222 | return (offset >= 0 |
3223 | && offset % sizeof b->symbols[0] == 0 | |
34400008 GM |
3224 | && (b != symbol_block |
3225 | || offset / sizeof b->symbols[0] < symbol_block_index) | |
3226 | && !EQ (((struct Lisp_Symbol *) p)->function, Vdead)); | |
3227 | } | |
3228 | else | |
3229 | return 0; | |
3230 | } | |
3231 | ||
3232 | ||
3233 | /* Value is non-zero if P is a pointer to a live Lisp float on | |
3234 | the heap. M is a pointer to the mem_block for P. */ | |
3235 | ||
3236 | static INLINE int | |
3237 | live_float_p (m, p) | |
3238 | struct mem_node *m; | |
3239 | void *p; | |
3240 | { | |
3241 | if (m->type == MEM_TYPE_FLOAT) | |
3242 | { | |
3243 | struct float_block *b = (struct float_block *) m->start; | |
3244 | int offset = (char *) p - (char *) &b->floats[0]; | |
177c0ea7 | 3245 | |
34400008 GM |
3246 | /* P must point to the start of a Lisp_Float, not be |
3247 | one of the unused cells in the current float block, | |
3248 | and not be on the free-list. */ | |
176bc847 GM |
3249 | return (offset >= 0 |
3250 | && offset % sizeof b->floats[0] == 0 | |
34400008 GM |
3251 | && (b != float_block |
3252 | || offset / sizeof b->floats[0] < float_block_index) | |
3253 | && !EQ (((struct Lisp_Float *) p)->type, Vdead)); | |
3254 | } | |
3255 | else | |
3256 | return 0; | |
3257 | } | |
3258 | ||
3259 | ||
3260 | /* Value is non-zero if P is a pointer to a live Lisp Misc on | |
3261 | the heap. M is a pointer to the mem_block for P. */ | |
3262 | ||
3263 | static INLINE int | |
3264 | live_misc_p (m, p) | |
3265 | struct mem_node *m; | |
3266 | void *p; | |
3267 | { | |
3268 | if (m->type == MEM_TYPE_MISC) | |
3269 | { | |
3270 | struct marker_block *b = (struct marker_block *) m->start; | |
3271 | int offset = (char *) p - (char *) &b->markers[0]; | |
177c0ea7 | 3272 | |
34400008 GM |
3273 | /* P must point to the start of a Lisp_Misc, not be |
3274 | one of the unused cells in the current misc block, | |
3275 | and not be on the free-list. */ | |
176bc847 GM |
3276 | return (offset >= 0 |
3277 | && offset % sizeof b->markers[0] == 0 | |
34400008 GM |
3278 | && (b != marker_block |
3279 | || offset / sizeof b->markers[0] < marker_block_index) | |
3280 | && ((union Lisp_Misc *) p)->u_marker.type != Lisp_Misc_Free); | |
3281 | } | |
3282 | else | |
3283 | return 0; | |
3284 | } | |
3285 | ||
3286 | ||
3287 | /* Value is non-zero if P is a pointer to a live vector-like object. | |
3288 | M is a pointer to the mem_block for P. */ | |
3289 | ||
3290 | static INLINE int | |
3291 | live_vector_p (m, p) | |
3292 | struct mem_node *m; | |
3293 | void *p; | |
3294 | { | |
ece93c02 GM |
3295 | return (p == m->start |
3296 | && m->type >= MEM_TYPE_VECTOR | |
3297 | && m->type <= MEM_TYPE_WINDOW); | |
34400008 GM |
3298 | } |
3299 | ||
3300 | ||
3301 | /* Value is non-zero of P is a pointer to a live buffer. M is a | |
3302 | pointer to the mem_block for P. */ | |
3303 | ||
3304 | static INLINE int | |
3305 | live_buffer_p (m, p) | |
3306 | struct mem_node *m; | |
3307 | void *p; | |
3308 | { | |
3309 | /* P must point to the start of the block, and the buffer | |
3310 | must not have been killed. */ | |
3311 | return (m->type == MEM_TYPE_BUFFER | |
3312 | && p == m->start | |
3313 | && !NILP (((struct buffer *) p)->name)); | |
3314 | } | |
3315 | ||
13c844fb GM |
3316 | #endif /* GC_MARK_STACK || defined GC_MALLOC_CHECK */ |
3317 | ||
3318 | #if GC_MARK_STACK | |
3319 | ||
34400008 GM |
3320 | #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES |
3321 | ||
3322 | /* Array of objects that are kept alive because the C stack contains | |
3323 | a pattern that looks like a reference to them . */ | |
3324 | ||
3325 | #define MAX_ZOMBIES 10 | |
3326 | static Lisp_Object zombies[MAX_ZOMBIES]; | |
3327 | ||
3328 | /* Number of zombie objects. */ | |
3329 | ||
3330 | static int nzombies; | |
3331 | ||
3332 | /* Number of garbage collections. */ | |
3333 | ||
3334 | static int ngcs; | |
3335 | ||
3336 | /* Average percentage of zombies per collection. */ | |
3337 | ||
3338 | static double avg_zombies; | |
3339 | ||
3340 | /* Max. number of live and zombie objects. */ | |
3341 | ||
3342 | static int max_live, max_zombies; | |
3343 | ||
3344 | /* Average number of live objects per GC. */ | |
3345 | ||
3346 | static double avg_live; | |
3347 | ||
3348 | DEFUN ("gc-status", Fgc_status, Sgc_status, 0, 0, "", | |
7ee72033 MB |
3349 | doc: /* Show information about live and zombie objects. */) |
3350 | () | |
34400008 | 3351 | { |
83fc9c63 DL |
3352 | Lisp_Object args[8], zombie_list = Qnil; |
3353 | int i; | |
3354 | for (i = 0; i < nzombies; i++) | |
3355 | zombie_list = Fcons (zombies[i], zombie_list); | |
3356 | args[0] = build_string ("%d GCs, avg live/zombies = %.2f/%.2f (%f%%), max %d/%d\nzombies: %S"); | |
34400008 GM |
3357 | args[1] = make_number (ngcs); |
3358 | args[2] = make_float (avg_live); | |
3359 | args[3] = make_float (avg_zombies); | |
3360 | args[4] = make_float (avg_zombies / avg_live / 100); | |
3361 | args[5] = make_number (max_live); | |
3362 | args[6] = make_number (max_zombies); | |
83fc9c63 DL |
3363 | args[7] = zombie_list; |
3364 | return Fmessage (8, args); | |
34400008 GM |
3365 | } |
3366 | ||
3367 | #endif /* GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES */ | |
3368 | ||
3369 | ||
182ff242 GM |
3370 | /* Mark OBJ if we can prove it's a Lisp_Object. */ |
3371 | ||
3372 | static INLINE void | |
3373 | mark_maybe_object (obj) | |
3374 | Lisp_Object obj; | |
3375 | { | |
3376 | void *po = (void *) XPNTR (obj); | |
3377 | struct mem_node *m = mem_find (po); | |
177c0ea7 | 3378 | |
182ff242 GM |
3379 | if (m != MEM_NIL) |
3380 | { | |
3381 | int mark_p = 0; | |
3382 | ||
3383 | switch (XGCTYPE (obj)) | |
3384 | { | |
3385 | case Lisp_String: | |
3386 | mark_p = (live_string_p (m, po) | |
3387 | && !STRING_MARKED_P ((struct Lisp_String *) po)); | |
3388 | break; | |
3389 | ||
3390 | case Lisp_Cons: | |
3391 | mark_p = (live_cons_p (m, po) | |
3392 | && !XMARKBIT (XCONS (obj)->car)); | |
3393 | break; | |
3394 | ||
3395 | case Lisp_Symbol: | |
3396 | mark_p = (live_symbol_p (m, po) | |
3397 | && !XMARKBIT (XSYMBOL (obj)->plist)); | |
3398 | break; | |
3399 | ||
3400 | case Lisp_Float: | |
3401 | mark_p = (live_float_p (m, po) | |
3402 | && !XMARKBIT (XFLOAT (obj)->type)); | |
3403 | break; | |
3404 | ||
3405 | case Lisp_Vectorlike: | |
3406 | /* Note: can't check GC_BUFFERP before we know it's a | |
3407 | buffer because checking that dereferences the pointer | |
3408 | PO which might point anywhere. */ | |
3409 | if (live_vector_p (m, po)) | |
3410 | mark_p = (!GC_SUBRP (obj) | |
3411 | && !(XVECTOR (obj)->size & ARRAY_MARK_FLAG)); | |
3412 | else if (live_buffer_p (m, po)) | |
3413 | mark_p = GC_BUFFERP (obj) && !XMARKBIT (XBUFFER (obj)->name); | |
3414 | break; | |
3415 | ||
3416 | case Lisp_Misc: | |
3417 | if (live_misc_p (m, po)) | |
3418 | { | |
3419 | switch (XMISCTYPE (obj)) | |
3420 | { | |
3421 | case Lisp_Misc_Marker: | |
3422 | mark_p = !XMARKBIT (XMARKER (obj)->chain); | |
3423 | break; | |
177c0ea7 | 3424 | |
182ff242 GM |
3425 | case Lisp_Misc_Buffer_Local_Value: |
3426 | case Lisp_Misc_Some_Buffer_Local_Value: | |
3427 | mark_p = !XMARKBIT (XBUFFER_LOCAL_VALUE (obj)->realvalue); | |
3428 | break; | |
177c0ea7 | 3429 | |
182ff242 GM |
3430 | case Lisp_Misc_Overlay: |
3431 | mark_p = !XMARKBIT (XOVERLAY (obj)->plist); | |
3432 | break; | |
3433 | } | |
3434 | } | |
3435 | break; | |
6bbd7a29 GM |
3436 | |
3437 | case Lisp_Int: | |
31d929e5 | 3438 | case Lisp_Type_Limit: |
6bbd7a29 | 3439 | break; |
182ff242 GM |
3440 | } |
3441 | ||
3442 | if (mark_p) | |
3443 | { | |
3444 | #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES | |
3445 | if (nzombies < MAX_ZOMBIES) | |
83fc9c63 | 3446 | zombies[nzombies] = obj; |
182ff242 GM |
3447 | ++nzombies; |
3448 | #endif | |
3449 | mark_object (&obj); | |
3450 | } | |
3451 | } | |
3452 | } | |
ece93c02 GM |
3453 | |
3454 | ||
3455 | /* If P points to Lisp data, mark that as live if it isn't already | |
3456 | marked. */ | |
3457 | ||
3458 | static INLINE void | |
3459 | mark_maybe_pointer (p) | |
3460 | void *p; | |
3461 | { | |
3462 | struct mem_node *m; | |
3463 | ||
3464 | /* Quickly rule out some values which can't point to Lisp data. We | |
3465 | assume that Lisp data is aligned on even addresses. */ | |
3466 | if ((EMACS_INT) p & 1) | |
3467 | return; | |
177c0ea7 | 3468 | |
ece93c02 GM |
3469 | m = mem_find (p); |
3470 | if (m != MEM_NIL) | |
3471 | { | |
3472 | Lisp_Object obj = Qnil; | |
177c0ea7 | 3473 | |
ece93c02 GM |
3474 | switch (m->type) |
3475 | { | |
3476 | case MEM_TYPE_NON_LISP: | |
2fe50224 | 3477 | /* Nothing to do; not a pointer to Lisp memory. */ |
ece93c02 | 3478 | break; |
177c0ea7 | 3479 | |
ece93c02 GM |
3480 | case MEM_TYPE_BUFFER: |
3481 | if (live_buffer_p (m, p) | |
3482 | && !XMARKBIT (((struct buffer *) p)->name)) | |
3483 | XSETVECTOR (obj, p); | |
3484 | break; | |
177c0ea7 | 3485 | |
ece93c02 GM |
3486 | case MEM_TYPE_CONS: |
3487 | if (live_cons_p (m, p) | |
3488 | && !XMARKBIT (((struct Lisp_Cons *) p)->car)) | |
3489 | XSETCONS (obj, p); | |
3490 | break; | |
177c0ea7 | 3491 | |
ece93c02 GM |
3492 | case MEM_TYPE_STRING: |
3493 | if (live_string_p (m, p) | |
3494 | && !STRING_MARKED_P ((struct Lisp_String *) p)) | |
3495 | XSETSTRING (obj, p); | |
3496 | break; | |
3497 | ||
3498 | case MEM_TYPE_MISC: | |
3499 | if (live_misc_p (m, p)) | |
3500 | { | |
3501 | Lisp_Object tem; | |
3502 | XSETMISC (tem, p); | |
177c0ea7 | 3503 | |
ece93c02 GM |
3504 | switch (XMISCTYPE (tem)) |
3505 | { | |
3506 | case Lisp_Misc_Marker: | |
3507 | if (!XMARKBIT (XMARKER (tem)->chain)) | |
3508 | obj = tem; | |
3509 | break; | |
177c0ea7 | 3510 | |
ece93c02 GM |
3511 | case Lisp_Misc_Buffer_Local_Value: |
3512 | case Lisp_Misc_Some_Buffer_Local_Value: | |
3513 | if (!XMARKBIT (XBUFFER_LOCAL_VALUE (tem)->realvalue)) | |
3514 | obj = tem; | |
3515 | break; | |
177c0ea7 | 3516 | |
ece93c02 GM |
3517 | case Lisp_Misc_Overlay: |
3518 | if (!XMARKBIT (XOVERLAY (tem)->plist)) | |
3519 | obj = tem; | |
3520 | break; | |
3521 | } | |
3522 | } | |
3523 | break; | |
177c0ea7 | 3524 | |
ece93c02 GM |
3525 | case MEM_TYPE_SYMBOL: |
3526 | if (live_symbol_p (m, p) | |
3527 | && !XMARKBIT (((struct Lisp_Symbol *) p)->plist)) | |
3528 | XSETSYMBOL (obj, p); | |
3529 | break; | |
177c0ea7 | 3530 | |
ece93c02 GM |
3531 | case MEM_TYPE_FLOAT: |
3532 | if (live_float_p (m, p) | |
3533 | && !XMARKBIT (((struct Lisp_Float *) p)->type)) | |
3534 | XSETFLOAT (obj, p); | |
3535 | break; | |
177c0ea7 | 3536 | |
ece93c02 GM |
3537 | case MEM_TYPE_VECTOR: |
3538 | case MEM_TYPE_PROCESS: | |
3539 | case MEM_TYPE_HASH_TABLE: | |
3540 | case MEM_TYPE_FRAME: | |
3541 | case MEM_TYPE_WINDOW: | |
3542 | if (live_vector_p (m, p)) | |
3543 | { | |
3544 | Lisp_Object tem; | |
3545 | XSETVECTOR (tem, p); | |
3546 | if (!GC_SUBRP (tem) | |
3547 | && !(XVECTOR (tem)->size & ARRAY_MARK_FLAG)) | |
3548 | obj = tem; | |
3549 | } | |
3550 | break; | |
3551 | ||
3552 | default: | |
3553 | abort (); | |
3554 | } | |
3555 | ||
3556 | if (!GC_NILP (obj)) | |
3557 | mark_object (&obj); | |
3558 | } | |
3559 | } | |
3560 | ||
3561 | ||
3562 | /* Mark Lisp objects referenced from the address range START..END. */ | |
34400008 | 3563 | |
177c0ea7 | 3564 | static void |
34400008 GM |
3565 | mark_memory (start, end) |
3566 | void *start, *end; | |
3567 | { | |
3568 | Lisp_Object *p; | |
ece93c02 | 3569 | void **pp; |
34400008 GM |
3570 | |
3571 | #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES | |
3572 | nzombies = 0; | |
3573 | #endif | |
3574 | ||
3575 | /* Make START the pointer to the start of the memory region, | |
3576 | if it isn't already. */ | |
3577 | if (end < start) | |
3578 | { | |
3579 | void *tem = start; | |
3580 | start = end; | |
3581 | end = tem; | |
3582 | } | |
ece93c02 GM |
3583 | |
3584 | /* Mark Lisp_Objects. */ | |
34400008 | 3585 | for (p = (Lisp_Object *) start; (void *) p < end; ++p) |
182ff242 | 3586 | mark_maybe_object (*p); |
ece93c02 GM |
3587 | |
3588 | /* Mark Lisp data pointed to. This is necessary because, in some | |
3589 | situations, the C compiler optimizes Lisp objects away, so that | |
3590 | only a pointer to them remains. Example: | |
3591 | ||
3592 | DEFUN ("testme", Ftestme, Stestme, 0, 0, 0, "") | |
7ee72033 | 3593 | () |
ece93c02 GM |
3594 | { |
3595 | Lisp_Object obj = build_string ("test"); | |
3596 | struct Lisp_String *s = XSTRING (obj); | |
3597 | Fgarbage_collect (); | |
3598 | fprintf (stderr, "test `%s'\n", s->data); | |
3599 | return Qnil; | |
3600 | } | |
3601 | ||
3602 | Here, `obj' isn't really used, and the compiler optimizes it | |
3603 | away. The only reference to the life string is through the | |
3604 | pointer `s'. */ | |
177c0ea7 | 3605 | |
ece93c02 GM |
3606 | for (pp = (void **) start; (void *) pp < end; ++pp) |
3607 | mark_maybe_pointer (*pp); | |
182ff242 GM |
3608 | } |
3609 | ||
30f637f8 DL |
3610 | /* setjmp will work with GCC unless NON_SAVING_SETJMP is defined in |
3611 | the GCC system configuration. In gcc 3.2, the only systems for | |
3612 | which this is so are i386-sco5 non-ELF, i386-sysv3 (maybe included | |
3613 | by others?) and ns32k-pc532-min. */ | |
182ff242 GM |
3614 | |
3615 | #if !defined GC_SAVE_REGISTERS_ON_STACK && !defined GC_SETJMP_WORKS | |
3616 | ||
3617 | static int setjmp_tested_p, longjmps_done; | |
3618 | ||
3619 | #define SETJMP_WILL_LIKELY_WORK "\ | |
3620 | \n\ | |
3621 | Emacs garbage collector has been changed to use conservative stack\n\ | |
3622 | marking. Emacs has determined that the method it uses to do the\n\ | |
3623 | marking will likely work on your system, but this isn't sure.\n\ | |
3624 | \n\ | |
3625 | If you are a system-programmer, or can get the help of a local wizard\n\ | |
3626 | who is, please take a look at the function mark_stack in alloc.c, and\n\ | |
3627 | verify that the methods used are appropriate for your system.\n\ | |
3628 | \n\ | |
d191623b | 3629 | Please mail the result to <emacs-devel@gnu.org>.\n\ |
182ff242 GM |
3630 | " |
3631 | ||
3632 | #define SETJMP_WILL_NOT_WORK "\ | |
3633 | \n\ | |
3634 | Emacs garbage collector has been changed to use conservative stack\n\ | |
3635 | marking. Emacs has determined that the default method it uses to do the\n\ | |
3636 | marking will not work on your system. We will need a system-dependent\n\ | |
3637 | solution for your system.\n\ | |
3638 | \n\ | |
3639 | Please take a look at the function mark_stack in alloc.c, and\n\ | |
3640 | try to find a way to make it work on your system.\n\ | |
30f637f8 DL |
3641 | \n\ |
3642 | Note that you may get false negatives, depending on the compiler.\n\ | |
3643 | In particular, you need to use -O with GCC for this test.\n\ | |
3644 | \n\ | |
d191623b | 3645 | Please mail the result to <emacs-devel@gnu.org>.\n\ |
182ff242 GM |
3646 | " |
3647 | ||
3648 | ||
3649 | /* Perform a quick check if it looks like setjmp saves registers in a | |
3650 | jmp_buf. Print a message to stderr saying so. When this test | |
3651 | succeeds, this is _not_ a proof that setjmp is sufficient for | |
3652 | conservative stack marking. Only the sources or a disassembly | |
3653 | can prove that. */ | |
3654 | ||
3655 | static void | |
3656 | test_setjmp () | |
3657 | { | |
3658 | char buf[10]; | |
3659 | register int x; | |
3660 | jmp_buf jbuf; | |
3661 | int result = 0; | |
3662 | ||
3663 | /* Arrange for X to be put in a register. */ | |
3664 | sprintf (buf, "1"); | |
3665 | x = strlen (buf); | |
3666 | x = 2 * x - 1; | |
3667 | ||
3668 | setjmp (jbuf); | |
3669 | if (longjmps_done == 1) | |
34400008 | 3670 | { |
182ff242 | 3671 | /* Came here after the longjmp at the end of the function. |
34400008 | 3672 | |
182ff242 GM |
3673 | If x == 1, the longjmp has restored the register to its |
3674 | value before the setjmp, and we can hope that setjmp | |
3675 | saves all such registers in the jmp_buf, although that | |
3676 | isn't sure. | |
34400008 | 3677 | |
182ff242 GM |
3678 | For other values of X, either something really strange is |
3679 | taking place, or the setjmp just didn't save the register. */ | |
3680 | ||
3681 | if (x == 1) | |
3682 | fprintf (stderr, SETJMP_WILL_LIKELY_WORK); | |
3683 | else | |
3684 | { | |
3685 | fprintf (stderr, SETJMP_WILL_NOT_WORK); | |
3686 | exit (1); | |
34400008 GM |
3687 | } |
3688 | } | |
182ff242 GM |
3689 | |
3690 | ++longjmps_done; | |
3691 | x = 2; | |
3692 | if (longjmps_done == 1) | |
3693 | longjmp (jbuf, 1); | |
34400008 GM |
3694 | } |
3695 | ||
182ff242 GM |
3696 | #endif /* not GC_SAVE_REGISTERS_ON_STACK && not GC_SETJMP_WORKS */ |
3697 | ||
34400008 GM |
3698 | |
3699 | #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS | |
3700 | ||
3701 | /* Abort if anything GCPRO'd doesn't survive the GC. */ | |
3702 | ||
3703 | static void | |
3704 | check_gcpros () | |
3705 | { | |
3706 | struct gcpro *p; | |
3707 | int i; | |
3708 | ||
3709 | for (p = gcprolist; p; p = p->next) | |
3710 | for (i = 0; i < p->nvars; ++i) | |
3711 | if (!survives_gc_p (p->var[i])) | |
3712 | abort (); | |
3713 | } | |
3714 | ||
3715 | #elif GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES | |
3716 | ||
3717 | static void | |
3718 | dump_zombies () | |
3719 | { | |
3720 | int i; | |
3721 | ||
3722 | fprintf (stderr, "\nZombies kept alive = %d:\n", nzombies); | |
3723 | for (i = 0; i < min (MAX_ZOMBIES, nzombies); ++i) | |
3724 | { | |
3725 | fprintf (stderr, " %d = ", i); | |
3726 | debug_print (zombies[i]); | |
3727 | } | |
3728 | } | |
3729 | ||
3730 | #endif /* GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES */ | |
3731 | ||
3732 | ||
182ff242 GM |
3733 | /* Mark live Lisp objects on the C stack. |
3734 | ||
3735 | There are several system-dependent problems to consider when | |
3736 | porting this to new architectures: | |
3737 | ||
3738 | Processor Registers | |
3739 | ||
3740 | We have to mark Lisp objects in CPU registers that can hold local | |
3741 | variables or are used to pass parameters. | |
3742 | ||
3743 | If GC_SAVE_REGISTERS_ON_STACK is defined, it should expand to | |
3744 | something that either saves relevant registers on the stack, or | |
3745 | calls mark_maybe_object passing it each register's contents. | |
3746 | ||
3747 | If GC_SAVE_REGISTERS_ON_STACK is not defined, the current | |
3748 | implementation assumes that calling setjmp saves registers we need | |
3749 | to see in a jmp_buf which itself lies on the stack. This doesn't | |
3750 | have to be true! It must be verified for each system, possibly | |
3751 | by taking a look at the source code of setjmp. | |
3752 | ||
3753 | Stack Layout | |
3754 | ||
3755 | Architectures differ in the way their processor stack is organized. | |
3756 | For example, the stack might look like this | |
3757 | ||
3758 | +----------------+ | |
3759 | | Lisp_Object | size = 4 | |
3760 | +----------------+ | |
3761 | | something else | size = 2 | |
3762 | +----------------+ | |
3763 | | Lisp_Object | size = 4 | |
3764 | +----------------+ | |
3765 | | ... | | |
3766 | ||
3767 | In such a case, not every Lisp_Object will be aligned equally. To | |
3768 | find all Lisp_Object on the stack it won't be sufficient to walk | |
3769 | the stack in steps of 4 bytes. Instead, two passes will be | |
3770 | necessary, one starting at the start of the stack, and a second | |
3771 | pass starting at the start of the stack + 2. Likewise, if the | |
3772 | minimal alignment of Lisp_Objects on the stack is 1, four passes | |
3773 | would be necessary, each one starting with one byte more offset | |
3774 | from the stack start. | |
3775 | ||
3776 | The current code assumes by default that Lisp_Objects are aligned | |
3777 | equally on the stack. */ | |
34400008 GM |
3778 | |
3779 | static void | |
3780 | mark_stack () | |
3781 | { | |
630909a5 | 3782 | int i; |
34400008 | 3783 | jmp_buf j; |
6bbd7a29 | 3784 | volatile int stack_grows_down_p = (char *) &j > (char *) stack_base; |
34400008 GM |
3785 | void *end; |
3786 | ||
3787 | /* This trick flushes the register windows so that all the state of | |
3788 | the process is contained in the stack. */ | |
422eec7e DL |
3789 | /* Fixme: Code in the Boehm GC sugests flushing (with `flushrs') is |
3790 | needed on ia64 too. See mach_dep.c, where it also says inline | |
3791 | assembler doesn't work with relevant proprietary compilers. */ | |
34400008 GM |
3792 | #ifdef sparc |
3793 | asm ("ta 3"); | |
3794 | #endif | |
177c0ea7 | 3795 | |
34400008 GM |
3796 | /* Save registers that we need to see on the stack. We need to see |
3797 | registers used to hold register variables and registers used to | |
3798 | pass parameters. */ | |
3799 | #ifdef GC_SAVE_REGISTERS_ON_STACK | |
3800 | GC_SAVE_REGISTERS_ON_STACK (end); | |
182ff242 | 3801 | #else /* not GC_SAVE_REGISTERS_ON_STACK */ |
177c0ea7 | 3802 | |
182ff242 GM |
3803 | #ifndef GC_SETJMP_WORKS /* If it hasn't been checked yet that |
3804 | setjmp will definitely work, test it | |
3805 | and print a message with the result | |
3806 | of the test. */ | |
3807 | if (!setjmp_tested_p) | |
3808 | { | |
3809 | setjmp_tested_p = 1; | |
3810 | test_setjmp (); | |
3811 | } | |
3812 | #endif /* GC_SETJMP_WORKS */ | |
177c0ea7 | 3813 | |
34400008 GM |
3814 | setjmp (j); |
3815 | end = stack_grows_down_p ? (char *) &j + sizeof j : (char *) &j; | |
182ff242 | 3816 | #endif /* not GC_SAVE_REGISTERS_ON_STACK */ |
34400008 GM |
3817 | |
3818 | /* This assumes that the stack is a contiguous region in memory. If | |
182ff242 GM |
3819 | that's not the case, something has to be done here to iterate |
3820 | over the stack segments. */ | |
630909a5 | 3821 | #ifndef GC_LISP_OBJECT_ALIGNMENT |
422eec7e DL |
3822 | #ifdef __GNUC__ |
3823 | #define GC_LISP_OBJECT_ALIGNMENT __alignof__ (Lisp_Object) | |
3824 | #else | |
630909a5 | 3825 | #define GC_LISP_OBJECT_ALIGNMENT sizeof (Lisp_Object) |
422eec7e | 3826 | #endif |
182ff242 | 3827 | #endif |
24452cd5 | 3828 | for (i = 0; i < sizeof (Lisp_Object); i += GC_LISP_OBJECT_ALIGNMENT) |
630909a5 | 3829 | mark_memory ((char *) stack_base + i, end); |
34400008 GM |
3830 | |
3831 | #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS | |
3832 | check_gcpros (); | |
3833 | #endif | |
3834 | } | |
3835 | ||
3836 | ||
3837 | #endif /* GC_MARK_STACK != 0 */ | |
3838 | ||
3839 | ||
3840 | \f | |
2e471eb5 GM |
3841 | /*********************************************************************** |
3842 | Pure Storage Management | |
3843 | ***********************************************************************/ | |
3844 | ||
1f0b3fd2 GM |
3845 | /* Allocate room for SIZE bytes from pure Lisp storage and return a |
3846 | pointer to it. TYPE is the Lisp type for which the memory is | |
3847 | allocated. TYPE < 0 means it's not used for a Lisp object. | |
3848 | ||
3849 | If store_pure_type_info is set and TYPE is >= 0, the type of | |
3850 | the allocated object is recorded in pure_types. */ | |
3851 | ||
3852 | static POINTER_TYPE * | |
3853 | pure_alloc (size, type) | |
3854 | size_t size; | |
3855 | int type; | |
3856 | { | |
1f0b3fd2 | 3857 | POINTER_TYPE *result; |
44117420 | 3858 | size_t alignment = sizeof (EMACS_INT); |
1f0b3fd2 GM |
3859 | |
3860 | /* Give Lisp_Floats an extra alignment. */ | |
3861 | if (type == Lisp_Float) | |
3862 | { | |
1f0b3fd2 GM |
3863 | #if defined __GNUC__ && __GNUC__ >= 2 |
3864 | alignment = __alignof (struct Lisp_Float); | |
3865 | #else | |
3866 | alignment = sizeof (struct Lisp_Float); | |
3867 | #endif | |
9e713715 | 3868 | } |
1f0b3fd2 | 3869 | |
44117420 KS |
3870 | again: |
3871 | result = (POINTER_TYPE *) ALIGN ((EMACS_UINT)purebeg + pure_bytes_used, alignment); | |
3872 | pure_bytes_used = ((char *)result - (char *)purebeg) + size; | |
3873 | ||
3874 | if (pure_bytes_used <= pure_size) | |
3875 | return result; | |
3876 | ||
3877 | /* Don't allocate a large amount here, | |
3878 | because it might get mmap'd and then its address | |
3879 | might not be usable. */ | |
3880 | purebeg = (char *) xmalloc (10000); | |
3881 | pure_size = 10000; | |
3882 | pure_bytes_used_before_overflow += pure_bytes_used - size; | |
3883 | pure_bytes_used = 0; | |
3884 | goto again; | |
1f0b3fd2 GM |
3885 | } |
3886 | ||
3887 | ||
852f8cdc | 3888 | /* Print a warning if PURESIZE is too small. */ |
9e713715 GM |
3889 | |
3890 | void | |
3891 | check_pure_size () | |
3892 | { | |
3893 | if (pure_bytes_used_before_overflow) | |
a4d35afd SM |
3894 | message ("Pure Lisp storage overflow (approx. %d bytes needed)", |
3895 | (int) (pure_bytes_used + pure_bytes_used_before_overflow)); | |
9e713715 GM |
3896 | } |
3897 | ||
3898 | ||
2e471eb5 GM |
3899 | /* Return a string allocated in pure space. DATA is a buffer holding |
3900 | NCHARS characters, and NBYTES bytes of string data. MULTIBYTE | |
3901 | non-zero means make the result string multibyte. | |
1a4f1e2c | 3902 | |
2e471eb5 GM |
3903 | Must get an error if pure storage is full, since if it cannot hold |
3904 | a large string it may be able to hold conses that point to that | |
3905 | string; then the string is not protected from gc. */ | |
7146af97 JB |
3906 | |
3907 | Lisp_Object | |
2e471eb5 | 3908 | make_pure_string (data, nchars, nbytes, multibyte) |
7146af97 | 3909 | char *data; |
2e471eb5 | 3910 | int nchars, nbytes; |
c0696668 | 3911 | int multibyte; |
7146af97 | 3912 | { |
2e471eb5 GM |
3913 | Lisp_Object string; |
3914 | struct Lisp_String *s; | |
c0696668 | 3915 | |
1f0b3fd2 GM |
3916 | s = (struct Lisp_String *) pure_alloc (sizeof *s, Lisp_String); |
3917 | s->data = (unsigned char *) pure_alloc (nbytes + 1, -1); | |
2e471eb5 GM |
3918 | s->size = nchars; |
3919 | s->size_byte = multibyte ? nbytes : -1; | |
3920 | bcopy (data, s->data, nbytes); | |
3921 | s->data[nbytes] = '\0'; | |
3922 | s->intervals = NULL_INTERVAL; | |
2e471eb5 GM |
3923 | XSETSTRING (string, s); |
3924 | return string; | |
7146af97 JB |
3925 | } |
3926 | ||
2e471eb5 | 3927 | |
34400008 GM |
3928 | /* Return a cons allocated from pure space. Give it pure copies |
3929 | of CAR as car and CDR as cdr. */ | |
3930 | ||
7146af97 JB |
3931 | Lisp_Object |
3932 | pure_cons (car, cdr) | |
3933 | Lisp_Object car, cdr; | |
3934 | { | |
3935 | register Lisp_Object new; | |
1f0b3fd2 | 3936 | struct Lisp_Cons *p; |
7146af97 | 3937 | |
1f0b3fd2 GM |
3938 | p = (struct Lisp_Cons *) pure_alloc (sizeof *p, Lisp_Cons); |
3939 | XSETCONS (new, p); | |
f3fbd155 KR |
3940 | XSETCAR (new, Fpurecopy (car)); |
3941 | XSETCDR (new, Fpurecopy (cdr)); | |
7146af97 JB |
3942 | return new; |
3943 | } | |
3944 | ||
7146af97 | 3945 | |
34400008 GM |
3946 | /* Value is a float object with value NUM allocated from pure space. */ |
3947 | ||
7146af97 JB |
3948 | Lisp_Object |
3949 | make_pure_float (num) | |
3950 | double num; | |
3951 | { | |
3952 | register Lisp_Object new; | |
1f0b3fd2 | 3953 | struct Lisp_Float *p; |
7146af97 | 3954 | |
1f0b3fd2 GM |
3955 | p = (struct Lisp_Float *) pure_alloc (sizeof *p, Lisp_Float); |
3956 | XSETFLOAT (new, p); | |
70949dac | 3957 | XFLOAT_DATA (new) = num; |
7146af97 JB |
3958 | return new; |
3959 | } | |
3960 | ||
34400008 GM |
3961 | |
3962 | /* Return a vector with room for LEN Lisp_Objects allocated from | |
3963 | pure space. */ | |
3964 | ||
7146af97 JB |
3965 | Lisp_Object |
3966 | make_pure_vector (len) | |
42607681 | 3967 | EMACS_INT len; |
7146af97 | 3968 | { |
1f0b3fd2 GM |
3969 | Lisp_Object new; |
3970 | struct Lisp_Vector *p; | |
3971 | size_t size = sizeof *p + (len - 1) * sizeof (Lisp_Object); | |
7146af97 | 3972 | |
1f0b3fd2 GM |
3973 | p = (struct Lisp_Vector *) pure_alloc (size, Lisp_Vectorlike); |
3974 | XSETVECTOR (new, p); | |
7146af97 JB |
3975 | XVECTOR (new)->size = len; |
3976 | return new; | |
3977 | } | |
3978 | ||
34400008 | 3979 | |
7146af97 | 3980 | DEFUN ("purecopy", Fpurecopy, Spurecopy, 1, 1, 0, |
7ee72033 | 3981 | doc: /* Make a copy of OBJECT in pure storage. |
228299fa | 3982 | Recursively copies contents of vectors and cons cells. |
7ee72033 MB |
3983 | Does not copy symbols. Copies strings without text properties. */) |
3984 | (obj) | |
7146af97 JB |
3985 | register Lisp_Object obj; |
3986 | { | |
265a9e55 | 3987 | if (NILP (Vpurify_flag)) |
7146af97 JB |
3988 | return obj; |
3989 | ||
1f0b3fd2 | 3990 | if (PURE_POINTER_P (XPNTR (obj))) |
7146af97 JB |
3991 | return obj; |
3992 | ||
d6dd74bb | 3993 | if (CONSP (obj)) |
70949dac | 3994 | return pure_cons (XCAR (obj), XCDR (obj)); |
d6dd74bb | 3995 | else if (FLOATP (obj)) |
70949dac | 3996 | return make_pure_float (XFLOAT_DATA (obj)); |
d6dd74bb | 3997 | else if (STRINGP (obj)) |
d5db4077 KR |
3998 | return make_pure_string (SDATA (obj), SCHARS (obj), |
3999 | SBYTES (obj), | |
c0696668 | 4000 | STRING_MULTIBYTE (obj)); |
d6dd74bb KH |
4001 | else if (COMPILEDP (obj) || VECTORP (obj)) |
4002 | { | |
4003 | register struct Lisp_Vector *vec; | |
4004 | register int i, size; | |
4005 | ||
4006 | size = XVECTOR (obj)->size; | |
7d535c68 KH |
4007 | if (size & PSEUDOVECTOR_FLAG) |
4008 | size &= PSEUDOVECTOR_SIZE_MASK; | |
01a4d290 | 4009 | vec = XVECTOR (make_pure_vector ((EMACS_INT) size)); |
d6dd74bb KH |
4010 | for (i = 0; i < size; i++) |
4011 | vec->contents[i] = Fpurecopy (XVECTOR (obj)->contents[i]); | |
4012 | if (COMPILEDP (obj)) | |
4013 | XSETCOMPILED (obj, vec); | |
4014 | else | |
4015 | XSETVECTOR (obj, vec); | |
7146af97 JB |
4016 | return obj; |
4017 | } | |
d6dd74bb KH |
4018 | else if (MARKERP (obj)) |
4019 | error ("Attempt to copy a marker to pure storage"); | |
6bbd7a29 GM |
4020 | |
4021 | return obj; | |
7146af97 | 4022 | } |
2e471eb5 | 4023 | |
34400008 | 4024 | |
7146af97 | 4025 | \f |
34400008 GM |
4026 | /*********************************************************************** |
4027 | Protection from GC | |
4028 | ***********************************************************************/ | |
4029 | ||
2e471eb5 GM |
4030 | /* Put an entry in staticvec, pointing at the variable with address |
4031 | VARADDRESS. */ | |
7146af97 JB |
4032 | |
4033 | void | |
4034 | staticpro (varaddress) | |
4035 | Lisp_Object *varaddress; | |
4036 | { | |
4037 | staticvec[staticidx++] = varaddress; | |
4038 | if (staticidx >= NSTATICS) | |
4039 | abort (); | |
4040 | } | |
4041 | ||
4042 | struct catchtag | |
2e471eb5 | 4043 | { |
7146af97 JB |
4044 | Lisp_Object tag; |
4045 | Lisp_Object val; | |
4046 | struct catchtag *next; | |
2e471eb5 | 4047 | }; |
7146af97 JB |
4048 | |
4049 | struct backtrace | |
2e471eb5 GM |
4050 | { |
4051 | struct backtrace *next; | |
4052 | Lisp_Object *function; | |
4053 | Lisp_Object *args; /* Points to vector of args. */ | |
4054 | int nargs; /* Length of vector. */ | |
4055 | /* If nargs is UNEVALLED, args points to slot holding list of | |
4056 | unevalled args. */ | |
4057 | char evalargs; | |
4058 | }; | |
4059 | ||
34400008 | 4060 | |
7146af97 | 4061 | \f |
34400008 GM |
4062 | /*********************************************************************** |
4063 | Protection from GC | |
4064 | ***********************************************************************/ | |
1a4f1e2c | 4065 | |
e8197642 RS |
4066 | /* Temporarily prevent garbage collection. */ |
4067 | ||
4068 | int | |
4069 | inhibit_garbage_collection () | |
4070 | { | |
aed13378 | 4071 | int count = SPECPDL_INDEX (); |
54defd0d AS |
4072 | int nbits = min (VALBITS, BITS_PER_INT); |
4073 | ||
4074 | specbind (Qgc_cons_threshold, make_number (((EMACS_INT) 1 << (nbits - 1)) - 1)); | |
e8197642 RS |
4075 | return count; |
4076 | } | |
4077 | ||
34400008 | 4078 | |
7146af97 | 4079 | DEFUN ("garbage-collect", Fgarbage_collect, Sgarbage_collect, 0, 0, "", |
7ee72033 | 4080 | doc: /* Reclaim storage for Lisp objects no longer needed. |
228299fa GM |
4081 | Returns info on amount of space in use: |
4082 | ((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS) | |
4083 | (USED-MARKERS . FREE-MARKERS) USED-STRING-CHARS USED-VECTOR-SLOTS | |
4084 | (USED-FLOATS . FREE-FLOATS) (USED-INTERVALS . FREE-INTERVALS) | |
4085 | (USED-STRINGS . FREE-STRINGS)) | |
4086 | Garbage collection happens automatically if you cons more than | |
7ee72033 MB |
4087 | `gc-cons-threshold' bytes of Lisp data since previous garbage collection. */) |
4088 | () | |
7146af97 JB |
4089 | { |
4090 | register struct gcpro *tail; | |
4091 | register struct specbinding *bind; | |
4092 | struct catchtag *catch; | |
4093 | struct handler *handler; | |
4094 | register struct backtrace *backlist; | |
7146af97 JB |
4095 | char stack_top_variable; |
4096 | register int i; | |
6efc7df7 | 4097 | int message_p; |
96117bc7 | 4098 | Lisp_Object total[8]; |
331379bf | 4099 | int count = SPECPDL_INDEX (); |
2c5bd608 DL |
4100 | EMACS_TIME t1, t2, t3; |
4101 | ||
4102 | EMACS_GET_TIME (t1); | |
7146af97 | 4103 | |
9e713715 GM |
4104 | /* Can't GC if pure storage overflowed because we can't determine |
4105 | if something is a pure object or not. */ | |
4106 | if (pure_bytes_used_before_overflow) | |
4107 | return Qnil; | |
4108 | ||
58595309 KH |
4109 | /* In case user calls debug_print during GC, |
4110 | don't let that cause a recursive GC. */ | |
4111 | consing_since_gc = 0; | |
4112 | ||
6efc7df7 GM |
4113 | /* Save what's currently displayed in the echo area. */ |
4114 | message_p = push_message (); | |
c55b0da6 | 4115 | record_unwind_protect (pop_message_unwind, Qnil); |
41c28a37 | 4116 | |
7146af97 JB |
4117 | /* Save a copy of the contents of the stack, for debugging. */ |
4118 | #if MAX_SAVE_STACK > 0 | |
265a9e55 | 4119 | if (NILP (Vpurify_flag)) |
7146af97 JB |
4120 | { |
4121 | i = &stack_top_variable - stack_bottom; | |
4122 | if (i < 0) i = -i; | |
4123 | if (i < MAX_SAVE_STACK) | |
4124 | { | |
4125 | if (stack_copy == 0) | |
9ac0d9e0 | 4126 | stack_copy = (char *) xmalloc (stack_copy_size = i); |
7146af97 | 4127 | else if (stack_copy_size < i) |
9ac0d9e0 | 4128 | stack_copy = (char *) xrealloc (stack_copy, (stack_copy_size = i)); |
7146af97 JB |
4129 | if (stack_copy) |
4130 | { | |
42607681 | 4131 | if ((EMACS_INT) (&stack_top_variable - stack_bottom) > 0) |
7146af97 JB |
4132 | bcopy (stack_bottom, stack_copy, i); |
4133 | else | |
4134 | bcopy (&stack_top_variable, stack_copy, i); | |
4135 | } | |
4136 | } | |
4137 | } | |
4138 | #endif /* MAX_SAVE_STACK > 0 */ | |
4139 | ||
299585ee | 4140 | if (garbage_collection_messages) |
691c4285 | 4141 | message1_nolog ("Garbage collecting..."); |
7146af97 | 4142 | |
6e0fca1d RS |
4143 | BLOCK_INPUT; |
4144 | ||
eec7b73d RS |
4145 | shrink_regexp_cache (); |
4146 | ||
4929a878 | 4147 | /* Don't keep undo information around forever. */ |
7146af97 JB |
4148 | { |
4149 | register struct buffer *nextb = all_buffers; | |
4150 | ||
4151 | while (nextb) | |
4152 | { | |
ffd56f97 JB |
4153 | /* If a buffer's undo list is Qt, that means that undo is |
4154 | turned off in that buffer. Calling truncate_undo_list on | |
4155 | Qt tends to return NULL, which effectively turns undo back on. | |
4156 | So don't call truncate_undo_list if undo_list is Qt. */ | |
4157 | if (! EQ (nextb->undo_list, Qt)) | |
177c0ea7 | 4158 | nextb->undo_list |
502b9b64 JB |
4159 | = truncate_undo_list (nextb->undo_list, undo_limit, |
4160 | undo_strong_limit); | |
e0fead5d AI |
4161 | |
4162 | /* Shrink buffer gaps, but skip indirect and dead buffers. */ | |
4163 | if (nextb->base_buffer == 0 && !NILP (nextb->name)) | |
4164 | { | |
4165 | /* If a buffer's gap size is more than 10% of the buffer | |
4166 | size, or larger than 2000 bytes, then shrink it | |
4167 | accordingly. Keep a minimum size of 20 bytes. */ | |
4168 | int size = min (2000, max (20, (nextb->text->z_byte / 10))); | |
4169 | ||
4170 | if (nextb->text->gap_size > size) | |
4171 | { | |
4172 | struct buffer *save_current = current_buffer; | |
4173 | current_buffer = nextb; | |
4174 | make_gap (-(nextb->text->gap_size - size)); | |
4175 | current_buffer = save_current; | |
4176 | } | |
4177 | } | |
4178 | ||
7146af97 JB |
4179 | nextb = nextb->next; |
4180 | } | |
4181 | } | |
4182 | ||
4183 | gc_in_progress = 1; | |
4184 | ||
c23baf9f | 4185 | /* clear_marks (); */ |
7146af97 | 4186 | |
7146af97 JB |
4187 | /* Mark all the special slots that serve as the roots of accessibility. |
4188 | ||
4189 | Usually the special slots to mark are contained in particular structures. | |
4190 | Then we know no slot is marked twice because the structures don't overlap. | |
4191 | In some cases, the structures point to the slots to be marked. | |
4192 | For these, we use MARKBIT to avoid double marking of the slot. */ | |
4193 | ||
4194 | for (i = 0; i < staticidx; i++) | |
4195 | mark_object (staticvec[i]); | |
34400008 GM |
4196 | |
4197 | #if (GC_MARK_STACK == GC_MAKE_GCPROS_NOOPS \ | |
4198 | || GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS) | |
4199 | mark_stack (); | |
4200 | #else | |
7146af97 JB |
4201 | for (tail = gcprolist; tail; tail = tail->next) |
4202 | for (i = 0; i < tail->nvars; i++) | |
4203 | if (!XMARKBIT (tail->var[i])) | |
4204 | { | |
1efc2bb9 EZ |
4205 | /* Explicit casting prevents compiler warning about |
4206 | discarding the `volatile' qualifier. */ | |
4207 | mark_object ((Lisp_Object *)&tail->var[i]); | |
7146af97 JB |
4208 | XMARK (tail->var[i]); |
4209 | } | |
34400008 | 4210 | #endif |
177c0ea7 | 4211 | |
630686c8 | 4212 | mark_byte_stack (); |
7146af97 JB |
4213 | for (bind = specpdl; bind != specpdl_ptr; bind++) |
4214 | { | |
4215 | mark_object (&bind->symbol); | |
4216 | mark_object (&bind->old_value); | |
4217 | } | |
4218 | for (catch = catchlist; catch; catch = catch->next) | |
4219 | { | |
4220 | mark_object (&catch->tag); | |
4221 | mark_object (&catch->val); | |
177c0ea7 | 4222 | } |
7146af97 JB |
4223 | for (handler = handlerlist; handler; handler = handler->next) |
4224 | { | |
4225 | mark_object (&handler->handler); | |
4226 | mark_object (&handler->var); | |
177c0ea7 | 4227 | } |
7146af97 JB |
4228 | for (backlist = backtrace_list; backlist; backlist = backlist->next) |
4229 | { | |
4230 | if (!XMARKBIT (*backlist->function)) | |
4231 | { | |
4232 | mark_object (backlist->function); | |
4233 | XMARK (*backlist->function); | |
4234 | } | |
4235 | if (backlist->nargs == UNEVALLED || backlist->nargs == MANY) | |
4236 | i = 0; | |
4237 | else | |
4238 | i = backlist->nargs - 1; | |
4239 | for (; i >= 0; i--) | |
4240 | if (!XMARKBIT (backlist->args[i])) | |
4241 | { | |
4242 | mark_object (&backlist->args[i]); | |
4243 | XMARK (backlist->args[i]); | |
4244 | } | |
177c0ea7 | 4245 | } |
b875d3f7 | 4246 | mark_kboards (); |
7146af97 | 4247 | |
4c315bda RS |
4248 | /* Look thru every buffer's undo list |
4249 | for elements that update markers that were not marked, | |
4250 | and delete them. */ | |
4251 | { | |
4252 | register struct buffer *nextb = all_buffers; | |
4253 | ||
4254 | while (nextb) | |
4255 | { | |
4256 | /* If a buffer's undo list is Qt, that means that undo is | |
4257 | turned off in that buffer. Calling truncate_undo_list on | |
4258 | Qt tends to return NULL, which effectively turns undo back on. | |
4259 | So don't call truncate_undo_list if undo_list is Qt. */ | |
4260 | if (! EQ (nextb->undo_list, Qt)) | |
4261 | { | |
4262 | Lisp_Object tail, prev; | |
4263 | tail = nextb->undo_list; | |
4264 | prev = Qnil; | |
4265 | while (CONSP (tail)) | |
4266 | { | |
70949dac KR |
4267 | if (GC_CONSP (XCAR (tail)) |
4268 | && GC_MARKERP (XCAR (XCAR (tail))) | |
4269 | && ! XMARKBIT (XMARKER (XCAR (XCAR (tail)))->chain)) | |
4c315bda RS |
4270 | { |
4271 | if (NILP (prev)) | |
70949dac | 4272 | nextb->undo_list = tail = XCDR (tail); |
4c315bda | 4273 | else |
f3fbd155 KR |
4274 | { |
4275 | tail = XCDR (tail); | |
4276 | XSETCDR (prev, tail); | |
4277 | } | |
4c315bda RS |
4278 | } |
4279 | else | |
4280 | { | |
4281 | prev = tail; | |
70949dac | 4282 | tail = XCDR (tail); |
4c315bda RS |
4283 | } |
4284 | } | |
4285 | } | |
4286 | ||
4287 | nextb = nextb->next; | |
4288 | } | |
4289 | } | |
4290 | ||
34400008 GM |
4291 | #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES |
4292 | mark_stack (); | |
4293 | #endif | |
4294 | ||
488dd4c4 JD |
4295 | #ifdef USE_GTK |
4296 | { | |
4297 | extern void xg_mark_data (); | |
4298 | xg_mark_data (); | |
4299 | } | |
4300 | #endif | |
4301 | ||
7146af97 JB |
4302 | gc_sweep (); |
4303 | ||
4304 | /* Clear the mark bits that we set in certain root slots. */ | |
4305 | ||
34400008 GM |
4306 | #if (GC_MARK_STACK == GC_USE_GCPROS_AS_BEFORE \ |
4307 | || GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES) | |
7146af97 JB |
4308 | for (tail = gcprolist; tail; tail = tail->next) |
4309 | for (i = 0; i < tail->nvars; i++) | |
4310 | XUNMARK (tail->var[i]); | |
34400008 | 4311 | #endif |
177c0ea7 | 4312 | |
033a5fa3 | 4313 | unmark_byte_stack (); |
7146af97 JB |
4314 | for (backlist = backtrace_list; backlist; backlist = backlist->next) |
4315 | { | |
4316 | XUNMARK (*backlist->function); | |
4317 | if (backlist->nargs == UNEVALLED || backlist->nargs == MANY) | |
4318 | i = 0; | |
4319 | else | |
4320 | i = backlist->nargs - 1; | |
4321 | for (; i >= 0; i--) | |
4322 | XUNMARK (backlist->args[i]); | |
177c0ea7 | 4323 | } |
7146af97 JB |
4324 | XUNMARK (buffer_defaults.name); |
4325 | XUNMARK (buffer_local_symbols.name); | |
4326 | ||
34400008 GM |
4327 | #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES && 0 |
4328 | dump_zombies (); | |
4329 | #endif | |
4330 | ||
6e0fca1d RS |
4331 | UNBLOCK_INPUT; |
4332 | ||
c23baf9f | 4333 | /* clear_marks (); */ |
7146af97 JB |
4334 | gc_in_progress = 0; |
4335 | ||
4336 | consing_since_gc = 0; | |
4337 | if (gc_cons_threshold < 10000) | |
4338 | gc_cons_threshold = 10000; | |
4339 | ||
299585ee RS |
4340 | if (garbage_collection_messages) |
4341 | { | |
6efc7df7 GM |
4342 | if (message_p || minibuf_level > 0) |
4343 | restore_message (); | |
299585ee RS |
4344 | else |
4345 | message1_nolog ("Garbage collecting...done"); | |
4346 | } | |
7146af97 | 4347 | |
98edb5ff | 4348 | unbind_to (count, Qnil); |
2e471eb5 GM |
4349 | |
4350 | total[0] = Fcons (make_number (total_conses), | |
4351 | make_number (total_free_conses)); | |
4352 | total[1] = Fcons (make_number (total_symbols), | |
4353 | make_number (total_free_symbols)); | |
4354 | total[2] = Fcons (make_number (total_markers), | |
4355 | make_number (total_free_markers)); | |
96117bc7 GM |
4356 | total[3] = make_number (total_string_size); |
4357 | total[4] = make_number (total_vector_size); | |
4358 | total[5] = Fcons (make_number (total_floats), | |
2e471eb5 | 4359 | make_number (total_free_floats)); |
96117bc7 | 4360 | total[6] = Fcons (make_number (total_intervals), |
2e471eb5 | 4361 | make_number (total_free_intervals)); |
96117bc7 | 4362 | total[7] = Fcons (make_number (total_strings), |
2e471eb5 GM |
4363 | make_number (total_free_strings)); |
4364 | ||
34400008 | 4365 | #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES |
7146af97 | 4366 | { |
34400008 GM |
4367 | /* Compute average percentage of zombies. */ |
4368 | double nlive = 0; | |
177c0ea7 | 4369 | |
34400008 | 4370 | for (i = 0; i < 7; ++i) |
83fc9c63 DL |
4371 | if (CONSP (total[i])) |
4372 | nlive += XFASTINT (XCAR (total[i])); | |
34400008 GM |
4373 | |
4374 | avg_live = (avg_live * ngcs + nlive) / (ngcs + 1); | |
4375 | max_live = max (nlive, max_live); | |
4376 | avg_zombies = (avg_zombies * ngcs + nzombies) / (ngcs + 1); | |
4377 | max_zombies = max (nzombies, max_zombies); | |
4378 | ++ngcs; | |
4379 | } | |
4380 | #endif | |
7146af97 | 4381 | |
9e713715 GM |
4382 | if (!NILP (Vpost_gc_hook)) |
4383 | { | |
4384 | int count = inhibit_garbage_collection (); | |
4385 | safe_run_hooks (Qpost_gc_hook); | |
4386 | unbind_to (count, Qnil); | |
4387 | } | |
2c5bd608 DL |
4388 | |
4389 | /* Accumulate statistics. */ | |
4390 | EMACS_GET_TIME (t2); | |
4391 | EMACS_SUB_TIME (t3, t2, t1); | |
4392 | if (FLOATP (Vgc_elapsed)) | |
69ab9f85 SM |
4393 | Vgc_elapsed = make_float (XFLOAT_DATA (Vgc_elapsed) + |
4394 | EMACS_SECS (t3) + | |
4395 | EMACS_USECS (t3) * 1.0e-6); | |
2c5bd608 DL |
4396 | gcs_done++; |
4397 | ||
96117bc7 | 4398 | return Flist (sizeof total / sizeof *total, total); |
7146af97 | 4399 | } |
34400008 | 4400 | |
41c28a37 | 4401 | |
3770920e GM |
4402 | /* Mark Lisp objects in glyph matrix MATRIX. Currently the |
4403 | only interesting objects referenced from glyphs are strings. */ | |
41c28a37 GM |
4404 | |
4405 | static void | |
4406 | mark_glyph_matrix (matrix) | |
4407 | struct glyph_matrix *matrix; | |
4408 | { | |
4409 | struct glyph_row *row = matrix->rows; | |
4410 | struct glyph_row *end = row + matrix->nrows; | |
4411 | ||
2e471eb5 GM |
4412 | for (; row < end; ++row) |
4413 | if (row->enabled_p) | |
4414 | { | |
4415 | int area; | |
4416 | for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area) | |
4417 | { | |
4418 | struct glyph *glyph = row->glyphs[area]; | |
4419 | struct glyph *end_glyph = glyph + row->used[area]; | |
177c0ea7 | 4420 | |
2e471eb5 GM |
4421 | for (; glyph < end_glyph; ++glyph) |
4422 | if (GC_STRINGP (glyph->object) | |
4423 | && !STRING_MARKED_P (XSTRING (glyph->object))) | |
4424 | mark_object (&glyph->object); | |
4425 | } | |
4426 | } | |
41c28a37 GM |
4427 | } |
4428 | ||
34400008 | 4429 | |
41c28a37 GM |
4430 | /* Mark Lisp faces in the face cache C. */ |
4431 | ||
4432 | static void | |
4433 | mark_face_cache (c) | |
4434 | struct face_cache *c; | |
4435 | { | |
4436 | if (c) | |
4437 | { | |
4438 | int i, j; | |
4439 | for (i = 0; i < c->used; ++i) | |
4440 | { | |
4441 | struct face *face = FACE_FROM_ID (c->f, i); | |
4442 | ||
4443 | if (face) | |
4444 | { | |
4445 | for (j = 0; j < LFACE_VECTOR_SIZE; ++j) | |
4446 | mark_object (&face->lface[j]); | |
41c28a37 GM |
4447 | } |
4448 | } | |
4449 | } | |
4450 | } | |
4451 | ||
4452 | ||
4453 | #ifdef HAVE_WINDOW_SYSTEM | |
4454 | ||
4455 | /* Mark Lisp objects in image IMG. */ | |
4456 | ||
4457 | static void | |
4458 | mark_image (img) | |
4459 | struct image *img; | |
4460 | { | |
4461 | mark_object (&img->spec); | |
177c0ea7 | 4462 | |
3e60b029 | 4463 | if (!NILP (img->data.lisp_val)) |
41c28a37 GM |
4464 | mark_object (&img->data.lisp_val); |
4465 | } | |
4466 | ||
4467 | ||
4468 | /* Mark Lisp objects in image cache of frame F. It's done this way so | |
4469 | that we don't have to include xterm.h here. */ | |
4470 | ||
4471 | static void | |
4472 | mark_image_cache (f) | |
4473 | struct frame *f; | |
4474 | { | |
4475 | forall_images_in_image_cache (f, mark_image); | |
4476 | } | |
4477 | ||
4478 | #endif /* HAVE_X_WINDOWS */ | |
4479 | ||
4480 | ||
7146af97 | 4481 | \f |
1a4f1e2c | 4482 | /* Mark reference to a Lisp_Object. |
2e471eb5 GM |
4483 | If the object referred to has not been seen yet, recursively mark |
4484 | all the references contained in it. */ | |
7146af97 | 4485 | |
785cd37f RS |
4486 | #define LAST_MARKED_SIZE 500 |
4487 | Lisp_Object *last_marked[LAST_MARKED_SIZE]; | |
4488 | int last_marked_index; | |
4489 | ||
1342fc6f RS |
4490 | /* For debugging--call abort when we cdr down this many |
4491 | links of a list, in mark_object. In debugging, | |
4492 | the call to abort will hit a breakpoint. | |
4493 | Normally this is zero and the check never goes off. */ | |
4494 | int mark_object_loop_halt; | |
4495 | ||
41c28a37 | 4496 | void |
436c5811 RS |
4497 | mark_object (argptr) |
4498 | Lisp_Object *argptr; | |
7146af97 | 4499 | { |
436c5811 | 4500 | Lisp_Object *objptr = argptr; |
7146af97 | 4501 | register Lisp_Object obj; |
4f5c1376 GM |
4502 | #ifdef GC_CHECK_MARKED_OBJECTS |
4503 | void *po; | |
4504 | struct mem_node *m; | |
4505 | #endif | |
1342fc6f | 4506 | int cdr_count = 0; |
7146af97 | 4507 | |
9149e743 | 4508 | loop: |
7146af97 | 4509 | obj = *objptr; |
9149e743 | 4510 | loop2: |
7146af97 JB |
4511 | XUNMARK (obj); |
4512 | ||
1f0b3fd2 | 4513 | if (PURE_POINTER_P (XPNTR (obj))) |
7146af97 JB |
4514 | return; |
4515 | ||
785cd37f RS |
4516 | last_marked[last_marked_index++] = objptr; |
4517 | if (last_marked_index == LAST_MARKED_SIZE) | |
4518 | last_marked_index = 0; | |
4519 | ||
4f5c1376 GM |
4520 | /* Perform some sanity checks on the objects marked here. Abort if |
4521 | we encounter an object we know is bogus. This increases GC time | |
4522 | by ~80%, and requires compilation with GC_MARK_STACK != 0. */ | |
4523 | #ifdef GC_CHECK_MARKED_OBJECTS | |
4524 | ||
4525 | po = (void *) XPNTR (obj); | |
4526 | ||
4527 | /* Check that the object pointed to by PO is known to be a Lisp | |
4528 | structure allocated from the heap. */ | |
4529 | #define CHECK_ALLOCATED() \ | |
4530 | do { \ | |
4531 | m = mem_find (po); \ | |
4532 | if (m == MEM_NIL) \ | |
4533 | abort (); \ | |
4534 | } while (0) | |
4535 | ||
4536 | /* Check that the object pointed to by PO is live, using predicate | |
4537 | function LIVEP. */ | |
4538 | #define CHECK_LIVE(LIVEP) \ | |
4539 | do { \ | |
4540 | if (!LIVEP (m, po)) \ | |
4541 | abort (); \ | |
4542 | } while (0) | |
4543 | ||
4544 | /* Check both of the above conditions. */ | |
4545 | #define CHECK_ALLOCATED_AND_LIVE(LIVEP) \ | |
4546 | do { \ | |
4547 | CHECK_ALLOCATED (); \ | |
4548 | CHECK_LIVE (LIVEP); \ | |
4549 | } while (0) \ | |
177c0ea7 | 4550 | |
4f5c1376 | 4551 | #else /* not GC_CHECK_MARKED_OBJECTS */ |
177c0ea7 | 4552 | |
4f5c1376 GM |
4553 | #define CHECK_ALLOCATED() (void) 0 |
4554 | #define CHECK_LIVE(LIVEP) (void) 0 | |
4555 | #define CHECK_ALLOCATED_AND_LIVE(LIVEP) (void) 0 | |
177c0ea7 | 4556 | |
4f5c1376 GM |
4557 | #endif /* not GC_CHECK_MARKED_OBJECTS */ |
4558 | ||
0220c518 | 4559 | switch (SWITCH_ENUM_CAST (XGCTYPE (obj))) |
7146af97 JB |
4560 | { |
4561 | case Lisp_String: | |
4562 | { | |
4563 | register struct Lisp_String *ptr = XSTRING (obj); | |
4f5c1376 | 4564 | CHECK_ALLOCATED_AND_LIVE (live_string_p); |
d5e35230 | 4565 | MARK_INTERVAL_TREE (ptr->intervals); |
2e471eb5 | 4566 | MARK_STRING (ptr); |
361b097f | 4567 | #ifdef GC_CHECK_STRING_BYTES |
676a7251 GM |
4568 | /* Check that the string size recorded in the string is the |
4569 | same as the one recorded in the sdata structure. */ | |
4570 | CHECK_STRING_BYTES (ptr); | |
361b097f | 4571 | #endif /* GC_CHECK_STRING_BYTES */ |
7146af97 JB |
4572 | } |
4573 | break; | |
4574 | ||
76437631 | 4575 | case Lisp_Vectorlike: |
4f5c1376 GM |
4576 | #ifdef GC_CHECK_MARKED_OBJECTS |
4577 | m = mem_find (po); | |
4578 | if (m == MEM_NIL && !GC_SUBRP (obj) | |
4579 | && po != &buffer_defaults | |
4580 | && po != &buffer_local_symbols) | |
4581 | abort (); | |
4582 | #endif /* GC_CHECK_MARKED_OBJECTS */ | |
177c0ea7 | 4583 | |
30e3190a | 4584 | if (GC_BUFFERP (obj)) |
6b552283 KH |
4585 | { |
4586 | if (!XMARKBIT (XBUFFER (obj)->name)) | |
4f5c1376 GM |
4587 | { |
4588 | #ifdef GC_CHECK_MARKED_OBJECTS | |
4589 | if (po != &buffer_defaults && po != &buffer_local_symbols) | |
4590 | { | |
4591 | struct buffer *b; | |
4592 | for (b = all_buffers; b && b != po; b = b->next) | |
4593 | ; | |
4594 | if (b == NULL) | |
4595 | abort (); | |
4596 | } | |
4597 | #endif /* GC_CHECK_MARKED_OBJECTS */ | |
4598 | mark_buffer (obj); | |
4599 | } | |
6b552283 | 4600 | } |
30e3190a | 4601 | else if (GC_SUBRP (obj)) |
169ee243 RS |
4602 | break; |
4603 | else if (GC_COMPILEDP (obj)) | |
2e471eb5 GM |
4604 | /* We could treat this just like a vector, but it is better to |
4605 | save the COMPILED_CONSTANTS element for last and avoid | |
4606 | recursion there. */ | |
169ee243 RS |
4607 | { |
4608 | register struct Lisp_Vector *ptr = XVECTOR (obj); | |
4609 | register EMACS_INT size = ptr->size; | |
169ee243 RS |
4610 | register int i; |
4611 | ||
4612 | if (size & ARRAY_MARK_FLAG) | |
4613 | break; /* Already marked */ | |
177c0ea7 | 4614 | |
4f5c1376 | 4615 | CHECK_LIVE (live_vector_p); |
169ee243 | 4616 | ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */ |
76437631 | 4617 | size &= PSEUDOVECTOR_SIZE_MASK; |
169ee243 RS |
4618 | for (i = 0; i < size; i++) /* and then mark its elements */ |
4619 | { | |
4620 | if (i != COMPILED_CONSTANTS) | |
c70bbf06 | 4621 | mark_object (&ptr->contents[i]); |
169ee243 RS |
4622 | } |
4623 | /* This cast should be unnecessary, but some Mips compiler complains | |
4624 | (MIPS-ABI + SysVR4, DC/OSx, etc). */ | |
c70bbf06 | 4625 | objptr = (Lisp_Object *) &ptr->contents[COMPILED_CONSTANTS]; |
169ee243 RS |
4626 | goto loop; |
4627 | } | |
169ee243 RS |
4628 | else if (GC_FRAMEP (obj)) |
4629 | { | |
c70bbf06 | 4630 | register struct frame *ptr = XFRAME (obj); |
169ee243 RS |
4631 | register EMACS_INT size = ptr->size; |
4632 | ||
4633 | if (size & ARRAY_MARK_FLAG) break; /* Already marked */ | |
4634 | ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */ | |
4635 | ||
4f5c1376 | 4636 | CHECK_LIVE (live_vector_p); |
169ee243 | 4637 | mark_object (&ptr->name); |
894a9d16 | 4638 | mark_object (&ptr->icon_name); |
aba6deb8 | 4639 | mark_object (&ptr->title); |
169ee243 RS |
4640 | mark_object (&ptr->focus_frame); |
4641 | mark_object (&ptr->selected_window); | |
4642 | mark_object (&ptr->minibuffer_window); | |
4643 | mark_object (&ptr->param_alist); | |
4644 | mark_object (&ptr->scroll_bars); | |
4645 | mark_object (&ptr->condemned_scroll_bars); | |
4646 | mark_object (&ptr->menu_bar_items); | |
4647 | mark_object (&ptr->face_alist); | |
4648 | mark_object (&ptr->menu_bar_vector); | |
4649 | mark_object (&ptr->buffer_predicate); | |
a0e1f185 | 4650 | mark_object (&ptr->buffer_list); |
41c28a37 | 4651 | mark_object (&ptr->menu_bar_window); |
9ea173e8 | 4652 | mark_object (&ptr->tool_bar_window); |
41c28a37 GM |
4653 | mark_face_cache (ptr->face_cache); |
4654 | #ifdef HAVE_WINDOW_SYSTEM | |
4655 | mark_image_cache (ptr); | |
e2c556b4 | 4656 | mark_object (&ptr->tool_bar_items); |
9ea173e8 GM |
4657 | mark_object (&ptr->desired_tool_bar_string); |
4658 | mark_object (&ptr->current_tool_bar_string); | |
41c28a37 | 4659 | #endif /* HAVE_WINDOW_SYSTEM */ |
169ee243 | 4660 | } |
7b07587b | 4661 | else if (GC_BOOL_VECTOR_P (obj)) |
707788bd RS |
4662 | { |
4663 | register struct Lisp_Vector *ptr = XVECTOR (obj); | |
4664 | ||
4665 | if (ptr->size & ARRAY_MARK_FLAG) | |
4666 | break; /* Already marked */ | |
4f5c1376 | 4667 | CHECK_LIVE (live_vector_p); |
707788bd RS |
4668 | ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */ |
4669 | } | |
41c28a37 GM |
4670 | else if (GC_WINDOWP (obj)) |
4671 | { | |
4672 | register struct Lisp_Vector *ptr = XVECTOR (obj); | |
4673 | struct window *w = XWINDOW (obj); | |
4674 | register EMACS_INT size = ptr->size; | |
41c28a37 GM |
4675 | register int i; |
4676 | ||
4677 | /* Stop if already marked. */ | |
4678 | if (size & ARRAY_MARK_FLAG) | |
4679 | break; | |
4680 | ||
4681 | /* Mark it. */ | |
4f5c1376 | 4682 | CHECK_LIVE (live_vector_p); |
41c28a37 GM |
4683 | ptr->size |= ARRAY_MARK_FLAG; |
4684 | ||
4685 | /* There is no Lisp data above The member CURRENT_MATRIX in | |
4686 | struct WINDOW. Stop marking when that slot is reached. */ | |
4687 | for (i = 0; | |
c70bbf06 | 4688 | (char *) &ptr->contents[i] < (char *) &w->current_matrix; |
41c28a37 | 4689 | i++) |
c70bbf06 | 4690 | mark_object (&ptr->contents[i]); |
41c28a37 GM |
4691 | |
4692 | /* Mark glyphs for leaf windows. Marking window matrices is | |
4693 | sufficient because frame matrices use the same glyph | |
4694 | memory. */ | |
4695 | if (NILP (w->hchild) | |
4696 | && NILP (w->vchild) | |
4697 | && w->current_matrix) | |
4698 | { | |
4699 | mark_glyph_matrix (w->current_matrix); | |
4700 | mark_glyph_matrix (w->desired_matrix); | |
4701 | } | |
4702 | } | |
4703 | else if (GC_HASH_TABLE_P (obj)) | |
4704 | { | |
4705 | struct Lisp_Hash_Table *h = XHASH_TABLE (obj); | |
4706 | EMACS_INT size = h->size; | |
177c0ea7 | 4707 | |
41c28a37 GM |
4708 | /* Stop if already marked. */ |
4709 | if (size & ARRAY_MARK_FLAG) | |
4710 | break; | |
177c0ea7 | 4711 | |
41c28a37 | 4712 | /* Mark it. */ |
4f5c1376 | 4713 | CHECK_LIVE (live_vector_p); |
41c28a37 GM |
4714 | h->size |= ARRAY_MARK_FLAG; |
4715 | ||
4716 | /* Mark contents. */ | |
94a877ef | 4717 | /* Do not mark next_free or next_weak. |
177c0ea7 | 4718 | Being in the next_weak chain |
94a877ef RS |
4719 | should not keep the hash table alive. |
4720 | No need to mark `count' since it is an integer. */ | |
41c28a37 GM |
4721 | mark_object (&h->test); |
4722 | mark_object (&h->weak); | |
4723 | mark_object (&h->rehash_size); | |
4724 | mark_object (&h->rehash_threshold); | |
4725 | mark_object (&h->hash); | |
4726 | mark_object (&h->next); | |
4727 | mark_object (&h->index); | |
4728 | mark_object (&h->user_hash_function); | |
4729 | mark_object (&h->user_cmp_function); | |
4730 | ||
4731 | /* If hash table is not weak, mark all keys and values. | |
4732 | For weak tables, mark only the vector. */ | |
4733 | if (GC_NILP (h->weak)) | |
4734 | mark_object (&h->key_and_value); | |
4735 | else | |
4736 | XVECTOR (h->key_and_value)->size |= ARRAY_MARK_FLAG; | |
177c0ea7 | 4737 | |
41c28a37 | 4738 | } |
04ff9756 | 4739 | else |
169ee243 RS |
4740 | { |
4741 | register struct Lisp_Vector *ptr = XVECTOR (obj); | |
4742 | register EMACS_INT size = ptr->size; | |
169ee243 RS |
4743 | register int i; |
4744 | ||
4745 | if (size & ARRAY_MARK_FLAG) break; /* Already marked */ | |
4f5c1376 | 4746 | CHECK_LIVE (live_vector_p); |
169ee243 RS |
4747 | ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */ |
4748 | if (size & PSEUDOVECTOR_FLAG) | |
4749 | size &= PSEUDOVECTOR_SIZE_MASK; | |
41c28a37 | 4750 | |
169ee243 | 4751 | for (i = 0; i < size; i++) /* and then mark its elements */ |
c70bbf06 | 4752 | mark_object (&ptr->contents[i]); |
169ee243 RS |
4753 | } |
4754 | break; | |
7146af97 | 4755 | |
7146af97 JB |
4756 | case Lisp_Symbol: |
4757 | { | |
c70bbf06 | 4758 | register struct Lisp_Symbol *ptr = XSYMBOL (obj); |
7146af97 JB |
4759 | struct Lisp_Symbol *ptrx; |
4760 | ||
4761 | if (XMARKBIT (ptr->plist)) break; | |
4f5c1376 | 4762 | CHECK_ALLOCATED_AND_LIVE (live_symbol_p); |
7146af97 | 4763 | XMARK (ptr->plist); |
7146af97 JB |
4764 | mark_object ((Lisp_Object *) &ptr->value); |
4765 | mark_object (&ptr->function); | |
4766 | mark_object (&ptr->plist); | |
34400008 | 4767 | |
8fe5665d KR |
4768 | if (!PURE_POINTER_P (XSTRING (ptr->xname))) |
4769 | MARK_STRING (XSTRING (ptr->xname)); | |
d5db4077 | 4770 | MARK_INTERVAL_TREE (STRING_INTERVALS (ptr->xname)); |
177c0ea7 | 4771 | |
1c6bb482 RS |
4772 | /* Note that we do not mark the obarray of the symbol. |
4773 | It is safe not to do so because nothing accesses that | |
4774 | slot except to check whether it is nil. */ | |
7146af97 JB |
4775 | ptr = ptr->next; |
4776 | if (ptr) | |
4777 | { | |
9149e743 KH |
4778 | /* For the benefit of the last_marked log. */ |
4779 | objptr = (Lisp_Object *)&XSYMBOL (obj)->next; | |
b0846f52 | 4780 | ptrx = ptr; /* Use of ptrx avoids compiler bug on Sun */ |
7146af97 | 4781 | XSETSYMBOL (obj, ptrx); |
9149e743 KH |
4782 | /* We can't goto loop here because *objptr doesn't contain an |
4783 | actual Lisp_Object with valid datatype field. */ | |
4784 | goto loop2; | |
7146af97 JB |
4785 | } |
4786 | } | |
4787 | break; | |
4788 | ||
a0a38eb7 | 4789 | case Lisp_Misc: |
4f5c1376 | 4790 | CHECK_ALLOCATED_AND_LIVE (live_misc_p); |
a5da44fe | 4791 | switch (XMISCTYPE (obj)) |
a0a38eb7 KH |
4792 | { |
4793 | case Lisp_Misc_Marker: | |
4794 | XMARK (XMARKER (obj)->chain); | |
4795 | /* DO NOT mark thru the marker's chain. | |
4796 | The buffer's markers chain does not preserve markers from gc; | |
4797 | instead, markers are removed from the chain when freed by gc. */ | |
4798 | break; | |
4799 | ||
465edf35 KH |
4800 | case Lisp_Misc_Buffer_Local_Value: |
4801 | case Lisp_Misc_Some_Buffer_Local_Value: | |
4802 | { | |
4803 | register struct Lisp_Buffer_Local_Value *ptr | |
4804 | = XBUFFER_LOCAL_VALUE (obj); | |
a9faeabe RS |
4805 | if (XMARKBIT (ptr->realvalue)) break; |
4806 | XMARK (ptr->realvalue); | |
465edf35 KH |
4807 | /* If the cdr is nil, avoid recursion for the car. */ |
4808 | if (EQ (ptr->cdr, Qnil)) | |
4809 | { | |
a9faeabe | 4810 | objptr = &ptr->realvalue; |
465edf35 KH |
4811 | goto loop; |
4812 | } | |
a9faeabe RS |
4813 | mark_object (&ptr->realvalue); |
4814 | mark_object (&ptr->buffer); | |
4815 | mark_object (&ptr->frame); | |
c70bbf06 | 4816 | objptr = &ptr->cdr; |
465edf35 KH |
4817 | goto loop; |
4818 | } | |
4819 | ||
c8616056 KH |
4820 | case Lisp_Misc_Intfwd: |
4821 | case Lisp_Misc_Boolfwd: | |
4822 | case Lisp_Misc_Objfwd: | |
4823 | case Lisp_Misc_Buffer_Objfwd: | |
b875d3f7 | 4824 | case Lisp_Misc_Kboard_Objfwd: |
c8616056 KH |
4825 | /* Don't bother with Lisp_Buffer_Objfwd, |
4826 | since all markable slots in current buffer marked anyway. */ | |
4827 | /* Don't need to do Lisp_Objfwd, since the places they point | |
4828 | are protected with staticpro. */ | |
4829 | break; | |
4830 | ||
e202fa34 KH |
4831 | case Lisp_Misc_Overlay: |
4832 | { | |
4833 | struct Lisp_Overlay *ptr = XOVERLAY (obj); | |
4834 | if (!XMARKBIT (ptr->plist)) | |
4835 | { | |
4836 | XMARK (ptr->plist); | |
4837 | mark_object (&ptr->start); | |
4838 | mark_object (&ptr->end); | |
4839 | objptr = &ptr->plist; | |
4840 | goto loop; | |
4841 | } | |
4842 | } | |
4843 | break; | |
4844 | ||
a0a38eb7 KH |
4845 | default: |
4846 | abort (); | |
4847 | } | |
7146af97 JB |
4848 | break; |
4849 | ||
4850 | case Lisp_Cons: | |
7146af97 JB |
4851 | { |
4852 | register struct Lisp_Cons *ptr = XCONS (obj); | |
4853 | if (XMARKBIT (ptr->car)) break; | |
4f5c1376 | 4854 | CHECK_ALLOCATED_AND_LIVE (live_cons_p); |
7146af97 | 4855 | XMARK (ptr->car); |
c54ca951 RS |
4856 | /* If the cdr is nil, avoid recursion for the car. */ |
4857 | if (EQ (ptr->cdr, Qnil)) | |
4858 | { | |
4859 | objptr = &ptr->car; | |
1342fc6f | 4860 | cdr_count = 0; |
c54ca951 RS |
4861 | goto loop; |
4862 | } | |
7146af97 | 4863 | mark_object (&ptr->car); |
c70bbf06 | 4864 | objptr = &ptr->cdr; |
1342fc6f RS |
4865 | cdr_count++; |
4866 | if (cdr_count == mark_object_loop_halt) | |
4867 | abort (); | |
7146af97 JB |
4868 | goto loop; |
4869 | } | |
4870 | ||
7146af97 | 4871 | case Lisp_Float: |
4f5c1376 | 4872 | CHECK_ALLOCATED_AND_LIVE (live_float_p); |
7146af97 JB |
4873 | XMARK (XFLOAT (obj)->type); |
4874 | break; | |
7146af97 | 4875 | |
7146af97 | 4876 | case Lisp_Int: |
7146af97 JB |
4877 | break; |
4878 | ||
4879 | default: | |
4880 | abort (); | |
4881 | } | |
4f5c1376 GM |
4882 | |
4883 | #undef CHECK_LIVE | |
4884 | #undef CHECK_ALLOCATED | |
4885 | #undef CHECK_ALLOCATED_AND_LIVE | |
7146af97 JB |
4886 | } |
4887 | ||
4888 | /* Mark the pointers in a buffer structure. */ | |
4889 | ||
4890 | static void | |
4891 | mark_buffer (buf) | |
4892 | Lisp_Object buf; | |
4893 | { | |
7146af97 JB |
4894 | register struct buffer *buffer = XBUFFER (buf); |
4895 | register Lisp_Object *ptr; | |
30e3190a | 4896 | Lisp_Object base_buffer; |
7146af97 JB |
4897 | |
4898 | /* This is the buffer's markbit */ | |
4899 | mark_object (&buffer->name); | |
4900 | XMARK (buffer->name); | |
4901 | ||
30e3190a | 4902 | MARK_INTERVAL_TREE (BUF_INTERVALS (buffer)); |
d5e35230 | 4903 | |
4c315bda RS |
4904 | if (CONSP (buffer->undo_list)) |
4905 | { | |
4906 | Lisp_Object tail; | |
4907 | tail = buffer->undo_list; | |
4908 | ||
4909 | while (CONSP (tail)) | |
4910 | { | |
4911 | register struct Lisp_Cons *ptr = XCONS (tail); | |
4912 | ||
4913 | if (XMARKBIT (ptr->car)) | |
4914 | break; | |
4915 | XMARK (ptr->car); | |
4916 | if (GC_CONSP (ptr->car) | |
70949dac KR |
4917 | && ! XMARKBIT (XCAR (ptr->car)) |
4918 | && GC_MARKERP (XCAR (ptr->car))) | |
4c315bda | 4919 | { |
f3fbd155 KR |
4920 | XMARK (XCAR_AS_LVALUE (ptr->car)); |
4921 | mark_object (&XCDR_AS_LVALUE (ptr->car)); | |
4c315bda RS |
4922 | } |
4923 | else | |
4924 | mark_object (&ptr->car); | |
4925 | ||
4926 | if (CONSP (ptr->cdr)) | |
4927 | tail = ptr->cdr; | |
4928 | else | |
4929 | break; | |
4930 | } | |
4931 | ||
f3fbd155 | 4932 | mark_object (&XCDR_AS_LVALUE (tail)); |
4c315bda RS |
4933 | } |
4934 | else | |
4935 | mark_object (&buffer->undo_list); | |
4936 | ||
7146af97 JB |
4937 | for (ptr = &buffer->name + 1; |
4938 | (char *)ptr < (char *)buffer + sizeof (struct buffer); | |
4939 | ptr++) | |
4940 | mark_object (ptr); | |
30e3190a RS |
4941 | |
4942 | /* If this is an indirect buffer, mark its base buffer. */ | |
6b552283 | 4943 | if (buffer->base_buffer && !XMARKBIT (buffer->base_buffer->name)) |
30e3190a | 4944 | { |
177c0ea7 | 4945 | XSETBUFFER (base_buffer, buffer->base_buffer); |
30e3190a RS |
4946 | mark_buffer (base_buffer); |
4947 | } | |
7146af97 | 4948 | } |
084b1a0c KH |
4949 | |
4950 | ||
b875d3f7 | 4951 | /* Mark the pointers in the kboard objects. */ |
084b1a0c KH |
4952 | |
4953 | static void | |
b875d3f7 | 4954 | mark_kboards () |
084b1a0c | 4955 | { |
b875d3f7 | 4956 | KBOARD *kb; |
b94daf1e | 4957 | Lisp_Object *p; |
b875d3f7 | 4958 | for (kb = all_kboards; kb; kb = kb->next_kboard) |
084b1a0c | 4959 | { |
b94daf1e KH |
4960 | if (kb->kbd_macro_buffer) |
4961 | for (p = kb->kbd_macro_buffer; p < kb->kbd_macro_ptr; p++) | |
4962 | mark_object (p); | |
4bfd0c4f RS |
4963 | mark_object (&kb->Voverriding_terminal_local_map); |
4964 | mark_object (&kb->Vlast_command); | |
4965 | mark_object (&kb->Vreal_last_command); | |
9671abc2 | 4966 | mark_object (&kb->Vprefix_arg); |
23c73c16 | 4967 | mark_object (&kb->Vlast_prefix_arg); |
b875d3f7 | 4968 | mark_object (&kb->kbd_queue); |
4bfd0c4f | 4969 | mark_object (&kb->defining_kbd_macro); |
b875d3f7 | 4970 | mark_object (&kb->Vlast_kbd_macro); |
b94daf1e | 4971 | mark_object (&kb->Vsystem_key_alist); |
6d03a6fd | 4972 | mark_object (&kb->system_key_syms); |
4bfd0c4f | 4973 | mark_object (&kb->Vdefault_minibuffer_frame); |
e60b0c44 | 4974 | mark_object (&kb->echo_string); |
084b1a0c KH |
4975 | } |
4976 | } | |
41c28a37 GM |
4977 | |
4978 | ||
4979 | /* Value is non-zero if OBJ will survive the current GC because it's | |
4980 | either marked or does not need to be marked to survive. */ | |
4981 | ||
4982 | int | |
4983 | survives_gc_p (obj) | |
4984 | Lisp_Object obj; | |
4985 | { | |
4986 | int survives_p; | |
177c0ea7 | 4987 | |
41c28a37 GM |
4988 | switch (XGCTYPE (obj)) |
4989 | { | |
4990 | case Lisp_Int: | |
4991 | survives_p = 1; | |
4992 | break; | |
4993 | ||
4994 | case Lisp_Symbol: | |
4995 | survives_p = XMARKBIT (XSYMBOL (obj)->plist); | |
4996 | break; | |
4997 | ||
4998 | case Lisp_Misc: | |
4999 | switch (XMISCTYPE (obj)) | |
5000 | { | |
5001 | case Lisp_Misc_Marker: | |
5002 | survives_p = XMARKBIT (obj); | |
5003 | break; | |
177c0ea7 | 5004 | |
41c28a37 GM |
5005 | case Lisp_Misc_Buffer_Local_Value: |
5006 | case Lisp_Misc_Some_Buffer_Local_Value: | |
5007 | survives_p = XMARKBIT (XBUFFER_LOCAL_VALUE (obj)->realvalue); | |
5008 | break; | |
177c0ea7 | 5009 | |
41c28a37 GM |
5010 | case Lisp_Misc_Intfwd: |
5011 | case Lisp_Misc_Boolfwd: | |
5012 | case Lisp_Misc_Objfwd: | |
5013 | case Lisp_Misc_Buffer_Objfwd: | |
5014 | case Lisp_Misc_Kboard_Objfwd: | |
5015 | survives_p = 1; | |
5016 | break; | |
177c0ea7 | 5017 | |
41c28a37 GM |
5018 | case Lisp_Misc_Overlay: |
5019 | survives_p = XMARKBIT (XOVERLAY (obj)->plist); | |
5020 | break; | |
5021 | ||
5022 | default: | |
5023 | abort (); | |
5024 | } | |
5025 | break; | |
5026 | ||
5027 | case Lisp_String: | |
5028 | { | |
5029 | struct Lisp_String *s = XSTRING (obj); | |
2e471eb5 | 5030 | survives_p = STRING_MARKED_P (s); |
41c28a37 GM |
5031 | } |
5032 | break; | |
5033 | ||
5034 | case Lisp_Vectorlike: | |
5035 | if (GC_BUFFERP (obj)) | |
5036 | survives_p = XMARKBIT (XBUFFER (obj)->name); | |
5037 | else if (GC_SUBRP (obj)) | |
5038 | survives_p = 1; | |
5039 | else | |
5040 | survives_p = XVECTOR (obj)->size & ARRAY_MARK_FLAG; | |
5041 | break; | |
5042 | ||
5043 | case Lisp_Cons: | |
5044 | survives_p = XMARKBIT (XCAR (obj)); | |
5045 | break; | |
5046 | ||
41c28a37 GM |
5047 | case Lisp_Float: |
5048 | survives_p = XMARKBIT (XFLOAT (obj)->type); | |
5049 | break; | |
41c28a37 GM |
5050 | |
5051 | default: | |
5052 | abort (); | |
5053 | } | |
5054 | ||
34400008 | 5055 | return survives_p || PURE_POINTER_P ((void *) XPNTR (obj)); |
41c28a37 GM |
5056 | } |
5057 | ||
5058 | ||
7146af97 | 5059 | \f |
1a4f1e2c | 5060 | /* Sweep: find all structures not marked, and free them. */ |
7146af97 JB |
5061 | |
5062 | static void | |
5063 | gc_sweep () | |
5064 | { | |
41c28a37 GM |
5065 | /* Remove or mark entries in weak hash tables. |
5066 | This must be done before any object is unmarked. */ | |
5067 | sweep_weak_hash_tables (); | |
5068 | ||
2e471eb5 | 5069 | sweep_strings (); |
676a7251 GM |
5070 | #ifdef GC_CHECK_STRING_BYTES |
5071 | if (!noninteractive) | |
5072 | check_string_bytes (1); | |
5073 | #endif | |
7146af97 JB |
5074 | |
5075 | /* Put all unmarked conses on free list */ | |
5076 | { | |
5077 | register struct cons_block *cblk; | |
6ca94ac9 | 5078 | struct cons_block **cprev = &cons_block; |
7146af97 JB |
5079 | register int lim = cons_block_index; |
5080 | register int num_free = 0, num_used = 0; | |
5081 | ||
5082 | cons_free_list = 0; | |
177c0ea7 | 5083 | |
6ca94ac9 | 5084 | for (cblk = cons_block; cblk; cblk = *cprev) |
7146af97 JB |
5085 | { |
5086 | register int i; | |
6ca94ac9 | 5087 | int this_free = 0; |
7146af97 JB |
5088 | for (i = 0; i < lim; i++) |
5089 | if (!XMARKBIT (cblk->conses[i].car)) | |
5090 | { | |
6ca94ac9 | 5091 | this_free++; |
1cd5fe6a | 5092 | *(struct Lisp_Cons **)&cblk->conses[i].cdr = cons_free_list; |
7146af97 | 5093 | cons_free_list = &cblk->conses[i]; |
34400008 GM |
5094 | #if GC_MARK_STACK |
5095 | cons_free_list->car = Vdead; | |
5096 | #endif | |
7146af97 JB |
5097 | } |
5098 | else | |
5099 | { | |
5100 | num_used++; | |
5101 | XUNMARK (cblk->conses[i].car); | |
5102 | } | |
5103 | lim = CONS_BLOCK_SIZE; | |
6ca94ac9 KH |
5104 | /* If this block contains only free conses and we have already |
5105 | seen more than two blocks worth of free conses then deallocate | |
5106 | this block. */ | |
6feef451 | 5107 | if (this_free == CONS_BLOCK_SIZE && num_free > CONS_BLOCK_SIZE) |
6ca94ac9 | 5108 | { |
6ca94ac9 KH |
5109 | *cprev = cblk->next; |
5110 | /* Unhook from the free list. */ | |
5111 | cons_free_list = *(struct Lisp_Cons **) &cblk->conses[0].cdr; | |
c8099634 RS |
5112 | lisp_free (cblk); |
5113 | n_cons_blocks--; | |
6ca94ac9 KH |
5114 | } |
5115 | else | |
6feef451 AS |
5116 | { |
5117 | num_free += this_free; | |
5118 | cprev = &cblk->next; | |
5119 | } | |
7146af97 JB |
5120 | } |
5121 | total_conses = num_used; | |
5122 | total_free_conses = num_free; | |
5123 | } | |
5124 | ||
7146af97 JB |
5125 | /* Put all unmarked floats on free list */ |
5126 | { | |
5127 | register struct float_block *fblk; | |
6ca94ac9 | 5128 | struct float_block **fprev = &float_block; |
7146af97 JB |
5129 | register int lim = float_block_index; |
5130 | register int num_free = 0, num_used = 0; | |
5131 | ||
5132 | float_free_list = 0; | |
177c0ea7 | 5133 | |
6ca94ac9 | 5134 | for (fblk = float_block; fblk; fblk = *fprev) |
7146af97 JB |
5135 | { |
5136 | register int i; | |
6ca94ac9 | 5137 | int this_free = 0; |
7146af97 JB |
5138 | for (i = 0; i < lim; i++) |
5139 | if (!XMARKBIT (fblk->floats[i].type)) | |
5140 | { | |
6ca94ac9 | 5141 | this_free++; |
1cd5fe6a | 5142 | *(struct Lisp_Float **)&fblk->floats[i].data = float_free_list; |
7146af97 | 5143 | float_free_list = &fblk->floats[i]; |
34400008 GM |
5144 | #if GC_MARK_STACK |
5145 | float_free_list->type = Vdead; | |
5146 | #endif | |
7146af97 JB |
5147 | } |
5148 | else | |
5149 | { | |
5150 | num_used++; | |
5151 | XUNMARK (fblk->floats[i].type); | |
5152 | } | |
5153 | lim = FLOAT_BLOCK_SIZE; | |
6ca94ac9 KH |
5154 | /* If this block contains only free floats and we have already |
5155 | seen more than two blocks worth of free floats then deallocate | |
5156 | this block. */ | |
6feef451 | 5157 | if (this_free == FLOAT_BLOCK_SIZE && num_free > FLOAT_BLOCK_SIZE) |
6ca94ac9 | 5158 | { |
6ca94ac9 KH |
5159 | *fprev = fblk->next; |
5160 | /* Unhook from the free list. */ | |
5161 | float_free_list = *(struct Lisp_Float **) &fblk->floats[0].data; | |
c8099634 RS |
5162 | lisp_free (fblk); |
5163 | n_float_blocks--; | |
6ca94ac9 KH |
5164 | } |
5165 | else | |
6feef451 AS |
5166 | { |
5167 | num_free += this_free; | |
5168 | fprev = &fblk->next; | |
5169 | } | |
7146af97 JB |
5170 | } |
5171 | total_floats = num_used; | |
5172 | total_free_floats = num_free; | |
5173 | } | |
7146af97 | 5174 | |
d5e35230 JA |
5175 | /* Put all unmarked intervals on free list */ |
5176 | { | |
5177 | register struct interval_block *iblk; | |
6ca94ac9 | 5178 | struct interval_block **iprev = &interval_block; |
d5e35230 JA |
5179 | register int lim = interval_block_index; |
5180 | register int num_free = 0, num_used = 0; | |
5181 | ||
5182 | interval_free_list = 0; | |
5183 | ||
6ca94ac9 | 5184 | for (iblk = interval_block; iblk; iblk = *iprev) |
d5e35230 JA |
5185 | { |
5186 | register int i; | |
6ca94ac9 | 5187 | int this_free = 0; |
d5e35230 JA |
5188 | |
5189 | for (i = 0; i < lim; i++) | |
5190 | { | |
5191 | if (! XMARKBIT (iblk->intervals[i].plist)) | |
5192 | { | |
439d5cb4 | 5193 | SET_INTERVAL_PARENT (&iblk->intervals[i], interval_free_list); |
d5e35230 | 5194 | interval_free_list = &iblk->intervals[i]; |
6ca94ac9 | 5195 | this_free++; |
d5e35230 JA |
5196 | } |
5197 | else | |
5198 | { | |
5199 | num_used++; | |
5200 | XUNMARK (iblk->intervals[i].plist); | |
5201 | } | |
5202 | } | |
5203 | lim = INTERVAL_BLOCK_SIZE; | |
6ca94ac9 KH |
5204 | /* If this block contains only free intervals and we have already |
5205 | seen more than two blocks worth of free intervals then | |
5206 | deallocate this block. */ | |
6feef451 | 5207 | if (this_free == INTERVAL_BLOCK_SIZE && num_free > INTERVAL_BLOCK_SIZE) |
6ca94ac9 | 5208 | { |
6ca94ac9 KH |
5209 | *iprev = iblk->next; |
5210 | /* Unhook from the free list. */ | |
439d5cb4 | 5211 | interval_free_list = INTERVAL_PARENT (&iblk->intervals[0]); |
c8099634 RS |
5212 | lisp_free (iblk); |
5213 | n_interval_blocks--; | |
6ca94ac9 KH |
5214 | } |
5215 | else | |
6feef451 AS |
5216 | { |
5217 | num_free += this_free; | |
5218 | iprev = &iblk->next; | |
5219 | } | |
d5e35230 JA |
5220 | } |
5221 | total_intervals = num_used; | |
5222 | total_free_intervals = num_free; | |
5223 | } | |
d5e35230 | 5224 | |
7146af97 JB |
5225 | /* Put all unmarked symbols on free list */ |
5226 | { | |
5227 | register struct symbol_block *sblk; | |
6ca94ac9 | 5228 | struct symbol_block **sprev = &symbol_block; |
7146af97 JB |
5229 | register int lim = symbol_block_index; |
5230 | register int num_free = 0, num_used = 0; | |
5231 | ||
d285b373 | 5232 | symbol_free_list = NULL; |
177c0ea7 | 5233 | |
6ca94ac9 | 5234 | for (sblk = symbol_block; sblk; sblk = *sprev) |
7146af97 | 5235 | { |
6ca94ac9 | 5236 | int this_free = 0; |
d285b373 GM |
5237 | struct Lisp_Symbol *sym = sblk->symbols; |
5238 | struct Lisp_Symbol *end = sym + lim; | |
5239 | ||
5240 | for (; sym < end; ++sym) | |
5241 | { | |
20035321 SM |
5242 | /* Check if the symbol was created during loadup. In such a case |
5243 | it might be pointed to by pure bytecode which we don't trace, | |
5244 | so we conservatively assume that it is live. */ | |
8fe5665d | 5245 | int pure_p = PURE_POINTER_P (XSTRING (sym->xname)); |
177c0ea7 | 5246 | |
d285b373 GM |
5247 | if (!XMARKBIT (sym->plist) && !pure_p) |
5248 | { | |
5249 | *(struct Lisp_Symbol **) &sym->value = symbol_free_list; | |
5250 | symbol_free_list = sym; | |
34400008 | 5251 | #if GC_MARK_STACK |
d285b373 | 5252 | symbol_free_list->function = Vdead; |
34400008 | 5253 | #endif |
d285b373 GM |
5254 | ++this_free; |
5255 | } | |
5256 | else | |
5257 | { | |
5258 | ++num_used; | |
5259 | if (!pure_p) | |
8fe5665d | 5260 | UNMARK_STRING (XSTRING (sym->xname)); |
d285b373 GM |
5261 | XUNMARK (sym->plist); |
5262 | } | |
5263 | } | |
177c0ea7 | 5264 | |
7146af97 | 5265 | lim = SYMBOL_BLOCK_SIZE; |
6ca94ac9 KH |
5266 | /* If this block contains only free symbols and we have already |
5267 | seen more than two blocks worth of free symbols then deallocate | |
5268 | this block. */ | |
6feef451 | 5269 | if (this_free == SYMBOL_BLOCK_SIZE && num_free > SYMBOL_BLOCK_SIZE) |
6ca94ac9 | 5270 | { |
6ca94ac9 KH |
5271 | *sprev = sblk->next; |
5272 | /* Unhook from the free list. */ | |
5273 | symbol_free_list = *(struct Lisp_Symbol **)&sblk->symbols[0].value; | |
c8099634 RS |
5274 | lisp_free (sblk); |
5275 | n_symbol_blocks--; | |
6ca94ac9 KH |
5276 | } |
5277 | else | |
6feef451 AS |
5278 | { |
5279 | num_free += this_free; | |
5280 | sprev = &sblk->next; | |
5281 | } | |
7146af97 JB |
5282 | } |
5283 | total_symbols = num_used; | |
5284 | total_free_symbols = num_free; | |
5285 | } | |
5286 | ||
a9faeabe RS |
5287 | /* Put all unmarked misc's on free list. |
5288 | For a marker, first unchain it from the buffer it points into. */ | |
7146af97 JB |
5289 | { |
5290 | register struct marker_block *mblk; | |
6ca94ac9 | 5291 | struct marker_block **mprev = &marker_block; |
7146af97 JB |
5292 | register int lim = marker_block_index; |
5293 | register int num_free = 0, num_used = 0; | |
5294 | ||
5295 | marker_free_list = 0; | |
177c0ea7 | 5296 | |
6ca94ac9 | 5297 | for (mblk = marker_block; mblk; mblk = *mprev) |
7146af97 JB |
5298 | { |
5299 | register int i; | |
6ca94ac9 | 5300 | int this_free = 0; |
26b926e1 | 5301 | EMACS_INT already_free = -1; |
fa05e253 | 5302 | |
7146af97 | 5303 | for (i = 0; i < lim; i++) |
465edf35 KH |
5304 | { |
5305 | Lisp_Object *markword; | |
a5da44fe | 5306 | switch (mblk->markers[i].u_marker.type) |
465edf35 KH |
5307 | { |
5308 | case Lisp_Misc_Marker: | |
5309 | markword = &mblk->markers[i].u_marker.chain; | |
5310 | break; | |
5311 | case Lisp_Misc_Buffer_Local_Value: | |
5312 | case Lisp_Misc_Some_Buffer_Local_Value: | |
a9faeabe | 5313 | markword = &mblk->markers[i].u_buffer_local_value.realvalue; |
465edf35 | 5314 | break; |
e202fa34 KH |
5315 | case Lisp_Misc_Overlay: |
5316 | markword = &mblk->markers[i].u_overlay.plist; | |
5317 | break; | |
fa05e253 RS |
5318 | case Lisp_Misc_Free: |
5319 | /* If the object was already free, keep it | |
5320 | on the free list. */ | |
74d84334 | 5321 | markword = (Lisp_Object *) &already_free; |
fa05e253 | 5322 | break; |
465edf35 KH |
5323 | default: |
5324 | markword = 0; | |
e202fa34 | 5325 | break; |
465edf35 KH |
5326 | } |
5327 | if (markword && !XMARKBIT (*markword)) | |
5328 | { | |
5329 | Lisp_Object tem; | |
a5da44fe | 5330 | if (mblk->markers[i].u_marker.type == Lisp_Misc_Marker) |
465edf35 KH |
5331 | { |
5332 | /* tem1 avoids Sun compiler bug */ | |
5333 | struct Lisp_Marker *tem1 = &mblk->markers[i].u_marker; | |
5334 | XSETMARKER (tem, tem1); | |
5335 | unchain_marker (tem); | |
5336 | } | |
fa05e253 RS |
5337 | /* Set the type of the freed object to Lisp_Misc_Free. |
5338 | We could leave the type alone, since nobody checks it, | |
465edf35 | 5339 | but this might catch bugs faster. */ |
a5da44fe | 5340 | mblk->markers[i].u_marker.type = Lisp_Misc_Free; |
465edf35 KH |
5341 | mblk->markers[i].u_free.chain = marker_free_list; |
5342 | marker_free_list = &mblk->markers[i]; | |
6ca94ac9 | 5343 | this_free++; |
465edf35 KH |
5344 | } |
5345 | else | |
5346 | { | |
5347 | num_used++; | |
5348 | if (markword) | |
5349 | XUNMARK (*markword); | |
5350 | } | |
5351 | } | |
7146af97 | 5352 | lim = MARKER_BLOCK_SIZE; |
6ca94ac9 KH |
5353 | /* If this block contains only free markers and we have already |
5354 | seen more than two blocks worth of free markers then deallocate | |
5355 | this block. */ | |
6feef451 | 5356 | if (this_free == MARKER_BLOCK_SIZE && num_free > MARKER_BLOCK_SIZE) |
6ca94ac9 | 5357 | { |
6ca94ac9 KH |
5358 | *mprev = mblk->next; |
5359 | /* Unhook from the free list. */ | |
5360 | marker_free_list = mblk->markers[0].u_free.chain; | |
c8099634 RS |
5361 | lisp_free (mblk); |
5362 | n_marker_blocks--; | |
6ca94ac9 KH |
5363 | } |
5364 | else | |
6feef451 AS |
5365 | { |
5366 | num_free += this_free; | |
5367 | mprev = &mblk->next; | |
5368 | } | |
7146af97 JB |
5369 | } |
5370 | ||
5371 | total_markers = num_used; | |
5372 | total_free_markers = num_free; | |
5373 | } | |
5374 | ||
5375 | /* Free all unmarked buffers */ | |
5376 | { | |
5377 | register struct buffer *buffer = all_buffers, *prev = 0, *next; | |
5378 | ||
5379 | while (buffer) | |
5380 | if (!XMARKBIT (buffer->name)) | |
5381 | { | |
5382 | if (prev) | |
5383 | prev->next = buffer->next; | |
5384 | else | |
5385 | all_buffers = buffer->next; | |
5386 | next = buffer->next; | |
34400008 | 5387 | lisp_free (buffer); |
7146af97 JB |
5388 | buffer = next; |
5389 | } | |
5390 | else | |
5391 | { | |
5392 | XUNMARK (buffer->name); | |
30e3190a | 5393 | UNMARK_BALANCE_INTERVALS (BUF_INTERVALS (buffer)); |
7146af97 JB |
5394 | prev = buffer, buffer = buffer->next; |
5395 | } | |
5396 | } | |
5397 | ||
7146af97 JB |
5398 | /* Free all unmarked vectors */ |
5399 | { | |
5400 | register struct Lisp_Vector *vector = all_vectors, *prev = 0, *next; | |
5401 | total_vector_size = 0; | |
5402 | ||
5403 | while (vector) | |
5404 | if (!(vector->size & ARRAY_MARK_FLAG)) | |
5405 | { | |
5406 | if (prev) | |
5407 | prev->next = vector->next; | |
5408 | else | |
5409 | all_vectors = vector->next; | |
5410 | next = vector->next; | |
c8099634 RS |
5411 | lisp_free (vector); |
5412 | n_vectors--; | |
7146af97 | 5413 | vector = next; |
41c28a37 | 5414 | |
7146af97 JB |
5415 | } |
5416 | else | |
5417 | { | |
5418 | vector->size &= ~ARRAY_MARK_FLAG; | |
fa05e253 RS |
5419 | if (vector->size & PSEUDOVECTOR_FLAG) |
5420 | total_vector_size += (PSEUDOVECTOR_SIZE_MASK & vector->size); | |
5421 | else | |
5422 | total_vector_size += vector->size; | |
7146af97 JB |
5423 | prev = vector, vector = vector->next; |
5424 | } | |
5425 | } | |
177c0ea7 | 5426 | |
676a7251 GM |
5427 | #ifdef GC_CHECK_STRING_BYTES |
5428 | if (!noninteractive) | |
5429 | check_string_bytes (1); | |
5430 | #endif | |
7146af97 | 5431 | } |
7146af97 | 5432 | |
7146af97 | 5433 | |
7146af97 | 5434 | |
7146af97 | 5435 | \f |
20d24714 JB |
5436 | /* Debugging aids. */ |
5437 | ||
31ce1c91 | 5438 | DEFUN ("memory-limit", Fmemory_limit, Smemory_limit, 0, 0, 0, |
a6266d23 | 5439 | doc: /* Return the address of the last byte Emacs has allocated, divided by 1024. |
228299fa | 5440 | This may be helpful in debugging Emacs's memory usage. |
7ee72033 MB |
5441 | We divide the value by 1024 to make sure it fits in a Lisp integer. */) |
5442 | () | |
20d24714 JB |
5443 | { |
5444 | Lisp_Object end; | |
5445 | ||
45d12a89 | 5446 | XSETINT (end, (EMACS_INT) sbrk (0) / 1024); |
20d24714 JB |
5447 | |
5448 | return end; | |
5449 | } | |
5450 | ||
310ea200 | 5451 | DEFUN ("memory-use-counts", Fmemory_use_counts, Smemory_use_counts, 0, 0, 0, |
a6266d23 | 5452 | doc: /* Return a list of counters that measure how much consing there has been. |
228299fa GM |
5453 | Each of these counters increments for a certain kind of object. |
5454 | The counters wrap around from the largest positive integer to zero. | |
5455 | Garbage collection does not decrease them. | |
5456 | The elements of the value are as follows: | |
5457 | (CONSES FLOATS VECTOR-CELLS SYMBOLS STRING-CHARS MISCS INTERVALS STRINGS) | |
5458 | All are in units of 1 = one object consed | |
5459 | except for VECTOR-CELLS and STRING-CHARS, which count the total length of | |
5460 | objects consed. | |
5461 | MISCS include overlays, markers, and some internal types. | |
5462 | Frames, windows, buffers, and subprocesses count as vectors | |
7ee72033 MB |
5463 | (but the contents of a buffer's text do not count here). */) |
5464 | () | |
310ea200 | 5465 | { |
2e471eb5 | 5466 | Lisp_Object consed[8]; |
310ea200 | 5467 | |
78e985eb GM |
5468 | consed[0] = make_number (min (MOST_POSITIVE_FIXNUM, cons_cells_consed)); |
5469 | consed[1] = make_number (min (MOST_POSITIVE_FIXNUM, floats_consed)); | |
5470 | consed[2] = make_number (min (MOST_POSITIVE_FIXNUM, vector_cells_consed)); | |
5471 | consed[3] = make_number (min (MOST_POSITIVE_FIXNUM, symbols_consed)); | |
5472 | consed[4] = make_number (min (MOST_POSITIVE_FIXNUM, string_chars_consed)); | |
5473 | consed[5] = make_number (min (MOST_POSITIVE_FIXNUM, misc_objects_consed)); | |
5474 | consed[6] = make_number (min (MOST_POSITIVE_FIXNUM, intervals_consed)); | |
5475 | consed[7] = make_number (min (MOST_POSITIVE_FIXNUM, strings_consed)); | |
310ea200 | 5476 | |
2e471eb5 | 5477 | return Flist (8, consed); |
310ea200 | 5478 | } |
e0b8c689 KR |
5479 | |
5480 | int suppress_checking; | |
5481 | void | |
5482 | die (msg, file, line) | |
5483 | const char *msg; | |
5484 | const char *file; | |
5485 | int line; | |
5486 | { | |
5487 | fprintf (stderr, "\r\nEmacs fatal error: %s:%d: %s\r\n", | |
5488 | file, line, msg); | |
5489 | abort (); | |
5490 | } | |
20d24714 | 5491 | \f |
7146af97 JB |
5492 | /* Initialization */ |
5493 | ||
dfcf069d | 5494 | void |
7146af97 JB |
5495 | init_alloc_once () |
5496 | { | |
5497 | /* Used to do Vpurify_flag = Qt here, but Qt isn't set up yet! */ | |
9e713715 GM |
5498 | purebeg = PUREBEG; |
5499 | pure_size = PURESIZE; | |
1f0b3fd2 | 5500 | pure_bytes_used = 0; |
9e713715 GM |
5501 | pure_bytes_used_before_overflow = 0; |
5502 | ||
877935b1 | 5503 | #if GC_MARK_STACK || defined GC_MALLOC_CHECK |
34400008 GM |
5504 | mem_init (); |
5505 | Vdead = make_pure_string ("DEAD", 4, 4, 0); | |
5506 | #endif | |
9e713715 | 5507 | |
7146af97 JB |
5508 | all_vectors = 0; |
5509 | ignore_warnings = 1; | |
d1658221 RS |
5510 | #ifdef DOUG_LEA_MALLOC |
5511 | mallopt (M_TRIM_THRESHOLD, 128*1024); /* trim threshold */ | |
5512 | mallopt (M_MMAP_THRESHOLD, 64*1024); /* mmap threshold */ | |
81d492d5 | 5513 | mallopt (M_MMAP_MAX, MMAP_MAX_AREAS); /* max. number of mmap'ed areas */ |
d1658221 | 5514 | #endif |
7146af97 JB |
5515 | init_strings (); |
5516 | init_cons (); | |
5517 | init_symbol (); | |
5518 | init_marker (); | |
7146af97 | 5519 | init_float (); |
34400008 | 5520 | init_intervals (); |
d5e35230 | 5521 | |
276cbe5a RS |
5522 | #ifdef REL_ALLOC |
5523 | malloc_hysteresis = 32; | |
5524 | #else | |
5525 | malloc_hysteresis = 0; | |
5526 | #endif | |
5527 | ||
5528 | spare_memory = (char *) malloc (SPARE_MEMORY); | |
5529 | ||
7146af97 JB |
5530 | ignore_warnings = 0; |
5531 | gcprolist = 0; | |
630686c8 | 5532 | byte_stack_list = 0; |
7146af97 JB |
5533 | staticidx = 0; |
5534 | consing_since_gc = 0; | |
7d179cea | 5535 | gc_cons_threshold = 100000 * sizeof (Lisp_Object); |
7146af97 JB |
5536 | #ifdef VIRT_ADDR_VARIES |
5537 | malloc_sbrk_unused = 1<<22; /* A large number */ | |
5538 | malloc_sbrk_used = 100000; /* as reasonable as any number */ | |
5539 | #endif /* VIRT_ADDR_VARIES */ | |
5540 | } | |
5541 | ||
dfcf069d | 5542 | void |
7146af97 JB |
5543 | init_alloc () |
5544 | { | |
5545 | gcprolist = 0; | |
630686c8 | 5546 | byte_stack_list = 0; |
182ff242 GM |
5547 | #if GC_MARK_STACK |
5548 | #if !defined GC_SAVE_REGISTERS_ON_STACK && !defined GC_SETJMP_WORKS | |
5549 | setjmp_tested_p = longjmps_done = 0; | |
5550 | #endif | |
5551 | #endif | |
2c5bd608 DL |
5552 | Vgc_elapsed = make_float (0.0); |
5553 | gcs_done = 0; | |
7146af97 JB |
5554 | } |
5555 | ||
5556 | void | |
5557 | syms_of_alloc () | |
5558 | { | |
7ee72033 | 5559 | DEFVAR_INT ("gc-cons-threshold", &gc_cons_threshold, |
a6266d23 | 5560 | doc: /* *Number of bytes of consing between garbage collections. |
228299fa GM |
5561 | Garbage collection can happen automatically once this many bytes have been |
5562 | allocated since the last garbage collection. All data types count. | |
7146af97 | 5563 | |
228299fa | 5564 | Garbage collection happens automatically only when `eval' is called. |
7146af97 | 5565 | |
228299fa GM |
5566 | By binding this temporarily to a large number, you can effectively |
5567 | prevent garbage collection during a part of the program. */); | |
0819585c | 5568 | |
7ee72033 | 5569 | DEFVAR_INT ("pure-bytes-used", &pure_bytes_used, |
a6266d23 | 5570 | doc: /* Number of bytes of sharable Lisp data allocated so far. */); |
0819585c | 5571 | |
7ee72033 | 5572 | DEFVAR_INT ("cons-cells-consed", &cons_cells_consed, |
a6266d23 | 5573 | doc: /* Number of cons cells that have been consed so far. */); |
0819585c | 5574 | |
7ee72033 | 5575 | DEFVAR_INT ("floats-consed", &floats_consed, |
a6266d23 | 5576 | doc: /* Number of floats that have been consed so far. */); |
0819585c | 5577 | |
7ee72033 | 5578 | DEFVAR_INT ("vector-cells-consed", &vector_cells_consed, |
a6266d23 | 5579 | doc: /* Number of vector cells that have been consed so far. */); |
0819585c | 5580 | |
7ee72033 | 5581 | DEFVAR_INT ("symbols-consed", &symbols_consed, |
a6266d23 | 5582 | doc: /* Number of symbols that have been consed so far. */); |
0819585c | 5583 | |
7ee72033 | 5584 | DEFVAR_INT ("string-chars-consed", &string_chars_consed, |
a6266d23 | 5585 | doc: /* Number of string characters that have been consed so far. */); |
0819585c | 5586 | |
7ee72033 | 5587 | DEFVAR_INT ("misc-objects-consed", &misc_objects_consed, |
a6266d23 | 5588 | doc: /* Number of miscellaneous objects that have been consed so far. */); |
2e471eb5 | 5589 | |
7ee72033 | 5590 | DEFVAR_INT ("intervals-consed", &intervals_consed, |
a6266d23 | 5591 | doc: /* Number of intervals that have been consed so far. */); |
7146af97 | 5592 | |
7ee72033 | 5593 | DEFVAR_INT ("strings-consed", &strings_consed, |
a6266d23 | 5594 | doc: /* Number of strings that have been consed so far. */); |
228299fa | 5595 | |
7ee72033 | 5596 | DEFVAR_LISP ("purify-flag", &Vpurify_flag, |
a6266d23 | 5597 | doc: /* Non-nil means loading Lisp code in order to dump an executable. |
228299fa GM |
5598 | This means that certain objects should be allocated in shared (pure) space. */); |
5599 | ||
7ee72033 | 5600 | DEFVAR_INT ("undo-limit", &undo_limit, |
a6266d23 | 5601 | doc: /* Keep no more undo information once it exceeds this size. |
228299fa GM |
5602 | This limit is applied when garbage collection happens. |
5603 | The size is counted as the number of bytes occupied, | |
5604 | which includes both saved text and other data. */); | |
502b9b64 | 5605 | undo_limit = 20000; |
7146af97 | 5606 | |
7ee72033 | 5607 | DEFVAR_INT ("undo-strong-limit", &undo_strong_limit, |
a6266d23 | 5608 | doc: /* Don't keep more than this much size of undo information. |
228299fa GM |
5609 | A command which pushes past this size is itself forgotten. |
5610 | This limit is applied when garbage collection happens. | |
5611 | The size is counted as the number of bytes occupied, | |
5612 | which includes both saved text and other data. */); | |
502b9b64 | 5613 | undo_strong_limit = 30000; |
7146af97 | 5614 | |
7ee72033 | 5615 | DEFVAR_BOOL ("garbage-collection-messages", &garbage_collection_messages, |
a6266d23 | 5616 | doc: /* Non-nil means display messages at start and end of garbage collection. */); |
299585ee RS |
5617 | garbage_collection_messages = 0; |
5618 | ||
7ee72033 | 5619 | DEFVAR_LISP ("post-gc-hook", &Vpost_gc_hook, |
a6266d23 | 5620 | doc: /* Hook run after garbage collection has finished. */); |
9e713715 GM |
5621 | Vpost_gc_hook = Qnil; |
5622 | Qpost_gc_hook = intern ("post-gc-hook"); | |
5623 | staticpro (&Qpost_gc_hook); | |
5624 | ||
74a54b04 RS |
5625 | DEFVAR_LISP ("memory-signal-data", &Vmemory_signal_data, |
5626 | doc: /* Precomputed `signal' argument for memory-full error. */); | |
bcb61d60 KH |
5627 | /* We build this in advance because if we wait until we need it, we might |
5628 | not be able to allocate the memory to hold it. */ | |
74a54b04 RS |
5629 | Vmemory_signal_data |
5630 | = list2 (Qerror, | |
5631 | build_string ("Memory exhausted--use M-x save-some-buffers then exit and restart Emacs")); | |
5632 | ||
5633 | DEFVAR_LISP ("memory-full", &Vmemory_full, | |
5634 | doc: /* Non-nil means we are handling a memory-full error. */); | |
5635 | Vmemory_full = Qnil; | |
bcb61d60 | 5636 | |
e8197642 RS |
5637 | staticpro (&Qgc_cons_threshold); |
5638 | Qgc_cons_threshold = intern ("gc-cons-threshold"); | |
5639 | ||
a59de17b RS |
5640 | staticpro (&Qchar_table_extra_slots); |
5641 | Qchar_table_extra_slots = intern ("char-table-extra-slots"); | |
5642 | ||
2c5bd608 DL |
5643 | DEFVAR_LISP ("gc-elapsed", &Vgc_elapsed, |
5644 | doc: /* Accumulated time elapsed in garbage collections. | |
5645 | The time is in seconds as a floating point value. | |
5646 | Programs may reset this to get statistics in a specific period. */); | |
5647 | DEFVAR_INT ("gcs-done", &gcs_done, | |
5648 | doc: /* Accumulated number of garbage collections done. | |
5649 | Programs may reset this to get statistics in a specific period. */); | |
5650 | ||
7146af97 JB |
5651 | defsubr (&Scons); |
5652 | defsubr (&Slist); | |
5653 | defsubr (&Svector); | |
5654 | defsubr (&Smake_byte_code); | |
5655 | defsubr (&Smake_list); | |
5656 | defsubr (&Smake_vector); | |
7b07587b | 5657 | defsubr (&Smake_char_table); |
7146af97 | 5658 | defsubr (&Smake_string); |
7b07587b | 5659 | defsubr (&Smake_bool_vector); |
7146af97 JB |
5660 | defsubr (&Smake_symbol); |
5661 | defsubr (&Smake_marker); | |
5662 | defsubr (&Spurecopy); | |
5663 | defsubr (&Sgarbage_collect); | |
20d24714 | 5664 | defsubr (&Smemory_limit); |
310ea200 | 5665 | defsubr (&Smemory_use_counts); |
34400008 GM |
5666 | |
5667 | #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES | |
5668 | defsubr (&Sgc_status); | |
5669 | #endif | |
7146af97 | 5670 | } |