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