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