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