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