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