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