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