Mercurial > hg > xemacs-beta
annotate src/alloc.c @ 2775:05d62157e048
[xemacs-hg @ 2005-05-15 16:37:52 by crestani]
New allocator improvements
lisp/ChangeLog addition:
2005-05-15 Marcus Crestani <crestani@xemacs.org>
* diagnose.el: Lrecord and string data statistics.
* diagnose.el (show-memory-usage): Add output for additional
lrecord statistics (currently only string data).
* diagnose.el (show-lrecord-stats): New. Print detailed lrecord
statistics.
src/ChangeLog addition:
2005-05-15 Marcus Crestani <crestani@xemacs.org>
* alloc.c: Add string data statistics.
* alloc.c (dec_lrecord_stats): Use size of lrecord for statistics
and cons counter bookkeeping.
* alloc.c (finalize_string): Add string data statistics.
* alloc.c (make_uninit_string): Add string data statistics.
* alloc.c (make_string_nocopy): Add string data statistics.
* alloc.c (kkcc_marking): Move break out of #ifdef.
* alloc.c (Flrecord_stats): New. Collect lrecord statistics.
* alloc.c (Fgarbage_collect): Use Flrecord_stats.
* alloc.c (syms_of_alloc): Add Flrecord_stats.
* dumper.c: Fix hash table.
* dumper.c (pdump_make_hash): Fix hash table.
* dumper.c (pdump_get_mc_addr): Fix hash table.
* dumper.c (pdump_put_mc_addr): Fix hash table.
* dumper.c (pdump_reloc_one_mc): Fix indentation.
* dumper.c (pdump_load_finish): Add lrecord statistics
bookkeeping.
* lrecord.h: Add string data statistics.
* mc-alloc.c (remove_cell): Lrecord statistics, fix indentation.
* mule-charset.c: Marking through *_unicode_description not
needed.
* symbols.c (init_symbols_once_early): Bump lrecord statistics.
* window.c: Marking through line_start_cache not needed.
* xemacs.def.in.in: Fix typo.
author | crestani |
---|---|
date | Sun, 15 May 2005 16:38:14 +0000 |
parents | 6fa9919a9a0b |
children | adda8fccb13d |
rev | line source |
---|---|
428 | 1 /* Storage allocation and gc for XEmacs Lisp interpreter. |
2 Copyright (C) 1985-1998 Free Software Foundation, Inc. | |
3 Copyright (C) 1995 Sun Microsystems, Inc. | |
2367 | 4 Copyright (C) 1995, 1996, 2001, 2002, 2003, 2004 Ben Wing. |
428 | 5 |
6 This file is part of XEmacs. | |
7 | |
8 XEmacs is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
10 Free Software Foundation; either version 2, or (at your option) any | |
11 later version. | |
12 | |
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with XEmacs; see the file COPYING. If not, write to | |
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
21 Boston, MA 02111-1307, USA. */ | |
22 | |
23 /* Synched up with: FSF 19.28, Mule 2.0. Substantially different from | |
24 FSF. */ | |
25 | |
26 /* Authorship: | |
27 | |
28 FSF: Original version; a long time ago. | |
29 Mly: Significantly rewritten to use new 3-bit tags and | |
30 nicely abstracted object definitions, for 19.8. | |
31 JWZ: Improved code to keep track of purespace usage and | |
32 issue nice purespace and GC stats. | |
33 Ben Wing: Cleaned up frob-block lrecord code, added error-checking | |
34 and various changes for Mule, for 19.12. | |
35 Added bit vectors for 19.13. | |
36 Added lcrecord lists for 19.14. | |
37 slb: Lots of work on the purification and dump time code. | |
38 Synched Doug Lea malloc support from Emacs 20.2. | |
442 | 39 og: Killed the purespace. Portable dumper (moved to dumper.c) |
428 | 40 */ |
41 | |
42 #include <config.h> | |
43 #include "lisp.h" | |
44 | |
45 #include "backtrace.h" | |
46 #include "buffer.h" | |
47 #include "bytecode.h" | |
48 #include "chartab.h" | |
49 #include "device.h" | |
50 #include "elhash.h" | |
51 #include "events.h" | |
872 | 52 #include "extents-impl.h" |
1204 | 53 #include "file-coding.h" |
872 | 54 #include "frame-impl.h" |
428 | 55 #include "glyphs.h" |
56 #include "opaque.h" | |
1204 | 57 #include "lstream.h" |
872 | 58 #include "process.h" |
1292 | 59 #include "profile.h" |
428 | 60 #include "redisplay.h" |
61 #include "specifier.h" | |
62 #include "sysfile.h" | |
442 | 63 #include "sysdep.h" |
428 | 64 #include "window.h" |
65 #include "console-stream.h" | |
66 | |
67 #ifdef DOUG_LEA_MALLOC | |
68 #include <malloc.h> | |
69 #endif | |
70 | |
71 EXFUN (Fgarbage_collect, 0); | |
72 | |
814 | 73 static void recompute_need_to_garbage_collect (void); |
74 | |
428 | 75 #if 0 /* this is _way_ too slow to be part of the standard debug options */ |
76 #if defined(DEBUG_XEMACS) && defined(MULE) | |
77 #define VERIFY_STRING_CHARS_INTEGRITY | |
78 #endif | |
79 #endif | |
80 | |
81 /* Define this to use malloc/free with no freelist for all datatypes, | |
82 the hope being that some debugging tools may help detect | |
83 freed memory references */ | |
84 #ifdef USE_DEBUG_MALLOC /* Taking the above comment at face value -slb */ | |
85 #include <dmalloc.h> | |
86 #define ALLOC_NO_POOLS | |
87 #endif | |
88 | |
89 #ifdef DEBUG_XEMACS | |
458 | 90 static Fixnum debug_allocation; |
91 static Fixnum debug_allocation_backtrace_length; | |
428 | 92 #endif |
93 | |
94 /* Number of bytes of consing done since the last gc */ | |
814 | 95 static EMACS_INT consing_since_gc; |
1292 | 96 EMACS_UINT total_consing; |
97 | |
814 | 98 int need_to_garbage_collect; |
851 | 99 int need_to_check_c_alloca; |
887 | 100 int need_to_signal_post_gc; |
851 | 101 int funcall_allocation_flag; |
102 Bytecount __temp_alloca_size__; | |
103 Bytecount funcall_alloca_count; | |
814 | 104 |
105 /* Determine now whether we need to garbage collect or not, to make | |
106 Ffuncall() faster */ | |
107 #define INCREMENT_CONS_COUNTER_1(size) \ | |
108 do \ | |
109 { \ | |
110 consing_since_gc += (size); \ | |
1292 | 111 total_consing += (size); \ |
112 if (profiling_active) \ | |
113 profile_record_consing (size); \ | |
814 | 114 recompute_need_to_garbage_collect (); \ |
115 } while (0) | |
428 | 116 |
117 #define debug_allocation_backtrace() \ | |
118 do { \ | |
119 if (debug_allocation_backtrace_length > 0) \ | |
120 debug_short_backtrace (debug_allocation_backtrace_length); \ | |
121 } while (0) | |
122 | |
123 #ifdef DEBUG_XEMACS | |
801 | 124 #define INCREMENT_CONS_COUNTER(foosize, type) \ |
125 do { \ | |
126 if (debug_allocation) \ | |
127 { \ | |
128 stderr_out ("allocating %s (size %ld)\n", type, \ | |
129 (long) foosize); \ | |
130 debug_allocation_backtrace (); \ | |
131 } \ | |
132 INCREMENT_CONS_COUNTER_1 (foosize); \ | |
428 | 133 } while (0) |
134 #define NOSEEUM_INCREMENT_CONS_COUNTER(foosize, type) \ | |
135 do { \ | |
136 if (debug_allocation > 1) \ | |
137 { \ | |
801 | 138 stderr_out ("allocating noseeum %s (size %ld)\n", type, \ |
139 (long) foosize); \ | |
428 | 140 debug_allocation_backtrace (); \ |
141 } \ | |
142 INCREMENT_CONS_COUNTER_1 (foosize); \ | |
143 } while (0) | |
144 #else | |
145 #define INCREMENT_CONS_COUNTER(size, type) INCREMENT_CONS_COUNTER_1 (size) | |
146 #define NOSEEUM_INCREMENT_CONS_COUNTER(size, type) \ | |
147 INCREMENT_CONS_COUNTER_1 (size) | |
148 #endif | |
149 | |
150 #define DECREMENT_CONS_COUNTER(size) do { \ | |
151 consing_since_gc -= (size); \ | |
1292 | 152 total_consing -= (size); \ |
153 if (profiling_active) \ | |
154 profile_record_unconsing (size); \ | |
428 | 155 if (consing_since_gc < 0) \ |
156 consing_since_gc = 0; \ | |
814 | 157 recompute_need_to_garbage_collect (); \ |
428 | 158 } while (0) |
159 | |
160 /* Number of bytes of consing since gc before another gc should be done. */ | |
801 | 161 static EMACS_INT gc_cons_threshold; |
162 | |
163 /* Percentage of consing of total data size before another GC. */ | |
164 static EMACS_INT gc_cons_percentage; | |
165 | |
166 #ifdef ERROR_CHECK_GC | |
853 | 167 int always_gc; /* Debugging hack; equivalent to |
168 (setq gc-cons-thresold -1) */ | |
801 | 169 #else |
170 #define always_gc 0 | |
171 #endif | |
428 | 172 |
173 /* Nonzero during gc */ | |
174 int gc_in_progress; | |
175 | |
1154 | 176 /* Nonzero means display messages at beginning and end of GC. */ |
177 | |
178 int garbage_collection_messages; | |
179 | |
428 | 180 /* Number of times GC has happened at this level or below. |
181 * Level 0 is most volatile, contrary to usual convention. | |
182 * (Of course, there's only one level at present) */ | |
183 EMACS_INT gc_generation_number[1]; | |
184 | |
185 /* This is just for use by the printer, to allow things to print uniquely */ | |
186 static int lrecord_uid_counter; | |
187 | |
188 /* Nonzero when calling certain hooks or doing other things where | |
189 a GC would be bad */ | |
1957 | 190 int gc_currently_forbidden; |
428 | 191 |
192 /* Hooks. */ | |
193 Lisp_Object Vpre_gc_hook, Qpre_gc_hook; | |
194 Lisp_Object Vpost_gc_hook, Qpost_gc_hook; | |
195 | |
196 /* "Garbage collecting" */ | |
197 Lisp_Object Vgc_message; | |
198 Lisp_Object Vgc_pointer_glyph; | |
2367 | 199 static const Ascbyte gc_default_message[] = "Garbage collecting"; |
428 | 200 Lisp_Object Qgarbage_collecting; |
201 | |
1292 | 202 static Lisp_Object QSin_garbage_collection; |
203 | |
428 | 204 /* Non-zero means we're in the process of doing the dump */ |
205 int purify_flag; | |
206 | |
1204 | 207 /* Non-zero means we're pdumping out or in */ |
208 #ifdef PDUMP | |
209 int in_pdump; | |
210 #endif | |
211 | |
800 | 212 #ifdef ERROR_CHECK_TYPES |
428 | 213 |
793 | 214 Error_Behavior ERROR_ME, ERROR_ME_NOT, ERROR_ME_WARN, ERROR_ME_DEBUG_WARN; |
428 | 215 |
216 #endif | |
217 | |
801 | 218 /* Very cheesy ways of figuring out how much memory is being used for |
219 data. #### Need better (system-dependent) ways. */ | |
220 void *minimum_address_seen; | |
221 void *maximum_address_seen; | |
222 | |
2720 | 223 #ifndef MC_ALLOC |
428 | 224 int |
225 c_readonly (Lisp_Object obj) | |
226 { | |
227 return POINTER_TYPE_P (XTYPE (obj)) && C_READONLY (obj); | |
228 } | |
2720 | 229 #endif /* MC_ALLOC */ |
428 | 230 |
231 int | |
232 lisp_readonly (Lisp_Object obj) | |
233 { | |
234 return POINTER_TYPE_P (XTYPE (obj)) && LISP_READONLY (obj); | |
235 } | |
236 | |
237 | |
238 /* Maximum amount of C stack to save when a GC happens. */ | |
239 | |
240 #ifndef MAX_SAVE_STACK | |
241 #define MAX_SAVE_STACK 0 /* 16000 */ | |
242 #endif | |
243 | |
244 /* Non-zero means ignore malloc warnings. Set during initialization. */ | |
245 int ignore_malloc_warnings; | |
246 | |
247 | |
2720 | 248 #ifndef MC_ALLOC |
428 | 249 static void *breathing_space; |
250 | |
251 void | |
252 release_breathing_space (void) | |
253 { | |
254 if (breathing_space) | |
255 { | |
256 void *tmp = breathing_space; | |
257 breathing_space = 0; | |
1726 | 258 xfree (tmp, void *); |
428 | 259 } |
260 } | |
2720 | 261 #endif /* not MC_ALLOC */ |
428 | 262 |
263 /* malloc calls this if it finds we are near exhausting storage */ | |
264 void | |
442 | 265 malloc_warning (const char *str) |
428 | 266 { |
267 if (ignore_malloc_warnings) | |
268 return; | |
269 | |
270 warn_when_safe | |
793 | 271 (Qmemory, Qemergency, |
428 | 272 "%s\n" |
273 "Killing some buffers may delay running out of memory.\n" | |
274 "However, certainly by the time you receive the 95%% warning,\n" | |
275 "you should clean up, kill this Emacs, and start a new one.", | |
276 str); | |
277 } | |
278 | |
279 /* Called if malloc returns zero */ | |
280 DOESNT_RETURN | |
281 memory_full (void) | |
282 { | |
283 /* Force a GC next time eval is called. | |
284 It's better to loop garbage-collecting (we might reclaim enough | |
285 to win) than to loop beeping and barfing "Memory exhausted" | |
286 */ | |
287 consing_since_gc = gc_cons_threshold + 1; | |
814 | 288 recompute_need_to_garbage_collect (); |
2720 | 289 #ifndef MC_ALLOC |
428 | 290 release_breathing_space (); |
2720 | 291 #endif /* not MC_ALLOC */ |
428 | 292 |
293 /* Flush some histories which might conceivably contain garbalogical | |
294 inhibitors. */ | |
295 if (!NILP (Fboundp (Qvalues))) | |
296 Fset (Qvalues, Qnil); | |
297 Vcommand_history = Qnil; | |
298 | |
563 | 299 out_of_memory ("Memory exhausted", Qunbound); |
428 | 300 } |
301 | |
801 | 302 static void |
303 set_alloc_mins_and_maxes (void *val, Bytecount size) | |
304 { | |
305 if (!val) | |
306 return; | |
307 if ((char *) val + size > (char *) maximum_address_seen) | |
308 maximum_address_seen = (char *) val + size; | |
309 if (!minimum_address_seen) | |
310 minimum_address_seen = | |
311 #if SIZEOF_VOID_P == 8 | |
312 (void *) 0xFFFFFFFFFFFFFFFF; | |
313 #else | |
314 (void *) 0xFFFFFFFF; | |
315 #endif | |
316 if ((char *) val < (char *) minimum_address_seen) | |
317 minimum_address_seen = (char *) val; | |
318 } | |
319 | |
1315 | 320 #ifdef ERROR_CHECK_MALLOC |
1292 | 321 static int in_malloc; |
1333 | 322 extern int regex_malloc_disallowed; |
2367 | 323 |
324 #define MALLOC_BEGIN() \ | |
325 do \ | |
326 { \ | |
327 assert (!in_malloc); \ | |
328 assert (!regex_malloc_disallowed); \ | |
329 in_malloc = 1; \ | |
330 } \ | |
331 while (0) | |
332 | |
2720 | 333 #ifdef MC_ALLOC |
334 #define FREE_OR_REALLOC_BEGIN(block) \ | |
335 do \ | |
336 { \ | |
337 /* Unbelievably, calling free() on 0xDEADBEEF doesn't cause an \ | |
338 error until much later on for many system mallocs, such as \ | |
339 the one that comes with Solaris 2.3. FMH!! */ \ | |
340 assert (block != (void *) 0xDEADBEEF); \ | |
341 MALLOC_BEGIN (); \ | |
342 } \ | |
343 while (0) | |
344 #else /* not MC_ALLOC */ | |
2367 | 345 #define FREE_OR_REALLOC_BEGIN(block) \ |
346 do \ | |
347 { \ | |
348 /* Unbelievably, calling free() on 0xDEADBEEF doesn't cause an \ | |
349 error until much later on for many system mallocs, such as \ | |
350 the one that comes with Solaris 2.3. FMH!! */ \ | |
351 assert (block != (void *) 0xDEADBEEF); \ | |
352 /* You cannot free something within dumped space, because there is \ | |
353 no longer any sort of malloc structure associated with the block. \ | |
354 If you are tripping this, you may need to conditionalize on \ | |
355 DUMPEDP. */ \ | |
356 assert (!DUMPEDP (block)); \ | |
357 MALLOC_BEGIN (); \ | |
358 } \ | |
359 while (0) | |
2720 | 360 #endif /* not MC_ALLOC */ |
2367 | 361 |
362 #define MALLOC_END() \ | |
363 do \ | |
364 { \ | |
365 in_malloc = 0; \ | |
366 } \ | |
367 while (0) | |
368 | |
369 #else /* ERROR_CHECK_MALLOC */ | |
370 | |
2658 | 371 #define MALLOC_BEGIN() |
2367 | 372 #define FREE_OR_REALLOC_BEGIN(block) |
373 #define MALLOC_END() | |
374 | |
375 #endif /* ERROR_CHECK_MALLOC */ | |
376 | |
377 static void | |
378 malloc_after (void *val, Bytecount size) | |
379 { | |
380 if (!val && size != 0) | |
381 memory_full (); | |
382 set_alloc_mins_and_maxes (val, size); | |
383 } | |
384 | |
385 /* like malloc, calloc, realloc, free but: | |
386 | |
387 -- check for no memory left | |
388 -- set internal mins and maxes | |
389 -- with error-checking on, check for reentrancy, invalid freeing, etc. | |
390 */ | |
1292 | 391 |
428 | 392 #undef xmalloc |
393 void * | |
665 | 394 xmalloc (Bytecount size) |
428 | 395 { |
1292 | 396 void *val; |
2367 | 397 MALLOC_BEGIN (); |
1292 | 398 val = malloc (size); |
2367 | 399 MALLOC_END (); |
400 malloc_after (val, size); | |
428 | 401 return val; |
402 } | |
403 | |
404 #undef xcalloc | |
405 static void * | |
665 | 406 xcalloc (Elemcount nelem, Bytecount elsize) |
428 | 407 { |
1292 | 408 void *val; |
2367 | 409 MALLOC_BEGIN (); |
1292 | 410 val= calloc (nelem, elsize); |
2367 | 411 MALLOC_END (); |
412 malloc_after (val, nelem * elsize); | |
428 | 413 return val; |
414 } | |
415 | |
416 void * | |
665 | 417 xmalloc_and_zero (Bytecount size) |
428 | 418 { |
419 return xcalloc (size, sizeof (char)); | |
420 } | |
421 | |
422 #undef xrealloc | |
423 void * | |
665 | 424 xrealloc (void *block, Bytecount size) |
428 | 425 { |
2367 | 426 FREE_OR_REALLOC_BEGIN (block); |
551 | 427 block = realloc (block, size); |
2367 | 428 MALLOC_END (); |
429 malloc_after (block, size); | |
551 | 430 return block; |
428 | 431 } |
432 | |
433 void | |
434 xfree_1 (void *block) | |
435 { | |
436 #ifdef ERROR_CHECK_MALLOC | |
437 assert (block); | |
438 #endif /* ERROR_CHECK_MALLOC */ | |
2367 | 439 FREE_OR_REALLOC_BEGIN (block); |
428 | 440 free (block); |
2367 | 441 MALLOC_END (); |
428 | 442 } |
443 | |
444 #ifdef ERROR_CHECK_GC | |
445 | |
2720 | 446 #ifndef MC_ALLOC |
428 | 447 static void |
665 | 448 deadbeef_memory (void *ptr, Bytecount size) |
428 | 449 { |
826 | 450 UINT_32_BIT *ptr4 = (UINT_32_BIT *) ptr; |
665 | 451 Bytecount beefs = size >> 2; |
428 | 452 |
453 /* In practice, size will always be a multiple of four. */ | |
454 while (beefs--) | |
1204 | 455 (*ptr4++) = 0xDEADBEEF; /* -559038737 base 10 */ |
428 | 456 } |
2720 | 457 #endif /* not MC_ALLOC */ |
428 | 458 |
459 #else /* !ERROR_CHECK_GC */ | |
460 | |
461 | |
462 #define deadbeef_memory(ptr, size) | |
463 | |
464 #endif /* !ERROR_CHECK_GC */ | |
465 | |
466 #undef xstrdup | |
467 char * | |
442 | 468 xstrdup (const char *str) |
428 | 469 { |
470 int len = strlen (str) + 1; /* for stupid terminating 0 */ | |
471 void *val = xmalloc (len); | |
771 | 472 |
428 | 473 if (val == 0) return 0; |
474 return (char *) memcpy (val, str, len); | |
475 } | |
476 | |
477 #ifdef NEED_STRDUP | |
478 char * | |
442 | 479 strdup (const char *s) |
428 | 480 { |
481 return xstrdup (s); | |
482 } | |
483 #endif /* NEED_STRDUP */ | |
484 | |
485 | |
2720 | 486 #ifndef MC_ALLOC |
428 | 487 static void * |
665 | 488 allocate_lisp_storage (Bytecount size) |
428 | 489 { |
793 | 490 void *val = xmalloc (size); |
491 /* We don't increment the cons counter anymore. Calling functions do | |
492 that now because we have two different kinds of cons counters -- one | |
493 for normal objects, and one for no-see-um conses (and possibly others | |
494 similar) where the conses are used totally internally, never escape, | |
495 and are created and then freed and shouldn't logically increment the | |
496 cons counting. #### (Or perhaps, we should decrement it when an object | |
497 get freed?) */ | |
498 | |
499 /* But we do now (as of 3-27-02) go and zero out the memory. This is a | |
500 good thing, as it will guarantee we won't get any intermittent bugs | |
1204 | 501 coming from an uninitiated field. The speed loss is unnoticeable, |
502 esp. as the objects are not large -- large stuff like buffer text and | |
503 redisplay structures are allocated separately. */ | |
793 | 504 memset (val, 0, size); |
851 | 505 |
506 if (need_to_check_c_alloca) | |
507 xemacs_c_alloca (0); | |
508 | |
793 | 509 return val; |
428 | 510 } |
2720 | 511 #endif /* not MC_ALLOC */ |
512 | |
513 #ifdef MC_ALLOC_TYPE_STATS | |
514 static struct | |
515 { | |
516 int instances_in_use; | |
517 int bytes_in_use; | |
518 int bytes_in_use_including_overhead; | |
519 } lrecord_stats [countof (lrecord_implementations_table) | |
520 + MODULE_DEFINABLE_TYPE_COUNT]; | |
521 | |
2775 | 522 int lrecord_string_data_instances_in_use; |
523 int lrecord_string_data_bytes_in_use; | |
524 int lrecord_string_data_bytes_in_use_including_overhead; | |
525 | |
2720 | 526 void |
527 init_lrecord_stats () | |
528 { | |
529 xzero (lrecord_stats); | |
2775 | 530 lrecord_string_data_instances_in_use = 0; |
531 lrecord_string_data_bytes_in_use = 0; | |
532 lrecord_string_data_bytes_in_use_including_overhead = 0; | |
533 } | |
534 | |
535 void | |
536 inc_lrecord_string_data_stats (Bytecount size) | |
537 { | |
538 lrecord_string_data_instances_in_use++; | |
539 lrecord_string_data_bytes_in_use += size; | |
540 lrecord_string_data_bytes_in_use_including_overhead += size; | |
541 } | |
542 | |
543 void | |
544 dec_lrecord_string_data_stats (Bytecount size) | |
545 { | |
546 lrecord_string_data_instances_in_use--; | |
547 lrecord_string_data_bytes_in_use -= size; | |
548 lrecord_string_data_bytes_in_use_including_overhead -= size; | |
2720 | 549 } |
550 | |
551 void | |
552 inc_lrecord_stats (Bytecount size, const struct lrecord_header *h) | |
553 { | |
554 int type_index = h->type; | |
555 if (!size) | |
556 size = detagged_lisp_object_size (h); | |
557 | |
558 lrecord_stats[type_index].instances_in_use++; | |
559 lrecord_stats[type_index].bytes_in_use += size; | |
560 lrecord_stats[type_index].bytes_in_use_including_overhead | |
561 #ifdef MEMORY_USAGE_STATS | |
562 += mc_alloced_storage_size (size, 0); | |
563 #else /* not MEMORY_USAGE_STATS */ | |
564 += size; | |
565 #endif /* not MEMORY_USAGE_STATS */ | |
566 } | |
567 | |
568 void | |
569 dec_lrecord_stats (Bytecount size_including_overhead, | |
570 const struct lrecord_header *h) | |
571 { | |
572 int type_index = h->type; | |
2775 | 573 int size = detagged_lisp_object_size (h); |
2720 | 574 |
575 lrecord_stats[type_index].instances_in_use--; | |
2775 | 576 lrecord_stats[type_index].bytes_in_use -= size; |
2720 | 577 lrecord_stats[type_index].bytes_in_use_including_overhead |
578 -= size_including_overhead; | |
579 | |
2775 | 580 DECREMENT_CONS_COUNTER (size); |
2720 | 581 } |
582 #endif /* not MC_ALLOC_TYPE_STATS */ | |
583 | |
584 #ifndef MC_ALLOC | |
442 | 585 /* lcrecords are chained together through their "next" field. |
586 After doing the mark phase, GC will walk this linked list | |
587 and free any lcrecord which hasn't been marked. */ | |
428 | 588 static struct lcrecord_header *all_lcrecords; |
2720 | 589 #endif /* not MC_ALLOC */ |
590 | |
591 #ifdef MC_ALLOC | |
592 /* The basic lrecord allocation functions. See lrecord.h for details. */ | |
593 void * | |
594 alloc_lrecord (Bytecount size, | |
595 const struct lrecord_implementation *implementation) | |
596 { | |
597 struct lrecord_header *lheader; | |
598 | |
599 type_checking_assert | |
600 ((implementation->static_size == 0 ? | |
601 implementation->size_in_bytes_method != NULL : | |
602 implementation->static_size == size)); | |
603 | |
604 lheader = (struct lrecord_header *) mc_alloc (size); | |
605 gc_checking_assert (LRECORD_FREE_P (lheader)); | |
606 set_lheader_implementation (lheader, implementation); | |
607 lheader->uid = lrecord_uid_counter++; | |
608 #ifdef MC_ALLOC_TYPE_STATS | |
609 inc_lrecord_stats (size, lheader); | |
610 #endif /* not MC_ALLOC_TYPE_STATS */ | |
611 INCREMENT_CONS_COUNTER (size, implementation->name); | |
612 return lheader; | |
613 } | |
614 | |
615 void * | |
616 noseeum_alloc_lrecord (Bytecount size, | |
617 const struct lrecord_implementation *implementation) | |
618 { | |
619 struct lrecord_header *lheader; | |
620 | |
621 type_checking_assert | |
622 ((implementation->static_size == 0 ? | |
623 implementation->size_in_bytes_method != NULL : | |
624 implementation->static_size == size)); | |
625 | |
626 lheader = (struct lrecord_header *) mc_alloc (size); | |
627 gc_checking_assert (LRECORD_FREE_P (lheader)); | |
628 set_lheader_implementation (lheader, implementation); | |
629 lheader->uid = lrecord_uid_counter++; | |
630 #ifdef MC_ALLOC_TYPE_STATS | |
631 inc_lrecord_stats (size, lheader); | |
632 #endif /* not MC_ALLOC_TYPE_STATS */ | |
633 NOSEEUM_INCREMENT_CONS_COUNTER (size, implementation->name); | |
634 return lheader; | |
635 } | |
636 | |
637 void | |
638 free_lrecord (Lisp_Object lrecord) | |
639 { | |
640 gc_checking_assert (!gc_in_progress); | |
641 gc_checking_assert (!LRECORD_FREE_P (XRECORD_LHEADER (lrecord))); | |
642 gc_checking_assert (!XRECORD_LHEADER (lrecord)->free); | |
643 | |
644 MC_ALLOC_CALL_FINALIZER (XPNTR (lrecord)); | |
645 mc_free (XPNTR (lrecord)); | |
646 } | |
647 #else /* not MC_ALLOC */ | |
428 | 648 |
1204 | 649 /* The most basic of the lcrecord allocation functions. Not usually called |
650 directly. Allocates an lrecord not managed by any lcrecord-list, of a | |
651 specified size. See lrecord.h. */ | |
652 | |
428 | 653 void * |
1204 | 654 basic_alloc_lcrecord (Bytecount size, |
655 const struct lrecord_implementation *implementation) | |
428 | 656 { |
657 struct lcrecord_header *lcheader; | |
658 | |
442 | 659 type_checking_assert |
660 ((implementation->static_size == 0 ? | |
661 implementation->size_in_bytes_method != NULL : | |
662 implementation->static_size == size) | |
663 && | |
664 (! implementation->basic_p) | |
665 && | |
666 (! (implementation->hash == NULL && implementation->equal != NULL))); | |
428 | 667 |
668 lcheader = (struct lcrecord_header *) allocate_lisp_storage (size); | |
442 | 669 set_lheader_implementation (&lcheader->lheader, implementation); |
428 | 670 lcheader->next = all_lcrecords; |
671 #if 1 /* mly prefers to see small ID numbers */ | |
672 lcheader->uid = lrecord_uid_counter++; | |
673 #else /* jwz prefers to see real addrs */ | |
674 lcheader->uid = (int) &lcheader; | |
675 #endif | |
676 lcheader->free = 0; | |
677 all_lcrecords = lcheader; | |
678 INCREMENT_CONS_COUNTER (size, implementation->name); | |
679 return lcheader; | |
680 } | |
681 | |
682 #if 0 /* Presently unused */ | |
683 /* Very, very poor man's EGC? | |
684 * This may be slow and thrash pages all over the place. | |
685 * Only call it if you really feel you must (and if the | |
686 * lrecord was fairly recently allocated). | |
687 * Otherwise, just let the GC do its job -- that's what it's there for | |
688 */ | |
689 void | |
771 | 690 very_old_free_lcrecord (struct lcrecord_header *lcrecord) |
428 | 691 { |
692 if (all_lcrecords == lcrecord) | |
693 { | |
694 all_lcrecords = lcrecord->next; | |
695 } | |
696 else | |
697 { | |
698 struct lrecord_header *header = all_lcrecords; | |
699 for (;;) | |
700 { | |
701 struct lrecord_header *next = header->next; | |
702 if (next == lcrecord) | |
703 { | |
704 header->next = lrecord->next; | |
705 break; | |
706 } | |
707 else if (next == 0) | |
2500 | 708 ABORT (); |
428 | 709 else |
710 header = next; | |
711 } | |
712 } | |
713 if (lrecord->implementation->finalizer) | |
714 lrecord->implementation->finalizer (lrecord, 0); | |
715 xfree (lrecord); | |
716 return; | |
717 } | |
718 #endif /* Unused */ | |
2720 | 719 #endif /* not MC_ALLOC */ |
428 | 720 |
721 | |
722 static void | |
723 disksave_object_finalization_1 (void) | |
724 { | |
2720 | 725 #ifdef MC_ALLOC |
726 mc_finalize_for_disksave (); | |
727 #else /* not MC_ALLOC */ | |
428 | 728 struct lcrecord_header *header; |
729 | |
730 for (header = all_lcrecords; header; header = header->next) | |
731 { | |
442 | 732 if (LHEADER_IMPLEMENTATION (&header->lheader)->finalizer && |
428 | 733 !header->free) |
442 | 734 LHEADER_IMPLEMENTATION (&header->lheader)->finalizer (header, 1); |
428 | 735 } |
2720 | 736 #endif /* not MC_ALLOC */ |
428 | 737 } |
738 | |
1204 | 739 /* Bitwise copy all parts of a Lisp object other than the header */ |
740 | |
741 void | |
742 copy_lisp_object (Lisp_Object dst, Lisp_Object src) | |
743 { | |
744 const struct lrecord_implementation *imp = | |
745 XRECORD_LHEADER_IMPLEMENTATION (src); | |
746 Bytecount size = lisp_object_size (src); | |
747 | |
748 assert (imp == XRECORD_LHEADER_IMPLEMENTATION (dst)); | |
749 assert (size == lisp_object_size (dst)); | |
750 | |
2720 | 751 #ifdef MC_ALLOC |
752 memcpy ((char *) XRECORD_LHEADER (dst) + sizeof (struct lrecord_header), | |
753 (char *) XRECORD_LHEADER (src) + sizeof (struct lrecord_header), | |
754 size - sizeof (struct lrecord_header)); | |
755 #else /* not MC_ALLOC */ | |
1204 | 756 if (imp->basic_p) |
757 memcpy ((char *) XRECORD_LHEADER (dst) + sizeof (struct lrecord_header), | |
758 (char *) XRECORD_LHEADER (src) + sizeof (struct lrecord_header), | |
759 size - sizeof (struct lrecord_header)); | |
760 else | |
761 memcpy ((char *) XRECORD_LHEADER (dst) + sizeof (struct lcrecord_header), | |
762 (char *) XRECORD_LHEADER (src) + sizeof (struct lcrecord_header), | |
763 size - sizeof (struct lcrecord_header)); | |
2720 | 764 #endif /* not MC_ALLOC */ |
1204 | 765 } |
766 | |
428 | 767 |
768 /************************************************************************/ | |
769 /* Debugger support */ | |
770 /************************************************************************/ | |
771 /* Give gdb/dbx enough information to decode Lisp Objects. We make | |
772 sure certain symbols are always defined, so gdb doesn't complain | |
438 | 773 about expressions in src/.gdbinit. See src/.gdbinit or src/.dbxrc |
774 to see how this is used. */ | |
428 | 775 |
458 | 776 EMACS_UINT dbg_valmask = ((1UL << VALBITS) - 1) << GCBITS; |
777 EMACS_UINT dbg_typemask = (1UL << GCTYPEBITS) - 1; | |
428 | 778 |
779 #ifdef USE_UNION_TYPE | |
458 | 780 unsigned char dbg_USE_UNION_TYPE = 1; |
428 | 781 #else |
458 | 782 unsigned char dbg_USE_UNION_TYPE = 0; |
428 | 783 #endif |
784 | |
458 | 785 unsigned char dbg_valbits = VALBITS; |
786 unsigned char dbg_gctypebits = GCTYPEBITS; | |
787 | |
788 /* On some systems, the above definitions will be optimized away by | |
789 the compiler or linker unless they are referenced in some function. */ | |
790 long dbg_inhibit_dbg_symbol_deletion (void); | |
791 long | |
792 dbg_inhibit_dbg_symbol_deletion (void) | |
793 { | |
794 return | |
795 (dbg_valmask + | |
796 dbg_typemask + | |
797 dbg_USE_UNION_TYPE + | |
798 dbg_valbits + | |
799 dbg_gctypebits); | |
800 } | |
428 | 801 |
802 /* Macros turned into functions for ease of debugging. | |
803 Debuggers don't know about macros! */ | |
804 int dbg_eq (Lisp_Object obj1, Lisp_Object obj2); | |
805 int | |
806 dbg_eq (Lisp_Object obj1, Lisp_Object obj2) | |
807 { | |
808 return EQ (obj1, obj2); | |
809 } | |
810 | |
811 | |
2720 | 812 #ifndef MC_ALLOC |
428 | 813 /************************************************************************/ |
814 /* Fixed-size type macros */ | |
815 /************************************************************************/ | |
816 | |
817 /* For fixed-size types that are commonly used, we malloc() large blocks | |
818 of memory at a time and subdivide them into chunks of the correct | |
819 size for an object of that type. This is more efficient than | |
820 malloc()ing each object separately because we save on malloc() time | |
821 and overhead due to the fewer number of malloc()ed blocks, and | |
822 also because we don't need any extra pointers within each object | |
823 to keep them threaded together for GC purposes. For less common | |
824 (and frequently large-size) types, we use lcrecords, which are | |
825 malloc()ed individually and chained together through a pointer | |
826 in the lcrecord header. lcrecords do not need to be fixed-size | |
827 (i.e. two objects of the same type need not have the same size; | |
828 however, the size of a particular object cannot vary dynamically). | |
829 It is also much easier to create a new lcrecord type because no | |
830 additional code needs to be added to alloc.c. Finally, lcrecords | |
831 may be more efficient when there are only a small number of them. | |
832 | |
833 The types that are stored in these large blocks (or "frob blocks") | |
1983 | 834 are cons, all number types except fixnum, compiled-function, symbol, |
835 marker, extent, event, and string. | |
428 | 836 |
837 Note that strings are special in that they are actually stored in | |
838 two parts: a structure containing information about the string, and | |
839 the actual data associated with the string. The former structure | |
840 (a struct Lisp_String) is a fixed-size structure and is managed the | |
841 same way as all the other such types. This structure contains a | |
842 pointer to the actual string data, which is stored in structures of | |
843 type struct string_chars_block. Each string_chars_block consists | |
844 of a pointer to a struct Lisp_String, followed by the data for that | |
440 | 845 string, followed by another pointer to a Lisp_String, followed by |
846 the data for that string, etc. At GC time, the data in these | |
847 blocks is compacted by searching sequentially through all the | |
428 | 848 blocks and compressing out any holes created by unmarked strings. |
849 Strings that are more than a certain size (bigger than the size of | |
850 a string_chars_block, although something like half as big might | |
851 make more sense) are malloc()ed separately and not stored in | |
852 string_chars_blocks. Furthermore, no one string stretches across | |
853 two string_chars_blocks. | |
854 | |
1204 | 855 Vectors are each malloc()ed separately as lcrecords. |
428 | 856 |
857 In the following discussion, we use conses, but it applies equally | |
858 well to the other fixed-size types. | |
859 | |
860 We store cons cells inside of cons_blocks, allocating a new | |
861 cons_block with malloc() whenever necessary. Cons cells reclaimed | |
862 by GC are put on a free list to be reallocated before allocating | |
863 any new cons cells from the latest cons_block. Each cons_block is | |
864 just under 2^n - MALLOC_OVERHEAD bytes long, since malloc (at least | |
865 the versions in malloc.c and gmalloc.c) really allocates in units | |
866 of powers of two and uses 4 bytes for its own overhead. | |
867 | |
868 What GC actually does is to search through all the cons_blocks, | |
869 from the most recently allocated to the oldest, and put all | |
870 cons cells that are not marked (whether or not they're already | |
871 free) on a cons_free_list. The cons_free_list is a stack, and | |
872 so the cons cells in the oldest-allocated cons_block end up | |
873 at the head of the stack and are the first to be reallocated. | |
874 If any cons_block is entirely free, it is freed with free() | |
875 and its cons cells removed from the cons_free_list. Because | |
876 the cons_free_list ends up basically in memory order, we have | |
877 a high locality of reference (assuming a reasonable turnover | |
878 of allocating and freeing) and have a reasonable probability | |
879 of entirely freeing up cons_blocks that have been more recently | |
880 allocated. This stage is called the "sweep stage" of GC, and | |
881 is executed after the "mark stage", which involves starting | |
882 from all places that are known to point to in-use Lisp objects | |
883 (e.g. the obarray, where are all symbols are stored; the | |
884 current catches and condition-cases; the backtrace list of | |
885 currently executing functions; the gcpro list; etc.) and | |
886 recursively marking all objects that are accessible. | |
887 | |
454 | 888 At the beginning of the sweep stage, the conses in the cons blocks |
889 are in one of three states: in use and marked, in use but not | |
890 marked, and not in use (already freed). Any conses that are marked | |
891 have been marked in the mark stage just executed, because as part | |
892 of the sweep stage we unmark any marked objects. The way we tell | |
893 whether or not a cons cell is in use is through the LRECORD_FREE_P | |
894 macro. This uses a special lrecord type `lrecord_type_free', | |
895 which is never associated with any valid object. | |
896 | |
897 Conses on the free_cons_list are threaded through a pointer stored | |
898 in the conses themselves. Because the cons is still in a | |
899 cons_block and needs to remain marked as not in use for the next | |
900 time that GC happens, we need room to store both the "free" | |
901 indicator and the chaining pointer. So this pointer is stored | |
902 after the lrecord header (actually where C places a pointer after | |
903 the lrecord header; they are not necessarily contiguous). This | |
904 implies that all fixed-size types must be big enough to contain at | |
905 least one pointer. This is true for all current fixed-size types, | |
906 with the possible exception of Lisp_Floats, for which we define the | |
907 meat of the struct using a union of a pointer and a double to | |
908 ensure adequate space for the free list chain pointer. | |
428 | 909 |
910 Some types of objects need additional "finalization" done | |
911 when an object is converted from in use to not in use; | |
912 this is the purpose of the ADDITIONAL_FREE_type macro. | |
913 For example, markers need to be removed from the chain | |
914 of markers that is kept in each buffer. This is because | |
915 markers in a buffer automatically disappear if the marker | |
916 is no longer referenced anywhere (the same does not | |
917 apply to extents, however). | |
918 | |
919 WARNING: Things are in an extremely bizarre state when | |
920 the ADDITIONAL_FREE_type macros are called, so beware! | |
921 | |
454 | 922 When ERROR_CHECK_GC is defined, we do things differently so as to |
923 maximize our chances of catching places where there is insufficient | |
924 GCPROing. The thing we want to avoid is having an object that | |
925 we're using but didn't GCPRO get freed by GC and then reallocated | |
926 while we're in the process of using it -- this will result in | |
927 something seemingly unrelated getting trashed, and is extremely | |
928 difficult to track down. If the object gets freed but not | |
929 reallocated, we can usually catch this because we set most of the | |
930 bytes of a freed object to 0xDEADBEEF. (The lisp object type is set | |
931 to the invalid type `lrecord_type_free', however, and a pointer | |
932 used to chain freed objects together is stored after the lrecord | |
933 header; we play some tricks with this pointer to make it more | |
428 | 934 bogus, so crashes are more likely to occur right away.) |
935 | |
936 We want freed objects to stay free as long as possible, | |
937 so instead of doing what we do above, we maintain the | |
938 free objects in a first-in first-out queue. We also | |
939 don't recompute the free list each GC, unlike above; | |
940 this ensures that the queue ordering is preserved. | |
941 [This means that we are likely to have worse locality | |
942 of reference, and that we can never free a frob block | |
943 once it's allocated. (Even if we know that all cells | |
944 in it are free, there's no easy way to remove all those | |
945 cells from the free list because the objects on the | |
946 free list are unlikely to be in memory order.)] | |
947 Furthermore, we never take objects off the free list | |
948 unless there's a large number (usually 1000, but | |
949 varies depending on type) of them already on the list. | |
950 This way, we ensure that an object that gets freed will | |
951 remain free for the next 1000 (or whatever) times that | |
440 | 952 an object of that type is allocated. */ |
428 | 953 |
954 #ifndef MALLOC_OVERHEAD | |
955 #ifdef GNU_MALLOC | |
956 #define MALLOC_OVERHEAD 0 | |
957 #elif defined (rcheck) | |
958 #define MALLOC_OVERHEAD 20 | |
959 #else | |
960 #define MALLOC_OVERHEAD 8 | |
961 #endif | |
962 #endif /* MALLOC_OVERHEAD */ | |
963 | |
964 #if !defined(HAVE_MMAP) || defined(DOUG_LEA_MALLOC) | |
965 /* If we released our reserve (due to running out of memory), | |
966 and we have a fair amount free once again, | |
967 try to set aside another reserve in case we run out once more. | |
968 | |
969 This is called when a relocatable block is freed in ralloc.c. */ | |
970 void refill_memory_reserve (void); | |
971 void | |
442 | 972 refill_memory_reserve (void) |
428 | 973 { |
974 if (breathing_space == 0) | |
975 breathing_space = (char *) malloc (4096 - MALLOC_OVERHEAD); | |
976 } | |
977 #endif | |
978 | |
979 #ifdef ALLOC_NO_POOLS | |
980 # define TYPE_ALLOC_SIZE(type, structtype) 1 | |
981 #else | |
982 # define TYPE_ALLOC_SIZE(type, structtype) \ | |
983 ((2048 - MALLOC_OVERHEAD - sizeof (struct type##_block *)) \ | |
984 / sizeof (structtype)) | |
985 #endif /* ALLOC_NO_POOLS */ | |
986 | |
987 #define DECLARE_FIXED_TYPE_ALLOC(type, structtype) \ | |
988 \ | |
989 struct type##_block \ | |
990 { \ | |
991 struct type##_block *prev; \ | |
992 structtype block[TYPE_ALLOC_SIZE (type, structtype)]; \ | |
993 }; \ | |
994 \ | |
995 static struct type##_block *current_##type##_block; \ | |
996 static int current_##type##_block_index; \ | |
997 \ | |
454 | 998 static Lisp_Free *type##_free_list; \ |
999 static Lisp_Free *type##_free_list_tail; \ | |
428 | 1000 \ |
1001 static void \ | |
1002 init_##type##_alloc (void) \ | |
1003 { \ | |
1004 current_##type##_block = 0; \ | |
1005 current_##type##_block_index = \ | |
1006 countof (current_##type##_block->block); \ | |
1007 type##_free_list = 0; \ | |
1008 type##_free_list_tail = 0; \ | |
1009 } \ | |
1010 \ | |
1011 static int gc_count_num_##type##_in_use; \ | |
1012 static int gc_count_num_##type##_freelist | |
1013 | |
1014 #define ALLOCATE_FIXED_TYPE_FROM_BLOCK(type, result) do { \ | |
1015 if (current_##type##_block_index \ | |
1016 == countof (current_##type##_block->block)) \ | |
1017 { \ | |
1018 struct type##_block *AFTFB_new = (struct type##_block *) \ | |
1019 allocate_lisp_storage (sizeof (struct type##_block)); \ | |
1020 AFTFB_new->prev = current_##type##_block; \ | |
1021 current_##type##_block = AFTFB_new; \ | |
1022 current_##type##_block_index = 0; \ | |
1023 } \ | |
1024 (result) = \ | |
1025 &(current_##type##_block->block[current_##type##_block_index++]); \ | |
1026 } while (0) | |
1027 | |
1028 /* Allocate an instance of a type that is stored in blocks. | |
1029 TYPE is the "name" of the type, STRUCTTYPE is the corresponding | |
1030 structure type. */ | |
1031 | |
1032 #ifdef ERROR_CHECK_GC | |
1033 | |
1034 /* Note: if you get crashes in this function, suspect incorrect calls | |
1035 to free_cons() and friends. This happened once because the cons | |
1036 cell was not GC-protected and was getting collected before | |
1037 free_cons() was called. */ | |
1038 | |
454 | 1039 #define ALLOCATE_FIXED_TYPE_1(type, structtype, result) do { \ |
1040 if (gc_count_num_##type##_freelist > \ | |
1041 MINIMUM_ALLOWED_FIXED_TYPE_CELLS_##type) \ | |
1042 { \ | |
1043 result = (structtype *) type##_free_list; \ | |
1204 | 1044 assert (LRECORD_FREE_P (result)); \ |
1045 /* Before actually using the chain pointer, we complement \ | |
1046 all its bits; see PUT_FIXED_TYPE_ON_FREE_LIST(). */ \ | |
454 | 1047 type##_free_list = (Lisp_Free *) \ |
1048 (~ (EMACS_UINT) (type##_free_list->chain)); \ | |
1049 gc_count_num_##type##_freelist--; \ | |
1050 } \ | |
1051 else \ | |
1052 ALLOCATE_FIXED_TYPE_FROM_BLOCK (type, result); \ | |
1053 MARK_LRECORD_AS_NOT_FREE (result); \ | |
428 | 1054 } while (0) |
1055 | |
1056 #else /* !ERROR_CHECK_GC */ | |
1057 | |
454 | 1058 #define ALLOCATE_FIXED_TYPE_1(type, structtype, result) do { \ |
428 | 1059 if (type##_free_list) \ |
1060 { \ | |
454 | 1061 result = (structtype *) type##_free_list; \ |
1062 type##_free_list = type##_free_list->chain; \ | |
428 | 1063 } \ |
1064 else \ | |
1065 ALLOCATE_FIXED_TYPE_FROM_BLOCK (type, result); \ | |
454 | 1066 MARK_LRECORD_AS_NOT_FREE (result); \ |
428 | 1067 } while (0) |
1068 | |
1069 #endif /* !ERROR_CHECK_GC */ | |
1070 | |
454 | 1071 |
428 | 1072 #define ALLOCATE_FIXED_TYPE(type, structtype, result) \ |
1073 do \ | |
1074 { \ | |
1075 ALLOCATE_FIXED_TYPE_1 (type, structtype, result); \ | |
1076 INCREMENT_CONS_COUNTER (sizeof (structtype), #type); \ | |
1077 } while (0) | |
1078 | |
1079 #define NOSEEUM_ALLOCATE_FIXED_TYPE(type, structtype, result) \ | |
1080 do \ | |
1081 { \ | |
1082 ALLOCATE_FIXED_TYPE_1 (type, structtype, result); \ | |
1083 NOSEEUM_INCREMENT_CONS_COUNTER (sizeof (structtype), #type); \ | |
1084 } while (0) | |
1085 | |
454 | 1086 |
1087 /* Lisp_Free is the type to represent a free list member inside a frob | |
1088 block of any lisp object type. */ | |
1089 typedef struct Lisp_Free | |
1090 { | |
1091 struct lrecord_header lheader; | |
1092 struct Lisp_Free *chain; | |
1093 } Lisp_Free; | |
1094 | |
1095 #define LRECORD_FREE_P(ptr) \ | |
771 | 1096 (((struct lrecord_header *) ptr)->type == lrecord_type_free) |
454 | 1097 |
1098 #define MARK_LRECORD_AS_FREE(ptr) \ | |
771 | 1099 ((void) (((struct lrecord_header *) ptr)->type = lrecord_type_free)) |
454 | 1100 |
1101 #ifdef ERROR_CHECK_GC | |
1102 #define MARK_LRECORD_AS_NOT_FREE(ptr) \ | |
771 | 1103 ((void) (((struct lrecord_header *) ptr)->type = lrecord_type_undefined)) |
428 | 1104 #else |
454 | 1105 #define MARK_LRECORD_AS_NOT_FREE(ptr) DO_NOTHING |
428 | 1106 #endif |
1107 | |
1108 #ifdef ERROR_CHECK_GC | |
1109 | |
454 | 1110 #define PUT_FIXED_TYPE_ON_FREE_LIST(type, structtype, ptr) do { \ |
1111 if (type##_free_list_tail) \ | |
1112 { \ | |
1113 /* When we store the chain pointer, we complement all \ | |
1114 its bits; this should significantly increase its \ | |
1115 bogosity in case someone tries to use the value, and \ | |
1116 should make us crash faster if someone overwrites the \ | |
1117 pointer because when it gets un-complemented in \ | |
1118 ALLOCATED_FIXED_TYPE(), the resulting pointer will be \ | |
1119 extremely bogus. */ \ | |
1120 type##_free_list_tail->chain = \ | |
1121 (Lisp_Free *) ~ (EMACS_UINT) (ptr); \ | |
1122 } \ | |
1123 else \ | |
1124 type##_free_list = (Lisp_Free *) (ptr); \ | |
1125 type##_free_list_tail = (Lisp_Free *) (ptr); \ | |
1126 } while (0) | |
428 | 1127 |
1128 #else /* !ERROR_CHECK_GC */ | |
1129 | |
454 | 1130 #define PUT_FIXED_TYPE_ON_FREE_LIST(type, structtype, ptr) do { \ |
1131 ((Lisp_Free *) (ptr))->chain = type##_free_list; \ | |
1132 type##_free_list = (Lisp_Free *) (ptr); \ | |
1133 } while (0) \ | |
428 | 1134 |
1135 #endif /* !ERROR_CHECK_GC */ | |
1136 | |
1137 /* TYPE and STRUCTTYPE are the same as in ALLOCATE_FIXED_TYPE(). */ | |
1138 | |
1139 #define FREE_FIXED_TYPE(type, structtype, ptr) do { \ | |
1140 structtype *FFT_ptr = (ptr); \ | |
1204 | 1141 gc_checking_assert (!LRECORD_FREE_P (FFT_ptr)); \ |
2367 | 1142 gc_checking_assert (!DUMPEDP (FFT_ptr)); \ |
428 | 1143 ADDITIONAL_FREE_##type (FFT_ptr); \ |
1144 deadbeef_memory (FFT_ptr, sizeof (structtype)); \ | |
1145 PUT_FIXED_TYPE_ON_FREE_LIST (type, structtype, FFT_ptr); \ | |
454 | 1146 MARK_LRECORD_AS_FREE (FFT_ptr); \ |
428 | 1147 } while (0) |
1148 | |
1149 /* Like FREE_FIXED_TYPE() but used when we are explicitly | |
1150 freeing a structure through free_cons(), free_marker(), etc. | |
1151 rather than through the normal process of sweeping. | |
1152 We attempt to undo the changes made to the allocation counters | |
1153 as a result of this structure being allocated. This is not | |
1154 completely necessary but helps keep things saner: e.g. this way, | |
1155 repeatedly allocating and freeing a cons will not result in | |
1156 the consing-since-gc counter advancing, which would cause a GC | |
1204 | 1157 and somewhat defeat the purpose of explicitly freeing. |
1158 | |
1159 We also disable this mechanism entirely when ALLOC_NO_POOLS is | |
1160 set, which is used for Purify and the like. */ | |
1161 | |
1162 #ifndef ALLOC_NO_POOLS | |
428 | 1163 #define FREE_FIXED_TYPE_WHEN_NOT_IN_GC(type, structtype, ptr) \ |
1164 do { FREE_FIXED_TYPE (type, structtype, ptr); \ | |
1165 DECREMENT_CONS_COUNTER (sizeof (structtype)); \ | |
1166 gc_count_num_##type##_freelist++; \ | |
1167 } while (0) | |
1204 | 1168 #else |
1169 #define FREE_FIXED_TYPE_WHEN_NOT_IN_GC(type, structtype, ptr) | |
1170 #endif | |
2720 | 1171 #endif /* not MC_ALLOC */ |
428 | 1172 |
1173 | |
1174 | |
1175 /************************************************************************/ | |
1176 /* Cons allocation */ | |
1177 /************************************************************************/ | |
1178 | |
2720 | 1179 #ifndef MC_ALLOC |
440 | 1180 DECLARE_FIXED_TYPE_ALLOC (cons, Lisp_Cons); |
428 | 1181 /* conses are used and freed so often that we set this really high */ |
1182 /* #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_cons 20000 */ | |
1183 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_cons 2000 | |
2720 | 1184 #endif /* not MC_ALLOC */ |
428 | 1185 |
1186 static Lisp_Object | |
1187 mark_cons (Lisp_Object obj) | |
1188 { | |
1189 if (NILP (XCDR (obj))) | |
1190 return XCAR (obj); | |
1191 | |
1192 mark_object (XCAR (obj)); | |
1193 return XCDR (obj); | |
1194 } | |
1195 | |
1196 static int | |
1197 cons_equal (Lisp_Object ob1, Lisp_Object ob2, int depth) | |
1198 { | |
442 | 1199 depth++; |
1200 while (internal_equal (XCAR (ob1), XCAR (ob2), depth)) | |
428 | 1201 { |
1202 ob1 = XCDR (ob1); | |
1203 ob2 = XCDR (ob2); | |
1204 if (! CONSP (ob1) || ! CONSP (ob2)) | |
442 | 1205 return internal_equal (ob1, ob2, depth); |
428 | 1206 } |
1207 return 0; | |
1208 } | |
1209 | |
1204 | 1210 static const struct memory_description cons_description[] = { |
853 | 1211 { XD_LISP_OBJECT, offsetof (Lisp_Cons, car_) }, |
1212 { XD_LISP_OBJECT, offsetof (Lisp_Cons, cdr_) }, | |
428 | 1213 { XD_END } |
1214 }; | |
1215 | |
934 | 1216 DEFINE_BASIC_LRECORD_IMPLEMENTATION ("cons", cons, |
1217 1, /*dumpable-flag*/ | |
1218 mark_cons, print_cons, 0, | |
1219 cons_equal, | |
1220 /* | |
1221 * No `hash' method needed. | |
1222 * internal_hash knows how to | |
1223 * handle conses. | |
1224 */ | |
1225 0, | |
1226 cons_description, | |
1227 Lisp_Cons); | |
428 | 1228 |
1229 DEFUN ("cons", Fcons, 2, 2, 0, /* | |
1230 Create a new cons, give it CAR and CDR as components, and return it. | |
1231 */ | |
1232 (car, cdr)) | |
1233 { | |
1234 /* This cannot GC. */ | |
1235 Lisp_Object val; | |
440 | 1236 Lisp_Cons *c; |
1237 | |
2720 | 1238 #ifdef MC_ALLOC |
1239 c = alloc_lrecord_type (Lisp_Cons, &lrecord_cons); | |
1240 #else /* not MC_ALLOC */ | |
440 | 1241 ALLOCATE_FIXED_TYPE (cons, Lisp_Cons, c); |
442 | 1242 set_lheader_implementation (&c->lheader, &lrecord_cons); |
2720 | 1243 #endif /* not MC_ALLOC */ |
793 | 1244 val = wrap_cons (c); |
853 | 1245 XSETCAR (val, car); |
1246 XSETCDR (val, cdr); | |
428 | 1247 return val; |
1248 } | |
1249 | |
1250 /* This is identical to Fcons() but it used for conses that we're | |
1251 going to free later, and is useful when trying to track down | |
1252 "real" consing. */ | |
1253 Lisp_Object | |
1254 noseeum_cons (Lisp_Object car, Lisp_Object cdr) | |
1255 { | |
1256 Lisp_Object val; | |
440 | 1257 Lisp_Cons *c; |
1258 | |
2720 | 1259 #ifdef MC_ALLOC |
1260 c = noseeum_alloc_lrecord_type (Lisp_Cons, &lrecord_cons); | |
1261 #else /* not MC_ALLOC */ | |
440 | 1262 NOSEEUM_ALLOCATE_FIXED_TYPE (cons, Lisp_Cons, c); |
442 | 1263 set_lheader_implementation (&c->lheader, &lrecord_cons); |
2720 | 1264 #endif /* not MC_ALLOC */ |
793 | 1265 val = wrap_cons (c); |
428 | 1266 XCAR (val) = car; |
1267 XCDR (val) = cdr; | |
1268 return val; | |
1269 } | |
1270 | |
1271 DEFUN ("list", Flist, 0, MANY, 0, /* | |
1272 Return a newly created list with specified arguments as elements. | |
1273 Any number of arguments, even zero arguments, are allowed. | |
1274 */ | |
1275 (int nargs, Lisp_Object *args)) | |
1276 { | |
1277 Lisp_Object val = Qnil; | |
1278 Lisp_Object *argp = args + nargs; | |
1279 | |
1280 while (argp > args) | |
1281 val = Fcons (*--argp, val); | |
1282 return val; | |
1283 } | |
1284 | |
1285 Lisp_Object | |
1286 list1 (Lisp_Object obj0) | |
1287 { | |
1288 /* This cannot GC. */ | |
1289 return Fcons (obj0, Qnil); | |
1290 } | |
1291 | |
1292 Lisp_Object | |
1293 list2 (Lisp_Object obj0, Lisp_Object obj1) | |
1294 { | |
1295 /* This cannot GC. */ | |
1296 return Fcons (obj0, Fcons (obj1, Qnil)); | |
1297 } | |
1298 | |
1299 Lisp_Object | |
1300 list3 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2) | |
1301 { | |
1302 /* This cannot GC. */ | |
1303 return Fcons (obj0, Fcons (obj1, Fcons (obj2, Qnil))); | |
1304 } | |
1305 | |
1306 Lisp_Object | |
1307 cons3 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2) | |
1308 { | |
1309 /* This cannot GC. */ | |
1310 return Fcons (obj0, Fcons (obj1, obj2)); | |
1311 } | |
1312 | |
1313 Lisp_Object | |
1314 acons (Lisp_Object key, Lisp_Object value, Lisp_Object alist) | |
1315 { | |
1316 return Fcons (Fcons (key, value), alist); | |
1317 } | |
1318 | |
1319 Lisp_Object | |
1320 list4 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2, Lisp_Object obj3) | |
1321 { | |
1322 /* This cannot GC. */ | |
1323 return Fcons (obj0, Fcons (obj1, Fcons (obj2, Fcons (obj3, Qnil)))); | |
1324 } | |
1325 | |
1326 Lisp_Object | |
1327 list5 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2, Lisp_Object obj3, | |
1328 Lisp_Object obj4) | |
1329 { | |
1330 /* This cannot GC. */ | |
1331 return Fcons (obj0, Fcons (obj1, Fcons (obj2, Fcons (obj3, Fcons (obj4, Qnil))))); | |
1332 } | |
1333 | |
1334 Lisp_Object | |
1335 list6 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2, Lisp_Object obj3, | |
1336 Lisp_Object obj4, Lisp_Object obj5) | |
1337 { | |
1338 /* This cannot GC. */ | |
1339 return Fcons (obj0, Fcons (obj1, Fcons (obj2, Fcons (obj3, Fcons (obj4, Fcons (obj5, Qnil)))))); | |
1340 } | |
1341 | |
1342 DEFUN ("make-list", Fmake_list, 2, 2, 0, /* | |
444 | 1343 Return a new list of length LENGTH, with each element being OBJECT. |
428 | 1344 */ |
444 | 1345 (length, object)) |
428 | 1346 { |
1347 CHECK_NATNUM (length); | |
1348 | |
1349 { | |
1350 Lisp_Object val = Qnil; | |
647 | 1351 EMACS_INT size = XINT (length); |
428 | 1352 |
1353 while (size--) | |
444 | 1354 val = Fcons (object, val); |
428 | 1355 return val; |
1356 } | |
1357 } | |
1358 | |
1359 | |
1360 /************************************************************************/ | |
1361 /* Float allocation */ | |
1362 /************************************************************************/ | |
1363 | |
1983 | 1364 /*** With enhanced number support, these are short floats */ |
1365 | |
2720 | 1366 #ifndef MC_ALLOC |
440 | 1367 DECLARE_FIXED_TYPE_ALLOC (float, Lisp_Float); |
428 | 1368 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_float 1000 |
2720 | 1369 #endif /* not MC_ALLOC */ |
428 | 1370 |
1371 Lisp_Object | |
1372 make_float (double float_value) | |
1373 { | |
440 | 1374 Lisp_Float *f; |
1375 | |
2720 | 1376 #ifdef MC_ALLOC |
1377 f = alloc_lrecord_type (Lisp_Float, &lrecord_float); | |
1378 #else /* not MC_ALLOC */ | |
440 | 1379 ALLOCATE_FIXED_TYPE (float, Lisp_Float, f); |
1380 | |
1381 /* Avoid dump-time `uninitialized memory read' purify warnings. */ | |
1382 if (sizeof (struct lrecord_header) + sizeof (double) != sizeof (*f)) | |
1383 xzero (*f); | |
2720 | 1384 #endif /* not MC_ALLOC */ |
440 | 1385 |
442 | 1386 set_lheader_implementation (&f->lheader, &lrecord_float); |
428 | 1387 float_data (f) = float_value; |
793 | 1388 return wrap_float (f); |
428 | 1389 } |
1390 | |
1391 | |
1392 /************************************************************************/ | |
1983 | 1393 /* Enhanced number allocation */ |
1394 /************************************************************************/ | |
1395 | |
1396 /*** Bignum ***/ | |
1397 #ifdef HAVE_BIGNUM | |
2720 | 1398 #ifndef MC_ALLOC |
1983 | 1399 DECLARE_FIXED_TYPE_ALLOC (bignum, Lisp_Bignum); |
1400 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_bignum 250 | |
2720 | 1401 #endif /* not MC_ALLOC */ |
1983 | 1402 |
1403 /* WARNING: This function returns a bignum even if its argument fits into a | |
1404 fixnum. See Fcanonicalize_number(). */ | |
1405 Lisp_Object | |
1406 make_bignum (long bignum_value) | |
1407 { | |
1408 Lisp_Bignum *b; | |
1409 | |
2720 | 1410 #ifdef MC_ALLOC |
1411 b = alloc_lrecord_type (Lisp_Bignum, &lrecord_bignum); | |
1412 #else /* not MC_ALLOC */ | |
1983 | 1413 ALLOCATE_FIXED_TYPE (bignum, Lisp_Bignum, b); |
1414 set_lheader_implementation (&b->lheader, &lrecord_bignum); | |
2720 | 1415 #endif /* not MC_ALLOC */ |
1983 | 1416 bignum_init (bignum_data (b)); |
1417 bignum_set_long (bignum_data (b), bignum_value); | |
1418 return wrap_bignum (b); | |
1419 } | |
1420 | |
1421 /* WARNING: This function returns a bignum even if its argument fits into a | |
1422 fixnum. See Fcanonicalize_number(). */ | |
1423 Lisp_Object | |
1424 make_bignum_bg (bignum bg) | |
1425 { | |
1426 Lisp_Bignum *b; | |
1427 | |
2720 | 1428 #ifdef MC_ALLOC |
1429 b = alloc_lrecord_type (Lisp_Bignum, &lrecord_bignum); | |
1430 #else /* not MC_ALLOC */ | |
1983 | 1431 ALLOCATE_FIXED_TYPE (bignum, Lisp_Bignum, b); |
1432 set_lheader_implementation (&b->lheader, &lrecord_bignum); | |
2720 | 1433 #endif /* not MC_ALLOC */ |
1983 | 1434 bignum_init (bignum_data (b)); |
1435 bignum_set (bignum_data (b), bg); | |
1436 return wrap_bignum (b); | |
1437 } | |
1438 #endif /* HAVE_BIGNUM */ | |
1439 | |
1440 /*** Ratio ***/ | |
1441 #ifdef HAVE_RATIO | |
2720 | 1442 #ifndef MC_ALLOC |
1983 | 1443 DECLARE_FIXED_TYPE_ALLOC (ratio, Lisp_Ratio); |
1444 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_ratio 250 | |
2720 | 1445 #endif /* not MC_ALLOC */ |
1983 | 1446 |
1447 Lisp_Object | |
1448 make_ratio (long numerator, unsigned long denominator) | |
1449 { | |
1450 Lisp_Ratio *r; | |
1451 | |
2720 | 1452 #ifdef MC_ALLOC |
1453 r = alloc_lrecord_type (Lisp_Ratio, &lrecord_ratio); | |
1454 #else /* not MC_ALLOC */ | |
1983 | 1455 ALLOCATE_FIXED_TYPE (ratio, Lisp_Ratio, r); |
1456 set_lheader_implementation (&r->lheader, &lrecord_ratio); | |
2720 | 1457 #endif /* not MC_ALLOC */ |
1983 | 1458 ratio_init (ratio_data (r)); |
1459 ratio_set_long_ulong (ratio_data (r), numerator, denominator); | |
1460 ratio_canonicalize (ratio_data (r)); | |
1461 return wrap_ratio (r); | |
1462 } | |
1463 | |
1464 Lisp_Object | |
1465 make_ratio_bg (bignum numerator, bignum denominator) | |
1466 { | |
1467 Lisp_Ratio *r; | |
1468 | |
2720 | 1469 #ifdef MC_ALLOC |
1470 r = alloc_lrecord_type (Lisp_Ratio, &lrecord_ratio); | |
1471 #else /* not MC_ALLOC */ | |
1983 | 1472 ALLOCATE_FIXED_TYPE (ratio, Lisp_Ratio, r); |
1473 set_lheader_implementation (&r->lheader, &lrecord_ratio); | |
2720 | 1474 #endif /* not MC_ALLOC */ |
1983 | 1475 ratio_init (ratio_data (r)); |
1476 ratio_set_bignum_bignum (ratio_data (r), numerator, denominator); | |
1477 ratio_canonicalize (ratio_data (r)); | |
1478 return wrap_ratio (r); | |
1479 } | |
1480 | |
1481 Lisp_Object | |
1482 make_ratio_rt (ratio rat) | |
1483 { | |
1484 Lisp_Ratio *r; | |
1485 | |
2720 | 1486 #ifdef MC_ALLOC |
1487 r = alloc_lrecord_type (Lisp_Ratio, &lrecord_ratio); | |
1488 #else /* not MC_ALLOC */ | |
1983 | 1489 ALLOCATE_FIXED_TYPE (ratio, Lisp_Ratio, r); |
1490 set_lheader_implementation (&r->lheader, &lrecord_ratio); | |
2720 | 1491 #endif /* not MC_ALLOC */ |
1983 | 1492 ratio_init (ratio_data (r)); |
1493 ratio_set (ratio_data (r), rat); | |
1494 return wrap_ratio (r); | |
1495 } | |
1496 #endif /* HAVE_RATIO */ | |
1497 | |
1498 /*** Bigfloat ***/ | |
1499 #ifdef HAVE_BIGFLOAT | |
2720 | 1500 #ifndef MC_ALLOC |
1983 | 1501 DECLARE_FIXED_TYPE_ALLOC (bigfloat, Lisp_Bigfloat); |
1502 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_bigfloat 250 | |
2720 | 1503 #endif /* not MC_ALLOC */ |
1983 | 1504 |
1505 /* This function creates a bigfloat with the default precision if the | |
1506 PRECISION argument is zero. */ | |
1507 Lisp_Object | |
1508 make_bigfloat (double float_value, unsigned long precision) | |
1509 { | |
1510 Lisp_Bigfloat *f; | |
1511 | |
2720 | 1512 #ifdef MC_ALLOC |
1513 f = alloc_lrecord_type (Lisp_Bigfloat, &lrecord_bigfloat); | |
1514 #else /* not MC_ALLOC */ | |
1983 | 1515 ALLOCATE_FIXED_TYPE (bigfloat, Lisp_Bigfloat, f); |
1516 set_lheader_implementation (&f->lheader, &lrecord_bigfloat); | |
2720 | 1517 #endif /* not MC_ALLOC */ |
1983 | 1518 if (precision == 0UL) |
1519 bigfloat_init (bigfloat_data (f)); | |
1520 else | |
1521 bigfloat_init_prec (bigfloat_data (f), precision); | |
1522 bigfloat_set_double (bigfloat_data (f), float_value); | |
1523 return wrap_bigfloat (f); | |
1524 } | |
1525 | |
1526 /* This function creates a bigfloat with the precision of its argument */ | |
1527 Lisp_Object | |
1528 make_bigfloat_bf (bigfloat float_value) | |
1529 { | |
1530 Lisp_Bigfloat *f; | |
1531 | |
2720 | 1532 #ifdef MC_ALLOC |
1533 f = alloc_lrecord_type (Lisp_Bigfloat, &lrecord_bigfloat); | |
1534 #else /* not MC_ALLOC */ | |
1983 | 1535 ALLOCATE_FIXED_TYPE (bigfloat, Lisp_Bigfloat, f); |
1536 set_lheader_implementation (&f->lheader, &lrecord_bigfloat); | |
2720 | 1537 #endif /* not MC_ALLOC */ |
1983 | 1538 bigfloat_init_prec (bigfloat_data (f), bigfloat_get_prec (float_value)); |
1539 bigfloat_set (bigfloat_data (f), float_value); | |
1540 return wrap_bigfloat (f); | |
1541 } | |
1542 #endif /* HAVE_BIGFLOAT */ | |
1543 | |
1544 /************************************************************************/ | |
428 | 1545 /* Vector allocation */ |
1546 /************************************************************************/ | |
1547 | |
1548 static Lisp_Object | |
1549 mark_vector (Lisp_Object obj) | |
1550 { | |
1551 Lisp_Vector *ptr = XVECTOR (obj); | |
1552 int len = vector_length (ptr); | |
1553 int i; | |
1554 | |
1555 for (i = 0; i < len - 1; i++) | |
1556 mark_object (ptr->contents[i]); | |
1557 return (len > 0) ? ptr->contents[len - 1] : Qnil; | |
1558 } | |
1559 | |
665 | 1560 static Bytecount |
442 | 1561 size_vector (const void *lheader) |
428 | 1562 { |
456 | 1563 return FLEXIBLE_ARRAY_STRUCT_SIZEOF (Lisp_Vector, Lisp_Object, contents, |
442 | 1564 ((Lisp_Vector *) lheader)->size); |
428 | 1565 } |
1566 | |
1567 static int | |
1568 vector_equal (Lisp_Object obj1, Lisp_Object obj2, int depth) | |
1569 { | |
1570 int len = XVECTOR_LENGTH (obj1); | |
1571 if (len != XVECTOR_LENGTH (obj2)) | |
1572 return 0; | |
1573 | |
1574 { | |
1575 Lisp_Object *ptr1 = XVECTOR_DATA (obj1); | |
1576 Lisp_Object *ptr2 = XVECTOR_DATA (obj2); | |
1577 while (len--) | |
1578 if (!internal_equal (*ptr1++, *ptr2++, depth + 1)) | |
1579 return 0; | |
1580 } | |
1581 return 1; | |
1582 } | |
1583 | |
665 | 1584 static Hashcode |
442 | 1585 vector_hash (Lisp_Object obj, int depth) |
1586 { | |
1587 return HASH2 (XVECTOR_LENGTH (obj), | |
1588 internal_array_hash (XVECTOR_DATA (obj), | |
1589 XVECTOR_LENGTH (obj), | |
1590 depth + 1)); | |
1591 } | |
1592 | |
1204 | 1593 static const struct memory_description vector_description[] = { |
440 | 1594 { XD_LONG, offsetof (Lisp_Vector, size) }, |
1595 { XD_LISP_OBJECT_ARRAY, offsetof (Lisp_Vector, contents), XD_INDIRECT(0, 0) }, | |
428 | 1596 { XD_END } |
1597 }; | |
1598 | |
1204 | 1599 DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION ("vector", vector, |
1600 1, /*dumpable-flag*/ | |
1601 mark_vector, print_vector, 0, | |
1602 vector_equal, | |
1603 vector_hash, | |
1604 vector_description, | |
1605 size_vector, Lisp_Vector); | |
428 | 1606 /* #### should allocate `small' vectors from a frob-block */ |
1607 static Lisp_Vector * | |
665 | 1608 make_vector_internal (Elemcount sizei) |
428 | 1609 { |
1204 | 1610 /* no `next' field; we use lcrecords */ |
665 | 1611 Bytecount sizem = FLEXIBLE_ARRAY_STRUCT_SIZEOF (Lisp_Vector, Lisp_Object, |
1204 | 1612 contents, sizei); |
1613 Lisp_Vector *p = | |
2720 | 1614 #ifdef MC_ALLOC |
1615 (Lisp_Vector *) alloc_lrecord (sizem, &lrecord_vector); | |
1616 #else /* not MC_ALLOC */ | |
1204 | 1617 (Lisp_Vector *) basic_alloc_lcrecord (sizem, &lrecord_vector); |
2720 | 1618 #endif /* not MC_ALLOC */ |
428 | 1619 |
1620 p->size = sizei; | |
1621 return p; | |
1622 } | |
1623 | |
1624 Lisp_Object | |
665 | 1625 make_vector (Elemcount length, Lisp_Object object) |
428 | 1626 { |
1627 Lisp_Vector *vecp = make_vector_internal (length); | |
1628 Lisp_Object *p = vector_data (vecp); | |
1629 | |
1630 while (length--) | |
444 | 1631 *p++ = object; |
428 | 1632 |
793 | 1633 return wrap_vector (vecp); |
428 | 1634 } |
1635 | |
1636 DEFUN ("make-vector", Fmake_vector, 2, 2, 0, /* | |
444 | 1637 Return a new vector of length LENGTH, with each element being OBJECT. |
428 | 1638 See also the function `vector'. |
1639 */ | |
444 | 1640 (length, object)) |
428 | 1641 { |
1642 CONCHECK_NATNUM (length); | |
444 | 1643 return make_vector (XINT (length), object); |
428 | 1644 } |
1645 | |
1646 DEFUN ("vector", Fvector, 0, MANY, 0, /* | |
1647 Return a newly created vector with specified arguments as elements. | |
1648 Any number of arguments, even zero arguments, are allowed. | |
1649 */ | |
1650 (int nargs, Lisp_Object *args)) | |
1651 { | |
1652 Lisp_Vector *vecp = make_vector_internal (nargs); | |
1653 Lisp_Object *p = vector_data (vecp); | |
1654 | |
1655 while (nargs--) | |
1656 *p++ = *args++; | |
1657 | |
793 | 1658 return wrap_vector (vecp); |
428 | 1659 } |
1660 | |
1661 Lisp_Object | |
1662 vector1 (Lisp_Object obj0) | |
1663 { | |
1664 return Fvector (1, &obj0); | |
1665 } | |
1666 | |
1667 Lisp_Object | |
1668 vector2 (Lisp_Object obj0, Lisp_Object obj1) | |
1669 { | |
1670 Lisp_Object args[2]; | |
1671 args[0] = obj0; | |
1672 args[1] = obj1; | |
1673 return Fvector (2, args); | |
1674 } | |
1675 | |
1676 Lisp_Object | |
1677 vector3 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2) | |
1678 { | |
1679 Lisp_Object args[3]; | |
1680 args[0] = obj0; | |
1681 args[1] = obj1; | |
1682 args[2] = obj2; | |
1683 return Fvector (3, args); | |
1684 } | |
1685 | |
1686 #if 0 /* currently unused */ | |
1687 | |
1688 Lisp_Object | |
1689 vector4 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2, | |
1690 Lisp_Object obj3) | |
1691 { | |
1692 Lisp_Object args[4]; | |
1693 args[0] = obj0; | |
1694 args[1] = obj1; | |
1695 args[2] = obj2; | |
1696 args[3] = obj3; | |
1697 return Fvector (4, args); | |
1698 } | |
1699 | |
1700 Lisp_Object | |
1701 vector5 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2, | |
1702 Lisp_Object obj3, Lisp_Object obj4) | |
1703 { | |
1704 Lisp_Object args[5]; | |
1705 args[0] = obj0; | |
1706 args[1] = obj1; | |
1707 args[2] = obj2; | |
1708 args[3] = obj3; | |
1709 args[4] = obj4; | |
1710 return Fvector (5, args); | |
1711 } | |
1712 | |
1713 Lisp_Object | |
1714 vector6 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2, | |
1715 Lisp_Object obj3, Lisp_Object obj4, Lisp_Object obj5) | |
1716 { | |
1717 Lisp_Object args[6]; | |
1718 args[0] = obj0; | |
1719 args[1] = obj1; | |
1720 args[2] = obj2; | |
1721 args[3] = obj3; | |
1722 args[4] = obj4; | |
1723 args[5] = obj5; | |
1724 return Fvector (6, args); | |
1725 } | |
1726 | |
1727 Lisp_Object | |
1728 vector7 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2, | |
1729 Lisp_Object obj3, Lisp_Object obj4, Lisp_Object obj5, | |
1730 Lisp_Object obj6) | |
1731 { | |
1732 Lisp_Object args[7]; | |
1733 args[0] = obj0; | |
1734 args[1] = obj1; | |
1735 args[2] = obj2; | |
1736 args[3] = obj3; | |
1737 args[4] = obj4; | |
1738 args[5] = obj5; | |
1739 args[6] = obj6; | |
1740 return Fvector (7, args); | |
1741 } | |
1742 | |
1743 Lisp_Object | |
1744 vector8 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2, | |
1745 Lisp_Object obj3, Lisp_Object obj4, Lisp_Object obj5, | |
1746 Lisp_Object obj6, Lisp_Object obj7) | |
1747 { | |
1748 Lisp_Object args[8]; | |
1749 args[0] = obj0; | |
1750 args[1] = obj1; | |
1751 args[2] = obj2; | |
1752 args[3] = obj3; | |
1753 args[4] = obj4; | |
1754 args[5] = obj5; | |
1755 args[6] = obj6; | |
1756 args[7] = obj7; | |
1757 return Fvector (8, args); | |
1758 } | |
1759 #endif /* unused */ | |
1760 | |
1761 /************************************************************************/ | |
1762 /* Bit Vector allocation */ | |
1763 /************************************************************************/ | |
1764 | |
1765 /* #### should allocate `small' bit vectors from a frob-block */ | |
440 | 1766 static Lisp_Bit_Vector * |
665 | 1767 make_bit_vector_internal (Elemcount sizei) |
428 | 1768 { |
1204 | 1769 /* no `next' field; we use lcrecords */ |
665 | 1770 Elemcount num_longs = BIT_VECTOR_LONG_STORAGE (sizei); |
1771 Bytecount sizem = FLEXIBLE_ARRAY_STRUCT_SIZEOF (Lisp_Bit_Vector, | |
1204 | 1772 unsigned long, |
1773 bits, num_longs); | |
1774 Lisp_Bit_Vector *p = (Lisp_Bit_Vector *) | |
2720 | 1775 #ifdef MC_ALLOC |
1776 alloc_lrecord (sizem, &lrecord_bit_vector); | |
1777 #else /* not MC_ALLOC */ | |
1204 | 1778 basic_alloc_lcrecord (sizem, &lrecord_bit_vector); |
2720 | 1779 #endif /* not MC_ALLOC */ |
428 | 1780 |
1781 bit_vector_length (p) = sizei; | |
1782 return p; | |
1783 } | |
1784 | |
1785 Lisp_Object | |
665 | 1786 make_bit_vector (Elemcount length, Lisp_Object bit) |
428 | 1787 { |
440 | 1788 Lisp_Bit_Vector *p = make_bit_vector_internal (length); |
665 | 1789 Elemcount num_longs = BIT_VECTOR_LONG_STORAGE (length); |
428 | 1790 |
444 | 1791 CHECK_BIT (bit); |
1792 | |
1793 if (ZEROP (bit)) | |
428 | 1794 memset (p->bits, 0, num_longs * sizeof (long)); |
1795 else | |
1796 { | |
665 | 1797 Elemcount bits_in_last = length & (LONGBITS_POWER_OF_2 - 1); |
428 | 1798 memset (p->bits, ~0, num_longs * sizeof (long)); |
1799 /* But we have to make sure that the unused bits in the | |
1800 last long are 0, so that equal/hash is easy. */ | |
1801 if (bits_in_last) | |
1802 p->bits[num_longs - 1] &= (1 << bits_in_last) - 1; | |
1803 } | |
1804 | |
793 | 1805 return wrap_bit_vector (p); |
428 | 1806 } |
1807 | |
1808 Lisp_Object | |
665 | 1809 make_bit_vector_from_byte_vector (unsigned char *bytevec, Elemcount length) |
428 | 1810 { |
665 | 1811 Elemcount i; |
428 | 1812 Lisp_Bit_Vector *p = make_bit_vector_internal (length); |
1813 | |
1814 for (i = 0; i < length; i++) | |
1815 set_bit_vector_bit (p, i, bytevec[i]); | |
1816 | |
793 | 1817 return wrap_bit_vector (p); |
428 | 1818 } |
1819 | |
1820 DEFUN ("make-bit-vector", Fmake_bit_vector, 2, 2, 0, /* | |
444 | 1821 Return a new bit vector of length LENGTH. with each bit set to BIT. |
1822 BIT must be one of the integers 0 or 1. See also the function `bit-vector'. | |
428 | 1823 */ |
444 | 1824 (length, bit)) |
428 | 1825 { |
1826 CONCHECK_NATNUM (length); | |
1827 | |
444 | 1828 return make_bit_vector (XINT (length), bit); |
428 | 1829 } |
1830 | |
1831 DEFUN ("bit-vector", Fbit_vector, 0, MANY, 0, /* | |
1832 Return a newly created bit vector with specified arguments as elements. | |
1833 Any number of arguments, even zero arguments, are allowed. | |
444 | 1834 Each argument must be one of the integers 0 or 1. |
428 | 1835 */ |
1836 (int nargs, Lisp_Object *args)) | |
1837 { | |
1838 int i; | |
1839 Lisp_Bit_Vector *p = make_bit_vector_internal (nargs); | |
1840 | |
1841 for (i = 0; i < nargs; i++) | |
1842 { | |
1843 CHECK_BIT (args[i]); | |
1844 set_bit_vector_bit (p, i, !ZEROP (args[i])); | |
1845 } | |
1846 | |
793 | 1847 return wrap_bit_vector (p); |
428 | 1848 } |
1849 | |
1850 | |
1851 /************************************************************************/ | |
1852 /* Compiled-function allocation */ | |
1853 /************************************************************************/ | |
1854 | |
2720 | 1855 #ifndef MC_ALLOC |
428 | 1856 DECLARE_FIXED_TYPE_ALLOC (compiled_function, Lisp_Compiled_Function); |
1857 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_compiled_function 1000 | |
2720 | 1858 #endif /* not MC_ALLOC */ |
428 | 1859 |
1860 static Lisp_Object | |
1861 make_compiled_function (void) | |
1862 { | |
1863 Lisp_Compiled_Function *f; | |
1864 | |
2720 | 1865 #ifdef MC_ALLOC |
1866 f = alloc_lrecord_type (Lisp_Compiled_Function, &lrecord_compiled_function); | |
1867 #else /* not MC_ALLOC */ | |
428 | 1868 ALLOCATE_FIXED_TYPE (compiled_function, Lisp_Compiled_Function, f); |
442 | 1869 set_lheader_implementation (&f->lheader, &lrecord_compiled_function); |
2720 | 1870 #endif /* not MC_ALLOC */ |
428 | 1871 |
1872 f->stack_depth = 0; | |
1873 f->specpdl_depth = 0; | |
1874 f->flags.documentationp = 0; | |
1875 f->flags.interactivep = 0; | |
1876 f->flags.domainp = 0; /* I18N3 */ | |
1877 f->instructions = Qzero; | |
1878 f->constants = Qzero; | |
1879 f->arglist = Qnil; | |
1739 | 1880 f->args = NULL; |
1881 f->max_args = f->min_args = f->args_in_array = 0; | |
428 | 1882 f->doc_and_interactive = Qnil; |
1883 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK | |
1884 f->annotated = Qnil; | |
1885 #endif | |
793 | 1886 return wrap_compiled_function (f); |
428 | 1887 } |
1888 | |
1889 DEFUN ("make-byte-code", Fmake_byte_code, 4, MANY, 0, /* | |
1890 Return a new compiled-function object. | |
1891 Usage: (arglist instructions constants stack-depth | |
1892 &optional doc-string interactive) | |
1893 Note that, unlike all other emacs-lisp functions, calling this with five | |
1894 arguments is NOT the same as calling it with six arguments, the last of | |
1895 which is nil. If the INTERACTIVE arg is specified as nil, then that means | |
1896 that this function was defined with `(interactive)'. If the arg is not | |
1897 specified, then that means the function is not interactive. | |
1898 This is terrible behavior which is retained for compatibility with old | |
1899 `.elc' files which expect these semantics. | |
1900 */ | |
1901 (int nargs, Lisp_Object *args)) | |
1902 { | |
1903 /* In a non-insane world this function would have this arglist... | |
1904 (arglist instructions constants stack_depth &optional doc_string interactive) | |
1905 */ | |
1906 Lisp_Object fun = make_compiled_function (); | |
1907 Lisp_Compiled_Function *f = XCOMPILED_FUNCTION (fun); | |
1908 | |
1909 Lisp_Object arglist = args[0]; | |
1910 Lisp_Object instructions = args[1]; | |
1911 Lisp_Object constants = args[2]; | |
1912 Lisp_Object stack_depth = args[3]; | |
1913 Lisp_Object doc_string = (nargs > 4) ? args[4] : Qnil; | |
1914 Lisp_Object interactive = (nargs > 5) ? args[5] : Qunbound; | |
1915 | |
1916 if (nargs < 4 || nargs > 6) | |
1917 return Fsignal (Qwrong_number_of_arguments, | |
1918 list2 (intern ("make-byte-code"), make_int (nargs))); | |
1919 | |
1920 /* Check for valid formal parameter list now, to allow us to use | |
1921 SPECBIND_FAST_UNSAFE() later in funcall_compiled_function(). */ | |
1922 { | |
814 | 1923 EXTERNAL_LIST_LOOP_2 (symbol, arglist) |
428 | 1924 { |
1925 CHECK_SYMBOL (symbol); | |
1926 if (EQ (symbol, Qt) || | |
1927 EQ (symbol, Qnil) || | |
1928 SYMBOL_IS_KEYWORD (symbol)) | |
563 | 1929 invalid_constant_2 |
428 | 1930 ("Invalid constant symbol in formal parameter list", |
1931 symbol, arglist); | |
1932 } | |
1933 } | |
1934 f->arglist = arglist; | |
1935 | |
1936 /* `instructions' is a string or a cons (string . int) for a | |
1937 lazy-loaded function. */ | |
1938 if (CONSP (instructions)) | |
1939 { | |
1940 CHECK_STRING (XCAR (instructions)); | |
1941 CHECK_INT (XCDR (instructions)); | |
1942 } | |
1943 else | |
1944 { | |
1945 CHECK_STRING (instructions); | |
1946 } | |
1947 f->instructions = instructions; | |
1948 | |
1949 if (!NILP (constants)) | |
1950 CHECK_VECTOR (constants); | |
1951 f->constants = constants; | |
1952 | |
1953 CHECK_NATNUM (stack_depth); | |
442 | 1954 f->stack_depth = (unsigned short) XINT (stack_depth); |
428 | 1955 |
1956 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK | |
1957 if (!NILP (Vcurrent_compiled_function_annotation)) | |
1958 f->annotated = Fcopy (Vcurrent_compiled_function_annotation); | |
1959 else if (!NILP (Vload_file_name_internal_the_purecopy)) | |
1960 f->annotated = Vload_file_name_internal_the_purecopy; | |
1961 else if (!NILP (Vload_file_name_internal)) | |
1962 { | |
1963 struct gcpro gcpro1; | |
1964 GCPRO1 (fun); /* don't let fun get reaped */ | |
1965 Vload_file_name_internal_the_purecopy = | |
1966 Ffile_name_nondirectory (Vload_file_name_internal); | |
1967 f->annotated = Vload_file_name_internal_the_purecopy; | |
1968 UNGCPRO; | |
1969 } | |
1970 #endif /* COMPILED_FUNCTION_ANNOTATION_HACK */ | |
1971 | |
1972 /* doc_string may be nil, string, int, or a cons (string . int). | |
1973 interactive may be list or string (or unbound). */ | |
1974 f->doc_and_interactive = Qunbound; | |
1975 #ifdef I18N3 | |
1976 if ((f->flags.domainp = !NILP (Vfile_domain)) != 0) | |
1977 f->doc_and_interactive = Vfile_domain; | |
1978 #endif | |
1979 if ((f->flags.interactivep = !UNBOUNDP (interactive)) != 0) | |
1980 { | |
1981 f->doc_and_interactive | |
1982 = (UNBOUNDP (f->doc_and_interactive) ? interactive : | |
1983 Fcons (interactive, f->doc_and_interactive)); | |
1984 } | |
1985 if ((f->flags.documentationp = !NILP (doc_string)) != 0) | |
1986 { | |
1987 f->doc_and_interactive | |
1988 = (UNBOUNDP (f->doc_and_interactive) ? doc_string : | |
1989 Fcons (doc_string, f->doc_and_interactive)); | |
1990 } | |
1991 if (UNBOUNDP (f->doc_and_interactive)) | |
1992 f->doc_and_interactive = Qnil; | |
1993 | |
1994 return fun; | |
1995 } | |
1996 | |
1997 | |
1998 /************************************************************************/ | |
1999 /* Symbol allocation */ | |
2000 /************************************************************************/ | |
2001 | |
2720 | 2002 #ifndef MC_ALLOC |
440 | 2003 DECLARE_FIXED_TYPE_ALLOC (symbol, Lisp_Symbol); |
428 | 2004 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_symbol 1000 |
2720 | 2005 #endif /* not MC_ALLOC */ |
428 | 2006 |
2007 DEFUN ("make-symbol", Fmake_symbol, 1, 1, 0, /* | |
2008 Return a newly allocated uninterned symbol whose name is NAME. | |
2009 Its value and function definition are void, and its property list is nil. | |
2010 */ | |
2011 (name)) | |
2012 { | |
440 | 2013 Lisp_Symbol *p; |
428 | 2014 |
2015 CHECK_STRING (name); | |
2016 | |
2720 | 2017 #ifdef MC_ALLOC |
2018 p = alloc_lrecord_type (Lisp_Symbol, &lrecord_symbol); | |
2019 #else /* not MC_ALLOC */ | |
440 | 2020 ALLOCATE_FIXED_TYPE (symbol, Lisp_Symbol, p); |
442 | 2021 set_lheader_implementation (&p->lheader, &lrecord_symbol); |
2720 | 2022 #endif /* not MC_ALLOC */ |
793 | 2023 p->name = name; |
428 | 2024 p->plist = Qnil; |
2025 p->value = Qunbound; | |
2026 p->function = Qunbound; | |
2027 symbol_next (p) = 0; | |
793 | 2028 return wrap_symbol (p); |
428 | 2029 } |
2030 | |
2031 | |
2032 /************************************************************************/ | |
2033 /* Extent allocation */ | |
2034 /************************************************************************/ | |
2035 | |
2720 | 2036 #ifndef MC_ALLOC |
428 | 2037 DECLARE_FIXED_TYPE_ALLOC (extent, struct extent); |
2038 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_extent 1000 | |
2720 | 2039 #endif /* not MC_ALLOC */ |
428 | 2040 |
2041 struct extent * | |
2042 allocate_extent (void) | |
2043 { | |
2044 struct extent *e; | |
2045 | |
2720 | 2046 #ifdef MC_ALLOC |
2047 e = alloc_lrecord_type (struct extent, &lrecord_extent); | |
2048 #else /* not MC_ALLOC */ | |
428 | 2049 ALLOCATE_FIXED_TYPE (extent, struct extent, e); |
442 | 2050 set_lheader_implementation (&e->lheader, &lrecord_extent); |
2720 | 2051 #endif /* not MC_ALLOC */ |
428 | 2052 extent_object (e) = Qnil; |
2053 set_extent_start (e, -1); | |
2054 set_extent_end (e, -1); | |
2055 e->plist = Qnil; | |
2056 | |
2057 xzero (e->flags); | |
2058 | |
2059 extent_face (e) = Qnil; | |
2060 e->flags.end_open = 1; /* default is for endpoints to behave like markers */ | |
2061 e->flags.detachable = 1; | |
2062 | |
2063 return e; | |
2064 } | |
2065 | |
2066 | |
2067 /************************************************************************/ | |
2068 /* Event allocation */ | |
2069 /************************************************************************/ | |
2070 | |
2720 | 2071 #ifndef MC_ALLOC |
440 | 2072 DECLARE_FIXED_TYPE_ALLOC (event, Lisp_Event); |
428 | 2073 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_event 1000 |
2720 | 2074 #endif /* not MC_ALLOC */ |
428 | 2075 |
2076 Lisp_Object | |
2077 allocate_event (void) | |
2078 { | |
440 | 2079 Lisp_Event *e; |
2080 | |
2720 | 2081 #ifdef MC_ALLOC |
2082 e = alloc_lrecord_type (Lisp_Event, &lrecord_event); | |
2083 #else /* not MC_ALLOC */ | |
440 | 2084 ALLOCATE_FIXED_TYPE (event, Lisp_Event, e); |
442 | 2085 set_lheader_implementation (&e->lheader, &lrecord_event); |
2720 | 2086 #endif /* not MC_ALLOC */ |
428 | 2087 |
793 | 2088 return wrap_event (e); |
428 | 2089 } |
2090 | |
1204 | 2091 #ifdef EVENT_DATA_AS_OBJECTS |
2720 | 2092 #ifndef MC_ALLOC |
934 | 2093 DECLARE_FIXED_TYPE_ALLOC (key_data, Lisp_Key_Data); |
2094 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_key_data 1000 | |
2720 | 2095 #endif /* not MC_ALLOC */ |
934 | 2096 |
2097 Lisp_Object | |
1204 | 2098 make_key_data (void) |
934 | 2099 { |
2100 Lisp_Key_Data *d; | |
2101 | |
2720 | 2102 #ifdef MC_ALLOC |
2103 d = alloc_lrecord_type (Lisp_Key_Data, &lrecord_key_data); | |
2104 #else /* not MC_ALLOC */ | |
934 | 2105 ALLOCATE_FIXED_TYPE (key_data, Lisp_Key_Data, d); |
1204 | 2106 xzero (*d); |
934 | 2107 set_lheader_implementation (&d->lheader, &lrecord_key_data); |
2720 | 2108 #endif /* not MC_ALLOC */ |
1204 | 2109 d->keysym = Qnil; |
2110 | |
2111 return wrap_key_data (d); | |
934 | 2112 } |
2113 | |
2720 | 2114 #ifndef MC_ALLOC |
934 | 2115 DECLARE_FIXED_TYPE_ALLOC (button_data, Lisp_Button_Data); |
2116 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_button_data 1000 | |
2720 | 2117 #endif /* not MC_ALLOC */ |
934 | 2118 |
2119 Lisp_Object | |
1204 | 2120 make_button_data (void) |
934 | 2121 { |
2122 Lisp_Button_Data *d; | |
2123 | |
2720 | 2124 #ifdef MC_ALLOC |
2125 d = alloc_lrecord_type (Lisp_Button_Data, &lrecord_button_data); | |
2126 #else /* not MC_ALLOC */ | |
934 | 2127 ALLOCATE_FIXED_TYPE (button_data, Lisp_Button_Data, d); |
1204 | 2128 xzero (*d); |
934 | 2129 set_lheader_implementation (&d->lheader, &lrecord_button_data); |
2130 | |
2720 | 2131 #endif /* not MC_ALLOC */ |
1204 | 2132 return wrap_button_data (d); |
934 | 2133 } |
2134 | |
2720 | 2135 #ifndef MC_ALLOC |
934 | 2136 DECLARE_FIXED_TYPE_ALLOC (motion_data, Lisp_Motion_Data); |
2137 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_motion_data 1000 | |
2720 | 2138 #endif /* not MC_ALLOC */ |
934 | 2139 |
2140 Lisp_Object | |
1204 | 2141 make_motion_data (void) |
934 | 2142 { |
2143 Lisp_Motion_Data *d; | |
2144 | |
2720 | 2145 #ifdef MC_ALLOC |
2146 d = alloc_lrecord_type (Lisp_Motion_Data, &lrecord_motion_data); | |
2147 #else /* not MC_ALLOC */ | |
934 | 2148 ALLOCATE_FIXED_TYPE (motion_data, Lisp_Motion_Data, d); |
1204 | 2149 xzero (*d); |
934 | 2150 set_lheader_implementation (&d->lheader, &lrecord_motion_data); |
2720 | 2151 #endif /* not MC_ALLOC */ |
934 | 2152 |
1204 | 2153 return wrap_motion_data (d); |
934 | 2154 } |
2155 | |
2720 | 2156 #ifndef MC_ALLOC |
934 | 2157 DECLARE_FIXED_TYPE_ALLOC (process_data, Lisp_Process_Data); |
2158 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_process_data 1000 | |
2720 | 2159 #endif /* not MC_ALLOC */ |
934 | 2160 |
2161 Lisp_Object | |
1204 | 2162 make_process_data (void) |
934 | 2163 { |
2164 Lisp_Process_Data *d; | |
2165 | |
2720 | 2166 #ifdef MC_ALLOC |
2167 d = alloc_lrecord_type (Lisp_Process_Data, &lrecord_process_data); | |
2168 #else /* not MC_ALLOC */ | |
934 | 2169 ALLOCATE_FIXED_TYPE (process_data, Lisp_Process_Data, d); |
1204 | 2170 xzero (*d); |
934 | 2171 set_lheader_implementation (&d->lheader, &lrecord_process_data); |
1204 | 2172 d->process = Qnil; |
2720 | 2173 #endif /* not MC_ALLOC */ |
1204 | 2174 |
2175 return wrap_process_data (d); | |
934 | 2176 } |
2177 | |
2720 | 2178 #ifndef MC_ALLOC |
934 | 2179 DECLARE_FIXED_TYPE_ALLOC (timeout_data, Lisp_Timeout_Data); |
2180 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_timeout_data 1000 | |
2720 | 2181 #endif /* not MC_ALLOC */ |
934 | 2182 |
2183 Lisp_Object | |
1204 | 2184 make_timeout_data (void) |
934 | 2185 { |
2186 Lisp_Timeout_Data *d; | |
2187 | |
2720 | 2188 #ifdef MC_ALLOC |
2189 d = alloc_lrecord_type (Lisp_Timeout_Data, &lrecord_timeout_data); | |
2190 #else /* not MC_ALLOC */ | |
934 | 2191 ALLOCATE_FIXED_TYPE (timeout_data, Lisp_Timeout_Data, d); |
1204 | 2192 xzero (*d); |
934 | 2193 set_lheader_implementation (&d->lheader, &lrecord_timeout_data); |
1204 | 2194 d->function = Qnil; |
2195 d->object = Qnil; | |
2720 | 2196 #endif /* not MC_ALLOC */ |
1204 | 2197 |
2198 return wrap_timeout_data (d); | |
934 | 2199 } |
2200 | |
2720 | 2201 #ifndef MC_ALLOC |
934 | 2202 DECLARE_FIXED_TYPE_ALLOC (magic_data, Lisp_Magic_Data); |
2203 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_magic_data 1000 | |
2720 | 2204 #endif /* not MC_ALLOC */ |
934 | 2205 |
2206 Lisp_Object | |
1204 | 2207 make_magic_data (void) |
934 | 2208 { |
2209 Lisp_Magic_Data *d; | |
2210 | |
2720 | 2211 #ifdef MC_ALLOC |
2212 d = alloc_lrecord_type (Lisp_Magic_Data, &lrecord_magic_data); | |
2213 #else /* not MC_ALLOC */ | |
934 | 2214 ALLOCATE_FIXED_TYPE (magic_data, Lisp_Magic_Data, d); |
1204 | 2215 xzero (*d); |
934 | 2216 set_lheader_implementation (&d->lheader, &lrecord_magic_data); |
2720 | 2217 #endif /* not MC_ALLOC */ |
934 | 2218 |
1204 | 2219 return wrap_magic_data (d); |
934 | 2220 } |
2221 | |
2720 | 2222 #ifndef MC_ALLOC |
934 | 2223 DECLARE_FIXED_TYPE_ALLOC (magic_eval_data, Lisp_Magic_Eval_Data); |
2224 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_magic_eval_data 1000 | |
2720 | 2225 #endif /* not MC_ALLOC */ |
934 | 2226 |
2227 Lisp_Object | |
1204 | 2228 make_magic_eval_data (void) |
934 | 2229 { |
2230 Lisp_Magic_Eval_Data *d; | |
2231 | |
2720 | 2232 #ifdef MC_ALLOC |
2233 d = alloc_lrecord_type (Lisp_Magic_Eval_Data, &lrecord_magic_eval_data); | |
2234 #else /* not MC_ALLOC */ | |
934 | 2235 ALLOCATE_FIXED_TYPE (magic_eval_data, Lisp_Magic_Eval_Data, d); |
1204 | 2236 xzero (*d); |
934 | 2237 set_lheader_implementation (&d->lheader, &lrecord_magic_eval_data); |
1204 | 2238 d->object = Qnil; |
2720 | 2239 #endif /* not MC_ALLOC */ |
1204 | 2240 |
2241 return wrap_magic_eval_data (d); | |
934 | 2242 } |
2243 | |
2720 | 2244 #ifndef MC_ALLOC |
934 | 2245 DECLARE_FIXED_TYPE_ALLOC (eval_data, Lisp_Eval_Data); |
2246 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_eval_data 1000 | |
2720 | 2247 #endif /* not MC_ALLOC */ |
934 | 2248 |
2249 Lisp_Object | |
1204 | 2250 make_eval_data (void) |
934 | 2251 { |
2252 Lisp_Eval_Data *d; | |
2253 | |
2720 | 2254 #ifdef MC_ALLOC |
2255 d = alloc_lrecord_type (Lisp_Eval_Data, &lrecord_eval_data); | |
2256 #else /* not MC_ALLOC */ | |
934 | 2257 ALLOCATE_FIXED_TYPE (eval_data, Lisp_Eval_Data, d); |
1204 | 2258 xzero (*d); |
934 | 2259 set_lheader_implementation (&d->lheader, &lrecord_eval_data); |
1204 | 2260 d->function = Qnil; |
2261 d->object = Qnil; | |
2720 | 2262 #endif /* not MC_ALLOC */ |
1204 | 2263 |
2264 return wrap_eval_data (d); | |
934 | 2265 } |
2266 | |
2720 | 2267 #ifndef MC_ALLOC |
934 | 2268 DECLARE_FIXED_TYPE_ALLOC (misc_user_data, Lisp_Misc_User_Data); |
2269 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_misc_user_data 1000 | |
2720 | 2270 #endif /* not MC_ALLOC */ |
934 | 2271 |
2272 Lisp_Object | |
1204 | 2273 make_misc_user_data (void) |
934 | 2274 { |
2275 Lisp_Misc_User_Data *d; | |
2276 | |
2720 | 2277 #ifdef MC_ALLOC |
2278 d = alloc_lrecord_type (Lisp_Misc_User_Data, &lrecord_misc_user_data); | |
2279 #else /* not MC_ALLOC */ | |
934 | 2280 ALLOCATE_FIXED_TYPE (misc_user_data, Lisp_Misc_User_Data, d); |
1204 | 2281 xzero (*d); |
934 | 2282 set_lheader_implementation (&d->lheader, &lrecord_misc_user_data); |
1204 | 2283 d->function = Qnil; |
2284 d->object = Qnil; | |
2720 | 2285 #endif /* not MC_ALLOC */ |
1204 | 2286 |
2287 return wrap_misc_user_data (d); | |
934 | 2288 } |
1204 | 2289 |
2290 #endif /* EVENT_DATA_AS_OBJECTS */ | |
428 | 2291 |
2292 /************************************************************************/ | |
2293 /* Marker allocation */ | |
2294 /************************************************************************/ | |
2295 | |
2720 | 2296 #ifndef MC_ALLOC |
440 | 2297 DECLARE_FIXED_TYPE_ALLOC (marker, Lisp_Marker); |
428 | 2298 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_marker 1000 |
2720 | 2299 #endif /* not MC_ALLOC */ |
428 | 2300 |
2301 DEFUN ("make-marker", Fmake_marker, 0, 0, 0, /* | |
2302 Return a new marker which does not point at any place. | |
2303 */ | |
2304 ()) | |
2305 { | |
440 | 2306 Lisp_Marker *p; |
2307 | |
2720 | 2308 #ifdef MC_ALLOC |
2309 p = alloc_lrecord_type (Lisp_Marker, &lrecord_marker); | |
2310 #else /* not MC_ALLOC */ | |
440 | 2311 ALLOCATE_FIXED_TYPE (marker, Lisp_Marker, p); |
442 | 2312 set_lheader_implementation (&p->lheader, &lrecord_marker); |
2720 | 2313 #endif /* not MC_ALLOC */ |
428 | 2314 p->buffer = 0; |
665 | 2315 p->membpos = 0; |
428 | 2316 marker_next (p) = 0; |
2317 marker_prev (p) = 0; | |
2318 p->insertion_type = 0; | |
793 | 2319 return wrap_marker (p); |
428 | 2320 } |
2321 | |
2322 Lisp_Object | |
2323 noseeum_make_marker (void) | |
2324 { | |
440 | 2325 Lisp_Marker *p; |
2326 | |
2720 | 2327 #ifdef MC_ALLOC |
2328 p = noseeum_alloc_lrecord_type (Lisp_Marker, &lrecord_marker); | |
2329 #else /* not MC_ALLOC */ | |
440 | 2330 NOSEEUM_ALLOCATE_FIXED_TYPE (marker, Lisp_Marker, p); |
442 | 2331 set_lheader_implementation (&p->lheader, &lrecord_marker); |
2720 | 2332 #endif /* not MC_ALLOC */ |
428 | 2333 p->buffer = 0; |
665 | 2334 p->membpos = 0; |
428 | 2335 marker_next (p) = 0; |
2336 marker_prev (p) = 0; | |
2337 p->insertion_type = 0; | |
793 | 2338 return wrap_marker (p); |
428 | 2339 } |
2340 | |
2341 | |
2342 /************************************************************************/ | |
2343 /* String allocation */ | |
2344 /************************************************************************/ | |
2345 | |
2346 /* The data for "short" strings generally resides inside of structs of type | |
2347 string_chars_block. The Lisp_String structure is allocated just like any | |
1204 | 2348 other basic lrecord, and these are freelisted when they get garbage |
2349 collected. The data for short strings get compacted, but the data for | |
2350 large strings do not. | |
428 | 2351 |
2352 Previously Lisp_String structures were relocated, but this caused a lot | |
2353 of bus-errors because the C code didn't include enough GCPRO's for | |
2354 strings (since EVERY REFERENCE to a short string needed to be GCPRO'd so | |
2355 that the reference would get relocated). | |
2356 | |
2357 This new method makes things somewhat bigger, but it is MUCH safer. */ | |
2358 | |
2720 | 2359 #ifndef MC_ALLOC |
438 | 2360 DECLARE_FIXED_TYPE_ALLOC (string, Lisp_String); |
428 | 2361 /* strings are used and freed quite often */ |
2362 /* #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_string 10000 */ | |
2363 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_string 1000 | |
2720 | 2364 #endif /* not MC_ALLOC */ |
428 | 2365 |
2366 static Lisp_Object | |
2367 mark_string (Lisp_Object obj) | |
2368 { | |
793 | 2369 if (CONSP (XSTRING_PLIST (obj)) && EXTENT_INFOP (XCAR (XSTRING_PLIST (obj)))) |
2370 flush_cached_extent_info (XCAR (XSTRING_PLIST (obj))); | |
2371 return XSTRING_PLIST (obj); | |
428 | 2372 } |
2373 | |
2374 static int | |
2286 | 2375 string_equal (Lisp_Object obj1, Lisp_Object obj2, int UNUSED (depth)) |
428 | 2376 { |
2377 Bytecount len; | |
2378 return (((len = XSTRING_LENGTH (obj1)) == XSTRING_LENGTH (obj2)) && | |
2379 !memcmp (XSTRING_DATA (obj1), XSTRING_DATA (obj2), len)); | |
2380 } | |
2381 | |
1204 | 2382 static const struct memory_description string_description[] = { |
793 | 2383 { XD_BYTECOUNT, offsetof (Lisp_String, size_) }, |
2384 { XD_OPAQUE_DATA_PTR, offsetof (Lisp_String, data_), XD_INDIRECT(0, 1) }, | |
440 | 2385 { XD_LISP_OBJECT, offsetof (Lisp_String, plist) }, |
428 | 2386 { XD_END } |
2387 }; | |
2388 | |
442 | 2389 /* We store the string's extent info as the first element of the string's |
2390 property list; and the string's MODIFF as the first or second element | |
2391 of the string's property list (depending on whether the extent info | |
2392 is present), but only if the string has been modified. This is ugly | |
2393 but it reduces the memory allocated for the string in the vast | |
2394 majority of cases, where the string is never modified and has no | |
2395 extent info. | |
2396 | |
2397 #### This means you can't use an int as a key in a string's plist. */ | |
2398 | |
2399 static Lisp_Object * | |
2400 string_plist_ptr (Lisp_Object string) | |
2401 { | |
793 | 2402 Lisp_Object *ptr = &XSTRING_PLIST (string); |
442 | 2403 |
2404 if (CONSP (*ptr) && EXTENT_INFOP (XCAR (*ptr))) | |
2405 ptr = &XCDR (*ptr); | |
2406 if (CONSP (*ptr) && INTP (XCAR (*ptr))) | |
2407 ptr = &XCDR (*ptr); | |
2408 return ptr; | |
2409 } | |
2410 | |
2411 static Lisp_Object | |
2412 string_getprop (Lisp_Object string, Lisp_Object property) | |
2413 { | |
2414 return external_plist_get (string_plist_ptr (string), property, 0, ERROR_ME); | |
2415 } | |
2416 | |
2417 static int | |
2418 string_putprop (Lisp_Object string, Lisp_Object property, Lisp_Object value) | |
2419 { | |
2420 external_plist_put (string_plist_ptr (string), property, value, 0, ERROR_ME); | |
2421 return 1; | |
2422 } | |
2423 | |
2424 static int | |
2425 string_remprop (Lisp_Object string, Lisp_Object property) | |
2426 { | |
2427 return external_remprop (string_plist_ptr (string), property, 0, ERROR_ME); | |
2428 } | |
2429 | |
2430 static Lisp_Object | |
2431 string_plist (Lisp_Object string) | |
2432 { | |
2433 return *string_plist_ptr (string); | |
2434 } | |
2435 | |
2720 | 2436 #ifndef MC_ALLOC |
442 | 2437 /* No `finalize', or `hash' methods. |
2438 internal_hash() already knows how to hash strings and finalization | |
2439 is done with the ADDITIONAL_FREE_string macro, which is the | |
2440 standard way to do finalization when using | |
2441 SWEEP_FIXED_TYPE_BLOCK(). */ | |
2720 | 2442 |
934 | 2443 DEFINE_BASIC_LRECORD_IMPLEMENTATION_WITH_PROPS ("string", string, |
2444 1, /*dumpable-flag*/ | |
2445 mark_string, print_string, | |
2446 0, string_equal, 0, | |
2447 string_description, | |
2448 string_getprop, | |
2449 string_putprop, | |
2450 string_remprop, | |
2451 string_plist, | |
2452 Lisp_String); | |
2720 | 2453 #endif /* not MC_ALLOC */ |
2454 | |
428 | 2455 /* String blocks contain this many useful bytes. */ |
2456 #define STRING_CHARS_BLOCK_SIZE \ | |
814 | 2457 ((Bytecount) (8192 - MALLOC_OVERHEAD - \ |
2458 ((2 * sizeof (struct string_chars_block *)) \ | |
2459 + sizeof (EMACS_INT)))) | |
428 | 2460 /* Block header for small strings. */ |
2461 struct string_chars_block | |
2462 { | |
2463 EMACS_INT pos; | |
2464 struct string_chars_block *next; | |
2465 struct string_chars_block *prev; | |
2466 /* Contents of string_chars_block->string_chars are interleaved | |
2467 string_chars structures (see below) and the actual string data */ | |
2468 unsigned char string_chars[STRING_CHARS_BLOCK_SIZE]; | |
2469 }; | |
2470 | |
2471 static struct string_chars_block *first_string_chars_block; | |
2472 static struct string_chars_block *current_string_chars_block; | |
2473 | |
2474 /* If SIZE is the length of a string, this returns how many bytes | |
2475 * the string occupies in string_chars_block->string_chars | |
2476 * (including alignment padding). | |
2477 */ | |
438 | 2478 #define STRING_FULLSIZE(size) \ |
826 | 2479 ALIGN_FOR_TYPE (((size) + 1 + sizeof (Lisp_String *)), Lisp_String *) |
428 | 2480 |
2481 #define BIG_STRING_FULLSIZE_P(fullsize) ((fullsize) >= STRING_CHARS_BLOCK_SIZE) | |
2482 #define BIG_STRING_SIZE_P(size) (BIG_STRING_FULLSIZE_P (STRING_FULLSIZE(size))) | |
2483 | |
454 | 2484 #define STRING_CHARS_FREE_P(ptr) ((ptr)->string == NULL) |
2485 #define MARK_STRING_CHARS_AS_FREE(ptr) ((void) ((ptr)->string = NULL)) | |
2486 | |
2720 | 2487 #ifdef MC_ALLOC |
2488 static void | |
2489 finalize_string (void *header, int for_disksave) | |
2490 { | |
2491 if (!for_disksave) | |
2492 { | |
2493 Lisp_String *s = (Lisp_String *) header; | |
2494 Bytecount size = s->size_; | |
2775 | 2495 #ifdef MC_ALLOC_TYPE_STATS |
2496 dec_lrecord_string_data_stats (size); | |
2497 #endif /* MC_ALLOC_TYPE_STATS */ | |
2720 | 2498 if (BIG_STRING_SIZE_P (size)) |
2499 xfree (s->data_, Ibyte *); | |
2500 } | |
2501 } | |
2502 | |
2503 DEFINE_LRECORD_IMPLEMENTATION_WITH_PROPS ("string", string, | |
2504 1, /*dumpable-flag*/ | |
2505 mark_string, print_string, | |
2506 finalize_string, | |
2507 string_equal, 0, | |
2508 string_description, | |
2509 string_getprop, | |
2510 string_putprop, | |
2511 string_remprop, | |
2512 string_plist, | |
2513 Lisp_String); | |
2514 | |
2515 #endif /* MC_ALLOC */ | |
2516 | |
428 | 2517 struct string_chars |
2518 { | |
438 | 2519 Lisp_String *string; |
428 | 2520 unsigned char chars[1]; |
2521 }; | |
2522 | |
2523 struct unused_string_chars | |
2524 { | |
438 | 2525 Lisp_String *string; |
428 | 2526 EMACS_INT fullsize; |
2527 }; | |
2528 | |
2529 static void | |
2530 init_string_chars_alloc (void) | |
2531 { | |
2532 first_string_chars_block = xnew (struct string_chars_block); | |
2533 first_string_chars_block->prev = 0; | |
2534 first_string_chars_block->next = 0; | |
2535 first_string_chars_block->pos = 0; | |
2536 current_string_chars_block = first_string_chars_block; | |
2537 } | |
2538 | |
1550 | 2539 static Ibyte * |
2540 allocate_big_string_chars (Bytecount length) | |
2541 { | |
2542 Ibyte *p = xnew_array (Ibyte, length); | |
2543 INCREMENT_CONS_COUNTER (length, "string chars"); | |
2544 return p; | |
2545 } | |
2546 | |
428 | 2547 static struct string_chars * |
793 | 2548 allocate_string_chars_struct (Lisp_Object string_it_goes_with, |
814 | 2549 Bytecount fullsize) |
428 | 2550 { |
2551 struct string_chars *s_chars; | |
2552 | |
438 | 2553 if (fullsize <= |
2554 (countof (current_string_chars_block->string_chars) | |
2555 - current_string_chars_block->pos)) | |
428 | 2556 { |
2557 /* This string can fit in the current string chars block */ | |
2558 s_chars = (struct string_chars *) | |
2559 (current_string_chars_block->string_chars | |
2560 + current_string_chars_block->pos); | |
2561 current_string_chars_block->pos += fullsize; | |
2562 } | |
2563 else | |
2564 { | |
2565 /* Make a new current string chars block */ | |
2566 struct string_chars_block *new_scb = xnew (struct string_chars_block); | |
2567 | |
2568 current_string_chars_block->next = new_scb; | |
2569 new_scb->prev = current_string_chars_block; | |
2570 new_scb->next = 0; | |
2571 current_string_chars_block = new_scb; | |
2572 new_scb->pos = fullsize; | |
2573 s_chars = (struct string_chars *) | |
2574 current_string_chars_block->string_chars; | |
2575 } | |
2576 | |
793 | 2577 s_chars->string = XSTRING (string_it_goes_with); |
428 | 2578 |
2579 INCREMENT_CONS_COUNTER (fullsize, "string chars"); | |
2580 | |
2581 return s_chars; | |
2582 } | |
2583 | |
771 | 2584 #ifdef SLEDGEHAMMER_CHECK_ASCII_BEGIN |
2585 void | |
2586 sledgehammer_check_ascii_begin (Lisp_Object str) | |
2587 { | |
2588 Bytecount i; | |
2589 | |
2590 for (i = 0; i < XSTRING_LENGTH (str); i++) | |
2591 { | |
826 | 2592 if (!byte_ascii_p (string_byte (str, i))) |
771 | 2593 break; |
2594 } | |
2595 | |
2596 assert (i == (Bytecount) XSTRING_ASCII_BEGIN (str) || | |
2597 (i > MAX_STRING_ASCII_BEGIN && | |
2598 (Bytecount) XSTRING_ASCII_BEGIN (str) == | |
2599 (Bytecount) MAX_STRING_ASCII_BEGIN)); | |
2600 } | |
2601 #endif | |
2602 | |
2603 /* You do NOT want to be calling this! (And if you do, you must call | |
851 | 2604 XSET_STRING_ASCII_BEGIN() after modifying the string.) Use ALLOCA () |
771 | 2605 instead and then call make_string() like the rest of the world. */ |
2606 | |
428 | 2607 Lisp_Object |
2608 make_uninit_string (Bytecount length) | |
2609 { | |
438 | 2610 Lisp_String *s; |
814 | 2611 Bytecount fullsize = STRING_FULLSIZE (length); |
428 | 2612 |
438 | 2613 assert (length >= 0 && fullsize > 0); |
428 | 2614 |
2720 | 2615 #ifdef MC_ALLOC |
2616 s = alloc_lrecord_type (Lisp_String, &lrecord_string); | |
2775 | 2617 #ifdef MC_ALLOC_TYPE_STATS |
2618 inc_lrecord_string_data_stats (length); | |
2619 #endif /* MC_ALLOC_TYPE_STATS */ | |
2720 | 2620 #else /* not MC_ALLOC */ |
428 | 2621 /* Allocate the string header */ |
438 | 2622 ALLOCATE_FIXED_TYPE (string, Lisp_String, s); |
793 | 2623 xzero (*s); |
771 | 2624 set_lheader_implementation (&s->u.lheader, &lrecord_string); |
2720 | 2625 #endif /* not MC_ALLOC */ |
2626 | |
826 | 2627 set_lispstringp_data (s, BIG_STRING_FULLSIZE_P (fullsize) |
2720 | 2628 ? allocate_big_string_chars (length + 1) |
2629 : allocate_string_chars_struct (wrap_string (s), | |
2630 fullsize)->chars); | |
438 | 2631 |
826 | 2632 set_lispstringp_length (s, length); |
428 | 2633 s->plist = Qnil; |
793 | 2634 set_string_byte (wrap_string (s), length, 0); |
2635 | |
2636 return wrap_string (s); | |
428 | 2637 } |
2638 | |
2639 #ifdef VERIFY_STRING_CHARS_INTEGRITY | |
2640 static void verify_string_chars_integrity (void); | |
2641 #endif | |
2642 | |
2643 /* Resize the string S so that DELTA bytes can be inserted starting | |
2644 at POS. If DELTA < 0, it means deletion starting at POS. If | |
2645 POS < 0, resize the string but don't copy any characters. Use | |
2646 this if you're planning on completely overwriting the string. | |
2647 */ | |
2648 | |
2649 void | |
793 | 2650 resize_string (Lisp_Object s, Bytecount pos, Bytecount delta) |
428 | 2651 { |
438 | 2652 Bytecount oldfullsize, newfullsize; |
428 | 2653 #ifdef VERIFY_STRING_CHARS_INTEGRITY |
2654 verify_string_chars_integrity (); | |
2655 #endif | |
800 | 2656 #ifdef ERROR_CHECK_TEXT |
428 | 2657 if (pos >= 0) |
2658 { | |
793 | 2659 assert (pos <= XSTRING_LENGTH (s)); |
428 | 2660 if (delta < 0) |
793 | 2661 assert (pos + (-delta) <= XSTRING_LENGTH (s)); |
428 | 2662 } |
2663 else | |
2664 { | |
2665 if (delta < 0) | |
793 | 2666 assert ((-delta) <= XSTRING_LENGTH (s)); |
428 | 2667 } |
800 | 2668 #endif /* ERROR_CHECK_TEXT */ |
428 | 2669 |
2670 if (delta == 0) | |
2671 /* simplest case: no size change. */ | |
2672 return; | |
438 | 2673 |
2674 if (pos >= 0 && delta < 0) | |
2675 /* If DELTA < 0, the functions below will delete the characters | |
2676 before POS. We want to delete characters *after* POS, however, | |
2677 so convert this to the appropriate form. */ | |
2678 pos += -delta; | |
2679 | |
793 | 2680 oldfullsize = STRING_FULLSIZE (XSTRING_LENGTH (s)); |
2681 newfullsize = STRING_FULLSIZE (XSTRING_LENGTH (s) + delta); | |
438 | 2682 |
2683 if (BIG_STRING_FULLSIZE_P (oldfullsize)) | |
428 | 2684 { |
438 | 2685 if (BIG_STRING_FULLSIZE_P (newfullsize)) |
428 | 2686 { |
440 | 2687 /* Both strings are big. We can just realloc(). |
2688 But careful! If the string is shrinking, we have to | |
2689 memmove() _before_ realloc(), and if growing, we have to | |
2690 memmove() _after_ realloc() - otherwise the access is | |
2691 illegal, and we might crash. */ | |
793 | 2692 Bytecount len = XSTRING_LENGTH (s) + 1 - pos; |
440 | 2693 |
2694 if (delta < 0 && pos >= 0) | |
793 | 2695 memmove (XSTRING_DATA (s) + pos + delta, |
2696 XSTRING_DATA (s) + pos, len); | |
2697 XSET_STRING_DATA | |
867 | 2698 (s, (Ibyte *) xrealloc (XSTRING_DATA (s), |
793 | 2699 XSTRING_LENGTH (s) + delta + 1)); |
440 | 2700 if (delta > 0 && pos >= 0) |
793 | 2701 memmove (XSTRING_DATA (s) + pos + delta, XSTRING_DATA (s) + pos, |
2702 len); | |
1550 | 2703 /* Bump the cons counter. |
2704 Conservative; Martin let the increment be delta. */ | |
2705 INCREMENT_CONS_COUNTER (newfullsize, "string chars"); | |
428 | 2706 } |
438 | 2707 else /* String has been demoted from BIG_STRING. */ |
428 | 2708 { |
867 | 2709 Ibyte *new_data = |
438 | 2710 allocate_string_chars_struct (s, newfullsize)->chars; |
867 | 2711 Ibyte *old_data = XSTRING_DATA (s); |
438 | 2712 |
2713 if (pos >= 0) | |
2714 { | |
2715 memcpy (new_data, old_data, pos); | |
2716 memcpy (new_data + pos + delta, old_data + pos, | |
793 | 2717 XSTRING_LENGTH (s) + 1 - pos); |
438 | 2718 } |
793 | 2719 XSET_STRING_DATA (s, new_data); |
1726 | 2720 xfree (old_data, Ibyte *); |
438 | 2721 } |
2722 } | |
2723 else /* old string is small */ | |
2724 { | |
2725 if (oldfullsize == newfullsize) | |
2726 { | |
2727 /* special case; size change but the necessary | |
2728 allocation size won't change (up or down; code | |
2729 somewhere depends on there not being any unused | |
2730 allocation space, modulo any alignment | |
2731 constraints). */ | |
428 | 2732 if (pos >= 0) |
2733 { | |
867 | 2734 Ibyte *addroff = pos + XSTRING_DATA (s); |
428 | 2735 |
2736 memmove (addroff + delta, addroff, | |
2737 /* +1 due to zero-termination. */ | |
793 | 2738 XSTRING_LENGTH (s) + 1 - pos); |
428 | 2739 } |
2740 } | |
2741 else | |
2742 { | |
867 | 2743 Ibyte *old_data = XSTRING_DATA (s); |
2744 Ibyte *new_data = | |
438 | 2745 BIG_STRING_FULLSIZE_P (newfullsize) |
1550 | 2746 ? allocate_big_string_chars (XSTRING_LENGTH (s) + delta + 1) |
438 | 2747 : allocate_string_chars_struct (s, newfullsize)->chars; |
2748 | |
428 | 2749 if (pos >= 0) |
2750 { | |
438 | 2751 memcpy (new_data, old_data, pos); |
2752 memcpy (new_data + pos + delta, old_data + pos, | |
793 | 2753 XSTRING_LENGTH (s) + 1 - pos); |
428 | 2754 } |
793 | 2755 XSET_STRING_DATA (s, new_data); |
438 | 2756 |
2757 { | |
2758 /* We need to mark this chunk of the string_chars_block | |
2759 as unused so that compact_string_chars() doesn't | |
2760 freak. */ | |
2761 struct string_chars *old_s_chars = (struct string_chars *) | |
2762 ((char *) old_data - offsetof (struct string_chars, chars)); | |
2763 /* Sanity check to make sure we aren't hosed by strange | |
2764 alignment/padding. */ | |
793 | 2765 assert (old_s_chars->string == XSTRING (s)); |
454 | 2766 MARK_STRING_CHARS_AS_FREE (old_s_chars); |
438 | 2767 ((struct unused_string_chars *) old_s_chars)->fullsize = |
2768 oldfullsize; | |
2769 } | |
428 | 2770 } |
438 | 2771 } |
2772 | |
793 | 2773 XSET_STRING_LENGTH (s, XSTRING_LENGTH (s) + delta); |
438 | 2774 /* If pos < 0, the string won't be zero-terminated. |
2775 Terminate now just to make sure. */ | |
793 | 2776 XSTRING_DATA (s)[XSTRING_LENGTH (s)] = '\0'; |
438 | 2777 |
2778 if (pos >= 0) | |
793 | 2779 /* We also have to adjust all of the extent indices after the |
2780 place we did the change. We say "pos - 1" because | |
2781 adjust_extents() is exclusive of the starting position | |
2782 passed to it. */ | |
2783 adjust_extents (s, pos - 1, XSTRING_LENGTH (s), delta); | |
428 | 2784 |
2785 #ifdef VERIFY_STRING_CHARS_INTEGRITY | |
2786 verify_string_chars_integrity (); | |
2787 #endif | |
2788 } | |
2789 | |
2790 #ifdef MULE | |
2791 | |
771 | 2792 /* WARNING: If you modify an existing string, you must call |
2793 CHECK_LISP_WRITEABLE() before and bump_string_modiff() afterwards. */ | |
428 | 2794 void |
867 | 2795 set_string_char (Lisp_Object s, Charcount i, Ichar c) |
428 | 2796 { |
867 | 2797 Ibyte newstr[MAX_ICHAR_LEN]; |
771 | 2798 Bytecount bytoff = string_index_char_to_byte (s, i); |
867 | 2799 Bytecount oldlen = itext_ichar_len (XSTRING_DATA (s) + bytoff); |
2800 Bytecount newlen = set_itext_ichar (newstr, c); | |
428 | 2801 |
793 | 2802 sledgehammer_check_ascii_begin (s); |
428 | 2803 if (oldlen != newlen) |
2804 resize_string (s, bytoff, newlen - oldlen); | |
793 | 2805 /* Remember, XSTRING_DATA (s) might have changed so we can't cache it. */ |
2806 memcpy (XSTRING_DATA (s) + bytoff, newstr, newlen); | |
771 | 2807 if (oldlen != newlen) |
2808 { | |
793 | 2809 if (newlen > 1 && i <= (Charcount) XSTRING_ASCII_BEGIN (s)) |
771 | 2810 /* Everything starting with the new char is no longer part of |
2811 ascii_begin */ | |
793 | 2812 XSET_STRING_ASCII_BEGIN (s, i); |
2813 else if (newlen == 1 && i == (Charcount) XSTRING_ASCII_BEGIN (s)) | |
771 | 2814 /* We've extended ascii_begin, and we have to figure out how much by */ |
2815 { | |
2816 Bytecount j; | |
814 | 2817 for (j = (Bytecount) i + 1; j < XSTRING_LENGTH (s); j++) |
771 | 2818 { |
826 | 2819 if (!byte_ascii_p (XSTRING_DATA (s)[j])) |
771 | 2820 break; |
2821 } | |
814 | 2822 XSET_STRING_ASCII_BEGIN (s, min (j, (Bytecount) MAX_STRING_ASCII_BEGIN)); |
771 | 2823 } |
2824 } | |
793 | 2825 sledgehammer_check_ascii_begin (s); |
428 | 2826 } |
2827 | |
2828 #endif /* MULE */ | |
2829 | |
2830 DEFUN ("make-string", Fmake_string, 2, 2, 0, /* | |
444 | 2831 Return a new string consisting of LENGTH copies of CHARACTER. |
2832 LENGTH must be a non-negative integer. | |
428 | 2833 */ |
444 | 2834 (length, character)) |
428 | 2835 { |
2836 CHECK_NATNUM (length); | |
444 | 2837 CHECK_CHAR_COERCE_INT (character); |
428 | 2838 { |
867 | 2839 Ibyte init_str[MAX_ICHAR_LEN]; |
2840 int len = set_itext_ichar (init_str, XCHAR (character)); | |
428 | 2841 Lisp_Object val = make_uninit_string (len * XINT (length)); |
2842 | |
2843 if (len == 1) | |
771 | 2844 { |
2845 /* Optimize the single-byte case */ | |
2846 memset (XSTRING_DATA (val), XCHAR (character), XSTRING_LENGTH (val)); | |
793 | 2847 XSET_STRING_ASCII_BEGIN (val, min (MAX_STRING_ASCII_BEGIN, |
2848 len * XINT (length))); | |
771 | 2849 } |
428 | 2850 else |
2851 { | |
647 | 2852 EMACS_INT i; |
867 | 2853 Ibyte *ptr = XSTRING_DATA (val); |
428 | 2854 |
2720 | 2855 #ifdef MC_ALLOC |
2856 /* Need this for the new allocator: strings are using the uid | |
2857 field for ascii_begin. The uid field is set for debugging, | |
2858 but the string code assumes here that ascii_begin is always | |
2859 zero, when not touched. This assumption is not true with | |
2860 the new allocator, so ascii_begin has to be set to zero | |
2861 here. */ | |
2862 XSET_STRING_ASCII_BEGIN (val, 0); | |
2863 #endif /* not MC_ALLOC */ | |
2864 | |
428 | 2865 for (i = XINT (length); i; i--) |
2866 { | |
867 | 2867 Ibyte *init_ptr = init_str; |
428 | 2868 switch (len) |
2869 { | |
2870 case 4: *ptr++ = *init_ptr++; | |
2871 case 3: *ptr++ = *init_ptr++; | |
2872 case 2: *ptr++ = *init_ptr++; | |
2873 case 1: *ptr++ = *init_ptr++; | |
2874 } | |
2875 } | |
2876 } | |
771 | 2877 sledgehammer_check_ascii_begin (val); |
428 | 2878 return val; |
2879 } | |
2880 } | |
2881 | |
2882 DEFUN ("string", Fstring, 0, MANY, 0, /* | |
2883 Concatenate all the argument characters and make the result a string. | |
2884 */ | |
2885 (int nargs, Lisp_Object *args)) | |
2886 { | |
2367 | 2887 Ibyte *storage = alloca_ibytes (nargs * MAX_ICHAR_LEN); |
867 | 2888 Ibyte *p = storage; |
428 | 2889 |
2890 for (; nargs; nargs--, args++) | |
2891 { | |
2892 Lisp_Object lisp_char = *args; | |
2893 CHECK_CHAR_COERCE_INT (lisp_char); | |
867 | 2894 p += set_itext_ichar (p, XCHAR (lisp_char)); |
428 | 2895 } |
2896 return make_string (storage, p - storage); | |
2897 } | |
2898 | |
771 | 2899 /* Initialize the ascii_begin member of a string to the correct value. */ |
2900 | |
2901 void | |
2902 init_string_ascii_begin (Lisp_Object string) | |
2903 { | |
2904 #ifdef MULE | |
2905 int i; | |
2906 Bytecount length = XSTRING_LENGTH (string); | |
867 | 2907 Ibyte *contents = XSTRING_DATA (string); |
771 | 2908 |
2909 for (i = 0; i < length; i++) | |
2910 { | |
826 | 2911 if (!byte_ascii_p (contents[i])) |
771 | 2912 break; |
2913 } | |
793 | 2914 XSET_STRING_ASCII_BEGIN (string, min (i, MAX_STRING_ASCII_BEGIN)); |
771 | 2915 #else |
793 | 2916 XSET_STRING_ASCII_BEGIN (string, min (XSTRING_LENGTH (string), |
2917 MAX_STRING_ASCII_BEGIN)); | |
771 | 2918 #endif |
2919 sledgehammer_check_ascii_begin (string); | |
2920 } | |
428 | 2921 |
2922 /* Take some raw memory, which MUST already be in internal format, | |
2923 and package it up into a Lisp string. */ | |
2924 Lisp_Object | |
867 | 2925 make_string (const Ibyte *contents, Bytecount length) |
428 | 2926 { |
2927 Lisp_Object val; | |
2928 | |
2929 /* Make sure we find out about bad make_string's when they happen */ | |
800 | 2930 #if defined (ERROR_CHECK_TEXT) && defined (MULE) |
428 | 2931 bytecount_to_charcount (contents, length); /* Just for the assertions */ |
2932 #endif | |
2933 | |
2934 val = make_uninit_string (length); | |
2935 memcpy (XSTRING_DATA (val), contents, length); | |
771 | 2936 init_string_ascii_begin (val); |
2937 sledgehammer_check_ascii_begin (val); | |
428 | 2938 return val; |
2939 } | |
2940 | |
2941 /* Take some raw memory, encoded in some external data format, | |
2942 and convert it into a Lisp string. */ | |
2943 Lisp_Object | |
442 | 2944 make_ext_string (const Extbyte *contents, EMACS_INT length, |
440 | 2945 Lisp_Object coding_system) |
428 | 2946 { |
440 | 2947 Lisp_Object string; |
2948 TO_INTERNAL_FORMAT (DATA, (contents, length), | |
2949 LISP_STRING, string, | |
2950 coding_system); | |
2951 return string; | |
428 | 2952 } |
2953 | |
2954 Lisp_Object | |
867 | 2955 build_intstring (const Ibyte *str) |
771 | 2956 { |
2957 /* Some strlen's crash and burn if passed null. */ | |
814 | 2958 return make_string (str, (str ? qxestrlen (str) : (Bytecount) 0)); |
771 | 2959 } |
2960 | |
2961 Lisp_Object | |
867 | 2962 build_string (const CIbyte *str) |
428 | 2963 { |
2964 /* Some strlen's crash and burn if passed null. */ | |
867 | 2965 return make_string ((const Ibyte *) str, (str ? strlen (str) : 0)); |
428 | 2966 } |
2967 | |
2968 Lisp_Object | |
593 | 2969 build_ext_string (const Extbyte *str, Lisp_Object coding_system) |
428 | 2970 { |
2971 /* Some strlen's crash and burn if passed null. */ | |
2367 | 2972 return make_ext_string ((const Extbyte *) str, |
2973 (str ? dfc_external_data_len (str, coding_system) : | |
2974 0), | |
440 | 2975 coding_system); |
428 | 2976 } |
2977 | |
2978 Lisp_Object | |
867 | 2979 build_msg_intstring (const Ibyte *str) |
428 | 2980 { |
771 | 2981 return build_intstring (GETTEXT (str)); |
2982 } | |
2983 | |
2984 Lisp_Object | |
867 | 2985 build_msg_string (const CIbyte *str) |
771 | 2986 { |
2987 return build_string (CGETTEXT (str)); | |
428 | 2988 } |
2989 | |
2990 Lisp_Object | |
867 | 2991 make_string_nocopy (const Ibyte *contents, Bytecount length) |
428 | 2992 { |
438 | 2993 Lisp_String *s; |
428 | 2994 Lisp_Object val; |
2995 | |
2996 /* Make sure we find out about bad make_string_nocopy's when they happen */ | |
800 | 2997 #if defined (ERROR_CHECK_TEXT) && defined (MULE) |
428 | 2998 bytecount_to_charcount (contents, length); /* Just for the assertions */ |
2999 #endif | |
3000 | |
2720 | 3001 #ifdef MC_ALLOC |
3002 s = alloc_lrecord_type (Lisp_String, &lrecord_string); | |
2775 | 3003 #ifdef MC_ALLOC_TYPE_STATS |
3004 inc_lrecord_string_data_stats (length); | |
3005 #endif /* MC_ALLOC_TYPE_STATS */ | |
2720 | 3006 mcpro (wrap_pointer_1 (s)); /* otherwise nocopy_strings get |
3007 collected and static data is tried to | |
3008 be freed. */ | |
3009 #else /* not MC_ALLOC */ | |
428 | 3010 /* Allocate the string header */ |
438 | 3011 ALLOCATE_FIXED_TYPE (string, Lisp_String, s); |
771 | 3012 set_lheader_implementation (&s->u.lheader, &lrecord_string); |
3013 SET_C_READONLY_RECORD_HEADER (&s->u.lheader); | |
2720 | 3014 #endif /* not MC_ALLOC */ |
428 | 3015 s->plist = Qnil; |
867 | 3016 set_lispstringp_data (s, (Ibyte *) contents); |
826 | 3017 set_lispstringp_length (s, length); |
793 | 3018 val = wrap_string (s); |
771 | 3019 init_string_ascii_begin (val); |
3020 sledgehammer_check_ascii_begin (val); | |
3021 | |
428 | 3022 return val; |
3023 } | |
3024 | |
3025 | |
2720 | 3026 #ifndef MC_ALLOC |
428 | 3027 /************************************************************************/ |
3028 /* lcrecord lists */ | |
3029 /************************************************************************/ | |
3030 | |
3031 /* Lcrecord lists are used to manage the allocation of particular | |
1204 | 3032 sorts of lcrecords, to avoid calling basic_alloc_lcrecord() (and thus |
428 | 3033 malloc() and garbage-collection junk) as much as possible. |
3034 It is similar to the Blocktype class. | |
3035 | |
1204 | 3036 See detailed comment in lcrecord.h. |
3037 */ | |
3038 | |
3039 const struct memory_description free_description[] = { | |
2551 | 3040 { XD_LISP_OBJECT, offsetof (struct free_lcrecord_header, chain), 0, { 0 }, |
1204 | 3041 XD_FLAG_FREE_LISP_OBJECT }, |
3042 { XD_END } | |
3043 }; | |
3044 | |
3045 DEFINE_LRECORD_IMPLEMENTATION ("free", free, | |
3046 0, /*dumpable-flag*/ | |
3047 0, internal_object_printer, | |
3048 0, 0, 0, free_description, | |
3049 struct free_lcrecord_header); | |
3050 | |
3051 const struct memory_description lcrecord_list_description[] = { | |
2551 | 3052 { XD_LISP_OBJECT, offsetof (struct lcrecord_list, free), 0, { 0 }, |
1204 | 3053 XD_FLAG_FREE_LISP_OBJECT }, |
3054 { XD_END } | |
3055 }; | |
428 | 3056 |
3057 static Lisp_Object | |
3058 mark_lcrecord_list (Lisp_Object obj) | |
3059 { | |
3060 struct lcrecord_list *list = XLCRECORD_LIST (obj); | |
3061 Lisp_Object chain = list->free; | |
3062 | |
3063 while (!NILP (chain)) | |
3064 { | |
3065 struct lrecord_header *lheader = XRECORD_LHEADER (chain); | |
3066 struct free_lcrecord_header *free_header = | |
3067 (struct free_lcrecord_header *) lheader; | |
3068 | |
442 | 3069 gc_checking_assert |
3070 (/* There should be no other pointers to the free list. */ | |
3071 ! MARKED_RECORD_HEADER_P (lheader) | |
3072 && | |
3073 /* Only lcrecords should be here. */ | |
1204 | 3074 ! list->implementation->basic_p |
442 | 3075 && |
3076 /* Only free lcrecords should be here. */ | |
3077 free_header->lcheader.free | |
3078 && | |
3079 /* The type of the lcrecord must be right. */ | |
1204 | 3080 lheader->type == lrecord_type_free |
442 | 3081 && |
3082 /* So must the size. */ | |
1204 | 3083 (list->implementation->static_size == 0 || |
3084 list->implementation->static_size == list->size) | |
442 | 3085 ); |
428 | 3086 |
3087 MARK_RECORD_HEADER (lheader); | |
3088 chain = free_header->chain; | |
3089 } | |
3090 | |
3091 return Qnil; | |
3092 } | |
3093 | |
934 | 3094 DEFINE_LRECORD_IMPLEMENTATION ("lcrecord-list", lcrecord_list, |
3095 0, /*dumpable-flag*/ | |
3096 mark_lcrecord_list, internal_object_printer, | |
1204 | 3097 0, 0, 0, lcrecord_list_description, |
3098 struct lcrecord_list); | |
934 | 3099 |
428 | 3100 Lisp_Object |
665 | 3101 make_lcrecord_list (Elemcount size, |
442 | 3102 const struct lrecord_implementation *implementation) |
428 | 3103 { |
1204 | 3104 /* Don't use alloc_lcrecord_type() avoid infinite recursion |
3105 allocating this, */ | |
3106 struct lcrecord_list *p = (struct lcrecord_list *) | |
3107 basic_alloc_lcrecord (sizeof (struct lcrecord_list), | |
3108 &lrecord_lcrecord_list); | |
428 | 3109 |
3110 p->implementation = implementation; | |
3111 p->size = size; | |
3112 p->free = Qnil; | |
793 | 3113 return wrap_lcrecord_list (p); |
428 | 3114 } |
3115 | |
3116 Lisp_Object | |
1204 | 3117 alloc_managed_lcrecord (Lisp_Object lcrecord_list) |
428 | 3118 { |
3119 struct lcrecord_list *list = XLCRECORD_LIST (lcrecord_list); | |
3120 if (!NILP (list->free)) | |
3121 { | |
3122 Lisp_Object val = list->free; | |
3123 struct free_lcrecord_header *free_header = | |
3124 (struct free_lcrecord_header *) XPNTR (val); | |
1204 | 3125 struct lrecord_header *lheader = &free_header->lcheader.lheader; |
428 | 3126 |
3127 #ifdef ERROR_CHECK_GC | |
1204 | 3128 /* Major overkill here. */ |
428 | 3129 /* There should be no other pointers to the free list. */ |
442 | 3130 assert (! MARKED_RECORD_HEADER_P (lheader)); |
428 | 3131 /* Only free lcrecords should be here. */ |
3132 assert (free_header->lcheader.free); | |
1204 | 3133 assert (lheader->type == lrecord_type_free); |
3134 /* Only lcrecords should be here. */ | |
3135 assert (! (list->implementation->basic_p)); | |
3136 #if 0 /* Not used anymore, now that we set the type of the header to | |
3137 lrecord_type_free. */ | |
428 | 3138 /* The type of the lcrecord must be right. */ |
442 | 3139 assert (LHEADER_IMPLEMENTATION (lheader) == list->implementation); |
1204 | 3140 #endif /* 0 */ |
428 | 3141 /* So must the size. */ |
1204 | 3142 assert (list->implementation->static_size == 0 || |
3143 list->implementation->static_size == list->size); | |
428 | 3144 #endif /* ERROR_CHECK_GC */ |
442 | 3145 |
428 | 3146 list->free = free_header->chain; |
3147 free_header->lcheader.free = 0; | |
1204 | 3148 /* Put back the correct type, as we set it to lrecord_type_free. */ |
3149 lheader->type = list->implementation->lrecord_type_index; | |
3150 zero_sized_lcrecord (free_header, list->size); | |
428 | 3151 return val; |
3152 } | |
3153 else | |
1204 | 3154 return wrap_pointer_1 (basic_alloc_lcrecord (list->size, |
3155 list->implementation)); | |
428 | 3156 } |
3157 | |
771 | 3158 /* "Free" a Lisp object LCRECORD by placing it on its associated free list |
1204 | 3159 LCRECORD_LIST; next time alloc_managed_lcrecord() is called with the |
771 | 3160 same LCRECORD_LIST as its parameter, it will return an object from the |
3161 free list, which may be this one. Be VERY VERY SURE there are no | |
3162 pointers to this object hanging around anywhere where they might be | |
3163 used! | |
3164 | |
3165 The first thing this does before making any global state change is to | |
3166 call the finalize method of the object, if it exists. */ | |
3167 | |
428 | 3168 void |
3169 free_managed_lcrecord (Lisp_Object lcrecord_list, Lisp_Object lcrecord) | |
3170 { | |
3171 struct lcrecord_list *list = XLCRECORD_LIST (lcrecord_list); | |
3172 struct free_lcrecord_header *free_header = | |
3173 (struct free_lcrecord_header *) XPNTR (lcrecord); | |
442 | 3174 struct lrecord_header *lheader = &free_header->lcheader.lheader; |
3175 const struct lrecord_implementation *implementation | |
428 | 3176 = LHEADER_IMPLEMENTATION (lheader); |
3177 | |
771 | 3178 /* Finalizer methods may try to free objects within them, which typically |
3179 won't be marked and thus are scheduled for demolition. Putting them | |
3180 on the free list would be very bad, as we'd have xfree()d memory in | |
3181 the list. Even if for some reason the objects are still live | |
3182 (generally a logic error!), we still will have problems putting such | |
3183 an object on the free list right now (e.g. we'd have to avoid calling | |
3184 the finalizer twice, etc.). So basically, those finalizers should not | |
3185 be freeing any objects if during GC. Abort now to catch those | |
3186 problems. */ | |
3187 gc_checking_assert (!gc_in_progress); | |
3188 | |
428 | 3189 /* Make sure the size is correct. This will catch, for example, |
3190 putting a window configuration on the wrong free list. */ | |
1204 | 3191 gc_checking_assert (detagged_lisp_object_size (lheader) == list->size); |
771 | 3192 /* Make sure the object isn't already freed. */ |
3193 gc_checking_assert (!free_header->lcheader.free); | |
2367 | 3194 /* Freeing stuff in dumped memory is bad. If you trip this, you |
3195 may need to check for this before freeing. */ | |
3196 gc_checking_assert (!OBJECT_DUMPED_P (lcrecord)); | |
771 | 3197 |
428 | 3198 if (implementation->finalizer) |
3199 implementation->finalizer (lheader, 0); | |
1204 | 3200 /* Yes, there are two ways to indicate freeness -- the type is |
3201 lrecord_type_free or the ->free flag is set. We used to do only the | |
3202 latter; now we do the former as well for KKCC purposes. Probably | |
3203 safer in any case, as we will lose quicker this way than keeping | |
3204 around an lrecord of apparently correct type but bogus junk in it. */ | |
3205 MARK_LRECORD_AS_FREE (lheader); | |
428 | 3206 free_header->chain = list->free; |
3207 free_header->lcheader.free = 1; | |
3208 list->free = lcrecord; | |
3209 } | |
3210 | |
771 | 3211 static Lisp_Object all_lcrecord_lists[countof (lrecord_implementations_table)]; |
3212 | |
3213 void * | |
3214 alloc_automanaged_lcrecord (Bytecount size, | |
3215 const struct lrecord_implementation *imp) | |
3216 { | |
3217 if (EQ (all_lcrecord_lists[imp->lrecord_type_index], Qzero)) | |
3218 all_lcrecord_lists[imp->lrecord_type_index] = | |
3219 make_lcrecord_list (size, imp); | |
3220 | |
1204 | 3221 return XPNTR (alloc_managed_lcrecord |
771 | 3222 (all_lcrecord_lists[imp->lrecord_type_index])); |
3223 } | |
3224 | |
3225 void | |
3226 free_lcrecord (Lisp_Object rec) | |
3227 { | |
3228 int type = XRECORD_LHEADER (rec)->type; | |
3229 | |
3230 assert (!EQ (all_lcrecord_lists[type], Qzero)); | |
3231 | |
3232 free_managed_lcrecord (all_lcrecord_lists[type], rec); | |
3233 } | |
2720 | 3234 #endif /* not MC_ALLOC */ |
428 | 3235 |
3236 | |
3237 DEFUN ("purecopy", Fpurecopy, 1, 1, 0, /* | |
3238 Kept for compatibility, returns its argument. | |
3239 Old: | |
3240 Make a copy of OBJECT in pure storage. | |
3241 Recursively copies contents of vectors and cons cells. | |
3242 Does not copy symbols. | |
3243 */ | |
444 | 3244 (object)) |
428 | 3245 { |
444 | 3246 return object; |
428 | 3247 } |
3248 | |
3249 | |
3250 /************************************************************************/ | |
3251 /* Garbage Collection */ | |
3252 /************************************************************************/ | |
3253 | |
442 | 3254 /* All the built-in lisp object types are enumerated in `enum lrecord_type'. |
3255 Additional ones may be defined by a module (none yet). We leave some | |
3256 room in `lrecord_implementations_table' for such new lisp object types. */ | |
647 | 3257 const struct lrecord_implementation *lrecord_implementations_table[(int)lrecord_type_last_built_in_type + MODULE_DEFINABLE_TYPE_COUNT]; |
3258 int lrecord_type_count = lrecord_type_last_built_in_type; | |
1676 | 3259 #ifndef USE_KKCC |
442 | 3260 /* Object marker functions are in the lrecord_implementation structure. |
3261 But copying them to a parallel array is much more cache-friendly. | |
3262 This hack speeds up (garbage-collect) by about 5%. */ | |
3263 Lisp_Object (*lrecord_markers[countof (lrecord_implementations_table)]) (Lisp_Object); | |
1676 | 3264 #endif /* not USE_KKCC */ |
428 | 3265 |
3266 struct gcpro *gcprolist; | |
3267 | |
771 | 3268 /* We want the staticpro list relocated, but not the pointers found |
3269 therein, because they refer to locations in the global data segment, not | |
3270 in the heap; we only dump heap objects. Hence we use a trivial | |
3271 description, as for pointerless objects. (Note that the data segment | |
3272 objects, which are global variables like Qfoo or Vbar, themselves are | |
3273 pointers to heap objects. Each needs to be described to pdump as a | |
3274 "root pointer"; this happens in the call to staticpro(). */ | |
1204 | 3275 static const struct memory_description staticpro_description_1[] = { |
452 | 3276 { XD_END } |
3277 }; | |
3278 | |
1204 | 3279 static const struct sized_memory_description staticpro_description = { |
452 | 3280 sizeof (Lisp_Object *), |
3281 staticpro_description_1 | |
3282 }; | |
3283 | |
1204 | 3284 static const struct memory_description staticpros_description_1[] = { |
452 | 3285 XD_DYNARR_DESC (Lisp_Object_ptr_dynarr, &staticpro_description), |
3286 { XD_END } | |
3287 }; | |
3288 | |
1204 | 3289 static const struct sized_memory_description staticpros_description = { |
452 | 3290 sizeof (Lisp_Object_ptr_dynarr), |
3291 staticpros_description_1 | |
3292 }; | |
3293 | |
771 | 3294 #ifdef DEBUG_XEMACS |
3295 | |
1204 | 3296 static const struct memory_description staticpro_one_name_description_1[] = { |
2367 | 3297 { XD_ASCII_STRING, 0 }, |
771 | 3298 { XD_END } |
3299 }; | |
3300 | |
1204 | 3301 static const struct sized_memory_description staticpro_one_name_description = { |
771 | 3302 sizeof (char *), |
3303 staticpro_one_name_description_1 | |
3304 }; | |
3305 | |
1204 | 3306 static const struct memory_description staticpro_names_description_1[] = { |
771 | 3307 XD_DYNARR_DESC (char_ptr_dynarr, &staticpro_one_name_description), |
3308 { XD_END } | |
3309 }; | |
3310 | |
1204 | 3311 |
3312 extern const struct sized_memory_description staticpro_names_description; | |
3313 | |
3314 const struct sized_memory_description staticpro_names_description = { | |
771 | 3315 sizeof (char_ptr_dynarr), |
3316 staticpro_names_description_1 | |
3317 }; | |
3318 | |
3319 /* Help debug crashes gc-marking a staticpro'ed object. */ | |
3320 | |
3321 Lisp_Object_ptr_dynarr *staticpros; | |
3322 char_ptr_dynarr *staticpro_names; | |
3323 | |
3324 /* Mark the Lisp_Object at non-heap VARADDRESS as a root object for | |
3325 garbage collection, and for dumping. */ | |
3326 void | |
3327 staticpro_1 (Lisp_Object *varaddress, char *varname) | |
3328 { | |
3329 Dynarr_add (staticpros, varaddress); | |
3330 Dynarr_add (staticpro_names, varname); | |
1204 | 3331 dump_add_root_lisp_object (varaddress); |
771 | 3332 } |
3333 | |
3334 | |
3335 Lisp_Object_ptr_dynarr *staticpros_nodump; | |
3336 char_ptr_dynarr *staticpro_nodump_names; | |
3337 | |
3338 /* Mark the Lisp_Object at heap VARADDRESS as a root object for | |
3339 garbage collection, but not for dumping. (See below.) */ | |
3340 void | |
3341 staticpro_nodump_1 (Lisp_Object *varaddress, char *varname) | |
3342 { | |
3343 Dynarr_add (staticpros_nodump, varaddress); | |
3344 Dynarr_add (staticpro_nodump_names, varname); | |
3345 } | |
3346 | |
996 | 3347 #ifdef HAVE_SHLIB |
3348 /* Stop treating the Lisp_Object at non-heap VARADDRESS as a root object | |
3349 for garbage collection, but not for dumping. */ | |
3350 void | |
3351 unstaticpro_nodump_1 (Lisp_Object *varaddress, char *varname) | |
3352 { | |
3353 Dynarr_delete_object (staticpros, varaddress); | |
3354 Dynarr_delete_object (staticpro_names, varname); | |
3355 } | |
3356 #endif | |
3357 | |
771 | 3358 #else /* not DEBUG_XEMACS */ |
3359 | |
452 | 3360 Lisp_Object_ptr_dynarr *staticpros; |
3361 | |
3362 /* Mark the Lisp_Object at non-heap VARADDRESS as a root object for | |
3363 garbage collection, and for dumping. */ | |
428 | 3364 void |
3365 staticpro (Lisp_Object *varaddress) | |
3366 { | |
452 | 3367 Dynarr_add (staticpros, varaddress); |
1204 | 3368 dump_add_root_lisp_object (varaddress); |
428 | 3369 } |
3370 | |
442 | 3371 |
452 | 3372 Lisp_Object_ptr_dynarr *staticpros_nodump; |
3373 | |
771 | 3374 /* Mark the Lisp_Object at heap VARADDRESS as a root object for garbage |
3375 collection, but not for dumping. This is used for objects where the | |
3376 only sure pointer is in the heap (rather than in the global data | |
3377 segment, as must be the case for pdump root pointers), but not inside of | |
3378 another Lisp object (where it will be marked as a result of that Lisp | |
3379 object's mark method). The call to staticpro_nodump() must occur *BOTH* | |
3380 at initialization time and at "reinitialization" time (startup, after | |
3381 pdump load.) (For example, this is the case with the predicate symbols | |
3382 for specifier and coding system types. The pointer to this symbol is | |
3383 inside of a methods structure, which is allocated on the heap. The | |
3384 methods structure will be written out to the pdump data file, and may be | |
3385 reloaded at a different address.) | |
3386 | |
3387 #### The necessity for reinitialization is a bug in pdump. Pdump should | |
3388 automatically regenerate the staticpro()s for these symbols when it | |
3389 loads the data in. */ | |
3390 | |
428 | 3391 void |
3392 staticpro_nodump (Lisp_Object *varaddress) | |
3393 { | |
452 | 3394 Dynarr_add (staticpros_nodump, varaddress); |
428 | 3395 } |
3396 | |
996 | 3397 #ifdef HAVE_SHLIB |
3398 /* Unmark the Lisp_Object at non-heap VARADDRESS as a root object for | |
3399 garbage collection, but not for dumping. */ | |
3400 void | |
3401 unstaticpro_nodump (Lisp_Object *varaddress) | |
3402 { | |
3403 Dynarr_delete_object (staticpros, varaddress); | |
3404 } | |
3405 #endif | |
3406 | |
771 | 3407 #endif /* not DEBUG_XEMACS */ |
3408 | |
2720 | 3409 |
3410 | |
3411 | |
3412 | |
3413 #ifdef MC_ALLOC | |
3414 static const struct memory_description mcpro_description_1[] = { | |
3415 { XD_END } | |
3416 }; | |
3417 | |
3418 static const struct sized_memory_description mcpro_description = { | |
3419 sizeof (Lisp_Object *), | |
3420 mcpro_description_1 | |
3421 }; | |
3422 | |
3423 static const struct memory_description mcpros_description_1[] = { | |
3424 XD_DYNARR_DESC (Lisp_Object_dynarr, &mcpro_description), | |
3425 { XD_END } | |
3426 }; | |
3427 | |
3428 static const struct sized_memory_description mcpros_description = { | |
3429 sizeof (Lisp_Object_dynarr), | |
3430 mcpros_description_1 | |
3431 }; | |
3432 | |
3433 #ifdef DEBUG_XEMACS | |
3434 | |
3435 static const struct memory_description mcpro_one_name_description_1[] = { | |
3436 { XD_ASCII_STRING, 0 }, | |
3437 { XD_END } | |
3438 }; | |
3439 | |
3440 static const struct sized_memory_description mcpro_one_name_description = { | |
3441 sizeof (char *), | |
3442 mcpro_one_name_description_1 | |
3443 }; | |
3444 | |
3445 static const struct memory_description mcpro_names_description_1[] = { | |
3446 XD_DYNARR_DESC (char_ptr_dynarr, &mcpro_one_name_description), | |
3447 { XD_END } | |
3448 }; | |
3449 | |
3450 extern const struct sized_memory_description mcpro_names_description; | |
3451 | |
3452 const struct sized_memory_description mcpro_names_description = { | |
3453 sizeof (char_ptr_dynarr), | |
3454 mcpro_names_description_1 | |
3455 }; | |
3456 | |
3457 /* Help debug crashes gc-marking a mcpro'ed object. */ | |
3458 | |
3459 Lisp_Object_dynarr *mcpros; | |
3460 char_ptr_dynarr *mcpro_names; | |
3461 | |
3462 /* Mark the Lisp_Object at non-heap VARADDRESS as a root object for | |
3463 garbage collection, and for dumping. */ | |
3464 void | |
3465 mcpro_1 (Lisp_Object varaddress, char *varname) | |
3466 { | |
3467 Dynarr_add (mcpros, varaddress); | |
3468 Dynarr_add (mcpro_names, varname); | |
3469 } | |
3470 | |
3471 #else /* not DEBUG_XEMACS */ | |
3472 | |
3473 Lisp_Object_dynarr *mcpros; | |
3474 | |
3475 /* Mark the Lisp_Object at non-heap VARADDRESS as a root object for | |
3476 garbage collection, and for dumping. */ | |
3477 void | |
3478 mcpro (Lisp_Object varaddress) | |
3479 { | |
3480 Dynarr_add (mcpros, varaddress); | |
3481 } | |
3482 | |
3483 #endif /* not DEBUG_XEMACS */ | |
3484 #endif /* MC_ALLOC */ | |
3485 | |
442 | 3486 #ifdef ERROR_CHECK_GC |
2720 | 3487 #ifdef MC_ALLOC |
3488 #define GC_CHECK_LHEADER_INVARIANTS(lheader) do { \ | |
3489 struct lrecord_header * GCLI_lh = (lheader); \ | |
3490 assert (GCLI_lh != 0); \ | |
3491 assert (GCLI_lh->type < (unsigned int) lrecord_type_count); \ | |
3492 } while (0) | |
3493 #else /* not MC_ALLOC */ | |
442 | 3494 #define GC_CHECK_LHEADER_INVARIANTS(lheader) do { \ |
3495 struct lrecord_header * GCLI_lh = (lheader); \ | |
3496 assert (GCLI_lh != 0); \ | |
647 | 3497 assert (GCLI_lh->type < (unsigned int) lrecord_type_count); \ |
442 | 3498 assert (! C_READONLY_RECORD_HEADER_P (GCLI_lh) || \ |
3499 (MARKED_RECORD_HEADER_P (GCLI_lh) && \ | |
3500 LISP_READONLY_RECORD_HEADER_P (GCLI_lh))); \ | |
3501 } while (0) | |
2720 | 3502 #endif /* not MC_ALLOC */ |
442 | 3503 #else |
3504 #define GC_CHECK_LHEADER_INVARIANTS(lheader) | |
3505 #endif | |
3506 | |
934 | 3507 |
1204 | 3508 static const struct memory_description lisp_object_description_1[] = { |
3509 { XD_LISP_OBJECT, 0 }, | |
3510 { XD_END } | |
3511 }; | |
3512 | |
3513 const struct sized_memory_description lisp_object_description = { | |
3514 sizeof (Lisp_Object), | |
3515 lisp_object_description_1 | |
3516 }; | |
3517 | |
3518 #if defined (USE_KKCC) || defined (PDUMP) | |
934 | 3519 |
3520 /* This function extracts the value of a count variable described somewhere | |
3521 else in the description. It is converted corresponding to the type */ | |
1204 | 3522 EMACS_INT |
3523 lispdesc_indirect_count_1 (EMACS_INT code, | |
3524 const struct memory_description *idesc, | |
3525 const void *idata) | |
934 | 3526 { |
3527 EMACS_INT count; | |
3528 const void *irdata; | |
3529 | |
3530 int line = XD_INDIRECT_VAL (code); | |
3531 int delta = XD_INDIRECT_DELTA (code); | |
3532 | |
1204 | 3533 irdata = ((char *) idata) + |
3534 lispdesc_indirect_count (idesc[line].offset, idesc, idata); | |
934 | 3535 switch (idesc[line].type) |
3536 { | |
3537 case XD_BYTECOUNT: | |
1204 | 3538 count = * (Bytecount *) irdata; |
934 | 3539 break; |
3540 case XD_ELEMCOUNT: | |
1204 | 3541 count = * (Elemcount *) irdata; |
934 | 3542 break; |
3543 case XD_HASHCODE: | |
1204 | 3544 count = * (Hashcode *) irdata; |
934 | 3545 break; |
3546 case XD_INT: | |
1204 | 3547 count = * (int *) irdata; |
934 | 3548 break; |
3549 case XD_LONG: | |
1204 | 3550 count = * (long *) irdata; |
934 | 3551 break; |
3552 default: | |
3553 stderr_out ("Unsupported count type : %d (line = %d, code = %ld)\n", | |
1204 | 3554 idesc[line].type, line, (long) code); |
2666 | 3555 #if defined(USE_KKCC) && defined(DEBUG_XEMACS) |
2645 | 3556 if (gc_in_progress) |
3557 kkcc_backtrace (); | |
3558 #endif | |
1204 | 3559 #ifdef PDUMP |
3560 if (in_pdump) | |
3561 pdump_backtrace (); | |
3562 #endif | |
934 | 3563 count = 0; /* warning suppression */ |
2500 | 3564 ABORT (); |
934 | 3565 } |
3566 count += delta; | |
3567 return count; | |
3568 } | |
3569 | |
1204 | 3570 /* SDESC is a "description map" (basically, a list of offsets used for |
3571 successive indirections) and OBJ is the first object to indirect off of. | |
3572 Return the description ultimately found. */ | |
3573 | |
3574 const struct sized_memory_description * | |
3575 lispdesc_indirect_description_1 (const void *obj, | |
3576 const struct sized_memory_description *sdesc) | |
934 | 3577 { |
3578 int pos; | |
3579 | |
1204 | 3580 for (pos = 0; sdesc[pos].size >= 0; pos++) |
3581 obj = * (const void **) ((const char *) obj + sdesc[pos].size); | |
3582 | |
3583 return (const struct sized_memory_description *) obj; | |
3584 } | |
3585 | |
3586 /* Compute the size of the data at RDATA, described by a single entry | |
3587 DESC1 in a description array. OBJ and DESC are used for | |
3588 XD_INDIRECT references. */ | |
3589 | |
3590 static Bytecount | |
3591 lispdesc_one_description_line_size (void *rdata, | |
3592 const struct memory_description *desc1, | |
3593 const void *obj, | |
3594 const struct memory_description *desc) | |
3595 { | |
3596 union_switcheroo: | |
3597 switch (desc1->type) | |
934 | 3598 { |
1204 | 3599 case XD_LISP_OBJECT_ARRAY: |
3600 { | |
3601 EMACS_INT val = lispdesc_indirect_count (desc1->data1, desc, obj); | |
3602 return (val * sizeof (Lisp_Object)); | |
3603 } | |
3604 case XD_LISP_OBJECT: | |
3605 case XD_LO_LINK: | |
3606 return sizeof (Lisp_Object); | |
3607 case XD_OPAQUE_PTR: | |
3608 return sizeof (void *); | |
2367 | 3609 case XD_BLOCK_PTR: |
1204 | 3610 { |
3611 EMACS_INT val = lispdesc_indirect_count (desc1->data1, desc, obj); | |
3612 return val * sizeof (void *); | |
3613 } | |
2367 | 3614 case XD_BLOCK_ARRAY: |
1204 | 3615 { |
3616 EMACS_INT val = lispdesc_indirect_count (desc1->data1, desc, obj); | |
3617 | |
3618 return (val * | |
2367 | 3619 lispdesc_block_size |
2551 | 3620 (rdata, |
3621 lispdesc_indirect_description (obj, desc1->data2.descr))); | |
1204 | 3622 } |
3623 case XD_OPAQUE_DATA_PTR: | |
3624 return sizeof (void *); | |
3625 case XD_UNION_DYNAMIC_SIZE: | |
3626 { | |
3627 /* If an explicit size was given in the first-level structure | |
3628 description, use it; else compute size based on current union | |
3629 constant. */ | |
3630 const struct sized_memory_description *sdesc = | |
2551 | 3631 lispdesc_indirect_description (obj, desc1->data2.descr); |
1204 | 3632 if (sdesc->size) |
3633 return sdesc->size; | |
3634 else | |
3635 { | |
3636 desc1 = lispdesc_process_xd_union (desc1, desc, obj); | |
3637 if (desc1) | |
3638 goto union_switcheroo; | |
934 | 3639 break; |
1204 | 3640 } |
3641 } | |
3642 case XD_UNION: | |
3643 { | |
3644 /* If an explicit size was given in the first-level structure | |
3645 description, use it; else compute size based on maximum of all | |
3646 possible structures. */ | |
3647 const struct sized_memory_description *sdesc = | |
2551 | 3648 lispdesc_indirect_description (obj, desc1->data2.descr); |
1204 | 3649 if (sdesc->size) |
3650 return sdesc->size; | |
3651 else | |
3652 { | |
3653 int count; | |
3654 Bytecount max_size = -1, size; | |
3655 | |
3656 desc1 = sdesc->description; | |
3657 | |
3658 for (count = 0; desc1[count].type != XD_END; count++) | |
3659 { | |
3660 size = lispdesc_one_description_line_size (rdata, | |
3661 &desc1[count], | |
3662 obj, desc); | |
3663 if (size > max_size) | |
3664 max_size = size; | |
3665 } | |
3666 return max_size; | |
3667 } | |
934 | 3668 } |
2367 | 3669 case XD_ASCII_STRING: |
1204 | 3670 return sizeof (void *); |
3671 case XD_DOC_STRING: | |
3672 return sizeof (void *); | |
3673 case XD_INT_RESET: | |
3674 return sizeof (int); | |
3675 case XD_BYTECOUNT: | |
3676 return sizeof (Bytecount); | |
3677 case XD_ELEMCOUNT: | |
3678 return sizeof (Elemcount); | |
3679 case XD_HASHCODE: | |
3680 return sizeof (Hashcode); | |
3681 case XD_INT: | |
3682 return sizeof (int); | |
3683 case XD_LONG: | |
3684 return sizeof (long); | |
3685 default: | |
3686 stderr_out ("Unsupported dump type : %d\n", desc1->type); | |
2500 | 3687 ABORT (); |
934 | 3688 } |
3689 | |
1204 | 3690 return 0; |
934 | 3691 } |
3692 | |
3693 | |
1204 | 3694 /* Return the size of the memory block (NOT necessarily a structure!) |
3695 described by SDESC and pointed to by OBJ. If SDESC records an | |
3696 explicit size (i.e. non-zero), it is simply returned; otherwise, | |
3697 the size is calculated by the maximum offset and the size of the | |
3698 object at that offset, rounded up to the maximum alignment. In | |
3699 this case, we may need the object, for example when retrieving an | |
3700 "indirect count" of an inlined array (the count is not constant, | |
3701 but is specified by one of the elements of the memory block). (It | |
3702 is generally not a problem if we return an overly large size -- we | |
3703 will simply end up reserving more space than necessary; but if the | |
3704 size is too small we could be in serious trouble, in particular | |
3705 with nested inlined structures, where there may be alignment | |
3706 padding in the middle of a block. #### In fact there is an (at | |
3707 least theoretical) problem with an overly large size -- we may | |
3708 trigger a protection fault when reading from invalid memory. We | |
3709 need to handle this -- perhaps in a stupid but dependable way, | |
3710 i.e. by trapping SIGSEGV and SIGBUS.) */ | |
3711 | |
3712 Bytecount | |
2367 | 3713 lispdesc_block_size_1 (const void *obj, Bytecount size, |
3714 const struct memory_description *desc) | |
934 | 3715 { |
1204 | 3716 EMACS_INT max_offset = -1; |
934 | 3717 int max_offset_pos = -1; |
3718 int pos; | |
2367 | 3719 |
3720 if (size) | |
3721 return size; | |
934 | 3722 |
3723 for (pos = 0; desc[pos].type != XD_END; pos++) | |
3724 { | |
1204 | 3725 EMACS_INT offset = lispdesc_indirect_count (desc[pos].offset, desc, obj); |
3726 if (offset == max_offset) | |
934 | 3727 { |
3728 stderr_out ("Two relocatable elements at same offset?\n"); | |
2500 | 3729 ABORT (); |
934 | 3730 } |
1204 | 3731 else if (offset > max_offset) |
934 | 3732 { |
1204 | 3733 max_offset = offset; |
934 | 3734 max_offset_pos = pos; |
3735 } | |
3736 } | |
3737 | |
3738 if (max_offset_pos < 0) | |
3739 return 0; | |
3740 | |
1204 | 3741 { |
3742 Bytecount size_at_max; | |
3743 size_at_max = | |
3744 lispdesc_one_description_line_size ((char *) obj + max_offset, | |
3745 &desc[max_offset_pos], obj, desc); | |
3746 | |
3747 /* We have no way of knowing the required alignment for this structure, | |
3748 so just make it maximally aligned. */ | |
3749 return MAX_ALIGN_SIZE (max_offset + size_at_max); | |
3750 } | |
3751 } | |
3752 | |
3753 #endif /* defined (USE_KKCC) || defined (PDUMP) */ | |
3754 | |
2720 | 3755 #ifdef MC_ALLOC |
3756 #define GC_CHECK_NOT_FREE(lheader) \ | |
3757 gc_checking_assert (! LRECORD_FREE_P (lheader)); | |
3758 #else /* MC_ALLOC */ | |
1276 | 3759 #define GC_CHECK_NOT_FREE(lheader) \ |
2720 | 3760 gc_checking_assert (! LRECORD_FREE_P (lheader)); \ |
1276 | 3761 gc_checking_assert (LHEADER_IMPLEMENTATION (lheader)->basic_p || \ |
3762 ! ((struct lcrecord_header *) lheader)->free) | |
2720 | 3763 #endif /* MC_ALLOC */ |
1276 | 3764 |
1204 | 3765 #ifdef USE_KKCC |
3766 /* The following functions implement the new mark algorithm. | |
3767 They mark objects according to their descriptions. They | |
3768 are modeled on the corresponding pdumper procedures. */ | |
3769 | |
2666 | 3770 #ifdef DEBUG_XEMACS |
3771 /* The backtrace for the KKCC mark functions. */ | |
3772 #define KKCC_INIT_BT_STACK_SIZE 4096 | |
1676 | 3773 |
3774 typedef struct | |
3775 { | |
2645 | 3776 void *obj; |
3777 const struct memory_description *desc; | |
3778 int pos; | |
2666 | 3779 } kkcc_bt_stack_entry; |
3780 | |
3781 static kkcc_bt_stack_entry *kkcc_bt; | |
3782 static int kkcc_bt_stack_size; | |
2645 | 3783 static int kkcc_bt_depth = 0; |
3784 | |
2666 | 3785 static void |
3786 kkcc_bt_init (void) | |
3787 { | |
3788 kkcc_bt_depth = 0; | |
3789 kkcc_bt_stack_size = KKCC_INIT_BT_STACK_SIZE; | |
3790 kkcc_bt = (kkcc_bt_stack_entry *) | |
3791 malloc (kkcc_bt_stack_size * sizeof (kkcc_bt_stack_entry)); | |
3792 if (!kkcc_bt) | |
3793 { | |
3794 stderr_out ("KKCC backtrace stack init failed for size %d\n", | |
3795 kkcc_bt_stack_size); | |
3796 ABORT (); | |
3797 } | |
3798 } | |
2645 | 3799 |
3800 void | |
3801 kkcc_backtrace (void) | |
3802 { | |
3803 int i; | |
3804 stderr_out ("KKCC mark stack backtrace :\n"); | |
3805 for (i = kkcc_bt_depth - 1; i >= 0; i--) | |
3806 { | |
2650 | 3807 Lisp_Object obj = wrap_pointer_1 (kkcc_bt[i].obj); |
2645 | 3808 stderr_out (" [%d]", i); |
2720 | 3809 #ifdef MC_ALLOC |
3810 if ((XRECORD_LHEADER (obj)->type >= lrecord_type_last_built_in_type) | |
3811 #else /* not MC_ALLOC */ | |
2650 | 3812 if ((XRECORD_LHEADER (obj)->type >= lrecord_type_free) |
2720 | 3813 #endif /* not MC_ALLOC */ |
2650 | 3814 || (!LRECORDP (obj)) |
3815 || (!XRECORD_LHEADER_IMPLEMENTATION (obj))) | |
2645 | 3816 { |
3817 stderr_out (" non Lisp Object"); | |
3818 } | |
3819 else | |
3820 { | |
3821 stderr_out (" %s", | |
2650 | 3822 XRECORD_LHEADER_IMPLEMENTATION (obj)->name); |
2645 | 3823 } |
3824 stderr_out (" (addr: 0x%x, desc: 0x%x, ", | |
3825 (int) kkcc_bt[i].obj, | |
3826 (int) kkcc_bt[i].desc); | |
3827 if (kkcc_bt[i].pos >= 0) | |
3828 stderr_out ("pos: %d)\n", kkcc_bt[i].pos); | |
3829 else | |
3830 stderr_out ("root set)\n"); | |
3831 } | |
3832 } | |
3833 | |
3834 static void | |
2666 | 3835 kkcc_bt_stack_realloc (void) |
3836 { | |
3837 kkcc_bt_stack_size *= 2; | |
3838 kkcc_bt = (kkcc_bt_stack_entry *) | |
3839 realloc (kkcc_bt, kkcc_bt_stack_size * sizeof (kkcc_bt_stack_entry)); | |
3840 if (!kkcc_bt) | |
3841 { | |
3842 stderr_out ("KKCC backtrace stack realloc failed for size %d\n", | |
3843 kkcc_bt_stack_size); | |
3844 ABORT (); | |
3845 } | |
3846 } | |
3847 | |
3848 static void | |
3849 kkcc_bt_free (void) | |
3850 { | |
3851 free (kkcc_bt); | |
3852 kkcc_bt = 0; | |
3853 kkcc_bt_stack_size = 0; | |
3854 } | |
3855 | |
3856 static void | |
2645 | 3857 kkcc_bt_push (void *obj, const struct memory_description *desc, |
3858 int level, int pos) | |
3859 { | |
3860 kkcc_bt_depth = level; | |
3861 kkcc_bt[kkcc_bt_depth].obj = obj; | |
3862 kkcc_bt[kkcc_bt_depth].desc = desc; | |
3863 kkcc_bt[kkcc_bt_depth].pos = pos; | |
3864 kkcc_bt_depth++; | |
2666 | 3865 if (kkcc_bt_depth >= kkcc_bt_stack_size) |
3866 kkcc_bt_stack_realloc (); | |
2645 | 3867 } |
3868 | |
3869 #else /* not DEBUG_XEMACS */ | |
2666 | 3870 #define kkcc_bt_init() |
2645 | 3871 #define kkcc_bt_push(obj, desc, level, pos) |
3872 #endif /* not DEBUG_XEMACS */ | |
3873 | |
2666 | 3874 /* Object memory descriptions are in the lrecord_implementation structure. |
3875 But copying them to a parallel array is much more cache-friendly. */ | |
3876 const struct memory_description *lrecord_memory_descriptions[countof (lrecord_implementations_table)]; | |
3877 | |
3878 /* the initial stack size in kkcc_gc_stack_entries */ | |
3879 #define KKCC_INIT_GC_STACK_SIZE 16384 | |
3880 | |
3881 typedef struct | |
3882 { | |
3883 void *data; | |
3884 const struct memory_description *desc; | |
3885 #ifdef DEBUG_XEMACS | |
3886 int level; | |
3887 int pos; | |
3888 #endif | |
3889 } kkcc_gc_stack_entry; | |
3890 | |
3891 static kkcc_gc_stack_entry *kkcc_gc_stack_ptr; | |
3892 static kkcc_gc_stack_entry *kkcc_gc_stack_top; | |
3893 static kkcc_gc_stack_entry *kkcc_gc_stack_last_entry; | |
3894 static int kkcc_gc_stack_size; | |
3895 | |
1676 | 3896 static void |
3897 kkcc_gc_stack_init (void) | |
3898 { | |
3899 kkcc_gc_stack_size = KKCC_INIT_GC_STACK_SIZE; | |
3900 kkcc_gc_stack_ptr = (kkcc_gc_stack_entry *) | |
3901 malloc (kkcc_gc_stack_size * sizeof (kkcc_gc_stack_entry)); | |
3902 if (!kkcc_gc_stack_ptr) | |
3903 { | |
3904 stderr_out ("stack init failed for size %d\n", kkcc_gc_stack_size); | |
2666 | 3905 ABORT (); |
1676 | 3906 } |
3907 kkcc_gc_stack_top = kkcc_gc_stack_ptr - 1; | |
3908 kkcc_gc_stack_last_entry = kkcc_gc_stack_ptr + kkcc_gc_stack_size - 1; | |
3909 } | |
3910 | |
3911 static void | |
3912 kkcc_gc_stack_free (void) | |
3913 { | |
3914 free (kkcc_gc_stack_ptr); | |
3915 kkcc_gc_stack_ptr = 0; | |
3916 kkcc_gc_stack_top = 0; | |
3917 kkcc_gc_stack_size = 0; | |
3918 } | |
3919 | |
3920 static void | |
3921 kkcc_gc_stack_realloc (void) | |
3922 { | |
3923 int current_offset = (int)(kkcc_gc_stack_top - kkcc_gc_stack_ptr); | |
3924 kkcc_gc_stack_size *= 2; | |
3925 kkcc_gc_stack_ptr = (kkcc_gc_stack_entry *) | |
3926 realloc (kkcc_gc_stack_ptr, | |
3927 kkcc_gc_stack_size * sizeof (kkcc_gc_stack_entry)); | |
3928 if (!kkcc_gc_stack_ptr) | |
3929 { | |
3930 stderr_out ("stack realloc failed for size %d\n", kkcc_gc_stack_size); | |
2666 | 3931 ABORT (); |
1676 | 3932 } |
3933 kkcc_gc_stack_top = kkcc_gc_stack_ptr + current_offset; | |
3934 kkcc_gc_stack_last_entry = kkcc_gc_stack_ptr + kkcc_gc_stack_size - 1; | |
3935 } | |
3936 | |
3937 #define KKCC_GC_STACK_FULL (kkcc_gc_stack_top >= kkcc_gc_stack_last_entry) | |
3938 #define KKCC_GC_STACK_EMPTY (kkcc_gc_stack_top < kkcc_gc_stack_ptr) | |
3939 | |
3940 static void | |
2645 | 3941 #ifdef DEBUG_XEMACS |
3942 kkcc_gc_stack_push_1 (void *data, const struct memory_description *desc, | |
3943 int level, int pos) | |
3944 #else | |
3945 kkcc_gc_stack_push_1 (void *data, const struct memory_description *desc) | |
3946 #endif | |
1676 | 3947 { |
3948 if (KKCC_GC_STACK_FULL) | |
3949 kkcc_gc_stack_realloc(); | |
3950 kkcc_gc_stack_top++; | |
3951 kkcc_gc_stack_top->data = data; | |
3952 kkcc_gc_stack_top->desc = desc; | |
2645 | 3953 #ifdef DEBUG_XEMACS |
3954 kkcc_gc_stack_top->level = level; | |
3955 kkcc_gc_stack_top->pos = pos; | |
3956 #endif | |
3957 } | |
3958 | |
3959 #ifdef DEBUG_XEMACS | |
3960 #define kkcc_gc_stack_push(data, desc, level, pos) \ | |
3961 kkcc_gc_stack_push_1 (data, desc, level, pos) | |
3962 #else | |
3963 #define kkcc_gc_stack_push(data, desc, level, pos) \ | |
3964 kkcc_gc_stack_push_1 (data, desc) | |
3965 #endif | |
1676 | 3966 |
3967 static kkcc_gc_stack_entry * | |
3968 kkcc_gc_stack_pop (void) | |
3969 { | |
3970 if (KKCC_GC_STACK_EMPTY) | |
3971 return 0; | |
3972 kkcc_gc_stack_top--; | |
3973 return kkcc_gc_stack_top + 1; | |
3974 } | |
3975 | |
3976 void | |
2645 | 3977 #ifdef DEBUG_XEMACS |
3978 kkcc_gc_stack_push_lisp_object_1 (Lisp_Object obj, int level, int pos) | |
3979 #else | |
3980 kkcc_gc_stack_push_lisp_object_1 (Lisp_Object obj) | |
3981 #endif | |
1676 | 3982 { |
3983 if (XTYPE (obj) == Lisp_Type_Record) | |
3984 { | |
3985 struct lrecord_header *lheader = XRECORD_LHEADER (obj); | |
3986 const struct memory_description *desc; | |
3987 GC_CHECK_LHEADER_INVARIANTS (lheader); | |
3988 desc = RECORD_DESCRIPTION (lheader); | |
3989 if (! MARKED_RECORD_HEADER_P (lheader)) | |
3990 { | |
3991 MARK_RECORD_HEADER (lheader); | |
2666 | 3992 kkcc_gc_stack_push ((void*) lheader, desc, level, pos); |
1676 | 3993 } |
3994 } | |
3995 } | |
3996 | |
2645 | 3997 #ifdef DEBUG_XEMACS |
3998 #define kkcc_gc_stack_push_lisp_object(obj, level, pos) \ | |
3999 kkcc_gc_stack_push_lisp_object_1 (obj, level, pos) | |
4000 #else | |
4001 #define kkcc_gc_stack_push_lisp_object(obj, level, pos) \ | |
4002 kkcc_gc_stack_push_lisp_object_1 (obj) | |
4003 #endif | |
4004 | |
1265 | 4005 #ifdef ERROR_CHECK_GC |
4006 #define KKCC_DO_CHECK_FREE(obj, allow_free) \ | |
4007 do \ | |
4008 { \ | |
4009 if (!allow_free && XTYPE (obj) == Lisp_Type_Record) \ | |
4010 { \ | |
4011 struct lrecord_header *lheader = XRECORD_LHEADER (obj); \ | |
4012 GC_CHECK_NOT_FREE (lheader); \ | |
4013 } \ | |
4014 } while (0) | |
4015 #else | |
4016 #define KKCC_DO_CHECK_FREE(obj, allow_free) | |
4017 #endif | |
1204 | 4018 |
4019 #ifdef ERROR_CHECK_GC | |
2645 | 4020 #ifdef DEBUG_XEMACS |
1598 | 4021 static void |
2645 | 4022 mark_object_maybe_checking_free_1 (Lisp_Object obj, int allow_free, |
4023 int level, int pos) | |
4024 #else | |
4025 static void | |
4026 mark_object_maybe_checking_free_1 (Lisp_Object obj, int allow_free) | |
4027 #endif | |
1204 | 4028 { |
1265 | 4029 KKCC_DO_CHECK_FREE (obj, allow_free); |
2645 | 4030 kkcc_gc_stack_push_lisp_object (obj, level, pos); |
4031 } | |
4032 | |
4033 #ifdef DEBUG_XEMACS | |
4034 #define mark_object_maybe_checking_free(obj, allow_free, level, pos) \ | |
4035 mark_object_maybe_checking_free_1 (obj, allow_free, level, pos) | |
1204 | 4036 #else |
2645 | 4037 #define mark_object_maybe_checking_free(obj, allow_free, level, pos) \ |
4038 mark_object_maybe_checking_free_1 (obj, allow_free) | |
4039 #endif | |
4040 #else /* not ERROR_CHECK_GC */ | |
4041 #define mark_object_maybe_checking_free(obj, allow_free, level, pos) \ | |
4042 kkcc_gc_stack_push_lisp_object (obj, level, pos) | |
4043 #endif /* not ERROR_CHECK_GC */ | |
1204 | 4044 |
934 | 4045 |
4046 /* This function loops all elements of a struct pointer and calls | |
4047 mark_with_description with each element. */ | |
4048 static void | |
2645 | 4049 #ifdef DEBUG_XEMACS |
4050 mark_struct_contents_1 (const void *data, | |
4051 const struct sized_memory_description *sdesc, | |
4052 int count, int level, int pos) | |
4053 #else | |
4054 mark_struct_contents_1 (const void *data, | |
1204 | 4055 const struct sized_memory_description *sdesc, |
4056 int count) | |
2645 | 4057 #endif |
934 | 4058 { |
4059 int i; | |
4060 Bytecount elsize; | |
2367 | 4061 elsize = lispdesc_block_size (data, sdesc); |
934 | 4062 |
4063 for (i = 0; i < count; i++) | |
4064 { | |
2645 | 4065 kkcc_gc_stack_push (((char *) data) + elsize * i, sdesc->description, |
4066 level, pos); | |
934 | 4067 } |
4068 } | |
4069 | |
2645 | 4070 #ifdef DEBUG_XEMACS |
4071 #define mark_struct_contents(data, sdesc, count, level, pos) \ | |
4072 mark_struct_contents_1 (data, sdesc, count, level, pos) | |
4073 #else | |
4074 #define mark_struct_contents(data, sdesc, count, level, pos) \ | |
4075 mark_struct_contents_1 (data, sdesc, count) | |
4076 #endif | |
1598 | 4077 |
4078 /* This function implements the KKCC mark algorithm. | |
4079 Instead of calling mark_object, all the alive Lisp_Objects are pushed | |
4080 on the kkcc_gc_stack. This function processes all elements on the stack | |
4081 according to their descriptions. */ | |
4082 static void | |
4083 kkcc_marking (void) | |
4084 { | |
4085 kkcc_gc_stack_entry *stack_entry = 0; | |
4086 void *data = 0; | |
4087 const struct memory_description *desc = 0; | |
4088 int pos; | |
2645 | 4089 #ifdef DEBUG_XEMACS |
4090 int level = 0; | |
2666 | 4091 kkcc_bt_init (); |
2645 | 4092 #endif |
1598 | 4093 |
4094 while ((stack_entry = kkcc_gc_stack_pop ()) != 0) | |
4095 { | |
4096 data = stack_entry->data; | |
4097 desc = stack_entry->desc; | |
2645 | 4098 #ifdef DEBUG_XEMACS |
4099 level = stack_entry->level + 1; | |
4100 #endif | |
4101 | |
4102 kkcc_bt_push (data, desc, stack_entry->level, stack_entry->pos); | |
1598 | 4103 |
2720 | 4104 gc_checking_assert (data); |
4105 gc_checking_assert (desc); | |
4106 | |
1598 | 4107 for (pos = 0; desc[pos].type != XD_END; pos++) |
4108 { | |
4109 const struct memory_description *desc1 = &desc[pos]; | |
4110 const void *rdata = | |
4111 (const char *) data + lispdesc_indirect_count (desc1->offset, | |
4112 desc, data); | |
4113 union_switcheroo: | |
4114 | |
4115 /* If the flag says don't mark, then don't mark. */ | |
4116 if ((desc1->flags) & XD_FLAG_NO_KKCC) | |
4117 continue; | |
4118 | |
4119 switch (desc1->type) | |
4120 { | |
4121 case XD_BYTECOUNT: | |
4122 case XD_ELEMCOUNT: | |
4123 case XD_HASHCODE: | |
4124 case XD_INT: | |
4125 case XD_LONG: | |
4126 case XD_INT_RESET: | |
4127 case XD_LO_LINK: | |
4128 case XD_OPAQUE_PTR: | |
4129 case XD_OPAQUE_DATA_PTR: | |
2367 | 4130 case XD_ASCII_STRING: |
1598 | 4131 case XD_DOC_STRING: |
4132 break; | |
4133 case XD_LISP_OBJECT: | |
4134 { | |
4135 const Lisp_Object *stored_obj = (const Lisp_Object *) rdata; | |
4136 | |
4137 /* Because of the way that tagged objects work (pointers and | |
4138 Lisp_Objects have the same representation), XD_LISP_OBJECT | |
4139 can be used for untagged pointers. They might be NULL, | |
4140 though. */ | |
4141 if (EQ (*stored_obj, Qnull_pointer)) | |
4142 break; | |
2720 | 4143 #ifdef MC_ALLOC |
4144 mark_object_maybe_checking_free (*stored_obj, 0, level, pos); | |
4145 #else /* not MC_ALLOC */ | |
1598 | 4146 mark_object_maybe_checking_free |
2645 | 4147 (*stored_obj, (desc1->flags) & XD_FLAG_FREE_LISP_OBJECT, |
4148 level, pos); | |
2775 | 4149 #endif /* not MC_ALLOC */ |
1598 | 4150 break; |
4151 } | |
4152 case XD_LISP_OBJECT_ARRAY: | |
4153 { | |
4154 int i; | |
4155 EMACS_INT count = | |
4156 lispdesc_indirect_count (desc1->data1, desc, data); | |
4157 | |
4158 for (i = 0; i < count; i++) | |
4159 { | |
4160 const Lisp_Object *stored_obj = | |
4161 (const Lisp_Object *) rdata + i; | |
4162 | |
4163 if (EQ (*stored_obj, Qnull_pointer)) | |
4164 break; | |
2720 | 4165 #ifdef MC_ALLOC |
4166 mark_object_maybe_checking_free (*stored_obj, 0, level, pos); | |
4167 #else /* not MC_ALLOC */ | |
1598 | 4168 mark_object_maybe_checking_free |
2645 | 4169 (*stored_obj, (desc1->flags) & XD_FLAG_FREE_LISP_OBJECT, |
4170 level, pos); | |
2720 | 4171 #endif /* not MC_ALLOC */ |
1598 | 4172 } |
4173 break; | |
4174 } | |
2367 | 4175 case XD_BLOCK_PTR: |
1598 | 4176 { |
4177 EMACS_INT count = lispdesc_indirect_count (desc1->data1, desc, | |
4178 data); | |
4179 const struct sized_memory_description *sdesc = | |
2551 | 4180 lispdesc_indirect_description (data, desc1->data2.descr); |
1598 | 4181 const char *dobj = * (const char **) rdata; |
4182 if (dobj) | |
2645 | 4183 mark_struct_contents (dobj, sdesc, count, level, pos); |
1598 | 4184 break; |
4185 } | |
2367 | 4186 case XD_BLOCK_ARRAY: |
1598 | 4187 { |
4188 EMACS_INT count = lispdesc_indirect_count (desc1->data1, desc, | |
4189 data); | |
4190 const struct sized_memory_description *sdesc = | |
2551 | 4191 lispdesc_indirect_description (data, desc1->data2.descr); |
1598 | 4192 |
2645 | 4193 mark_struct_contents (rdata, sdesc, count, level, pos); |
1598 | 4194 break; |
4195 } | |
4196 case XD_UNION: | |
4197 case XD_UNION_DYNAMIC_SIZE: | |
4198 desc1 = lispdesc_process_xd_union (desc1, desc, data); | |
4199 if (desc1) | |
4200 goto union_switcheroo; | |
4201 break; | |
4202 | |
4203 default: | |
4204 stderr_out ("Unsupported description type : %d\n", desc1->type); | |
2645 | 4205 kkcc_backtrace (); |
2500 | 4206 ABORT (); |
1598 | 4207 } |
4208 } | |
4209 } | |
2666 | 4210 #ifdef DEBUG_XEMACS |
4211 kkcc_bt_free (); | |
4212 #endif | |
1598 | 4213 } |
934 | 4214 #endif /* USE_KKCC */ |
4215 | |
428 | 4216 /* Mark reference to a Lisp_Object. If the object referred to has not been |
4217 seen yet, recursively mark all the references contained in it. */ | |
4218 | |
4219 void | |
2286 | 4220 mark_object ( |
4221 #ifdef USE_KKCC | |
4222 Lisp_Object UNUSED (obj) | |
4223 #else | |
4224 Lisp_Object obj | |
4225 #endif | |
4226 ) | |
428 | 4227 { |
1598 | 4228 #ifdef USE_KKCC |
4229 /* this code should never be reached when configured for KKCC */ | |
4230 stderr_out ("KKCC: Invalid mark_object call.\n"); | |
4231 stderr_out ("Replace mark_object with kkcc_gc_stack_push_lisp_object.\n"); | |
2500 | 4232 ABORT (); |
1676 | 4233 #else /* not USE_KKCC */ |
1598 | 4234 |
428 | 4235 tail_recurse: |
4236 | |
4237 /* Checks we used to perform */ | |
4238 /* if (EQ (obj, Qnull_pointer)) return; */ | |
4239 /* if (!POINTER_TYPE_P (XGCTYPE (obj))) return; */ | |
4240 /* if (PURIFIED (XPNTR (obj))) return; */ | |
4241 | |
4242 if (XTYPE (obj) == Lisp_Type_Record) | |
4243 { | |
4244 struct lrecord_header *lheader = XRECORD_LHEADER (obj); | |
442 | 4245 |
4246 GC_CHECK_LHEADER_INVARIANTS (lheader); | |
4247 | |
1204 | 4248 /* We handle this separately, above, so we can mark free objects */ |
1265 | 4249 GC_CHECK_NOT_FREE (lheader); |
1204 | 4250 |
442 | 4251 /* All c_readonly objects have their mark bit set, |
4252 so that we only need to check the mark bit here. */ | |
4253 if (! MARKED_RECORD_HEADER_P (lheader)) | |
428 | 4254 { |
4255 MARK_RECORD_HEADER (lheader); | |
442 | 4256 |
1598 | 4257 if (RECORD_MARKER (lheader)) |
4258 { | |
4259 obj = RECORD_MARKER (lheader) (obj); | |
4260 if (!NILP (obj)) goto tail_recurse; | |
4261 } | |
428 | 4262 } |
4263 } | |
1676 | 4264 #endif /* not KKCC */ |
428 | 4265 } |
4266 | |
4267 | |
2720 | 4268 #ifndef MC_ALLOC |
428 | 4269 static int gc_count_num_short_string_in_use; |
647 | 4270 static Bytecount gc_count_string_total_size; |
4271 static Bytecount gc_count_short_string_total_size; | |
428 | 4272 |
4273 /* static int gc_count_total_records_used, gc_count_records_total_size; */ | |
4274 | |
4275 | |
4276 /* stats on lcrecords in use - kinda kludgy */ | |
4277 | |
4278 static struct | |
4279 { | |
4280 int instances_in_use; | |
4281 int bytes_in_use; | |
4282 int instances_freed; | |
4283 int bytes_freed; | |
4284 int instances_on_free_list; | |
707 | 4285 } lcrecord_stats [countof (lrecord_implementations_table) |
4286 + MODULE_DEFINABLE_TYPE_COUNT]; | |
428 | 4287 |
4288 static void | |
442 | 4289 tick_lcrecord_stats (const struct lrecord_header *h, int free_p) |
428 | 4290 { |
647 | 4291 int type_index = h->type; |
428 | 4292 |
4293 if (((struct lcrecord_header *) h)->free) | |
4294 { | |
442 | 4295 gc_checking_assert (!free_p); |
428 | 4296 lcrecord_stats[type_index].instances_on_free_list++; |
4297 } | |
4298 else | |
4299 { | |
1204 | 4300 Bytecount sz = detagged_lisp_object_size (h); |
4301 | |
428 | 4302 if (free_p) |
4303 { | |
4304 lcrecord_stats[type_index].instances_freed++; | |
4305 lcrecord_stats[type_index].bytes_freed += sz; | |
4306 } | |
4307 else | |
4308 { | |
4309 lcrecord_stats[type_index].instances_in_use++; | |
4310 lcrecord_stats[type_index].bytes_in_use += sz; | |
4311 } | |
4312 } | |
4313 } | |
2720 | 4314 #endif /* not MC_ALLOC */ |
428 | 4315 |
4316 | |
2720 | 4317 #ifndef MC_ALLOC |
428 | 4318 /* Free all unmarked records */ |
4319 static void | |
4320 sweep_lcrecords_1 (struct lcrecord_header **prev, int *used) | |
4321 { | |
4322 struct lcrecord_header *header; | |
4323 int num_used = 0; | |
4324 /* int total_size = 0; */ | |
4325 | |
4326 xzero (lcrecord_stats); /* Reset all statistics to 0. */ | |
4327 | |
4328 /* First go through and call all the finalize methods. | |
4329 Then go through and free the objects. There used to | |
4330 be only one loop here, with the call to the finalizer | |
4331 occurring directly before the xfree() below. That | |
4332 is marginally faster but much less safe -- if the | |
4333 finalize method for an object needs to reference any | |
4334 other objects contained within it (and many do), | |
4335 we could easily be screwed by having already freed that | |
4336 other object. */ | |
4337 | |
4338 for (header = *prev; header; header = header->next) | |
4339 { | |
4340 struct lrecord_header *h = &(header->lheader); | |
442 | 4341 |
4342 GC_CHECK_LHEADER_INVARIANTS (h); | |
4343 | |
4344 if (! MARKED_RECORD_HEADER_P (h) && ! header->free) | |
428 | 4345 { |
4346 if (LHEADER_IMPLEMENTATION (h)->finalizer) | |
4347 LHEADER_IMPLEMENTATION (h)->finalizer (h, 0); | |
4348 } | |
4349 } | |
4350 | |
4351 for (header = *prev; header; ) | |
4352 { | |
4353 struct lrecord_header *h = &(header->lheader); | |
442 | 4354 if (MARKED_RECORD_HEADER_P (h)) |
428 | 4355 { |
442 | 4356 if (! C_READONLY_RECORD_HEADER_P (h)) |
428 | 4357 UNMARK_RECORD_HEADER (h); |
4358 num_used++; | |
4359 /* total_size += n->implementation->size_in_bytes (h);*/ | |
440 | 4360 /* #### May modify header->next on a C_READONLY lcrecord */ |
428 | 4361 prev = &(header->next); |
4362 header = *prev; | |
4363 tick_lcrecord_stats (h, 0); | |
4364 } | |
4365 else | |
4366 { | |
4367 struct lcrecord_header *next = header->next; | |
4368 *prev = next; | |
4369 tick_lcrecord_stats (h, 1); | |
4370 /* used to call finalizer right here. */ | |
1726 | 4371 xfree (header, struct lcrecord_header *); |
428 | 4372 header = next; |
4373 } | |
4374 } | |
4375 *used = num_used; | |
4376 /* *total = total_size; */ | |
4377 } | |
4378 | |
4379 /* And the Lord said: Thou shalt use the `c-backslash-region' command | |
4380 to make macros prettier. */ | |
4381 | |
4382 #ifdef ERROR_CHECK_GC | |
4383 | |
771 | 4384 #define SWEEP_FIXED_TYPE_BLOCK_1(typename, obj_type, lheader) \ |
428 | 4385 do { \ |
4386 struct typename##_block *SFTB_current; \ | |
4387 int SFTB_limit; \ | |
4388 int num_free = 0, num_used = 0; \ | |
4389 \ | |
444 | 4390 for (SFTB_current = current_##typename##_block, \ |
428 | 4391 SFTB_limit = current_##typename##_block_index; \ |
4392 SFTB_current; \ | |
4393 ) \ | |
4394 { \ | |
4395 int SFTB_iii; \ | |
4396 \ | |
4397 for (SFTB_iii = 0; SFTB_iii < SFTB_limit; SFTB_iii++) \ | |
4398 { \ | |
4399 obj_type *SFTB_victim = &(SFTB_current->block[SFTB_iii]); \ | |
4400 \ | |
454 | 4401 if (LRECORD_FREE_P (SFTB_victim)) \ |
428 | 4402 { \ |
4403 num_free++; \ | |
4404 } \ | |
4405 else if (C_READONLY_RECORD_HEADER_P (&SFTB_victim->lheader)) \ | |
4406 { \ | |
4407 num_used++; \ | |
4408 } \ | |
442 | 4409 else if (! MARKED_RECORD_HEADER_P (&SFTB_victim->lheader)) \ |
428 | 4410 { \ |
4411 num_free++; \ | |
4412 FREE_FIXED_TYPE (typename, obj_type, SFTB_victim); \ | |
4413 } \ | |
4414 else \ | |
4415 { \ | |
4416 num_used++; \ | |
4417 UNMARK_##typename (SFTB_victim); \ | |
4418 } \ | |
4419 } \ | |
4420 SFTB_current = SFTB_current->prev; \ | |
4421 SFTB_limit = countof (current_##typename##_block->block); \ | |
4422 } \ | |
4423 \ | |
4424 gc_count_num_##typename##_in_use = num_used; \ | |
4425 gc_count_num_##typename##_freelist = num_free; \ | |
4426 } while (0) | |
4427 | |
4428 #else /* !ERROR_CHECK_GC */ | |
4429 | |
771 | 4430 #define SWEEP_FIXED_TYPE_BLOCK_1(typename, obj_type, lheader) \ |
4431 do { \ | |
4432 struct typename##_block *SFTB_current; \ | |
4433 struct typename##_block **SFTB_prev; \ | |
4434 int SFTB_limit; \ | |
4435 int num_free = 0, num_used = 0; \ | |
4436 \ | |
4437 typename##_free_list = 0; \ | |
4438 \ | |
4439 for (SFTB_prev = ¤t_##typename##_block, \ | |
4440 SFTB_current = current_##typename##_block, \ | |
4441 SFTB_limit = current_##typename##_block_index; \ | |
4442 SFTB_current; \ | |
4443 ) \ | |
4444 { \ | |
4445 int SFTB_iii; \ | |
4446 int SFTB_empty = 1; \ | |
4447 Lisp_Free *SFTB_old_free_list = typename##_free_list; \ | |
4448 \ | |
4449 for (SFTB_iii = 0; SFTB_iii < SFTB_limit; SFTB_iii++) \ | |
4450 { \ | |
4451 obj_type *SFTB_victim = &(SFTB_current->block[SFTB_iii]); \ | |
4452 \ | |
4453 if (LRECORD_FREE_P (SFTB_victim)) \ | |
4454 { \ | |
4455 num_free++; \ | |
4456 PUT_FIXED_TYPE_ON_FREE_LIST (typename, obj_type, SFTB_victim); \ | |
4457 } \ | |
4458 else if (C_READONLY_RECORD_HEADER_P (&SFTB_victim->lheader)) \ | |
4459 { \ | |
4460 SFTB_empty = 0; \ | |
4461 num_used++; \ | |
4462 } \ | |
4463 else if (! MARKED_RECORD_HEADER_P (&SFTB_victim->lheader)) \ | |
4464 { \ | |
4465 num_free++; \ | |
4466 FREE_FIXED_TYPE (typename, obj_type, SFTB_victim); \ | |
4467 } \ | |
4468 else \ | |
4469 { \ | |
4470 SFTB_empty = 0; \ | |
4471 num_used++; \ | |
4472 UNMARK_##typename (SFTB_victim); \ | |
4473 } \ | |
4474 } \ | |
4475 if (!SFTB_empty) \ | |
4476 { \ | |
4477 SFTB_prev = &(SFTB_current->prev); \ | |
4478 SFTB_current = SFTB_current->prev; \ | |
4479 } \ | |
4480 else if (SFTB_current == current_##typename##_block \ | |
4481 && !SFTB_current->prev) \ | |
4482 { \ | |
4483 /* No real point in freeing sole allocation block */ \ | |
4484 break; \ | |
4485 } \ | |
4486 else \ | |
4487 { \ | |
4488 struct typename##_block *SFTB_victim_block = SFTB_current; \ | |
4489 if (SFTB_victim_block == current_##typename##_block) \ | |
4490 current_##typename##_block_index \ | |
4491 = countof (current_##typename##_block->block); \ | |
4492 SFTB_current = SFTB_current->prev; \ | |
4493 { \ | |
4494 *SFTB_prev = SFTB_current; \ | |
1726 | 4495 xfree (SFTB_victim_block, struct typename##_block *); \ |
771 | 4496 /* Restore free list to what it was before victim was swept */ \ |
4497 typename##_free_list = SFTB_old_free_list; \ | |
4498 num_free -= SFTB_limit; \ | |
4499 } \ | |
4500 } \ | |
4501 SFTB_limit = countof (current_##typename##_block->block); \ | |
4502 } \ | |
4503 \ | |
4504 gc_count_num_##typename##_in_use = num_used; \ | |
4505 gc_count_num_##typename##_freelist = num_free; \ | |
428 | 4506 } while (0) |
4507 | |
4508 #endif /* !ERROR_CHECK_GC */ | |
4509 | |
771 | 4510 #define SWEEP_FIXED_TYPE_BLOCK(typename, obj_type) \ |
4511 SWEEP_FIXED_TYPE_BLOCK_1 (typename, obj_type, lheader) | |
4512 | |
2720 | 4513 #endif /* not MC_ALLOC */ |
4514 | |
428 | 4515 |
2720 | 4516 #ifndef MC_ALLOC |
428 | 4517 static void |
4518 sweep_conses (void) | |
4519 { | |
4520 #define UNMARK_cons(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
4521 #define ADDITIONAL_FREE_cons(ptr) | |
4522 | |
440 | 4523 SWEEP_FIXED_TYPE_BLOCK (cons, Lisp_Cons); |
428 | 4524 } |
2720 | 4525 #endif /* not MC_ALLOC */ |
428 | 4526 |
4527 /* Explicitly free a cons cell. */ | |
4528 void | |
853 | 4529 free_cons (Lisp_Object cons) |
428 | 4530 { |
2720 | 4531 #ifndef MC_ALLOC /* to avoid compiler warning */ |
853 | 4532 Lisp_Cons *ptr = XCONS (cons); |
2720 | 4533 #endif /* MC_ALLOC */ |
853 | 4534 |
428 | 4535 #ifdef ERROR_CHECK_GC |
2720 | 4536 #ifdef MC_ALLOC |
4537 Lisp_Cons *ptr = XCONS (cons); | |
4538 #endif /* MC_ALLOC */ | |
428 | 4539 /* If the CAR is not an int, then it will be a pointer, which will |
4540 always be four-byte aligned. If this cons cell has already been | |
4541 placed on the free list, however, its car will probably contain | |
4542 a chain pointer to the next cons on the list, which has cleverly | |
4543 had all its 0's and 1's inverted. This allows for a quick | |
1204 | 4544 check to make sure we're not freeing something already freed. |
4545 | |
4546 NOTE: This check may not be necessary. Freeing an object sets its | |
4547 type to lrecord_type_free, which will trip up the XCONS() above -- as | |
4548 well as a check in FREE_FIXED_TYPE(). */ | |
853 | 4549 if (POINTER_TYPE_P (XTYPE (cons_car (ptr)))) |
4550 ASSERT_VALID_POINTER (XPNTR (cons_car (ptr))); | |
428 | 4551 #endif /* ERROR_CHECK_GC */ |
4552 | |
2720 | 4553 #ifdef MC_ALLOC |
4554 free_lrecord (cons); | |
4555 #else /* not MC_ALLOC */ | |
440 | 4556 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (cons, Lisp_Cons, ptr); |
2720 | 4557 #endif /* not MC_ALLOC */ |
428 | 4558 } |
4559 | |
4560 /* explicitly free a list. You **must make sure** that you have | |
4561 created all the cons cells that make up this list and that there | |
4562 are no pointers to any of these cons cells anywhere else. If there | |
4563 are, you will lose. */ | |
4564 | |
4565 void | |
4566 free_list (Lisp_Object list) | |
4567 { | |
4568 Lisp_Object rest, next; | |
4569 | |
4570 for (rest = list; !NILP (rest); rest = next) | |
4571 { | |
4572 next = XCDR (rest); | |
853 | 4573 free_cons (rest); |
428 | 4574 } |
4575 } | |
4576 | |
4577 /* explicitly free an alist. You **must make sure** that you have | |
4578 created all the cons cells that make up this alist and that there | |
4579 are no pointers to any of these cons cells anywhere else. If there | |
4580 are, you will lose. */ | |
4581 | |
4582 void | |
4583 free_alist (Lisp_Object alist) | |
4584 { | |
4585 Lisp_Object rest, next; | |
4586 | |
4587 for (rest = alist; !NILP (rest); rest = next) | |
4588 { | |
4589 next = XCDR (rest); | |
853 | 4590 free_cons (XCAR (rest)); |
4591 free_cons (rest); | |
428 | 4592 } |
4593 } | |
4594 | |
2720 | 4595 #ifndef MC_ALLOC |
428 | 4596 static void |
4597 sweep_compiled_functions (void) | |
4598 { | |
4599 #define UNMARK_compiled_function(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
945 | 4600 #define ADDITIONAL_FREE_compiled_function(ptr) \ |
1726 | 4601 if (ptr->args_in_array) xfree (ptr->args, Lisp_Object *) |
428 | 4602 |
4603 SWEEP_FIXED_TYPE_BLOCK (compiled_function, Lisp_Compiled_Function); | |
4604 } | |
4605 | |
4606 static void | |
4607 sweep_floats (void) | |
4608 { | |
4609 #define UNMARK_float(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
4610 #define ADDITIONAL_FREE_float(ptr) | |
4611 | |
440 | 4612 SWEEP_FIXED_TYPE_BLOCK (float, Lisp_Float); |
428 | 4613 } |
4614 | |
1983 | 4615 #ifdef HAVE_BIGNUM |
4616 static void | |
4617 sweep_bignums (void) | |
4618 { | |
4619 #define UNMARK_bignum(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
4620 #define ADDITIONAL_FREE_bignum(ptr) bignum_fini (ptr->data) | |
4621 | |
4622 SWEEP_FIXED_TYPE_BLOCK (bignum, Lisp_Bignum); | |
4623 } | |
4624 #endif /* HAVE_BIGNUM */ | |
4625 | |
4626 #ifdef HAVE_RATIO | |
4627 static void | |
4628 sweep_ratios (void) | |
4629 { | |
4630 #define UNMARK_ratio(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
4631 #define ADDITIONAL_FREE_ratio(ptr) ratio_fini (ptr->data) | |
4632 | |
4633 SWEEP_FIXED_TYPE_BLOCK (ratio, Lisp_Ratio); | |
4634 } | |
4635 #endif /* HAVE_RATIO */ | |
4636 | |
4637 #ifdef HAVE_BIGFLOAT | |
4638 static void | |
4639 sweep_bigfloats (void) | |
4640 { | |
4641 #define UNMARK_bigfloat(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
4642 #define ADDITIONAL_FREE_bigfloat(ptr) bigfloat_fini (ptr->bf) | |
4643 | |
4644 SWEEP_FIXED_TYPE_BLOCK (bigfloat, Lisp_Bigfloat); | |
4645 } | |
4646 #endif | |
4647 | |
428 | 4648 static void |
4649 sweep_symbols (void) | |
4650 { | |
4651 #define UNMARK_symbol(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
4652 #define ADDITIONAL_FREE_symbol(ptr) | |
4653 | |
440 | 4654 SWEEP_FIXED_TYPE_BLOCK (symbol, Lisp_Symbol); |
428 | 4655 } |
4656 | |
4657 static void | |
4658 sweep_extents (void) | |
4659 { | |
4660 #define UNMARK_extent(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
4661 #define ADDITIONAL_FREE_extent(ptr) | |
4662 | |
4663 SWEEP_FIXED_TYPE_BLOCK (extent, struct extent); | |
4664 } | |
4665 | |
4666 static void | |
4667 sweep_events (void) | |
4668 { | |
4669 #define UNMARK_event(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
4670 #define ADDITIONAL_FREE_event(ptr) | |
4671 | |
440 | 4672 SWEEP_FIXED_TYPE_BLOCK (event, Lisp_Event); |
428 | 4673 } |
2720 | 4674 #endif /* not MC_ALLOC */ |
428 | 4675 |
1204 | 4676 #ifdef EVENT_DATA_AS_OBJECTS |
934 | 4677 |
2720 | 4678 #ifndef MC_ALLOC |
934 | 4679 static void |
4680 sweep_key_data (void) | |
4681 { | |
4682 #define UNMARK_key_data(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
4683 #define ADDITIONAL_FREE_key_data(ptr) | |
4684 | |
4685 SWEEP_FIXED_TYPE_BLOCK (key_data, Lisp_Key_Data); | |
4686 } | |
2720 | 4687 #endif /* not MC_ALLOC */ |
934 | 4688 |
1204 | 4689 void |
4690 free_key_data (Lisp_Object ptr) | |
4691 { | |
2720 | 4692 #ifdef MC_ALLOC |
4693 free_lrecord (ptr); | |
4694 #else /* not MC_ALLOC */ | |
1204 | 4695 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (key_data, Lisp_Key_Data, XKEY_DATA (ptr)); |
2720 | 4696 #endif /* not MC_ALLOC */ |
4697 } | |
4698 | |
4699 #ifndef MC_ALLOC | |
934 | 4700 static void |
4701 sweep_button_data (void) | |
4702 { | |
4703 #define UNMARK_button_data(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
4704 #define ADDITIONAL_FREE_button_data(ptr) | |
4705 | |
4706 SWEEP_FIXED_TYPE_BLOCK (button_data, Lisp_Button_Data); | |
4707 } | |
2720 | 4708 #endif /* not MC_ALLOC */ |
934 | 4709 |
1204 | 4710 void |
4711 free_button_data (Lisp_Object ptr) | |
4712 { | |
2720 | 4713 #ifdef MC_ALLOC |
4714 free_lrecord (ptr); | |
4715 #else /* not MC_ALLOC */ | |
1204 | 4716 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (button_data, Lisp_Button_Data, XBUTTON_DATA (ptr)); |
2720 | 4717 #endif /* not MC_ALLOC */ |
4718 } | |
4719 | |
4720 #ifndef MC_ALLOC | |
934 | 4721 static void |
4722 sweep_motion_data (void) | |
4723 { | |
4724 #define UNMARK_motion_data(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
4725 #define ADDITIONAL_FREE_motion_data(ptr) | |
4726 | |
4727 SWEEP_FIXED_TYPE_BLOCK (motion_data, Lisp_Motion_Data); | |
4728 } | |
2720 | 4729 #endif /* not MC_ALLOC */ |
934 | 4730 |
1204 | 4731 void |
4732 free_motion_data (Lisp_Object ptr) | |
4733 { | |
2720 | 4734 #ifdef MC_ALLOC |
4735 free_lrecord (ptr); | |
4736 #else /* not MC_ALLOC */ | |
1204 | 4737 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (motion_data, Lisp_Motion_Data, XMOTION_DATA (ptr)); |
2720 | 4738 #endif /* not MC_ALLOC */ |
4739 } | |
4740 | |
4741 #ifndef MC_ALLOC | |
934 | 4742 static void |
4743 sweep_process_data (void) | |
4744 { | |
4745 #define UNMARK_process_data(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
4746 #define ADDITIONAL_FREE_process_data(ptr) | |
4747 | |
4748 SWEEP_FIXED_TYPE_BLOCK (process_data, Lisp_Process_Data); | |
4749 } | |
2720 | 4750 #endif /* not MC_ALLOC */ |
934 | 4751 |
1204 | 4752 void |
4753 free_process_data (Lisp_Object ptr) | |
4754 { | |
2720 | 4755 #ifdef MC_ALLOC |
4756 free_lrecord (ptr); | |
4757 #else /* not MC_ALLOC */ | |
1204 | 4758 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (process_data, Lisp_Process_Data, XPROCESS_DATA (ptr)); |
2720 | 4759 #endif /* not MC_ALLOC */ |
4760 } | |
4761 | |
4762 #ifndef MC_ALLOC | |
934 | 4763 static void |
4764 sweep_timeout_data (void) | |
4765 { | |
4766 #define UNMARK_timeout_data(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
4767 #define ADDITIONAL_FREE_timeout_data(ptr) | |
4768 | |
4769 SWEEP_FIXED_TYPE_BLOCK (timeout_data, Lisp_Timeout_Data); | |
4770 } | |
2720 | 4771 #endif /* not MC_ALLOC */ |
934 | 4772 |
1204 | 4773 void |
4774 free_timeout_data (Lisp_Object ptr) | |
4775 { | |
2720 | 4776 #ifdef MC_ALLOC |
4777 free_lrecord (ptr); | |
4778 #else /* not MC_ALLOC */ | |
1204 | 4779 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (timeout_data, Lisp_Timeout_Data, XTIMEOUT_DATA (ptr)); |
2720 | 4780 #endif /* not MC_ALLOC */ |
4781 } | |
4782 | |
4783 #ifndef MC_ALLOC | |
934 | 4784 static void |
4785 sweep_magic_data (void) | |
4786 { | |
4787 #define UNMARK_magic_data(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
4788 #define ADDITIONAL_FREE_magic_data(ptr) | |
4789 | |
4790 SWEEP_FIXED_TYPE_BLOCK (magic_data, Lisp_Magic_Data); | |
4791 } | |
2720 | 4792 #endif /* not MC_ALLOC */ |
934 | 4793 |
1204 | 4794 void |
4795 free_magic_data (Lisp_Object ptr) | |
4796 { | |
2720 | 4797 #ifdef MC_ALLOC |
4798 free_lrecord (ptr); | |
4799 #else /* not MC_ALLOC */ | |
1204 | 4800 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (magic_data, Lisp_Magic_Data, XMAGIC_DATA (ptr)); |
2720 | 4801 #endif /* not MC_ALLOC */ |
4802 } | |
4803 | |
4804 #ifndef MC_ALLOC | |
934 | 4805 static void |
4806 sweep_magic_eval_data (void) | |
4807 { | |
4808 #define UNMARK_magic_eval_data(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
4809 #define ADDITIONAL_FREE_magic_eval_data(ptr) | |
4810 | |
4811 SWEEP_FIXED_TYPE_BLOCK (magic_eval_data, Lisp_Magic_Eval_Data); | |
4812 } | |
2720 | 4813 #endif /* not MC_ALLOC */ |
934 | 4814 |
1204 | 4815 void |
4816 free_magic_eval_data (Lisp_Object ptr) | |
4817 { | |
2720 | 4818 #ifdef MC_ALLOC |
4819 free_lrecord (ptr); | |
4820 #else /* not MC_ALLOC */ | |
1204 | 4821 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (magic_eval_data, Lisp_Magic_Eval_Data, XMAGIC_EVAL_DATA (ptr)); |
2720 | 4822 #endif /* not MC_ALLOC */ |
4823 } | |
4824 | |
4825 #ifndef MC_ALLOC | |
934 | 4826 static void |
4827 sweep_eval_data (void) | |
4828 { | |
4829 #define UNMARK_eval_data(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
4830 #define ADDITIONAL_FREE_eval_data(ptr) | |
4831 | |
4832 SWEEP_FIXED_TYPE_BLOCK (eval_data, Lisp_Eval_Data); | |
4833 } | |
2720 | 4834 #endif /* not MC_ALLOC */ |
934 | 4835 |
1204 | 4836 void |
4837 free_eval_data (Lisp_Object ptr) | |
4838 { | |
2720 | 4839 #ifdef MC_ALLOC |
4840 free_lrecord (ptr); | |
4841 #else /* not MC_ALLOC */ | |
1204 | 4842 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (eval_data, Lisp_Eval_Data, XEVAL_DATA (ptr)); |
2720 | 4843 #endif /* not MC_ALLOC */ |
4844 } | |
4845 | |
4846 #ifndef MC_ALLOC | |
934 | 4847 static void |
4848 sweep_misc_user_data (void) | |
4849 { | |
4850 #define UNMARK_misc_user_data(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
4851 #define ADDITIONAL_FREE_misc_user_data(ptr) | |
4852 | |
4853 SWEEP_FIXED_TYPE_BLOCK (misc_user_data, Lisp_Misc_User_Data); | |
4854 } | |
2720 | 4855 #endif /* not MC_ALLOC */ |
934 | 4856 |
1204 | 4857 void |
4858 free_misc_user_data (Lisp_Object ptr) | |
4859 { | |
2720 | 4860 #ifdef MC_ALLOC |
4861 free_lrecord (ptr); | |
4862 #else /* not MC_ALLOC */ | |
1204 | 4863 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (misc_user_data, Lisp_Misc_User_Data, XMISC_USER_DATA (ptr)); |
2720 | 4864 #endif /* not MC_ALLOC */ |
1204 | 4865 } |
4866 | |
4867 #endif /* EVENT_DATA_AS_OBJECTS */ | |
934 | 4868 |
2720 | 4869 #ifndef MC_ALLOC |
428 | 4870 static void |
4871 sweep_markers (void) | |
4872 { | |
4873 #define UNMARK_marker(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
4874 #define ADDITIONAL_FREE_marker(ptr) \ | |
4875 do { Lisp_Object tem; \ | |
793 | 4876 tem = wrap_marker (ptr); \ |
428 | 4877 unchain_marker (tem); \ |
4878 } while (0) | |
4879 | |
440 | 4880 SWEEP_FIXED_TYPE_BLOCK (marker, Lisp_Marker); |
428 | 4881 } |
2720 | 4882 #endif /* not MC_ALLOC */ |
428 | 4883 |
4884 /* Explicitly free a marker. */ | |
4885 void | |
1204 | 4886 free_marker (Lisp_Object ptr) |
428 | 4887 { |
2720 | 4888 #ifdef MC_ALLOC |
4889 free_lrecord (ptr); | |
4890 #else /* not MC_ALLOC */ | |
1204 | 4891 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (marker, Lisp_Marker, XMARKER (ptr)); |
2720 | 4892 #endif /* not MC_ALLOC */ |
428 | 4893 } |
4894 | |
4895 | |
4896 #if defined (MULE) && defined (VERIFY_STRING_CHARS_INTEGRITY) | |
4897 | |
4898 static void | |
4899 verify_string_chars_integrity (void) | |
4900 { | |
4901 struct string_chars_block *sb; | |
4902 | |
4903 /* Scan each existing string block sequentially, string by string. */ | |
4904 for (sb = first_string_chars_block; sb; sb = sb->next) | |
4905 { | |
4906 int pos = 0; | |
4907 /* POS is the index of the next string in the block. */ | |
4908 while (pos < sb->pos) | |
4909 { | |
4910 struct string_chars *s_chars = | |
4911 (struct string_chars *) &(sb->string_chars[pos]); | |
438 | 4912 Lisp_String *string; |
428 | 4913 int size; |
4914 int fullsize; | |
4915 | |
454 | 4916 /* If the string_chars struct is marked as free (i.e. the |
4917 STRING pointer is NULL) then this is an unused chunk of | |
4918 string storage. (See below.) */ | |
4919 | |
4920 if (STRING_CHARS_FREE_P (s_chars)) | |
428 | 4921 { |
4922 fullsize = ((struct unused_string_chars *) s_chars)->fullsize; | |
4923 pos += fullsize; | |
4924 continue; | |
4925 } | |
4926 | |
4927 string = s_chars->string; | |
4928 /* Must be 32-bit aligned. */ | |
4929 assert ((((int) string) & 3) == 0); | |
4930 | |
793 | 4931 size = string->size_; |
428 | 4932 fullsize = STRING_FULLSIZE (size); |
4933 | |
4934 assert (!BIG_STRING_FULLSIZE_P (fullsize)); | |
2720 | 4935 assert (XSTRING_DATA (string) == s_chars->chars); |
428 | 4936 pos += fullsize; |
4937 } | |
4938 assert (pos == sb->pos); | |
4939 } | |
4940 } | |
4941 | |
1204 | 4942 #endif /* defined (MULE) && defined (VERIFY_STRING_CHARS_INTEGRITY) */ |
428 | 4943 |
4944 /* Compactify string chars, relocating the reference to each -- | |
4945 free any empty string_chars_block we see. */ | |
4946 static void | |
4947 compact_string_chars (void) | |
4948 { | |
4949 struct string_chars_block *to_sb = first_string_chars_block; | |
4950 int to_pos = 0; | |
4951 struct string_chars_block *from_sb; | |
4952 | |
4953 /* Scan each existing string block sequentially, string by string. */ | |
4954 for (from_sb = first_string_chars_block; from_sb; from_sb = from_sb->next) | |
4955 { | |
4956 int from_pos = 0; | |
4957 /* FROM_POS is the index of the next string in the block. */ | |
4958 while (from_pos < from_sb->pos) | |
4959 { | |
4960 struct string_chars *from_s_chars = | |
4961 (struct string_chars *) &(from_sb->string_chars[from_pos]); | |
4962 struct string_chars *to_s_chars; | |
438 | 4963 Lisp_String *string; |
428 | 4964 int size; |
4965 int fullsize; | |
4966 | |
454 | 4967 /* If the string_chars struct is marked as free (i.e. the |
4968 STRING pointer is NULL) then this is an unused chunk of | |
4969 string storage. This happens under Mule when a string's | |
4970 size changes in such a way that its fullsize changes. | |
4971 (Strings can change size because a different-length | |
4972 character can be substituted for another character.) | |
4973 In this case, after the bogus string pointer is the | |
4974 "fullsize" of this entry, i.e. how many bytes to skip. */ | |
4975 | |
4976 if (STRING_CHARS_FREE_P (from_s_chars)) | |
428 | 4977 { |
4978 fullsize = ((struct unused_string_chars *) from_s_chars)->fullsize; | |
4979 from_pos += fullsize; | |
4980 continue; | |
4981 } | |
4982 | |
4983 string = from_s_chars->string; | |
1204 | 4984 gc_checking_assert (!(LRECORD_FREE_P (string))); |
428 | 4985 |
793 | 4986 size = string->size_; |
428 | 4987 fullsize = STRING_FULLSIZE (size); |
4988 | |
442 | 4989 gc_checking_assert (! BIG_STRING_FULLSIZE_P (fullsize)); |
428 | 4990 |
4991 /* Just skip it if it isn't marked. */ | |
771 | 4992 if (! MARKED_RECORD_HEADER_P (&(string->u.lheader))) |
428 | 4993 { |
4994 from_pos += fullsize; | |
4995 continue; | |
4996 } | |
4997 | |
4998 /* If it won't fit in what's left of TO_SB, close TO_SB out | |
4999 and go on to the next string_chars_block. We know that TO_SB | |
5000 cannot advance past FROM_SB here since FROM_SB is large enough | |
5001 to currently contain this string. */ | |
5002 if ((to_pos + fullsize) > countof (to_sb->string_chars)) | |
5003 { | |
5004 to_sb->pos = to_pos; | |
5005 to_sb = to_sb->next; | |
5006 to_pos = 0; | |
5007 } | |
5008 | |
5009 /* Compute new address of this string | |
5010 and update TO_POS for the space being used. */ | |
5011 to_s_chars = (struct string_chars *) &(to_sb->string_chars[to_pos]); | |
5012 | |
5013 /* Copy the string_chars to the new place. */ | |
5014 if (from_s_chars != to_s_chars) | |
5015 memmove (to_s_chars, from_s_chars, fullsize); | |
5016 | |
5017 /* Relocate FROM_S_CHARS's reference */ | |
826 | 5018 set_lispstringp_data (string, &(to_s_chars->chars[0])); |
428 | 5019 |
5020 from_pos += fullsize; | |
5021 to_pos += fullsize; | |
5022 } | |
5023 } | |
5024 | |
5025 /* Set current to the last string chars block still used and | |
5026 free any that follow. */ | |
5027 { | |
5028 struct string_chars_block *victim; | |
5029 | |
5030 for (victim = to_sb->next; victim; ) | |
5031 { | |
5032 struct string_chars_block *next = victim->next; | |
1726 | 5033 xfree (victim, struct string_chars_block *); |
428 | 5034 victim = next; |
5035 } | |
5036 | |
5037 current_string_chars_block = to_sb; | |
5038 current_string_chars_block->pos = to_pos; | |
5039 current_string_chars_block->next = 0; | |
5040 } | |
5041 } | |
5042 | |
2720 | 5043 #ifndef MC_ALLOC |
428 | 5044 #if 1 /* Hack to debug missing purecopy's */ |
5045 static int debug_string_purity; | |
5046 | |
5047 static void | |
793 | 5048 debug_string_purity_print (Lisp_Object p) |
428 | 5049 { |
5050 Charcount i; | |
826 | 5051 Charcount s = string_char_length (p); |
442 | 5052 stderr_out ("\""); |
428 | 5053 for (i = 0; i < s; i++) |
5054 { | |
867 | 5055 Ichar ch = string_ichar (p, i); |
428 | 5056 if (ch < 32 || ch >= 126) |
5057 stderr_out ("\\%03o", ch); | |
5058 else if (ch == '\\' || ch == '\"') | |
5059 stderr_out ("\\%c", ch); | |
5060 else | |
5061 stderr_out ("%c", ch); | |
5062 } | |
5063 stderr_out ("\"\n"); | |
5064 } | |
5065 #endif /* 1 */ | |
2720 | 5066 #endif /* not MC_ALLOC */ |
5067 | |
5068 #ifndef MC_ALLOC | |
428 | 5069 static void |
5070 sweep_strings (void) | |
5071 { | |
647 | 5072 int num_small_used = 0; |
5073 Bytecount num_small_bytes = 0, num_bytes = 0; | |
428 | 5074 int debug = debug_string_purity; |
5075 | |
793 | 5076 #define UNMARK_string(ptr) do { \ |
5077 Lisp_String *p = (ptr); \ | |
5078 Bytecount size = p->size_; \ | |
5079 UNMARK_RECORD_HEADER (&(p->u.lheader)); \ | |
5080 num_bytes += size; \ | |
5081 if (!BIG_STRING_SIZE_P (size)) \ | |
5082 { \ | |
5083 num_small_bytes += size; \ | |
5084 num_small_used++; \ | |
5085 } \ | |
5086 if (debug) \ | |
5087 debug_string_purity_print (wrap_string (p)); \ | |
438 | 5088 } while (0) |
5089 #define ADDITIONAL_FREE_string(ptr) do { \ | |
793 | 5090 Bytecount size = ptr->size_; \ |
438 | 5091 if (BIG_STRING_SIZE_P (size)) \ |
1726 | 5092 xfree (ptr->data_, Ibyte *); \ |
438 | 5093 } while (0) |
5094 | |
771 | 5095 SWEEP_FIXED_TYPE_BLOCK_1 (string, Lisp_String, u.lheader); |
428 | 5096 |
5097 gc_count_num_short_string_in_use = num_small_used; | |
5098 gc_count_string_total_size = num_bytes; | |
5099 gc_count_short_string_total_size = num_small_bytes; | |
5100 } | |
2720 | 5101 #endif /* not MC_ALLOC */ |
428 | 5102 |
5103 /* I hate duplicating all this crap! */ | |
5104 int | |
5105 marked_p (Lisp_Object obj) | |
5106 { | |
5107 /* Checks we used to perform. */ | |
5108 /* if (EQ (obj, Qnull_pointer)) return 1; */ | |
5109 /* if (!POINTER_TYPE_P (XGCTYPE (obj))) return 1; */ | |
5110 /* if (PURIFIED (XPNTR (obj))) return 1; */ | |
5111 | |
5112 if (XTYPE (obj) == Lisp_Type_Record) | |
5113 { | |
5114 struct lrecord_header *lheader = XRECORD_LHEADER (obj); | |
442 | 5115 |
5116 GC_CHECK_LHEADER_INVARIANTS (lheader); | |
5117 | |
5118 return MARKED_RECORD_HEADER_P (lheader); | |
428 | 5119 } |
5120 return 1; | |
5121 } | |
5122 | |
5123 static void | |
5124 gc_sweep (void) | |
5125 { | |
2720 | 5126 #ifdef MC_ALLOC |
5127 compact_string_chars (); | |
5128 mc_finalize (); | |
5129 mc_sweep (); | |
5130 #else /* not MC_ALLOC */ | |
428 | 5131 /* Free all unmarked records. Do this at the very beginning, |
5132 before anything else, so that the finalize methods can safely | |
5133 examine items in the objects. sweep_lcrecords_1() makes | |
5134 sure to call all the finalize methods *before* freeing anything, | |
5135 to complete the safety. */ | |
5136 { | |
5137 int ignored; | |
5138 sweep_lcrecords_1 (&all_lcrecords, &ignored); | |
5139 } | |
5140 | |
5141 compact_string_chars (); | |
5142 | |
5143 /* Finalize methods below (called through the ADDITIONAL_FREE_foo | |
5144 macros) must be *extremely* careful to make sure they're not | |
5145 referencing freed objects. The only two existing finalize | |
5146 methods (for strings and markers) pass muster -- the string | |
5147 finalizer doesn't look at anything but its own specially- | |
5148 created block, and the marker finalizer only looks at live | |
5149 buffers (which will never be freed) and at the markers before | |
5150 and after it in the chain (which, by induction, will never be | |
5151 freed because if so, they would have already removed themselves | |
5152 from the chain). */ | |
5153 | |
5154 /* Put all unmarked strings on free list, free'ing the string chars | |
5155 of large unmarked strings */ | |
5156 sweep_strings (); | |
5157 | |
5158 /* Put all unmarked conses on free list */ | |
5159 sweep_conses (); | |
5160 | |
5161 /* Free all unmarked compiled-function objects */ | |
5162 sweep_compiled_functions (); | |
5163 | |
5164 /* Put all unmarked floats on free list */ | |
5165 sweep_floats (); | |
5166 | |
1983 | 5167 #ifdef HAVE_BIGNUM |
5168 /* Put all unmarked bignums on free list */ | |
5169 sweep_bignums (); | |
5170 #endif | |
5171 | |
5172 #ifdef HAVE_RATIO | |
5173 /* Put all unmarked ratios on free list */ | |
5174 sweep_ratios (); | |
5175 #endif | |
5176 | |
5177 #ifdef HAVE_BIGFLOAT | |
5178 /* Put all unmarked bigfloats on free list */ | |
5179 sweep_bigfloats (); | |
5180 #endif | |
5181 | |
428 | 5182 /* Put all unmarked symbols on free list */ |
5183 sweep_symbols (); | |
5184 | |
5185 /* Put all unmarked extents on free list */ | |
5186 sweep_extents (); | |
5187 | |
5188 /* Put all unmarked markers on free list. | |
5189 Dechain each one first from the buffer into which it points. */ | |
5190 sweep_markers (); | |
5191 | |
5192 sweep_events (); | |
5193 | |
1204 | 5194 #ifdef EVENT_DATA_AS_OBJECTS |
934 | 5195 sweep_key_data (); |
5196 sweep_button_data (); | |
5197 sweep_motion_data (); | |
5198 sweep_process_data (); | |
5199 sweep_timeout_data (); | |
5200 sweep_magic_data (); | |
5201 sweep_magic_eval_data (); | |
5202 sweep_eval_data (); | |
5203 sweep_misc_user_data (); | |
1204 | 5204 #endif /* EVENT_DATA_AS_OBJECTS */ |
2720 | 5205 #endif /* not MC_ALLOC */ |
5206 | |
5207 #ifndef MC_ALLOC | |
428 | 5208 #ifdef PDUMP |
442 | 5209 pdump_objects_unmark (); |
428 | 5210 #endif |
2720 | 5211 #endif /* not MC_ALLOC */ |
428 | 5212 } |
5213 | |
5214 /* Clearing for disksave. */ | |
5215 | |
5216 void | |
5217 disksave_object_finalization (void) | |
5218 { | |
5219 /* It's important that certain information from the environment not get | |
5220 dumped with the executable (pathnames, environment variables, etc.). | |
5221 To make it easier to tell when this has happened with strings(1) we | |
5222 clear some known-to-be-garbage blocks of memory, so that leftover | |
5223 results of old evaluation don't look like potential problems. | |
5224 But first we set some notable variables to nil and do one more GC, | |
5225 to turn those strings into garbage. | |
440 | 5226 */ |
428 | 5227 |
5228 /* Yeah, this list is pretty ad-hoc... */ | |
5229 Vprocess_environment = Qnil; | |
771 | 5230 env_initted = 0; |
428 | 5231 Vexec_directory = Qnil; |
5232 Vdata_directory = Qnil; | |
5233 Vsite_directory = Qnil; | |
5234 Vdoc_directory = Qnil; | |
5235 Vexec_path = Qnil; | |
5236 Vload_path = Qnil; | |
5237 /* Vdump_load_path = Qnil; */ | |
5238 /* Release hash tables for locate_file */ | |
5239 Flocate_file_clear_hashing (Qt); | |
771 | 5240 uncache_home_directory (); |
776 | 5241 zero_out_command_line_status_vars (); |
872 | 5242 clear_default_devices (); |
428 | 5243 |
5244 #if defined(LOADHIST) && !(defined(LOADHIST_DUMPED) || \ | |
5245 defined(LOADHIST_BUILTIN)) | |
5246 Vload_history = Qnil; | |
5247 #endif | |
5248 Vshell_file_name = Qnil; | |
5249 | |
5250 garbage_collect_1 (); | |
5251 | |
5252 /* Run the disksave finalization methods of all live objects. */ | |
5253 disksave_object_finalization_1 (); | |
5254 | |
5255 /* Zero out the uninitialized (really, unused) part of the containers | |
5256 for the live strings. */ | |
5257 { | |
5258 struct string_chars_block *scb; | |
5259 for (scb = first_string_chars_block; scb; scb = scb->next) | |
5260 { | |
5261 int count = sizeof (scb->string_chars) - scb->pos; | |
5262 | |
5263 assert (count >= 0 && count < STRING_CHARS_BLOCK_SIZE); | |
440 | 5264 if (count != 0) |
5265 { | |
5266 /* from the block's fill ptr to the end */ | |
5267 memset ((scb->string_chars + scb->pos), 0, count); | |
5268 } | |
428 | 5269 } |
5270 } | |
5271 | |
5272 /* There, that ought to be enough... */ | |
5273 | |
5274 } | |
5275 | |
5276 | |
771 | 5277 int |
5278 begin_gc_forbidden (void) | |
5279 { | |
853 | 5280 return internal_bind_int (&gc_currently_forbidden, 1); |
771 | 5281 } |
5282 | |
5283 void | |
5284 end_gc_forbidden (int count) | |
5285 { | |
5286 unbind_to (count); | |
5287 } | |
5288 | |
428 | 5289 /* Maybe we want to use this when doing a "panic" gc after memory_full()? */ |
5290 static int gc_hooks_inhibited; | |
5291 | |
611 | 5292 struct post_gc_action |
5293 { | |
5294 void (*fun) (void *); | |
5295 void *arg; | |
5296 }; | |
5297 | |
5298 typedef struct post_gc_action post_gc_action; | |
5299 | |
5300 typedef struct | |
5301 { | |
5302 Dynarr_declare (post_gc_action); | |
5303 } post_gc_action_dynarr; | |
5304 | |
5305 static post_gc_action_dynarr *post_gc_actions; | |
5306 | |
5307 /* Register an action to be called at the end of GC. | |
5308 gc_in_progress is 0 when this is called. | |
5309 This is used when it is discovered that an action needs to be taken, | |
5310 but it's during GC, so it's not safe. (e.g. in a finalize method.) | |
5311 | |
5312 As a general rule, do not use Lisp objects here. | |
5313 And NEVER signal an error. | |
5314 */ | |
5315 | |
5316 void | |
5317 register_post_gc_action (void (*fun) (void *), void *arg) | |
5318 { | |
5319 post_gc_action action; | |
5320 | |
5321 if (!post_gc_actions) | |
5322 post_gc_actions = Dynarr_new (post_gc_action); | |
5323 | |
5324 action.fun = fun; | |
5325 action.arg = arg; | |
5326 | |
5327 Dynarr_add (post_gc_actions, action); | |
5328 } | |
5329 | |
5330 static void | |
5331 run_post_gc_actions (void) | |
5332 { | |
5333 int i; | |
5334 | |
5335 if (post_gc_actions) | |
5336 { | |
5337 for (i = 0; i < Dynarr_length (post_gc_actions); i++) | |
5338 { | |
5339 post_gc_action action = Dynarr_at (post_gc_actions, i); | |
5340 (action.fun) (action.arg); | |
5341 } | |
5342 | |
5343 Dynarr_reset (post_gc_actions); | |
5344 } | |
5345 } | |
5346 | |
428 | 5347 |
5348 void | |
5349 garbage_collect_1 (void) | |
5350 { | |
5351 #if MAX_SAVE_STACK > 0 | |
5352 char stack_top_variable; | |
5353 extern char *stack_bottom; | |
5354 #endif | |
5355 struct frame *f; | |
5356 int speccount; | |
5357 int cursor_changed; | |
5358 Lisp_Object pre_gc_cursor; | |
5359 struct gcpro gcpro1; | |
1292 | 5360 PROFILE_DECLARE (); |
428 | 5361 |
1123 | 5362 assert (!in_display || gc_currently_forbidden); |
5363 | |
428 | 5364 if (gc_in_progress |
5365 || gc_currently_forbidden | |
5366 || in_display | |
5367 || preparing_for_armageddon) | |
5368 return; | |
5369 | |
1292 | 5370 PROFILE_RECORD_ENTERING_SECTION (QSin_garbage_collection); |
5371 | |
428 | 5372 /* We used to call selected_frame() here. |
5373 | |
5374 The following functions cannot be called inside GC | |
5375 so we move to after the above tests. */ | |
5376 { | |
5377 Lisp_Object frame; | |
5378 Lisp_Object device = Fselected_device (Qnil); | |
5379 if (NILP (device)) /* Could happen during startup, eg. if always_gc */ | |
5380 return; | |
872 | 5381 frame = Fselected_frame (device); |
428 | 5382 if (NILP (frame)) |
563 | 5383 invalid_state ("No frames exist on device", device); |
428 | 5384 f = XFRAME (frame); |
5385 } | |
5386 | |
5387 pre_gc_cursor = Qnil; | |
5388 cursor_changed = 0; | |
5389 | |
5390 GCPRO1 (pre_gc_cursor); | |
5391 | |
5392 /* Very important to prevent GC during any of the following | |
5393 stuff that might run Lisp code; otherwise, we'll likely | |
5394 have infinite GC recursion. */ | |
771 | 5395 speccount = begin_gc_forbidden (); |
428 | 5396 |
887 | 5397 need_to_signal_post_gc = 0; |
1318 | 5398 recompute_funcall_allocation_flag (); |
887 | 5399 |
428 | 5400 if (!gc_hooks_inhibited) |
853 | 5401 run_hook_trapping_problems |
1333 | 5402 (Qgarbage_collecting, Qpre_gc_hook, |
853 | 5403 INHIBIT_EXISTING_PERMANENT_DISPLAY_OBJECT_DELETION); |
428 | 5404 |
5405 /* Now show the GC cursor/message. */ | |
5406 if (!noninteractive) | |
5407 { | |
5408 if (FRAME_WIN_P (f)) | |
5409 { | |
771 | 5410 Lisp_Object frame = wrap_frame (f); |
428 | 5411 Lisp_Object cursor = glyph_image_instance (Vgc_pointer_glyph, |
5412 FRAME_SELECTED_WINDOW (f), | |
5413 ERROR_ME_NOT, 1); | |
5414 pre_gc_cursor = f->pointer; | |
5415 if (POINTER_IMAGE_INSTANCEP (cursor) | |
5416 /* don't change if we don't know how to change back. */ | |
5417 && POINTER_IMAGE_INSTANCEP (pre_gc_cursor)) | |
5418 { | |
5419 cursor_changed = 1; | |
5420 Fset_frame_pointer (frame, cursor); | |
5421 } | |
5422 } | |
5423 | |
5424 /* Don't print messages to the stream device. */ | |
5425 if (!cursor_changed && !FRAME_STREAM_P (f)) | |
5426 { | |
1154 | 5427 if (garbage_collection_messages) |
5428 { | |
5429 Lisp_Object args[2], whole_msg; | |
5430 args[0] = (STRINGP (Vgc_message) ? Vgc_message : | |
5431 build_msg_string (gc_default_message)); | |
5432 args[1] = build_string ("..."); | |
5433 whole_msg = Fconcat (2, args); | |
5434 echo_area_message (f, (Ibyte *) 0, whole_msg, 0, -1, | |
5435 Qgarbage_collecting); | |
5436 } | |
428 | 5437 } |
5438 } | |
5439 | |
5440 /***** Now we actually start the garbage collection. */ | |
5441 | |
5442 gc_in_progress = 1; | |
2367 | 5443 inhibit_non_essential_conversion_operations = 1; |
428 | 5444 |
5445 gc_generation_number[0]++; | |
5446 | |
5447 #if MAX_SAVE_STACK > 0 | |
5448 | |
5449 /* Save a copy of the contents of the stack, for debugging. */ | |
5450 if (!purify_flag) | |
5451 { | |
5452 /* Static buffer in which we save a copy of the C stack at each GC. */ | |
5453 static char *stack_copy; | |
665 | 5454 static Bytecount stack_copy_size; |
428 | 5455 |
5456 ptrdiff_t stack_diff = &stack_top_variable - stack_bottom; | |
665 | 5457 Bytecount stack_size = (stack_diff > 0 ? stack_diff : -stack_diff); |
428 | 5458 if (stack_size < MAX_SAVE_STACK) |
5459 { | |
5460 if (stack_copy_size < stack_size) | |
5461 { | |
5462 stack_copy = (char *) xrealloc (stack_copy, stack_size); | |
5463 stack_copy_size = stack_size; | |
5464 } | |
5465 | |
5466 memcpy (stack_copy, | |
5467 stack_diff > 0 ? stack_bottom : &stack_top_variable, | |
5468 stack_size); | |
5469 } | |
5470 } | |
5471 #endif /* MAX_SAVE_STACK > 0 */ | |
5472 | |
5473 /* Do some totally ad-hoc resource clearing. */ | |
5474 /* #### generalize this? */ | |
5475 clear_event_resource (); | |
5476 cleanup_specifiers (); | |
1204 | 5477 cleanup_buffer_undo_lists (); |
428 | 5478 |
5479 /* Mark all the special slots that serve as the roots of accessibility. */ | |
5480 | |
1598 | 5481 #ifdef USE_KKCC |
5482 /* initialize kkcc stack */ | |
5483 kkcc_gc_stack_init(); | |
2645 | 5484 #define mark_object(obj) kkcc_gc_stack_push_lisp_object (obj, 0, -1) |
1598 | 5485 #endif /* USE_KKCC */ |
5486 | |
428 | 5487 { /* staticpro() */ |
452 | 5488 Lisp_Object **p = Dynarr_begin (staticpros); |
665 | 5489 Elemcount count; |
452 | 5490 for (count = Dynarr_length (staticpros); count; count--) |
5491 mark_object (**p++); | |
5492 } | |
5493 | |
5494 { /* staticpro_nodump() */ | |
5495 Lisp_Object **p = Dynarr_begin (staticpros_nodump); | |
665 | 5496 Elemcount count; |
452 | 5497 for (count = Dynarr_length (staticpros_nodump); count; count--) |
5498 mark_object (**p++); | |
428 | 5499 } |
5500 | |
2720 | 5501 #ifdef MC_ALLOC |
5502 { /* mcpro () */ | |
5503 Lisp_Object *p = Dynarr_begin (mcpros); | |
5504 Elemcount count; | |
5505 for (count = Dynarr_length (mcpros); count; count--) | |
5506 mark_object (*p++); | |
5507 } | |
5508 #endif /* MC_ALLOC */ | |
5509 | |
428 | 5510 { /* GCPRO() */ |
5511 struct gcpro *tail; | |
5512 int i; | |
5513 for (tail = gcprolist; tail; tail = tail->next) | |
5514 for (i = 0; i < tail->nvars; i++) | |
5515 mark_object (tail->var[i]); | |
5516 } | |
5517 | |
5518 { /* specbind() */ | |
5519 struct specbinding *bind; | |
5520 for (bind = specpdl; bind != specpdl_ptr; bind++) | |
5521 { | |
5522 mark_object (bind->symbol); | |
5523 mark_object (bind->old_value); | |
5524 } | |
5525 } | |
5526 | |
5527 { | |
5528 struct catchtag *catch; | |
5529 for (catch = catchlist; catch; catch = catch->next) | |
5530 { | |
5531 mark_object (catch->tag); | |
5532 mark_object (catch->val); | |
853 | 5533 mark_object (catch->actual_tag); |
2532 | 5534 mark_object (catch->backtrace); |
428 | 5535 } |
5536 } | |
5537 | |
5538 { | |
5539 struct backtrace *backlist; | |
5540 for (backlist = backtrace_list; backlist; backlist = backlist->next) | |
5541 { | |
5542 int nargs = backlist->nargs; | |
5543 int i; | |
5544 | |
5545 mark_object (*backlist->function); | |
1292 | 5546 if (nargs < 0 /* nargs == UNEVALLED || nargs == MANY */ |
5547 /* might be fake (internal profiling entry) */ | |
5548 && backlist->args) | |
428 | 5549 mark_object (backlist->args[0]); |
5550 else | |
5551 for (i = 0; i < nargs; i++) | |
5552 mark_object (backlist->args[i]); | |
5553 } | |
5554 } | |
5555 | |
5556 mark_profiling_info (); | |
5557 | |
5558 /* OK, now do the after-mark stuff. This is for things that | |
5559 are only marked when something else is marked (e.g. weak hash tables). | |
5560 There may be complex dependencies between such objects -- e.g. | |
5561 a weak hash table might be unmarked, but after processing a later | |
5562 weak hash table, the former one might get marked. So we have to | |
5563 iterate until nothing more gets marked. */ | |
1598 | 5564 #ifdef USE_KKCC |
5565 kkcc_marking (); | |
5566 #endif /* USE_KKCC */ | |
1590 | 5567 init_marking_ephemerons (); |
428 | 5568 while (finish_marking_weak_hash_tables () > 0 || |
887 | 5569 finish_marking_weak_lists () > 0 || |
1590 | 5570 continue_marking_ephemerons () > 0) |
1773
aa0db78e67c4
[xemacs-hg @ 2003-11-01 14:54:53 by kaltenbach]
kaltenbach
parents:
1739
diff
changeset
|
5571 #ifdef USE_KKCC |
aa0db78e67c4
[xemacs-hg @ 2003-11-01 14:54:53 by kaltenbach]
kaltenbach
parents:
1739
diff
changeset
|
5572 { |
aa0db78e67c4
[xemacs-hg @ 2003-11-01 14:54:53 by kaltenbach]
kaltenbach
parents:
1739
diff
changeset
|
5573 kkcc_marking (); |
aa0db78e67c4
[xemacs-hg @ 2003-11-01 14:54:53 by kaltenbach]
kaltenbach
parents:
1739
diff
changeset
|
5574 } |
aa0db78e67c4
[xemacs-hg @ 2003-11-01 14:54:53 by kaltenbach]
kaltenbach
parents:
1739
diff
changeset
|
5575 #else /* NOT USE_KKCC */ |
1590 | 5576 ; |
1598 | 5577 #endif /* USE_KKCC */ |
5578 | |
1590 | 5579 /* At this point, we know which objects need to be finalized: we |
5580 still need to resurrect them */ | |
5581 | |
5582 while (finish_marking_ephemerons () > 0 || | |
5583 finish_marking_weak_lists () > 0 || | |
5584 finish_marking_weak_hash_tables () > 0) | |
1643 | 5585 #ifdef USE_KKCC |
1773
aa0db78e67c4
[xemacs-hg @ 2003-11-01 14:54:53 by kaltenbach]
kaltenbach
parents:
1739
diff
changeset
|
5586 { |
aa0db78e67c4
[xemacs-hg @ 2003-11-01 14:54:53 by kaltenbach]
kaltenbach
parents:
1739
diff
changeset
|
5587 kkcc_marking (); |
aa0db78e67c4
[xemacs-hg @ 2003-11-01 14:54:53 by kaltenbach]
kaltenbach
parents:
1739
diff
changeset
|
5588 } |
1643 | 5589 kkcc_gc_stack_free (); |
1676 | 5590 #undef mark_object |
1773
aa0db78e67c4
[xemacs-hg @ 2003-11-01 14:54:53 by kaltenbach]
kaltenbach
parents:
1739
diff
changeset
|
5591 #else /* NOT USE_KKCC */ |
aa0db78e67c4
[xemacs-hg @ 2003-11-01 14:54:53 by kaltenbach]
kaltenbach
parents:
1739
diff
changeset
|
5592 ; |
1643 | 5593 #endif /* USE_KKCC */ |
5594 | |
428 | 5595 /* And prune (this needs to be called after everything else has been |
5596 marked and before we do any sweeping). */ | |
5597 /* #### this is somewhat ad-hoc and should probably be an object | |
5598 method */ | |
5599 prune_weak_hash_tables (); | |
5600 prune_weak_lists (); | |
5601 prune_specifiers (); | |
5602 prune_syntax_tables (); | |
5603 | |
887 | 5604 prune_ephemerons (); |
858 | 5605 prune_weak_boxes (); |
5606 | |
428 | 5607 gc_sweep (); |
5608 | |
5609 consing_since_gc = 0; | |
5610 #ifndef DEBUG_XEMACS | |
5611 /* Allow you to set it really fucking low if you really want ... */ | |
5612 if (gc_cons_threshold < 10000) | |
5613 gc_cons_threshold = 10000; | |
5614 #endif | |
814 | 5615 recompute_need_to_garbage_collect (); |
428 | 5616 |
2367 | 5617 inhibit_non_essential_conversion_operations = 0; |
428 | 5618 gc_in_progress = 0; |
5619 | |
611 | 5620 run_post_gc_actions (); |
5621 | |
428 | 5622 /******* End of garbage collection ********/ |
5623 | |
5624 /* Now remove the GC cursor/message */ | |
5625 if (!noninteractive) | |
5626 { | |
5627 if (cursor_changed) | |
771 | 5628 Fset_frame_pointer (wrap_frame (f), pre_gc_cursor); |
428 | 5629 else if (!FRAME_STREAM_P (f)) |
5630 { | |
5631 /* Show "...done" only if the echo area would otherwise be empty. */ | |
5632 if (NILP (clear_echo_area (selected_frame (), | |
5633 Qgarbage_collecting, 0))) | |
5634 { | |
1154 | 5635 if (garbage_collection_messages) |
5636 { | |
5637 Lisp_Object args[2], whole_msg; | |
5638 args[0] = (STRINGP (Vgc_message) ? Vgc_message : | |
5639 build_msg_string (gc_default_message)); | |
5640 args[1] = build_msg_string ("... done"); | |
5641 whole_msg = Fconcat (2, args); | |
5642 echo_area_message (selected_frame (), (Ibyte *) 0, | |
5643 whole_msg, 0, -1, | |
5644 Qgarbage_collecting); | |
5645 } | |
428 | 5646 } |
5647 } | |
5648 } | |
5649 | |
5650 /* now stop inhibiting GC */ | |
771 | 5651 unbind_to (speccount); |
428 | 5652 |
2720 | 5653 #ifndef MC_ALLOC |
428 | 5654 if (!breathing_space) |
5655 { | |
5656 breathing_space = malloc (4096 - MALLOC_OVERHEAD); | |
5657 } | |
2720 | 5658 #endif /* not MC_ALLOC */ |
428 | 5659 |
5660 UNGCPRO; | |
887 | 5661 |
5662 need_to_signal_post_gc = 1; | |
5663 funcall_allocation_flag = 1; | |
5664 | |
1292 | 5665 PROFILE_RECORD_EXITING_SECTION (QSin_garbage_collection); |
5666 | |
428 | 5667 return; |
5668 } | |
5669 | |
2720 | 5670 #ifdef MC_ALLOC |
5671 #ifdef MC_ALLOC_TYPE_STATS | |
5672 static Lisp_Object | |
5673 gc_plist_hack (const Ascbyte *name, int value, Lisp_Object tail) | |
5674 { | |
5675 /* C doesn't have local functions (or closures, or GC, or readable syntax, | |
5676 or portable numeric datatypes, or bit-vectors, or characters, or | |
5677 arrays, or exceptions, or ...) */ | |
5678 return cons3 (intern (name), make_int (value), tail); | |
5679 } | |
2775 | 5680 |
5681 DEFUN("lrecord-stats", Flrecord_stats, 0, 0 ,"", /* | |
5682 Return statistics about lrecords in a property list. | |
2720 | 5683 */ |
5684 ()) | |
5685 { | |
5686 Lisp_Object pl = Qnil; | |
5687 int i; | |
2775 | 5688 |
2720 | 5689 for (i = 0; i < (countof (lrecord_implementations_table) |
5690 + MODULE_DEFINABLE_TYPE_COUNT); i++) | |
5691 { | |
5692 if (lrecord_stats[i].instances_in_use != 0) | |
5693 { | |
5694 char buf [255]; | |
5695 const char *name = lrecord_implementations_table[i]->name; | |
5696 int len = strlen (name); | |
5697 | |
5698 if (lrecord_stats[i].bytes_in_use_including_overhead != | |
5699 lrecord_stats[i].bytes_in_use) | |
5700 { | |
5701 sprintf (buf, "%s-storage-including-overhead", name); | |
5702 pl = gc_plist_hack (buf, | |
5703 lrecord_stats[i] | |
5704 .bytes_in_use_including_overhead, | |
5705 pl); | |
5706 } | |
5707 | |
5708 sprintf (buf, "%s-storage", name); | |
5709 pl = gc_plist_hack (buf, | |
5710 lrecord_stats[i].bytes_in_use, | |
5711 pl); | |
5712 | |
5713 if (name[len-1] == 's') | |
5714 sprintf (buf, "%ses-used", name); | |
5715 else | |
5716 sprintf (buf, "%ss-used", name); | |
5717 pl = gc_plist_hack (buf, lrecord_stats[i].instances_in_use, pl); | |
5718 } | |
5719 } | |
2775 | 5720 pl = gc_plist_hack ("string-data-storage-including-overhead", |
5721 lrecord_string_data_bytes_in_use_including_overhead, pl); | |
5722 pl = gc_plist_hack ("string-data-storage-additional", | |
5723 lrecord_string_data_bytes_in_use, pl); | |
5724 pl = gc_plist_hack ("string-data-used", | |
5725 lrecord_string_data_instances_in_use, pl); | |
5726 | |
5727 return pl; | |
5728 } | |
5729 #endif /* not MC_ALLOC_TYPE_STATS */ | |
5730 | |
5731 DEFUN ("garbage-collect", Fgarbage_collect, 0, 0, "", /* | |
5732 Reclaim storage for Lisp objects no longer needed. | |
5733 Return info on amount of space in use: | |
5734 ((USED-CONSES . STORAGE-CONSES) (USED-SYMS . STORAGE-SYMS) | |
5735 (USED-MARKERS . STORAGE-MARKERS) USED-STRING-CHARS USED-VECTOR-SLOTS | |
5736 PLIST) | |
5737 where `PLIST' is a list of alternating keyword/value pairs providing | |
5738 more detailed information. | |
5739 Garbage collection happens automatically if you cons more than | |
5740 `gc-cons-threshold' bytes of Lisp data since previous garbage collection. | |
5741 */ | |
5742 ()) | |
5743 { | |
5744 garbage_collect_1 (); | |
5745 | |
5746 #ifdef MC_ALLOC_TYPE_STATS | |
2720 | 5747 /* The things we do for backwards-compatibility */ |
5748 return | |
5749 list6 | |
5750 (Fcons (make_int (lrecord_stats[lrecord_type_cons].instances_in_use), | |
5751 make_int (lrecord_stats[lrecord_type_cons] | |
5752 .bytes_in_use_including_overhead)), | |
2775 | 5753 Fcons (make_int (lrecord_stats[lrecord_type_symbol].instances_in_use), |
2720 | 5754 make_int (lrecord_stats[lrecord_type_symbol] |
5755 .bytes_in_use_including_overhead)), | |
5756 Fcons (make_int (lrecord_stats[lrecord_type_marker].instances_in_use), | |
5757 make_int (lrecord_stats[lrecord_type_marker] | |
5758 .bytes_in_use_including_overhead)), | |
5759 make_int (lrecord_stats[lrecord_type_string] | |
5760 .bytes_in_use_including_overhead), | |
5761 make_int (lrecord_stats[lrecord_type_vector] | |
5762 .bytes_in_use_including_overhead), | |
2775 | 5763 Flrecord_stats ()); |
2720 | 5764 #else /* not MC_ALLOC_TYPE_STATS */ |
5765 return Qnil; | |
5766 #endif /* not MC_ALLOC_TYPE_STATS */ | |
5767 } | |
5768 #else /* not MC_ALLOC */ | |
428 | 5769 /* Debugging aids. */ |
5770 | |
5771 static Lisp_Object | |
2367 | 5772 gc_plist_hack (const Ascbyte *name, int value, Lisp_Object tail) |
428 | 5773 { |
5774 /* C doesn't have local functions (or closures, or GC, or readable syntax, | |
5775 or portable numeric datatypes, or bit-vectors, or characters, or | |
5776 arrays, or exceptions, or ...) */ | |
5777 return cons3 (intern (name), make_int (value), tail); | |
5778 } | |
5779 | |
5780 #define HACK_O_MATIC(type, name, pl) do { \ | |
5781 int s = 0; \ | |
5782 struct type##_block *x = current_##type##_block; \ | |
5783 while (x) { s += sizeof (*x) + MALLOC_OVERHEAD; x = x->prev; } \ | |
5784 (pl) = gc_plist_hack ((name), s, (pl)); \ | |
5785 } while (0) | |
5786 | |
5787 DEFUN ("garbage-collect", Fgarbage_collect, 0, 0, "", /* | |
5788 Reclaim storage for Lisp objects no longer needed. | |
5789 Return info on amount of space in use: | |
5790 ((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS) | |
5791 (USED-MARKERS . FREE-MARKERS) USED-STRING-CHARS USED-VECTOR-SLOTS | |
5792 PLIST) | |
5793 where `PLIST' is a list of alternating keyword/value pairs providing | |
5794 more detailed information. | |
5795 Garbage collection happens automatically if you cons more than | |
5796 `gc-cons-threshold' bytes of Lisp data since previous garbage collection. | |
5797 */ | |
5798 ()) | |
5799 { | |
5800 Lisp_Object pl = Qnil; | |
647 | 5801 int i; |
428 | 5802 int gc_count_vector_total_size = 0; |
5803 garbage_collect_1 (); | |
5804 | |
442 | 5805 for (i = 0; i < lrecord_type_count; i++) |
428 | 5806 { |
5807 if (lcrecord_stats[i].bytes_in_use != 0 | |
5808 || lcrecord_stats[i].bytes_freed != 0 | |
5809 || lcrecord_stats[i].instances_on_free_list != 0) | |
5810 { | |
5811 char buf [255]; | |
442 | 5812 const char *name = lrecord_implementations_table[i]->name; |
428 | 5813 int len = strlen (name); |
5814 /* save this for the FSFmacs-compatible part of the summary */ | |
460 | 5815 if (i == lrecord_type_vector) |
428 | 5816 gc_count_vector_total_size = |
5817 lcrecord_stats[i].bytes_in_use + lcrecord_stats[i].bytes_freed; | |
5818 | |
5819 sprintf (buf, "%s-storage", name); | |
5820 pl = gc_plist_hack (buf, lcrecord_stats[i].bytes_in_use, pl); | |
5821 /* Okay, simple pluralization check for `symbol-value-varalias' */ | |
5822 if (name[len-1] == 's') | |
5823 sprintf (buf, "%ses-freed", name); | |
5824 else | |
5825 sprintf (buf, "%ss-freed", name); | |
5826 if (lcrecord_stats[i].instances_freed != 0) | |
5827 pl = gc_plist_hack (buf, lcrecord_stats[i].instances_freed, pl); | |
5828 if (name[len-1] == 's') | |
5829 sprintf (buf, "%ses-on-free-list", name); | |
5830 else | |
5831 sprintf (buf, "%ss-on-free-list", name); | |
5832 if (lcrecord_stats[i].instances_on_free_list != 0) | |
5833 pl = gc_plist_hack (buf, lcrecord_stats[i].instances_on_free_list, | |
5834 pl); | |
5835 if (name[len-1] == 's') | |
5836 sprintf (buf, "%ses-used", name); | |
5837 else | |
5838 sprintf (buf, "%ss-used", name); | |
5839 pl = gc_plist_hack (buf, lcrecord_stats[i].instances_in_use, pl); | |
5840 } | |
5841 } | |
5842 | |
5843 HACK_O_MATIC (extent, "extent-storage", pl); | |
5844 pl = gc_plist_hack ("extents-free", gc_count_num_extent_freelist, pl); | |
5845 pl = gc_plist_hack ("extents-used", gc_count_num_extent_in_use, pl); | |
5846 HACK_O_MATIC (event, "event-storage", pl); | |
5847 pl = gc_plist_hack ("events-free", gc_count_num_event_freelist, pl); | |
5848 pl = gc_plist_hack ("events-used", gc_count_num_event_in_use, pl); | |
5849 HACK_O_MATIC (marker, "marker-storage", pl); | |
5850 pl = gc_plist_hack ("markers-free", gc_count_num_marker_freelist, pl); | |
5851 pl = gc_plist_hack ("markers-used", gc_count_num_marker_in_use, pl); | |
5852 HACK_O_MATIC (float, "float-storage", pl); | |
5853 pl = gc_plist_hack ("floats-free", gc_count_num_float_freelist, pl); | |
5854 pl = gc_plist_hack ("floats-used", gc_count_num_float_in_use, pl); | |
1983 | 5855 #ifdef HAVE_BIGNUM |
5856 HACK_O_MATIC (bignum, "bignum-storage", pl); | |
5857 pl = gc_plist_hack ("bignums-free", gc_count_num_bignum_freelist, pl); | |
5858 pl = gc_plist_hack ("bignums-used", gc_count_num_bignum_in_use, pl); | |
5859 #endif /* HAVE_BIGNUM */ | |
5860 #ifdef HAVE_RATIO | |
5861 HACK_O_MATIC (ratio, "ratio-storage", pl); | |
5862 pl = gc_plist_hack ("ratios-free", gc_count_num_ratio_freelist, pl); | |
5863 pl = gc_plist_hack ("ratios-used", gc_count_num_ratio_in_use, pl); | |
5864 #endif /* HAVE_RATIO */ | |
5865 #ifdef HAVE_BIGFLOAT | |
5866 HACK_O_MATIC (bigfloat, "bigfloat-storage", pl); | |
5867 pl = gc_plist_hack ("bigfloats-free", gc_count_num_bigfloat_freelist, pl); | |
5868 pl = gc_plist_hack ("bigfloats-used", gc_count_num_bigfloat_in_use, pl); | |
5869 #endif /* HAVE_BIGFLOAT */ | |
428 | 5870 HACK_O_MATIC (string, "string-header-storage", pl); |
5871 pl = gc_plist_hack ("long-strings-total-length", | |
5872 gc_count_string_total_size | |
5873 - gc_count_short_string_total_size, pl); | |
5874 HACK_O_MATIC (string_chars, "short-string-storage", pl); | |
5875 pl = gc_plist_hack ("short-strings-total-length", | |
5876 gc_count_short_string_total_size, pl); | |
5877 pl = gc_plist_hack ("strings-free", gc_count_num_string_freelist, pl); | |
5878 pl = gc_plist_hack ("long-strings-used", | |
5879 gc_count_num_string_in_use | |
5880 - gc_count_num_short_string_in_use, pl); | |
5881 pl = gc_plist_hack ("short-strings-used", | |
5882 gc_count_num_short_string_in_use, pl); | |
5883 | |
5884 HACK_O_MATIC (compiled_function, "compiled-function-storage", pl); | |
5885 pl = gc_plist_hack ("compiled-functions-free", | |
5886 gc_count_num_compiled_function_freelist, pl); | |
5887 pl = gc_plist_hack ("compiled-functions-used", | |
5888 gc_count_num_compiled_function_in_use, pl); | |
5889 | |
5890 HACK_O_MATIC (symbol, "symbol-storage", pl); | |
5891 pl = gc_plist_hack ("symbols-free", gc_count_num_symbol_freelist, pl); | |
5892 pl = gc_plist_hack ("symbols-used", gc_count_num_symbol_in_use, pl); | |
5893 | |
5894 HACK_O_MATIC (cons, "cons-storage", pl); | |
5895 pl = gc_plist_hack ("conses-free", gc_count_num_cons_freelist, pl); | |
5896 pl = gc_plist_hack ("conses-used", gc_count_num_cons_in_use, pl); | |
5897 | |
5898 /* The things we do for backwards-compatibility */ | |
5899 return | |
5900 list6 (Fcons (make_int (gc_count_num_cons_in_use), | |
5901 make_int (gc_count_num_cons_freelist)), | |
5902 Fcons (make_int (gc_count_num_symbol_in_use), | |
5903 make_int (gc_count_num_symbol_freelist)), | |
5904 Fcons (make_int (gc_count_num_marker_in_use), | |
5905 make_int (gc_count_num_marker_freelist)), | |
5906 make_int (gc_count_string_total_size), | |
5907 make_int (gc_count_vector_total_size), | |
5908 pl); | |
5909 } | |
5910 #undef HACK_O_MATIC | |
2720 | 5911 #endif /* not MC_ALLOC */ |
428 | 5912 |
5913 DEFUN ("consing-since-gc", Fconsing_since_gc, 0, 0, "", /* | |
5914 Return the number of bytes consed since the last garbage collection. | |
5915 \"Consed\" is a misnomer in that this actually counts allocation | |
5916 of all different kinds of objects, not just conses. | |
5917 | |
5918 If this value exceeds `gc-cons-threshold', a garbage collection happens. | |
5919 */ | |
5920 ()) | |
5921 { | |
5922 return make_int (consing_since_gc); | |
5923 } | |
5924 | |
440 | 5925 #if 0 |
444 | 5926 DEFUN ("memory-limit", Fmemory_limit, 0, 0, 0, /* |
801 | 5927 Return the address of the last byte XEmacs has allocated, divided by 1024. |
5928 This may be helpful in debugging XEmacs's memory usage. | |
428 | 5929 The value is divided by 1024 to make sure it will fit in a lisp integer. |
5930 */ | |
5931 ()) | |
5932 { | |
5933 return make_int ((EMACS_INT) sbrk (0) / 1024); | |
5934 } | |
440 | 5935 #endif |
428 | 5936 |
801 | 5937 DEFUN ("memory-usage", Fmemory_usage, 0, 0, 0, /* |
5938 Return the total number of bytes used by the data segment in XEmacs. | |
5939 This may be helpful in debugging XEmacs's memory usage. | |
5940 */ | |
5941 ()) | |
5942 { | |
5943 return make_int (total_data_usage ()); | |
5944 } | |
5945 | |
851 | 5946 void |
5947 recompute_funcall_allocation_flag (void) | |
5948 { | |
887 | 5949 funcall_allocation_flag = |
5950 need_to_garbage_collect || | |
5951 need_to_check_c_alloca || | |
5952 need_to_signal_post_gc; | |
851 | 5953 } |
5954 | |
801 | 5955 /* True if it's time to garbage collect now. */ |
814 | 5956 static void |
5957 recompute_need_to_garbage_collect (void) | |
801 | 5958 { |
5959 if (always_gc) | |
814 | 5960 need_to_garbage_collect = 1; |
5961 else | |
5962 need_to_garbage_collect = | |
5963 (consing_since_gc > gc_cons_threshold | |
5964 #if 0 /* #### implement this better */ | |
5965 && | |
5966 (100 * consing_since_gc) / total_data_usage () >= | |
5967 gc_cons_percentage | |
5968 #endif /* 0 */ | |
5969 ); | |
851 | 5970 recompute_funcall_allocation_flag (); |
801 | 5971 } |
5972 | |
428 | 5973 |
5974 int | |
5975 object_dead_p (Lisp_Object obj) | |
5976 { | |
5977 return ((BUFFERP (obj) && !BUFFER_LIVE_P (XBUFFER (obj))) || | |
5978 (FRAMEP (obj) && !FRAME_LIVE_P (XFRAME (obj))) || | |
5979 (WINDOWP (obj) && !WINDOW_LIVE_P (XWINDOW (obj))) || | |
5980 (DEVICEP (obj) && !DEVICE_LIVE_P (XDEVICE (obj))) || | |
5981 (CONSOLEP (obj) && !CONSOLE_LIVE_P (XCONSOLE (obj))) || | |
5982 (EVENTP (obj) && !EVENT_LIVE_P (XEVENT (obj))) || | |
5983 (EXTENTP (obj) && !EXTENT_LIVE_P (XEXTENT (obj)))); | |
5984 } | |
5985 | |
5986 #ifdef MEMORY_USAGE_STATS | |
5987 | |
5988 /* Attempt to determine the actual amount of space that is used for | |
5989 the block allocated starting at PTR, supposedly of size "CLAIMED_SIZE". | |
5990 | |
5991 It seems that the following holds: | |
5992 | |
5993 1. When using the old allocator (malloc.c): | |
5994 | |
5995 -- blocks are always allocated in chunks of powers of two. For | |
5996 each block, there is an overhead of 8 bytes if rcheck is not | |
5997 defined, 20 bytes if it is defined. In other words, a | |
5998 one-byte allocation needs 8 bytes of overhead for a total of | |
5999 9 bytes, and needs to have 16 bytes of memory chunked out for | |
6000 it. | |
6001 | |
6002 2. When using the new allocator (gmalloc.c): | |
6003 | |
6004 -- blocks are always allocated in chunks of powers of two up | |
6005 to 4096 bytes. Larger blocks are allocated in chunks of | |
6006 an integral multiple of 4096 bytes. The minimum block | |
6007 size is 2*sizeof (void *), or 16 bytes if SUNOS_LOCALTIME_BUG | |
6008 is defined. There is no per-block overhead, but there | |
6009 is an overhead of 3*sizeof (size_t) for each 4096 bytes | |
6010 allocated. | |
6011 | |
6012 3. When using the system malloc, anything goes, but they are | |
6013 generally slower and more space-efficient than the GNU | |
6014 allocators. One possibly reasonable assumption to make | |
6015 for want of better data is that sizeof (void *), or maybe | |
6016 2 * sizeof (void *), is required as overhead and that | |
6017 blocks are allocated in the minimum required size except | |
6018 that some minimum block size is imposed (e.g. 16 bytes). */ | |
6019 | |
665 | 6020 Bytecount |
2286 | 6021 malloced_storage_size (void *UNUSED (ptr), Bytecount claimed_size, |
428 | 6022 struct overhead_stats *stats) |
6023 { | |
665 | 6024 Bytecount orig_claimed_size = claimed_size; |
428 | 6025 |
6026 #ifdef GNU_MALLOC | |
665 | 6027 if (claimed_size < (Bytecount) (2 * sizeof (void *))) |
428 | 6028 claimed_size = 2 * sizeof (void *); |
6029 # ifdef SUNOS_LOCALTIME_BUG | |
6030 if (claimed_size < 16) | |
6031 claimed_size = 16; | |
6032 # endif | |
6033 if (claimed_size < 4096) | |
6034 { | |
2260 | 6035 /* fxg: rename log->log2 to supress gcc3 shadow warning */ |
6036 int log2 = 1; | |
428 | 6037 |
6038 /* compute the log base two, more or less, then use it to compute | |
6039 the block size needed. */ | |
6040 claimed_size--; | |
6041 /* It's big, it's heavy, it's wood! */ | |
6042 while ((claimed_size /= 2) != 0) | |
2260 | 6043 ++log2; |
428 | 6044 claimed_size = 1; |
6045 /* It's better than bad, it's good! */ | |
2260 | 6046 while (log2 > 0) |
428 | 6047 { |
6048 claimed_size *= 2; | |
2260 | 6049 log2--; |
428 | 6050 } |
6051 /* We have to come up with some average about the amount of | |
6052 blocks used. */ | |
665 | 6053 if ((Bytecount) (rand () & 4095) < claimed_size) |
428 | 6054 claimed_size += 3 * sizeof (void *); |
6055 } | |
6056 else | |
6057 { | |
6058 claimed_size += 4095; | |
6059 claimed_size &= ~4095; | |
6060 claimed_size += (claimed_size / 4096) * 3 * sizeof (size_t); | |
6061 } | |
6062 | |
6063 #elif defined (SYSTEM_MALLOC) | |
6064 | |
6065 if (claimed_size < 16) | |
6066 claimed_size = 16; | |
6067 claimed_size += 2 * sizeof (void *); | |
6068 | |
6069 #else /* old GNU allocator */ | |
6070 | |
6071 # ifdef rcheck /* #### may not be defined here */ | |
6072 claimed_size += 20; | |
6073 # else | |
6074 claimed_size += 8; | |
6075 # endif | |
6076 { | |
2260 | 6077 /* fxg: rename log->log2 to supress gcc3 shadow warning */ |
6078 int log2 = 1; | |
428 | 6079 |
6080 /* compute the log base two, more or less, then use it to compute | |
6081 the block size needed. */ | |
6082 claimed_size--; | |
6083 /* It's big, it's heavy, it's wood! */ | |
6084 while ((claimed_size /= 2) != 0) | |
2260 | 6085 ++log2; |
428 | 6086 claimed_size = 1; |
6087 /* It's better than bad, it's good! */ | |
2260 | 6088 while (log2 > 0) |
428 | 6089 { |
6090 claimed_size *= 2; | |
2260 | 6091 log2--; |
428 | 6092 } |
6093 } | |
6094 | |
6095 #endif /* old GNU allocator */ | |
6096 | |
6097 if (stats) | |
6098 { | |
6099 stats->was_requested += orig_claimed_size; | |
6100 stats->malloc_overhead += claimed_size - orig_claimed_size; | |
6101 } | |
6102 return claimed_size; | |
6103 } | |
6104 | |
2720 | 6105 #ifndef MC_ALLOC |
665 | 6106 Bytecount |
6107 fixed_type_block_overhead (Bytecount size) | |
428 | 6108 { |
665 | 6109 Bytecount per_block = TYPE_ALLOC_SIZE (cons, unsigned char); |
6110 Bytecount overhead = 0; | |
6111 Bytecount storage_size = malloced_storage_size (0, per_block, 0); | |
428 | 6112 while (size >= per_block) |
6113 { | |
6114 size -= per_block; | |
6115 overhead += sizeof (void *) + per_block - storage_size; | |
6116 } | |
6117 if (rand () % per_block < size) | |
6118 overhead += sizeof (void *) + per_block - storage_size; | |
6119 return overhead; | |
6120 } | |
2720 | 6121 #endif /* not MC_ALLOC */ |
428 | 6122 #endif /* MEMORY_USAGE_STATS */ |
6123 | |
6124 | |
6125 /* Initialization */ | |
771 | 6126 static void |
1204 | 6127 common_init_alloc_early (void) |
428 | 6128 { |
771 | 6129 #ifndef Qzero |
6130 Qzero = make_int (0); /* Only used if Lisp_Object is a union type */ | |
6131 #endif | |
6132 | |
6133 #ifndef Qnull_pointer | |
6134 /* C guarantees that Qnull_pointer will be initialized to all 0 bits, | |
6135 so the following is actually a no-op. */ | |
793 | 6136 Qnull_pointer = wrap_pointer_1 (0); |
771 | 6137 #endif |
6138 | |
428 | 6139 gc_generation_number[0] = 0; |
2720 | 6140 #ifndef MC_ALLOC |
428 | 6141 breathing_space = 0; |
2720 | 6142 #endif /* not MC_ALLOC */ |
771 | 6143 Vgc_message = Qzero; |
2720 | 6144 #ifndef MC_ALLOC |
428 | 6145 all_lcrecords = 0; |
2720 | 6146 #endif /* not MC_ALLOC */ |
428 | 6147 ignore_malloc_warnings = 1; |
6148 #ifdef DOUG_LEA_MALLOC | |
6149 mallopt (M_TRIM_THRESHOLD, 128*1024); /* trim threshold */ | |
6150 mallopt (M_MMAP_THRESHOLD, 64*1024); /* mmap threshold */ | |
6151 #if 0 /* Moved to emacs.c */ | |
6152 mallopt (M_MMAP_MAX, 64); /* max. number of mmap'ed areas */ | |
6153 #endif | |
6154 #endif | |
2720 | 6155 init_string_chars_alloc (); |
6156 #ifndef MC_ALLOC | |
428 | 6157 init_string_alloc (); |
6158 init_string_chars_alloc (); | |
6159 init_cons_alloc (); | |
6160 init_symbol_alloc (); | |
6161 init_compiled_function_alloc (); | |
6162 init_float_alloc (); | |
1983 | 6163 #ifdef HAVE_BIGNUM |
6164 init_bignum_alloc (); | |
6165 #endif | |
6166 #ifdef HAVE_RATIO | |
6167 init_ratio_alloc (); | |
6168 #endif | |
6169 #ifdef HAVE_BIGFLOAT | |
6170 init_bigfloat_alloc (); | |
6171 #endif | |
428 | 6172 init_marker_alloc (); |
6173 init_extent_alloc (); | |
6174 init_event_alloc (); | |
1204 | 6175 #ifdef EVENT_DATA_AS_OBJECTS |
934 | 6176 init_key_data_alloc (); |
6177 init_button_data_alloc (); | |
6178 init_motion_data_alloc (); | |
6179 init_process_data_alloc (); | |
6180 init_timeout_data_alloc (); | |
6181 init_magic_data_alloc (); | |
6182 init_magic_eval_data_alloc (); | |
6183 init_eval_data_alloc (); | |
6184 init_misc_user_data_alloc (); | |
1204 | 6185 #endif /* EVENT_DATA_AS_OBJECTS */ |
2720 | 6186 #endif /* not MC_ALLOC */ |
428 | 6187 |
6188 ignore_malloc_warnings = 0; | |
6189 | |
452 | 6190 if (staticpros_nodump) |
6191 Dynarr_free (staticpros_nodump); | |
6192 staticpros_nodump = Dynarr_new2 (Lisp_Object_ptr_dynarr, Lisp_Object *); | |
6193 Dynarr_resize (staticpros_nodump, 100); /* merely a small optimization */ | |
771 | 6194 #ifdef DEBUG_XEMACS |
6195 if (staticpro_nodump_names) | |
6196 Dynarr_free (staticpro_nodump_names); | |
6197 staticpro_nodump_names = Dynarr_new2 (char_ptr_dynarr, char *); | |
6198 Dynarr_resize (staticpro_nodump_names, 100); /* ditto */ | |
6199 #endif | |
428 | 6200 |
2720 | 6201 #ifdef MC_ALLOC |
6202 mcpros = Dynarr_new2 (Lisp_Object_dynarr, Lisp_Object); | |
6203 Dynarr_resize (mcpros, 1410); /* merely a small optimization */ | |
6204 dump_add_root_block_ptr (&mcpros, &mcpros_description); | |
6205 #ifdef DEBUG_XEMACS | |
6206 mcpro_names = Dynarr_new2 (char_ptr_dynarr, char *); | |
6207 Dynarr_resize (mcpro_names, 1410); /* merely a small optimization */ | |
6208 dump_add_root_block_ptr (&mcpro_names, &mcpro_names_description); | |
6209 #endif | |
6210 #endif /* MC_ALLOC */ | |
6211 | |
428 | 6212 consing_since_gc = 0; |
814 | 6213 need_to_garbage_collect = always_gc; |
851 | 6214 need_to_check_c_alloca = 0; |
6215 funcall_allocation_flag = 0; | |
6216 funcall_alloca_count = 0; | |
814 | 6217 |
428 | 6218 #if 1 |
6219 gc_cons_threshold = 500000; /* XEmacs change */ | |
6220 #else | |
6221 gc_cons_threshold = 15000; /* debugging */ | |
6222 #endif | |
801 | 6223 gc_cons_percentage = 0; /* #### 20; Don't have an accurate measure of |
6224 memory usage on Windows; not verified on other | |
6225 systems */ | |
428 | 6226 lrecord_uid_counter = 259; |
2720 | 6227 #ifndef MC_ALLOC |
428 | 6228 debug_string_purity = 0; |
2720 | 6229 #endif /* not MC_ALLOC */ |
428 | 6230 |
6231 gc_currently_forbidden = 0; | |
6232 gc_hooks_inhibited = 0; | |
6233 | |
800 | 6234 #ifdef ERROR_CHECK_TYPES |
428 | 6235 ERROR_ME.really_unlikely_name_to_have_accidentally_in_a_non_errb_structure = |
6236 666; | |
6237 ERROR_ME_NOT. | |
6238 really_unlikely_name_to_have_accidentally_in_a_non_errb_structure = 42; | |
6239 ERROR_ME_WARN. | |
6240 really_unlikely_name_to_have_accidentally_in_a_non_errb_structure = | |
6241 3333632; | |
793 | 6242 ERROR_ME_DEBUG_WARN. |
6243 really_unlikely_name_to_have_accidentally_in_a_non_errb_structure = | |
6244 8675309; | |
800 | 6245 #endif /* ERROR_CHECK_TYPES */ |
428 | 6246 } |
6247 | |
2720 | 6248 #ifndef MC_ALLOC |
771 | 6249 static void |
6250 init_lcrecord_lists (void) | |
6251 { | |
6252 int i; | |
6253 | |
6254 for (i = 0; i < countof (lrecord_implementations_table); i++) | |
6255 { | |
6256 all_lcrecord_lists[i] = Qzero; /* Qnil not yet set */ | |
6257 staticpro_nodump (&all_lcrecord_lists[i]); | |
6258 } | |
6259 } | |
2720 | 6260 #endif /* not MC_ALLOC */ |
771 | 6261 |
6262 void | |
1204 | 6263 init_alloc_early (void) |
771 | 6264 { |
1204 | 6265 #if defined (__cplusplus) && defined (ERROR_CHECK_GC) |
6266 static struct gcpro initial_gcpro; | |
6267 | |
6268 initial_gcpro.next = 0; | |
6269 initial_gcpro.var = &Qnil; | |
6270 initial_gcpro.nvars = 1; | |
6271 gcprolist = &initial_gcpro; | |
6272 #else | |
6273 gcprolist = 0; | |
6274 #endif /* defined (__cplusplus) && defined (ERROR_CHECK_GC) */ | |
6275 } | |
6276 | |
6277 void | |
6278 reinit_alloc_early (void) | |
6279 { | |
6280 common_init_alloc_early (); | |
2720 | 6281 #ifndef MC_ALLOC |
771 | 6282 init_lcrecord_lists (); |
2720 | 6283 #endif /* not MC_ALLOC */ |
771 | 6284 } |
6285 | |
428 | 6286 void |
6287 init_alloc_once_early (void) | |
6288 { | |
1204 | 6289 common_init_alloc_early (); |
428 | 6290 |
442 | 6291 { |
6292 int i; | |
6293 for (i = 0; i < countof (lrecord_implementations_table); i++) | |
6294 lrecord_implementations_table[i] = 0; | |
6295 } | |
6296 | |
6297 INIT_LRECORD_IMPLEMENTATION (cons); | |
6298 INIT_LRECORD_IMPLEMENTATION (vector); | |
6299 INIT_LRECORD_IMPLEMENTATION (string); | |
2720 | 6300 #ifndef MC_ALLOC |
442 | 6301 INIT_LRECORD_IMPLEMENTATION (lcrecord_list); |
1204 | 6302 INIT_LRECORD_IMPLEMENTATION (free); |
2720 | 6303 #endif /* not MC_ALLOC */ |
428 | 6304 |
452 | 6305 staticpros = Dynarr_new2 (Lisp_Object_ptr_dynarr, Lisp_Object *); |
6306 Dynarr_resize (staticpros, 1410); /* merely a small optimization */ | |
2367 | 6307 dump_add_root_block_ptr (&staticpros, &staticpros_description); |
771 | 6308 #ifdef DEBUG_XEMACS |
6309 staticpro_names = Dynarr_new2 (char_ptr_dynarr, char *); | |
6310 Dynarr_resize (staticpro_names, 1410); /* merely a small optimization */ | |
2367 | 6311 dump_add_root_block_ptr (&staticpro_names, &staticpro_names_description); |
771 | 6312 #endif |
6313 | |
2720 | 6314 #ifdef MC_ALLOC |
6315 mcpros = Dynarr_new2 (Lisp_Object_dynarr, Lisp_Object); | |
6316 Dynarr_resize (mcpros, 1410); /* merely a small optimization */ | |
6317 dump_add_root_block_ptr (&mcpros, &mcpros_description); | |
6318 #ifdef DEBUG_XEMACS | |
6319 mcpro_names = Dynarr_new2 (char_ptr_dynarr, char *); | |
6320 Dynarr_resize (mcpro_names, 1410); /* merely a small optimization */ | |
6321 dump_add_root_block_ptr (&mcpro_names, &mcpro_names_description); | |
6322 #endif | |
6323 #endif /* MC_ALLOC */ | |
6324 | |
6325 #ifndef MC_ALLOC | |
771 | 6326 init_lcrecord_lists (); |
2720 | 6327 #endif /* not MC_ALLOC */ |
428 | 6328 } |
6329 | |
6330 void | |
6331 syms_of_alloc (void) | |
6332 { | |
442 | 6333 DEFSYMBOL (Qpre_gc_hook); |
6334 DEFSYMBOL (Qpost_gc_hook); | |
6335 DEFSYMBOL (Qgarbage_collecting); | |
428 | 6336 |
6337 DEFSUBR (Fcons); | |
6338 DEFSUBR (Flist); | |
6339 DEFSUBR (Fvector); | |
6340 DEFSUBR (Fbit_vector); | |
6341 DEFSUBR (Fmake_byte_code); | |
6342 DEFSUBR (Fmake_list); | |
6343 DEFSUBR (Fmake_vector); | |
6344 DEFSUBR (Fmake_bit_vector); | |
6345 DEFSUBR (Fmake_string); | |
6346 DEFSUBR (Fstring); | |
6347 DEFSUBR (Fmake_symbol); | |
6348 DEFSUBR (Fmake_marker); | |
6349 DEFSUBR (Fpurecopy); | |
2775 | 6350 #ifdef MC_ALLOC_TYPE_STATS |
6351 DEFSUBR (Flrecord_stats); | |
6352 #endif /* MC_ALLOC_TYPE_STATS */ | |
428 | 6353 DEFSUBR (Fgarbage_collect); |
440 | 6354 #if 0 |
428 | 6355 DEFSUBR (Fmemory_limit); |
440 | 6356 #endif |
801 | 6357 DEFSUBR (Fmemory_usage); |
428 | 6358 DEFSUBR (Fconsing_since_gc); |
6359 } | |
6360 | |
6361 void | |
6362 vars_of_alloc (void) | |
6363 { | |
1292 | 6364 QSin_garbage_collection = build_msg_string ("(in garbage collection)"); |
6365 staticpro (&QSin_garbage_collection); | |
6366 | |
428 | 6367 DEFVAR_INT ("gc-cons-threshold", &gc_cons_threshold /* |
6368 *Number of bytes of consing between garbage collections. | |
6369 \"Consing\" is a misnomer in that this actually counts allocation | |
6370 of all different kinds of objects, not just conses. | |
6371 Garbage collection can happen automatically once this many bytes have been | |
6372 allocated since the last garbage collection. All data types count. | |
6373 | |
6374 Garbage collection happens automatically when `eval' or `funcall' are | |
6375 called. (Note that `funcall' is called implicitly as part of evaluation.) | |
6376 By binding this temporarily to a large number, you can effectively | |
6377 prevent garbage collection during a part of the program. | |
6378 | |
853 | 6379 Normally, you cannot set this value less than 10,000 (if you do, it is |
6380 automatically reset during the next garbage collection). However, if | |
6381 XEmacs was compiled with DEBUG_XEMACS, this does not happen, allowing | |
6382 you to set this value very low to track down problems with insufficient | |
6383 GCPRO'ing. If you set this to a negative number, garbage collection will | |
6384 happen at *EVERY* call to `eval' or `funcall'. This is an extremely | |
6385 effective way to check GCPRO problems, but be warned that your XEmacs | |
6386 will be unusable! You almost certainly won't have the patience to wait | |
6387 long enough to be able to set it back. | |
6388 | |
428 | 6389 See also `consing-since-gc'. |
6390 */ ); | |
6391 | |
801 | 6392 DEFVAR_INT ("gc-cons-percentage", &gc_cons_percentage /* |
6393 *Percentage of memory allocated between garbage collections. | |
6394 | |
6395 Garbage collection will happen if this percentage of the total amount of | |
6396 memory used for data has been allocated since the last garbage collection. | |
6397 However, it will not happen if less than `gc-cons-threshold' bytes have | |
6398 been allocated -- this sets an absolute minimum in case very little data | |
6399 has been allocated or the percentage is set very low. Set this to 0 to | |
6400 have garbage collection always happen after `gc-cons-threshold' bytes have | |
6401 been allocated, regardless of current memory usage. | |
6402 | |
6403 Garbage collection happens automatically when `eval' or `funcall' are | |
6404 called. (Note that `funcall' is called implicitly as part of evaluation.) | |
6405 By binding this temporarily to a large number, you can effectively | |
6406 prevent garbage collection during a part of the program. | |
6407 | |
6408 See also `consing-since-gc'. | |
6409 */ ); | |
6410 | |
428 | 6411 #ifdef DEBUG_XEMACS |
6412 DEFVAR_INT ("debug-allocation", &debug_allocation /* | |
6413 If non-zero, print out information to stderr about all objects allocated. | |
6414 See also `debug-allocation-backtrace-length'. | |
6415 */ ); | |
6416 debug_allocation = 0; | |
6417 | |
6418 DEFVAR_INT ("debug-allocation-backtrace-length", | |
6419 &debug_allocation_backtrace_length /* | |
6420 Length (in stack frames) of short backtrace printed out by `debug-allocation'. | |
6421 */ ); | |
6422 debug_allocation_backtrace_length = 2; | |
6423 #endif | |
6424 | |
6425 DEFVAR_BOOL ("purify-flag", &purify_flag /* | |
6426 Non-nil means loading Lisp code in order to dump an executable. | |
6427 This means that certain objects should be allocated in readonly space. | |
6428 */ ); | |
6429 | |
1154 | 6430 DEFVAR_BOOL ("garbage-collection-messages", &garbage_collection_messages /* |
6431 Non-nil means display messages at start and end of garbage collection. | |
6432 */ ); | |
6433 garbage_collection_messages = 0; | |
6434 | |
428 | 6435 DEFVAR_LISP ("pre-gc-hook", &Vpre_gc_hook /* |
6436 Function or functions to be run just before each garbage collection. | |
6437 Interrupts, garbage collection, and errors are inhibited while this hook | |
6438 runs, so be extremely careful in what you add here. In particular, avoid | |
6439 consing, and do not interact with the user. | |
6440 */ ); | |
6441 Vpre_gc_hook = Qnil; | |
6442 | |
6443 DEFVAR_LISP ("post-gc-hook", &Vpost_gc_hook /* | |
6444 Function or functions to be run just after each garbage collection. | |
6445 Interrupts, garbage collection, and errors are inhibited while this hook | |
887 | 6446 runs. Each hook is called with one argument which is an alist with |
6447 finalization data. | |
428 | 6448 */ ); |
6449 Vpost_gc_hook = Qnil; | |
6450 | |
6451 DEFVAR_LISP ("gc-message", &Vgc_message /* | |
6452 String to print to indicate that a garbage collection is in progress. | |
6453 This is printed in the echo area. If the selected frame is on a | |
6454 window system and `gc-pointer-glyph' specifies a value (i.e. a pointer | |
6455 image instance) in the domain of the selected frame, the mouse pointer | |
6456 will change instead of this message being printed. | |
6457 */ ); | |
6458 Vgc_message = build_string (gc_default_message); | |
6459 | |
6460 DEFVAR_LISP ("gc-pointer-glyph", &Vgc_pointer_glyph /* | |
6461 Pointer glyph used to indicate that a garbage collection is in progress. | |
6462 If the selected window is on a window system and this glyph specifies a | |
6463 value (i.e. a pointer image instance) in the domain of the selected | |
6464 window, the pointer will be changed as specified during garbage collection. | |
6465 Otherwise, a message will be printed in the echo area, as controlled | |
6466 by `gc-message'. | |
6467 */ ); | |
6468 } | |
6469 | |
6470 void | |
6471 complex_vars_of_alloc (void) | |
6472 { | |
6473 Vgc_pointer_glyph = Fmake_glyph_internal (Qpointer); | |
6474 } |