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