Commit | Line | Data |
---|---|---|
7146af97 | 1 | /* Storage allocation and gc for GNU Emacs Lisp interpreter. |
188b4aea | 2 | Copyright (C) 1985, 86, 88, 93, 94, 95, 97 Free Software Foundation, Inc. |
7146af97 JB |
3 | |
4 | This file is part of GNU Emacs. | |
5 | ||
6 | GNU Emacs is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
7c299e7a | 8 | the Free Software Foundation; either version 2, or (at your option) |
7146af97 JB |
9 | any later version. |
10 | ||
11 | GNU Emacs is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GNU Emacs; see the file COPYING. If not, write to | |
3b7ad313 EN |
18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
19 | Boston, MA 02111-1307, USA. */ | |
7146af97 | 20 | |
290c8f1e | 21 | /* Note that this declares bzero on OSF/1. How dumb. */ |
cf026b25 | 22 | #include <signal.h> |
7146af97 | 23 | |
18160b98 | 24 | #include <config.h> |
7146af97 | 25 | #include "lisp.h" |
d5e35230 | 26 | #include "intervals.h" |
4c0be5f4 | 27 | #include "puresize.h" |
7146af97 JB |
28 | #ifndef standalone |
29 | #include "buffer.h" | |
30 | #include "window.h" | |
502b9b64 | 31 | #include "frame.h" |
9ac0d9e0 | 32 | #include "blockinput.h" |
077d751f | 33 | #include "keyboard.h" |
7146af97 JB |
34 | #endif |
35 | ||
e065a56e JB |
36 | #include "syssignal.h" |
37 | ||
ee1eea5c KH |
38 | extern char *sbrk (); |
39 | ||
d1658221 RS |
40 | #ifdef DOUG_LEA_MALLOC |
41 | #include <malloc.h> | |
42 | #define __malloc_size_t int | |
43 | #else | |
276cbe5a RS |
44 | /* The following come from gmalloc.c. */ |
45 | ||
46 | #if defined (__STDC__) && __STDC__ | |
47 | #include <stddef.h> | |
48 | #define __malloc_size_t size_t | |
49 | #else | |
50 | #define __malloc_size_t unsigned int | |
51 | #endif | |
52 | extern __malloc_size_t _bytes_used; | |
53 | extern int __malloc_extra_blocks; | |
d1658221 | 54 | #endif /* !defined(DOUG_LEA_MALLOC) */ |
276cbe5a | 55 | |
0f936def RS |
56 | extern Lisp_Object Vhistory_length; |
57 | ||
7146af97 | 58 | #define max(A,B) ((A) > (B) ? (A) : (B)) |
b580578b | 59 | #define min(A,B) ((A) < (B) ? (A) : (B)) |
7146af97 JB |
60 | |
61 | /* Macro to verify that storage intended for Lisp objects is not | |
62 | out of range to fit in the space for a pointer. | |
63 | ADDRESS is the start of the block, and SIZE | |
64 | is the amount of space within which objects can start. */ | |
65 | #define VALIDATE_LISP_STORAGE(address, size) \ | |
66 | do \ | |
67 | { \ | |
68 | Lisp_Object val; \ | |
45d12a89 | 69 | XSETCONS (val, (char *) address + size); \ |
7146af97 JB |
70 | if ((char *) XCONS (val) != (char *) address + size) \ |
71 | { \ | |
9ac0d9e0 | 72 | xfree (address); \ |
7146af97 JB |
73 | memory_full (); \ |
74 | } \ | |
75 | } while (0) | |
76 | ||
276cbe5a RS |
77 | /* Value of _bytes_used, when spare_memory was freed. */ |
78 | static __malloc_size_t bytes_used_when_full; | |
79 | ||
7146af97 JB |
80 | /* Number of bytes of consing done since the last gc */ |
81 | int consing_since_gc; | |
82 | ||
310ea200 RS |
83 | /* Count the amount of consing of various sorts of space. */ |
84 | int cons_cells_consed; | |
85 | int floats_consed; | |
86 | int vector_cells_consed; | |
87 | int symbols_consed; | |
88 | int string_chars_consed; | |
89 | int misc_objects_consed; | |
90 | int intervals_consed; | |
91 | ||
7146af97 | 92 | /* Number of bytes of consing since gc before another gc should be done. */ |
b580578b | 93 | int gc_cons_threshold; |
7146af97 JB |
94 | |
95 | /* Nonzero during gc */ | |
96 | int gc_in_progress; | |
97 | ||
299585ee RS |
98 | /* Nonzero means display messages at beginning and end of GC. */ |
99 | int garbage_collection_messages; | |
100 | ||
7146af97 JB |
101 | #ifndef VIRT_ADDR_VARIES |
102 | extern | |
103 | #endif /* VIRT_ADDR_VARIES */ | |
104 | int malloc_sbrk_used; | |
105 | ||
106 | #ifndef VIRT_ADDR_VARIES | |
107 | extern | |
108 | #endif /* VIRT_ADDR_VARIES */ | |
109 | int malloc_sbrk_unused; | |
110 | ||
502b9b64 JB |
111 | /* Two limits controlling how much undo information to keep. */ |
112 | int undo_limit; | |
113 | int undo_strong_limit; | |
7146af97 | 114 | |
fd27a537 RS |
115 | int total_conses, total_markers, total_symbols, total_string_size, total_vector_size; |
116 | int total_free_conses, total_free_markers, total_free_symbols; | |
117 | #ifdef LISP_FLOAT_TYPE | |
118 | int total_free_floats, total_floats; | |
119 | #endif /* LISP_FLOAT_TYPE */ | |
120 | ||
276cbe5a RS |
121 | /* Points to memory space allocated as "spare", |
122 | to be freed if we run out of memory. */ | |
123 | static char *spare_memory; | |
124 | ||
125 | /* Amount of spare memory to keep in reserve. */ | |
126 | #define SPARE_MEMORY (1 << 14) | |
127 | ||
128 | /* Number of extra blocks malloc should get when it needs more core. */ | |
129 | static int malloc_hysteresis; | |
130 | ||
3c06d205 KH |
131 | /* Nonzero when malloc is called for allocating Lisp object space. */ |
132 | int allocating_for_lisp; | |
133 | ||
7146af97 JB |
134 | /* Non-nil means defun should do purecopy on the function definition */ |
135 | Lisp_Object Vpurify_flag; | |
136 | ||
137 | #ifndef HAVE_SHM | |
42607681 | 138 | EMACS_INT pure[PURESIZE / sizeof (EMACS_INT)] = {0,}; /* Force it into data space! */ |
7146af97 JB |
139 | #define PUREBEG (char *) pure |
140 | #else | |
141 | #define pure PURE_SEG_BITS /* Use shared memory segment */ | |
142 | #define PUREBEG (char *)PURE_SEG_BITS | |
4c0be5f4 JB |
143 | |
144 | /* This variable is used only by the XPNTR macro when HAVE_SHM is | |
145 | defined. If we used the PURESIZE macro directly there, that would | |
146 | make most of emacs dependent on puresize.h, which we don't want - | |
147 | you should be able to change that without too much recompilation. | |
148 | So map_in_data initializes pure_size, and the dependencies work | |
149 | out. */ | |
42607681 | 150 | EMACS_INT pure_size; |
7146af97 JB |
151 | #endif /* not HAVE_SHM */ |
152 | ||
153 | /* Index in pure at which next pure object will be allocated. */ | |
154 | int pureptr; | |
155 | ||
156 | /* If nonzero, this is a warning delivered by malloc and not yet displayed. */ | |
157 | char *pending_malloc_warning; | |
158 | ||
bcb61d60 | 159 | /* Pre-computed signal argument for use when memory is exhausted. */ |
cf3540e4 | 160 | Lisp_Object memory_signal_data; |
bcb61d60 | 161 | |
7146af97 JB |
162 | /* Maximum amount of C stack to save when a GC happens. */ |
163 | ||
164 | #ifndef MAX_SAVE_STACK | |
165 | #define MAX_SAVE_STACK 16000 | |
166 | #endif | |
167 | ||
1fb577f7 KH |
168 | /* Define DONT_COPY_FLAG to be some bit which will always be zero in a |
169 | pointer to a Lisp_Object, when that pointer is viewed as an integer. | |
170 | (On most machines, pointers are even, so we can use the low bit. | |
8e6208c5 | 171 | Word-addressable architectures may need to override this in the m-file.) |
1fb577f7 KH |
172 | When linking references to small strings through the size field, we |
173 | use this slot to hold the bit that would otherwise be interpreted as | |
174 | the GC mark bit. */ | |
155ffe9c | 175 | #ifndef DONT_COPY_FLAG |
1fb577f7 | 176 | #define DONT_COPY_FLAG 1 |
155ffe9c RS |
177 | #endif /* no DONT_COPY_FLAG */ |
178 | ||
7146af97 JB |
179 | /* Buffer in which we save a copy of the C stack at each GC. */ |
180 | ||
181 | char *stack_copy; | |
182 | int stack_copy_size; | |
183 | ||
184 | /* Non-zero means ignore malloc warnings. Set during initialization. */ | |
185 | int ignore_warnings; | |
350273a4 | 186 | |
a59de17b | 187 | Lisp_Object Qgc_cons_threshold, Qchar_table_extra_slots; |
e8197642 | 188 | |
b875d3f7 | 189 | static void mark_object (), mark_buffer (), mark_kboards (); |
350273a4 JA |
190 | static void clear_marks (), gc_sweep (); |
191 | static void compact_strings (); | |
7da0b0d3 RS |
192 | |
193 | extern int message_enable_multibyte; | |
7146af97 | 194 | \f |
1a4f1e2c JB |
195 | /* Versions of malloc and realloc that print warnings as memory gets full. */ |
196 | ||
7146af97 JB |
197 | Lisp_Object |
198 | malloc_warning_1 (str) | |
199 | Lisp_Object str; | |
200 | { | |
201 | Fprinc (str, Vstandard_output); | |
202 | write_string ("\nKilling some buffers may delay running out of memory.\n", -1); | |
203 | write_string ("However, certainly by the time you receive the 95% warning,\n", -1); | |
204 | write_string ("you should clean up, kill this Emacs, and start a new one.", -1); | |
205 | return Qnil; | |
206 | } | |
207 | ||
208 | /* malloc calls this if it finds we are near exhausting storage */ | |
d457598b AS |
209 | |
210 | void | |
7146af97 JB |
211 | malloc_warning (str) |
212 | char *str; | |
213 | { | |
214 | pending_malloc_warning = str; | |
215 | } | |
216 | ||
d457598b | 217 | void |
7146af97 JB |
218 | display_malloc_warning () |
219 | { | |
220 | register Lisp_Object val; | |
221 | ||
222 | val = build_string (pending_malloc_warning); | |
223 | pending_malloc_warning = 0; | |
224 | internal_with_output_to_temp_buffer (" *Danger*", malloc_warning_1, val); | |
225 | } | |
226 | ||
d1658221 | 227 | #ifdef DOUG_LEA_MALLOC |
1177ecf6 | 228 | # define BYTES_USED (mallinfo ().arena) |
d1658221 | 229 | #else |
1177ecf6 | 230 | # define BYTES_USED _bytes_used |
d1658221 RS |
231 | #endif |
232 | ||
7146af97 | 233 | /* Called if malloc returns zero */ |
276cbe5a | 234 | |
d457598b | 235 | void |
7146af97 JB |
236 | memory_full () |
237 | { | |
276cbe5a | 238 | #ifndef SYSTEM_MALLOC |
d1658221 | 239 | bytes_used_when_full = BYTES_USED; |
276cbe5a RS |
240 | #endif |
241 | ||
242 | /* The first time we get here, free the spare memory. */ | |
243 | if (spare_memory) | |
244 | { | |
245 | free (spare_memory); | |
246 | spare_memory = 0; | |
247 | } | |
248 | ||
249 | /* This used to call error, but if we've run out of memory, we could get | |
250 | infinite recursion trying to build the string. */ | |
251 | while (1) | |
74d84334 | 252 | Fsignal (Qnil, memory_signal_data); |
276cbe5a RS |
253 | } |
254 | ||
255 | /* Called if we can't allocate relocatable space for a buffer. */ | |
256 | ||
257 | void | |
258 | buffer_memory_full () | |
259 | { | |
260 | /* If buffers use the relocating allocator, | |
261 | no need to free spare_memory, because we may have plenty of malloc | |
262 | space left that we could get, and if we don't, the malloc that fails | |
263 | will itself cause spare_memory to be freed. | |
264 | If buffers don't use the relocating allocator, | |
265 | treat this like any other failing malloc. */ | |
266 | ||
267 | #ifndef REL_ALLOC | |
268 | memory_full (); | |
269 | #endif | |
270 | ||
bcb61d60 KH |
271 | /* This used to call error, but if we've run out of memory, we could get |
272 | infinite recursion trying to build the string. */ | |
273 | while (1) | |
274 | Fsignal (Qerror, memory_signal_data); | |
7146af97 JB |
275 | } |
276 | ||
9ac0d9e0 | 277 | /* like malloc routines but check for no memory and block interrupt input. */ |
7146af97 JB |
278 | |
279 | long * | |
280 | xmalloc (size) | |
281 | int size; | |
282 | { | |
283 | register long *val; | |
284 | ||
9ac0d9e0 | 285 | BLOCK_INPUT; |
7146af97 | 286 | val = (long *) malloc (size); |
9ac0d9e0 | 287 | UNBLOCK_INPUT; |
7146af97 JB |
288 | |
289 | if (!val && size) memory_full (); | |
290 | return val; | |
291 | } | |
292 | ||
293 | long * | |
294 | xrealloc (block, size) | |
295 | long *block; | |
296 | int size; | |
297 | { | |
298 | register long *val; | |
299 | ||
9ac0d9e0 | 300 | BLOCK_INPUT; |
56d2031b JB |
301 | /* We must call malloc explicitly when BLOCK is 0, since some |
302 | reallocs don't do this. */ | |
303 | if (! block) | |
304 | val = (long *) malloc (size); | |
f048679d | 305 | else |
56d2031b | 306 | val = (long *) realloc (block, size); |
9ac0d9e0 | 307 | UNBLOCK_INPUT; |
7146af97 JB |
308 | |
309 | if (!val && size) memory_full (); | |
310 | return val; | |
311 | } | |
9ac0d9e0 JB |
312 | |
313 | void | |
314 | xfree (block) | |
315 | long *block; | |
316 | { | |
317 | BLOCK_INPUT; | |
318 | free (block); | |
319 | UNBLOCK_INPUT; | |
320 | } | |
321 | ||
322 | \f | |
323 | /* Arranging to disable input signals while we're in malloc. | |
324 | ||
325 | This only works with GNU malloc. To help out systems which can't | |
326 | use GNU malloc, all the calls to malloc, realloc, and free | |
327 | elsewhere in the code should be inside a BLOCK_INPUT/UNBLOCK_INPUT | |
328 | pairs; unfortunately, we have no idea what C library functions | |
329 | might call malloc, so we can't really protect them unless you're | |
330 | using GNU malloc. Fortunately, most of the major operating can use | |
331 | GNU malloc. */ | |
332 | ||
333 | #ifndef SYSTEM_MALLOC | |
b0846f52 JB |
334 | extern void * (*__malloc_hook) (); |
335 | static void * (*old_malloc_hook) (); | |
336 | extern void * (*__realloc_hook) (); | |
337 | static void * (*old_realloc_hook) (); | |
338 | extern void (*__free_hook) (); | |
339 | static void (*old_free_hook) (); | |
9ac0d9e0 | 340 | |
276cbe5a RS |
341 | /* This function is used as the hook for free to call. */ |
342 | ||
9ac0d9e0 JB |
343 | static void |
344 | emacs_blocked_free (ptr) | |
345 | void *ptr; | |
346 | { | |
347 | BLOCK_INPUT; | |
348 | __free_hook = old_free_hook; | |
349 | free (ptr); | |
276cbe5a RS |
350 | /* If we released our reserve (due to running out of memory), |
351 | and we have a fair amount free once again, | |
352 | try to set aside another reserve in case we run out once more. */ | |
353 | if (spare_memory == 0 | |
354 | /* Verify there is enough space that even with the malloc | |
355 | hysteresis this call won't run out again. | |
356 | The code here is correct as long as SPARE_MEMORY | |
357 | is substantially larger than the block size malloc uses. */ | |
358 | && (bytes_used_when_full | |
d1658221 | 359 | > BYTES_USED + max (malloc_hysteresis, 4) * SPARE_MEMORY)) |
276cbe5a RS |
360 | spare_memory = (char *) malloc (SPARE_MEMORY); |
361 | ||
b0846f52 | 362 | __free_hook = emacs_blocked_free; |
9ac0d9e0 JB |
363 | UNBLOCK_INPUT; |
364 | } | |
365 | ||
276cbe5a RS |
366 | /* If we released our reserve (due to running out of memory), |
367 | and we have a fair amount free once again, | |
368 | try to set aside another reserve in case we run out once more. | |
369 | ||
370 | This is called when a relocatable block is freed in ralloc.c. */ | |
371 | ||
372 | void | |
373 | refill_memory_reserve () | |
374 | { | |
375 | if (spare_memory == 0) | |
376 | spare_memory = (char *) malloc (SPARE_MEMORY); | |
377 | } | |
378 | ||
379 | /* This function is the malloc hook that Emacs uses. */ | |
380 | ||
9ac0d9e0 JB |
381 | static void * |
382 | emacs_blocked_malloc (size) | |
383 | unsigned size; | |
384 | { | |
385 | void *value; | |
386 | ||
387 | BLOCK_INPUT; | |
388 | __malloc_hook = old_malloc_hook; | |
1177ecf6 | 389 | #ifdef DOUG_LEA_MALLOC |
d1658221 | 390 | mallopt (M_TOP_PAD, malloc_hysteresis * 4096); |
1177ecf6 | 391 | #else |
d1658221 | 392 | __malloc_extra_blocks = malloc_hysteresis; |
1177ecf6 | 393 | #endif |
2756d8ee | 394 | value = (void *) malloc (size); |
b0846f52 | 395 | __malloc_hook = emacs_blocked_malloc; |
9ac0d9e0 JB |
396 | UNBLOCK_INPUT; |
397 | ||
398 | return value; | |
399 | } | |
400 | ||
401 | static void * | |
402 | emacs_blocked_realloc (ptr, size) | |
403 | void *ptr; | |
404 | unsigned size; | |
405 | { | |
406 | void *value; | |
407 | ||
408 | BLOCK_INPUT; | |
409 | __realloc_hook = old_realloc_hook; | |
2756d8ee | 410 | value = (void *) realloc (ptr, size); |
b0846f52 | 411 | __realloc_hook = emacs_blocked_realloc; |
9ac0d9e0 JB |
412 | UNBLOCK_INPUT; |
413 | ||
414 | return value; | |
415 | } | |
416 | ||
417 | void | |
418 | uninterrupt_malloc () | |
419 | { | |
420 | old_free_hook = __free_hook; | |
b0846f52 | 421 | __free_hook = emacs_blocked_free; |
9ac0d9e0 JB |
422 | |
423 | old_malloc_hook = __malloc_hook; | |
b0846f52 | 424 | __malloc_hook = emacs_blocked_malloc; |
9ac0d9e0 JB |
425 | |
426 | old_realloc_hook = __realloc_hook; | |
b0846f52 | 427 | __realloc_hook = emacs_blocked_realloc; |
9ac0d9e0 JB |
428 | } |
429 | #endif | |
7146af97 | 430 | \f |
1a4f1e2c JB |
431 | /* Interval allocation. */ |
432 | ||
d5e35230 JA |
433 | #ifdef USE_TEXT_PROPERTIES |
434 | #define INTERVAL_BLOCK_SIZE \ | |
435 | ((1020 - sizeof (struct interval_block *)) / sizeof (struct interval)) | |
436 | ||
437 | struct interval_block | |
438 | { | |
439 | struct interval_block *next; | |
440 | struct interval intervals[INTERVAL_BLOCK_SIZE]; | |
441 | }; | |
442 | ||
443 | struct interval_block *interval_block; | |
444 | static int interval_block_index; | |
445 | ||
446 | INTERVAL interval_free_list; | |
447 | ||
448 | static void | |
449 | init_intervals () | |
450 | { | |
3c06d205 | 451 | allocating_for_lisp = 1; |
d5e35230 JA |
452 | interval_block |
453 | = (struct interval_block *) malloc (sizeof (struct interval_block)); | |
3c06d205 | 454 | allocating_for_lisp = 0; |
d5e35230 | 455 | interval_block->next = 0; |
290c8f1e | 456 | bzero ((char *) interval_block->intervals, sizeof interval_block->intervals); |
d5e35230 JA |
457 | interval_block_index = 0; |
458 | interval_free_list = 0; | |
459 | } | |
460 | ||
461 | #define INIT_INTERVALS init_intervals () | |
462 | ||
463 | INTERVAL | |
464 | make_interval () | |
465 | { | |
466 | INTERVAL val; | |
467 | ||
468 | if (interval_free_list) | |
469 | { | |
470 | val = interval_free_list; | |
471 | interval_free_list = interval_free_list->parent; | |
472 | } | |
473 | else | |
474 | { | |
475 | if (interval_block_index == INTERVAL_BLOCK_SIZE) | |
476 | { | |
3c06d205 KH |
477 | register struct interval_block *newi; |
478 | ||
479 | allocating_for_lisp = 1; | |
480 | newi = (struct interval_block *) xmalloc (sizeof (struct interval_block)); | |
d5e35230 | 481 | |
3c06d205 | 482 | allocating_for_lisp = 0; |
d5e35230 JA |
483 | VALIDATE_LISP_STORAGE (newi, sizeof *newi); |
484 | newi->next = interval_block; | |
485 | interval_block = newi; | |
486 | interval_block_index = 0; | |
487 | } | |
488 | val = &interval_block->intervals[interval_block_index++]; | |
489 | } | |
490 | consing_since_gc += sizeof (struct interval); | |
310ea200 | 491 | intervals_consed++; |
d5e35230 JA |
492 | RESET_INTERVAL (val); |
493 | return val; | |
494 | } | |
495 | ||
496 | static int total_free_intervals, total_intervals; | |
497 | ||
498 | /* Mark the pointers of one interval. */ | |
499 | ||
500 | static void | |
d393c068 | 501 | mark_interval (i, dummy) |
d5e35230 | 502 | register INTERVAL i; |
d393c068 | 503 | Lisp_Object dummy; |
d5e35230 JA |
504 | { |
505 | if (XMARKBIT (i->plist)) | |
506 | abort (); | |
507 | mark_object (&i->plist); | |
508 | XMARK (i->plist); | |
509 | } | |
510 | ||
511 | static void | |
512 | mark_interval_tree (tree) | |
513 | register INTERVAL tree; | |
514 | { | |
e8720644 JB |
515 | /* No need to test if this tree has been marked already; this |
516 | function is always called through the MARK_INTERVAL_TREE macro, | |
517 | which takes care of that. */ | |
518 | ||
519 | /* XMARK expands to an assignment; the LHS of an assignment can't be | |
520 | a cast. */ | |
521 | XMARK (* (Lisp_Object *) &tree->parent); | |
d5e35230 | 522 | |
d393c068 | 523 | traverse_intervals (tree, 1, 0, mark_interval, Qnil); |
d5e35230 JA |
524 | } |
525 | ||
e8720644 JB |
526 | #define MARK_INTERVAL_TREE(i) \ |
527 | do { \ | |
528 | if (!NULL_INTERVAL_P (i) \ | |
74d84334 | 529 | && ! XMARKBIT (*(Lisp_Object *) &i->parent)) \ |
e8720644 JB |
530 | mark_interval_tree (i); \ |
531 | } while (0) | |
d5e35230 | 532 | |
1a4f1e2c | 533 | /* The oddity in the call to XUNMARK is necessary because XUNMARK |
eb8c3be9 | 534 | expands to an assignment to its argument, and most C compilers don't |
1a4f1e2c JB |
535 | support casts on the left operand of `='. */ |
536 | #define UNMARK_BALANCE_INTERVALS(i) \ | |
537 | { \ | |
538 | if (! NULL_INTERVAL_P (i)) \ | |
539 | { \ | |
540 | XUNMARK (* (Lisp_Object *) (&(i)->parent)); \ | |
541 | (i) = balance_intervals (i); \ | |
542 | } \ | |
d5e35230 JA |
543 | } |
544 | ||
545 | #else /* no interval use */ | |
546 | ||
547 | #define INIT_INTERVALS | |
548 | ||
549 | #define UNMARK_BALANCE_INTERVALS(i) | |
550 | #define MARK_INTERVAL_TREE(i) | |
551 | ||
552 | #endif /* no interval use */ | |
553 | \f | |
1a4f1e2c JB |
554 | /* Floating point allocation. */ |
555 | ||
7146af97 JB |
556 | #ifdef LISP_FLOAT_TYPE |
557 | /* Allocation of float cells, just like conses */ | |
558 | /* We store float cells inside of float_blocks, allocating a new | |
559 | float_block with malloc whenever necessary. Float cells reclaimed by | |
560 | GC are put on a free list to be reallocated before allocating | |
561 | any new float cells from the latest float_block. | |
562 | ||
563 | Each float_block is just under 1020 bytes long, | |
564 | since malloc really allocates in units of powers of two | |
565 | and uses 4 bytes for its own overhead. */ | |
566 | ||
567 | #define FLOAT_BLOCK_SIZE \ | |
568 | ((1020 - sizeof (struct float_block *)) / sizeof (struct Lisp_Float)) | |
569 | ||
570 | struct float_block | |
571 | { | |
572 | struct float_block *next; | |
573 | struct Lisp_Float floats[FLOAT_BLOCK_SIZE]; | |
574 | }; | |
575 | ||
576 | struct float_block *float_block; | |
577 | int float_block_index; | |
578 | ||
579 | struct Lisp_Float *float_free_list; | |
580 | ||
581 | void | |
582 | init_float () | |
583 | { | |
3c06d205 | 584 | allocating_for_lisp = 1; |
7146af97 | 585 | float_block = (struct float_block *) malloc (sizeof (struct float_block)); |
3c06d205 | 586 | allocating_for_lisp = 0; |
7146af97 | 587 | float_block->next = 0; |
290c8f1e | 588 | bzero ((char *) float_block->floats, sizeof float_block->floats); |
7146af97 JB |
589 | float_block_index = 0; |
590 | float_free_list = 0; | |
591 | } | |
592 | ||
593 | /* Explicitly free a float cell. */ | |
594 | free_float (ptr) | |
595 | struct Lisp_Float *ptr; | |
596 | { | |
1cd5fe6a | 597 | *(struct Lisp_Float **)&ptr->data = float_free_list; |
7146af97 JB |
598 | float_free_list = ptr; |
599 | } | |
600 | ||
601 | Lisp_Object | |
602 | make_float (float_value) | |
603 | double float_value; | |
604 | { | |
605 | register Lisp_Object val; | |
606 | ||
607 | if (float_free_list) | |
608 | { | |
1cd5fe6a RS |
609 | /* We use the data field for chaining the free list |
610 | so that we won't use the same field that has the mark bit. */ | |
45d12a89 | 611 | XSETFLOAT (val, float_free_list); |
1cd5fe6a | 612 | float_free_list = *(struct Lisp_Float **)&float_free_list->data; |
7146af97 JB |
613 | } |
614 | else | |
615 | { | |
616 | if (float_block_index == FLOAT_BLOCK_SIZE) | |
617 | { | |
3c06d205 KH |
618 | register struct float_block *new; |
619 | ||
620 | allocating_for_lisp = 1; | |
621 | new = (struct float_block *) xmalloc (sizeof (struct float_block)); | |
622 | allocating_for_lisp = 0; | |
7146af97 JB |
623 | VALIDATE_LISP_STORAGE (new, sizeof *new); |
624 | new->next = float_block; | |
625 | float_block = new; | |
626 | float_block_index = 0; | |
627 | } | |
45d12a89 | 628 | XSETFLOAT (val, &float_block->floats[float_block_index++]); |
7146af97 JB |
629 | } |
630 | XFLOAT (val)->data = float_value; | |
67ba9986 | 631 | XSETFASTINT (XFLOAT (val)->type, 0); /* bug chasing -wsr */ |
7146af97 | 632 | consing_since_gc += sizeof (struct Lisp_Float); |
310ea200 | 633 | floats_consed++; |
7146af97 JB |
634 | return val; |
635 | } | |
636 | ||
637 | #endif /* LISP_FLOAT_TYPE */ | |
638 | \f | |
639 | /* Allocation of cons cells */ | |
640 | /* We store cons cells inside of cons_blocks, allocating a new | |
641 | cons_block with malloc whenever necessary. Cons cells reclaimed by | |
642 | GC are put on a free list to be reallocated before allocating | |
643 | any new cons cells from the latest cons_block. | |
644 | ||
645 | Each cons_block is just under 1020 bytes long, | |
646 | since malloc really allocates in units of powers of two | |
647 | and uses 4 bytes for its own overhead. */ | |
648 | ||
649 | #define CONS_BLOCK_SIZE \ | |
650 | ((1020 - sizeof (struct cons_block *)) / sizeof (struct Lisp_Cons)) | |
651 | ||
652 | struct cons_block | |
653 | { | |
654 | struct cons_block *next; | |
655 | struct Lisp_Cons conses[CONS_BLOCK_SIZE]; | |
656 | }; | |
657 | ||
658 | struct cons_block *cons_block; | |
659 | int cons_block_index; | |
660 | ||
661 | struct Lisp_Cons *cons_free_list; | |
662 | ||
663 | void | |
664 | init_cons () | |
665 | { | |
3c06d205 | 666 | allocating_for_lisp = 1; |
7146af97 | 667 | cons_block = (struct cons_block *) malloc (sizeof (struct cons_block)); |
3c06d205 | 668 | allocating_for_lisp = 0; |
7146af97 | 669 | cons_block->next = 0; |
290c8f1e | 670 | bzero ((char *) cons_block->conses, sizeof cons_block->conses); |
7146af97 JB |
671 | cons_block_index = 0; |
672 | cons_free_list = 0; | |
673 | } | |
674 | ||
675 | /* Explicitly free a cons cell. */ | |
d457598b AS |
676 | |
677 | void | |
7146af97 JB |
678 | free_cons (ptr) |
679 | struct Lisp_Cons *ptr; | |
680 | { | |
1cd5fe6a | 681 | *(struct Lisp_Cons **)&ptr->cdr = cons_free_list; |
7146af97 JB |
682 | cons_free_list = ptr; |
683 | } | |
684 | ||
685 | DEFUN ("cons", Fcons, Scons, 2, 2, 0, | |
686 | "Create a new cons, give it CAR and CDR as components, and return it.") | |
687 | (car, cdr) | |
688 | Lisp_Object car, cdr; | |
689 | { | |
690 | register Lisp_Object val; | |
691 | ||
692 | if (cons_free_list) | |
693 | { | |
1cd5fe6a RS |
694 | /* We use the cdr for chaining the free list |
695 | so that we won't use the same field that has the mark bit. */ | |
45d12a89 | 696 | XSETCONS (val, cons_free_list); |
1cd5fe6a | 697 | cons_free_list = *(struct Lisp_Cons **)&cons_free_list->cdr; |
7146af97 JB |
698 | } |
699 | else | |
700 | { | |
701 | if (cons_block_index == CONS_BLOCK_SIZE) | |
702 | { | |
3c06d205 KH |
703 | register struct cons_block *new; |
704 | allocating_for_lisp = 1; | |
705 | new = (struct cons_block *) xmalloc (sizeof (struct cons_block)); | |
706 | allocating_for_lisp = 0; | |
7146af97 JB |
707 | VALIDATE_LISP_STORAGE (new, sizeof *new); |
708 | new->next = cons_block; | |
709 | cons_block = new; | |
710 | cons_block_index = 0; | |
711 | } | |
45d12a89 | 712 | XSETCONS (val, &cons_block->conses[cons_block_index++]); |
7146af97 JB |
713 | } |
714 | XCONS (val)->car = car; | |
715 | XCONS (val)->cdr = cdr; | |
716 | consing_since_gc += sizeof (struct Lisp_Cons); | |
310ea200 | 717 | cons_cells_consed++; |
7146af97 JB |
718 | return val; |
719 | } | |
720 | ||
721 | DEFUN ("list", Flist, Slist, 0, MANY, 0, | |
722 | "Return a newly created list with specified arguments as elements.\n\ | |
723 | Any number of arguments, even zero arguments, are allowed.") | |
724 | (nargs, args) | |
725 | int nargs; | |
726 | register Lisp_Object *args; | |
727 | { | |
128a5b55 RS |
728 | register Lisp_Object val; |
729 | val = Qnil; | |
7146af97 | 730 | |
128a5b55 RS |
731 | while (nargs > 0) |
732 | { | |
733 | nargs--; | |
734 | val = Fcons (args[nargs], val); | |
735 | } | |
7146af97 JB |
736 | return val; |
737 | } | |
738 | ||
739 | DEFUN ("make-list", Fmake_list, Smake_list, 2, 2, 0, | |
740 | "Return a newly created list of length LENGTH, with each element being INIT.") | |
741 | (length, init) | |
742 | register Lisp_Object length, init; | |
743 | { | |
744 | register Lisp_Object val; | |
745 | register int size; | |
746 | ||
c9dad5ed KH |
747 | CHECK_NATNUM (length, 0); |
748 | size = XFASTINT (length); | |
7146af97 JB |
749 | |
750 | val = Qnil; | |
751 | while (size-- > 0) | |
752 | val = Fcons (init, val); | |
753 | return val; | |
754 | } | |
755 | \f | |
756 | /* Allocation of vectors */ | |
757 | ||
758 | struct Lisp_Vector *all_vectors; | |
759 | ||
1825c68d KH |
760 | struct Lisp_Vector * |
761 | allocate_vectorlike (len) | |
762 | EMACS_INT len; | |
763 | { | |
764 | struct Lisp_Vector *p; | |
765 | ||
3c06d205 | 766 | allocating_for_lisp = 1; |
d1658221 RS |
767 | #ifdef DOUG_LEA_MALLOC |
768 | /* Prevent mmap'ing the chunk (which is potentially very large). */ | |
769 | mallopt (M_MMAP_MAX, 0); | |
770 | #endif | |
1825c68d KH |
771 | p = (struct Lisp_Vector *)xmalloc (sizeof (struct Lisp_Vector) |
772 | + (len - 1) * sizeof (Lisp_Object)); | |
d1658221 RS |
773 | #ifdef DOUG_LEA_MALLOC |
774 | /* Back to a reasonable maximum of mmap'ed areas. */ | |
775 | mallopt (M_MMAP_MAX, 64); | |
776 | #endif | |
3c06d205 | 777 | allocating_for_lisp = 0; |
1825c68d KH |
778 | VALIDATE_LISP_STORAGE (p, 0); |
779 | consing_since_gc += (sizeof (struct Lisp_Vector) | |
780 | + (len - 1) * sizeof (Lisp_Object)); | |
310ea200 | 781 | vector_cells_consed += len; |
1825c68d KH |
782 | |
783 | p->next = all_vectors; | |
784 | all_vectors = p; | |
785 | return p; | |
786 | } | |
787 | ||
7146af97 JB |
788 | DEFUN ("make-vector", Fmake_vector, Smake_vector, 2, 2, 0, |
789 | "Return a newly created vector of length LENGTH, with each element being INIT.\n\ | |
790 | See also the function `vector'.") | |
791 | (length, init) | |
792 | register Lisp_Object length, init; | |
793 | { | |
1825c68d KH |
794 | Lisp_Object vector; |
795 | register EMACS_INT sizei; | |
796 | register int index; | |
7146af97 JB |
797 | register struct Lisp_Vector *p; |
798 | ||
c9dad5ed KH |
799 | CHECK_NATNUM (length, 0); |
800 | sizei = XFASTINT (length); | |
7146af97 | 801 | |
1825c68d | 802 | p = allocate_vectorlike (sizei); |
7146af97 | 803 | p->size = sizei; |
7146af97 JB |
804 | for (index = 0; index < sizei; index++) |
805 | p->contents[index] = init; | |
806 | ||
1825c68d | 807 | XSETVECTOR (vector, p); |
7146af97 JB |
808 | return vector; |
809 | } | |
810 | ||
a59de17b | 811 | DEFUN ("make-char-table", Fmake_char_table, Smake_char_table, 1, 2, 0, |
c58b2b4d | 812 | "Return a newly created char-table, with purpose PURPOSE.\n\ |
7b07587b | 813 | Each element is initialized to INIT, which defaults to nil.\n\ |
d7cd5d4f | 814 | PURPOSE should be a symbol which has a `char-table-extra-slots' property.\n\ |
a59de17b RS |
815 | The property's value should be an integer between 0 and 10.") |
816 | (purpose, init) | |
817 | register Lisp_Object purpose, init; | |
7b07587b RS |
818 | { |
819 | Lisp_Object vector; | |
a59de17b RS |
820 | Lisp_Object n; |
821 | CHECK_SYMBOL (purpose, 1); | |
0551bde3 | 822 | n = Fget (purpose, Qchar_table_extra_slots); |
a59de17b | 823 | CHECK_NUMBER (n, 0); |
7b07587b RS |
824 | if (XINT (n) < 0 || XINT (n) > 10) |
825 | args_out_of_range (n, Qnil); | |
826 | /* Add 2 to the size for the defalt and parent slots. */ | |
827 | vector = Fmake_vector (make_number (CHAR_TABLE_STANDARD_SLOTS + XINT (n)), | |
828 | init); | |
0551bde3 | 829 | XCHAR_TABLE (vector)->top = Qt; |
c96a008c | 830 | XCHAR_TABLE (vector)->parent = Qnil; |
a59de17b | 831 | XCHAR_TABLE (vector)->purpose = purpose; |
7b07587b RS |
832 | XSETCHAR_TABLE (vector, XCHAR_TABLE (vector)); |
833 | return vector; | |
834 | } | |
835 | ||
0551bde3 KH |
836 | /* Return a newly created sub char table with default value DEFALT. |
837 | Since a sub char table does not appear as a top level Emacs Lisp | |
838 | object, we don't need a Lisp interface to make it. */ | |
839 | ||
840 | Lisp_Object | |
841 | make_sub_char_table (defalt) | |
842 | Lisp_Object defalt; | |
843 | { | |
844 | Lisp_Object vector | |
845 | = Fmake_vector (make_number (SUB_CHAR_TABLE_STANDARD_SLOTS), Qnil); | |
846 | XCHAR_TABLE (vector)->top = Qnil; | |
847 | XCHAR_TABLE (vector)->defalt = defalt; | |
848 | XSETCHAR_TABLE (vector, XCHAR_TABLE (vector)); | |
849 | return vector; | |
850 | } | |
851 | ||
7146af97 JB |
852 | DEFUN ("vector", Fvector, Svector, 0, MANY, 0, |
853 | "Return a newly created vector with specified arguments as elements.\n\ | |
854 | Any number of arguments, even zero arguments, are allowed.") | |
855 | (nargs, args) | |
856 | register int nargs; | |
857 | Lisp_Object *args; | |
858 | { | |
859 | register Lisp_Object len, val; | |
860 | register int index; | |
861 | register struct Lisp_Vector *p; | |
862 | ||
67ba9986 | 863 | XSETFASTINT (len, nargs); |
7146af97 JB |
864 | val = Fmake_vector (len, Qnil); |
865 | p = XVECTOR (val); | |
866 | for (index = 0; index < nargs; index++) | |
867 | p->contents[index] = args[index]; | |
868 | return val; | |
869 | } | |
870 | ||
871 | DEFUN ("make-byte-code", Fmake_byte_code, Smake_byte_code, 4, MANY, 0, | |
872 | "Create a byte-code object with specified arguments as elements.\n\ | |
873 | The arguments should be the arglist, bytecode-string, constant vector,\n\ | |
874 | stack size, (optional) doc string, and (optional) interactive spec.\n\ | |
875 | The first four arguments are required; at most six have any\n\ | |
876 | significance.") | |
877 | (nargs, args) | |
878 | register int nargs; | |
879 | Lisp_Object *args; | |
880 | { | |
881 | register Lisp_Object len, val; | |
882 | register int index; | |
883 | register struct Lisp_Vector *p; | |
884 | ||
67ba9986 | 885 | XSETFASTINT (len, nargs); |
265a9e55 | 886 | if (!NILP (Vpurify_flag)) |
5a053ea9 | 887 | val = make_pure_vector ((EMACS_INT) nargs); |
7146af97 JB |
888 | else |
889 | val = Fmake_vector (len, Qnil); | |
890 | p = XVECTOR (val); | |
891 | for (index = 0; index < nargs; index++) | |
892 | { | |
265a9e55 | 893 | if (!NILP (Vpurify_flag)) |
7146af97 JB |
894 | args[index] = Fpurecopy (args[index]); |
895 | p->contents[index] = args[index]; | |
896 | } | |
50aee051 | 897 | XSETCOMPILED (val, p); |
7146af97 JB |
898 | return val; |
899 | } | |
900 | \f | |
901 | /* Allocation of symbols. | |
902 | Just like allocation of conses! | |
903 | ||
904 | Each symbol_block is just under 1020 bytes long, | |
905 | since malloc really allocates in units of powers of two | |
906 | and uses 4 bytes for its own overhead. */ | |
907 | ||
908 | #define SYMBOL_BLOCK_SIZE \ | |
909 | ((1020 - sizeof (struct symbol_block *)) / sizeof (struct Lisp_Symbol)) | |
910 | ||
911 | struct symbol_block | |
912 | { | |
913 | struct symbol_block *next; | |
914 | struct Lisp_Symbol symbols[SYMBOL_BLOCK_SIZE]; | |
915 | }; | |
916 | ||
917 | struct symbol_block *symbol_block; | |
918 | int symbol_block_index; | |
919 | ||
920 | struct Lisp_Symbol *symbol_free_list; | |
921 | ||
922 | void | |
923 | init_symbol () | |
924 | { | |
3c06d205 | 925 | allocating_for_lisp = 1; |
7146af97 | 926 | symbol_block = (struct symbol_block *) malloc (sizeof (struct symbol_block)); |
3c06d205 | 927 | allocating_for_lisp = 0; |
7146af97 | 928 | symbol_block->next = 0; |
290c8f1e | 929 | bzero ((char *) symbol_block->symbols, sizeof symbol_block->symbols); |
7146af97 JB |
930 | symbol_block_index = 0; |
931 | symbol_free_list = 0; | |
932 | } | |
933 | ||
934 | DEFUN ("make-symbol", Fmake_symbol, Smake_symbol, 1, 1, 0, | |
935 | "Return a newly allocated uninterned symbol whose name is NAME.\n\ | |
936 | Its value and function definition are void, and its property list is nil.") | |
54ee42dd EN |
937 | (name) |
938 | Lisp_Object name; | |
7146af97 JB |
939 | { |
940 | register Lisp_Object val; | |
941 | register struct Lisp_Symbol *p; | |
942 | ||
54ee42dd | 943 | CHECK_STRING (name, 0); |
7146af97 JB |
944 | |
945 | if (symbol_free_list) | |
946 | { | |
45d12a89 | 947 | XSETSYMBOL (val, symbol_free_list); |
85481507 | 948 | symbol_free_list = *(struct Lisp_Symbol **)&symbol_free_list->value; |
7146af97 JB |
949 | } |
950 | else | |
951 | { | |
952 | if (symbol_block_index == SYMBOL_BLOCK_SIZE) | |
953 | { | |
3c06d205 KH |
954 | struct symbol_block *new; |
955 | allocating_for_lisp = 1; | |
956 | new = (struct symbol_block *) xmalloc (sizeof (struct symbol_block)); | |
957 | allocating_for_lisp = 0; | |
7146af97 JB |
958 | VALIDATE_LISP_STORAGE (new, sizeof *new); |
959 | new->next = symbol_block; | |
960 | symbol_block = new; | |
961 | symbol_block_index = 0; | |
962 | } | |
45d12a89 | 963 | XSETSYMBOL (val, &symbol_block->symbols[symbol_block_index++]); |
7146af97 JB |
964 | } |
965 | p = XSYMBOL (val); | |
636b7260 | 966 | p->name = XSTRING (name); |
47d5b31e | 967 | p->obarray = Qnil; |
7146af97 JB |
968 | p->plist = Qnil; |
969 | p->value = Qunbound; | |
970 | p->function = Qunbound; | |
971 | p->next = 0; | |
972 | consing_since_gc += sizeof (struct Lisp_Symbol); | |
310ea200 | 973 | symbols_consed++; |
7146af97 JB |
974 | return val; |
975 | } | |
976 | \f | |
a0a38eb7 | 977 | /* Allocation of markers and other objects that share that structure. |
7146af97 JB |
978 | Works like allocation of conses. */ |
979 | ||
980 | #define MARKER_BLOCK_SIZE \ | |
a0a38eb7 | 981 | ((1020 - sizeof (struct marker_block *)) / sizeof (union Lisp_Misc)) |
7146af97 JB |
982 | |
983 | struct marker_block | |
984 | { | |
985 | struct marker_block *next; | |
a0a38eb7 | 986 | union Lisp_Misc markers[MARKER_BLOCK_SIZE]; |
7146af97 JB |
987 | }; |
988 | ||
989 | struct marker_block *marker_block; | |
990 | int marker_block_index; | |
991 | ||
a0a38eb7 | 992 | union Lisp_Misc *marker_free_list; |
7146af97 JB |
993 | |
994 | void | |
995 | init_marker () | |
996 | { | |
3c06d205 | 997 | allocating_for_lisp = 1; |
7146af97 | 998 | marker_block = (struct marker_block *) malloc (sizeof (struct marker_block)); |
3c06d205 | 999 | allocating_for_lisp = 0; |
7146af97 | 1000 | marker_block->next = 0; |
290c8f1e | 1001 | bzero ((char *) marker_block->markers, sizeof marker_block->markers); |
7146af97 JB |
1002 | marker_block_index = 0; |
1003 | marker_free_list = 0; | |
1004 | } | |
1005 | ||
a0a38eb7 KH |
1006 | /* Return a newly allocated Lisp_Misc object, with no substructure. */ |
1007 | Lisp_Object | |
1008 | allocate_misc () | |
7146af97 | 1009 | { |
a0a38eb7 | 1010 | Lisp_Object val; |
e065a56e | 1011 | |
7146af97 JB |
1012 | if (marker_free_list) |
1013 | { | |
a0a38eb7 KH |
1014 | XSETMISC (val, marker_free_list); |
1015 | marker_free_list = marker_free_list->u_free.chain; | |
7146af97 JB |
1016 | } |
1017 | else | |
1018 | { | |
1019 | if (marker_block_index == MARKER_BLOCK_SIZE) | |
1020 | { | |
3c06d205 KH |
1021 | struct marker_block *new; |
1022 | allocating_for_lisp = 1; | |
1023 | new = (struct marker_block *) xmalloc (sizeof (struct marker_block)); | |
1024 | allocating_for_lisp = 0; | |
7146af97 JB |
1025 | VALIDATE_LISP_STORAGE (new, sizeof *new); |
1026 | new->next = marker_block; | |
1027 | marker_block = new; | |
1028 | marker_block_index = 0; | |
1029 | } | |
a0a38eb7 | 1030 | XSETMISC (val, &marker_block->markers[marker_block_index++]); |
7146af97 | 1031 | } |
a0a38eb7 | 1032 | consing_since_gc += sizeof (union Lisp_Misc); |
310ea200 | 1033 | misc_objects_consed++; |
a0a38eb7 KH |
1034 | return val; |
1035 | } | |
1036 | ||
1037 | DEFUN ("make-marker", Fmake_marker, Smake_marker, 0, 0, 0, | |
1038 | "Return a newly allocated marker which does not point at any place.") | |
1039 | () | |
1040 | { | |
1041 | register Lisp_Object val; | |
1042 | register struct Lisp_Marker *p; | |
1043 | ||
1044 | val = allocate_misc (); | |
a5da44fe | 1045 | XMISCTYPE (val) = Lisp_Misc_Marker; |
7146af97 JB |
1046 | p = XMARKER (val); |
1047 | p->buffer = 0; | |
193b12ca RS |
1048 | p->bytepos = 0; |
1049 | p->charpos = 0; | |
7146af97 | 1050 | p->chain = Qnil; |
6ef5c4bd | 1051 | p->insertion_type = 0; |
7146af97 JB |
1052 | return val; |
1053 | } | |
fd27a537 RS |
1054 | |
1055 | /* Put MARKER back on the free list after using it temporarily. */ | |
1056 | ||
d457598b | 1057 | void |
fd27a537 RS |
1058 | free_marker (marker) |
1059 | Lisp_Object marker; | |
1060 | { | |
5c5631cf RS |
1061 | unchain_marker (marker); |
1062 | ||
fd27a537 RS |
1063 | XMISC (marker)->u_marker.type = Lisp_Misc_Free; |
1064 | XMISC (marker)->u_free.chain = marker_free_list; | |
1065 | marker_free_list = XMISC (marker); | |
1066 | ||
1067 | total_free_markers++; | |
1068 | } | |
7146af97 JB |
1069 | \f |
1070 | /* Allocation of strings */ | |
1071 | ||
1072 | /* Strings reside inside of string_blocks. The entire data of the string, | |
1073 | both the size and the contents, live in part of the `chars' component of a string_block. | |
1074 | The `pos' component is the index within `chars' of the first free byte. | |
1075 | ||
1076 | first_string_block points to the first string_block ever allocated. | |
1077 | Each block points to the next one with its `next' field. | |
1078 | The `prev' fields chain in reverse order. | |
1079 | The last one allocated is the one currently being filled. | |
1080 | current_string_block points to it. | |
1081 | ||
1082 | The string_blocks that hold individual large strings | |
1083 | go in a separate chain, started by large_string_blocks. */ | |
1084 | ||
1085 | ||
1086 | /* String blocks contain this many useful bytes. | |
1087 | 8188 is power of 2, minus 4 for malloc overhead. */ | |
1088 | #define STRING_BLOCK_SIZE (8188 - sizeof (struct string_block_head)) | |
1089 | ||
1090 | /* A string bigger than this gets its own specially-made string block | |
1091 | if it doesn't fit in the current one. */ | |
1092 | #define STRING_BLOCK_OUTSIZE 1024 | |
1093 | ||
1094 | struct string_block_head | |
1095 | { | |
1096 | struct string_block *next, *prev; | |
0e7ff58f | 1097 | EMACS_INT pos; |
7146af97 JB |
1098 | }; |
1099 | ||
1100 | struct string_block | |
1101 | { | |
1102 | struct string_block *next, *prev; | |
42607681 | 1103 | EMACS_INT pos; |
7146af97 JB |
1104 | char chars[STRING_BLOCK_SIZE]; |
1105 | }; | |
1106 | ||
1107 | /* This points to the string block we are now allocating strings. */ | |
1108 | ||
1109 | struct string_block *current_string_block; | |
1110 | ||
1111 | /* This points to the oldest string block, the one that starts the chain. */ | |
1112 | ||
1113 | struct string_block *first_string_block; | |
1114 | ||
1115 | /* Last string block in chain of those made for individual large strings. */ | |
1116 | ||
1117 | struct string_block *large_string_blocks; | |
1118 | ||
1119 | /* If SIZE is the length of a string, this returns how many bytes | |
1120 | the string occupies in a string_block (including padding). */ | |
1121 | ||
1122 | #define STRING_FULLSIZE(size) (((size) + sizeof (struct Lisp_String) + PAD) \ | |
1123 | & ~(PAD - 1)) | |
42607681 | 1124 | #define PAD (sizeof (EMACS_INT)) |
7146af97 JB |
1125 | |
1126 | #if 0 | |
1127 | #define STRING_FULLSIZE(SIZE) \ | |
42607681 | 1128 | (((SIZE) + 2 * sizeof (EMACS_INT)) & ~(sizeof (EMACS_INT) - 1)) |
7146af97 JB |
1129 | #endif |
1130 | ||
1131 | void | |
1132 | init_strings () | |
1133 | { | |
3c06d205 | 1134 | allocating_for_lisp = 1; |
7146af97 | 1135 | current_string_block = (struct string_block *) malloc (sizeof (struct string_block)); |
3c06d205 | 1136 | allocating_for_lisp = 0; |
7146af97 JB |
1137 | first_string_block = current_string_block; |
1138 | consing_since_gc += sizeof (struct string_block); | |
1139 | current_string_block->next = 0; | |
1140 | current_string_block->prev = 0; | |
1141 | current_string_block->pos = 0; | |
1142 | large_string_blocks = 0; | |
1143 | } | |
1144 | ||
1145 | DEFUN ("make-string", Fmake_string, Smake_string, 2, 2, 0, | |
1146 | "Return a newly created string of length LENGTH, with each element being INIT.\n\ | |
1147 | Both LENGTH and INIT must be numbers.") | |
1148 | (length, init) | |
1149 | Lisp_Object length, init; | |
1150 | { | |
1151 | register Lisp_Object val; | |
1152 | register unsigned char *p, *end, c; | |
1153 | ||
c9dad5ed | 1154 | CHECK_NATNUM (length, 0); |
7146af97 | 1155 | CHECK_NUMBER (init, 1); |
c9dad5ed | 1156 | val = make_uninit_string (XFASTINT (length)); |
7146af97 JB |
1157 | c = XINT (init); |
1158 | p = XSTRING (val)->data; | |
1159 | end = p + XSTRING (val)->size; | |
1160 | while (p != end) | |
1161 | *p++ = c; | |
1162 | *p = 0; | |
1163 | return val; | |
1164 | } | |
1165 | ||
7b07587b | 1166 | DEFUN ("make-bool-vector", Fmake_bool_vector, Smake_bool_vector, 2, 2, 0, |
41ab2240 RS |
1167 | "Return a new bool-vector of length LENGTH, using INIT for as each element.\n\ |
1168 | LENGTH must be a number. INIT matters only in whether it is t or nil.") | |
7b07587b RS |
1169 | (length, init) |
1170 | Lisp_Object length, init; | |
1171 | { | |
1172 | register Lisp_Object val; | |
1173 | struct Lisp_Bool_Vector *p; | |
1174 | int real_init, i; | |
1175 | int length_in_chars, length_in_elts, bits_per_value; | |
1176 | ||
1177 | CHECK_NATNUM (length, 0); | |
1178 | ||
68be917d | 1179 | bits_per_value = sizeof (EMACS_INT) * BITS_PER_CHAR; |
7b07587b RS |
1180 | |
1181 | length_in_elts = (XFASTINT (length) + bits_per_value - 1) / bits_per_value; | |
1182 | length_in_chars = length_in_elts * sizeof (EMACS_INT); | |
1183 | ||
38a1965a KH |
1184 | /* We must allocate one more elements than LENGTH_IN_ELTS for the |
1185 | slot `size' of the struct Lisp_Bool_Vector. */ | |
1186 | val = Fmake_vector (make_number (length_in_elts + 1), Qnil); | |
7b07587b RS |
1187 | p = XBOOL_VECTOR (val); |
1188 | /* Get rid of any bits that would cause confusion. */ | |
1189 | p->vector_size = 0; | |
1190 | XSETBOOL_VECTOR (val, p); | |
1191 | p->size = XFASTINT (length); | |
1192 | ||
1193 | real_init = (NILP (init) ? 0 : -1); | |
1194 | for (i = 0; i < length_in_chars ; i++) | |
1195 | p->data[i] = real_init; | |
1196 | ||
1197 | return val; | |
1198 | } | |
1199 | ||
3f25e183 RS |
1200 | /* Make a string from NBYTES bytes at CONTENTS, |
1201 | and compute the number of characters from the contents. */ | |
1202 | ||
7146af97 | 1203 | Lisp_Object |
3f25e183 RS |
1204 | make_string (contents, nbytes) |
1205 | char *contents; | |
1206 | int nbytes; | |
1207 | { | |
1208 | register Lisp_Object val; | |
1209 | int nchars = chars_in_text (contents, nbytes); | |
1210 | val = make_uninit_multibyte_string (nchars, nbytes); | |
1211 | bcopy (contents, XSTRING (val)->data, nbytes); | |
1212 | return val; | |
1213 | } | |
1214 | ||
1215 | /* Make a string from LENGTH bytes at CONTENTS, | |
1216 | assuming each byte is a character. */ | |
1217 | ||
1218 | Lisp_Object | |
1219 | make_unibyte_string (contents, length) | |
7146af97 JB |
1220 | char *contents; |
1221 | int length; | |
1222 | { | |
1223 | register Lisp_Object val; | |
1224 | val = make_uninit_string (length); | |
1225 | bcopy (contents, XSTRING (val)->data, length); | |
1226 | return val; | |
1227 | } | |
1228 | ||
3f25e183 RS |
1229 | /* Make a string from NCHARS characters occupying NBYTES bytes at CONTENTS. */ |
1230 | ||
1231 | Lisp_Object | |
1232 | make_multibyte_string (contents, nchars, nbytes) | |
1233 | char *contents; | |
1234 | int nchars, nbytes; | |
1235 | { | |
1236 | register Lisp_Object val; | |
1237 | val = make_uninit_multibyte_string (nchars, nbytes); | |
1238 | bcopy (contents, XSTRING (val)->data, nbytes); | |
1239 | return val; | |
1240 | } | |
1241 | ||
1242 | /* Make a string from the data at STR, | |
1243 | treating it as multibyte if the data warrants. */ | |
1244 | ||
7146af97 JB |
1245 | Lisp_Object |
1246 | build_string (str) | |
1247 | char *str; | |
1248 | { | |
1249 | return make_string (str, strlen (str)); | |
1250 | } | |
1251 | ||
1252 | Lisp_Object | |
1253 | make_uninit_string (length) | |
1254 | int length; | |
3f25e183 RS |
1255 | { |
1256 | return make_uninit_multibyte_string (length, length); | |
1257 | } | |
1258 | ||
1259 | Lisp_Object | |
1260 | make_uninit_multibyte_string (length, length_byte) | |
1261 | int length, length_byte; | |
7146af97 JB |
1262 | { |
1263 | register Lisp_Object val; | |
3f25e183 | 1264 | register int fullsize = STRING_FULLSIZE (length_byte); |
7146af97 JB |
1265 | |
1266 | if (length < 0) abort (); | |
1267 | ||
1268 | if (fullsize <= STRING_BLOCK_SIZE - current_string_block->pos) | |
1269 | /* This string can fit in the current string block */ | |
1270 | { | |
45d12a89 KH |
1271 | XSETSTRING (val, |
1272 | ((struct Lisp_String *) | |
1273 | (current_string_block->chars + current_string_block->pos))); | |
7146af97 JB |
1274 | current_string_block->pos += fullsize; |
1275 | } | |
1276 | else if (fullsize > STRING_BLOCK_OUTSIZE) | |
1277 | /* This string gets its own string block */ | |
1278 | { | |
3c06d205 KH |
1279 | register struct string_block *new; |
1280 | allocating_for_lisp = 1; | |
d1658221 RS |
1281 | #ifdef DOUG_LEA_MALLOC |
1282 | /* Prevent mmap'ing the chunk (which is potentially very large). */ | |
1283 | mallopt (M_MMAP_MAX, 0); | |
1284 | #endif | |
3c06d205 | 1285 | new = (struct string_block *) xmalloc (sizeof (struct string_block_head) + fullsize); |
d1658221 RS |
1286 | #ifdef DOUG_LEA_MALLOC |
1287 | /* Back to a reasonable maximum of mmap'ed areas. */ | |
1288 | mallopt (M_MMAP_MAX, 64); | |
1289 | #endif | |
3c06d205 | 1290 | allocating_for_lisp = 0; |
7146af97 | 1291 | VALIDATE_LISP_STORAGE (new, 0); |
7146af97 JB |
1292 | consing_since_gc += sizeof (struct string_block_head) + fullsize; |
1293 | new->pos = fullsize; | |
1294 | new->next = large_string_blocks; | |
1295 | large_string_blocks = new; | |
45d12a89 KH |
1296 | XSETSTRING (val, |
1297 | ((struct Lisp_String *) | |
1298 | ((struct string_block_head *)new + 1))); | |
7146af97 JB |
1299 | } |
1300 | else | |
1301 | /* Make a new current string block and start it off with this string */ | |
1302 | { | |
3c06d205 KH |
1303 | register struct string_block *new; |
1304 | allocating_for_lisp = 1; | |
1305 | new = (struct string_block *) xmalloc (sizeof (struct string_block)); | |
1306 | allocating_for_lisp = 0; | |
7146af97 JB |
1307 | VALIDATE_LISP_STORAGE (new, sizeof *new); |
1308 | consing_since_gc += sizeof (struct string_block); | |
1309 | current_string_block->next = new; | |
1310 | new->prev = current_string_block; | |
1311 | new->next = 0; | |
1312 | current_string_block = new; | |
1313 | new->pos = fullsize; | |
45d12a89 KH |
1314 | XSETSTRING (val, |
1315 | (struct Lisp_String *) current_string_block->chars); | |
7146af97 JB |
1316 | } |
1317 | ||
310ea200 | 1318 | string_chars_consed += fullsize; |
7146af97 | 1319 | XSTRING (val)->size = length; |
3f25e183 RS |
1320 | XSTRING (val)->size_byte = length_byte; |
1321 | XSTRING (val)->data[length_byte] = 0; | |
d5e35230 | 1322 | INITIALIZE_INTERVAL (XSTRING (val), NULL_INTERVAL); |
7146af97 JB |
1323 | |
1324 | return val; | |
1325 | } | |
1326 | ||
1327 | /* Return a newly created vector or string with specified arguments as | |
736471d1 RS |
1328 | elements. If all the arguments are characters that can fit |
1329 | in a string of events, make a string; otherwise, make a vector. | |
1330 | ||
1331 | Any number of arguments, even zero arguments, are allowed. */ | |
7146af97 JB |
1332 | |
1333 | Lisp_Object | |
736471d1 | 1334 | make_event_array (nargs, args) |
7146af97 JB |
1335 | register int nargs; |
1336 | Lisp_Object *args; | |
1337 | { | |
1338 | int i; | |
1339 | ||
1340 | for (i = 0; i < nargs; i++) | |
736471d1 | 1341 | /* The things that fit in a string |
c9ca4659 RS |
1342 | are characters that are in 0...127, |
1343 | after discarding the meta bit and all the bits above it. */ | |
e687453f | 1344 | if (!INTEGERP (args[i]) |
c9ca4659 | 1345 | || (XUINT (args[i]) & ~(-CHAR_META)) >= 0200) |
7146af97 JB |
1346 | return Fvector (nargs, args); |
1347 | ||
1348 | /* Since the loop exited, we know that all the things in it are | |
1349 | characters, so we can make a string. */ | |
1350 | { | |
c13ccad2 | 1351 | Lisp_Object result; |
7146af97 | 1352 | |
50aee051 | 1353 | result = Fmake_string (make_number (nargs), make_number (0)); |
7146af97 | 1354 | for (i = 0; i < nargs; i++) |
736471d1 RS |
1355 | { |
1356 | XSTRING (result)->data[i] = XINT (args[i]); | |
1357 | /* Move the meta bit to the right place for a string char. */ | |
1358 | if (XINT (args[i]) & CHAR_META) | |
1359 | XSTRING (result)->data[i] |= 0x80; | |
1360 | } | |
7146af97 JB |
1361 | |
1362 | return result; | |
1363 | } | |
1364 | } | |
1365 | \f | |
1a4f1e2c JB |
1366 | /* Pure storage management. */ |
1367 | ||
7146af97 JB |
1368 | /* Must get an error if pure storage is full, |
1369 | since if it cannot hold a large string | |
1370 | it may be able to hold conses that point to that string; | |
1371 | then the string is not protected from gc. */ | |
1372 | ||
1373 | Lisp_Object | |
3f25e183 | 1374 | make_pure_string (data, length, length_byte) |
7146af97 JB |
1375 | char *data; |
1376 | int length; | |
3f25e183 | 1377 | int length_byte; |
7146af97 JB |
1378 | { |
1379 | register Lisp_Object new; | |
3f25e183 RS |
1380 | register int size = (2 * sizeof (EMACS_INT) |
1381 | + INTERVAL_PTR_SIZE + length_byte + 1); | |
7146af97 JB |
1382 | |
1383 | if (pureptr + size > PURESIZE) | |
1384 | error ("Pure Lisp storage exhausted"); | |
45d12a89 | 1385 | XSETSTRING (new, PUREBEG + pureptr); |
7146af97 | 1386 | XSTRING (new)->size = length; |
3f25e183 RS |
1387 | XSTRING (new)->size_byte = length_byte; |
1388 | bcopy (data, XSTRING (new)->data, length_byte); | |
1389 | XSTRING (new)->data[length_byte] = 0; | |
06c5fe00 RS |
1390 | |
1391 | /* We must give strings in pure storage some kind of interval. So we | |
1392 | give them a null one. */ | |
1393 | #if defined (USE_TEXT_PROPERTIES) | |
1394 | XSTRING (new)->intervals = NULL_INTERVAL; | |
1395 | #endif | |
42607681 RS |
1396 | pureptr += (size + sizeof (EMACS_INT) - 1) |
1397 | / sizeof (EMACS_INT) * sizeof (EMACS_INT); | |
7146af97 JB |
1398 | return new; |
1399 | } | |
1400 | ||
1401 | Lisp_Object | |
1402 | pure_cons (car, cdr) | |
1403 | Lisp_Object car, cdr; | |
1404 | { | |
1405 | register Lisp_Object new; | |
1406 | ||
1407 | if (pureptr + sizeof (struct Lisp_Cons) > PURESIZE) | |
1408 | error ("Pure Lisp storage exhausted"); | |
45d12a89 | 1409 | XSETCONS (new, PUREBEG + pureptr); |
7146af97 JB |
1410 | pureptr += sizeof (struct Lisp_Cons); |
1411 | XCONS (new)->car = Fpurecopy (car); | |
1412 | XCONS (new)->cdr = Fpurecopy (cdr); | |
1413 | return new; | |
1414 | } | |
1415 | ||
1416 | #ifdef LISP_FLOAT_TYPE | |
1417 | ||
1418 | Lisp_Object | |
1419 | make_pure_float (num) | |
1420 | double num; | |
1421 | { | |
1422 | register Lisp_Object new; | |
1423 | ||
6d19f28a JB |
1424 | /* Make sure that PUREBEG + pureptr is aligned on at least a sizeof |
1425 | (double) boundary. Some architectures (like the sparc) require | |
1426 | this, and I suspect that floats are rare enough that it's no | |
1427 | tragedy for those that do. */ | |
1428 | { | |
1429 | int alignment; | |
1430 | char *p = PUREBEG + pureptr; | |
1431 | ||
fe90ad97 JB |
1432 | #ifdef __GNUC__ |
1433 | #if __GNUC__ >= 2 | |
6d19f28a | 1434 | alignment = __alignof (struct Lisp_Float); |
fe90ad97 | 1435 | #else |
6d19f28a | 1436 | alignment = sizeof (struct Lisp_Float); |
fe90ad97 JB |
1437 | #endif |
1438 | #else | |
6d19f28a | 1439 | alignment = sizeof (struct Lisp_Float); |
fe90ad97 | 1440 | #endif |
6d19f28a JB |
1441 | p = (char *) (((unsigned long) p + alignment - 1) & - alignment); |
1442 | pureptr = p - PUREBEG; | |
1443 | } | |
1a4f1e2c | 1444 | |
7146af97 JB |
1445 | if (pureptr + sizeof (struct Lisp_Float) > PURESIZE) |
1446 | error ("Pure Lisp storage exhausted"); | |
45d12a89 | 1447 | XSETFLOAT (new, PUREBEG + pureptr); |
7146af97 JB |
1448 | pureptr += sizeof (struct Lisp_Float); |
1449 | XFLOAT (new)->data = num; | |
67ba9986 | 1450 | XSETFASTINT (XFLOAT (new)->type, 0); /* bug chasing -wsr */ |
7146af97 JB |
1451 | return new; |
1452 | } | |
1453 | ||
1454 | #endif /* LISP_FLOAT_TYPE */ | |
1455 | ||
1456 | Lisp_Object | |
1457 | make_pure_vector (len) | |
42607681 | 1458 | EMACS_INT len; |
7146af97 JB |
1459 | { |
1460 | register Lisp_Object new; | |
42607681 | 1461 | register EMACS_INT size = sizeof (struct Lisp_Vector) + (len - 1) * sizeof (Lisp_Object); |
7146af97 JB |
1462 | |
1463 | if (pureptr + size > PURESIZE) | |
1464 | error ("Pure Lisp storage exhausted"); | |
1465 | ||
45d12a89 | 1466 | XSETVECTOR (new, PUREBEG + pureptr); |
7146af97 JB |
1467 | pureptr += size; |
1468 | XVECTOR (new)->size = len; | |
1469 | return new; | |
1470 | } | |
1471 | ||
1472 | DEFUN ("purecopy", Fpurecopy, Spurecopy, 1, 1, 0, | |
1473 | "Make a copy of OBJECT in pure storage.\n\ | |
1474 | Recursively copies contents of vectors and cons cells.\n\ | |
1475 | Does not copy symbols.") | |
1476 | (obj) | |
1477 | register Lisp_Object obj; | |
1478 | { | |
265a9e55 | 1479 | if (NILP (Vpurify_flag)) |
7146af97 JB |
1480 | return obj; |
1481 | ||
1482 | if ((PNTR_COMPARISON_TYPE) XPNTR (obj) < (PNTR_COMPARISON_TYPE) ((char *) pure + PURESIZE) | |
1483 | && (PNTR_COMPARISON_TYPE) XPNTR (obj) >= (PNTR_COMPARISON_TYPE) pure) | |
1484 | return obj; | |
1485 | ||
d6dd74bb KH |
1486 | if (CONSP (obj)) |
1487 | return pure_cons (XCONS (obj)->car, XCONS (obj)->cdr); | |
7146af97 | 1488 | #ifdef LISP_FLOAT_TYPE |
d6dd74bb KH |
1489 | else if (FLOATP (obj)) |
1490 | return make_pure_float (XFLOAT (obj)->data); | |
7146af97 | 1491 | #endif /* LISP_FLOAT_TYPE */ |
d6dd74bb | 1492 | else if (STRINGP (obj)) |
3f25e183 RS |
1493 | return make_pure_string (XSTRING (obj)->data, XSTRING (obj)->size, |
1494 | XSTRING (obj)->size_byte); | |
d6dd74bb KH |
1495 | else if (COMPILEDP (obj) || VECTORP (obj)) |
1496 | { | |
1497 | register struct Lisp_Vector *vec; | |
1498 | register int i, size; | |
1499 | ||
1500 | size = XVECTOR (obj)->size; | |
7d535c68 KH |
1501 | if (size & PSEUDOVECTOR_FLAG) |
1502 | size &= PSEUDOVECTOR_SIZE_MASK; | |
01a4d290 | 1503 | vec = XVECTOR (make_pure_vector ((EMACS_INT) size)); |
d6dd74bb KH |
1504 | for (i = 0; i < size; i++) |
1505 | vec->contents[i] = Fpurecopy (XVECTOR (obj)->contents[i]); | |
1506 | if (COMPILEDP (obj)) | |
1507 | XSETCOMPILED (obj, vec); | |
1508 | else | |
1509 | XSETVECTOR (obj, vec); | |
7146af97 JB |
1510 | return obj; |
1511 | } | |
d6dd74bb KH |
1512 | else if (MARKERP (obj)) |
1513 | error ("Attempt to copy a marker to pure storage"); | |
1514 | else | |
1515 | return obj; | |
7146af97 JB |
1516 | } |
1517 | \f | |
1518 | /* Recording what needs to be marked for gc. */ | |
1519 | ||
1520 | struct gcpro *gcprolist; | |
1521 | ||
4cb7d267 | 1522 | #define NSTATICS 768 |
7146af97 JB |
1523 | |
1524 | Lisp_Object *staticvec[NSTATICS] = {0}; | |
1525 | ||
1526 | int staticidx = 0; | |
1527 | ||
1528 | /* Put an entry in staticvec, pointing at the variable whose address is given */ | |
1529 | ||
1530 | void | |
1531 | staticpro (varaddress) | |
1532 | Lisp_Object *varaddress; | |
1533 | { | |
1534 | staticvec[staticidx++] = varaddress; | |
1535 | if (staticidx >= NSTATICS) | |
1536 | abort (); | |
1537 | } | |
1538 | ||
1539 | struct catchtag | |
1540 | { | |
1541 | Lisp_Object tag; | |
1542 | Lisp_Object val; | |
1543 | struct catchtag *next; | |
cd67c797 KH |
1544 | #if 0 /* We don't need this for GC purposes */ |
1545 | jmp_buf jmp; | |
1546 | #endif | |
7146af97 JB |
1547 | }; |
1548 | ||
1549 | struct backtrace | |
1550 | { | |
1551 | struct backtrace *next; | |
1552 | Lisp_Object *function; | |
1553 | Lisp_Object *args; /* Points to vector of args. */ | |
1554 | int nargs; /* length of vector */ | |
1555 | /* if nargs is UNEVALLED, args points to slot holding list of unevalled args */ | |
1556 | char evalargs; | |
1557 | }; | |
7146af97 | 1558 | \f |
1a4f1e2c JB |
1559 | /* Garbage collection! */ |
1560 | ||
e8197642 RS |
1561 | /* Temporarily prevent garbage collection. */ |
1562 | ||
1563 | int | |
1564 | inhibit_garbage_collection () | |
1565 | { | |
1566 | int count = specpdl_ptr - specpdl; | |
26b926e1 | 1567 | Lisp_Object number; |
68be917d | 1568 | int nbits = min (VALBITS, BITS_PER_INT); |
e8197642 | 1569 | |
b580578b | 1570 | XSETINT (number, ((EMACS_INT) 1 << (nbits - 1)) - 1); |
26b926e1 RS |
1571 | |
1572 | specbind (Qgc_cons_threshold, number); | |
e8197642 RS |
1573 | |
1574 | return count; | |
1575 | } | |
1576 | ||
7146af97 JB |
1577 | DEFUN ("garbage-collect", Fgarbage_collect, Sgarbage_collect, 0, 0, "", |
1578 | "Reclaim storage for Lisp objects no longer needed.\n\ | |
1579 | Returns info on amount of space in use:\n\ | |
1580 | ((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS)\n\ | |
1581 | (USED-MARKERS . FREE-MARKERS) USED-STRING-CHARS USED-VECTOR-SLOTS\n\ | |
fa9e8864 | 1582 | (USED-FLOATS . FREE-FLOATS) (USED-INTERVALS . FREE-INTERVALS))\n\ |
7146af97 JB |
1583 | Garbage collection happens automatically if you cons more than\n\ |
1584 | `gc-cons-threshold' bytes of Lisp data since previous garbage collection.") | |
1585 | () | |
1586 | { | |
1587 | register struct gcpro *tail; | |
1588 | register struct specbinding *bind; | |
1589 | struct catchtag *catch; | |
1590 | struct handler *handler; | |
1591 | register struct backtrace *backlist; | |
1592 | register Lisp_Object tem; | |
1593 | char *omessage = echo_area_glyphs; | |
51056d11 | 1594 | int omessage_length = echo_area_glyphs_length; |
7da0b0d3 | 1595 | int oldmultibyte = message_enable_multibyte; |
7146af97 JB |
1596 | char stack_top_variable; |
1597 | register int i; | |
1598 | ||
58595309 KH |
1599 | /* In case user calls debug_print during GC, |
1600 | don't let that cause a recursive GC. */ | |
1601 | consing_since_gc = 0; | |
1602 | ||
7146af97 JB |
1603 | /* Save a copy of the contents of the stack, for debugging. */ |
1604 | #if MAX_SAVE_STACK > 0 | |
265a9e55 | 1605 | if (NILP (Vpurify_flag)) |
7146af97 JB |
1606 | { |
1607 | i = &stack_top_variable - stack_bottom; | |
1608 | if (i < 0) i = -i; | |
1609 | if (i < MAX_SAVE_STACK) | |
1610 | { | |
1611 | if (stack_copy == 0) | |
9ac0d9e0 | 1612 | stack_copy = (char *) xmalloc (stack_copy_size = i); |
7146af97 | 1613 | else if (stack_copy_size < i) |
9ac0d9e0 | 1614 | stack_copy = (char *) xrealloc (stack_copy, (stack_copy_size = i)); |
7146af97 JB |
1615 | if (stack_copy) |
1616 | { | |
42607681 | 1617 | if ((EMACS_INT) (&stack_top_variable - stack_bottom) > 0) |
7146af97 JB |
1618 | bcopy (stack_bottom, stack_copy, i); |
1619 | else | |
1620 | bcopy (&stack_top_variable, stack_copy, i); | |
1621 | } | |
1622 | } | |
1623 | } | |
1624 | #endif /* MAX_SAVE_STACK > 0 */ | |
1625 | ||
299585ee | 1626 | if (garbage_collection_messages) |
691c4285 | 1627 | message1_nolog ("Garbage collecting..."); |
7146af97 | 1628 | |
0f936def RS |
1629 | /* Don't keep command history around forever. */ |
1630 | if (NUMBERP (Vhistory_length) && XINT (Vhistory_length) > 0) | |
1631 | { | |
1632 | tem = Fnthcdr (Vhistory_length, Vcommand_history); | |
1633 | if (CONSP (tem)) | |
1634 | XCONS (tem)->cdr = Qnil; | |
1635 | } | |
ffd56f97 | 1636 | |
7146af97 JB |
1637 | /* Likewise for undo information. */ |
1638 | { | |
1639 | register struct buffer *nextb = all_buffers; | |
1640 | ||
1641 | while (nextb) | |
1642 | { | |
ffd56f97 JB |
1643 | /* If a buffer's undo list is Qt, that means that undo is |
1644 | turned off in that buffer. Calling truncate_undo_list on | |
1645 | Qt tends to return NULL, which effectively turns undo back on. | |
1646 | So don't call truncate_undo_list if undo_list is Qt. */ | |
1647 | if (! EQ (nextb->undo_list, Qt)) | |
1648 | nextb->undo_list | |
502b9b64 JB |
1649 | = truncate_undo_list (nextb->undo_list, undo_limit, |
1650 | undo_strong_limit); | |
7146af97 JB |
1651 | nextb = nextb->next; |
1652 | } | |
1653 | } | |
1654 | ||
1655 | gc_in_progress = 1; | |
1656 | ||
c23baf9f | 1657 | /* clear_marks (); */ |
7146af97 JB |
1658 | |
1659 | /* In each "large string", set the MARKBIT of the size field. | |
1660 | That enables mark_object to recognize them. */ | |
1661 | { | |
1662 | register struct string_block *b; | |
1663 | for (b = large_string_blocks; b; b = b->next) | |
1664 | ((struct Lisp_String *)(&b->chars[0]))->size |= MARKBIT; | |
1665 | } | |
1666 | ||
1667 | /* Mark all the special slots that serve as the roots of accessibility. | |
1668 | ||
1669 | Usually the special slots to mark are contained in particular structures. | |
1670 | Then we know no slot is marked twice because the structures don't overlap. | |
1671 | In some cases, the structures point to the slots to be marked. | |
1672 | For these, we use MARKBIT to avoid double marking of the slot. */ | |
1673 | ||
1674 | for (i = 0; i < staticidx; i++) | |
1675 | mark_object (staticvec[i]); | |
1676 | for (tail = gcprolist; tail; tail = tail->next) | |
1677 | for (i = 0; i < tail->nvars; i++) | |
1678 | if (!XMARKBIT (tail->var[i])) | |
1679 | { | |
1680 | mark_object (&tail->var[i]); | |
1681 | XMARK (tail->var[i]); | |
1682 | } | |
1683 | for (bind = specpdl; bind != specpdl_ptr; bind++) | |
1684 | { | |
1685 | mark_object (&bind->symbol); | |
1686 | mark_object (&bind->old_value); | |
1687 | } | |
1688 | for (catch = catchlist; catch; catch = catch->next) | |
1689 | { | |
1690 | mark_object (&catch->tag); | |
1691 | mark_object (&catch->val); | |
1692 | } | |
1693 | for (handler = handlerlist; handler; handler = handler->next) | |
1694 | { | |
1695 | mark_object (&handler->handler); | |
1696 | mark_object (&handler->var); | |
1697 | } | |
1698 | for (backlist = backtrace_list; backlist; backlist = backlist->next) | |
1699 | { | |
1700 | if (!XMARKBIT (*backlist->function)) | |
1701 | { | |
1702 | mark_object (backlist->function); | |
1703 | XMARK (*backlist->function); | |
1704 | } | |
1705 | if (backlist->nargs == UNEVALLED || backlist->nargs == MANY) | |
1706 | i = 0; | |
1707 | else | |
1708 | i = backlist->nargs - 1; | |
1709 | for (; i >= 0; i--) | |
1710 | if (!XMARKBIT (backlist->args[i])) | |
1711 | { | |
1712 | mark_object (&backlist->args[i]); | |
1713 | XMARK (backlist->args[i]); | |
1714 | } | |
1715 | } | |
b875d3f7 | 1716 | mark_kboards (); |
7146af97 JB |
1717 | |
1718 | gc_sweep (); | |
1719 | ||
1720 | /* Clear the mark bits that we set in certain root slots. */ | |
1721 | ||
1722 | for (tail = gcprolist; tail; tail = tail->next) | |
1723 | for (i = 0; i < tail->nvars; i++) | |
1724 | XUNMARK (tail->var[i]); | |
1725 | for (backlist = backtrace_list; backlist; backlist = backlist->next) | |
1726 | { | |
1727 | XUNMARK (*backlist->function); | |
1728 | if (backlist->nargs == UNEVALLED || backlist->nargs == MANY) | |
1729 | i = 0; | |
1730 | else | |
1731 | i = backlist->nargs - 1; | |
1732 | for (; i >= 0; i--) | |
1733 | XUNMARK (backlist->args[i]); | |
1734 | } | |
1735 | XUNMARK (buffer_defaults.name); | |
1736 | XUNMARK (buffer_local_symbols.name); | |
1737 | ||
c23baf9f | 1738 | /* clear_marks (); */ |
7146af97 JB |
1739 | gc_in_progress = 0; |
1740 | ||
1741 | consing_since_gc = 0; | |
1742 | if (gc_cons_threshold < 10000) | |
1743 | gc_cons_threshold = 10000; | |
1744 | ||
299585ee RS |
1745 | if (garbage_collection_messages) |
1746 | { | |
1747 | if (omessage || minibuf_level > 0) | |
7da0b0d3 | 1748 | message2_nolog (omessage, omessage_length, oldmultibyte); |
299585ee RS |
1749 | else |
1750 | message1_nolog ("Garbage collecting...done"); | |
1751 | } | |
7146af97 | 1752 | |
7146af97 JB |
1753 | return Fcons (Fcons (make_number (total_conses), |
1754 | make_number (total_free_conses)), | |
1755 | Fcons (Fcons (make_number (total_symbols), | |
1756 | make_number (total_free_symbols)), | |
1757 | Fcons (Fcons (make_number (total_markers), | |
1758 | make_number (total_free_markers)), | |
1759 | Fcons (make_number (total_string_size), | |
1760 | Fcons (make_number (total_vector_size), | |
fa9e8864 | 1761 | Fcons (Fcons |
7146af97 | 1762 | #ifdef LISP_FLOAT_TYPE |
fa9e8864 RS |
1763 | (make_number (total_floats), |
1764 | make_number (total_free_floats)), | |
7146af97 | 1765 | #else /* not LISP_FLOAT_TYPE */ |
fa9e8864 | 1766 | (make_number (0), make_number (0)), |
7146af97 | 1767 | #endif /* not LISP_FLOAT_TYPE */ |
fa9e8864 RS |
1768 | Fcons (Fcons |
1769 | #ifdef USE_TEXT_PROPERTIES | |
1770 | (make_number (total_intervals), | |
1771 | make_number (total_free_intervals)), | |
1772 | #else /* not USE_TEXT_PROPERTIES */ | |
1773 | (make_number (0), make_number (0)), | |
1774 | #endif /* not USE_TEXT_PROPERTIES */ | |
1775 | Qnil))))))); | |
7146af97 JB |
1776 | } |
1777 | \f | |
1778 | #if 0 | |
1779 | static void | |
1780 | clear_marks () | |
1781 | { | |
1782 | /* Clear marks on all conses */ | |
1783 | { | |
1784 | register struct cons_block *cblk; | |
1785 | register int lim = cons_block_index; | |
1786 | ||
1787 | for (cblk = cons_block; cblk; cblk = cblk->next) | |
1788 | { | |
1789 | register int i; | |
1790 | for (i = 0; i < lim; i++) | |
1791 | XUNMARK (cblk->conses[i].car); | |
1792 | lim = CONS_BLOCK_SIZE; | |
1793 | } | |
1794 | } | |
1795 | /* Clear marks on all symbols */ | |
1796 | { | |
1797 | register struct symbol_block *sblk; | |
1798 | register int lim = symbol_block_index; | |
1799 | ||
1800 | for (sblk = symbol_block; sblk; sblk = sblk->next) | |
1801 | { | |
1802 | register int i; | |
1803 | for (i = 0; i < lim; i++) | |
1804 | { | |
1805 | XUNMARK (sblk->symbols[i].plist); | |
1806 | } | |
1807 | lim = SYMBOL_BLOCK_SIZE; | |
1808 | } | |
1809 | } | |
1810 | /* Clear marks on all markers */ | |
1811 | { | |
1812 | register struct marker_block *sblk; | |
1813 | register int lim = marker_block_index; | |
1814 | ||
1815 | for (sblk = marker_block; sblk; sblk = sblk->next) | |
1816 | { | |
1817 | register int i; | |
1818 | for (i = 0; i < lim; i++) | |
a5da44fe | 1819 | if (sblk->markers[i].u_marker.type == Lisp_Misc_Marker) |
a0a38eb7 | 1820 | XUNMARK (sblk->markers[i].u_marker.chain); |
7146af97 JB |
1821 | lim = MARKER_BLOCK_SIZE; |
1822 | } | |
1823 | } | |
1824 | /* Clear mark bits on all buffers */ | |
1825 | { | |
1826 | register struct buffer *nextb = all_buffers; | |
1827 | ||
1828 | while (nextb) | |
1829 | { | |
1830 | XUNMARK (nextb->name); | |
1831 | nextb = nextb->next; | |
1832 | } | |
1833 | } | |
1834 | } | |
1835 | #endif | |
1836 | \f | |
1a4f1e2c JB |
1837 | /* Mark reference to a Lisp_Object. |
1838 | If the object referred to has not been seen yet, recursively mark | |
1839 | all the references contained in it. | |
7146af97 | 1840 | |
eb8c3be9 | 1841 | If the object referenced is a short string, the referencing slot |
7146af97 JB |
1842 | is threaded into a chain of such slots, pointed to from |
1843 | the `size' field of the string. The actual string size | |
1844 | lives in the last slot in the chain. We recognize the end | |
1845 | because it is < (unsigned) STRING_BLOCK_SIZE. */ | |
1846 | ||
785cd37f RS |
1847 | #define LAST_MARKED_SIZE 500 |
1848 | Lisp_Object *last_marked[LAST_MARKED_SIZE]; | |
1849 | int last_marked_index; | |
1850 | ||
7146af97 | 1851 | static void |
436c5811 RS |
1852 | mark_object (argptr) |
1853 | Lisp_Object *argptr; | |
7146af97 | 1854 | { |
436c5811 | 1855 | Lisp_Object *objptr = argptr; |
7146af97 JB |
1856 | register Lisp_Object obj; |
1857 | ||
9149e743 | 1858 | loop: |
7146af97 | 1859 | obj = *objptr; |
9149e743 | 1860 | loop2: |
7146af97 JB |
1861 | XUNMARK (obj); |
1862 | ||
7146af97 JB |
1863 | if ((PNTR_COMPARISON_TYPE) XPNTR (obj) < (PNTR_COMPARISON_TYPE) ((char *) pure + PURESIZE) |
1864 | && (PNTR_COMPARISON_TYPE) XPNTR (obj) >= (PNTR_COMPARISON_TYPE) pure) | |
1865 | return; | |
1866 | ||
785cd37f RS |
1867 | last_marked[last_marked_index++] = objptr; |
1868 | if (last_marked_index == LAST_MARKED_SIZE) | |
1869 | last_marked_index = 0; | |
1870 | ||
0220c518 | 1871 | switch (SWITCH_ENUM_CAST (XGCTYPE (obj))) |
7146af97 JB |
1872 | { |
1873 | case Lisp_String: | |
1874 | { | |
1875 | register struct Lisp_String *ptr = XSTRING (obj); | |
1876 | ||
d5e35230 | 1877 | MARK_INTERVAL_TREE (ptr->intervals); |
7146af97 JB |
1878 | if (ptr->size & MARKBIT) |
1879 | /* A large string. Just set ARRAY_MARK_FLAG. */ | |
1880 | ptr->size |= ARRAY_MARK_FLAG; | |
1881 | else | |
1882 | { | |
1883 | /* A small string. Put this reference | |
1884 | into the chain of references to it. | |
1fb577f7 | 1885 | If the address includes MARKBIT, put that bit elsewhere |
7146af97 JB |
1886 | when we store OBJPTR into the size field. */ |
1887 | ||
1888 | if (XMARKBIT (*objptr)) | |
1889 | { | |
67ba9986 | 1890 | XSETFASTINT (*objptr, ptr->size); |
7146af97 JB |
1891 | XMARK (*objptr); |
1892 | } | |
1893 | else | |
67ba9986 | 1894 | XSETFASTINT (*objptr, ptr->size); |
155ffe9c RS |
1895 | |
1896 | if ((EMACS_INT) objptr & DONT_COPY_FLAG) | |
1897 | abort (); | |
1fb577f7 KH |
1898 | ptr->size = (EMACS_INT) objptr; |
1899 | if (ptr->size & MARKBIT) | |
1900 | ptr->size ^= MARKBIT | DONT_COPY_FLAG; | |
7146af97 JB |
1901 | } |
1902 | } | |
1903 | break; | |
1904 | ||
76437631 | 1905 | case Lisp_Vectorlike: |
30e3190a | 1906 | if (GC_BUFFERP (obj)) |
6b552283 KH |
1907 | { |
1908 | if (!XMARKBIT (XBUFFER (obj)->name)) | |
1909 | mark_buffer (obj); | |
1910 | } | |
30e3190a | 1911 | else if (GC_SUBRP (obj)) |
169ee243 RS |
1912 | break; |
1913 | else if (GC_COMPILEDP (obj)) | |
1914 | /* We could treat this just like a vector, but it is better | |
1915 | to save the COMPILED_CONSTANTS element for last and avoid recursion | |
1916 | there. */ | |
1917 | { | |
1918 | register struct Lisp_Vector *ptr = XVECTOR (obj); | |
1919 | register EMACS_INT size = ptr->size; | |
1920 | /* See comment above under Lisp_Vector. */ | |
1921 | struct Lisp_Vector *volatile ptr1 = ptr; | |
1922 | register int i; | |
1923 | ||
1924 | if (size & ARRAY_MARK_FLAG) | |
1925 | break; /* Already marked */ | |
1926 | ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */ | |
76437631 | 1927 | size &= PSEUDOVECTOR_SIZE_MASK; |
169ee243 RS |
1928 | for (i = 0; i < size; i++) /* and then mark its elements */ |
1929 | { | |
1930 | if (i != COMPILED_CONSTANTS) | |
1931 | mark_object (&ptr1->contents[i]); | |
1932 | } | |
1933 | /* This cast should be unnecessary, but some Mips compiler complains | |
1934 | (MIPS-ABI + SysVR4, DC/OSx, etc). */ | |
1935 | objptr = (Lisp_Object *) &ptr1->contents[COMPILED_CONSTANTS]; | |
1936 | goto loop; | |
1937 | } | |
169ee243 RS |
1938 | else if (GC_FRAMEP (obj)) |
1939 | { | |
1940 | /* See comment above under Lisp_Vector for why this is volatile. */ | |
1941 | register struct frame *volatile ptr = XFRAME (obj); | |
1942 | register EMACS_INT size = ptr->size; | |
1943 | ||
1944 | if (size & ARRAY_MARK_FLAG) break; /* Already marked */ | |
1945 | ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */ | |
1946 | ||
1947 | mark_object (&ptr->name); | |
894a9d16 | 1948 | mark_object (&ptr->icon_name); |
aba6deb8 | 1949 | mark_object (&ptr->title); |
169ee243 RS |
1950 | mark_object (&ptr->focus_frame); |
1951 | mark_object (&ptr->selected_window); | |
1952 | mark_object (&ptr->minibuffer_window); | |
1953 | mark_object (&ptr->param_alist); | |
1954 | mark_object (&ptr->scroll_bars); | |
1955 | mark_object (&ptr->condemned_scroll_bars); | |
1956 | mark_object (&ptr->menu_bar_items); | |
1957 | mark_object (&ptr->face_alist); | |
1958 | mark_object (&ptr->menu_bar_vector); | |
1959 | mark_object (&ptr->buffer_predicate); | |
a0e1f185 | 1960 | mark_object (&ptr->buffer_list); |
169ee243 | 1961 | } |
7b07587b | 1962 | else if (GC_BOOL_VECTOR_P (obj)) |
707788bd RS |
1963 | { |
1964 | register struct Lisp_Vector *ptr = XVECTOR (obj); | |
1965 | ||
1966 | if (ptr->size & ARRAY_MARK_FLAG) | |
1967 | break; /* Already marked */ | |
1968 | ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */ | |
1969 | } | |
04ff9756 | 1970 | else |
169ee243 RS |
1971 | { |
1972 | register struct Lisp_Vector *ptr = XVECTOR (obj); | |
1973 | register EMACS_INT size = ptr->size; | |
1974 | /* The reason we use ptr1 is to avoid an apparent hardware bug | |
1975 | that happens occasionally on the FSF's HP 300s. | |
1976 | The bug is that a2 gets clobbered by recursive calls to mark_object. | |
1977 | The clobberage seems to happen during function entry, | |
1978 | perhaps in the moveml instruction. | |
1979 | Yes, this is a crock, but we have to do it. */ | |
1980 | struct Lisp_Vector *volatile ptr1 = ptr; | |
1981 | register int i; | |
1982 | ||
1983 | if (size & ARRAY_MARK_FLAG) break; /* Already marked */ | |
1984 | ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */ | |
1985 | if (size & PSEUDOVECTOR_FLAG) | |
1986 | size &= PSEUDOVECTOR_SIZE_MASK; | |
1987 | for (i = 0; i < size; i++) /* and then mark its elements */ | |
1988 | mark_object (&ptr1->contents[i]); | |
1989 | } | |
1990 | break; | |
7146af97 | 1991 | |
7146af97 JB |
1992 | case Lisp_Symbol: |
1993 | { | |
41f54422 RS |
1994 | /* See comment above under Lisp_Vector for why this is volatile. */ |
1995 | register struct Lisp_Symbol *volatile ptr = XSYMBOL (obj); | |
7146af97 JB |
1996 | struct Lisp_Symbol *ptrx; |
1997 | ||
1998 | if (XMARKBIT (ptr->plist)) break; | |
1999 | XMARK (ptr->plist); | |
7146af97 JB |
2000 | mark_object ((Lisp_Object *) &ptr->value); |
2001 | mark_object (&ptr->function); | |
2002 | mark_object (&ptr->plist); | |
8aaa7c8a JB |
2003 | XSETTYPE (*(Lisp_Object *) &ptr->name, Lisp_String); |
2004 | mark_object (&ptr->name); | |
7146af97 JB |
2005 | ptr = ptr->next; |
2006 | if (ptr) | |
2007 | { | |
9149e743 KH |
2008 | /* For the benefit of the last_marked log. */ |
2009 | objptr = (Lisp_Object *)&XSYMBOL (obj)->next; | |
b0846f52 | 2010 | ptrx = ptr; /* Use of ptrx avoids compiler bug on Sun */ |
7146af97 | 2011 | XSETSYMBOL (obj, ptrx); |
9149e743 KH |
2012 | /* We can't goto loop here because *objptr doesn't contain an |
2013 | actual Lisp_Object with valid datatype field. */ | |
2014 | goto loop2; | |
7146af97 JB |
2015 | } |
2016 | } | |
2017 | break; | |
2018 | ||
a0a38eb7 | 2019 | case Lisp_Misc: |
a5da44fe | 2020 | switch (XMISCTYPE (obj)) |
a0a38eb7 KH |
2021 | { |
2022 | case Lisp_Misc_Marker: | |
2023 | XMARK (XMARKER (obj)->chain); | |
2024 | /* DO NOT mark thru the marker's chain. | |
2025 | The buffer's markers chain does not preserve markers from gc; | |
2026 | instead, markers are removed from the chain when freed by gc. */ | |
2027 | break; | |
2028 | ||
465edf35 KH |
2029 | case Lisp_Misc_Buffer_Local_Value: |
2030 | case Lisp_Misc_Some_Buffer_Local_Value: | |
2031 | { | |
2032 | register struct Lisp_Buffer_Local_Value *ptr | |
2033 | = XBUFFER_LOCAL_VALUE (obj); | |
2034 | if (XMARKBIT (ptr->car)) break; | |
2035 | XMARK (ptr->car); | |
2036 | /* If the cdr is nil, avoid recursion for the car. */ | |
2037 | if (EQ (ptr->cdr, Qnil)) | |
2038 | { | |
2039 | objptr = &ptr->car; | |
2040 | goto loop; | |
2041 | } | |
2042 | mark_object (&ptr->car); | |
2043 | /* See comment above under Lisp_Vector for why not use ptr here. */ | |
2044 | objptr = &XBUFFER_LOCAL_VALUE (obj)->cdr; | |
2045 | goto loop; | |
2046 | } | |
2047 | ||
c8616056 KH |
2048 | case Lisp_Misc_Intfwd: |
2049 | case Lisp_Misc_Boolfwd: | |
2050 | case Lisp_Misc_Objfwd: | |
2051 | case Lisp_Misc_Buffer_Objfwd: | |
b875d3f7 | 2052 | case Lisp_Misc_Kboard_Objfwd: |
c8616056 KH |
2053 | /* Don't bother with Lisp_Buffer_Objfwd, |
2054 | since all markable slots in current buffer marked anyway. */ | |
2055 | /* Don't need to do Lisp_Objfwd, since the places they point | |
2056 | are protected with staticpro. */ | |
2057 | break; | |
2058 | ||
e202fa34 KH |
2059 | case Lisp_Misc_Overlay: |
2060 | { | |
2061 | struct Lisp_Overlay *ptr = XOVERLAY (obj); | |
2062 | if (!XMARKBIT (ptr->plist)) | |
2063 | { | |
2064 | XMARK (ptr->plist); | |
2065 | mark_object (&ptr->start); | |
2066 | mark_object (&ptr->end); | |
2067 | objptr = &ptr->plist; | |
2068 | goto loop; | |
2069 | } | |
2070 | } | |
2071 | break; | |
2072 | ||
a0a38eb7 KH |
2073 | default: |
2074 | abort (); | |
2075 | } | |
7146af97 JB |
2076 | break; |
2077 | ||
2078 | case Lisp_Cons: | |
7146af97 JB |
2079 | { |
2080 | register struct Lisp_Cons *ptr = XCONS (obj); | |
2081 | if (XMARKBIT (ptr->car)) break; | |
2082 | XMARK (ptr->car); | |
c54ca951 RS |
2083 | /* If the cdr is nil, avoid recursion for the car. */ |
2084 | if (EQ (ptr->cdr, Qnil)) | |
2085 | { | |
2086 | objptr = &ptr->car; | |
c54ca951 RS |
2087 | goto loop; |
2088 | } | |
7146af97 | 2089 | mark_object (&ptr->car); |
41f54422 RS |
2090 | /* See comment above under Lisp_Vector for why not use ptr here. */ |
2091 | objptr = &XCONS (obj)->cdr; | |
7146af97 JB |
2092 | goto loop; |
2093 | } | |
2094 | ||
2095 | #ifdef LISP_FLOAT_TYPE | |
2096 | case Lisp_Float: | |
2097 | XMARK (XFLOAT (obj)->type); | |
2098 | break; | |
2099 | #endif /* LISP_FLOAT_TYPE */ | |
2100 | ||
7146af97 | 2101 | case Lisp_Int: |
7146af97 JB |
2102 | break; |
2103 | ||
2104 | default: | |
2105 | abort (); | |
2106 | } | |
2107 | } | |
2108 | ||
2109 | /* Mark the pointers in a buffer structure. */ | |
2110 | ||
2111 | static void | |
2112 | mark_buffer (buf) | |
2113 | Lisp_Object buf; | |
2114 | { | |
7146af97 JB |
2115 | register struct buffer *buffer = XBUFFER (buf); |
2116 | register Lisp_Object *ptr; | |
30e3190a | 2117 | Lisp_Object base_buffer; |
7146af97 JB |
2118 | |
2119 | /* This is the buffer's markbit */ | |
2120 | mark_object (&buffer->name); | |
2121 | XMARK (buffer->name); | |
2122 | ||
30e3190a | 2123 | MARK_INTERVAL_TREE (BUF_INTERVALS (buffer)); |
d5e35230 | 2124 | |
7146af97 JB |
2125 | #if 0 |
2126 | mark_object (buffer->syntax_table); | |
2127 | ||
2128 | /* Mark the various string-pointers in the buffer object. | |
2129 | Since the strings may be relocated, we must mark them | |
2130 | in their actual slots. So gc_sweep must convert each slot | |
2131 | back to an ordinary C pointer. */ | |
45d12a89 | 2132 | XSETSTRING (*(Lisp_Object *)&buffer->upcase_table, buffer->upcase_table); |
7146af97 | 2133 | mark_object ((Lisp_Object *)&buffer->upcase_table); |
45d12a89 | 2134 | XSETSTRING (*(Lisp_Object *)&buffer->downcase_table, buffer->downcase_table); |
7146af97 JB |
2135 | mark_object ((Lisp_Object *)&buffer->downcase_table); |
2136 | ||
45d12a89 | 2137 | XSETSTRING (*(Lisp_Object *)&buffer->sort_table, buffer->sort_table); |
7146af97 | 2138 | mark_object ((Lisp_Object *)&buffer->sort_table); |
45d12a89 | 2139 | XSETSTRING (*(Lisp_Object *)&buffer->folding_sort_table, buffer->folding_sort_table); |
7146af97 JB |
2140 | mark_object ((Lisp_Object *)&buffer->folding_sort_table); |
2141 | #endif | |
2142 | ||
2143 | for (ptr = &buffer->name + 1; | |
2144 | (char *)ptr < (char *)buffer + sizeof (struct buffer); | |
2145 | ptr++) | |
2146 | mark_object (ptr); | |
30e3190a RS |
2147 | |
2148 | /* If this is an indirect buffer, mark its base buffer. */ | |
6b552283 | 2149 | if (buffer->base_buffer && !XMARKBIT (buffer->base_buffer->name)) |
30e3190a RS |
2150 | { |
2151 | XSETBUFFER (base_buffer, buffer->base_buffer); | |
2152 | mark_buffer (base_buffer); | |
2153 | } | |
7146af97 | 2154 | } |
084b1a0c KH |
2155 | |
2156 | ||
b875d3f7 | 2157 | /* Mark the pointers in the kboard objects. */ |
084b1a0c KH |
2158 | |
2159 | static void | |
b875d3f7 | 2160 | mark_kboards () |
084b1a0c | 2161 | { |
b875d3f7 | 2162 | KBOARD *kb; |
b94daf1e | 2163 | Lisp_Object *p; |
b875d3f7 | 2164 | for (kb = all_kboards; kb; kb = kb->next_kboard) |
084b1a0c | 2165 | { |
b94daf1e KH |
2166 | if (kb->kbd_macro_buffer) |
2167 | for (p = kb->kbd_macro_buffer; p < kb->kbd_macro_ptr; p++) | |
2168 | mark_object (p); | |
9671abc2 | 2169 | mark_object (&kb->Vprefix_arg); |
b875d3f7 KH |
2170 | mark_object (&kb->kbd_queue); |
2171 | mark_object (&kb->Vlast_kbd_macro); | |
b94daf1e | 2172 | mark_object (&kb->Vsystem_key_alist); |
6d03a6fd | 2173 | mark_object (&kb->system_key_syms); |
084b1a0c KH |
2174 | } |
2175 | } | |
7146af97 | 2176 | \f |
1a4f1e2c | 2177 | /* Sweep: find all structures not marked, and free them. */ |
7146af97 JB |
2178 | |
2179 | static void | |
2180 | gc_sweep () | |
2181 | { | |
2182 | total_string_size = 0; | |
2183 | compact_strings (); | |
2184 | ||
2185 | /* Put all unmarked conses on free list */ | |
2186 | { | |
2187 | register struct cons_block *cblk; | |
6ca94ac9 | 2188 | struct cons_block **cprev = &cons_block; |
7146af97 JB |
2189 | register int lim = cons_block_index; |
2190 | register int num_free = 0, num_used = 0; | |
2191 | ||
2192 | cons_free_list = 0; | |
2193 | ||
6ca94ac9 | 2194 | for (cblk = cons_block; cblk; cblk = *cprev) |
7146af97 JB |
2195 | { |
2196 | register int i; | |
6ca94ac9 | 2197 | int this_free = 0; |
7146af97 JB |
2198 | for (i = 0; i < lim; i++) |
2199 | if (!XMARKBIT (cblk->conses[i].car)) | |
2200 | { | |
7146af97 | 2201 | num_free++; |
6ca94ac9 | 2202 | this_free++; |
1cd5fe6a | 2203 | *(struct Lisp_Cons **)&cblk->conses[i].cdr = cons_free_list; |
7146af97 JB |
2204 | cons_free_list = &cblk->conses[i]; |
2205 | } | |
2206 | else | |
2207 | { | |
2208 | num_used++; | |
2209 | XUNMARK (cblk->conses[i].car); | |
2210 | } | |
2211 | lim = CONS_BLOCK_SIZE; | |
6ca94ac9 KH |
2212 | /* If this block contains only free conses and we have already |
2213 | seen more than two blocks worth of free conses then deallocate | |
2214 | this block. */ | |
2215 | if (this_free == CONS_BLOCK_SIZE && num_free > 2*CONS_BLOCK_SIZE) | |
2216 | { | |
2217 | num_free -= CONS_BLOCK_SIZE; | |
2218 | *cprev = cblk->next; | |
2219 | /* Unhook from the free list. */ | |
2220 | cons_free_list = *(struct Lisp_Cons **) &cblk->conses[0].cdr; | |
2221 | xfree (cblk); | |
2222 | } | |
2223 | else | |
2224 | cprev = &cblk->next; | |
7146af97 JB |
2225 | } |
2226 | total_conses = num_used; | |
2227 | total_free_conses = num_free; | |
2228 | } | |
2229 | ||
2230 | #ifdef LISP_FLOAT_TYPE | |
2231 | /* Put all unmarked floats on free list */ | |
2232 | { | |
2233 | register struct float_block *fblk; | |
6ca94ac9 | 2234 | struct float_block **fprev = &float_block; |
7146af97 JB |
2235 | register int lim = float_block_index; |
2236 | register int num_free = 0, num_used = 0; | |
2237 | ||
2238 | float_free_list = 0; | |
2239 | ||
6ca94ac9 | 2240 | for (fblk = float_block; fblk; fblk = *fprev) |
7146af97 JB |
2241 | { |
2242 | register int i; | |
6ca94ac9 | 2243 | int this_free = 0; |
7146af97 JB |
2244 | for (i = 0; i < lim; i++) |
2245 | if (!XMARKBIT (fblk->floats[i].type)) | |
2246 | { | |
7146af97 | 2247 | num_free++; |
6ca94ac9 | 2248 | this_free++; |
1cd5fe6a | 2249 | *(struct Lisp_Float **)&fblk->floats[i].data = float_free_list; |
7146af97 JB |
2250 | float_free_list = &fblk->floats[i]; |
2251 | } | |
2252 | else | |
2253 | { | |
2254 | num_used++; | |
2255 | XUNMARK (fblk->floats[i].type); | |
2256 | } | |
2257 | lim = FLOAT_BLOCK_SIZE; | |
6ca94ac9 KH |
2258 | /* If this block contains only free floats and we have already |
2259 | seen more than two blocks worth of free floats then deallocate | |
2260 | this block. */ | |
2261 | if (this_free == FLOAT_BLOCK_SIZE && num_free > 2*FLOAT_BLOCK_SIZE) | |
2262 | { | |
2263 | num_free -= FLOAT_BLOCK_SIZE; | |
2264 | *fprev = fblk->next; | |
2265 | /* Unhook from the free list. */ | |
2266 | float_free_list = *(struct Lisp_Float **) &fblk->floats[0].data; | |
2267 | xfree (fblk); | |
2268 | } | |
2269 | else | |
2270 | fprev = &fblk->next; | |
7146af97 JB |
2271 | } |
2272 | total_floats = num_used; | |
2273 | total_free_floats = num_free; | |
2274 | } | |
2275 | #endif /* LISP_FLOAT_TYPE */ | |
2276 | ||
d5e35230 JA |
2277 | #ifdef USE_TEXT_PROPERTIES |
2278 | /* Put all unmarked intervals on free list */ | |
2279 | { | |
2280 | register struct interval_block *iblk; | |
6ca94ac9 | 2281 | struct interval_block **iprev = &interval_block; |
d5e35230 JA |
2282 | register int lim = interval_block_index; |
2283 | register int num_free = 0, num_used = 0; | |
2284 | ||
2285 | interval_free_list = 0; | |
2286 | ||
6ca94ac9 | 2287 | for (iblk = interval_block; iblk; iblk = *iprev) |
d5e35230 JA |
2288 | { |
2289 | register int i; | |
6ca94ac9 | 2290 | int this_free = 0; |
d5e35230 JA |
2291 | |
2292 | for (i = 0; i < lim; i++) | |
2293 | { | |
2294 | if (! XMARKBIT (iblk->intervals[i].plist)) | |
2295 | { | |
2296 | iblk->intervals[i].parent = interval_free_list; | |
2297 | interval_free_list = &iblk->intervals[i]; | |
2298 | num_free++; | |
6ca94ac9 | 2299 | this_free++; |
d5e35230 JA |
2300 | } |
2301 | else | |
2302 | { | |
2303 | num_used++; | |
2304 | XUNMARK (iblk->intervals[i].plist); | |
2305 | } | |
2306 | } | |
2307 | lim = INTERVAL_BLOCK_SIZE; | |
6ca94ac9 KH |
2308 | /* If this block contains only free intervals and we have already |
2309 | seen more than two blocks worth of free intervals then | |
2310 | deallocate this block. */ | |
2311 | if (this_free == INTERVAL_BLOCK_SIZE | |
2312 | && num_free > 2*INTERVAL_BLOCK_SIZE) | |
2313 | { | |
2314 | num_free -= INTERVAL_BLOCK_SIZE; | |
2315 | *iprev = iblk->next; | |
2316 | /* Unhook from the free list. */ | |
2317 | interval_free_list = iblk->intervals[0].parent; | |
2318 | xfree (iblk); | |
2319 | } | |
2320 | else | |
2321 | iprev = &iblk->next; | |
d5e35230 JA |
2322 | } |
2323 | total_intervals = num_used; | |
2324 | total_free_intervals = num_free; | |
2325 | } | |
2326 | #endif /* USE_TEXT_PROPERTIES */ | |
2327 | ||
7146af97 JB |
2328 | /* Put all unmarked symbols on free list */ |
2329 | { | |
2330 | register struct symbol_block *sblk; | |
6ca94ac9 | 2331 | struct symbol_block **sprev = &symbol_block; |
7146af97 JB |
2332 | register int lim = symbol_block_index; |
2333 | register int num_free = 0, num_used = 0; | |
2334 | ||
2335 | symbol_free_list = 0; | |
2336 | ||
6ca94ac9 | 2337 | for (sblk = symbol_block; sblk; sblk = *sprev) |
7146af97 JB |
2338 | { |
2339 | register int i; | |
6ca94ac9 | 2340 | int this_free = 0; |
7146af97 JB |
2341 | for (i = 0; i < lim; i++) |
2342 | if (!XMARKBIT (sblk->symbols[i].plist)) | |
2343 | { | |
85481507 | 2344 | *(struct Lisp_Symbol **)&sblk->symbols[i].value = symbol_free_list; |
7146af97 JB |
2345 | symbol_free_list = &sblk->symbols[i]; |
2346 | num_free++; | |
6ca94ac9 | 2347 | this_free++; |
7146af97 JB |
2348 | } |
2349 | else | |
2350 | { | |
2351 | num_used++; | |
2352 | sblk->symbols[i].name | |
2353 | = XSTRING (*(Lisp_Object *) &sblk->symbols[i].name); | |
2354 | XUNMARK (sblk->symbols[i].plist); | |
2355 | } | |
2356 | lim = SYMBOL_BLOCK_SIZE; | |
6ca94ac9 KH |
2357 | /* If this block contains only free symbols and we have already |
2358 | seen more than two blocks worth of free symbols then deallocate | |
2359 | this block. */ | |
2360 | if (this_free == SYMBOL_BLOCK_SIZE && num_free > 2*SYMBOL_BLOCK_SIZE) | |
2361 | { | |
2362 | num_free -= SYMBOL_BLOCK_SIZE; | |
2363 | *sprev = sblk->next; | |
2364 | /* Unhook from the free list. */ | |
2365 | symbol_free_list = *(struct Lisp_Symbol **)&sblk->symbols[0].value; | |
2366 | xfree (sblk); | |
2367 | } | |
2368 | else | |
2369 | sprev = &sblk->next; | |
7146af97 JB |
2370 | } |
2371 | total_symbols = num_used; | |
2372 | total_free_symbols = num_free; | |
2373 | } | |
2374 | ||
2375 | #ifndef standalone | |
2376 | /* Put all unmarked markers on free list. | |
8e6208c5 | 2377 | Unchain each one first from the buffer it points into, |
465edf35 | 2378 | but only if it's a real marker. */ |
7146af97 JB |
2379 | { |
2380 | register struct marker_block *mblk; | |
6ca94ac9 | 2381 | struct marker_block **mprev = &marker_block; |
7146af97 JB |
2382 | register int lim = marker_block_index; |
2383 | register int num_free = 0, num_used = 0; | |
2384 | ||
2385 | marker_free_list = 0; | |
2386 | ||
6ca94ac9 | 2387 | for (mblk = marker_block; mblk; mblk = *mprev) |
7146af97 JB |
2388 | { |
2389 | register int i; | |
6ca94ac9 | 2390 | int this_free = 0; |
26b926e1 | 2391 | EMACS_INT already_free = -1; |
fa05e253 | 2392 | |
7146af97 | 2393 | for (i = 0; i < lim; i++) |
465edf35 KH |
2394 | { |
2395 | Lisp_Object *markword; | |
a5da44fe | 2396 | switch (mblk->markers[i].u_marker.type) |
465edf35 KH |
2397 | { |
2398 | case Lisp_Misc_Marker: | |
2399 | markword = &mblk->markers[i].u_marker.chain; | |
2400 | break; | |
2401 | case Lisp_Misc_Buffer_Local_Value: | |
2402 | case Lisp_Misc_Some_Buffer_Local_Value: | |
2403 | markword = &mblk->markers[i].u_buffer_local_value.car; | |
2404 | break; | |
e202fa34 KH |
2405 | case Lisp_Misc_Overlay: |
2406 | markword = &mblk->markers[i].u_overlay.plist; | |
2407 | break; | |
fa05e253 RS |
2408 | case Lisp_Misc_Free: |
2409 | /* If the object was already free, keep it | |
2410 | on the free list. */ | |
74d84334 | 2411 | markword = (Lisp_Object *) &already_free; |
fa05e253 | 2412 | break; |
465edf35 KH |
2413 | default: |
2414 | markword = 0; | |
e202fa34 | 2415 | break; |
465edf35 KH |
2416 | } |
2417 | if (markword && !XMARKBIT (*markword)) | |
2418 | { | |
2419 | Lisp_Object tem; | |
a5da44fe | 2420 | if (mblk->markers[i].u_marker.type == Lisp_Misc_Marker) |
465edf35 KH |
2421 | { |
2422 | /* tem1 avoids Sun compiler bug */ | |
2423 | struct Lisp_Marker *tem1 = &mblk->markers[i].u_marker; | |
2424 | XSETMARKER (tem, tem1); | |
2425 | unchain_marker (tem); | |
2426 | } | |
fa05e253 RS |
2427 | /* Set the type of the freed object to Lisp_Misc_Free. |
2428 | We could leave the type alone, since nobody checks it, | |
465edf35 | 2429 | but this might catch bugs faster. */ |
a5da44fe | 2430 | mblk->markers[i].u_marker.type = Lisp_Misc_Free; |
465edf35 KH |
2431 | mblk->markers[i].u_free.chain = marker_free_list; |
2432 | marker_free_list = &mblk->markers[i]; | |
2433 | num_free++; | |
6ca94ac9 | 2434 | this_free++; |
465edf35 KH |
2435 | } |
2436 | else | |
2437 | { | |
2438 | num_used++; | |
2439 | if (markword) | |
2440 | XUNMARK (*markword); | |
2441 | } | |
2442 | } | |
7146af97 | 2443 | lim = MARKER_BLOCK_SIZE; |
6ca94ac9 KH |
2444 | /* If this block contains only free markers and we have already |
2445 | seen more than two blocks worth of free markers then deallocate | |
2446 | this block. */ | |
2447 | if (this_free == MARKER_BLOCK_SIZE && num_free > 2*MARKER_BLOCK_SIZE) | |
2448 | { | |
2449 | num_free -= MARKER_BLOCK_SIZE; | |
2450 | *mprev = mblk->next; | |
2451 | /* Unhook from the free list. */ | |
2452 | marker_free_list = mblk->markers[0].u_free.chain; | |
2453 | xfree (mblk); | |
2454 | } | |
2455 | else | |
2456 | mprev = &mblk->next; | |
7146af97 JB |
2457 | } |
2458 | ||
2459 | total_markers = num_used; | |
2460 | total_free_markers = num_free; | |
2461 | } | |
2462 | ||
2463 | /* Free all unmarked buffers */ | |
2464 | { | |
2465 | register struct buffer *buffer = all_buffers, *prev = 0, *next; | |
2466 | ||
2467 | while (buffer) | |
2468 | if (!XMARKBIT (buffer->name)) | |
2469 | { | |
2470 | if (prev) | |
2471 | prev->next = buffer->next; | |
2472 | else | |
2473 | all_buffers = buffer->next; | |
2474 | next = buffer->next; | |
9ac0d9e0 | 2475 | xfree (buffer); |
7146af97 JB |
2476 | buffer = next; |
2477 | } | |
2478 | else | |
2479 | { | |
2480 | XUNMARK (buffer->name); | |
30e3190a | 2481 | UNMARK_BALANCE_INTERVALS (BUF_INTERVALS (buffer)); |
7146af97 JB |
2482 | |
2483 | #if 0 | |
2484 | /* Each `struct Lisp_String *' was turned into a Lisp_Object | |
2485 | for purposes of marking and relocation. | |
2486 | Turn them back into C pointers now. */ | |
2487 | buffer->upcase_table | |
2488 | = XSTRING (*(Lisp_Object *)&buffer->upcase_table); | |
2489 | buffer->downcase_table | |
2490 | = XSTRING (*(Lisp_Object *)&buffer->downcase_table); | |
2491 | buffer->sort_table | |
2492 | = XSTRING (*(Lisp_Object *)&buffer->sort_table); | |
2493 | buffer->folding_sort_table | |
2494 | = XSTRING (*(Lisp_Object *)&buffer->folding_sort_table); | |
2495 | #endif | |
2496 | ||
2497 | prev = buffer, buffer = buffer->next; | |
2498 | } | |
2499 | } | |
2500 | ||
2501 | #endif /* standalone */ | |
2502 | ||
2503 | /* Free all unmarked vectors */ | |
2504 | { | |
2505 | register struct Lisp_Vector *vector = all_vectors, *prev = 0, *next; | |
2506 | total_vector_size = 0; | |
2507 | ||
2508 | while (vector) | |
2509 | if (!(vector->size & ARRAY_MARK_FLAG)) | |
2510 | { | |
2511 | if (prev) | |
2512 | prev->next = vector->next; | |
2513 | else | |
2514 | all_vectors = vector->next; | |
2515 | next = vector->next; | |
9ac0d9e0 | 2516 | xfree (vector); |
7146af97 JB |
2517 | vector = next; |
2518 | } | |
2519 | else | |
2520 | { | |
2521 | vector->size &= ~ARRAY_MARK_FLAG; | |
fa05e253 RS |
2522 | if (vector->size & PSEUDOVECTOR_FLAG) |
2523 | total_vector_size += (PSEUDOVECTOR_SIZE_MASK & vector->size); | |
2524 | else | |
2525 | total_vector_size += vector->size; | |
7146af97 JB |
2526 | prev = vector, vector = vector->next; |
2527 | } | |
2528 | } | |
2529 | ||
2530 | /* Free all "large strings" not marked with ARRAY_MARK_FLAG. */ | |
2531 | { | |
2532 | register struct string_block *sb = large_string_blocks, *prev = 0, *next; | |
e8720644 | 2533 | struct Lisp_String *s; |
7146af97 JB |
2534 | |
2535 | while (sb) | |
e8720644 JB |
2536 | { |
2537 | s = (struct Lisp_String *) &sb->chars[0]; | |
2538 | if (s->size & ARRAY_MARK_FLAG) | |
2539 | { | |
2540 | ((struct Lisp_String *)(&sb->chars[0]))->size | |
1fb577f7 | 2541 | &= ~ARRAY_MARK_FLAG & ~MARKBIT; |
e8720644 JB |
2542 | UNMARK_BALANCE_INTERVALS (s->intervals); |
2543 | total_string_size += ((struct Lisp_String *)(&sb->chars[0]))->size; | |
2544 | prev = sb, sb = sb->next; | |
2545 | } | |
2546 | else | |
2547 | { | |
2548 | if (prev) | |
2549 | prev->next = sb->next; | |
2550 | else | |
2551 | large_string_blocks = sb->next; | |
2552 | next = sb->next; | |
2553 | xfree (sb); | |
2554 | sb = next; | |
2555 | } | |
2556 | } | |
7146af97 JB |
2557 | } |
2558 | } | |
2559 | \f | |
1a4f1e2c | 2560 | /* Compactify strings, relocate references, and free empty string blocks. */ |
7146af97 JB |
2561 | |
2562 | static void | |
2563 | compact_strings () | |
2564 | { | |
2565 | /* String block of old strings we are scanning. */ | |
2566 | register struct string_block *from_sb; | |
2567 | /* A preceding string block (or maybe the same one) | |
2568 | where we are copying the still-live strings to. */ | |
2569 | register struct string_block *to_sb; | |
2570 | int pos; | |
2571 | int to_pos; | |
2572 | ||
2573 | to_sb = first_string_block; | |
2574 | to_pos = 0; | |
2575 | ||
2576 | /* Scan each existing string block sequentially, string by string. */ | |
2577 | for (from_sb = first_string_block; from_sb; from_sb = from_sb->next) | |
2578 | { | |
2579 | pos = 0; | |
2580 | /* POS is the index of the next string in the block. */ | |
2581 | while (pos < from_sb->pos) | |
2582 | { | |
2583 | register struct Lisp_String *nextstr | |
2584 | = (struct Lisp_String *) &from_sb->chars[pos]; | |
2585 | ||
2586 | register struct Lisp_String *newaddr; | |
42607681 | 2587 | register EMACS_INT size = nextstr->size; |
3f25e183 | 2588 | EMACS_INT size_byte = nextstr->size_byte; |
7146af97 JB |
2589 | |
2590 | /* NEXTSTR is the old address of the next string. | |
2591 | Just skip it if it isn't marked. */ | |
155ffe9c | 2592 | if (((EMACS_UINT) size & ~DONT_COPY_FLAG) > STRING_BLOCK_SIZE) |
7146af97 JB |
2593 | { |
2594 | /* It is marked, so its size field is really a chain of refs. | |
2595 | Find the end of the chain, where the actual size lives. */ | |
155ffe9c | 2596 | while (((EMACS_UINT) size & ~DONT_COPY_FLAG) > STRING_BLOCK_SIZE) |
7146af97 | 2597 | { |
155ffe9c RS |
2598 | if (size & DONT_COPY_FLAG) |
2599 | size ^= MARKBIT | DONT_COPY_FLAG; | |
42607681 | 2600 | size = *(EMACS_INT *)size & ~MARKBIT; |
7146af97 JB |
2601 | } |
2602 | ||
3f25e183 | 2603 | total_string_size += size_byte; |
7146af97 JB |
2604 | |
2605 | /* If it won't fit in TO_SB, close it out, | |
2606 | and move to the next sb. Keep doing so until | |
2607 | TO_SB reaches a large enough, empty enough string block. | |
2608 | We know that TO_SB cannot advance past FROM_SB here | |
2609 | since FROM_SB is large enough to contain this string. | |
2610 | Any string blocks skipped here | |
2611 | will be patched out and freed later. */ | |
3f25e183 | 2612 | while (to_pos + STRING_FULLSIZE (size_byte) |
7146af97 JB |
2613 | > max (to_sb->pos, STRING_BLOCK_SIZE)) |
2614 | { | |
2615 | to_sb->pos = to_pos; | |
2616 | to_sb = to_sb->next; | |
2617 | to_pos = 0; | |
2618 | } | |
2619 | /* Compute new address of this string | |
2620 | and update TO_POS for the space being used. */ | |
2621 | newaddr = (struct Lisp_String *) &to_sb->chars[to_pos]; | |
3f25e183 | 2622 | to_pos += STRING_FULLSIZE (size_byte); |
7146af97 JB |
2623 | |
2624 | /* Copy the string itself to the new place. */ | |
2625 | if (nextstr != newaddr) | |
3f25e183 | 2626 | bcopy (nextstr, newaddr, STRING_FULLSIZE (size_byte)); |
7146af97 JB |
2627 | |
2628 | /* Go through NEXTSTR's chain of references | |
2629 | and make each slot in the chain point to | |
2630 | the new address of this string. */ | |
2631 | size = newaddr->size; | |
155ffe9c | 2632 | while (((EMACS_UINT) size & ~DONT_COPY_FLAG) > STRING_BLOCK_SIZE) |
7146af97 JB |
2633 | { |
2634 | register Lisp_Object *objptr; | |
155ffe9c RS |
2635 | if (size & DONT_COPY_FLAG) |
2636 | size ^= MARKBIT | DONT_COPY_FLAG; | |
7146af97 JB |
2637 | objptr = (Lisp_Object *)size; |
2638 | ||
2639 | size = XFASTINT (*objptr) & ~MARKBIT; | |
2640 | if (XMARKBIT (*objptr)) | |
2641 | { | |
45d12a89 | 2642 | XSETSTRING (*objptr, newaddr); |
7146af97 JB |
2643 | XMARK (*objptr); |
2644 | } | |
2645 | else | |
45d12a89 | 2646 | XSETSTRING (*objptr, newaddr); |
7146af97 JB |
2647 | } |
2648 | /* Store the actual size in the size field. */ | |
2649 | newaddr->size = size; | |
e8720644 | 2650 | |
5f60ed47 | 2651 | #ifdef USE_TEXT_PROPERTIES |
e8720644 JB |
2652 | /* Now that the string has been relocated, rebalance its |
2653 | interval tree, and update the tree's parent pointer. */ | |
2654 | if (! NULL_INTERVAL_P (newaddr->intervals)) | |
2655 | { | |
2656 | UNMARK_BALANCE_INTERVALS (newaddr->intervals); | |
45d12a89 KH |
2657 | XSETSTRING (* (Lisp_Object *) &newaddr->intervals->parent, |
2658 | newaddr); | |
e8720644 | 2659 | } |
5f60ed47 | 2660 | #endif /* USE_TEXT_PROPERTIES */ |
7146af97 | 2661 | } |
3f25e183 | 2662 | pos += STRING_FULLSIZE (size_byte); |
7146af97 JB |
2663 | } |
2664 | } | |
2665 | ||
2666 | /* Close out the last string block still used and free any that follow. */ | |
2667 | to_sb->pos = to_pos; | |
2668 | current_string_block = to_sb; | |
2669 | ||
2670 | from_sb = to_sb->next; | |
2671 | to_sb->next = 0; | |
2672 | while (from_sb) | |
2673 | { | |
2674 | to_sb = from_sb->next; | |
9ac0d9e0 | 2675 | xfree (from_sb); |
7146af97 JB |
2676 | from_sb = to_sb; |
2677 | } | |
2678 | ||
2679 | /* Free any empty string blocks further back in the chain. | |
2680 | This loop will never free first_string_block, but it is very | |
2681 | unlikely that that one will become empty, so why bother checking? */ | |
2682 | ||
2683 | from_sb = first_string_block; | |
2684 | while (to_sb = from_sb->next) | |
2685 | { | |
2686 | if (to_sb->pos == 0) | |
2687 | { | |
2688 | if (from_sb->next = to_sb->next) | |
2689 | from_sb->next->prev = from_sb; | |
9ac0d9e0 | 2690 | xfree (to_sb); |
7146af97 JB |
2691 | } |
2692 | else | |
2693 | from_sb = to_sb; | |
2694 | } | |
2695 | } | |
2696 | \f | |
20d24714 JB |
2697 | /* Debugging aids. */ |
2698 | ||
31ce1c91 | 2699 | DEFUN ("memory-limit", Fmemory_limit, Smemory_limit, 0, 0, 0, |
20d24714 JB |
2700 | "Return the address of the last byte Emacs has allocated, divided by 1024.\n\ |
2701 | This may be helpful in debugging Emacs's memory usage.\n\ | |
e41ae81f | 2702 | We divide the value by 1024 to make sure it fits in a Lisp integer.") |
20d24714 JB |
2703 | () |
2704 | { | |
2705 | Lisp_Object end; | |
2706 | ||
45d12a89 | 2707 | XSETINT (end, (EMACS_INT) sbrk (0) / 1024); |
20d24714 JB |
2708 | |
2709 | return end; | |
2710 | } | |
2711 | ||
310ea200 RS |
2712 | DEFUN ("memory-use-counts", Fmemory_use_counts, Smemory_use_counts, 0, 0, 0, |
2713 | "Return a list of counters that measure how much consing there has been.\n\ | |
2714 | Each of these counters increments for a certain kind of object.\n\ | |
2715 | The counters wrap around from the largest positive integer to zero.\n\ | |
2716 | Garbage collection does not decrease them.\n\ | |
2717 | The elements of the value are as follows:\n\ | |
2718 | (CONSES FLOATS VECTOR-CELLS SYMBOLS STRING-CHARS MISCS INTERVALS)\n\ | |
2719 | All are in units of 1 = one object consed\n\ | |
2720 | except for VECTOR-CELLS and STRING-CHARS, which count the total length of\n\ | |
2721 | objects consed.\n\ | |
2722 | MISCS include overlays, markers, and some internal types.\n\ | |
2723 | Frames, windows, buffers, and subprocesses count as vectors\n\ | |
2724 | (but the contents of a buffer's text do not count here).") | |
2725 | () | |
2726 | { | |
2727 | Lisp_Object lisp_cons_cells_consed; | |
2728 | Lisp_Object lisp_floats_consed; | |
2729 | Lisp_Object lisp_vector_cells_consed; | |
2730 | Lisp_Object lisp_symbols_consed; | |
2731 | Lisp_Object lisp_string_chars_consed; | |
2732 | Lisp_Object lisp_misc_objects_consed; | |
2733 | Lisp_Object lisp_intervals_consed; | |
2734 | ||
2735 | XSETINT (lisp_cons_cells_consed, | |
290c8f1e | 2736 | cons_cells_consed & ~(((EMACS_INT) 1) << (VALBITS - 1))); |
310ea200 | 2737 | XSETINT (lisp_floats_consed, |
290c8f1e | 2738 | floats_consed & ~(((EMACS_INT) 1) << (VALBITS - 1))); |
310ea200 | 2739 | XSETINT (lisp_vector_cells_consed, |
290c8f1e | 2740 | vector_cells_consed & ~(((EMACS_INT) 1) << (VALBITS - 1))); |
310ea200 | 2741 | XSETINT (lisp_symbols_consed, |
290c8f1e | 2742 | symbols_consed & ~(((EMACS_INT) 1) << (VALBITS - 1))); |
310ea200 | 2743 | XSETINT (lisp_string_chars_consed, |
290c8f1e | 2744 | string_chars_consed & ~(((EMACS_INT) 1) << (VALBITS - 1))); |
310ea200 | 2745 | XSETINT (lisp_misc_objects_consed, |
290c8f1e | 2746 | misc_objects_consed & ~(((EMACS_INT) 1) << (VALBITS - 1))); |
310ea200 | 2747 | XSETINT (lisp_intervals_consed, |
290c8f1e | 2748 | intervals_consed & ~(((EMACS_INT) 1) << (VALBITS - 1))); |
310ea200 RS |
2749 | |
2750 | return Fcons (lisp_cons_cells_consed, | |
2751 | Fcons (lisp_floats_consed, | |
2752 | Fcons (lisp_vector_cells_consed, | |
2753 | Fcons (lisp_symbols_consed, | |
2754 | Fcons (lisp_string_chars_consed, | |
2755 | Fcons (lisp_misc_objects_consed, | |
2756 | Fcons (lisp_intervals_consed, | |
2757 | Qnil))))))); | |
2758 | } | |
20d24714 | 2759 | \f |
7146af97 JB |
2760 | /* Initialization */ |
2761 | ||
2762 | init_alloc_once () | |
2763 | { | |
2764 | /* Used to do Vpurify_flag = Qt here, but Qt isn't set up yet! */ | |
2765 | pureptr = 0; | |
4c0be5f4 JB |
2766 | #ifdef HAVE_SHM |
2767 | pure_size = PURESIZE; | |
2768 | #endif | |
7146af97 JB |
2769 | all_vectors = 0; |
2770 | ignore_warnings = 1; | |
d1658221 RS |
2771 | #ifdef DOUG_LEA_MALLOC |
2772 | mallopt (M_TRIM_THRESHOLD, 128*1024); /* trim threshold */ | |
2773 | mallopt (M_MMAP_THRESHOLD, 64*1024); /* mmap threshold */ | |
2774 | mallopt (M_MMAP_MAX, 64); /* max. number of mmap'ed areas */ | |
2775 | #endif | |
7146af97 JB |
2776 | init_strings (); |
2777 | init_cons (); | |
2778 | init_symbol (); | |
2779 | init_marker (); | |
2780 | #ifdef LISP_FLOAT_TYPE | |
2781 | init_float (); | |
2782 | #endif /* LISP_FLOAT_TYPE */ | |
d5e35230 JA |
2783 | INIT_INTERVALS; |
2784 | ||
276cbe5a RS |
2785 | #ifdef REL_ALLOC |
2786 | malloc_hysteresis = 32; | |
2787 | #else | |
2788 | malloc_hysteresis = 0; | |
2789 | #endif | |
2790 | ||
2791 | spare_memory = (char *) malloc (SPARE_MEMORY); | |
2792 | ||
7146af97 JB |
2793 | ignore_warnings = 0; |
2794 | gcprolist = 0; | |
2795 | staticidx = 0; | |
2796 | consing_since_gc = 0; | |
7d179cea | 2797 | gc_cons_threshold = 100000 * sizeof (Lisp_Object); |
7146af97 JB |
2798 | #ifdef VIRT_ADDR_VARIES |
2799 | malloc_sbrk_unused = 1<<22; /* A large number */ | |
2800 | malloc_sbrk_used = 100000; /* as reasonable as any number */ | |
2801 | #endif /* VIRT_ADDR_VARIES */ | |
2802 | } | |
2803 | ||
2804 | init_alloc () | |
2805 | { | |
2806 | gcprolist = 0; | |
2807 | } | |
2808 | ||
2809 | void | |
2810 | syms_of_alloc () | |
2811 | { | |
2812 | DEFVAR_INT ("gc-cons-threshold", &gc_cons_threshold, | |
2813 | "*Number of bytes of consing between garbage collections.\n\ | |
2814 | Garbage collection can happen automatically once this many bytes have been\n\ | |
2815 | allocated since the last garbage collection. All data types count.\n\n\ | |
2816 | Garbage collection happens automatically only when `eval' is called.\n\n\ | |
2817 | By binding this temporarily to a large number, you can effectively\n\ | |
2818 | prevent garbage collection during a part of the program."); | |
2819 | ||
2820 | DEFVAR_INT ("pure-bytes-used", &pureptr, | |
2821 | "Number of bytes of sharable Lisp data allocated so far."); | |
2822 | ||
0819585c RS |
2823 | DEFVAR_INT ("cons-cells-consed", &cons_cells_consed, |
2824 | "Number of cons cells that have been consed so far."); | |
2825 | ||
2826 | DEFVAR_INT ("floats-consed", &floats_consed, | |
2827 | "Number of floats that have been consed so far."); | |
2828 | ||
2829 | DEFVAR_INT ("vector-cells-consed", &vector_cells_consed, | |
2830 | "Number of vector cells that have been consed so far."); | |
2831 | ||
2832 | DEFVAR_INT ("symbols-consed", &symbols_consed, | |
2833 | "Number of symbols that have been consed so far."); | |
2834 | ||
2835 | DEFVAR_INT ("string-chars-consed", &string_chars_consed, | |
2836 | "Number of string characters that have been consed so far."); | |
2837 | ||
2838 | DEFVAR_INT ("misc-objects-consed", &misc_objects_consed, | |
2839 | "Number of miscellaneous objects that have been consed so far."); | |
2840 | ||
2841 | DEFVAR_INT ("intervals-consed", &intervals_consed, | |
2842 | "Number of intervals that have been consed so far."); | |
2843 | ||
7146af97 JB |
2844 | #if 0 |
2845 | DEFVAR_INT ("data-bytes-used", &malloc_sbrk_used, | |
2846 | "Number of bytes of unshared memory allocated in this session."); | |
2847 | ||
2848 | DEFVAR_INT ("data-bytes-free", &malloc_sbrk_unused, | |
2849 | "Number of bytes of unshared memory remaining available in this session."); | |
2850 | #endif | |
2851 | ||
2852 | DEFVAR_LISP ("purify-flag", &Vpurify_flag, | |
2853 | "Non-nil means loading Lisp code in order to dump an executable.\n\ | |
2854 | This means that certain objects should be allocated in shared (pure) space."); | |
2855 | ||
502b9b64 | 2856 | DEFVAR_INT ("undo-limit", &undo_limit, |
7146af97 | 2857 | "Keep no more undo information once it exceeds this size.\n\ |
502b9b64 | 2858 | This limit is applied when garbage collection happens.\n\ |
7146af97 JB |
2859 | The size is counted as the number of bytes occupied,\n\ |
2860 | which includes both saved text and other data."); | |
502b9b64 | 2861 | undo_limit = 20000; |
7146af97 | 2862 | |
502b9b64 | 2863 | DEFVAR_INT ("undo-strong-limit", &undo_strong_limit, |
7146af97 JB |
2864 | "Don't keep more than this much size of undo information.\n\ |
2865 | A command which pushes past this size is itself forgotten.\n\ | |
502b9b64 | 2866 | This limit is applied when garbage collection happens.\n\ |
7146af97 JB |
2867 | The size is counted as the number of bytes occupied,\n\ |
2868 | which includes both saved text and other data."); | |
502b9b64 | 2869 | undo_strong_limit = 30000; |
7146af97 | 2870 | |
299585ee RS |
2871 | DEFVAR_BOOL ("garbage-collection-messages", &garbage_collection_messages, |
2872 | "Non-nil means display messages at start and end of garbage collection."); | |
2873 | garbage_collection_messages = 0; | |
2874 | ||
bcb61d60 KH |
2875 | /* We build this in advance because if we wait until we need it, we might |
2876 | not be able to allocate the memory to hold it. */ | |
cf3540e4 | 2877 | memory_signal_data |
276cbe5a | 2878 | = Fcons (Qerror, Fcons (build_string ("Memory exhausted--use M-x save-some-buffers RET"), Qnil)); |
bcb61d60 KH |
2879 | staticpro (&memory_signal_data); |
2880 | ||
e8197642 RS |
2881 | staticpro (&Qgc_cons_threshold); |
2882 | Qgc_cons_threshold = intern ("gc-cons-threshold"); | |
2883 | ||
a59de17b RS |
2884 | staticpro (&Qchar_table_extra_slots); |
2885 | Qchar_table_extra_slots = intern ("char-table-extra-slots"); | |
2886 | ||
7146af97 JB |
2887 | defsubr (&Scons); |
2888 | defsubr (&Slist); | |
2889 | defsubr (&Svector); | |
2890 | defsubr (&Smake_byte_code); | |
2891 | defsubr (&Smake_list); | |
2892 | defsubr (&Smake_vector); | |
7b07587b | 2893 | defsubr (&Smake_char_table); |
7146af97 | 2894 | defsubr (&Smake_string); |
7b07587b | 2895 | defsubr (&Smake_bool_vector); |
7146af97 JB |
2896 | defsubr (&Smake_symbol); |
2897 | defsubr (&Smake_marker); | |
2898 | defsubr (&Spurecopy); | |
2899 | defsubr (&Sgarbage_collect); | |
20d24714 | 2900 | defsubr (&Smemory_limit); |
310ea200 | 2901 | defsubr (&Smemory_use_counts); |
7146af97 | 2902 | } |