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