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