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