Mercurial > hg > xemacs-beta
annotate src/alloc.c @ 4786:6e6f7b79c1fc
xemacs: fix issue 634
-------------------- ChangeLog entries follow: --------------------
src/ChangeLog addition:
2009-11-27 Adrian Aichner <adrian@xemacs.org>
* nt.c (mswindows_getdcwd): Check first whether drive is valid
with _getdrives() to avoid crash in _wgetdcwd(...) when linking
against msvcrt8. This fix was taken from the Mozilla project
which experienced the same problem.
| author | Adrian Aichner <adrian@xemacs.org> |
|---|---|
| date | Fri, 27 Nov 2009 00:51:41 +0100 |
| parents | 80d74fed5399 |
| children | 73e8632018ad |
| 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" |
| 3092 | 55 #include "gc.h" |
| 428 | 56 #include "glyphs.h" |
| 57 #include "opaque.h" | |
| 1204 | 58 #include "lstream.h" |
| 872 | 59 #include "process.h" |
| 1292 | 60 #include "profile.h" |
| 428 | 61 #include "redisplay.h" |
| 62 #include "specifier.h" | |
| 63 #include "sysfile.h" | |
| 442 | 64 #include "sysdep.h" |
| 428 | 65 #include "window.h" |
| 3092 | 66 #ifdef NEW_GC |
| 67 #include "vdb.h" | |
| 68 #endif /* NEW_GC */ | |
| 428 | 69 #include "console-stream.h" |
| 70 | |
| 71 #ifdef DOUG_LEA_MALLOC | |
| 72 #include <malloc.h> | |
| 73 #endif | |
| 74 | |
| 75 EXFUN (Fgarbage_collect, 0); | |
| 76 | |
| 77 #if 0 /* this is _way_ too slow to be part of the standard debug options */ | |
| 78 #if defined(DEBUG_XEMACS) && defined(MULE) | |
| 79 #define VERIFY_STRING_CHARS_INTEGRITY | |
| 80 #endif | |
| 81 #endif | |
| 82 | |
| 83 /* Define this to use malloc/free with no freelist for all datatypes, | |
| 84 the hope being that some debugging tools may help detect | |
| 85 freed memory references */ | |
| 86 #ifdef USE_DEBUG_MALLOC /* Taking the above comment at face value -slb */ | |
| 87 #include <dmalloc.h> | |
| 88 #define ALLOC_NO_POOLS | |
| 89 #endif | |
| 90 | |
| 91 #ifdef DEBUG_XEMACS | |
| 458 | 92 static Fixnum debug_allocation; |
| 93 static Fixnum debug_allocation_backtrace_length; | |
| 428 | 94 #endif |
| 95 | |
| 851 | 96 int need_to_check_c_alloca; |
| 887 | 97 int need_to_signal_post_gc; |
| 851 | 98 int funcall_allocation_flag; |
| 99 Bytecount __temp_alloca_size__; | |
| 100 Bytecount funcall_alloca_count; | |
| 814 | 101 |
| 102 /* Determine now whether we need to garbage collect or not, to make | |
| 103 Ffuncall() faster */ | |
| 104 #define INCREMENT_CONS_COUNTER_1(size) \ | |
| 105 do \ | |
| 106 { \ | |
| 107 consing_since_gc += (size); \ | |
| 1292 | 108 total_consing += (size); \ |
| 109 if (profiling_active) \ | |
| 110 profile_record_consing (size); \ | |
| 814 | 111 recompute_need_to_garbage_collect (); \ |
| 112 } while (0) | |
| 428 | 113 |
| 114 #define debug_allocation_backtrace() \ | |
| 115 do { \ | |
| 116 if (debug_allocation_backtrace_length > 0) \ | |
| 117 debug_short_backtrace (debug_allocation_backtrace_length); \ | |
| 118 } while (0) | |
| 119 | |
| 120 #ifdef DEBUG_XEMACS | |
| 801 | 121 #define INCREMENT_CONS_COUNTER(foosize, type) \ |
| 122 do { \ | |
| 123 if (debug_allocation) \ | |
| 124 { \ | |
| 125 stderr_out ("allocating %s (size %ld)\n", type, \ | |
| 126 (long) foosize); \ | |
| 127 debug_allocation_backtrace (); \ | |
| 128 } \ | |
| 129 INCREMENT_CONS_COUNTER_1 (foosize); \ | |
| 428 | 130 } while (0) |
| 131 #define NOSEEUM_INCREMENT_CONS_COUNTER(foosize, type) \ | |
| 132 do { \ | |
| 133 if (debug_allocation > 1) \ | |
| 134 { \ | |
| 801 | 135 stderr_out ("allocating noseeum %s (size %ld)\n", type, \ |
| 136 (long) foosize); \ | |
| 428 | 137 debug_allocation_backtrace (); \ |
| 138 } \ | |
| 139 INCREMENT_CONS_COUNTER_1 (foosize); \ | |
| 140 } while (0) | |
| 141 #else | |
| 142 #define INCREMENT_CONS_COUNTER(size, type) INCREMENT_CONS_COUNTER_1 (size) | |
| 143 #define NOSEEUM_INCREMENT_CONS_COUNTER(size, type) \ | |
| 144 INCREMENT_CONS_COUNTER_1 (size) | |
| 145 #endif | |
| 146 | |
| 3092 | 147 #ifdef NEW_GC |
| 148 /* The call to recompute_need_to_garbage_collect is moved to | |
| 149 free_lrecord, since DECREMENT_CONS_COUNTER is extensively called | |
| 150 during sweep and recomputing need_to_garbage_collect all the time | |
| 151 is not needed. */ | |
| 152 #define DECREMENT_CONS_COUNTER(size) do { \ | |
| 153 consing_since_gc -= (size); \ | |
| 154 total_consing -= (size); \ | |
| 155 if (profiling_active) \ | |
| 156 profile_record_unconsing (size); \ | |
| 157 if (consing_since_gc < 0) \ | |
| 158 consing_since_gc = 0; \ | |
| 159 } while (0) | |
| 160 #else /* not NEW_GC */ | |
| 428 | 161 #define DECREMENT_CONS_COUNTER(size) do { \ |
| 162 consing_since_gc -= (size); \ | |
| 1292 | 163 total_consing -= (size); \ |
| 164 if (profiling_active) \ | |
| 165 profile_record_unconsing (size); \ | |
| 428 | 166 if (consing_since_gc < 0) \ |
| 167 consing_since_gc = 0; \ | |
| 814 | 168 recompute_need_to_garbage_collect (); \ |
| 428 | 169 } while (0) |
| 3092 | 170 #endif /*not NEW_GC */ |
| 428 | 171 |
| 172 /* This is just for use by the printer, to allow things to print uniquely */ | |
| 3063 | 173 int lrecord_uid_counter; |
| 428 | 174 |
| 175 /* Non-zero means we're in the process of doing the dump */ | |
| 176 int purify_flag; | |
| 177 | |
| 1204 | 178 /* Non-zero means we're pdumping out or in */ |
| 179 #ifdef PDUMP | |
| 180 int in_pdump; | |
| 181 #endif | |
| 182 | |
| 800 | 183 #ifdef ERROR_CHECK_TYPES |
| 428 | 184 |
| 793 | 185 Error_Behavior ERROR_ME, ERROR_ME_NOT, ERROR_ME_WARN, ERROR_ME_DEBUG_WARN; |
| 428 | 186 |
| 187 #endif | |
| 188 | |
| 801 | 189 /* Very cheesy ways of figuring out how much memory is being used for |
| 190 data. #### Need better (system-dependent) ways. */ | |
| 191 void *minimum_address_seen; | |
| 192 void *maximum_address_seen; | |
| 193 | |
| 3263 | 194 #ifndef NEW_GC |
| 428 | 195 int |
| 196 c_readonly (Lisp_Object obj) | |
| 197 { | |
| 198 return POINTER_TYPE_P (XTYPE (obj)) && C_READONLY (obj); | |
| 199 } | |
| 3263 | 200 #endif /* not NEW_GC */ |
| 428 | 201 |
| 202 int | |
| 203 lisp_readonly (Lisp_Object obj) | |
| 204 { | |
| 205 return POINTER_TYPE_P (XTYPE (obj)) && LISP_READONLY (obj); | |
| 206 } | |
| 207 | |
| 208 | |
| 209 /* Maximum amount of C stack to save when a GC happens. */ | |
| 210 | |
| 211 #ifndef MAX_SAVE_STACK | |
| 212 #define MAX_SAVE_STACK 0 /* 16000 */ | |
| 213 #endif | |
| 214 | |
| 215 /* Non-zero means ignore malloc warnings. Set during initialization. */ | |
| 216 int ignore_malloc_warnings; | |
| 217 | |
| 218 | |
| 3263 | 219 #ifndef NEW_GC |
| 3092 | 220 void *breathing_space; |
| 428 | 221 |
| 222 void | |
| 223 release_breathing_space (void) | |
| 224 { | |
| 225 if (breathing_space) | |
| 226 { | |
| 227 void *tmp = breathing_space; | |
| 228 breathing_space = 0; | |
| 1726 | 229 xfree (tmp, void *); |
| 428 | 230 } |
| 231 } | |
| 3263 | 232 #endif /* not NEW_GC */ |
| 428 | 233 |
| 801 | 234 static void |
| 235 set_alloc_mins_and_maxes (void *val, Bytecount size) | |
| 236 { | |
| 237 if (!val) | |
| 238 return; | |
| 239 if ((char *) val + size > (char *) maximum_address_seen) | |
| 240 maximum_address_seen = (char *) val + size; | |
| 241 if (!minimum_address_seen) | |
| 242 minimum_address_seen = | |
| 243 #if SIZEOF_VOID_P == 8 | |
| 244 (void *) 0xFFFFFFFFFFFFFFFF; | |
| 245 #else | |
| 246 (void *) 0xFFFFFFFF; | |
| 247 #endif | |
| 248 if ((char *) val < (char *) minimum_address_seen) | |
| 249 minimum_address_seen = (char *) val; | |
| 250 } | |
| 251 | |
| 1315 | 252 #ifdef ERROR_CHECK_MALLOC |
| 3176 | 253 static int in_malloc; |
| 1333 | 254 extern int regex_malloc_disallowed; |
| 2367 | 255 |
| 256 #define MALLOC_BEGIN() \ | |
| 257 do \ | |
| 258 { \ | |
| 3176 | 259 assert (!in_malloc); \ |
| 2367 | 260 assert (!regex_malloc_disallowed); \ |
| 261 in_malloc = 1; \ | |
| 262 } \ | |
| 263 while (0) | |
| 264 | |
| 3263 | 265 #ifdef NEW_GC |
| 2720 | 266 #define FREE_OR_REALLOC_BEGIN(block) \ |
| 267 do \ | |
| 268 { \ | |
| 269 /* Unbelievably, calling free() on 0xDEADBEEF doesn't cause an \ | |
| 270 error until much later on for many system mallocs, such as \ | |
| 271 the one that comes with Solaris 2.3. FMH!! */ \ | |
| 272 assert (block != (void *) 0xDEADBEEF); \ | |
| 273 MALLOC_BEGIN (); \ | |
| 274 } \ | |
| 275 while (0) | |
| 3263 | 276 #else /* not NEW_GC */ |
| 2367 | 277 #define FREE_OR_REALLOC_BEGIN(block) \ |
| 278 do \ | |
| 279 { \ | |
| 280 /* Unbelievably, calling free() on 0xDEADBEEF doesn't cause an \ | |
| 281 error until much later on for many system mallocs, such as \ | |
| 282 the one that comes with Solaris 2.3. FMH!! */ \ | |
| 283 assert (block != (void *) 0xDEADBEEF); \ | |
| 284 /* You cannot free something within dumped space, because there is \ | |
| 285 no longer any sort of malloc structure associated with the block. \ | |
| 286 If you are tripping this, you may need to conditionalize on \ | |
| 287 DUMPEDP. */ \ | |
| 288 assert (!DUMPEDP (block)); \ | |
| 289 MALLOC_BEGIN (); \ | |
| 290 } \ | |
| 291 while (0) | |
| 3263 | 292 #endif /* not NEW_GC */ |
| 2367 | 293 |
| 294 #define MALLOC_END() \ | |
| 295 do \ | |
| 296 { \ | |
| 297 in_malloc = 0; \ | |
| 298 } \ | |
| 299 while (0) | |
| 300 | |
| 301 #else /* ERROR_CHECK_MALLOC */ | |
| 302 | |
| 2658 | 303 #define MALLOC_BEGIN() |
| 2367 | 304 #define FREE_OR_REALLOC_BEGIN(block) |
| 305 #define MALLOC_END() | |
| 306 | |
| 307 #endif /* ERROR_CHECK_MALLOC */ | |
| 308 | |
| 309 static void | |
| 310 malloc_after (void *val, Bytecount size) | |
| 311 { | |
| 312 if (!val && size != 0) | |
| 313 memory_full (); | |
| 314 set_alloc_mins_and_maxes (val, size); | |
| 315 } | |
| 316 | |
| 3305 | 317 /* malloc calls this if it finds we are near exhausting storage */ |
| 318 void | |
| 319 malloc_warning (const char *str) | |
| 320 { | |
| 321 if (ignore_malloc_warnings) | |
| 322 return; | |
| 323 | |
| 324 /* Remove the malloc lock here, because warn_when_safe may allocate | |
| 325 again. It is safe to remove the malloc lock here, because malloc | |
| 326 is already finished (malloc_warning is called via | |
| 327 after_morecore_hook -> check_memory_limits -> save_warn_fun -> | |
| 328 malloc_warning). */ | |
| 329 MALLOC_END (); | |
| 330 | |
| 331 warn_when_safe | |
| 332 (Qmemory, Qemergency, | |
| 333 "%s\n" | |
| 334 "Killing some buffers may delay running out of memory.\n" | |
| 335 "However, certainly by the time you receive the 95%% warning,\n" | |
| 336 "you should clean up, kill this Emacs, and start a new one.", | |
| 337 str); | |
| 338 } | |
| 339 | |
| 340 /* Called if malloc returns zero */ | |
| 341 DOESNT_RETURN | |
| 342 memory_full (void) | |
| 343 { | |
| 344 /* Force a GC next time eval is called. | |
| 345 It's better to loop garbage-collecting (we might reclaim enough | |
| 346 to win) than to loop beeping and barfing "Memory exhausted" | |
| 347 */ | |
| 348 consing_since_gc = gc_cons_threshold + 1; | |
| 349 recompute_need_to_garbage_collect (); | |
| 350 #ifdef NEW_GC | |
| 351 /* Put mc-alloc into memory shortage mode. This may keep XEmacs | |
| 352 alive until the garbage collector can free enough memory to get | |
| 353 us out of the memory exhaustion. If already in memory shortage | |
| 354 mode, we are in a loop and hopelessly lost. */ | |
| 355 if (memory_shortage) | |
| 356 { | |
| 357 fprintf (stderr, "Memory full, cannot recover.\n"); | |
| 358 ABORT (); | |
| 359 } | |
| 360 fprintf (stderr, | |
| 361 "Memory full, try to recover.\n" | |
| 362 "You should clean up, kill this Emacs, and start a new one.\n"); | |
| 363 memory_shortage++; | |
| 364 #else /* not NEW_GC */ | |
| 365 release_breathing_space (); | |
| 366 #endif /* not NEW_GC */ | |
| 367 | |
| 368 /* Flush some histories which might conceivably contain garbalogical | |
| 369 inhibitors. */ | |
| 370 if (!NILP (Fboundp (Qvalues))) | |
| 371 Fset (Qvalues, Qnil); | |
| 372 Vcommand_history = Qnil; | |
| 373 | |
| 374 out_of_memory ("Memory exhausted", Qunbound); | |
| 375 } | |
| 376 | |
| 2367 | 377 /* like malloc, calloc, realloc, free but: |
| 378 | |
| 379 -- check for no memory left | |
| 380 -- set internal mins and maxes | |
| 381 -- with error-checking on, check for reentrancy, invalid freeing, etc. | |
| 382 */ | |
| 1292 | 383 |
| 428 | 384 #undef xmalloc |
| 385 void * | |
| 665 | 386 xmalloc (Bytecount size) |
| 428 | 387 { |
| 1292 | 388 void *val; |
| 2367 | 389 MALLOC_BEGIN (); |
| 1292 | 390 val = malloc (size); |
| 2367 | 391 MALLOC_END (); |
| 392 malloc_after (val, size); | |
| 428 | 393 return val; |
| 394 } | |
| 395 | |
| 396 #undef xcalloc | |
| 397 static void * | |
| 665 | 398 xcalloc (Elemcount nelem, Bytecount elsize) |
| 428 | 399 { |
| 1292 | 400 void *val; |
| 2367 | 401 MALLOC_BEGIN (); |
| 1292 | 402 val= calloc (nelem, elsize); |
| 2367 | 403 MALLOC_END (); |
| 404 malloc_after (val, nelem * elsize); | |
| 428 | 405 return val; |
| 406 } | |
| 407 | |
| 408 void * | |
| 665 | 409 xmalloc_and_zero (Bytecount size) |
| 428 | 410 { |
| 411 return xcalloc (size, sizeof (char)); | |
| 412 } | |
| 413 | |
| 414 #undef xrealloc | |
| 415 void * | |
| 665 | 416 xrealloc (void *block, Bytecount size) |
| 428 | 417 { |
| 2367 | 418 FREE_OR_REALLOC_BEGIN (block); |
| 551 | 419 block = realloc (block, size); |
| 2367 | 420 MALLOC_END (); |
| 421 malloc_after (block, size); | |
| 551 | 422 return block; |
| 428 | 423 } |
| 424 | |
| 425 void | |
| 426 xfree_1 (void *block) | |
| 427 { | |
| 428 #ifdef ERROR_CHECK_MALLOC | |
| 429 assert (block); | |
| 430 #endif /* ERROR_CHECK_MALLOC */ | |
| 2367 | 431 FREE_OR_REALLOC_BEGIN (block); |
| 428 | 432 free (block); |
| 2367 | 433 MALLOC_END (); |
| 428 | 434 } |
| 435 | |
| 436 #ifdef ERROR_CHECK_GC | |
| 437 | |
| 3263 | 438 #ifndef NEW_GC |
| 428 | 439 static void |
| 665 | 440 deadbeef_memory (void *ptr, Bytecount size) |
| 428 | 441 { |
| 826 | 442 UINT_32_BIT *ptr4 = (UINT_32_BIT *) ptr; |
| 665 | 443 Bytecount beefs = size >> 2; |
| 428 | 444 |
| 445 /* In practice, size will always be a multiple of four. */ | |
| 446 while (beefs--) | |
| 1204 | 447 (*ptr4++) = 0xDEADBEEF; /* -559038737 base 10 */ |
| 428 | 448 } |
| 3263 | 449 #endif /* not NEW_GC */ |
| 428 | 450 |
| 451 #else /* !ERROR_CHECK_GC */ | |
| 452 | |
| 453 | |
| 454 #define deadbeef_memory(ptr, size) | |
| 455 | |
| 456 #endif /* !ERROR_CHECK_GC */ | |
| 457 | |
| 458 #undef xstrdup | |
| 459 char * | |
| 442 | 460 xstrdup (const char *str) |
| 428 | 461 { |
| 462 int len = strlen (str) + 1; /* for stupid terminating 0 */ | |
| 463 void *val = xmalloc (len); | |
| 771 | 464 |
| 428 | 465 if (val == 0) return 0; |
| 466 return (char *) memcpy (val, str, len); | |
| 467 } | |
| 468 | |
| 469 #ifdef NEED_STRDUP | |
| 470 char * | |
| 442 | 471 strdup (const char *s) |
| 428 | 472 { |
| 473 return xstrdup (s); | |
| 474 } | |
| 475 #endif /* NEED_STRDUP */ | |
| 476 | |
| 477 | |
| 3263 | 478 #ifndef NEW_GC |
| 428 | 479 static void * |
| 665 | 480 allocate_lisp_storage (Bytecount size) |
| 428 | 481 { |
| 793 | 482 void *val = xmalloc (size); |
| 483 /* We don't increment the cons counter anymore. Calling functions do | |
| 484 that now because we have two different kinds of cons counters -- one | |
| 485 for normal objects, and one for no-see-um conses (and possibly others | |
| 486 similar) where the conses are used totally internally, never escape, | |
| 487 and are created and then freed and shouldn't logically increment the | |
| 488 cons counting. #### (Or perhaps, we should decrement it when an object | |
| 489 get freed?) */ | |
| 490 | |
| 491 /* But we do now (as of 3-27-02) go and zero out the memory. This is a | |
| 492 good thing, as it will guarantee we won't get any intermittent bugs | |
| 1204 | 493 coming from an uninitiated field. The speed loss is unnoticeable, |
| 494 esp. as the objects are not large -- large stuff like buffer text and | |
| 495 redisplay structures are allocated separately. */ | |
| 793 | 496 memset (val, 0, size); |
| 851 | 497 |
| 498 if (need_to_check_c_alloca) | |
| 499 xemacs_c_alloca (0); | |
| 500 | |
| 793 | 501 return val; |
| 428 | 502 } |
| 3263 | 503 #endif /* not NEW_GC */ |
| 504 | |
| 505 #if defined (NEW_GC) && defined (ALLOC_TYPE_STATS) | |
| 2720 | 506 static struct |
| 507 { | |
| 508 int instances_in_use; | |
| 509 int bytes_in_use; | |
| 510 int bytes_in_use_including_overhead; | |
| 3461 | 511 } lrecord_stats [countof (lrecord_implementations_table)]; |
| 2720 | 512 |
| 513 void | |
| 514 init_lrecord_stats () | |
| 515 { | |
| 516 xzero (lrecord_stats); | |
| 517 } | |
| 518 | |
| 519 void | |
| 520 inc_lrecord_stats (Bytecount size, const struct lrecord_header *h) | |
| 521 { | |
| 522 int type_index = h->type; | |
| 523 if (!size) | |
| 524 size = detagged_lisp_object_size (h); | |
| 525 | |
| 526 lrecord_stats[type_index].instances_in_use++; | |
| 527 lrecord_stats[type_index].bytes_in_use += size; | |
| 528 lrecord_stats[type_index].bytes_in_use_including_overhead | |
| 529 #ifdef MEMORY_USAGE_STATS | |
| 530 += mc_alloced_storage_size (size, 0); | |
| 531 #else /* not MEMORY_USAGE_STATS */ | |
| 532 += size; | |
| 533 #endif /* not MEMORY_USAGE_STATS */ | |
| 534 } | |
| 535 | |
| 536 void | |
| 537 dec_lrecord_stats (Bytecount size_including_overhead, | |
| 538 const struct lrecord_header *h) | |
| 539 { | |
| 540 int type_index = h->type; | |
| 2775 | 541 int size = detagged_lisp_object_size (h); |
| 2720 | 542 |
| 543 lrecord_stats[type_index].instances_in_use--; | |
| 2775 | 544 lrecord_stats[type_index].bytes_in_use -= size; |
| 2720 | 545 lrecord_stats[type_index].bytes_in_use_including_overhead |
| 546 -= size_including_overhead; | |
| 547 | |
| 2775 | 548 DECREMENT_CONS_COUNTER (size); |
| 2720 | 549 } |
| 3092 | 550 |
| 551 int | |
| 552 lrecord_stats_heap_size (void) | |
| 553 { | |
| 554 int i; | |
| 555 int size = 0; | |
| 3461 | 556 for (i = 0; i < countof (lrecord_implementations_table); i++) |
| 3092 | 557 size += lrecord_stats[i].bytes_in_use; |
| 558 return size; | |
| 559 } | |
| 3263 | 560 #endif /* NEW_GC && ALLOC_TYPE_STATS */ |
| 561 | |
| 562 #ifndef NEW_GC | |
| 442 | 563 /* lcrecords are chained together through their "next" field. |
| 564 After doing the mark phase, GC will walk this linked list | |
| 565 and free any lcrecord which hasn't been marked. */ | |
| 3024 | 566 static struct old_lcrecord_header *all_lcrecords; |
| 3263 | 567 #endif /* not NEW_GC */ |
| 568 | |
| 569 #ifdef NEW_GC | |
| 2720 | 570 /* The basic lrecord allocation functions. See lrecord.h for details. */ |
| 571 void * | |
| 572 alloc_lrecord (Bytecount size, | |
| 573 const struct lrecord_implementation *implementation) | |
| 574 { | |
| 575 struct lrecord_header *lheader; | |
| 576 | |
| 577 type_checking_assert | |
| 578 ((implementation->static_size == 0 ? | |
| 579 implementation->size_in_bytes_method != NULL : | |
| 580 implementation->static_size == size)); | |
| 581 | |
| 582 lheader = (struct lrecord_header *) mc_alloc (size); | |
| 583 gc_checking_assert (LRECORD_FREE_P (lheader)); | |
| 584 set_lheader_implementation (lheader, implementation); | |
| 2994 | 585 #ifdef ALLOC_TYPE_STATS |
| 2720 | 586 inc_lrecord_stats (size, lheader); |
| 2994 | 587 #endif /* ALLOC_TYPE_STATS */ |
| 3263 | 588 if (implementation->finalizer) |
| 589 add_finalizable_obj (wrap_pointer_1 (lheader)); | |
| 2720 | 590 INCREMENT_CONS_COUNTER (size, implementation->name); |
| 591 return lheader; | |
| 592 } | |
| 593 | |
| 3092 | 594 |
| 2720 | 595 void * |
| 596 noseeum_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); | |
| 2994 | 609 #ifdef ALLOC_TYPE_STATS |
| 2720 | 610 inc_lrecord_stats (size, lheader); |
| 2994 | 611 #endif /* ALLOC_TYPE_STATS */ |
| 3263 | 612 if (implementation->finalizer) |
| 613 add_finalizable_obj (wrap_pointer_1 (lheader)); | |
| 2720 | 614 NOSEEUM_INCREMENT_CONS_COUNTER (size, implementation->name); |
| 615 return lheader; | |
| 616 } | |
| 617 | |
| 3092 | 618 void * |
| 619 alloc_lrecord_array (Bytecount size, int elemcount, | |
| 620 const struct lrecord_implementation *implementation) | |
| 621 { | |
| 622 struct lrecord_header *lheader; | |
| 623 Rawbyte *start, *stop; | |
| 624 | |
| 625 type_checking_assert | |
| 626 ((implementation->static_size == 0 ? | |
| 627 implementation->size_in_bytes_method != NULL : | |
| 628 implementation->static_size == size)); | |
| 629 | |
| 630 lheader = (struct lrecord_header *) mc_alloc_array (size, elemcount); | |
| 631 gc_checking_assert (LRECORD_FREE_P (lheader)); | |
| 632 | |
| 633 for (start = (Rawbyte *) lheader, | |
| 634 stop = ((Rawbyte *) lheader) + (size * elemcount -1); | |
| 635 start < stop; start += size) | |
| 636 { | |
| 637 struct lrecord_header *lh = (struct lrecord_header *) start; | |
| 638 set_lheader_implementation (lh, implementation); | |
| 639 lh->uid = lrecord_uid_counter++; | |
| 640 #ifdef ALLOC_TYPE_STATS | |
| 641 inc_lrecord_stats (size, lh); | |
| 642 #endif /* not ALLOC_TYPE_STATS */ | |
| 3263 | 643 if (implementation->finalizer) |
| 644 add_finalizable_obj (wrap_pointer_1 (lh)); | |
| 3092 | 645 } |
| 646 INCREMENT_CONS_COUNTER (size * elemcount, implementation->name); | |
| 647 return lheader; | |
| 648 } | |
| 649 | |
| 2720 | 650 void |
| 3263 | 651 free_lrecord (Lisp_Object UNUSED (lrecord)) |
| 2720 | 652 { |
| 3263 | 653 /* Manual frees are not allowed with asynchronous finalization */ |
| 654 return; | |
| 2720 | 655 } |
| 3263 | 656 #else /* not NEW_GC */ |
| 428 | 657 |
| 1204 | 658 /* The most basic of the lcrecord allocation functions. Not usually called |
| 659 directly. Allocates an lrecord not managed by any lcrecord-list, of a | |
| 660 specified size. See lrecord.h. */ | |
| 661 | |
| 428 | 662 void * |
| 3024 | 663 old_basic_alloc_lcrecord (Bytecount size, |
| 664 const struct lrecord_implementation *implementation) | |
| 665 { | |
| 666 struct old_lcrecord_header *lcheader; | |
| 428 | 667 |
| 442 | 668 type_checking_assert |
| 669 ((implementation->static_size == 0 ? | |
| 670 implementation->size_in_bytes_method != NULL : | |
| 671 implementation->static_size == size) | |
| 672 && | |
| 673 (! implementation->basic_p) | |
| 674 && | |
| 675 (! (implementation->hash == NULL && implementation->equal != NULL))); | |
| 428 | 676 |
| 3024 | 677 lcheader = (struct old_lcrecord_header *) allocate_lisp_storage (size); |
| 442 | 678 set_lheader_implementation (&lcheader->lheader, implementation); |
| 428 | 679 lcheader->next = all_lcrecords; |
| 680 #if 1 /* mly prefers to see small ID numbers */ | |
| 681 lcheader->uid = lrecord_uid_counter++; | |
| 682 #else /* jwz prefers to see real addrs */ | |
| 683 lcheader->uid = (int) &lcheader; | |
| 684 #endif | |
| 685 lcheader->free = 0; | |
| 686 all_lcrecords = lcheader; | |
| 687 INCREMENT_CONS_COUNTER (size, implementation->name); | |
| 688 return lcheader; | |
| 689 } | |
| 690 | |
| 691 #if 0 /* Presently unused */ | |
| 692 /* Very, very poor man's EGC? | |
| 693 * This may be slow and thrash pages all over the place. | |
| 694 * Only call it if you really feel you must (and if the | |
| 695 * lrecord was fairly recently allocated). | |
| 696 * Otherwise, just let the GC do its job -- that's what it's there for | |
| 697 */ | |
| 698 void | |
| 3024 | 699 very_old_free_lcrecord (struct old_lcrecord_header *lcrecord) |
| 428 | 700 { |
| 701 if (all_lcrecords == lcrecord) | |
| 702 { | |
| 703 all_lcrecords = lcrecord->next; | |
| 704 } | |
| 705 else | |
| 706 { | |
| 3024 | 707 struct old_lcrecord_header *header = all_lcrecords; |
| 428 | 708 for (;;) |
| 709 { | |
| 3024 | 710 struct old_lcrecord_header *next = header->next; |
| 428 | 711 if (next == lcrecord) |
| 712 { | |
| 713 header->next = lrecord->next; | |
| 714 break; | |
| 715 } | |
| 716 else if (next == 0) | |
| 2500 | 717 ABORT (); |
| 428 | 718 else |
| 719 header = next; | |
| 720 } | |
| 721 } | |
| 722 if (lrecord->implementation->finalizer) | |
| 723 lrecord->implementation->finalizer (lrecord, 0); | |
| 724 xfree (lrecord); | |
| 725 return; | |
| 726 } | |
| 727 #endif /* Unused */ | |
| 3263 | 728 #endif /* not NEW_GC */ |
| 428 | 729 |
| 730 | |
| 731 static void | |
| 732 disksave_object_finalization_1 (void) | |
| 733 { | |
| 3263 | 734 #ifdef NEW_GC |
| 2720 | 735 mc_finalize_for_disksave (); |
| 3263 | 736 #else /* not NEW_GC */ |
| 3024 | 737 struct old_lcrecord_header *header; |
| 428 | 738 |
| 739 for (header = all_lcrecords; header; header = header->next) | |
| 740 { | |
| 442 | 741 if (LHEADER_IMPLEMENTATION (&header->lheader)->finalizer && |
| 428 | 742 !header->free) |
| 442 | 743 LHEADER_IMPLEMENTATION (&header->lheader)->finalizer (header, 1); |
| 428 | 744 } |
| 3263 | 745 #endif /* not NEW_GC */ |
| 428 | 746 } |
| 747 | |
| 1204 | 748 /* Bitwise copy all parts of a Lisp object other than the header */ |
| 749 | |
| 750 void | |
| 751 copy_lisp_object (Lisp_Object dst, Lisp_Object src) | |
| 752 { | |
| 753 const struct lrecord_implementation *imp = | |
| 754 XRECORD_LHEADER_IMPLEMENTATION (src); | |
| 755 Bytecount size = lisp_object_size (src); | |
| 756 | |
| 757 assert (imp == XRECORD_LHEADER_IMPLEMENTATION (dst)); | |
| 758 assert (size == lisp_object_size (dst)); | |
| 759 | |
| 3263 | 760 #ifdef NEW_GC |
| 2720 | 761 memcpy ((char *) XRECORD_LHEADER (dst) + sizeof (struct lrecord_header), |
| 762 (char *) XRECORD_LHEADER (src) + sizeof (struct lrecord_header), | |
| 763 size - sizeof (struct lrecord_header)); | |
| 3263 | 764 #else /* not NEW_GC */ |
| 1204 | 765 if (imp->basic_p) |
| 766 memcpy ((char *) XRECORD_LHEADER (dst) + sizeof (struct lrecord_header), | |
| 767 (char *) XRECORD_LHEADER (src) + sizeof (struct lrecord_header), | |
| 768 size - sizeof (struct lrecord_header)); | |
| 769 else | |
| 3024 | 770 memcpy ((char *) XRECORD_LHEADER (dst) + |
| 771 sizeof (struct old_lcrecord_header), | |
| 772 (char *) XRECORD_LHEADER (src) + | |
| 773 sizeof (struct old_lcrecord_header), | |
| 774 size - sizeof (struct old_lcrecord_header)); | |
| 3263 | 775 #endif /* not NEW_GC */ |
| 1204 | 776 } |
| 777 | |
| 428 | 778 |
| 779 /************************************************************************/ | |
| 780 /* Debugger support */ | |
| 781 /************************************************************************/ | |
| 782 /* Give gdb/dbx enough information to decode Lisp Objects. We make | |
| 783 sure certain symbols are always defined, so gdb doesn't complain | |
| 438 | 784 about expressions in src/.gdbinit. See src/.gdbinit or src/.dbxrc |
| 785 to see how this is used. */ | |
| 428 | 786 |
| 458 | 787 EMACS_UINT dbg_valmask = ((1UL << VALBITS) - 1) << GCBITS; |
| 788 EMACS_UINT dbg_typemask = (1UL << GCTYPEBITS) - 1; | |
| 428 | 789 |
| 790 #ifdef USE_UNION_TYPE | |
| 458 | 791 unsigned char dbg_USE_UNION_TYPE = 1; |
| 428 | 792 #else |
| 458 | 793 unsigned char dbg_USE_UNION_TYPE = 0; |
| 428 | 794 #endif |
| 795 | |
| 458 | 796 unsigned char dbg_valbits = VALBITS; |
| 797 unsigned char dbg_gctypebits = GCTYPEBITS; | |
| 798 | |
| 799 /* On some systems, the above definitions will be optimized away by | |
| 800 the compiler or linker unless they are referenced in some function. */ | |
| 801 long dbg_inhibit_dbg_symbol_deletion (void); | |
| 802 long | |
| 803 dbg_inhibit_dbg_symbol_deletion (void) | |
| 804 { | |
| 805 return | |
| 806 (dbg_valmask + | |
| 807 dbg_typemask + | |
| 808 dbg_USE_UNION_TYPE + | |
| 809 dbg_valbits + | |
| 810 dbg_gctypebits); | |
| 811 } | |
| 428 | 812 |
| 813 /* Macros turned into functions for ease of debugging. | |
| 814 Debuggers don't know about macros! */ | |
| 815 int dbg_eq (Lisp_Object obj1, Lisp_Object obj2); | |
| 816 int | |
| 817 dbg_eq (Lisp_Object obj1, Lisp_Object obj2) | |
| 818 { | |
| 819 return EQ (obj1, obj2); | |
| 820 } | |
| 821 | |
| 822 | |
| 3263 | 823 #ifdef NEW_GC |
| 3017 | 824 #define DECLARE_FIXED_TYPE_ALLOC(type, structture) struct __foo__ |
| 825 #else | |
| 428 | 826 /************************************************************************/ |
| 827 /* Fixed-size type macros */ | |
| 828 /************************************************************************/ | |
| 829 | |
| 830 /* For fixed-size types that are commonly used, we malloc() large blocks | |
| 831 of memory at a time and subdivide them into chunks of the correct | |
| 832 size for an object of that type. This is more efficient than | |
| 833 malloc()ing each object separately because we save on malloc() time | |
| 834 and overhead due to the fewer number of malloc()ed blocks, and | |
| 835 also because we don't need any extra pointers within each object | |
| 836 to keep them threaded together for GC purposes. For less common | |
| 837 (and frequently large-size) types, we use lcrecords, which are | |
| 838 malloc()ed individually and chained together through a pointer | |
| 839 in the lcrecord header. lcrecords do not need to be fixed-size | |
| 840 (i.e. two objects of the same type need not have the same size; | |
| 841 however, the size of a particular object cannot vary dynamically). | |
| 842 It is also much easier to create a new lcrecord type because no | |
| 843 additional code needs to be added to alloc.c. Finally, lcrecords | |
| 844 may be more efficient when there are only a small number of them. | |
| 845 | |
| 846 The types that are stored in these large blocks (or "frob blocks") | |
| 1983 | 847 are cons, all number types except fixnum, compiled-function, symbol, |
| 848 marker, extent, event, and string. | |
| 428 | 849 |
| 850 Note that strings are special in that they are actually stored in | |
| 851 two parts: a structure containing information about the string, and | |
| 852 the actual data associated with the string. The former structure | |
| 853 (a struct Lisp_String) is a fixed-size structure and is managed the | |
| 854 same way as all the other such types. This structure contains a | |
| 855 pointer to the actual string data, which is stored in structures of | |
| 856 type struct string_chars_block. Each string_chars_block consists | |
| 857 of a pointer to a struct Lisp_String, followed by the data for that | |
| 440 | 858 string, followed by another pointer to a Lisp_String, followed by |
| 859 the data for that string, etc. At GC time, the data in these | |
| 860 blocks is compacted by searching sequentially through all the | |
| 428 | 861 blocks and compressing out any holes created by unmarked strings. |
| 862 Strings that are more than a certain size (bigger than the size of | |
| 863 a string_chars_block, although something like half as big might | |
| 864 make more sense) are malloc()ed separately and not stored in | |
| 865 string_chars_blocks. Furthermore, no one string stretches across | |
| 866 two string_chars_blocks. | |
| 867 | |
| 1204 | 868 Vectors are each malloc()ed separately as lcrecords. |
| 428 | 869 |
| 870 In the following discussion, we use conses, but it applies equally | |
| 871 well to the other fixed-size types. | |
| 872 | |
| 873 We store cons cells inside of cons_blocks, allocating a new | |
| 874 cons_block with malloc() whenever necessary. Cons cells reclaimed | |
| 875 by GC are put on a free list to be reallocated before allocating | |
| 876 any new cons cells from the latest cons_block. Each cons_block is | |
| 877 just under 2^n - MALLOC_OVERHEAD bytes long, since malloc (at least | |
| 878 the versions in malloc.c and gmalloc.c) really allocates in units | |
| 879 of powers of two and uses 4 bytes for its own overhead. | |
| 880 | |
| 881 What GC actually does is to search through all the cons_blocks, | |
| 882 from the most recently allocated to the oldest, and put all | |
| 883 cons cells that are not marked (whether or not they're already | |
| 884 free) on a cons_free_list. The cons_free_list is a stack, and | |
| 885 so the cons cells in the oldest-allocated cons_block end up | |
| 886 at the head of the stack and are the first to be reallocated. | |
| 887 If any cons_block is entirely free, it is freed with free() | |
| 888 and its cons cells removed from the cons_free_list. Because | |
| 889 the cons_free_list ends up basically in memory order, we have | |
| 890 a high locality of reference (assuming a reasonable turnover | |
| 891 of allocating and freeing) and have a reasonable probability | |
| 892 of entirely freeing up cons_blocks that have been more recently | |
| 893 allocated. This stage is called the "sweep stage" of GC, and | |
| 894 is executed after the "mark stage", which involves starting | |
| 895 from all places that are known to point to in-use Lisp objects | |
| 896 (e.g. the obarray, where are all symbols are stored; the | |
| 897 current catches and condition-cases; the backtrace list of | |
| 898 currently executing functions; the gcpro list; etc.) and | |
| 899 recursively marking all objects that are accessible. | |
| 900 | |
| 454 | 901 At the beginning of the sweep stage, the conses in the cons blocks |
| 902 are in one of three states: in use and marked, in use but not | |
| 903 marked, and not in use (already freed). Any conses that are marked | |
| 904 have been marked in the mark stage just executed, because as part | |
| 905 of the sweep stage we unmark any marked objects. The way we tell | |
| 906 whether or not a cons cell is in use is through the LRECORD_FREE_P | |
| 907 macro. This uses a special lrecord type `lrecord_type_free', | |
| 908 which is never associated with any valid object. | |
| 909 | |
| 910 Conses on the free_cons_list are threaded through a pointer stored | |
| 911 in the conses themselves. Because the cons is still in a | |
| 912 cons_block and needs to remain marked as not in use for the next | |
| 913 time that GC happens, we need room to store both the "free" | |
| 914 indicator and the chaining pointer. So this pointer is stored | |
| 915 after the lrecord header (actually where C places a pointer after | |
| 916 the lrecord header; they are not necessarily contiguous). This | |
| 917 implies that all fixed-size types must be big enough to contain at | |
| 918 least one pointer. This is true for all current fixed-size types, | |
| 919 with the possible exception of Lisp_Floats, for which we define the | |
| 920 meat of the struct using a union of a pointer and a double to | |
| 921 ensure adequate space for the free list chain pointer. | |
| 428 | 922 |
| 923 Some types of objects need additional "finalization" done | |
| 924 when an object is converted from in use to not in use; | |
| 925 this is the purpose of the ADDITIONAL_FREE_type macro. | |
| 926 For example, markers need to be removed from the chain | |
| 927 of markers that is kept in each buffer. This is because | |
| 928 markers in a buffer automatically disappear if the marker | |
| 929 is no longer referenced anywhere (the same does not | |
| 930 apply to extents, however). | |
| 931 | |
| 932 WARNING: Things are in an extremely bizarre state when | |
| 933 the ADDITIONAL_FREE_type macros are called, so beware! | |
| 934 | |
| 454 | 935 When ERROR_CHECK_GC is defined, we do things differently so as to |
| 936 maximize our chances of catching places where there is insufficient | |
| 937 GCPROing. The thing we want to avoid is having an object that | |
| 938 we're using but didn't GCPRO get freed by GC and then reallocated | |
| 939 while we're in the process of using it -- this will result in | |
| 940 something seemingly unrelated getting trashed, and is extremely | |
| 941 difficult to track down. If the object gets freed but not | |
| 942 reallocated, we can usually catch this because we set most of the | |
| 943 bytes of a freed object to 0xDEADBEEF. (The lisp object type is set | |
| 944 to the invalid type `lrecord_type_free', however, and a pointer | |
| 945 used to chain freed objects together is stored after the lrecord | |
| 946 header; we play some tricks with this pointer to make it more | |
| 428 | 947 bogus, so crashes are more likely to occur right away.) |
| 948 | |
| 949 We want freed objects to stay free as long as possible, | |
| 950 so instead of doing what we do above, we maintain the | |
| 951 free objects in a first-in first-out queue. We also | |
| 952 don't recompute the free list each GC, unlike above; | |
| 953 this ensures that the queue ordering is preserved. | |
| 954 [This means that we are likely to have worse locality | |
| 955 of reference, and that we can never free a frob block | |
| 956 once it's allocated. (Even if we know that all cells | |
| 957 in it are free, there's no easy way to remove all those | |
| 958 cells from the free list because the objects on the | |
| 959 free list are unlikely to be in memory order.)] | |
| 960 Furthermore, we never take objects off the free list | |
| 961 unless there's a large number (usually 1000, but | |
| 962 varies depending on type) of them already on the list. | |
| 963 This way, we ensure that an object that gets freed will | |
| 964 remain free for the next 1000 (or whatever) times that | |
| 440 | 965 an object of that type is allocated. */ |
| 428 | 966 |
| 967 #if !defined(HAVE_MMAP) || defined(DOUG_LEA_MALLOC) | |
| 968 /* If we released our reserve (due to running out of memory), | |
| 969 and we have a fair amount free once again, | |
| 970 try to set aside another reserve in case we run out once more. | |
| 971 | |
| 972 This is called when a relocatable block is freed in ralloc.c. */ | |
| 973 void refill_memory_reserve (void); | |
| 974 void | |
| 442 | 975 refill_memory_reserve (void) |
| 428 | 976 { |
| 977 if (breathing_space == 0) | |
| 978 breathing_space = (char *) malloc (4096 - MALLOC_OVERHEAD); | |
| 979 } | |
| 980 #endif | |
| 981 | |
| 982 #ifdef ALLOC_NO_POOLS | |
| 983 # define TYPE_ALLOC_SIZE(type, structtype) 1 | |
| 984 #else | |
| 985 # define TYPE_ALLOC_SIZE(type, structtype) \ | |
| 986 ((2048 - MALLOC_OVERHEAD - sizeof (struct type##_block *)) \ | |
| 987 / sizeof (structtype)) | |
| 988 #endif /* ALLOC_NO_POOLS */ | |
| 989 | |
| 990 #define DECLARE_FIXED_TYPE_ALLOC(type, structtype) \ | |
| 991 \ | |
| 992 struct type##_block \ | |
| 993 { \ | |
| 994 struct type##_block *prev; \ | |
| 995 structtype block[TYPE_ALLOC_SIZE (type, structtype)]; \ | |
| 996 }; \ | |
| 997 \ | |
| 998 static struct type##_block *current_##type##_block; \ | |
| 999 static int current_##type##_block_index; \ | |
| 1000 \ | |
| 454 | 1001 static Lisp_Free *type##_free_list; \ |
| 1002 static Lisp_Free *type##_free_list_tail; \ | |
| 428 | 1003 \ |
| 1004 static void \ | |
| 1005 init_##type##_alloc (void) \ | |
| 1006 { \ | |
| 1007 current_##type##_block = 0; \ | |
| 1008 current_##type##_block_index = \ | |
| 1009 countof (current_##type##_block->block); \ | |
| 1010 type##_free_list = 0; \ | |
| 1011 type##_free_list_tail = 0; \ | |
| 1012 } \ | |
| 1013 \ | |
| 1014 static int gc_count_num_##type##_in_use; \ | |
| 1015 static int gc_count_num_##type##_freelist | |
| 1016 | |
| 1017 #define ALLOCATE_FIXED_TYPE_FROM_BLOCK(type, result) do { \ | |
| 1018 if (current_##type##_block_index \ | |
| 1019 == countof (current_##type##_block->block)) \ | |
| 1020 { \ | |
| 1021 struct type##_block *AFTFB_new = (struct type##_block *) \ | |
| 1022 allocate_lisp_storage (sizeof (struct type##_block)); \ | |
| 1023 AFTFB_new->prev = current_##type##_block; \ | |
| 1024 current_##type##_block = AFTFB_new; \ | |
| 1025 current_##type##_block_index = 0; \ | |
| 1026 } \ | |
| 1027 (result) = \ | |
| 1028 &(current_##type##_block->block[current_##type##_block_index++]); \ | |
| 1029 } while (0) | |
| 1030 | |
| 1031 /* Allocate an instance of a type that is stored in blocks. | |
| 1032 TYPE is the "name" of the type, STRUCTTYPE is the corresponding | |
| 1033 structure type. */ | |
| 1034 | |
| 1035 #ifdef ERROR_CHECK_GC | |
| 1036 | |
| 1037 /* Note: if you get crashes in this function, suspect incorrect calls | |
| 1038 to free_cons() and friends. This happened once because the cons | |
| 1039 cell was not GC-protected and was getting collected before | |
| 1040 free_cons() was called. */ | |
| 1041 | |
| 454 | 1042 #define ALLOCATE_FIXED_TYPE_1(type, structtype, result) do { \ |
| 1043 if (gc_count_num_##type##_freelist > \ | |
| 1044 MINIMUM_ALLOWED_FIXED_TYPE_CELLS_##type) \ | |
| 1045 { \ | |
| 1046 result = (structtype *) type##_free_list; \ | |
| 1204 | 1047 assert (LRECORD_FREE_P (result)); \ |
| 1048 /* Before actually using the chain pointer, we complement \ | |
| 1049 all its bits; see PUT_FIXED_TYPE_ON_FREE_LIST(). */ \ | |
| 454 | 1050 type##_free_list = (Lisp_Free *) \ |
| 1051 (~ (EMACS_UINT) (type##_free_list->chain)); \ | |
| 1052 gc_count_num_##type##_freelist--; \ | |
| 1053 } \ | |
| 1054 else \ | |
| 1055 ALLOCATE_FIXED_TYPE_FROM_BLOCK (type, result); \ | |
| 1056 MARK_LRECORD_AS_NOT_FREE (result); \ | |
| 428 | 1057 } while (0) |
| 1058 | |
| 1059 #else /* !ERROR_CHECK_GC */ | |
| 1060 | |
| 454 | 1061 #define ALLOCATE_FIXED_TYPE_1(type, structtype, result) do { \ |
| 428 | 1062 if (type##_free_list) \ |
| 1063 { \ | |
| 454 | 1064 result = (structtype *) type##_free_list; \ |
| 1065 type##_free_list = type##_free_list->chain; \ | |
| 428 | 1066 } \ |
| 1067 else \ | |
| 1068 ALLOCATE_FIXED_TYPE_FROM_BLOCK (type, result); \ | |
| 454 | 1069 MARK_LRECORD_AS_NOT_FREE (result); \ |
| 428 | 1070 } while (0) |
| 1071 | |
| 1072 #endif /* !ERROR_CHECK_GC */ | |
| 1073 | |
| 454 | 1074 |
| 428 | 1075 #define ALLOCATE_FIXED_TYPE(type, structtype, result) \ |
| 1076 do \ | |
| 1077 { \ | |
| 1078 ALLOCATE_FIXED_TYPE_1 (type, structtype, result); \ | |
| 1079 INCREMENT_CONS_COUNTER (sizeof (structtype), #type); \ | |
| 1080 } while (0) | |
| 1081 | |
| 1082 #define NOSEEUM_ALLOCATE_FIXED_TYPE(type, structtype, result) \ | |
| 1083 do \ | |
| 1084 { \ | |
| 1085 ALLOCATE_FIXED_TYPE_1 (type, structtype, result); \ | |
| 1086 NOSEEUM_INCREMENT_CONS_COUNTER (sizeof (structtype), #type); \ | |
| 1087 } while (0) | |
| 1088 | |
| 454 | 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 | |
| 3263 | 1173 #endif /* NEW_GC */ |
| 1174 | |
| 1175 #ifdef NEW_GC | |
| 3017 | 1176 #define ALLOCATE_FIXED_TYPE_AND_SET_IMPL(type, lisp_type, var, lrec_ptr) \ |
| 1177 do { \ | |
| 1178 (var) = alloc_lrecord_type (lisp_type, lrec_ptr); \ | |
| 1179 } while (0) | |
| 1180 #define NOSEEUM_ALLOCATE_FIXED_TYPE_AND_SET_IMPL(type, lisp_type, var, \ | |
| 1181 lrec_ptr) \ | |
| 1182 do { \ | |
| 1183 (var) = noseeum_alloc_lrecord_type (lisp_type, lrec_ptr); \ | |
| 1184 } while (0) | |
| 3263 | 1185 #else /* not NEW_GC */ |
| 3017 | 1186 #define ALLOCATE_FIXED_TYPE_AND_SET_IMPL(type, lisp_type, var, lrec_ptr) \ |
| 1187 do \ | |
| 1188 { \ | |
| 1189 ALLOCATE_FIXED_TYPE (type, lisp_type, var); \ | |
| 1190 set_lheader_implementation (&(var)->lheader, lrec_ptr); \ | |
| 1191 } while (0) | |
| 1192 #define NOSEEUM_ALLOCATE_FIXED_TYPE_AND_SET_IMPL(type, lisp_type, var, \ | |
| 1193 lrec_ptr) \ | |
| 1194 do \ | |
| 1195 { \ | |
| 1196 NOSEEUM_ALLOCATE_FIXED_TYPE (type, lisp_type, var); \ | |
| 1197 set_lheader_implementation (&(var)->lheader, lrec_ptr); \ | |
| 1198 } while (0) | |
| 3263 | 1199 #endif /* not NEW_GC */ |
| 3017 | 1200 |
| 428 | 1201 |
| 1202 | |
| 1203 /************************************************************************/ | |
| 1204 /* Cons allocation */ | |
| 1205 /************************************************************************/ | |
| 1206 | |
| 440 | 1207 DECLARE_FIXED_TYPE_ALLOC (cons, Lisp_Cons); |
| 428 | 1208 /* conses are used and freed so often that we set this really high */ |
| 1209 /* #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_cons 20000 */ | |
| 1210 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_cons 2000 | |
| 1211 | |
| 1212 static Lisp_Object | |
| 1213 mark_cons (Lisp_Object obj) | |
| 1214 { | |
| 1215 if (NILP (XCDR (obj))) | |
| 1216 return XCAR (obj); | |
| 1217 | |
| 1218 mark_object (XCAR (obj)); | |
| 1219 return XCDR (obj); | |
| 1220 } | |
| 1221 | |
| 1222 static int | |
| 1223 cons_equal (Lisp_Object ob1, Lisp_Object ob2, int depth) | |
| 1224 { | |
| 442 | 1225 depth++; |
| 1226 while (internal_equal (XCAR (ob1), XCAR (ob2), depth)) | |
| 428 | 1227 { |
| 1228 ob1 = XCDR (ob1); | |
| 1229 ob2 = XCDR (ob2); | |
| 1230 if (! CONSP (ob1) || ! CONSP (ob2)) | |
| 442 | 1231 return internal_equal (ob1, ob2, depth); |
| 428 | 1232 } |
| 1233 return 0; | |
| 1234 } | |
| 1235 | |
| 1204 | 1236 static const struct memory_description cons_description[] = { |
| 853 | 1237 { XD_LISP_OBJECT, offsetof (Lisp_Cons, car_) }, |
| 1238 { XD_LISP_OBJECT, offsetof (Lisp_Cons, cdr_) }, | |
| 428 | 1239 { XD_END } |
| 1240 }; | |
| 1241 | |
| 934 | 1242 DEFINE_BASIC_LRECORD_IMPLEMENTATION ("cons", cons, |
| 1243 1, /*dumpable-flag*/ | |
| 1244 mark_cons, print_cons, 0, | |
| 1245 cons_equal, | |
| 1246 /* | |
| 1247 * No `hash' method needed. | |
| 1248 * internal_hash knows how to | |
| 1249 * handle conses. | |
| 1250 */ | |
| 1251 0, | |
| 1252 cons_description, | |
| 1253 Lisp_Cons); | |
| 428 | 1254 |
| 1255 DEFUN ("cons", Fcons, 2, 2, 0, /* | |
| 3355 | 1256 Create a new cons cell, give it CAR and CDR as components, and return it. |
| 1257 | |
| 1258 A cons cell is a Lisp object (an area in memory) made up of two pointers | |
| 1259 called the CAR and the CDR. Each of these pointers can point to any other | |
| 1260 Lisp object. The common Lisp data type, the list, is a specially-structured | |
| 1261 series of cons cells. | |
| 1262 | |
| 1263 The pointers are accessed from Lisp with `car' and `cdr', and mutated with | |
| 1264 `setcar' and `setcdr' respectively. For historical reasons, the aliases | |
| 1265 `rplaca' and `rplacd' (for `setcar' and `setcdr') are supported. | |
| 428 | 1266 */ |
| 1267 (car, cdr)) | |
| 1268 { | |
| 1269 /* This cannot GC. */ | |
| 1270 Lisp_Object val; | |
| 440 | 1271 Lisp_Cons *c; |
| 1272 | |
| 3017 | 1273 ALLOCATE_FIXED_TYPE_AND_SET_IMPL (cons, Lisp_Cons, c, &lrecord_cons); |
| 793 | 1274 val = wrap_cons (c); |
| 853 | 1275 XSETCAR (val, car); |
| 1276 XSETCDR (val, cdr); | |
| 428 | 1277 return val; |
| 1278 } | |
| 1279 | |
| 1280 /* This is identical to Fcons() but it used for conses that we're | |
| 1281 going to free later, and is useful when trying to track down | |
| 1282 "real" consing. */ | |
| 1283 Lisp_Object | |
| 1284 noseeum_cons (Lisp_Object car, Lisp_Object cdr) | |
| 1285 { | |
| 1286 Lisp_Object val; | |
| 440 | 1287 Lisp_Cons *c; |
| 1288 | |
| 3017 | 1289 NOSEEUM_ALLOCATE_FIXED_TYPE_AND_SET_IMPL (cons, Lisp_Cons, c, &lrecord_cons); |
| 793 | 1290 val = wrap_cons (c); |
| 428 | 1291 XCAR (val) = car; |
| 1292 XCDR (val) = cdr; | |
| 1293 return val; | |
| 1294 } | |
| 1295 | |
| 1296 DEFUN ("list", Flist, 0, MANY, 0, /* | |
|
4693
80cd90837ac5
Add argument information to remaining MANY or UNEVALLED C subrs.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3514
diff
changeset
|
1297 Return a newly created list with specified ARGS as elements. |
| 428 | 1298 Any number of arguments, even zero arguments, are allowed. |
|
4693
80cd90837ac5
Add argument information to remaining MANY or UNEVALLED C subrs.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3514
diff
changeset
|
1299 |
|
80cd90837ac5
Add argument information to remaining MANY or UNEVALLED C subrs.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3514
diff
changeset
|
1300 arguments: (&rest ARGS) |
| 428 | 1301 */ |
| 1302 (int nargs, Lisp_Object *args)) | |
| 1303 { | |
| 1304 Lisp_Object val = Qnil; | |
| 1305 Lisp_Object *argp = args + nargs; | |
| 1306 | |
| 1307 while (argp > args) | |
| 1308 val = Fcons (*--argp, val); | |
| 1309 return val; | |
| 1310 } | |
| 1311 | |
| 1312 Lisp_Object | |
| 1313 list1 (Lisp_Object obj0) | |
| 1314 { | |
| 1315 /* This cannot GC. */ | |
| 1316 return Fcons (obj0, Qnil); | |
| 1317 } | |
| 1318 | |
| 1319 Lisp_Object | |
| 1320 list2 (Lisp_Object obj0, Lisp_Object obj1) | |
| 1321 { | |
| 1322 /* This cannot GC. */ | |
| 1323 return Fcons (obj0, Fcons (obj1, Qnil)); | |
| 1324 } | |
| 1325 | |
| 1326 Lisp_Object | |
| 1327 list3 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2) | |
| 1328 { | |
| 1329 /* This cannot GC. */ | |
| 1330 return Fcons (obj0, Fcons (obj1, Fcons (obj2, Qnil))); | |
| 1331 } | |
| 1332 | |
| 1333 Lisp_Object | |
| 1334 cons3 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2) | |
| 1335 { | |
| 1336 /* This cannot GC. */ | |
| 1337 return Fcons (obj0, Fcons (obj1, obj2)); | |
| 1338 } | |
| 1339 | |
| 1340 Lisp_Object | |
| 1341 acons (Lisp_Object key, Lisp_Object value, Lisp_Object alist) | |
| 1342 { | |
| 1343 return Fcons (Fcons (key, value), alist); | |
| 1344 } | |
| 1345 | |
| 1346 Lisp_Object | |
| 1347 list4 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2, Lisp_Object obj3) | |
| 1348 { | |
| 1349 /* This cannot GC. */ | |
| 1350 return Fcons (obj0, Fcons (obj1, Fcons (obj2, Fcons (obj3, Qnil)))); | |
| 1351 } | |
| 1352 | |
| 1353 Lisp_Object | |
| 1354 list5 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2, Lisp_Object obj3, | |
| 1355 Lisp_Object obj4) | |
| 1356 { | |
| 1357 /* This cannot GC. */ | |
| 1358 return Fcons (obj0, Fcons (obj1, Fcons (obj2, Fcons (obj3, Fcons (obj4, Qnil))))); | |
| 1359 } | |
| 1360 | |
| 1361 Lisp_Object | |
| 1362 list6 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2, Lisp_Object obj3, | |
| 1363 Lisp_Object obj4, Lisp_Object obj5) | |
| 1364 { | |
| 1365 /* This cannot GC. */ | |
| 1366 return Fcons (obj0, Fcons (obj1, Fcons (obj2, Fcons (obj3, Fcons (obj4, Fcons (obj5, Qnil)))))); | |
| 1367 } | |
| 1368 | |
| 1369 DEFUN ("make-list", Fmake_list, 2, 2, 0, /* | |
| 444 | 1370 Return a new list of length LENGTH, with each element being OBJECT. |
| 428 | 1371 */ |
| 444 | 1372 (length, object)) |
| 428 | 1373 { |
| 1374 CHECK_NATNUM (length); | |
| 1375 | |
| 1376 { | |
| 1377 Lisp_Object val = Qnil; | |
| 647 | 1378 EMACS_INT size = XINT (length); |
| 428 | 1379 |
| 1380 while (size--) | |
| 444 | 1381 val = Fcons (object, val); |
| 428 | 1382 return val; |
| 1383 } | |
| 1384 } | |
| 1385 | |
| 1386 | |
| 1387 /************************************************************************/ | |
| 1388 /* Float allocation */ | |
| 1389 /************************************************************************/ | |
| 1390 | |
| 1983 | 1391 /*** With enhanced number support, these are short floats */ |
| 1392 | |
| 440 | 1393 DECLARE_FIXED_TYPE_ALLOC (float, Lisp_Float); |
| 428 | 1394 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_float 1000 |
| 1395 | |
| 1396 Lisp_Object | |
| 1397 make_float (double float_value) | |
| 1398 { | |
| 440 | 1399 Lisp_Float *f; |
| 1400 | |
| 3017 | 1401 ALLOCATE_FIXED_TYPE_AND_SET_IMPL (float, Lisp_Float, f, &lrecord_float); |
| 440 | 1402 |
| 1403 /* Avoid dump-time `uninitialized memory read' purify warnings. */ | |
| 1404 if (sizeof (struct lrecord_header) + sizeof (double) != sizeof (*f)) | |
| 3017 | 1405 zero_lrecord (f); |
| 1406 | |
| 428 | 1407 float_data (f) = float_value; |
| 793 | 1408 return wrap_float (f); |
| 428 | 1409 } |
| 1410 | |
| 1411 | |
| 1412 /************************************************************************/ | |
| 1983 | 1413 /* Enhanced number allocation */ |
| 1414 /************************************************************************/ | |
| 1415 | |
| 1416 /*** Bignum ***/ | |
| 1417 #ifdef HAVE_BIGNUM | |
| 1418 DECLARE_FIXED_TYPE_ALLOC (bignum, Lisp_Bignum); | |
| 1419 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_bignum 250 | |
| 1420 | |
| 1421 /* WARNING: This function returns a bignum even if its argument fits into a | |
| 1422 fixnum. See Fcanonicalize_number(). */ | |
| 1423 Lisp_Object | |
| 1424 make_bignum (long bignum_value) | |
| 1425 { | |
| 1426 Lisp_Bignum *b; | |
| 1427 | |
| 3017 | 1428 ALLOCATE_FIXED_TYPE_AND_SET_IMPL (bignum, Lisp_Bignum, b, &lrecord_bignum); |
| 1983 | 1429 bignum_init (bignum_data (b)); |
| 1430 bignum_set_long (bignum_data (b), bignum_value); | |
| 1431 return wrap_bignum (b); | |
| 1432 } | |
| 1433 | |
| 1434 /* WARNING: This function returns a bignum even if its argument fits into a | |
| 1435 fixnum. See Fcanonicalize_number(). */ | |
| 1436 Lisp_Object | |
| 1437 make_bignum_bg (bignum bg) | |
| 1438 { | |
| 1439 Lisp_Bignum *b; | |
| 1440 | |
| 3017 | 1441 ALLOCATE_FIXED_TYPE_AND_SET_IMPL (bignum, Lisp_Bignum, b, &lrecord_bignum); |
| 1983 | 1442 bignum_init (bignum_data (b)); |
| 1443 bignum_set (bignum_data (b), bg); | |
| 1444 return wrap_bignum (b); | |
| 1445 } | |
| 1446 #endif /* HAVE_BIGNUM */ | |
| 1447 | |
| 1448 /*** Ratio ***/ | |
| 1449 #ifdef HAVE_RATIO | |
| 1450 DECLARE_FIXED_TYPE_ALLOC (ratio, Lisp_Ratio); | |
| 1451 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_ratio 250 | |
| 1452 | |
| 1453 Lisp_Object | |
| 1454 make_ratio (long numerator, unsigned long denominator) | |
| 1455 { | |
| 1456 Lisp_Ratio *r; | |
| 1457 | |
| 3017 | 1458 ALLOCATE_FIXED_TYPE_AND_SET_IMPL (ratio, Lisp_Ratio, r, &lrecord_ratio); |
| 1983 | 1459 ratio_init (ratio_data (r)); |
| 1460 ratio_set_long_ulong (ratio_data (r), numerator, denominator); | |
| 1461 ratio_canonicalize (ratio_data (r)); | |
| 1462 return wrap_ratio (r); | |
| 1463 } | |
| 1464 | |
| 1465 Lisp_Object | |
| 1466 make_ratio_bg (bignum numerator, bignum denominator) | |
| 1467 { | |
| 1468 Lisp_Ratio *r; | |
| 1469 | |
| 3017 | 1470 ALLOCATE_FIXED_TYPE_AND_SET_IMPL (ratio, Lisp_Ratio, r, &lrecord_ratio); |
| 1983 | 1471 ratio_init (ratio_data (r)); |
| 1472 ratio_set_bignum_bignum (ratio_data (r), numerator, denominator); | |
| 1473 ratio_canonicalize (ratio_data (r)); | |
| 1474 return wrap_ratio (r); | |
| 1475 } | |
| 1476 | |
| 1477 Lisp_Object | |
| 1478 make_ratio_rt (ratio rat) | |
| 1479 { | |
| 1480 Lisp_Ratio *r; | |
| 1481 | |
| 3017 | 1482 ALLOCATE_FIXED_TYPE_AND_SET_IMPL (ratio, Lisp_Ratio, r, &lrecord_ratio); |
| 1983 | 1483 ratio_init (ratio_data (r)); |
| 1484 ratio_set (ratio_data (r), rat); | |
| 1485 return wrap_ratio (r); | |
| 1486 } | |
| 1487 #endif /* HAVE_RATIO */ | |
| 1488 | |
| 1489 /*** Bigfloat ***/ | |
| 1490 #ifdef HAVE_BIGFLOAT | |
| 1491 DECLARE_FIXED_TYPE_ALLOC (bigfloat, Lisp_Bigfloat); | |
| 1492 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_bigfloat 250 | |
| 1493 | |
| 1494 /* This function creates a bigfloat with the default precision if the | |
| 1495 PRECISION argument is zero. */ | |
| 1496 Lisp_Object | |
| 1497 make_bigfloat (double float_value, unsigned long precision) | |
| 1498 { | |
| 1499 Lisp_Bigfloat *f; | |
| 1500 | |
| 3017 | 1501 ALLOCATE_FIXED_TYPE_AND_SET_IMPL (bigfloat, Lisp_Bigfloat, f, &lrecord_bigfloat); |
| 1983 | 1502 if (precision == 0UL) |
| 1503 bigfloat_init (bigfloat_data (f)); | |
| 1504 else | |
| 1505 bigfloat_init_prec (bigfloat_data (f), precision); | |
| 1506 bigfloat_set_double (bigfloat_data (f), float_value); | |
| 1507 return wrap_bigfloat (f); | |
| 1508 } | |
| 1509 | |
| 1510 /* This function creates a bigfloat with the precision of its argument */ | |
| 1511 Lisp_Object | |
| 1512 make_bigfloat_bf (bigfloat float_value) | |
| 1513 { | |
| 1514 Lisp_Bigfloat *f; | |
| 1515 | |
| 3017 | 1516 ALLOCATE_FIXED_TYPE_AND_SET_IMPL (bigfloat, Lisp_Bigfloat, f, &lrecord_bigfloat); |
| 1983 | 1517 bigfloat_init_prec (bigfloat_data (f), bigfloat_get_prec (float_value)); |
| 1518 bigfloat_set (bigfloat_data (f), float_value); | |
| 1519 return wrap_bigfloat (f); | |
| 1520 } | |
| 1521 #endif /* HAVE_BIGFLOAT */ | |
| 1522 | |
| 1523 /************************************************************************/ | |
| 428 | 1524 /* Vector allocation */ |
| 1525 /************************************************************************/ | |
| 1526 | |
| 1527 static Lisp_Object | |
| 1528 mark_vector (Lisp_Object obj) | |
| 1529 { | |
| 1530 Lisp_Vector *ptr = XVECTOR (obj); | |
| 1531 int len = vector_length (ptr); | |
| 1532 int i; | |
| 1533 | |
| 1534 for (i = 0; i < len - 1; i++) | |
| 1535 mark_object (ptr->contents[i]); | |
| 1536 return (len > 0) ? ptr->contents[len - 1] : Qnil; | |
| 1537 } | |
| 1538 | |
| 665 | 1539 static Bytecount |
| 442 | 1540 size_vector (const void *lheader) |
| 428 | 1541 { |
| 456 | 1542 return FLEXIBLE_ARRAY_STRUCT_SIZEOF (Lisp_Vector, Lisp_Object, contents, |
| 442 | 1543 ((Lisp_Vector *) lheader)->size); |
| 428 | 1544 } |
| 1545 | |
| 1546 static int | |
| 1547 vector_equal (Lisp_Object obj1, Lisp_Object obj2, int depth) | |
| 1548 { | |
| 1549 int len = XVECTOR_LENGTH (obj1); | |
| 1550 if (len != XVECTOR_LENGTH (obj2)) | |
| 1551 return 0; | |
| 1552 | |
| 1553 { | |
| 1554 Lisp_Object *ptr1 = XVECTOR_DATA (obj1); | |
| 1555 Lisp_Object *ptr2 = XVECTOR_DATA (obj2); | |
| 1556 while (len--) | |
| 1557 if (!internal_equal (*ptr1++, *ptr2++, depth + 1)) | |
| 1558 return 0; | |
| 1559 } | |
| 1560 return 1; | |
| 1561 } | |
| 1562 | |
| 665 | 1563 static Hashcode |
| 442 | 1564 vector_hash (Lisp_Object obj, int depth) |
| 1565 { | |
| 1566 return HASH2 (XVECTOR_LENGTH (obj), | |
| 1567 internal_array_hash (XVECTOR_DATA (obj), | |
| 1568 XVECTOR_LENGTH (obj), | |
| 1569 depth + 1)); | |
| 1570 } | |
| 1571 | |
| 1204 | 1572 static const struct memory_description vector_description[] = { |
| 440 | 1573 { XD_LONG, offsetof (Lisp_Vector, size) }, |
| 1574 { XD_LISP_OBJECT_ARRAY, offsetof (Lisp_Vector, contents), XD_INDIRECT(0, 0) }, | |
| 428 | 1575 { XD_END } |
| 1576 }; | |
| 1577 | |
| 1204 | 1578 DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION ("vector", vector, |
| 1579 1, /*dumpable-flag*/ | |
| 1580 mark_vector, print_vector, 0, | |
| 1581 vector_equal, | |
| 1582 vector_hash, | |
| 1583 vector_description, | |
| 1584 size_vector, Lisp_Vector); | |
| 428 | 1585 /* #### should allocate `small' vectors from a frob-block */ |
| 1586 static Lisp_Vector * | |
| 665 | 1587 make_vector_internal (Elemcount sizei) |
| 428 | 1588 { |
| 1204 | 1589 /* no `next' field; we use lcrecords */ |
| 665 | 1590 Bytecount sizem = FLEXIBLE_ARRAY_STRUCT_SIZEOF (Lisp_Vector, Lisp_Object, |
| 1204 | 1591 contents, sizei); |
| 1592 Lisp_Vector *p = | |
| 3017 | 1593 (Lisp_Vector *) BASIC_ALLOC_LCRECORD (sizem, &lrecord_vector); |
| 428 | 1594 |
| 1595 p->size = sizei; | |
| 1596 return p; | |
| 1597 } | |
| 1598 | |
| 1599 Lisp_Object | |
| 665 | 1600 make_vector (Elemcount length, Lisp_Object object) |
| 428 | 1601 { |
| 1602 Lisp_Vector *vecp = make_vector_internal (length); | |
| 1603 Lisp_Object *p = vector_data (vecp); | |
| 1604 | |
| 1605 while (length--) | |
| 444 | 1606 *p++ = object; |
| 428 | 1607 |
| 793 | 1608 return wrap_vector (vecp); |
| 428 | 1609 } |
| 1610 | |
| 1611 DEFUN ("make-vector", Fmake_vector, 2, 2, 0, /* | |
| 444 | 1612 Return a new vector of length LENGTH, with each element being OBJECT. |
| 428 | 1613 See also the function `vector'. |
| 1614 */ | |
| 444 | 1615 (length, object)) |
| 428 | 1616 { |
| 1617 CONCHECK_NATNUM (length); | |
| 444 | 1618 return make_vector (XINT (length), object); |
| 428 | 1619 } |
| 1620 | |
| 1621 DEFUN ("vector", Fvector, 0, MANY, 0, /* | |
|
4693
80cd90837ac5
Add argument information to remaining MANY or UNEVALLED C subrs.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3514
diff
changeset
|
1622 Return a newly created vector with specified ARGS as elements. |
| 428 | 1623 Any number of arguments, even zero arguments, are allowed. |
|
4693
80cd90837ac5
Add argument information to remaining MANY or UNEVALLED C subrs.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3514
diff
changeset
|
1624 |
|
80cd90837ac5
Add argument information to remaining MANY or UNEVALLED C subrs.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3514
diff
changeset
|
1625 arguments: (&rest ARGS) |
| 428 | 1626 */ |
| 1627 (int nargs, Lisp_Object *args)) | |
| 1628 { | |
| 1629 Lisp_Vector *vecp = make_vector_internal (nargs); | |
| 1630 Lisp_Object *p = vector_data (vecp); | |
| 1631 | |
| 1632 while (nargs--) | |
| 1633 *p++ = *args++; | |
| 1634 | |
| 793 | 1635 return wrap_vector (vecp); |
| 428 | 1636 } |
| 1637 | |
| 1638 Lisp_Object | |
| 1639 vector1 (Lisp_Object obj0) | |
| 1640 { | |
| 1641 return Fvector (1, &obj0); | |
| 1642 } | |
| 1643 | |
| 1644 Lisp_Object | |
| 1645 vector2 (Lisp_Object obj0, Lisp_Object obj1) | |
| 1646 { | |
| 1647 Lisp_Object args[2]; | |
| 1648 args[0] = obj0; | |
| 1649 args[1] = obj1; | |
| 1650 return Fvector (2, args); | |
| 1651 } | |
| 1652 | |
| 1653 Lisp_Object | |
| 1654 vector3 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2) | |
| 1655 { | |
| 1656 Lisp_Object args[3]; | |
| 1657 args[0] = obj0; | |
| 1658 args[1] = obj1; | |
| 1659 args[2] = obj2; | |
| 1660 return Fvector (3, args); | |
| 1661 } | |
| 1662 | |
| 1663 #if 0 /* currently unused */ | |
| 1664 | |
| 1665 Lisp_Object | |
| 1666 vector4 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2, | |
| 1667 Lisp_Object obj3) | |
| 1668 { | |
| 1669 Lisp_Object args[4]; | |
| 1670 args[0] = obj0; | |
| 1671 args[1] = obj1; | |
| 1672 args[2] = obj2; | |
| 1673 args[3] = obj3; | |
| 1674 return Fvector (4, args); | |
| 1675 } | |
| 1676 | |
| 1677 Lisp_Object | |
| 1678 vector5 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2, | |
| 1679 Lisp_Object obj3, Lisp_Object obj4) | |
| 1680 { | |
| 1681 Lisp_Object args[5]; | |
| 1682 args[0] = obj0; | |
| 1683 args[1] = obj1; | |
| 1684 args[2] = obj2; | |
| 1685 args[3] = obj3; | |
| 1686 args[4] = obj4; | |
| 1687 return Fvector (5, args); | |
| 1688 } | |
| 1689 | |
| 1690 Lisp_Object | |
| 1691 vector6 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2, | |
| 1692 Lisp_Object obj3, Lisp_Object obj4, Lisp_Object obj5) | |
| 1693 { | |
| 1694 Lisp_Object args[6]; | |
| 1695 args[0] = obj0; | |
| 1696 args[1] = obj1; | |
| 1697 args[2] = obj2; | |
| 1698 args[3] = obj3; | |
| 1699 args[4] = obj4; | |
| 1700 args[5] = obj5; | |
| 1701 return Fvector (6, args); | |
| 1702 } | |
| 1703 | |
| 1704 Lisp_Object | |
| 1705 vector7 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2, | |
| 1706 Lisp_Object obj3, Lisp_Object obj4, Lisp_Object obj5, | |
| 1707 Lisp_Object obj6) | |
| 1708 { | |
| 1709 Lisp_Object args[7]; | |
| 1710 args[0] = obj0; | |
| 1711 args[1] = obj1; | |
| 1712 args[2] = obj2; | |
| 1713 args[3] = obj3; | |
| 1714 args[4] = obj4; | |
| 1715 args[5] = obj5; | |
| 1716 args[6] = obj6; | |
| 1717 return Fvector (7, args); | |
| 1718 } | |
| 1719 | |
| 1720 Lisp_Object | |
| 1721 vector8 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2, | |
| 1722 Lisp_Object obj3, Lisp_Object obj4, Lisp_Object obj5, | |
| 1723 Lisp_Object obj6, Lisp_Object obj7) | |
| 1724 { | |
| 1725 Lisp_Object args[8]; | |
| 1726 args[0] = obj0; | |
| 1727 args[1] = obj1; | |
| 1728 args[2] = obj2; | |
| 1729 args[3] = obj3; | |
| 1730 args[4] = obj4; | |
| 1731 args[5] = obj5; | |
| 1732 args[6] = obj6; | |
| 1733 args[7] = obj7; | |
| 1734 return Fvector (8, args); | |
| 1735 } | |
| 1736 #endif /* unused */ | |
| 1737 | |
| 1738 /************************************************************************/ | |
| 1739 /* Bit Vector allocation */ | |
| 1740 /************************************************************************/ | |
| 1741 | |
| 1742 /* #### should allocate `small' bit vectors from a frob-block */ | |
| 440 | 1743 static Lisp_Bit_Vector * |
| 665 | 1744 make_bit_vector_internal (Elemcount sizei) |
| 428 | 1745 { |
| 1204 | 1746 /* no `next' field; we use lcrecords */ |
| 665 | 1747 Elemcount num_longs = BIT_VECTOR_LONG_STORAGE (sizei); |
| 1748 Bytecount sizem = FLEXIBLE_ARRAY_STRUCT_SIZEOF (Lisp_Bit_Vector, | |
| 1204 | 1749 unsigned long, |
| 1750 bits, num_longs); | |
| 1751 Lisp_Bit_Vector *p = (Lisp_Bit_Vector *) | |
| 3017 | 1752 BASIC_ALLOC_LCRECORD (sizem, &lrecord_bit_vector); |
| 428 | 1753 |
| 1754 bit_vector_length (p) = sizei; | |
| 1755 return p; | |
| 1756 } | |
| 1757 | |
| 1758 Lisp_Object | |
| 665 | 1759 make_bit_vector (Elemcount length, Lisp_Object bit) |
| 428 | 1760 { |
| 440 | 1761 Lisp_Bit_Vector *p = make_bit_vector_internal (length); |
| 665 | 1762 Elemcount num_longs = BIT_VECTOR_LONG_STORAGE (length); |
| 428 | 1763 |
| 444 | 1764 CHECK_BIT (bit); |
| 1765 | |
| 1766 if (ZEROP (bit)) | |
| 428 | 1767 memset (p->bits, 0, num_longs * sizeof (long)); |
| 1768 else | |
| 1769 { | |
| 665 | 1770 Elemcount bits_in_last = length & (LONGBITS_POWER_OF_2 - 1); |
| 428 | 1771 memset (p->bits, ~0, num_longs * sizeof (long)); |
| 1772 /* But we have to make sure that the unused bits in the | |
| 1773 last long are 0, so that equal/hash is easy. */ | |
| 1774 if (bits_in_last) | |
| 1775 p->bits[num_longs - 1] &= (1 << bits_in_last) - 1; | |
| 1776 } | |
| 1777 | |
| 793 | 1778 return wrap_bit_vector (p); |
| 428 | 1779 } |
| 1780 | |
| 1781 Lisp_Object | |
| 665 | 1782 make_bit_vector_from_byte_vector (unsigned char *bytevec, Elemcount length) |
| 428 | 1783 { |
| 665 | 1784 Elemcount i; |
| 428 | 1785 Lisp_Bit_Vector *p = make_bit_vector_internal (length); |
| 1786 | |
| 1787 for (i = 0; i < length; i++) | |
| 1788 set_bit_vector_bit (p, i, bytevec[i]); | |
| 1789 | |
| 793 | 1790 return wrap_bit_vector (p); |
| 428 | 1791 } |
| 1792 | |
| 1793 DEFUN ("make-bit-vector", Fmake_bit_vector, 2, 2, 0, /* | |
| 444 | 1794 Return a new bit vector of length LENGTH. with each bit set to BIT. |
| 1795 BIT must be one of the integers 0 or 1. See also the function `bit-vector'. | |
| 428 | 1796 */ |
| 444 | 1797 (length, bit)) |
| 428 | 1798 { |
| 1799 CONCHECK_NATNUM (length); | |
| 1800 | |
| 444 | 1801 return make_bit_vector (XINT (length), bit); |
| 428 | 1802 } |
| 1803 | |
| 1804 DEFUN ("bit-vector", Fbit_vector, 0, MANY, 0, /* | |
|
4693
80cd90837ac5
Add argument information to remaining MANY or UNEVALLED C subrs.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3514
diff
changeset
|
1805 Return a newly created bit vector with specified ARGS as elements. |
| 428 | 1806 Any number of arguments, even zero arguments, are allowed. |
| 444 | 1807 Each argument must be one of the integers 0 or 1. |
|
4693
80cd90837ac5
Add argument information to remaining MANY or UNEVALLED C subrs.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3514
diff
changeset
|
1808 |
|
80cd90837ac5
Add argument information to remaining MANY or UNEVALLED C subrs.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3514
diff
changeset
|
1809 arguments: (&rest ARGS) |
| 428 | 1810 */ |
| 1811 (int nargs, Lisp_Object *args)) | |
| 1812 { | |
| 1813 int i; | |
| 1814 Lisp_Bit_Vector *p = make_bit_vector_internal (nargs); | |
| 1815 | |
| 1816 for (i = 0; i < nargs; i++) | |
| 1817 { | |
| 1818 CHECK_BIT (args[i]); | |
| 1819 set_bit_vector_bit (p, i, !ZEROP (args[i])); | |
| 1820 } | |
| 1821 | |
| 793 | 1822 return wrap_bit_vector (p); |
| 428 | 1823 } |
| 1824 | |
| 1825 | |
| 1826 /************************************************************************/ | |
| 1827 /* Compiled-function allocation */ | |
| 1828 /************************************************************************/ | |
| 1829 | |
| 1830 DECLARE_FIXED_TYPE_ALLOC (compiled_function, Lisp_Compiled_Function); | |
| 1831 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_compiled_function 1000 | |
| 1832 | |
| 1833 static Lisp_Object | |
| 1834 make_compiled_function (void) | |
| 1835 { | |
| 1836 Lisp_Compiled_Function *f; | |
| 1837 | |
| 3017 | 1838 ALLOCATE_FIXED_TYPE_AND_SET_IMPL (compiled_function, Lisp_Compiled_Function, |
| 1839 f, &lrecord_compiled_function); | |
| 428 | 1840 |
| 1841 f->stack_depth = 0; | |
| 1842 f->specpdl_depth = 0; | |
| 1843 f->flags.documentationp = 0; | |
| 1844 f->flags.interactivep = 0; | |
| 1845 f->flags.domainp = 0; /* I18N3 */ | |
| 1846 f->instructions = Qzero; | |
| 1847 f->constants = Qzero; | |
| 1848 f->arglist = Qnil; | |
| 3092 | 1849 #ifdef NEW_GC |
| 1850 f->arguments = Qnil; | |
| 1851 #else /* not NEW_GC */ | |
| 1739 | 1852 f->args = NULL; |
| 3092 | 1853 #endif /* not NEW_GC */ |
| 1739 | 1854 f->max_args = f->min_args = f->args_in_array = 0; |
| 428 | 1855 f->doc_and_interactive = Qnil; |
| 1856 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK | |
| 1857 f->annotated = Qnil; | |
| 1858 #endif | |
| 793 | 1859 return wrap_compiled_function (f); |
| 428 | 1860 } |
| 1861 | |
| 1862 DEFUN ("make-byte-code", Fmake_byte_code, 4, MANY, 0, /* | |
| 1863 Return a new compiled-function object. | |
| 1864 Note that, unlike all other emacs-lisp functions, calling this with five | |
| 1865 arguments is NOT the same as calling it with six arguments, the last of | |
| 1866 which is nil. If the INTERACTIVE arg is specified as nil, then that means | |
| 1867 that this function was defined with `(interactive)'. If the arg is not | |
| 1868 specified, then that means the function is not interactive. | |
| 1869 This is terrible behavior which is retained for compatibility with old | |
| 1870 `.elc' files which expect these semantics. | |
|
4693
80cd90837ac5
Add argument information to remaining MANY or UNEVALLED C subrs.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3514
diff
changeset
|
1871 |
|
80cd90837ac5
Add argument information to remaining MANY or UNEVALLED C subrs.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3514
diff
changeset
|
1872 arguments: (ARGLIST INSTRUCTIONS CONSTANTS STACK-DEPTH &optional DOC-STRING INTERACTIVE) |
| 428 | 1873 */ |
| 1874 (int nargs, Lisp_Object *args)) | |
| 1875 { | |
| 1876 /* In a non-insane world this function would have this arglist... | |
| 1877 (arglist instructions constants stack_depth &optional doc_string interactive) | |
| 1878 */ | |
| 1879 Lisp_Object fun = make_compiled_function (); | |
| 1880 Lisp_Compiled_Function *f = XCOMPILED_FUNCTION (fun); | |
| 1881 | |
| 1882 Lisp_Object arglist = args[0]; | |
| 1883 Lisp_Object instructions = args[1]; | |
| 1884 Lisp_Object constants = args[2]; | |
| 1885 Lisp_Object stack_depth = args[3]; | |
| 1886 Lisp_Object doc_string = (nargs > 4) ? args[4] : Qnil; | |
| 1887 Lisp_Object interactive = (nargs > 5) ? args[5] : Qunbound; | |
| 1888 | |
| 1889 if (nargs < 4 || nargs > 6) | |
| 1890 return Fsignal (Qwrong_number_of_arguments, | |
| 1891 list2 (intern ("make-byte-code"), make_int (nargs))); | |
| 1892 | |
| 1893 /* Check for valid formal parameter list now, to allow us to use | |
| 1894 SPECBIND_FAST_UNSAFE() later in funcall_compiled_function(). */ | |
| 1895 { | |
| 814 | 1896 EXTERNAL_LIST_LOOP_2 (symbol, arglist) |
| 428 | 1897 { |
| 1898 CHECK_SYMBOL (symbol); | |
| 1899 if (EQ (symbol, Qt) || | |
| 1900 EQ (symbol, Qnil) || | |
| 1901 SYMBOL_IS_KEYWORD (symbol)) | |
| 563 | 1902 invalid_constant_2 |
| 428 | 1903 ("Invalid constant symbol in formal parameter list", |
| 1904 symbol, arglist); | |
| 1905 } | |
| 1906 } | |
| 1907 f->arglist = arglist; | |
| 1908 | |
| 1909 /* `instructions' is a string or a cons (string . int) for a | |
| 1910 lazy-loaded function. */ | |
| 1911 if (CONSP (instructions)) | |
| 1912 { | |
| 1913 CHECK_STRING (XCAR (instructions)); | |
| 1914 CHECK_INT (XCDR (instructions)); | |
| 1915 } | |
| 1916 else | |
| 1917 { | |
| 1918 CHECK_STRING (instructions); | |
| 1919 } | |
| 1920 f->instructions = instructions; | |
| 1921 | |
| 1922 if (!NILP (constants)) | |
| 1923 CHECK_VECTOR (constants); | |
| 1924 f->constants = constants; | |
| 1925 | |
| 1926 CHECK_NATNUM (stack_depth); | |
| 442 | 1927 f->stack_depth = (unsigned short) XINT (stack_depth); |
| 428 | 1928 |
| 1929 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK | |
| 1930 if (!NILP (Vcurrent_compiled_function_annotation)) | |
| 1931 f->annotated = Fcopy (Vcurrent_compiled_function_annotation); | |
| 1932 else if (!NILP (Vload_file_name_internal_the_purecopy)) | |
| 1933 f->annotated = Vload_file_name_internal_the_purecopy; | |
| 1934 else if (!NILP (Vload_file_name_internal)) | |
| 1935 { | |
| 1936 struct gcpro gcpro1; | |
| 1937 GCPRO1 (fun); /* don't let fun get reaped */ | |
| 1938 Vload_file_name_internal_the_purecopy = | |
| 1939 Ffile_name_nondirectory (Vload_file_name_internal); | |
| 1940 f->annotated = Vload_file_name_internal_the_purecopy; | |
| 1941 UNGCPRO; | |
| 1942 } | |
| 1943 #endif /* COMPILED_FUNCTION_ANNOTATION_HACK */ | |
| 1944 | |
| 1945 /* doc_string may be nil, string, int, or a cons (string . int). | |
| 1946 interactive may be list or string (or unbound). */ | |
| 1947 f->doc_and_interactive = Qunbound; | |
| 1948 #ifdef I18N3 | |
| 1949 if ((f->flags.domainp = !NILP (Vfile_domain)) != 0) | |
| 1950 f->doc_and_interactive = Vfile_domain; | |
| 1951 #endif | |
| 1952 if ((f->flags.interactivep = !UNBOUNDP (interactive)) != 0) | |
| 1953 { | |
| 1954 f->doc_and_interactive | |
| 1955 = (UNBOUNDP (f->doc_and_interactive) ? interactive : | |
| 1956 Fcons (interactive, f->doc_and_interactive)); | |
| 1957 } | |
| 1958 if ((f->flags.documentationp = !NILP (doc_string)) != 0) | |
| 1959 { | |
| 1960 f->doc_and_interactive | |
| 1961 = (UNBOUNDP (f->doc_and_interactive) ? doc_string : | |
| 1962 Fcons (doc_string, f->doc_and_interactive)); | |
| 1963 } | |
| 1964 if (UNBOUNDP (f->doc_and_interactive)) | |
| 1965 f->doc_and_interactive = Qnil; | |
| 1966 | |
| 1967 return fun; | |
| 1968 } | |
| 1969 | |
| 1970 | |
| 1971 /************************************************************************/ | |
| 1972 /* Symbol allocation */ | |
| 1973 /************************************************************************/ | |
| 1974 | |
| 440 | 1975 DECLARE_FIXED_TYPE_ALLOC (symbol, Lisp_Symbol); |
| 428 | 1976 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_symbol 1000 |
| 1977 | |
| 1978 DEFUN ("make-symbol", Fmake_symbol, 1, 1, 0, /* | |
| 1979 Return a newly allocated uninterned symbol whose name is NAME. | |
| 1980 Its value and function definition are void, and its property list is nil. | |
| 1981 */ | |
| 1982 (name)) | |
| 1983 { | |
| 440 | 1984 Lisp_Symbol *p; |
| 428 | 1985 |
| 1986 CHECK_STRING (name); | |
| 1987 | |
| 3017 | 1988 ALLOCATE_FIXED_TYPE_AND_SET_IMPL (symbol, Lisp_Symbol, p, &lrecord_symbol); |
| 793 | 1989 p->name = name; |
| 428 | 1990 p->plist = Qnil; |
| 1991 p->value = Qunbound; | |
| 1992 p->function = Qunbound; | |
| 1993 symbol_next (p) = 0; | |
| 793 | 1994 return wrap_symbol (p); |
| 428 | 1995 } |
| 1996 | |
| 1997 | |
| 1998 /************************************************************************/ | |
| 1999 /* Extent allocation */ | |
| 2000 /************************************************************************/ | |
| 2001 | |
| 2002 DECLARE_FIXED_TYPE_ALLOC (extent, struct extent); | |
| 2003 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_extent 1000 | |
| 2004 | |
| 2005 struct extent * | |
| 2006 allocate_extent (void) | |
| 2007 { | |
| 2008 struct extent *e; | |
| 2009 | |
| 3017 | 2010 ALLOCATE_FIXED_TYPE_AND_SET_IMPL (extent, struct extent, e, &lrecord_extent); |
| 428 | 2011 extent_object (e) = Qnil; |
| 2012 set_extent_start (e, -1); | |
| 2013 set_extent_end (e, -1); | |
| 2014 e->plist = Qnil; | |
| 2015 | |
| 2016 xzero (e->flags); | |
| 2017 | |
| 2018 extent_face (e) = Qnil; | |
| 2019 e->flags.end_open = 1; /* default is for endpoints to behave like markers */ | |
| 2020 e->flags.detachable = 1; | |
| 2021 | |
| 2022 return e; | |
| 2023 } | |
| 2024 | |
| 2025 | |
| 2026 /************************************************************************/ | |
| 2027 /* Event allocation */ | |
| 2028 /************************************************************************/ | |
| 2029 | |
| 440 | 2030 DECLARE_FIXED_TYPE_ALLOC (event, Lisp_Event); |
| 428 | 2031 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_event 1000 |
| 2032 | |
| 2033 Lisp_Object | |
| 2034 allocate_event (void) | |
| 2035 { | |
| 440 | 2036 Lisp_Event *e; |
| 2037 | |
| 3017 | 2038 ALLOCATE_FIXED_TYPE_AND_SET_IMPL (event, Lisp_Event, e, &lrecord_event); |
| 428 | 2039 |
| 793 | 2040 return wrap_event (e); |
| 428 | 2041 } |
| 2042 | |
| 1204 | 2043 #ifdef EVENT_DATA_AS_OBJECTS |
| 934 | 2044 DECLARE_FIXED_TYPE_ALLOC (key_data, Lisp_Key_Data); |
| 2045 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_key_data 1000 | |
| 2046 | |
| 2047 Lisp_Object | |
| 1204 | 2048 make_key_data (void) |
| 934 | 2049 { |
| 2050 Lisp_Key_Data *d; | |
| 2051 | |
| 3017 | 2052 ALLOCATE_FIXED_TYPE_AND_SET_IMPL (key_data, Lisp_Key_Data, d, |
| 2053 &lrecord_key_data); | |
| 2054 zero_lrecord (d); | |
| 1204 | 2055 d->keysym = Qnil; |
| 2056 | |
| 2057 return wrap_key_data (d); | |
| 934 | 2058 } |
| 2059 | |
| 2060 DECLARE_FIXED_TYPE_ALLOC (button_data, Lisp_Button_Data); | |
| 2061 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_button_data 1000 | |
| 2062 | |
| 2063 Lisp_Object | |
| 1204 | 2064 make_button_data (void) |
| 934 | 2065 { |
| 2066 Lisp_Button_Data *d; | |
| 2067 | |
| 3017 | 2068 ALLOCATE_FIXED_TYPE_AND_SET_IMPL (button_data, Lisp_Button_Data, d, &lrecord_button_data); |
| 2069 zero_lrecord (d); | |
| 1204 | 2070 return wrap_button_data (d); |
| 934 | 2071 } |
| 2072 | |
| 2073 DECLARE_FIXED_TYPE_ALLOC (motion_data, Lisp_Motion_Data); | |
| 2074 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_motion_data 1000 | |
| 2075 | |
| 2076 Lisp_Object | |
| 1204 | 2077 make_motion_data (void) |
| 934 | 2078 { |
| 2079 Lisp_Motion_Data *d; | |
| 2080 | |
| 3017 | 2081 ALLOCATE_FIXED_TYPE_AND_SET_IMPL (motion_data, Lisp_Motion_Data, d, &lrecord_motion_data); |
| 2082 zero_lrecord (d); | |
| 934 | 2083 |
| 1204 | 2084 return wrap_motion_data (d); |
| 934 | 2085 } |
| 2086 | |
| 2087 DECLARE_FIXED_TYPE_ALLOC (process_data, Lisp_Process_Data); | |
| 2088 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_process_data 1000 | |
| 2089 | |
| 2090 Lisp_Object | |
| 1204 | 2091 make_process_data (void) |
| 934 | 2092 { |
| 2093 Lisp_Process_Data *d; | |
| 2094 | |
| 3017 | 2095 ALLOCATE_FIXED_TYPE_AND_SET_IMPL (process_data, Lisp_Process_Data, d, &lrecord_process_data); |
| 2096 zero_lrecord (d); | |
| 1204 | 2097 d->process = Qnil; |
| 2098 | |
| 2099 return wrap_process_data (d); | |
| 934 | 2100 } |
| 2101 | |
| 2102 DECLARE_FIXED_TYPE_ALLOC (timeout_data, Lisp_Timeout_Data); | |
| 2103 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_timeout_data 1000 | |
| 2104 | |
| 2105 Lisp_Object | |
| 1204 | 2106 make_timeout_data (void) |
| 934 | 2107 { |
| 2108 Lisp_Timeout_Data *d; | |
| 2109 | |
| 3017 | 2110 ALLOCATE_FIXED_TYPE_AND_SET_IMPL (timeout_data, Lisp_Timeout_Data, d, &lrecord_timeout_data); |
| 2111 zero_lrecord (d); | |
| 1204 | 2112 d->function = Qnil; |
| 2113 d->object = Qnil; | |
| 2114 | |
| 2115 return wrap_timeout_data (d); | |
| 934 | 2116 } |
| 2117 | |
| 2118 DECLARE_FIXED_TYPE_ALLOC (magic_data, Lisp_Magic_Data); | |
| 2119 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_magic_data 1000 | |
| 2120 | |
| 2121 Lisp_Object | |
| 1204 | 2122 make_magic_data (void) |
| 934 | 2123 { |
| 2124 Lisp_Magic_Data *d; | |
| 2125 | |
| 3017 | 2126 ALLOCATE_FIXED_TYPE_AND_SET_IMPL (magic_data, Lisp_Magic_Data, d, &lrecord_magic_data); |
| 2127 zero_lrecord (d); | |
| 934 | 2128 |
| 1204 | 2129 return wrap_magic_data (d); |
| 934 | 2130 } |
| 2131 | |
| 2132 DECLARE_FIXED_TYPE_ALLOC (magic_eval_data, Lisp_Magic_Eval_Data); | |
| 2133 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_magic_eval_data 1000 | |
| 2134 | |
| 2135 Lisp_Object | |
| 1204 | 2136 make_magic_eval_data (void) |
| 934 | 2137 { |
| 2138 Lisp_Magic_Eval_Data *d; | |
| 2139 | |
| 3017 | 2140 ALLOCATE_FIXED_TYPE_AND_SET_IMPL (magic_eval_data, Lisp_Magic_Eval_Data, d, &lrecord_magic_eval_data); |
| 2141 zero_lrecord (d); | |
| 1204 | 2142 d->object = Qnil; |
| 2143 | |
| 2144 return wrap_magic_eval_data (d); | |
| 934 | 2145 } |
| 2146 | |
| 2147 DECLARE_FIXED_TYPE_ALLOC (eval_data, Lisp_Eval_Data); | |
| 2148 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_eval_data 1000 | |
| 2149 | |
| 2150 Lisp_Object | |
| 1204 | 2151 make_eval_data (void) |
| 934 | 2152 { |
| 2153 Lisp_Eval_Data *d; | |
| 2154 | |
| 3017 | 2155 ALLOCATE_FIXED_TYPE_AND_SET_IMPL (eval_data, Lisp_Eval_Data, d, &lrecord_eval_data); |
| 2156 zero_lrecord (d); | |
| 1204 | 2157 d->function = Qnil; |
| 2158 d->object = Qnil; | |
| 2159 | |
| 2160 return wrap_eval_data (d); | |
| 934 | 2161 } |
| 2162 | |
| 2163 DECLARE_FIXED_TYPE_ALLOC (misc_user_data, Lisp_Misc_User_Data); | |
| 2164 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_misc_user_data 1000 | |
| 2165 | |
| 2166 Lisp_Object | |
| 1204 | 2167 make_misc_user_data (void) |
| 934 | 2168 { |
| 2169 Lisp_Misc_User_Data *d; | |
| 2170 | |
| 3017 | 2171 ALLOCATE_FIXED_TYPE_AND_SET_IMPL (misc_user_data, Lisp_Misc_User_Data, d, &lrecord_misc_user_data); |
| 2172 zero_lrecord (d); | |
| 1204 | 2173 d->function = Qnil; |
| 2174 d->object = Qnil; | |
| 2175 | |
| 2176 return wrap_misc_user_data (d); | |
| 934 | 2177 } |
| 1204 | 2178 |
| 2179 #endif /* EVENT_DATA_AS_OBJECTS */ | |
| 428 | 2180 |
| 2181 /************************************************************************/ | |
| 2182 /* Marker allocation */ | |
| 2183 /************************************************************************/ | |
| 2184 | |
| 440 | 2185 DECLARE_FIXED_TYPE_ALLOC (marker, Lisp_Marker); |
| 428 | 2186 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_marker 1000 |
| 2187 | |
| 2188 DEFUN ("make-marker", Fmake_marker, 0, 0, 0, /* | |
| 2189 Return a new marker which does not point at any place. | |
| 2190 */ | |
| 2191 ()) | |
| 2192 { | |
| 440 | 2193 Lisp_Marker *p; |
| 2194 | |
| 3017 | 2195 ALLOCATE_FIXED_TYPE_AND_SET_IMPL (marker, Lisp_Marker, p, &lrecord_marker); |
| 428 | 2196 p->buffer = 0; |
| 665 | 2197 p->membpos = 0; |
| 428 | 2198 marker_next (p) = 0; |
| 2199 marker_prev (p) = 0; | |
| 2200 p->insertion_type = 0; | |
| 793 | 2201 return wrap_marker (p); |
| 428 | 2202 } |
| 2203 | |
| 2204 Lisp_Object | |
| 2205 noseeum_make_marker (void) | |
| 2206 { | |
| 440 | 2207 Lisp_Marker *p; |
| 2208 | |
| 3017 | 2209 NOSEEUM_ALLOCATE_FIXED_TYPE_AND_SET_IMPL (marker, Lisp_Marker, p, |
| 2210 &lrecord_marker); | |
| 428 | 2211 p->buffer = 0; |
| 665 | 2212 p->membpos = 0; |
| 428 | 2213 marker_next (p) = 0; |
| 2214 marker_prev (p) = 0; | |
| 2215 p->insertion_type = 0; | |
| 793 | 2216 return wrap_marker (p); |
| 428 | 2217 } |
| 2218 | |
| 2219 | |
| 2220 /************************************************************************/ | |
| 2221 /* String allocation */ | |
| 2222 /************************************************************************/ | |
| 2223 | |
| 2224 /* The data for "short" strings generally resides inside of structs of type | |
| 2225 string_chars_block. The Lisp_String structure is allocated just like any | |
| 1204 | 2226 other basic lrecord, and these are freelisted when they get garbage |
| 2227 collected. The data for short strings get compacted, but the data for | |
| 2228 large strings do not. | |
| 428 | 2229 |
| 2230 Previously Lisp_String structures were relocated, but this caused a lot | |
| 2231 of bus-errors because the C code didn't include enough GCPRO's for | |
| 2232 strings (since EVERY REFERENCE to a short string needed to be GCPRO'd so | |
| 2233 that the reference would get relocated). | |
| 2234 | |
| 2235 This new method makes things somewhat bigger, but it is MUCH safer. */ | |
| 2236 | |
| 438 | 2237 DECLARE_FIXED_TYPE_ALLOC (string, Lisp_String); |
| 428 | 2238 /* strings are used and freed quite often */ |
| 2239 /* #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_string 10000 */ | |
| 2240 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_string 1000 | |
| 2241 | |
| 2242 static Lisp_Object | |
| 2243 mark_string (Lisp_Object obj) | |
| 2244 { | |
| 793 | 2245 if (CONSP (XSTRING_PLIST (obj)) && EXTENT_INFOP (XCAR (XSTRING_PLIST (obj)))) |
| 2246 flush_cached_extent_info (XCAR (XSTRING_PLIST (obj))); | |
| 2247 return XSTRING_PLIST (obj); | |
| 428 | 2248 } |
| 2249 | |
| 2250 static int | |
| 2286 | 2251 string_equal (Lisp_Object obj1, Lisp_Object obj2, int UNUSED (depth)) |
| 428 | 2252 { |
| 2253 Bytecount len; | |
| 2254 return (((len = XSTRING_LENGTH (obj1)) == XSTRING_LENGTH (obj2)) && | |
| 2255 !memcmp (XSTRING_DATA (obj1), XSTRING_DATA (obj2), len)); | |
| 2256 } | |
| 2257 | |
| 1204 | 2258 static const struct memory_description string_description[] = { |
| 3092 | 2259 #ifdef NEW_GC |
| 2260 { XD_LISP_OBJECT, offsetof (Lisp_String, data_object) }, | |
| 2261 #else /* not NEW_GC */ | |
| 793 | 2262 { XD_BYTECOUNT, offsetof (Lisp_String, size_) }, |
| 2263 { XD_OPAQUE_DATA_PTR, offsetof (Lisp_String, data_), XD_INDIRECT(0, 1) }, | |
| 3092 | 2264 #endif /* not NEW_GC */ |
| 440 | 2265 { XD_LISP_OBJECT, offsetof (Lisp_String, plist) }, |
| 428 | 2266 { XD_END } |
| 2267 }; | |
| 2268 | |
| 442 | 2269 /* We store the string's extent info as the first element of the string's |
| 2270 property list; and the string's MODIFF as the first or second element | |
| 2271 of the string's property list (depending on whether the extent info | |
| 2272 is present), but only if the string has been modified. This is ugly | |
| 2273 but it reduces the memory allocated for the string in the vast | |
| 2274 majority of cases, where the string is never modified and has no | |
| 2275 extent info. | |
| 2276 | |
| 2277 #### This means you can't use an int as a key in a string's plist. */ | |
| 2278 | |
| 2279 static Lisp_Object * | |
| 2280 string_plist_ptr (Lisp_Object string) | |
| 2281 { | |
| 793 | 2282 Lisp_Object *ptr = &XSTRING_PLIST (string); |
| 442 | 2283 |
| 2284 if (CONSP (*ptr) && EXTENT_INFOP (XCAR (*ptr))) | |
| 2285 ptr = &XCDR (*ptr); | |
| 2286 if (CONSP (*ptr) && INTP (XCAR (*ptr))) | |
| 2287 ptr = &XCDR (*ptr); | |
| 2288 return ptr; | |
| 2289 } | |
| 2290 | |
| 2291 static Lisp_Object | |
| 2292 string_getprop (Lisp_Object string, Lisp_Object property) | |
| 2293 { | |
| 2294 return external_plist_get (string_plist_ptr (string), property, 0, ERROR_ME); | |
| 2295 } | |
| 2296 | |
| 2297 static int | |
| 2298 string_putprop (Lisp_Object string, Lisp_Object property, Lisp_Object value) | |
| 2299 { | |
| 2300 external_plist_put (string_plist_ptr (string), property, value, 0, ERROR_ME); | |
| 2301 return 1; | |
| 2302 } | |
| 2303 | |
| 2304 static int | |
| 2305 string_remprop (Lisp_Object string, Lisp_Object property) | |
| 2306 { | |
| 2307 return external_remprop (string_plist_ptr (string), property, 0, ERROR_ME); | |
| 2308 } | |
| 2309 | |
| 2310 static Lisp_Object | |
| 2311 string_plist (Lisp_Object string) | |
| 2312 { | |
| 2313 return *string_plist_ptr (string); | |
| 2314 } | |
| 2315 | |
| 3263 | 2316 #ifndef NEW_GC |
| 442 | 2317 /* No `finalize', or `hash' methods. |
| 2318 internal_hash() already knows how to hash strings and finalization | |
| 2319 is done with the ADDITIONAL_FREE_string macro, which is the | |
| 2320 standard way to do finalization when using | |
| 2321 SWEEP_FIXED_TYPE_BLOCK(). */ | |
| 2720 | 2322 |
| 934 | 2323 DEFINE_BASIC_LRECORD_IMPLEMENTATION_WITH_PROPS ("string", string, |
| 2324 1, /*dumpable-flag*/ | |
| 2325 mark_string, print_string, | |
| 2326 0, string_equal, 0, | |
| 2327 string_description, | |
| 2328 string_getprop, | |
| 2329 string_putprop, | |
| 2330 string_remprop, | |
| 2331 string_plist, | |
| 2332 Lisp_String); | |
| 3263 | 2333 #endif /* not NEW_GC */ |
| 2720 | 2334 |
| 3092 | 2335 #ifdef NEW_GC |
| 2336 #define STRING_FULLSIZE(size) \ | |
| 2337 ALIGN_SIZE (FLEXIBLE_ARRAY_STRUCT_SIZEOF (Lisp_String_Direct_Data, Lisp_Object, data, (size) + 1), sizeof (Lisp_Object *)); | |
| 2338 #else /* not NEW_GC */ | |
| 428 | 2339 /* String blocks contain this many useful bytes. */ |
| 2340 #define STRING_CHARS_BLOCK_SIZE \ | |
| 814 | 2341 ((Bytecount) (8192 - MALLOC_OVERHEAD - \ |
| 2342 ((2 * sizeof (struct string_chars_block *)) \ | |
| 2343 + sizeof (EMACS_INT)))) | |
| 428 | 2344 /* Block header for small strings. */ |
| 2345 struct string_chars_block | |
| 2346 { | |
| 2347 EMACS_INT pos; | |
| 2348 struct string_chars_block *next; | |
| 2349 struct string_chars_block *prev; | |
| 2350 /* Contents of string_chars_block->string_chars are interleaved | |
| 2351 string_chars structures (see below) and the actual string data */ | |
| 2352 unsigned char string_chars[STRING_CHARS_BLOCK_SIZE]; | |
| 2353 }; | |
| 2354 | |
| 2355 static struct string_chars_block *first_string_chars_block; | |
| 2356 static struct string_chars_block *current_string_chars_block; | |
| 2357 | |
| 2358 /* If SIZE is the length of a string, this returns how many bytes | |
| 2359 * the string occupies in string_chars_block->string_chars | |
| 2360 * (including alignment padding). | |
| 2361 */ | |
| 438 | 2362 #define STRING_FULLSIZE(size) \ |
| 826 | 2363 ALIGN_FOR_TYPE (((size) + 1 + sizeof (Lisp_String *)), Lisp_String *) |
| 428 | 2364 |
| 2365 #define BIG_STRING_FULLSIZE_P(fullsize) ((fullsize) >= STRING_CHARS_BLOCK_SIZE) | |
| 2366 #define BIG_STRING_SIZE_P(size) (BIG_STRING_FULLSIZE_P (STRING_FULLSIZE(size))) | |
| 2367 | |
| 454 | 2368 #define STRING_CHARS_FREE_P(ptr) ((ptr)->string == NULL) |
| 2369 #define MARK_STRING_CHARS_AS_FREE(ptr) ((void) ((ptr)->string = NULL)) | |
| 3092 | 2370 #endif /* not NEW_GC */ |
| 454 | 2371 |
| 3263 | 2372 #ifdef NEW_GC |
| 3092 | 2373 DEFINE_LRECORD_IMPLEMENTATION_WITH_PROPS ("string", string, |
| 2374 1, /*dumpable-flag*/ | |
| 2375 mark_string, print_string, | |
| 2376 0, | |
| 2377 string_equal, 0, | |
| 2378 string_description, | |
| 2379 string_getprop, | |
| 2380 string_putprop, | |
| 2381 string_remprop, | |
| 2382 string_plist, | |
| 2383 Lisp_String); | |
| 2384 | |
| 2385 | |
| 2386 static const struct memory_description string_direct_data_description[] = { | |
| 3514 | 2387 { XD_BYTECOUNT, offsetof (Lisp_String_Direct_Data, size) }, |
| 3092 | 2388 { XD_END } |
| 2389 }; | |
| 2390 | |
| 2391 static Bytecount | |
| 2392 size_string_direct_data (const void *lheader) | |
| 2393 { | |
| 2394 return STRING_FULLSIZE (((Lisp_String_Direct_Data *) lheader)->size); | |
| 2395 } | |
| 2396 | |
| 2397 | |
| 2398 DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION ("string-direct-data", | |
| 2399 string_direct_data, | |
| 2400 1, /*dumpable-flag*/ | |
| 2401 0, 0, 0, 0, 0, | |
| 2402 string_direct_data_description, | |
| 2403 size_string_direct_data, | |
| 2404 Lisp_String_Direct_Data); | |
| 2405 | |
| 2406 | |
| 2407 static const struct memory_description string_indirect_data_description[] = { | |
| 2408 { XD_BYTECOUNT, offsetof (Lisp_String_Indirect_Data, size) }, | |
| 2409 { XD_OPAQUE_DATA_PTR, offsetof (Lisp_String_Indirect_Data, data), | |
| 2410 XD_INDIRECT(0, 1) }, | |
| 2411 { XD_END } | |
| 2412 }; | |
| 2413 | |
| 2414 DEFINE_LRECORD_IMPLEMENTATION ("string-indirect-data", | |
| 2415 string_indirect_data, | |
| 2416 1, /*dumpable-flag*/ | |
| 2417 0, 0, 0, 0, 0, | |
| 2418 string_indirect_data_description, | |
| 2419 Lisp_String_Indirect_Data); | |
| 2420 #endif /* NEW_GC */ | |
| 2720 | 2421 |
| 3092 | 2422 #ifndef NEW_GC |
| 428 | 2423 struct string_chars |
| 2424 { | |
| 438 | 2425 Lisp_String *string; |
| 428 | 2426 unsigned char chars[1]; |
| 2427 }; | |
| 2428 | |
| 2429 struct unused_string_chars | |
| 2430 { | |
| 438 | 2431 Lisp_String *string; |
| 428 | 2432 EMACS_INT fullsize; |
| 2433 }; | |
| 2434 | |
| 2435 static void | |
| 2436 init_string_chars_alloc (void) | |
| 2437 { | |
| 2438 first_string_chars_block = xnew (struct string_chars_block); | |
| 2439 first_string_chars_block->prev = 0; | |
| 2440 first_string_chars_block->next = 0; | |
| 2441 first_string_chars_block->pos = 0; | |
| 2442 current_string_chars_block = first_string_chars_block; | |
| 2443 } | |
| 2444 | |
| 1550 | 2445 static Ibyte * |
| 2446 allocate_big_string_chars (Bytecount length) | |
| 2447 { | |
| 2448 Ibyte *p = xnew_array (Ibyte, length); | |
| 2449 INCREMENT_CONS_COUNTER (length, "string chars"); | |
| 2450 return p; | |
| 2451 } | |
| 2452 | |
| 428 | 2453 static struct string_chars * |
| 793 | 2454 allocate_string_chars_struct (Lisp_Object string_it_goes_with, |
| 814 | 2455 Bytecount fullsize) |
| 428 | 2456 { |
| 2457 struct string_chars *s_chars; | |
| 2458 | |
| 438 | 2459 if (fullsize <= |
| 2460 (countof (current_string_chars_block->string_chars) | |
| 2461 - current_string_chars_block->pos)) | |
| 428 | 2462 { |
| 2463 /* This string can fit in the current string chars block */ | |
| 2464 s_chars = (struct string_chars *) | |
| 2465 (current_string_chars_block->string_chars | |
| 2466 + current_string_chars_block->pos); | |
| 2467 current_string_chars_block->pos += fullsize; | |
| 2468 } | |
| 2469 else | |
| 2470 { | |
| 2471 /* Make a new current string chars block */ | |
| 2472 struct string_chars_block *new_scb = xnew (struct string_chars_block); | |
| 2473 | |
| 2474 current_string_chars_block->next = new_scb; | |
| 2475 new_scb->prev = current_string_chars_block; | |
| 2476 new_scb->next = 0; | |
| 2477 current_string_chars_block = new_scb; | |
| 2478 new_scb->pos = fullsize; | |
| 2479 s_chars = (struct string_chars *) | |
| 2480 current_string_chars_block->string_chars; | |
| 2481 } | |
| 2482 | |
| 793 | 2483 s_chars->string = XSTRING (string_it_goes_with); |
| 428 | 2484 |
| 2485 INCREMENT_CONS_COUNTER (fullsize, "string chars"); | |
| 2486 | |
| 2487 return s_chars; | |
| 2488 } | |
| 3092 | 2489 #endif /* not NEW_GC */ |
| 428 | 2490 |
| 771 | 2491 #ifdef SLEDGEHAMMER_CHECK_ASCII_BEGIN |
| 2492 void | |
| 2493 sledgehammer_check_ascii_begin (Lisp_Object str) | |
| 2494 { | |
| 2495 Bytecount i; | |
| 2496 | |
| 2497 for (i = 0; i < XSTRING_LENGTH (str); i++) | |
| 2498 { | |
| 826 | 2499 if (!byte_ascii_p (string_byte (str, i))) |
| 771 | 2500 break; |
| 2501 } | |
| 2502 | |
| 2503 assert (i == (Bytecount) XSTRING_ASCII_BEGIN (str) || | |
| 2504 (i > MAX_STRING_ASCII_BEGIN && | |
| 2505 (Bytecount) XSTRING_ASCII_BEGIN (str) == | |
| 2506 (Bytecount) MAX_STRING_ASCII_BEGIN)); | |
| 2507 } | |
| 2508 #endif | |
| 2509 | |
| 2510 /* You do NOT want to be calling this! (And if you do, you must call | |
| 851 | 2511 XSET_STRING_ASCII_BEGIN() after modifying the string.) Use ALLOCA () |
| 771 | 2512 instead and then call make_string() like the rest of the world. */ |
| 2513 | |
| 428 | 2514 Lisp_Object |
| 2515 make_uninit_string (Bytecount length) | |
| 2516 { | |
| 438 | 2517 Lisp_String *s; |
| 814 | 2518 Bytecount fullsize = STRING_FULLSIZE (length); |
| 428 | 2519 |
| 438 | 2520 assert (length >= 0 && fullsize > 0); |
| 428 | 2521 |
| 3263 | 2522 #ifdef NEW_GC |
| 2720 | 2523 s = alloc_lrecord_type (Lisp_String, &lrecord_string); |
| 3263 | 2524 #else /* not NEW_GC */ |
| 428 | 2525 /* Allocate the string header */ |
| 438 | 2526 ALLOCATE_FIXED_TYPE (string, Lisp_String, s); |
| 793 | 2527 xzero (*s); |
| 771 | 2528 set_lheader_implementation (&s->u.lheader, &lrecord_string); |
| 3263 | 2529 #endif /* not NEW_GC */ |
| 2720 | 2530 |
| 3063 | 2531 /* The above allocations set the UID field, which overlaps with the |
| 2532 ascii-length field, to some non-zero value. We need to zero it. */ | |
| 2533 XSET_STRING_ASCII_BEGIN (wrap_string (s), 0); | |
| 2534 | |
| 3092 | 2535 #ifdef NEW_GC |
| 3304 | 2536 set_lispstringp_direct (s); |
| 3092 | 2537 STRING_DATA_OBJECT (s) = |
| 2538 wrap_string_direct_data (alloc_lrecord (fullsize, | |
| 2539 &lrecord_string_direct_data)); | |
| 2540 #else /* not NEW_GC */ | |
| 826 | 2541 set_lispstringp_data (s, BIG_STRING_FULLSIZE_P (fullsize) |
| 2720 | 2542 ? allocate_big_string_chars (length + 1) |
| 2543 : allocate_string_chars_struct (wrap_string (s), | |
| 2544 fullsize)->chars); | |
| 3092 | 2545 #endif /* not NEW_GC */ |
| 438 | 2546 |
| 826 | 2547 set_lispstringp_length (s, length); |
| 428 | 2548 s->plist = Qnil; |
| 793 | 2549 set_string_byte (wrap_string (s), length, 0); |
| 2550 | |
| 2551 return wrap_string (s); | |
| 428 | 2552 } |
| 2553 | |
| 2554 #ifdef VERIFY_STRING_CHARS_INTEGRITY | |
| 2555 static void verify_string_chars_integrity (void); | |
| 2556 #endif | |
| 2557 | |
| 2558 /* Resize the string S so that DELTA bytes can be inserted starting | |
| 2559 at POS. If DELTA < 0, it means deletion starting at POS. If | |
| 2560 POS < 0, resize the string but don't copy any characters. Use | |
| 2561 this if you're planning on completely overwriting the string. | |
| 2562 */ | |
| 2563 | |
| 2564 void | |
| 793 | 2565 resize_string (Lisp_Object s, Bytecount pos, Bytecount delta) |
| 428 | 2566 { |
| 3092 | 2567 #ifdef NEW_GC |
| 2568 Bytecount newfullsize, len; | |
| 2569 #else /* not NEW_GC */ | |
| 438 | 2570 Bytecount oldfullsize, newfullsize; |
| 3092 | 2571 #endif /* not NEW_GC */ |
| 428 | 2572 #ifdef VERIFY_STRING_CHARS_INTEGRITY |
| 2573 verify_string_chars_integrity (); | |
| 2574 #endif | |
| 800 | 2575 #ifdef ERROR_CHECK_TEXT |
| 428 | 2576 if (pos >= 0) |
| 2577 { | |
| 793 | 2578 assert (pos <= XSTRING_LENGTH (s)); |
| 428 | 2579 if (delta < 0) |
| 793 | 2580 assert (pos + (-delta) <= XSTRING_LENGTH (s)); |
| 428 | 2581 } |
| 2582 else | |
| 2583 { | |
| 2584 if (delta < 0) | |
| 793 | 2585 assert ((-delta) <= XSTRING_LENGTH (s)); |
| 428 | 2586 } |
| 800 | 2587 #endif /* ERROR_CHECK_TEXT */ |
| 428 | 2588 |
| 2589 if (delta == 0) | |
| 2590 /* simplest case: no size change. */ | |
| 2591 return; | |
| 438 | 2592 |
| 2593 if (pos >= 0 && delta < 0) | |
| 2594 /* If DELTA < 0, the functions below will delete the characters | |
| 2595 before POS. We want to delete characters *after* POS, however, | |
| 2596 so convert this to the appropriate form. */ | |
| 2597 pos += -delta; | |
| 2598 | |
| 3092 | 2599 #ifdef NEW_GC |
| 2600 newfullsize = STRING_FULLSIZE (XSTRING_LENGTH (s) + delta); | |
| 2601 | |
| 2602 len = XSTRING_LENGTH (s) + 1 - pos; | |
| 2603 | |
| 2604 if (delta < 0 && pos >= 0) | |
| 2605 memmove (XSTRING_DATA (s) + pos + delta, | |
| 2606 XSTRING_DATA (s) + pos, len); | |
| 2607 | |
| 2608 XSTRING_DATA_OBJECT (s) = | |
| 2609 wrap_string_direct_data (mc_realloc (XPNTR (XSTRING_DATA_OBJECT (s)), | |
| 2610 newfullsize)); | |
| 2611 if (delta > 0 && pos >= 0) | |
| 2612 memmove (XSTRING_DATA (s) + pos + delta, XSTRING_DATA (s) + pos, | |
| 2613 len); | |
| 2614 | |
| 3263 | 2615 #else /* not NEW_GC */ |
| 793 | 2616 oldfullsize = STRING_FULLSIZE (XSTRING_LENGTH (s)); |
| 2617 newfullsize = STRING_FULLSIZE (XSTRING_LENGTH (s) + delta); | |
| 438 | 2618 |
| 2619 if (BIG_STRING_FULLSIZE_P (oldfullsize)) | |
| 428 | 2620 { |
| 438 | 2621 if (BIG_STRING_FULLSIZE_P (newfullsize)) |
| 428 | 2622 { |
| 440 | 2623 /* Both strings are big. We can just realloc(). |
| 2624 But careful! If the string is shrinking, we have to | |
| 2625 memmove() _before_ realloc(), and if growing, we have to | |
| 2626 memmove() _after_ realloc() - otherwise the access is | |
| 2627 illegal, and we might crash. */ | |
| 793 | 2628 Bytecount len = XSTRING_LENGTH (s) + 1 - pos; |
| 440 | 2629 |
| 2630 if (delta < 0 && pos >= 0) | |
| 793 | 2631 memmove (XSTRING_DATA (s) + pos + delta, |
| 2632 XSTRING_DATA (s) + pos, len); | |
| 2633 XSET_STRING_DATA | |
| 867 | 2634 (s, (Ibyte *) xrealloc (XSTRING_DATA (s), |
| 793 | 2635 XSTRING_LENGTH (s) + delta + 1)); |
| 440 | 2636 if (delta > 0 && pos >= 0) |
| 793 | 2637 memmove (XSTRING_DATA (s) + pos + delta, XSTRING_DATA (s) + pos, |
| 2638 len); | |
| 1550 | 2639 /* Bump the cons counter. |
| 2640 Conservative; Martin let the increment be delta. */ | |
| 2641 INCREMENT_CONS_COUNTER (newfullsize, "string chars"); | |
| 428 | 2642 } |
| 438 | 2643 else /* String has been demoted from BIG_STRING. */ |
| 428 | 2644 { |
| 867 | 2645 Ibyte *new_data = |
| 438 | 2646 allocate_string_chars_struct (s, newfullsize)->chars; |
| 867 | 2647 Ibyte *old_data = XSTRING_DATA (s); |
| 438 | 2648 |
| 2649 if (pos >= 0) | |
| 2650 { | |
| 2651 memcpy (new_data, old_data, pos); | |
| 2652 memcpy (new_data + pos + delta, old_data + pos, | |
| 793 | 2653 XSTRING_LENGTH (s) + 1 - pos); |
| 438 | 2654 } |
| 793 | 2655 XSET_STRING_DATA (s, new_data); |
| 1726 | 2656 xfree (old_data, Ibyte *); |
| 438 | 2657 } |
| 2658 } | |
| 2659 else /* old string is small */ | |
| 2660 { | |
| 2661 if (oldfullsize == newfullsize) | |
| 2662 { | |
| 2663 /* special case; size change but the necessary | |
| 2664 allocation size won't change (up or down; code | |
| 2665 somewhere depends on there not being any unused | |
| 2666 allocation space, modulo any alignment | |
| 2667 constraints). */ | |
| 428 | 2668 if (pos >= 0) |
| 2669 { | |
| 867 | 2670 Ibyte *addroff = pos + XSTRING_DATA (s); |
| 428 | 2671 |
| 2672 memmove (addroff + delta, addroff, | |
| 2673 /* +1 due to zero-termination. */ | |
| 793 | 2674 XSTRING_LENGTH (s) + 1 - pos); |
| 428 | 2675 } |
| 2676 } | |
| 2677 else | |
| 2678 { | |
| 867 | 2679 Ibyte *old_data = XSTRING_DATA (s); |
| 2680 Ibyte *new_data = | |
| 438 | 2681 BIG_STRING_FULLSIZE_P (newfullsize) |
| 1550 | 2682 ? allocate_big_string_chars (XSTRING_LENGTH (s) + delta + 1) |
| 438 | 2683 : allocate_string_chars_struct (s, newfullsize)->chars; |
| 2684 | |
| 428 | 2685 if (pos >= 0) |
| 2686 { | |
| 438 | 2687 memcpy (new_data, old_data, pos); |
| 2688 memcpy (new_data + pos + delta, old_data + pos, | |
| 793 | 2689 XSTRING_LENGTH (s) + 1 - pos); |
| 428 | 2690 } |
| 793 | 2691 XSET_STRING_DATA (s, new_data); |
| 438 | 2692 |
| 2693 { | |
| 2694 /* We need to mark this chunk of the string_chars_block | |
| 2695 as unused so that compact_string_chars() doesn't | |
| 2696 freak. */ | |
| 2697 struct string_chars *old_s_chars = (struct string_chars *) | |
| 2698 ((char *) old_data - offsetof (struct string_chars, chars)); | |
| 2699 /* Sanity check to make sure we aren't hosed by strange | |
| 2700 alignment/padding. */ | |
| 793 | 2701 assert (old_s_chars->string == XSTRING (s)); |
| 454 | 2702 MARK_STRING_CHARS_AS_FREE (old_s_chars); |
| 438 | 2703 ((struct unused_string_chars *) old_s_chars)->fullsize = |
| 2704 oldfullsize; | |
| 2705 } | |
| 428 | 2706 } |
| 438 | 2707 } |
| 3092 | 2708 #endif /* not NEW_GC */ |
| 438 | 2709 |
| 793 | 2710 XSET_STRING_LENGTH (s, XSTRING_LENGTH (s) + delta); |
| 438 | 2711 /* If pos < 0, the string won't be zero-terminated. |
| 2712 Terminate now just to make sure. */ | |
| 793 | 2713 XSTRING_DATA (s)[XSTRING_LENGTH (s)] = '\0'; |
| 438 | 2714 |
| 2715 if (pos >= 0) | |
| 793 | 2716 /* We also have to adjust all of the extent indices after the |
| 2717 place we did the change. We say "pos - 1" because | |
| 2718 adjust_extents() is exclusive of the starting position | |
| 2719 passed to it. */ | |
| 2720 adjust_extents (s, pos - 1, XSTRING_LENGTH (s), delta); | |
| 428 | 2721 |
| 2722 #ifdef VERIFY_STRING_CHARS_INTEGRITY | |
| 2723 verify_string_chars_integrity (); | |
| 2724 #endif | |
| 2725 } | |
| 2726 | |
| 2727 #ifdef MULE | |
| 2728 | |
| 771 | 2729 /* WARNING: If you modify an existing string, you must call |
| 2730 CHECK_LISP_WRITEABLE() before and bump_string_modiff() afterwards. */ | |
| 428 | 2731 void |
| 867 | 2732 set_string_char (Lisp_Object s, Charcount i, Ichar c) |
| 428 | 2733 { |
| 867 | 2734 Ibyte newstr[MAX_ICHAR_LEN]; |
| 771 | 2735 Bytecount bytoff = string_index_char_to_byte (s, i); |
| 867 | 2736 Bytecount oldlen = itext_ichar_len (XSTRING_DATA (s) + bytoff); |
| 2737 Bytecount newlen = set_itext_ichar (newstr, c); | |
| 428 | 2738 |
| 793 | 2739 sledgehammer_check_ascii_begin (s); |
| 428 | 2740 if (oldlen != newlen) |
| 2741 resize_string (s, bytoff, newlen - oldlen); | |
| 793 | 2742 /* Remember, XSTRING_DATA (s) might have changed so we can't cache it. */ |
| 2743 memcpy (XSTRING_DATA (s) + bytoff, newstr, newlen); | |
| 771 | 2744 if (oldlen != newlen) |
| 2745 { | |
| 793 | 2746 if (newlen > 1 && i <= (Charcount) XSTRING_ASCII_BEGIN (s)) |
| 771 | 2747 /* Everything starting with the new char is no longer part of |
| 2748 ascii_begin */ | |
| 793 | 2749 XSET_STRING_ASCII_BEGIN (s, i); |
| 2750 else if (newlen == 1 && i == (Charcount) XSTRING_ASCII_BEGIN (s)) | |
| 771 | 2751 /* We've extended ascii_begin, and we have to figure out how much by */ |
| 2752 { | |
| 2753 Bytecount j; | |
| 814 | 2754 for (j = (Bytecount) i + 1; j < XSTRING_LENGTH (s); j++) |
| 771 | 2755 { |
| 826 | 2756 if (!byte_ascii_p (XSTRING_DATA (s)[j])) |
| 771 | 2757 break; |
| 2758 } | |
| 814 | 2759 XSET_STRING_ASCII_BEGIN (s, min (j, (Bytecount) MAX_STRING_ASCII_BEGIN)); |
| 771 | 2760 } |
| 2761 } | |
| 793 | 2762 sledgehammer_check_ascii_begin (s); |
| 428 | 2763 } |
| 2764 | |
| 2765 #endif /* MULE */ | |
| 2766 | |
| 2767 DEFUN ("make-string", Fmake_string, 2, 2, 0, /* | |
| 444 | 2768 Return a new string consisting of LENGTH copies of CHARACTER. |
| 2769 LENGTH must be a non-negative integer. | |
| 428 | 2770 */ |
| 444 | 2771 (length, character)) |
| 428 | 2772 { |
| 2773 CHECK_NATNUM (length); | |
| 444 | 2774 CHECK_CHAR_COERCE_INT (character); |
| 428 | 2775 { |
| 867 | 2776 Ibyte init_str[MAX_ICHAR_LEN]; |
| 2777 int len = set_itext_ichar (init_str, XCHAR (character)); | |
| 428 | 2778 Lisp_Object val = make_uninit_string (len * XINT (length)); |
| 2779 | |
| 2780 if (len == 1) | |
| 771 | 2781 { |
| 2782 /* Optimize the single-byte case */ | |
| 2783 memset (XSTRING_DATA (val), XCHAR (character), XSTRING_LENGTH (val)); | |
| 793 | 2784 XSET_STRING_ASCII_BEGIN (val, min (MAX_STRING_ASCII_BEGIN, |
| 2785 len * XINT (length))); | |
| 771 | 2786 } |
| 428 | 2787 else |
| 2788 { | |
| 647 | 2789 EMACS_INT i; |
| 867 | 2790 Ibyte *ptr = XSTRING_DATA (val); |
| 428 | 2791 |
| 2792 for (i = XINT (length); i; i--) | |
| 2793 { | |
| 867 | 2794 Ibyte *init_ptr = init_str; |
| 428 | 2795 switch (len) |
| 2796 { | |
| 2797 case 4: *ptr++ = *init_ptr++; | |
| 2798 case 3: *ptr++ = *init_ptr++; | |
| 2799 case 2: *ptr++ = *init_ptr++; | |
| 2800 case 1: *ptr++ = *init_ptr++; | |
| 2801 } | |
| 2802 } | |
| 2803 } | |
| 771 | 2804 sledgehammer_check_ascii_begin (val); |
| 428 | 2805 return val; |
| 2806 } | |
| 2807 } | |
| 2808 | |
| 2809 DEFUN ("string", Fstring, 0, MANY, 0, /* | |
| 2810 Concatenate all the argument characters and make the result a string. | |
|
4693
80cd90837ac5
Add argument information to remaining MANY or UNEVALLED C subrs.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3514
diff
changeset
|
2811 |
|
80cd90837ac5
Add argument information to remaining MANY or UNEVALLED C subrs.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3514
diff
changeset
|
2812 arguments: (&rest ARGS) |
| 428 | 2813 */ |
| 2814 (int nargs, Lisp_Object *args)) | |
| 2815 { | |
| 2367 | 2816 Ibyte *storage = alloca_ibytes (nargs * MAX_ICHAR_LEN); |
| 867 | 2817 Ibyte *p = storage; |
| 428 | 2818 |
| 2819 for (; nargs; nargs--, args++) | |
| 2820 { | |
| 2821 Lisp_Object lisp_char = *args; | |
| 2822 CHECK_CHAR_COERCE_INT (lisp_char); | |
| 867 | 2823 p += set_itext_ichar (p, XCHAR (lisp_char)); |
| 428 | 2824 } |
| 2825 return make_string (storage, p - storage); | |
| 2826 } | |
| 2827 | |
| 771 | 2828 /* Initialize the ascii_begin member of a string to the correct value. */ |
| 2829 | |
| 2830 void | |
| 2831 init_string_ascii_begin (Lisp_Object string) | |
| 2832 { | |
| 2833 #ifdef MULE | |
| 2834 int i; | |
| 2835 Bytecount length = XSTRING_LENGTH (string); | |
| 867 | 2836 Ibyte *contents = XSTRING_DATA (string); |
| 771 | 2837 |
| 2838 for (i = 0; i < length; i++) | |
| 2839 { | |
| 826 | 2840 if (!byte_ascii_p (contents[i])) |
| 771 | 2841 break; |
| 2842 } | |
| 793 | 2843 XSET_STRING_ASCII_BEGIN (string, min (i, MAX_STRING_ASCII_BEGIN)); |
| 771 | 2844 #else |
| 793 | 2845 XSET_STRING_ASCII_BEGIN (string, min (XSTRING_LENGTH (string), |
| 2846 MAX_STRING_ASCII_BEGIN)); | |
| 771 | 2847 #endif |
| 2848 sledgehammer_check_ascii_begin (string); | |
| 2849 } | |
| 428 | 2850 |
| 2851 /* Take some raw memory, which MUST already be in internal format, | |
| 2852 and package it up into a Lisp string. */ | |
| 2853 Lisp_Object | |
| 867 | 2854 make_string (const Ibyte *contents, Bytecount length) |
| 428 | 2855 { |
| 2856 Lisp_Object val; | |
| 2857 | |
| 2858 /* Make sure we find out about bad make_string's when they happen */ | |
| 800 | 2859 #if defined (ERROR_CHECK_TEXT) && defined (MULE) |
| 428 | 2860 bytecount_to_charcount (contents, length); /* Just for the assertions */ |
| 2861 #endif | |
| 2862 | |
| 2863 val = make_uninit_string (length); | |
| 2864 memcpy (XSTRING_DATA (val), contents, length); | |
| 771 | 2865 init_string_ascii_begin (val); |
| 2866 sledgehammer_check_ascii_begin (val); | |
| 428 | 2867 return val; |
| 2868 } | |
| 2869 | |
| 2870 /* Take some raw memory, encoded in some external data format, | |
| 2871 and convert it into a Lisp string. */ | |
| 2872 Lisp_Object | |
| 442 | 2873 make_ext_string (const Extbyte *contents, EMACS_INT length, |
| 440 | 2874 Lisp_Object coding_system) |
| 428 | 2875 { |
| 440 | 2876 Lisp_Object string; |
| 2877 TO_INTERNAL_FORMAT (DATA, (contents, length), | |
| 2878 LISP_STRING, string, | |
| 2879 coding_system); | |
| 2880 return string; | |
| 428 | 2881 } |
| 2882 | |
| 2883 Lisp_Object | |
| 867 | 2884 build_intstring (const Ibyte *str) |
| 771 | 2885 { |
| 2886 /* Some strlen's crash and burn if passed null. */ | |
| 814 | 2887 return make_string (str, (str ? qxestrlen (str) : (Bytecount) 0)); |
| 771 | 2888 } |
| 2889 | |
| 2890 Lisp_Object | |
| 867 | 2891 build_string (const CIbyte *str) |
| 428 | 2892 { |
| 2893 /* Some strlen's crash and burn if passed null. */ | |
| 867 | 2894 return make_string ((const Ibyte *) str, (str ? strlen (str) : 0)); |
| 428 | 2895 } |
| 2896 | |
| 2897 Lisp_Object | |
| 593 | 2898 build_ext_string (const Extbyte *str, Lisp_Object coding_system) |
| 428 | 2899 { |
| 2900 /* Some strlen's crash and burn if passed null. */ | |
| 2367 | 2901 return make_ext_string ((const Extbyte *) str, |
| 2902 (str ? dfc_external_data_len (str, coding_system) : | |
| 2903 0), | |
| 440 | 2904 coding_system); |
| 428 | 2905 } |
| 2906 | |
| 2907 Lisp_Object | |
| 867 | 2908 build_msg_intstring (const Ibyte *str) |
| 428 | 2909 { |
| 771 | 2910 return build_intstring (GETTEXT (str)); |
| 2911 } | |
| 2912 | |
| 2913 Lisp_Object | |
| 867 | 2914 build_msg_string (const CIbyte *str) |
| 771 | 2915 { |
| 2916 return build_string (CGETTEXT (str)); | |
| 428 | 2917 } |
| 2918 | |
| 2919 Lisp_Object | |
| 867 | 2920 make_string_nocopy (const Ibyte *contents, Bytecount length) |
| 428 | 2921 { |
| 438 | 2922 Lisp_String *s; |
| 428 | 2923 Lisp_Object val; |
| 2924 | |
| 2925 /* Make sure we find out about bad make_string_nocopy's when they happen */ | |
| 800 | 2926 #if defined (ERROR_CHECK_TEXT) && defined (MULE) |
| 428 | 2927 bytecount_to_charcount (contents, length); /* Just for the assertions */ |
| 2928 #endif | |
| 2929 | |
| 3263 | 2930 #ifdef NEW_GC |
| 2720 | 2931 s = alloc_lrecord_type (Lisp_String, &lrecord_string); |
| 2932 mcpro (wrap_pointer_1 (s)); /* otherwise nocopy_strings get | |
| 2933 collected and static data is tried to | |
| 2934 be freed. */ | |
| 3263 | 2935 #else /* not NEW_GC */ |
| 428 | 2936 /* Allocate the string header */ |
| 438 | 2937 ALLOCATE_FIXED_TYPE (string, Lisp_String, s); |
| 771 | 2938 set_lheader_implementation (&s->u.lheader, &lrecord_string); |
| 2939 SET_C_READONLY_RECORD_HEADER (&s->u.lheader); | |
| 3263 | 2940 #endif /* not NEW_GC */ |
| 3063 | 2941 /* Don't need to XSET_STRING_ASCII_BEGIN() here because it happens in |
| 2942 init_string_ascii_begin(). */ | |
| 428 | 2943 s->plist = Qnil; |
| 3092 | 2944 #ifdef NEW_GC |
| 2945 set_lispstringp_indirect (s); | |
| 2946 STRING_DATA_OBJECT (s) = | |
| 2947 wrap_string_indirect_data | |
| 2948 (alloc_lrecord_type (Lisp_String_Indirect_Data, | |
| 2949 &lrecord_string_indirect_data)); | |
| 2950 XSTRING_INDIRECT_DATA_DATA (STRING_DATA_OBJECT (s)) = (Ibyte *) contents; | |
| 2951 XSTRING_INDIRECT_DATA_SIZE (STRING_DATA_OBJECT (s)) = length; | |
| 2952 #else /* not NEW_GC */ | |
| 867 | 2953 set_lispstringp_data (s, (Ibyte *) contents); |
| 826 | 2954 set_lispstringp_length (s, length); |
| 3092 | 2955 #endif /* not NEW_GC */ |
| 793 | 2956 val = wrap_string (s); |
| 771 | 2957 init_string_ascii_begin (val); |
| 2958 sledgehammer_check_ascii_begin (val); | |
| 2959 | |
| 428 | 2960 return val; |
| 2961 } | |
| 2962 | |
| 2963 | |
| 3263 | 2964 #ifndef NEW_GC |
| 428 | 2965 /************************************************************************/ |
| 2966 /* lcrecord lists */ | |
| 2967 /************************************************************************/ | |
| 2968 | |
| 2969 /* Lcrecord lists are used to manage the allocation of particular | |
| 3024 | 2970 sorts of lcrecords, to avoid calling BASIC_ALLOC_LCRECORD() (and thus |
| 428 | 2971 malloc() and garbage-collection junk) as much as possible. |
| 2972 It is similar to the Blocktype class. | |
| 2973 | |
| 1204 | 2974 See detailed comment in lcrecord.h. |
| 2975 */ | |
| 2976 | |
| 2977 const struct memory_description free_description[] = { | |
| 2551 | 2978 { XD_LISP_OBJECT, offsetof (struct free_lcrecord_header, chain), 0, { 0 }, |
| 1204 | 2979 XD_FLAG_FREE_LISP_OBJECT }, |
| 2980 { XD_END } | |
| 2981 }; | |
| 2982 | |
| 2983 DEFINE_LRECORD_IMPLEMENTATION ("free", free, | |
| 2984 0, /*dumpable-flag*/ | |
| 2985 0, internal_object_printer, | |
| 2986 0, 0, 0, free_description, | |
| 2987 struct free_lcrecord_header); | |
| 2988 | |
| 2989 const struct memory_description lcrecord_list_description[] = { | |
| 2551 | 2990 { XD_LISP_OBJECT, offsetof (struct lcrecord_list, free), 0, { 0 }, |
| 1204 | 2991 XD_FLAG_FREE_LISP_OBJECT }, |
| 2992 { XD_END } | |
| 2993 }; | |
| 428 | 2994 |
| 2995 static Lisp_Object | |
| 2996 mark_lcrecord_list (Lisp_Object obj) | |
| 2997 { | |
| 2998 struct lcrecord_list *list = XLCRECORD_LIST (obj); | |
| 2999 Lisp_Object chain = list->free; | |
| 3000 | |
| 3001 while (!NILP (chain)) | |
| 3002 { | |
| 3003 struct lrecord_header *lheader = XRECORD_LHEADER (chain); | |
| 3004 struct free_lcrecord_header *free_header = | |
| 3005 (struct free_lcrecord_header *) lheader; | |
| 3006 | |
| 442 | 3007 gc_checking_assert |
| 3008 (/* There should be no other pointers to the free list. */ | |
| 3009 ! MARKED_RECORD_HEADER_P (lheader) | |
| 3010 && | |
| 3011 /* Only lcrecords should be here. */ | |
| 1204 | 3012 ! list->implementation->basic_p |
| 442 | 3013 && |
| 3014 /* Only free lcrecords should be here. */ | |
| 3015 free_header->lcheader.free | |
| 3016 && | |
| 3017 /* The type of the lcrecord must be right. */ | |
| 1204 | 3018 lheader->type == lrecord_type_free |
| 442 | 3019 && |
| 3020 /* So must the size. */ | |
| 1204 | 3021 (list->implementation->static_size == 0 || |
| 3022 list->implementation->static_size == list->size) | |
| 442 | 3023 ); |
| 428 | 3024 |
| 3025 MARK_RECORD_HEADER (lheader); | |
| 3026 chain = free_header->chain; | |
| 3027 } | |
| 3028 | |
| 3029 return Qnil; | |
| 3030 } | |
| 3031 | |
| 934 | 3032 DEFINE_LRECORD_IMPLEMENTATION ("lcrecord-list", lcrecord_list, |
| 3033 0, /*dumpable-flag*/ | |
| 3034 mark_lcrecord_list, internal_object_printer, | |
| 1204 | 3035 0, 0, 0, lcrecord_list_description, |
| 3036 struct lcrecord_list); | |
| 934 | 3037 |
| 428 | 3038 Lisp_Object |
| 665 | 3039 make_lcrecord_list (Elemcount size, |
| 442 | 3040 const struct lrecord_implementation *implementation) |
| 428 | 3041 { |
| 3024 | 3042 /* Don't use old_alloc_lcrecord_type() avoid infinite recursion |
| 1204 | 3043 allocating this, */ |
| 3044 struct lcrecord_list *p = (struct lcrecord_list *) | |
| 3024 | 3045 old_basic_alloc_lcrecord (sizeof (struct lcrecord_list), |
| 3046 &lrecord_lcrecord_list); | |
| 428 | 3047 |
| 3048 p->implementation = implementation; | |
| 3049 p->size = size; | |
| 3050 p->free = Qnil; | |
| 793 | 3051 return wrap_lcrecord_list (p); |
| 428 | 3052 } |
| 3053 | |
| 3054 Lisp_Object | |
| 1204 | 3055 alloc_managed_lcrecord (Lisp_Object lcrecord_list) |
| 428 | 3056 { |
| 3057 struct lcrecord_list *list = XLCRECORD_LIST (lcrecord_list); | |
| 3058 if (!NILP (list->free)) | |
| 3059 { | |
| 3060 Lisp_Object val = list->free; | |
| 3061 struct free_lcrecord_header *free_header = | |
| 3062 (struct free_lcrecord_header *) XPNTR (val); | |
| 1204 | 3063 struct lrecord_header *lheader = &free_header->lcheader.lheader; |
| 428 | 3064 |
| 3065 #ifdef ERROR_CHECK_GC | |
| 1204 | 3066 /* Major overkill here. */ |
| 428 | 3067 /* There should be no other pointers to the free list. */ |
| 442 | 3068 assert (! MARKED_RECORD_HEADER_P (lheader)); |
| 428 | 3069 /* Only free lcrecords should be here. */ |
| 3070 assert (free_header->lcheader.free); | |
| 1204 | 3071 assert (lheader->type == lrecord_type_free); |
| 3072 /* Only lcrecords should be here. */ | |
| 3073 assert (! (list->implementation->basic_p)); | |
| 3074 #if 0 /* Not used anymore, now that we set the type of the header to | |
| 3075 lrecord_type_free. */ | |
| 428 | 3076 /* The type of the lcrecord must be right. */ |
| 442 | 3077 assert (LHEADER_IMPLEMENTATION (lheader) == list->implementation); |
| 1204 | 3078 #endif /* 0 */ |
| 428 | 3079 /* So must the size. */ |
| 1204 | 3080 assert (list->implementation->static_size == 0 || |
| 3081 list->implementation->static_size == list->size); | |
| 428 | 3082 #endif /* ERROR_CHECK_GC */ |
| 442 | 3083 |
| 428 | 3084 list->free = free_header->chain; |
| 3085 free_header->lcheader.free = 0; | |
| 1204 | 3086 /* Put back the correct type, as we set it to lrecord_type_free. */ |
| 3087 lheader->type = list->implementation->lrecord_type_index; | |
| 3024 | 3088 old_zero_sized_lcrecord (free_header, list->size); |
| 428 | 3089 return val; |
| 3090 } | |
| 3091 else | |
| 3024 | 3092 return wrap_pointer_1 (old_basic_alloc_lcrecord (list->size, |
| 3093 list->implementation)); | |
| 428 | 3094 } |
| 3095 | |
| 771 | 3096 /* "Free" a Lisp object LCRECORD by placing it on its associated free list |
| 1204 | 3097 LCRECORD_LIST; next time alloc_managed_lcrecord() is called with the |
| 771 | 3098 same LCRECORD_LIST as its parameter, it will return an object from the |
| 3099 free list, which may be this one. Be VERY VERY SURE there are no | |
| 3100 pointers to this object hanging around anywhere where they might be | |
| 3101 used! | |
| 3102 | |
| 3103 The first thing this does before making any global state change is to | |
| 3104 call the finalize method of the object, if it exists. */ | |
| 3105 | |
| 428 | 3106 void |
| 3107 free_managed_lcrecord (Lisp_Object lcrecord_list, Lisp_Object lcrecord) | |
| 3108 { | |
| 3109 struct lcrecord_list *list = XLCRECORD_LIST (lcrecord_list); | |
| 3110 struct free_lcrecord_header *free_header = | |
| 3111 (struct free_lcrecord_header *) XPNTR (lcrecord); | |
| 442 | 3112 struct lrecord_header *lheader = &free_header->lcheader.lheader; |
| 3113 const struct lrecord_implementation *implementation | |
| 428 | 3114 = LHEADER_IMPLEMENTATION (lheader); |
| 3115 | |
| 771 | 3116 /* Finalizer methods may try to free objects within them, which typically |
| 3117 won't be marked and thus are scheduled for demolition. Putting them | |
| 3118 on the free list would be very bad, as we'd have xfree()d memory in | |
| 3119 the list. Even if for some reason the objects are still live | |
| 3120 (generally a logic error!), we still will have problems putting such | |
| 3121 an object on the free list right now (e.g. we'd have to avoid calling | |
| 3122 the finalizer twice, etc.). So basically, those finalizers should not | |
| 3123 be freeing any objects if during GC. Abort now to catch those | |
| 3124 problems. */ | |
| 3125 gc_checking_assert (!gc_in_progress); | |
| 3126 | |
| 428 | 3127 /* Make sure the size is correct. This will catch, for example, |
| 3128 putting a window configuration on the wrong free list. */ | |
| 1204 | 3129 gc_checking_assert (detagged_lisp_object_size (lheader) == list->size); |
| 771 | 3130 /* Make sure the object isn't already freed. */ |
| 3131 gc_checking_assert (!free_header->lcheader.free); | |
| 2367 | 3132 /* Freeing stuff in dumped memory is bad. If you trip this, you |
| 3133 may need to check for this before freeing. */ | |
| 3134 gc_checking_assert (!OBJECT_DUMPED_P (lcrecord)); | |
| 771 | 3135 |
| 428 | 3136 if (implementation->finalizer) |
| 3137 implementation->finalizer (lheader, 0); | |
| 1204 | 3138 /* Yes, there are two ways to indicate freeness -- the type is |
| 3139 lrecord_type_free or the ->free flag is set. We used to do only the | |
| 3140 latter; now we do the former as well for KKCC purposes. Probably | |
| 3141 safer in any case, as we will lose quicker this way than keeping | |
| 3142 around an lrecord of apparently correct type but bogus junk in it. */ | |
| 3143 MARK_LRECORD_AS_FREE (lheader); | |
| 428 | 3144 free_header->chain = list->free; |
| 3145 free_header->lcheader.free = 1; | |
| 3146 list->free = lcrecord; | |
| 3147 } | |
| 3148 | |
| 771 | 3149 static Lisp_Object all_lcrecord_lists[countof (lrecord_implementations_table)]; |
| 3150 | |
| 3151 void * | |
| 3152 alloc_automanaged_lcrecord (Bytecount size, | |
| 3153 const struct lrecord_implementation *imp) | |
| 3154 { | |
| 3155 if (EQ (all_lcrecord_lists[imp->lrecord_type_index], Qzero)) | |
| 3156 all_lcrecord_lists[imp->lrecord_type_index] = | |
| 3157 make_lcrecord_list (size, imp); | |
| 3158 | |
| 1204 | 3159 return XPNTR (alloc_managed_lcrecord |
| 771 | 3160 (all_lcrecord_lists[imp->lrecord_type_index])); |
| 3161 } | |
| 3162 | |
| 3163 void | |
| 3024 | 3164 old_free_lcrecord (Lisp_Object rec) |
| 771 | 3165 { |
| 3166 int type = XRECORD_LHEADER (rec)->type; | |
| 3167 | |
| 3168 assert (!EQ (all_lcrecord_lists[type], Qzero)); | |
| 3169 | |
| 3170 free_managed_lcrecord (all_lcrecord_lists[type], rec); | |
| 3171 } | |
| 3263 | 3172 #endif /* not NEW_GC */ |
| 428 | 3173 |
| 3174 | |
| 3175 DEFUN ("purecopy", Fpurecopy, 1, 1, 0, /* | |
| 3176 Kept for compatibility, returns its argument. | |
| 3177 Old: | |
| 3178 Make a copy of OBJECT in pure storage. | |
| 3179 Recursively copies contents of vectors and cons cells. | |
| 3180 Does not copy symbols. | |
| 3181 */ | |
| 444 | 3182 (object)) |
| 428 | 3183 { |
| 444 | 3184 return object; |
| 428 | 3185 } |
| 3186 | |
| 3187 | |
| 3188 /************************************************************************/ | |
| 3189 /* Garbage Collection */ | |
| 3190 /************************************************************************/ | |
| 3191 | |
| 442 | 3192 /* All the built-in lisp object types are enumerated in `enum lrecord_type'. |
| 3193 Additional ones may be defined by a module (none yet). We leave some | |
| 3194 room in `lrecord_implementations_table' for such new lisp object types. */ | |
| 647 | 3195 const struct lrecord_implementation *lrecord_implementations_table[(int)lrecord_type_last_built_in_type + MODULE_DEFINABLE_TYPE_COUNT]; |
| 3196 int lrecord_type_count = lrecord_type_last_built_in_type; | |
| 1676 | 3197 #ifndef USE_KKCC |
| 442 | 3198 /* Object marker functions are in the lrecord_implementation structure. |
| 3199 But copying them to a parallel array is much more cache-friendly. | |
| 3200 This hack speeds up (garbage-collect) by about 5%. */ | |
| 3201 Lisp_Object (*lrecord_markers[countof (lrecord_implementations_table)]) (Lisp_Object); | |
| 1676 | 3202 #endif /* not USE_KKCC */ |
| 428 | 3203 |
| 3204 struct gcpro *gcprolist; | |
| 3205 | |
| 771 | 3206 /* We want the staticpro list relocated, but not the pointers found |
| 3207 therein, because they refer to locations in the global data segment, not | |
| 3208 in the heap; we only dump heap objects. Hence we use a trivial | |
| 3209 description, as for pointerless objects. (Note that the data segment | |
| 3210 objects, which are global variables like Qfoo or Vbar, themselves are | |
| 3211 pointers to heap objects. Each needs to be described to pdump as a | |
| 3212 "root pointer"; this happens in the call to staticpro(). */ | |
| 1204 | 3213 static const struct memory_description staticpro_description_1[] = { |
| 452 | 3214 { XD_END } |
| 3215 }; | |
| 3216 | |
| 1204 | 3217 static const struct sized_memory_description staticpro_description = { |
| 452 | 3218 sizeof (Lisp_Object *), |
| 3219 staticpro_description_1 | |
| 3220 }; | |
| 3221 | |
| 1204 | 3222 static const struct memory_description staticpros_description_1[] = { |
| 452 | 3223 XD_DYNARR_DESC (Lisp_Object_ptr_dynarr, &staticpro_description), |
| 3224 { XD_END } | |
| 3225 }; | |
| 3226 | |
| 1204 | 3227 static const struct sized_memory_description staticpros_description = { |
| 452 | 3228 sizeof (Lisp_Object_ptr_dynarr), |
| 3229 staticpros_description_1 | |
| 3230 }; | |
| 3231 | |
| 771 | 3232 #ifdef DEBUG_XEMACS |
| 3233 | |
| 1204 | 3234 static const struct memory_description staticpro_one_name_description_1[] = { |
| 2367 | 3235 { XD_ASCII_STRING, 0 }, |
| 771 | 3236 { XD_END } |
| 3237 }; | |
| 3238 | |
| 1204 | 3239 static const struct sized_memory_description staticpro_one_name_description = { |
| 771 | 3240 sizeof (char *), |
| 3241 staticpro_one_name_description_1 | |
| 3242 }; | |
| 3243 | |
| 1204 | 3244 static const struct memory_description staticpro_names_description_1[] = { |
| 771 | 3245 XD_DYNARR_DESC (char_ptr_dynarr, &staticpro_one_name_description), |
| 3246 { XD_END } | |
| 3247 }; | |
| 3248 | |
| 1204 | 3249 |
| 3250 extern const struct sized_memory_description staticpro_names_description; | |
| 3251 | |
| 3252 const struct sized_memory_description staticpro_names_description = { | |
| 771 | 3253 sizeof (char_ptr_dynarr), |
| 3254 staticpro_names_description_1 | |
| 3255 }; | |
| 3256 | |
| 3257 /* Help debug crashes gc-marking a staticpro'ed object. */ | |
| 3258 | |
| 3259 Lisp_Object_ptr_dynarr *staticpros; | |
| 3260 char_ptr_dynarr *staticpro_names; | |
| 3261 | |
| 3262 /* Mark the Lisp_Object at non-heap VARADDRESS as a root object for | |
| 3263 garbage collection, and for dumping. */ | |
| 3264 void | |
| 3265 staticpro_1 (Lisp_Object *varaddress, char *varname) | |
| 3266 { | |
| 3267 Dynarr_add (staticpros, varaddress); | |
| 3268 Dynarr_add (staticpro_names, varname); | |
| 1204 | 3269 dump_add_root_lisp_object (varaddress); |
| 771 | 3270 } |
| 3271 | |
| 3272 | |
| 3273 Lisp_Object_ptr_dynarr *staticpros_nodump; | |
| 3274 char_ptr_dynarr *staticpro_nodump_names; | |
| 3275 | |
| 3276 /* Mark the Lisp_Object at heap VARADDRESS as a root object for | |
| 3277 garbage collection, but not for dumping. (See below.) */ | |
| 3278 void | |
| 3279 staticpro_nodump_1 (Lisp_Object *varaddress, char *varname) | |
| 3280 { | |
| 3281 Dynarr_add (staticpros_nodump, varaddress); | |
| 3282 Dynarr_add (staticpro_nodump_names, varname); | |
| 3283 } | |
| 3284 | |
| 996 | 3285 #ifdef HAVE_SHLIB |
| 3286 /* Stop treating the Lisp_Object at non-heap VARADDRESS as a root object | |
| 3287 for garbage collection, but not for dumping. */ | |
| 3288 void | |
| 3289 unstaticpro_nodump_1 (Lisp_Object *varaddress, char *varname) | |
| 3290 { | |
| 3291 Dynarr_delete_object (staticpros, varaddress); | |
| 3292 Dynarr_delete_object (staticpro_names, varname); | |
| 3293 } | |
| 3294 #endif | |
| 3295 | |
| 771 | 3296 #else /* not DEBUG_XEMACS */ |
| 3297 | |
| 452 | 3298 Lisp_Object_ptr_dynarr *staticpros; |
| 3299 | |
| 3300 /* Mark the Lisp_Object at non-heap VARADDRESS as a root object for | |
| 3301 garbage collection, and for dumping. */ | |
| 428 | 3302 void |
| 3303 staticpro (Lisp_Object *varaddress) | |
| 3304 { | |
| 452 | 3305 Dynarr_add (staticpros, varaddress); |
| 1204 | 3306 dump_add_root_lisp_object (varaddress); |
| 428 | 3307 } |
| 3308 | |
| 442 | 3309 |
| 452 | 3310 Lisp_Object_ptr_dynarr *staticpros_nodump; |
| 3311 | |
| 771 | 3312 /* Mark the Lisp_Object at heap VARADDRESS as a root object for garbage |
| 3313 collection, but not for dumping. This is used for objects where the | |
| 3314 only sure pointer is in the heap (rather than in the global data | |
| 3315 segment, as must be the case for pdump root pointers), but not inside of | |
| 3316 another Lisp object (where it will be marked as a result of that Lisp | |
| 3317 object's mark method). The call to staticpro_nodump() must occur *BOTH* | |
| 3318 at initialization time and at "reinitialization" time (startup, after | |
| 3319 pdump load.) (For example, this is the case with the predicate symbols | |
| 3320 for specifier and coding system types. The pointer to this symbol is | |
| 3321 inside of a methods structure, which is allocated on the heap. The | |
| 3322 methods structure will be written out to the pdump data file, and may be | |
| 3323 reloaded at a different address.) | |
| 3324 | |
| 3325 #### The necessity for reinitialization is a bug in pdump. Pdump should | |
| 3326 automatically regenerate the staticpro()s for these symbols when it | |
| 3327 loads the data in. */ | |
| 3328 | |
| 428 | 3329 void |
| 3330 staticpro_nodump (Lisp_Object *varaddress) | |
| 3331 { | |
| 452 | 3332 Dynarr_add (staticpros_nodump, varaddress); |
| 428 | 3333 } |
| 3334 | |
| 996 | 3335 #ifdef HAVE_SHLIB |
| 3336 /* Unmark the Lisp_Object at non-heap VARADDRESS as a root object for | |
| 3337 garbage collection, but not for dumping. */ | |
| 3338 void | |
| 3339 unstaticpro_nodump (Lisp_Object *varaddress) | |
| 3340 { | |
| 3341 Dynarr_delete_object (staticpros, varaddress); | |
| 3342 } | |
| 3343 #endif | |
| 3344 | |
| 771 | 3345 #endif /* not DEBUG_XEMACS */ |
| 3346 | |
| 2720 | 3347 |
| 3348 | |
| 3349 | |
| 3350 | |
| 3263 | 3351 #ifdef NEW_GC |
| 2720 | 3352 static const struct memory_description mcpro_description_1[] = { |
| 3353 { XD_END } | |
| 3354 }; | |
| 3355 | |
| 3356 static const struct sized_memory_description mcpro_description = { | |
| 3357 sizeof (Lisp_Object *), | |
| 3358 mcpro_description_1 | |
| 3359 }; | |
| 3360 | |
| 3361 static const struct memory_description mcpros_description_1[] = { | |
| 3362 XD_DYNARR_DESC (Lisp_Object_dynarr, &mcpro_description), | |
| 3363 { XD_END } | |
| 3364 }; | |
| 3365 | |
| 3366 static const struct sized_memory_description mcpros_description = { | |
| 3367 sizeof (Lisp_Object_dynarr), | |
| 3368 mcpros_description_1 | |
| 3369 }; | |
| 3370 | |
| 3371 #ifdef DEBUG_XEMACS | |
| 3372 | |
| 3373 static const struct memory_description mcpro_one_name_description_1[] = { | |
| 3374 { XD_ASCII_STRING, 0 }, | |
| 3375 { XD_END } | |
| 3376 }; | |
| 3377 | |
| 3378 static const struct sized_memory_description mcpro_one_name_description = { | |
| 3379 sizeof (char *), | |
| 3380 mcpro_one_name_description_1 | |
| 3381 }; | |
| 3382 | |
| 3383 static const struct memory_description mcpro_names_description_1[] = { | |
| 3384 XD_DYNARR_DESC (char_ptr_dynarr, &mcpro_one_name_description), | |
| 3385 { XD_END } | |
| 3386 }; | |
| 3387 | |
| 3388 extern const struct sized_memory_description mcpro_names_description; | |
| 3389 | |
| 3390 const struct sized_memory_description mcpro_names_description = { | |
| 3391 sizeof (char_ptr_dynarr), | |
| 3392 mcpro_names_description_1 | |
| 3393 }; | |
| 3394 | |
| 3395 /* Help debug crashes gc-marking a mcpro'ed object. */ | |
| 3396 | |
| 3397 Lisp_Object_dynarr *mcpros; | |
| 3398 char_ptr_dynarr *mcpro_names; | |
| 3399 | |
| 3400 /* Mark the Lisp_Object at non-heap VARADDRESS as a root object for | |
| 3401 garbage collection, and for dumping. */ | |
| 3402 void | |
| 3403 mcpro_1 (Lisp_Object varaddress, char *varname) | |
| 3404 { | |
| 3405 Dynarr_add (mcpros, varaddress); | |
| 3406 Dynarr_add (mcpro_names, varname); | |
| 3407 } | |
| 3408 | |
| 3409 #else /* not DEBUG_XEMACS */ | |
| 3410 | |
| 3411 Lisp_Object_dynarr *mcpros; | |
| 3412 | |
| 3413 /* Mark the Lisp_Object at non-heap VARADDRESS as a root object for | |
| 3414 garbage collection, and for dumping. */ | |
| 3415 void | |
| 3416 mcpro (Lisp_Object varaddress) | |
| 3417 { | |
| 3418 Dynarr_add (mcpros, varaddress); | |
| 3419 } | |
| 3420 | |
| 3421 #endif /* not DEBUG_XEMACS */ | |
| 3263 | 3422 #endif /* NEW_GC */ |
| 3423 | |
| 3424 | |
| 3425 #ifndef NEW_GC | |
| 428 | 3426 static int gc_count_num_short_string_in_use; |
| 647 | 3427 static Bytecount gc_count_string_total_size; |
| 3428 static Bytecount gc_count_short_string_total_size; | |
| 428 | 3429 |
| 3430 /* static int gc_count_total_records_used, gc_count_records_total_size; */ | |
| 3431 | |
| 3432 | |
| 3433 /* stats on lcrecords in use - kinda kludgy */ | |
| 3434 | |
| 3435 static struct | |
| 3436 { | |
| 3437 int instances_in_use; | |
| 3438 int bytes_in_use; | |
| 3439 int instances_freed; | |
| 3440 int bytes_freed; | |
| 3441 int instances_on_free_list; | |
| 3461 | 3442 } lcrecord_stats [countof (lrecord_implementations_table)]; |
| 428 | 3443 |
| 3444 static void | |
| 442 | 3445 tick_lcrecord_stats (const struct lrecord_header *h, int free_p) |
| 428 | 3446 { |
| 647 | 3447 int type_index = h->type; |
| 428 | 3448 |
| 3024 | 3449 if (((struct old_lcrecord_header *) h)->free) |
| 428 | 3450 { |
| 442 | 3451 gc_checking_assert (!free_p); |
| 428 | 3452 lcrecord_stats[type_index].instances_on_free_list++; |
| 3453 } | |
| 3454 else | |
| 3455 { | |
| 1204 | 3456 Bytecount sz = detagged_lisp_object_size (h); |
| 3457 | |
| 428 | 3458 if (free_p) |
| 3459 { | |
| 3460 lcrecord_stats[type_index].instances_freed++; | |
| 3461 lcrecord_stats[type_index].bytes_freed += sz; | |
| 3462 } | |
| 3463 else | |
| 3464 { | |
| 3465 lcrecord_stats[type_index].instances_in_use++; | |
| 3466 lcrecord_stats[type_index].bytes_in_use += sz; | |
| 3467 } | |
| 3468 } | |
| 3469 } | |
| 3263 | 3470 #endif /* not NEW_GC */ |
| 428 | 3471 |
| 3472 | |
| 3263 | 3473 #ifndef NEW_GC |
| 428 | 3474 /* Free all unmarked records */ |
| 3475 static void | |
| 3024 | 3476 sweep_lcrecords_1 (struct old_lcrecord_header **prev, int *used) |
| 3477 { | |
| 3478 struct old_lcrecord_header *header; | |
| 428 | 3479 int num_used = 0; |
| 3480 /* int total_size = 0; */ | |
| 3481 | |
| 3482 xzero (lcrecord_stats); /* Reset all statistics to 0. */ | |
| 3483 | |
| 3484 /* First go through and call all the finalize methods. | |
| 3485 Then go through and free the objects. There used to | |
| 3486 be only one loop here, with the call to the finalizer | |
| 3487 occurring directly before the xfree() below. That | |
| 3488 is marginally faster but much less safe -- if the | |
| 3489 finalize method for an object needs to reference any | |
| 3490 other objects contained within it (and many do), | |
| 3491 we could easily be screwed by having already freed that | |
| 3492 other object. */ | |
| 3493 | |
| 3494 for (header = *prev; header; header = header->next) | |
| 3495 { | |
| 3496 struct lrecord_header *h = &(header->lheader); | |
| 442 | 3497 |
| 3498 GC_CHECK_LHEADER_INVARIANTS (h); | |
| 3499 | |
| 3500 if (! MARKED_RECORD_HEADER_P (h) && ! header->free) | |
| 428 | 3501 { |
| 3502 if (LHEADER_IMPLEMENTATION (h)->finalizer) | |
| 3503 LHEADER_IMPLEMENTATION (h)->finalizer (h, 0); | |
| 3504 } | |
| 3505 } | |
| 3506 | |
| 3507 for (header = *prev; header; ) | |
| 3508 { | |
| 3509 struct lrecord_header *h = &(header->lheader); | |
| 442 | 3510 if (MARKED_RECORD_HEADER_P (h)) |
| 428 | 3511 { |
| 442 | 3512 if (! C_READONLY_RECORD_HEADER_P (h)) |
| 428 | 3513 UNMARK_RECORD_HEADER (h); |
| 3514 num_used++; | |
| 3515 /* total_size += n->implementation->size_in_bytes (h);*/ | |
| 440 | 3516 /* #### May modify header->next on a C_READONLY lcrecord */ |
| 428 | 3517 prev = &(header->next); |
| 3518 header = *prev; | |
| 3519 tick_lcrecord_stats (h, 0); | |
| 3520 } | |
| 3521 else | |
| 3522 { | |
| 3024 | 3523 struct old_lcrecord_header *next = header->next; |
| 428 | 3524 *prev = next; |
| 3525 tick_lcrecord_stats (h, 1); | |
| 3526 /* used to call finalizer right here. */ | |
| 3024 | 3527 xfree (header, struct old_lcrecord_header *); |
| 428 | 3528 header = next; |
| 3529 } | |
| 3530 } | |
| 3531 *used = num_used; | |
| 3532 /* *total = total_size; */ | |
| 3533 } | |
| 3534 | |
| 3535 /* And the Lord said: Thou shalt use the `c-backslash-region' command | |
| 3536 to make macros prettier. */ | |
| 3537 | |
| 3538 #ifdef ERROR_CHECK_GC | |
| 3539 | |
| 771 | 3540 #define SWEEP_FIXED_TYPE_BLOCK_1(typename, obj_type, lheader) \ |
| 428 | 3541 do { \ |
| 3542 struct typename##_block *SFTB_current; \ | |
| 3543 int SFTB_limit; \ | |
| 3544 int num_free = 0, num_used = 0; \ | |
| 3545 \ | |
| 444 | 3546 for (SFTB_current = current_##typename##_block, \ |
| 428 | 3547 SFTB_limit = current_##typename##_block_index; \ |
| 3548 SFTB_current; \ | |
| 3549 ) \ | |
| 3550 { \ | |
| 3551 int SFTB_iii; \ | |
| 3552 \ | |
| 3553 for (SFTB_iii = 0; SFTB_iii < SFTB_limit; SFTB_iii++) \ | |
| 3554 { \ | |
| 3555 obj_type *SFTB_victim = &(SFTB_current->block[SFTB_iii]); \ | |
| 3556 \ | |
| 454 | 3557 if (LRECORD_FREE_P (SFTB_victim)) \ |
| 428 | 3558 { \ |
| 3559 num_free++; \ | |
| 3560 } \ | |
| 3561 else if (C_READONLY_RECORD_HEADER_P (&SFTB_victim->lheader)) \ | |
| 3562 { \ | |
| 3563 num_used++; \ | |
| 3564 } \ | |
| 442 | 3565 else if (! MARKED_RECORD_HEADER_P (&SFTB_victim->lheader)) \ |
| 428 | 3566 { \ |
| 3567 num_free++; \ | |
| 3568 FREE_FIXED_TYPE (typename, obj_type, SFTB_victim); \ | |
| 3569 } \ | |
| 3570 else \ | |
| 3571 { \ | |
| 3572 num_used++; \ | |
| 3573 UNMARK_##typename (SFTB_victim); \ | |
| 3574 } \ | |
| 3575 } \ | |
| 3576 SFTB_current = SFTB_current->prev; \ | |
| 3577 SFTB_limit = countof (current_##typename##_block->block); \ | |
| 3578 } \ | |
| 3579 \ | |
| 3580 gc_count_num_##typename##_in_use = num_used; \ | |
| 3581 gc_count_num_##typename##_freelist = num_free; \ | |
| 3582 } while (0) | |
| 3583 | |
| 3584 #else /* !ERROR_CHECK_GC */ | |
| 3585 | |
| 771 | 3586 #define SWEEP_FIXED_TYPE_BLOCK_1(typename, obj_type, lheader) \ |
| 3587 do { \ | |
| 3588 struct typename##_block *SFTB_current; \ | |
| 3589 struct typename##_block **SFTB_prev; \ | |
| 3590 int SFTB_limit; \ | |
| 3591 int num_free = 0, num_used = 0; \ | |
| 3592 \ | |
| 3593 typename##_free_list = 0; \ | |
| 3594 \ | |
| 3595 for (SFTB_prev = ¤t_##typename##_block, \ | |
| 3596 SFTB_current = current_##typename##_block, \ | |
| 3597 SFTB_limit = current_##typename##_block_index; \ | |
| 3598 SFTB_current; \ | |
| 3599 ) \ | |
| 3600 { \ | |
| 3601 int SFTB_iii; \ | |
| 3602 int SFTB_empty = 1; \ | |
| 3603 Lisp_Free *SFTB_old_free_list = typename##_free_list; \ | |
| 3604 \ | |
| 3605 for (SFTB_iii = 0; SFTB_iii < SFTB_limit; SFTB_iii++) \ | |
| 3606 { \ | |
| 3607 obj_type *SFTB_victim = &(SFTB_current->block[SFTB_iii]); \ | |
| 3608 \ | |
| 3609 if (LRECORD_FREE_P (SFTB_victim)) \ | |
| 3610 { \ | |
| 3611 num_free++; \ | |
| 3612 PUT_FIXED_TYPE_ON_FREE_LIST (typename, obj_type, SFTB_victim); \ | |
| 3613 } \ | |
| 3614 else if (C_READONLY_RECORD_HEADER_P (&SFTB_victim->lheader)) \ | |
| 3615 { \ | |
| 3616 SFTB_empty = 0; \ | |
| 3617 num_used++; \ | |
| 3618 } \ | |
| 3619 else if (! MARKED_RECORD_HEADER_P (&SFTB_victim->lheader)) \ | |
| 3620 { \ | |
| 3621 num_free++; \ | |
| 3622 FREE_FIXED_TYPE (typename, obj_type, SFTB_victim); \ | |
| 3623 } \ | |
| 3624 else \ | |
| 3625 { \ | |
| 3626 SFTB_empty = 0; \ | |
| 3627 num_used++; \ | |
| 3628 UNMARK_##typename (SFTB_victim); \ | |
| 3629 } \ | |
| 3630 } \ | |
| 3631 if (!SFTB_empty) \ | |
| 3632 { \ | |
| 3633 SFTB_prev = &(SFTB_current->prev); \ | |
| 3634 SFTB_current = SFTB_current->prev; \ | |
| 3635 } \ | |
| 3636 else if (SFTB_current == current_##typename##_block \ | |
| 3637 && !SFTB_current->prev) \ | |
| 3638 { \ | |
| 3639 /* No real point in freeing sole allocation block */ \ | |
| 3640 break; \ | |
| 3641 } \ | |
| 3642 else \ | |
| 3643 { \ | |
| 3644 struct typename##_block *SFTB_victim_block = SFTB_current; \ | |
| 3645 if (SFTB_victim_block == current_##typename##_block) \ | |
| 3646 current_##typename##_block_index \ | |
| 3647 = countof (current_##typename##_block->block); \ | |
| 3648 SFTB_current = SFTB_current->prev; \ | |
| 3649 { \ | |
| 3650 *SFTB_prev = SFTB_current; \ | |
| 1726 | 3651 xfree (SFTB_victim_block, struct typename##_block *); \ |
| 771 | 3652 /* Restore free list to what it was before victim was swept */ \ |
| 3653 typename##_free_list = SFTB_old_free_list; \ | |
| 3654 num_free -= SFTB_limit; \ | |
| 3655 } \ | |
| 3656 } \ | |
| 3657 SFTB_limit = countof (current_##typename##_block->block); \ | |
| 3658 } \ | |
| 3659 \ | |
| 3660 gc_count_num_##typename##_in_use = num_used; \ | |
| 3661 gc_count_num_##typename##_freelist = num_free; \ | |
| 428 | 3662 } while (0) |
| 3663 | |
| 3664 #endif /* !ERROR_CHECK_GC */ | |
| 3665 | |
| 771 | 3666 #define SWEEP_FIXED_TYPE_BLOCK(typename, obj_type) \ |
| 3667 SWEEP_FIXED_TYPE_BLOCK_1 (typename, obj_type, lheader) | |
| 3668 | |
| 3263 | 3669 #endif /* not NEW_GC */ |
| 2720 | 3670 |
| 428 | 3671 |
| 3263 | 3672 #ifndef NEW_GC |
| 428 | 3673 static void |
| 3674 sweep_conses (void) | |
| 3675 { | |
| 3676 #define UNMARK_cons(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
| 3677 #define ADDITIONAL_FREE_cons(ptr) | |
| 3678 | |
| 440 | 3679 SWEEP_FIXED_TYPE_BLOCK (cons, Lisp_Cons); |
| 428 | 3680 } |
| 3263 | 3681 #endif /* not NEW_GC */ |
| 428 | 3682 |
| 3683 /* Explicitly free a cons cell. */ | |
| 3684 void | |
| 853 | 3685 free_cons (Lisp_Object cons) |
| 428 | 3686 { |
| 3263 | 3687 #ifndef NEW_GC /* to avoid compiler warning */ |
| 853 | 3688 Lisp_Cons *ptr = XCONS (cons); |
| 3263 | 3689 #endif /* not NEW_GC */ |
| 853 | 3690 |
| 428 | 3691 #ifdef ERROR_CHECK_GC |
| 3263 | 3692 #ifdef NEW_GC |
| 2720 | 3693 Lisp_Cons *ptr = XCONS (cons); |
| 3263 | 3694 #endif /* NEW_GC */ |
| 428 | 3695 /* If the CAR is not an int, then it will be a pointer, which will |
| 3696 always be four-byte aligned. If this cons cell has already been | |
| 3697 placed on the free list, however, its car will probably contain | |
| 3698 a chain pointer to the next cons on the list, which has cleverly | |
| 3699 had all its 0's and 1's inverted. This allows for a quick | |
| 1204 | 3700 check to make sure we're not freeing something already freed. |
| 3701 | |
| 3702 NOTE: This check may not be necessary. Freeing an object sets its | |
| 3703 type to lrecord_type_free, which will trip up the XCONS() above -- as | |
| 3704 well as a check in FREE_FIXED_TYPE(). */ | |
| 853 | 3705 if (POINTER_TYPE_P (XTYPE (cons_car (ptr)))) |
| 3706 ASSERT_VALID_POINTER (XPNTR (cons_car (ptr))); | |
| 428 | 3707 #endif /* ERROR_CHECK_GC */ |
| 3708 | |
| 3263 | 3709 #ifdef NEW_GC |
| 2720 | 3710 free_lrecord (cons); |
| 3263 | 3711 #else /* not NEW_GC */ |
| 440 | 3712 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (cons, Lisp_Cons, ptr); |
| 3263 | 3713 #endif /* not NEW_GC */ |
| 428 | 3714 } |
| 3715 | |
| 3716 /* explicitly free a list. You **must make sure** that you have | |
| 3717 created all the cons cells that make up this list and that there | |
| 3718 are no pointers to any of these cons cells anywhere else. If there | |
| 3719 are, you will lose. */ | |
| 3720 | |
| 3721 void | |
| 3722 free_list (Lisp_Object list) | |
| 3723 { | |
| 3724 Lisp_Object rest, next; | |
| 3725 | |
| 3726 for (rest = list; !NILP (rest); rest = next) | |
| 3727 { | |
| 3728 next = XCDR (rest); | |
| 853 | 3729 free_cons (rest); |
| 428 | 3730 } |
| 3731 } | |
| 3732 | |
| 3733 /* explicitly free an alist. You **must make sure** that you have | |
| 3734 created all the cons cells that make up this alist and that there | |
| 3735 are no pointers to any of these cons cells anywhere else. If there | |
| 3736 are, you will lose. */ | |
| 3737 | |
| 3738 void | |
| 3739 free_alist (Lisp_Object alist) | |
| 3740 { | |
| 3741 Lisp_Object rest, next; | |
| 3742 | |
| 3743 for (rest = alist; !NILP (rest); rest = next) | |
| 3744 { | |
| 3745 next = XCDR (rest); | |
| 853 | 3746 free_cons (XCAR (rest)); |
| 3747 free_cons (rest); | |
| 428 | 3748 } |
| 3749 } | |
| 3750 | |
| 3263 | 3751 #ifndef NEW_GC |
| 428 | 3752 static void |
| 3753 sweep_compiled_functions (void) | |
| 3754 { | |
| 3755 #define UNMARK_compiled_function(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
| 945 | 3756 #define ADDITIONAL_FREE_compiled_function(ptr) \ |
| 1726 | 3757 if (ptr->args_in_array) xfree (ptr->args, Lisp_Object *) |
| 428 | 3758 |
| 3759 SWEEP_FIXED_TYPE_BLOCK (compiled_function, Lisp_Compiled_Function); | |
| 3760 } | |
| 3761 | |
| 3762 static void | |
| 3763 sweep_floats (void) | |
| 3764 { | |
| 3765 #define UNMARK_float(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
| 3766 #define ADDITIONAL_FREE_float(ptr) | |
| 3767 | |
| 440 | 3768 SWEEP_FIXED_TYPE_BLOCK (float, Lisp_Float); |
| 428 | 3769 } |
| 3770 | |
| 1983 | 3771 #ifdef HAVE_BIGNUM |
| 3772 static void | |
| 3773 sweep_bignums (void) | |
| 3774 { | |
| 3775 #define UNMARK_bignum(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
| 3776 #define ADDITIONAL_FREE_bignum(ptr) bignum_fini (ptr->data) | |
| 3777 | |
| 3778 SWEEP_FIXED_TYPE_BLOCK (bignum, Lisp_Bignum); | |
| 3779 } | |
| 3780 #endif /* HAVE_BIGNUM */ | |
| 3781 | |
| 3782 #ifdef HAVE_RATIO | |
| 3783 static void | |
| 3784 sweep_ratios (void) | |
| 3785 { | |
| 3786 #define UNMARK_ratio(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
| 3787 #define ADDITIONAL_FREE_ratio(ptr) ratio_fini (ptr->data) | |
| 3788 | |
| 3789 SWEEP_FIXED_TYPE_BLOCK (ratio, Lisp_Ratio); | |
| 3790 } | |
| 3791 #endif /* HAVE_RATIO */ | |
| 3792 | |
| 3793 #ifdef HAVE_BIGFLOAT | |
| 3794 static void | |
| 3795 sweep_bigfloats (void) | |
| 3796 { | |
| 3797 #define UNMARK_bigfloat(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
| 3798 #define ADDITIONAL_FREE_bigfloat(ptr) bigfloat_fini (ptr->bf) | |
| 3799 | |
| 3800 SWEEP_FIXED_TYPE_BLOCK (bigfloat, Lisp_Bigfloat); | |
| 3801 } | |
| 3802 #endif | |
| 3803 | |
| 428 | 3804 static void |
| 3805 sweep_symbols (void) | |
| 3806 { | |
| 3807 #define UNMARK_symbol(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
| 3808 #define ADDITIONAL_FREE_symbol(ptr) | |
| 3809 | |
| 440 | 3810 SWEEP_FIXED_TYPE_BLOCK (symbol, Lisp_Symbol); |
| 428 | 3811 } |
| 3812 | |
| 3813 static void | |
| 3814 sweep_extents (void) | |
| 3815 { | |
| 3816 #define UNMARK_extent(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
| 3817 #define ADDITIONAL_FREE_extent(ptr) | |
| 3818 | |
| 3819 SWEEP_FIXED_TYPE_BLOCK (extent, struct extent); | |
| 3820 } | |
| 3821 | |
| 3822 static void | |
| 3823 sweep_events (void) | |
| 3824 { | |
| 3825 #define UNMARK_event(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
| 3826 #define ADDITIONAL_FREE_event(ptr) | |
| 3827 | |
| 440 | 3828 SWEEP_FIXED_TYPE_BLOCK (event, Lisp_Event); |
| 428 | 3829 } |
| 3263 | 3830 #endif /* not NEW_GC */ |
| 428 | 3831 |
| 1204 | 3832 #ifdef EVENT_DATA_AS_OBJECTS |
| 934 | 3833 |
| 3263 | 3834 #ifndef NEW_GC |
| 934 | 3835 static void |
| 3836 sweep_key_data (void) | |
| 3837 { | |
| 3838 #define UNMARK_key_data(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
| 3839 #define ADDITIONAL_FREE_key_data(ptr) | |
| 3840 | |
| 3841 SWEEP_FIXED_TYPE_BLOCK (key_data, Lisp_Key_Data); | |
| 3842 } | |
| 3263 | 3843 #endif /* not NEW_GC */ |
| 934 | 3844 |
| 1204 | 3845 void |
| 3846 free_key_data (Lisp_Object ptr) | |
| 3847 { | |
| 3263 | 3848 #ifdef NEW_GC |
| 2720 | 3849 free_lrecord (ptr); |
| 3263 | 3850 #else /* not NEW_GC */ |
| 1204 | 3851 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (key_data, Lisp_Key_Data, XKEY_DATA (ptr)); |
| 3263 | 3852 #endif /* not NEW_GC */ |
| 2720 | 3853 } |
| 3854 | |
| 3263 | 3855 #ifndef NEW_GC |
| 934 | 3856 static void |
| 3857 sweep_button_data (void) | |
| 3858 { | |
| 3859 #define UNMARK_button_data(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
| 3860 #define ADDITIONAL_FREE_button_data(ptr) | |
| 3861 | |
| 3862 SWEEP_FIXED_TYPE_BLOCK (button_data, Lisp_Button_Data); | |
| 3863 } | |
| 3263 | 3864 #endif /* not NEW_GC */ |
| 934 | 3865 |
| 1204 | 3866 void |
| 3867 free_button_data (Lisp_Object ptr) | |
| 3868 { | |
| 3263 | 3869 #ifdef NEW_GC |
| 2720 | 3870 free_lrecord (ptr); |
| 3263 | 3871 #else /* not NEW_GC */ |
| 1204 | 3872 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (button_data, Lisp_Button_Data, XBUTTON_DATA (ptr)); |
| 3263 | 3873 #endif /* not NEW_GC */ |
| 2720 | 3874 } |
| 3875 | |
| 3263 | 3876 #ifndef NEW_GC |
| 934 | 3877 static void |
| 3878 sweep_motion_data (void) | |
| 3879 { | |
| 3880 #define UNMARK_motion_data(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
| 3881 #define ADDITIONAL_FREE_motion_data(ptr) | |
| 3882 | |
| 3883 SWEEP_FIXED_TYPE_BLOCK (motion_data, Lisp_Motion_Data); | |
| 3884 } | |
| 3263 | 3885 #endif /* not NEW_GC */ |
| 934 | 3886 |
| 1204 | 3887 void |
| 3888 free_motion_data (Lisp_Object ptr) | |
| 3889 { | |
| 3263 | 3890 #ifdef NEW_GC |
| 2720 | 3891 free_lrecord (ptr); |
| 3263 | 3892 #else /* not NEW_GC */ |
| 1204 | 3893 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (motion_data, Lisp_Motion_Data, XMOTION_DATA (ptr)); |
| 3263 | 3894 #endif /* not NEW_GC */ |
| 2720 | 3895 } |
| 3896 | |
| 3263 | 3897 #ifndef NEW_GC |
| 934 | 3898 static void |
| 3899 sweep_process_data (void) | |
| 3900 { | |
| 3901 #define UNMARK_process_data(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
| 3902 #define ADDITIONAL_FREE_process_data(ptr) | |
| 3903 | |
| 3904 SWEEP_FIXED_TYPE_BLOCK (process_data, Lisp_Process_Data); | |
| 3905 } | |
| 3263 | 3906 #endif /* not NEW_GC */ |
| 934 | 3907 |
| 1204 | 3908 void |
| 3909 free_process_data (Lisp_Object ptr) | |
| 3910 { | |
| 3263 | 3911 #ifdef NEW_GC |
| 2720 | 3912 free_lrecord (ptr); |
| 3263 | 3913 #else /* not NEW_GC */ |
| 1204 | 3914 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (process_data, Lisp_Process_Data, XPROCESS_DATA (ptr)); |
| 3263 | 3915 #endif /* not NEW_GC */ |
| 2720 | 3916 } |
| 3917 | |
| 3263 | 3918 #ifndef NEW_GC |
| 934 | 3919 static void |
| 3920 sweep_timeout_data (void) | |
| 3921 { | |
| 3922 #define UNMARK_timeout_data(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
| 3923 #define ADDITIONAL_FREE_timeout_data(ptr) | |
| 3924 | |
| 3925 SWEEP_FIXED_TYPE_BLOCK (timeout_data, Lisp_Timeout_Data); | |
| 3926 } | |
| 3263 | 3927 #endif /* not NEW_GC */ |
| 934 | 3928 |
| 1204 | 3929 void |
| 3930 free_timeout_data (Lisp_Object ptr) | |
| 3931 { | |
| 3263 | 3932 #ifdef NEW_GC |
| 2720 | 3933 free_lrecord (ptr); |
| 3263 | 3934 #else /* not NEW_GC */ |
| 1204 | 3935 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (timeout_data, Lisp_Timeout_Data, XTIMEOUT_DATA (ptr)); |
| 3263 | 3936 #endif /* not NEW_GC */ |
| 2720 | 3937 } |
| 3938 | |
| 3263 | 3939 #ifndef NEW_GC |
| 934 | 3940 static void |
| 3941 sweep_magic_data (void) | |
| 3942 { | |
| 3943 #define UNMARK_magic_data(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
| 3944 #define ADDITIONAL_FREE_magic_data(ptr) | |
| 3945 | |
| 3946 SWEEP_FIXED_TYPE_BLOCK (magic_data, Lisp_Magic_Data); | |
| 3947 } | |
| 3263 | 3948 #endif /* not NEW_GC */ |
| 934 | 3949 |
| 1204 | 3950 void |
| 3951 free_magic_data (Lisp_Object ptr) | |
| 3952 { | |
| 3263 | 3953 #ifdef NEW_GC |
| 2720 | 3954 free_lrecord (ptr); |
| 3263 | 3955 #else /* not NEW_GC */ |
| 1204 | 3956 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (magic_data, Lisp_Magic_Data, XMAGIC_DATA (ptr)); |
| 3263 | 3957 #endif /* not NEW_GC */ |
| 2720 | 3958 } |
| 3959 | |
| 3263 | 3960 #ifndef NEW_GC |
| 934 | 3961 static void |
| 3962 sweep_magic_eval_data (void) | |
| 3963 { | |
| 3964 #define UNMARK_magic_eval_data(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
| 3965 #define ADDITIONAL_FREE_magic_eval_data(ptr) | |
| 3966 | |
| 3967 SWEEP_FIXED_TYPE_BLOCK (magic_eval_data, Lisp_Magic_Eval_Data); | |
| 3968 } | |
| 3263 | 3969 #endif /* not NEW_GC */ |
| 934 | 3970 |
| 1204 | 3971 void |
| 3972 free_magic_eval_data (Lisp_Object ptr) | |
| 3973 { | |
| 3263 | 3974 #ifdef NEW_GC |
| 2720 | 3975 free_lrecord (ptr); |
| 3263 | 3976 #else /* not NEW_GC */ |
| 1204 | 3977 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (magic_eval_data, Lisp_Magic_Eval_Data, XMAGIC_EVAL_DATA (ptr)); |
| 3263 | 3978 #endif /* not NEW_GC */ |
| 2720 | 3979 } |
| 3980 | |
| 3263 | 3981 #ifndef NEW_GC |
| 934 | 3982 static void |
| 3983 sweep_eval_data (void) | |
| 3984 { | |
| 3985 #define UNMARK_eval_data(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
| 3986 #define ADDITIONAL_FREE_eval_data(ptr) | |
| 3987 | |
| 3988 SWEEP_FIXED_TYPE_BLOCK (eval_data, Lisp_Eval_Data); | |
| 3989 } | |
| 3263 | 3990 #endif /* not NEW_GC */ |
| 934 | 3991 |
| 1204 | 3992 void |
| 3993 free_eval_data (Lisp_Object ptr) | |
| 3994 { | |
| 3263 | 3995 #ifdef NEW_GC |
| 2720 | 3996 free_lrecord (ptr); |
| 3263 | 3997 #else /* not NEW_GC */ |
| 1204 | 3998 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (eval_data, Lisp_Eval_Data, XEVAL_DATA (ptr)); |
| 3263 | 3999 #endif /* not NEW_GC */ |
| 2720 | 4000 } |
| 4001 | |
| 3263 | 4002 #ifndef NEW_GC |
| 934 | 4003 static void |
| 4004 sweep_misc_user_data (void) | |
| 4005 { | |
| 4006 #define UNMARK_misc_user_data(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
| 4007 #define ADDITIONAL_FREE_misc_user_data(ptr) | |
| 4008 | |
| 4009 SWEEP_FIXED_TYPE_BLOCK (misc_user_data, Lisp_Misc_User_Data); | |
| 4010 } | |
| 3263 | 4011 #endif /* not NEW_GC */ |
| 934 | 4012 |
| 1204 | 4013 void |
| 4014 free_misc_user_data (Lisp_Object ptr) | |
| 4015 { | |
| 3263 | 4016 #ifdef NEW_GC |
| 2720 | 4017 free_lrecord (ptr); |
| 3263 | 4018 #else /* not NEW_GC */ |
| 1204 | 4019 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (misc_user_data, Lisp_Misc_User_Data, XMISC_USER_DATA (ptr)); |
| 3263 | 4020 #endif /* not NEW_GC */ |
| 1204 | 4021 } |
| 4022 | |
| 4023 #endif /* EVENT_DATA_AS_OBJECTS */ | |
| 934 | 4024 |
| 3263 | 4025 #ifndef NEW_GC |
| 428 | 4026 static void |
| 4027 sweep_markers (void) | |
| 4028 { | |
| 4029 #define UNMARK_marker(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
| 4030 #define ADDITIONAL_FREE_marker(ptr) \ | |
| 4031 do { Lisp_Object tem; \ | |
| 793 | 4032 tem = wrap_marker (ptr); \ |
| 428 | 4033 unchain_marker (tem); \ |
| 4034 } while (0) | |
| 4035 | |
| 440 | 4036 SWEEP_FIXED_TYPE_BLOCK (marker, Lisp_Marker); |
| 428 | 4037 } |
| 3263 | 4038 #endif /* not NEW_GC */ |
| 428 | 4039 |
| 4040 /* Explicitly free a marker. */ | |
| 4041 void | |
| 1204 | 4042 free_marker (Lisp_Object ptr) |
| 428 | 4043 { |
| 3263 | 4044 #ifdef NEW_GC |
| 2720 | 4045 free_lrecord (ptr); |
| 3263 | 4046 #else /* not NEW_GC */ |
| 1204 | 4047 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (marker, Lisp_Marker, XMARKER (ptr)); |
| 3263 | 4048 #endif /* not NEW_GC */ |
| 428 | 4049 } |
| 4050 | |
| 4051 | |
| 4052 #if defined (MULE) && defined (VERIFY_STRING_CHARS_INTEGRITY) | |
| 4053 | |
| 4054 static void | |
| 4055 verify_string_chars_integrity (void) | |
| 4056 { | |
| 4057 struct string_chars_block *sb; | |
| 4058 | |
| 4059 /* Scan each existing string block sequentially, string by string. */ | |
| 4060 for (sb = first_string_chars_block; sb; sb = sb->next) | |
| 4061 { | |
| 4062 int pos = 0; | |
| 4063 /* POS is the index of the next string in the block. */ | |
| 4064 while (pos < sb->pos) | |
| 4065 { | |
| 4066 struct string_chars *s_chars = | |
| 4067 (struct string_chars *) &(sb->string_chars[pos]); | |
| 438 | 4068 Lisp_String *string; |
| 428 | 4069 int size; |
| 4070 int fullsize; | |
| 4071 | |
| 454 | 4072 /* If the string_chars struct is marked as free (i.e. the |
| 4073 STRING pointer is NULL) then this is an unused chunk of | |
| 4074 string storage. (See below.) */ | |
| 4075 | |
| 4076 if (STRING_CHARS_FREE_P (s_chars)) | |
| 428 | 4077 { |
| 4078 fullsize = ((struct unused_string_chars *) s_chars)->fullsize; | |
| 4079 pos += fullsize; | |
| 4080 continue; | |
| 4081 } | |
| 4082 | |
| 4083 string = s_chars->string; | |
| 4084 /* Must be 32-bit aligned. */ | |
| 4085 assert ((((int) string) & 3) == 0); | |
| 4086 | |
| 793 | 4087 size = string->size_; |
| 428 | 4088 fullsize = STRING_FULLSIZE (size); |
| 4089 | |
| 4090 assert (!BIG_STRING_FULLSIZE_P (fullsize)); | |
| 2720 | 4091 assert (XSTRING_DATA (string) == s_chars->chars); |
| 428 | 4092 pos += fullsize; |
| 4093 } | |
| 4094 assert (pos == sb->pos); | |
| 4095 } | |
| 4096 } | |
| 4097 | |
| 1204 | 4098 #endif /* defined (MULE) && defined (VERIFY_STRING_CHARS_INTEGRITY) */ |
| 428 | 4099 |
| 3092 | 4100 #ifndef NEW_GC |
| 428 | 4101 /* Compactify string chars, relocating the reference to each -- |
| 4102 free any empty string_chars_block we see. */ | |
| 3092 | 4103 void |
| 428 | 4104 compact_string_chars (void) |
| 4105 { | |
| 4106 struct string_chars_block *to_sb = first_string_chars_block; | |
| 4107 int to_pos = 0; | |
| 4108 struct string_chars_block *from_sb; | |
| 4109 | |
| 4110 /* Scan each existing string block sequentially, string by string. */ | |
| 4111 for (from_sb = first_string_chars_block; from_sb; from_sb = from_sb->next) | |
| 4112 { | |
| 4113 int from_pos = 0; | |
| 4114 /* FROM_POS is the index of the next string in the block. */ | |
| 4115 while (from_pos < from_sb->pos) | |
| 4116 { | |
| 4117 struct string_chars *from_s_chars = | |
| 4118 (struct string_chars *) &(from_sb->string_chars[from_pos]); | |
| 4119 struct string_chars *to_s_chars; | |
| 438 | 4120 Lisp_String *string; |
| 428 | 4121 int size; |
| 4122 int fullsize; | |
| 4123 | |
| 454 | 4124 /* If the string_chars struct is marked as free (i.e. the |
| 4125 STRING pointer is NULL) then this is an unused chunk of | |
| 4126 string storage. This happens under Mule when a string's | |
| 4127 size changes in such a way that its fullsize changes. | |
| 4128 (Strings can change size because a different-length | |
| 4129 character can be substituted for another character.) | |
| 4130 In this case, after the bogus string pointer is the | |
| 4131 "fullsize" of this entry, i.e. how many bytes to skip. */ | |
| 4132 | |
| 4133 if (STRING_CHARS_FREE_P (from_s_chars)) | |
| 428 | 4134 { |
| 4135 fullsize = ((struct unused_string_chars *) from_s_chars)->fullsize; | |
| 4136 from_pos += fullsize; | |
| 4137 continue; | |
| 4138 } | |
| 4139 | |
| 4140 string = from_s_chars->string; | |
| 1204 | 4141 gc_checking_assert (!(LRECORD_FREE_P (string))); |
| 428 | 4142 |
| 793 | 4143 size = string->size_; |
| 428 | 4144 fullsize = STRING_FULLSIZE (size); |
| 4145 | |
| 442 | 4146 gc_checking_assert (! BIG_STRING_FULLSIZE_P (fullsize)); |
| 428 | 4147 |
| 4148 /* Just skip it if it isn't marked. */ | |
| 771 | 4149 if (! MARKED_RECORD_HEADER_P (&(string->u.lheader))) |
| 428 | 4150 { |
| 4151 from_pos += fullsize; | |
| 4152 continue; | |
| 4153 } | |
| 4154 | |
| 4155 /* If it won't fit in what's left of TO_SB, close TO_SB out | |
| 4156 and go on to the next string_chars_block. We know that TO_SB | |
| 4157 cannot advance past FROM_SB here since FROM_SB is large enough | |
| 4158 to currently contain this string. */ | |
| 4159 if ((to_pos + fullsize) > countof (to_sb->string_chars)) | |
| 4160 { | |
| 4161 to_sb->pos = to_pos; | |
| 4162 to_sb = to_sb->next; | |
| 4163 to_pos = 0; | |
| 4164 } | |
| 4165 | |
| 4166 /* Compute new address of this string | |
| 4167 and update TO_POS for the space being used. */ | |
| 4168 to_s_chars = (struct string_chars *) &(to_sb->string_chars[to_pos]); | |
| 4169 | |
| 4170 /* Copy the string_chars to the new place. */ | |
| 4171 if (from_s_chars != to_s_chars) | |
| 4172 memmove (to_s_chars, from_s_chars, fullsize); | |
| 4173 | |
| 4174 /* Relocate FROM_S_CHARS's reference */ | |
| 826 | 4175 set_lispstringp_data (string, &(to_s_chars->chars[0])); |
| 428 | 4176 |
| 4177 from_pos += fullsize; | |
| 4178 to_pos += fullsize; | |
| 4179 } | |
| 4180 } | |
| 4181 | |
| 4182 /* Set current to the last string chars block still used and | |
| 4183 free any that follow. */ | |
| 4184 { | |
| 4185 struct string_chars_block *victim; | |
| 4186 | |
| 4187 for (victim = to_sb->next; victim; ) | |
| 4188 { | |
| 4189 struct string_chars_block *next = victim->next; | |
| 1726 | 4190 xfree (victim, struct string_chars_block *); |
| 428 | 4191 victim = next; |
| 4192 } | |
| 4193 | |
| 4194 current_string_chars_block = to_sb; | |
| 4195 current_string_chars_block->pos = to_pos; | |
| 4196 current_string_chars_block->next = 0; | |
| 4197 } | |
| 4198 } | |
| 3092 | 4199 #endif /* not NEW_GC */ |
| 428 | 4200 |
| 3263 | 4201 #ifndef NEW_GC |
| 428 | 4202 #if 1 /* Hack to debug missing purecopy's */ |
| 4203 static int debug_string_purity; | |
| 4204 | |
| 4205 static void | |
| 793 | 4206 debug_string_purity_print (Lisp_Object p) |
| 428 | 4207 { |
| 4208 Charcount i; | |
| 826 | 4209 Charcount s = string_char_length (p); |
| 442 | 4210 stderr_out ("\""); |
| 428 | 4211 for (i = 0; i < s; i++) |
| 4212 { | |
| 867 | 4213 Ichar ch = string_ichar (p, i); |
| 428 | 4214 if (ch < 32 || ch >= 126) |
| 4215 stderr_out ("\\%03o", ch); | |
| 4216 else if (ch == '\\' || ch == '\"') | |
| 4217 stderr_out ("\\%c", ch); | |
| 4218 else | |
| 4219 stderr_out ("%c", ch); | |
| 4220 } | |
| 4221 stderr_out ("\"\n"); | |
| 4222 } | |
| 4223 #endif /* 1 */ | |
| 3263 | 4224 #endif /* not NEW_GC */ |
| 4225 | |
| 4226 #ifndef NEW_GC | |
| 428 | 4227 static void |
| 4228 sweep_strings (void) | |
| 4229 { | |
| 647 | 4230 int num_small_used = 0; |
| 4231 Bytecount num_small_bytes = 0, num_bytes = 0; | |
| 428 | 4232 int debug = debug_string_purity; |
| 4233 | |
| 793 | 4234 #define UNMARK_string(ptr) do { \ |
| 4235 Lisp_String *p = (ptr); \ | |
| 4236 Bytecount size = p->size_; \ | |
| 4237 UNMARK_RECORD_HEADER (&(p->u.lheader)); \ | |
| 4238 num_bytes += size; \ | |
| 4239 if (!BIG_STRING_SIZE_P (size)) \ | |
| 4240 { \ | |
| 4241 num_small_bytes += size; \ | |
| 4242 num_small_used++; \ | |
| 4243 } \ | |
| 4244 if (debug) \ | |
| 4245 debug_string_purity_print (wrap_string (p)); \ | |
| 438 | 4246 } while (0) |
| 4247 #define ADDITIONAL_FREE_string(ptr) do { \ | |
| 793 | 4248 Bytecount size = ptr->size_; \ |
| 438 | 4249 if (BIG_STRING_SIZE_P (size)) \ |
| 1726 | 4250 xfree (ptr->data_, Ibyte *); \ |
| 438 | 4251 } while (0) |
| 4252 | |
| 771 | 4253 SWEEP_FIXED_TYPE_BLOCK_1 (string, Lisp_String, u.lheader); |
| 428 | 4254 |
| 4255 gc_count_num_short_string_in_use = num_small_used; | |
| 4256 gc_count_string_total_size = num_bytes; | |
| 4257 gc_count_short_string_total_size = num_small_bytes; | |
| 4258 } | |
| 3263 | 4259 #endif /* not NEW_GC */ |
| 428 | 4260 |
| 3092 | 4261 #ifndef NEW_GC |
| 4262 void | |
| 4263 gc_sweep_1 (void) | |
| 428 | 4264 { |
| 4265 /* Free all unmarked records. Do this at the very beginning, | |
| 4266 before anything else, so that the finalize methods can safely | |
| 4267 examine items in the objects. sweep_lcrecords_1() makes | |
| 4268 sure to call all the finalize methods *before* freeing anything, | |
| 4269 to complete the safety. */ | |
| 4270 { | |
| 4271 int ignored; | |
| 4272 sweep_lcrecords_1 (&all_lcrecords, &ignored); | |
| 4273 } | |
| 4274 | |
| 4275 compact_string_chars (); | |
| 4276 | |
| 4277 /* Finalize methods below (called through the ADDITIONAL_FREE_foo | |
| 4278 macros) must be *extremely* careful to make sure they're not | |
| 4279 referencing freed objects. The only two existing finalize | |
| 4280 methods (for strings and markers) pass muster -- the string | |
| 4281 finalizer doesn't look at anything but its own specially- | |
| 4282 created block, and the marker finalizer only looks at live | |
| 4283 buffers (which will never be freed) and at the markers before | |
| 4284 and after it in the chain (which, by induction, will never be | |
| 4285 freed because if so, they would have already removed themselves | |
| 4286 from the chain). */ | |
| 4287 | |
| 4288 /* Put all unmarked strings on free list, free'ing the string chars | |
| 4289 of large unmarked strings */ | |
| 4290 sweep_strings (); | |
| 4291 | |
| 4292 /* Put all unmarked conses on free list */ | |
| 4293 sweep_conses (); | |
| 4294 | |
| 4295 /* Free all unmarked compiled-function objects */ | |
| 4296 sweep_compiled_functions (); | |
| 4297 | |
| 4298 /* Put all unmarked floats on free list */ | |
| 4299 sweep_floats (); | |
| 4300 | |
| 1983 | 4301 #ifdef HAVE_BIGNUM |
| 4302 /* Put all unmarked bignums on free list */ | |
| 4303 sweep_bignums (); | |
| 4304 #endif | |
| 4305 | |
| 4306 #ifdef HAVE_RATIO | |
| 4307 /* Put all unmarked ratios on free list */ | |
| 4308 sweep_ratios (); | |
| 4309 #endif | |
| 4310 | |
| 4311 #ifdef HAVE_BIGFLOAT | |
| 4312 /* Put all unmarked bigfloats on free list */ | |
| 4313 sweep_bigfloats (); | |
| 4314 #endif | |
| 4315 | |
| 428 | 4316 /* Put all unmarked symbols on free list */ |
| 4317 sweep_symbols (); | |
| 4318 | |
| 4319 /* Put all unmarked extents on free list */ | |
| 4320 sweep_extents (); | |
| 4321 | |
| 4322 /* Put all unmarked markers on free list. | |
| 4323 Dechain each one first from the buffer into which it points. */ | |
| 4324 sweep_markers (); | |
| 4325 | |
| 4326 sweep_events (); | |
| 4327 | |
| 1204 | 4328 #ifdef EVENT_DATA_AS_OBJECTS |
| 934 | 4329 sweep_key_data (); |
| 4330 sweep_button_data (); | |
| 4331 sweep_motion_data (); | |
| 4332 sweep_process_data (); | |
| 4333 sweep_timeout_data (); | |
| 4334 sweep_magic_data (); | |
| 4335 sweep_magic_eval_data (); | |
| 4336 sweep_eval_data (); | |
| 4337 sweep_misc_user_data (); | |
| 1204 | 4338 #endif /* EVENT_DATA_AS_OBJECTS */ |
| 3263 | 4339 #endif /* not NEW_GC */ |
| 4340 | |
| 4341 #ifndef NEW_GC | |
| 428 | 4342 #ifdef PDUMP |
| 442 | 4343 pdump_objects_unmark (); |
| 428 | 4344 #endif |
| 4345 } | |
| 3092 | 4346 #endif /* not NEW_GC */ |
| 428 | 4347 |
| 4348 /* Clearing for disksave. */ | |
| 4349 | |
| 4350 void | |
| 4351 disksave_object_finalization (void) | |
| 4352 { | |
| 4353 /* It's important that certain information from the environment not get | |
| 4354 dumped with the executable (pathnames, environment variables, etc.). | |
| 4355 To make it easier to tell when this has happened with strings(1) we | |
| 4356 clear some known-to-be-garbage blocks of memory, so that leftover | |
| 4357 results of old evaluation don't look like potential problems. | |
| 4358 But first we set some notable variables to nil and do one more GC, | |
| 4359 to turn those strings into garbage. | |
| 440 | 4360 */ |
| 428 | 4361 |
| 4362 /* Yeah, this list is pretty ad-hoc... */ | |
| 4363 Vprocess_environment = Qnil; | |
| 771 | 4364 env_initted = 0; |
| 428 | 4365 Vexec_directory = Qnil; |
| 4366 Vdata_directory = Qnil; | |
| 4367 Vsite_directory = Qnil; | |
| 4368 Vdoc_directory = Qnil; | |
| 4369 Vexec_path = Qnil; | |
| 4370 Vload_path = Qnil; | |
| 4371 /* Vdump_load_path = Qnil; */ | |
| 4372 /* Release hash tables for locate_file */ | |
| 4373 Flocate_file_clear_hashing (Qt); | |
| 771 | 4374 uncache_home_directory (); |
| 776 | 4375 zero_out_command_line_status_vars (); |
| 872 | 4376 clear_default_devices (); |
| 428 | 4377 |
| 4378 #if defined(LOADHIST) && !(defined(LOADHIST_DUMPED) || \ | |
| 4379 defined(LOADHIST_BUILTIN)) | |
| 4380 Vload_history = Qnil; | |
| 4381 #endif | |
| 4382 Vshell_file_name = Qnil; | |
| 4383 | |
| 3092 | 4384 #ifdef NEW_GC |
| 4385 gc_full (); | |
| 4386 #else /* not NEW_GC */ | |
| 428 | 4387 garbage_collect_1 (); |
| 3092 | 4388 #endif /* not NEW_GC */ |
| 428 | 4389 |
| 4390 /* Run the disksave finalization methods of all live objects. */ | |
| 4391 disksave_object_finalization_1 (); | |
| 4392 | |
| 3092 | 4393 #ifndef NEW_GC |
| 428 | 4394 /* Zero out the uninitialized (really, unused) part of the containers |
| 4395 for the live strings. */ | |
| 4396 { | |
| 4397 struct string_chars_block *scb; | |
| 4398 for (scb = first_string_chars_block; scb; scb = scb->next) | |
| 4399 { | |
| 4400 int count = sizeof (scb->string_chars) - scb->pos; | |
| 4401 | |
| 4402 assert (count >= 0 && count < STRING_CHARS_BLOCK_SIZE); | |
| 440 | 4403 if (count != 0) |
| 4404 { | |
| 4405 /* from the block's fill ptr to the end */ | |
| 4406 memset ((scb->string_chars + scb->pos), 0, count); | |
| 4407 } | |
| 428 | 4408 } |
| 4409 } | |
| 3092 | 4410 #endif /* not NEW_GC */ |
| 428 | 4411 |
| 4412 /* There, that ought to be enough... */ | |
| 4413 | |
| 4414 } | |
| 4415 | |
| 2994 | 4416 #ifdef ALLOC_TYPE_STATS |
| 4417 | |
| 2720 | 4418 static Lisp_Object |
| 2994 | 4419 gc_plist_hack (const Ascbyte *name, EMACS_INT value, Lisp_Object tail) |
| 2720 | 4420 { |
| 4421 /* C doesn't have local functions (or closures, or GC, or readable syntax, | |
| 4422 or portable numeric datatypes, or bit-vectors, or characters, or | |
| 4423 arrays, or exceptions, or ...) */ | |
| 4424 return cons3 (intern (name), make_int (value), tail); | |
| 4425 } | |
| 2775 | 4426 |
| 2994 | 4427 static Lisp_Object |
| 4428 object_memory_usage_stats (int set_total_gc_usage) | |
| 2720 | 4429 { |
| 4430 Lisp_Object pl = Qnil; | |
| 4431 int i; | |
| 2994 | 4432 EMACS_INT tgu_val = 0; |
| 4433 | |
| 3263 | 4434 #ifdef NEW_GC |
| 2775 | 4435 |
| 3461 | 4436 for (i = 0; i < countof (lrecord_implementations_table); i++) |
| 2720 | 4437 { |
| 4438 if (lrecord_stats[i].instances_in_use != 0) | |
| 4439 { | |
| 4440 char buf [255]; | |
| 4441 const char *name = lrecord_implementations_table[i]->name; | |
| 4442 int len = strlen (name); | |
| 4443 | |
| 4444 if (lrecord_stats[i].bytes_in_use_including_overhead != | |
| 4445 lrecord_stats[i].bytes_in_use) | |
| 4446 { | |
| 4447 sprintf (buf, "%s-storage-including-overhead", name); | |
| 4448 pl = gc_plist_hack (buf, | |
| 4449 lrecord_stats[i] | |
| 4450 .bytes_in_use_including_overhead, | |
| 4451 pl); | |
| 4452 } | |
| 4453 | |
| 4454 sprintf (buf, "%s-storage", name); | |
| 4455 pl = gc_plist_hack (buf, | |
| 4456 lrecord_stats[i].bytes_in_use, | |
| 4457 pl); | |
| 2994 | 4458 tgu_val += lrecord_stats[i].bytes_in_use_including_overhead; |
| 2720 | 4459 |
| 4460 if (name[len-1] == 's') | |
| 4461 sprintf (buf, "%ses-used", name); | |
| 4462 else | |
| 4463 sprintf (buf, "%ss-used", name); | |
| 4464 pl = gc_plist_hack (buf, lrecord_stats[i].instances_in_use, pl); | |
| 4465 } | |
| 4466 } | |
| 2994 | 4467 |
| 3263 | 4468 #else /* not NEW_GC */ |
| 428 | 4469 |
| 4470 #define HACK_O_MATIC(type, name, pl) do { \ | |
| 2994 | 4471 EMACS_INT s = 0; \ |
| 428 | 4472 struct type##_block *x = current_##type##_block; \ |
| 4473 while (x) { s += sizeof (*x) + MALLOC_OVERHEAD; x = x->prev; } \ | |
| 2994 | 4474 tgu_val += s; \ |
| 428 | 4475 (pl) = gc_plist_hack ((name), s, (pl)); \ |
| 4476 } while (0) | |
| 4477 | |
| 442 | 4478 for (i = 0; i < lrecord_type_count; i++) |
| 428 | 4479 { |
| 4480 if (lcrecord_stats[i].bytes_in_use != 0 | |
| 4481 || lcrecord_stats[i].bytes_freed != 0 | |
| 4482 || lcrecord_stats[i].instances_on_free_list != 0) | |
| 4483 { | |
| 4484 char buf [255]; | |
| 442 | 4485 const char *name = lrecord_implementations_table[i]->name; |
| 428 | 4486 int len = strlen (name); |
| 4487 | |
| 4488 sprintf (buf, "%s-storage", name); | |
| 4489 pl = gc_plist_hack (buf, lcrecord_stats[i].bytes_in_use, pl); | |
| 2994 | 4490 tgu_val += lcrecord_stats[i].bytes_in_use; |
| 428 | 4491 /* Okay, simple pluralization check for `symbol-value-varalias' */ |
| 4492 if (name[len-1] == 's') | |
| 4493 sprintf (buf, "%ses-freed", name); | |
| 4494 else | |
| 4495 sprintf (buf, "%ss-freed", name); | |
| 4496 if (lcrecord_stats[i].instances_freed != 0) | |
| 4497 pl = gc_plist_hack (buf, lcrecord_stats[i].instances_freed, pl); | |
| 4498 if (name[len-1] == 's') | |
| 4499 sprintf (buf, "%ses-on-free-list", name); | |
| 4500 else | |
| 4501 sprintf (buf, "%ss-on-free-list", name); | |
| 4502 if (lcrecord_stats[i].instances_on_free_list != 0) | |
| 4503 pl = gc_plist_hack (buf, lcrecord_stats[i].instances_on_free_list, | |
| 4504 pl); | |
| 4505 if (name[len-1] == 's') | |
| 4506 sprintf (buf, "%ses-used", name); | |
| 4507 else | |
| 4508 sprintf (buf, "%ss-used", name); | |
| 4509 pl = gc_plist_hack (buf, lcrecord_stats[i].instances_in_use, pl); | |
| 4510 } | |
| 4511 } | |
| 4512 | |
| 4513 HACK_O_MATIC (extent, "extent-storage", pl); | |
| 4514 pl = gc_plist_hack ("extents-free", gc_count_num_extent_freelist, pl); | |
| 4515 pl = gc_plist_hack ("extents-used", gc_count_num_extent_in_use, pl); | |
| 4516 HACK_O_MATIC (event, "event-storage", pl); | |
| 4517 pl = gc_plist_hack ("events-free", gc_count_num_event_freelist, pl); | |
| 4518 pl = gc_plist_hack ("events-used", gc_count_num_event_in_use, pl); | |
| 4519 HACK_O_MATIC (marker, "marker-storage", pl); | |
| 4520 pl = gc_plist_hack ("markers-free", gc_count_num_marker_freelist, pl); | |
| 4521 pl = gc_plist_hack ("markers-used", gc_count_num_marker_in_use, pl); | |
| 4522 HACK_O_MATIC (float, "float-storage", pl); | |
| 4523 pl = gc_plist_hack ("floats-free", gc_count_num_float_freelist, pl); | |
| 4524 pl = gc_plist_hack ("floats-used", gc_count_num_float_in_use, pl); | |
| 1983 | 4525 #ifdef HAVE_BIGNUM |
| 4526 HACK_O_MATIC (bignum, "bignum-storage", pl); | |
| 4527 pl = gc_plist_hack ("bignums-free", gc_count_num_bignum_freelist, pl); | |
| 4528 pl = gc_plist_hack ("bignums-used", gc_count_num_bignum_in_use, pl); | |
| 4529 #endif /* HAVE_BIGNUM */ | |
| 4530 #ifdef HAVE_RATIO | |
| 4531 HACK_O_MATIC (ratio, "ratio-storage", pl); | |
| 4532 pl = gc_plist_hack ("ratios-free", gc_count_num_ratio_freelist, pl); | |
| 4533 pl = gc_plist_hack ("ratios-used", gc_count_num_ratio_in_use, pl); | |
| 4534 #endif /* HAVE_RATIO */ | |
| 4535 #ifdef HAVE_BIGFLOAT | |
| 4536 HACK_O_MATIC (bigfloat, "bigfloat-storage", pl); | |
| 4537 pl = gc_plist_hack ("bigfloats-free", gc_count_num_bigfloat_freelist, pl); | |
| 4538 pl = gc_plist_hack ("bigfloats-used", gc_count_num_bigfloat_in_use, pl); | |
| 4539 #endif /* HAVE_BIGFLOAT */ | |
| 428 | 4540 HACK_O_MATIC (string, "string-header-storage", pl); |
| 4541 pl = gc_plist_hack ("long-strings-total-length", | |
| 4542 gc_count_string_total_size | |
| 4543 - gc_count_short_string_total_size, pl); | |
| 4544 HACK_O_MATIC (string_chars, "short-string-storage", pl); | |
| 4545 pl = gc_plist_hack ("short-strings-total-length", | |
| 4546 gc_count_short_string_total_size, pl); | |
| 4547 pl = gc_plist_hack ("strings-free", gc_count_num_string_freelist, pl); | |
| 4548 pl = gc_plist_hack ("long-strings-used", | |
| 4549 gc_count_num_string_in_use | |
| 4550 - gc_count_num_short_string_in_use, pl); | |
| 4551 pl = gc_plist_hack ("short-strings-used", | |
| 4552 gc_count_num_short_string_in_use, pl); | |
| 4553 | |
| 4554 HACK_O_MATIC (compiled_function, "compiled-function-storage", pl); | |
| 4555 pl = gc_plist_hack ("compiled-functions-free", | |
| 4556 gc_count_num_compiled_function_freelist, pl); | |
| 4557 pl = gc_plist_hack ("compiled-functions-used", | |
| 4558 gc_count_num_compiled_function_in_use, pl); | |
| 4559 | |
| 4560 HACK_O_MATIC (symbol, "symbol-storage", pl); | |
| 4561 pl = gc_plist_hack ("symbols-free", gc_count_num_symbol_freelist, pl); | |
| 4562 pl = gc_plist_hack ("symbols-used", gc_count_num_symbol_in_use, pl); | |
| 4563 | |
| 4564 HACK_O_MATIC (cons, "cons-storage", pl); | |
| 4565 pl = gc_plist_hack ("conses-free", gc_count_num_cons_freelist, pl); | |
| 4566 pl = gc_plist_hack ("conses-used", gc_count_num_cons_in_use, pl); | |
| 4567 | |
| 2994 | 4568 #undef HACK_O_MATIC |
| 4569 | |
| 3263 | 4570 #endif /* NEW_GC */ |
| 2994 | 4571 |
| 4572 if (set_total_gc_usage) | |
| 4573 { | |
| 4574 total_gc_usage = tgu_val; | |
| 4575 total_gc_usage_set = 1; | |
| 4576 } | |
| 4577 | |
| 4578 return pl; | |
| 4579 } | |
| 4580 | |
| 4581 DEFUN("object-memory-usage-stats", Fobject_memory_usage_stats, 0, 0 ,"", /* | |
| 4582 Return statistics about memory usage of Lisp objects. | |
| 4583 */ | |
| 4584 ()) | |
| 4585 { | |
| 4586 return object_memory_usage_stats (0); | |
| 4587 } | |
| 4588 | |
| 4589 #endif /* ALLOC_TYPE_STATS */ | |
| 4590 | |
| 4591 /* Debugging aids. */ | |
| 4592 | |
| 4593 DEFUN ("garbage-collect", Fgarbage_collect, 0, 0, "", /* | |
| 4594 Reclaim storage for Lisp objects no longer needed. | |
| 4595 Return info on amount of space in use: | |
| 4596 ((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS) | |
| 4597 (USED-MARKERS . FREE-MARKERS) USED-STRING-CHARS USED-VECTOR-SLOTS | |
| 4598 PLIST) | |
| 4599 where `PLIST' is a list of alternating keyword/value pairs providing | |
| 4600 more detailed information. | |
| 4601 Garbage collection happens automatically if you cons more than | |
| 4602 `gc-cons-threshold' bytes of Lisp data since previous garbage collection. | |
| 4603 */ | |
| 4604 ()) | |
| 4605 { | |
| 4606 /* Record total usage for purposes of determining next GC */ | |
| 3092 | 4607 #ifdef NEW_GC |
| 4608 gc_full (); | |
| 4609 #else /* not NEW_GC */ | |
| 2994 | 4610 garbage_collect_1 (); |
| 3092 | 4611 #endif /* not NEW_GC */ |
| 2994 | 4612 |
| 4613 /* This will get set to 1, and total_gc_usage computed, as part of the | |
| 4614 call to object_memory_usage_stats() -- if ALLOC_TYPE_STATS is enabled. */ | |
| 4615 total_gc_usage_set = 0; | |
| 4616 #ifdef ALLOC_TYPE_STATS | |
| 428 | 4617 /* The things we do for backwards-compatibility */ |
| 3263 | 4618 #ifdef NEW_GC |
| 2994 | 4619 return |
| 4620 list6 | |
| 4621 (Fcons (make_int (lrecord_stats[lrecord_type_cons].instances_in_use), | |
| 4622 make_int (lrecord_stats[lrecord_type_cons] | |
| 4623 .bytes_in_use_including_overhead)), | |
| 4624 Fcons (make_int (lrecord_stats[lrecord_type_symbol].instances_in_use), | |
| 4625 make_int (lrecord_stats[lrecord_type_symbol] | |
| 4626 .bytes_in_use_including_overhead)), | |
| 4627 Fcons (make_int (lrecord_stats[lrecord_type_marker].instances_in_use), | |
| 4628 make_int (lrecord_stats[lrecord_type_marker] | |
| 4629 .bytes_in_use_including_overhead)), | |
| 4630 make_int (lrecord_stats[lrecord_type_string] | |
| 4631 .bytes_in_use_including_overhead), | |
| 4632 make_int (lrecord_stats[lrecord_type_vector] | |
| 4633 .bytes_in_use_including_overhead), | |
| 4634 object_memory_usage_stats (1)); | |
| 3263 | 4635 #else /* not NEW_GC */ |
| 428 | 4636 return |
| 4637 list6 (Fcons (make_int (gc_count_num_cons_in_use), | |
| 4638 make_int (gc_count_num_cons_freelist)), | |
| 4639 Fcons (make_int (gc_count_num_symbol_in_use), | |
| 4640 make_int (gc_count_num_symbol_freelist)), | |
| 4641 Fcons (make_int (gc_count_num_marker_in_use), | |
| 4642 make_int (gc_count_num_marker_freelist)), | |
| 4643 make_int (gc_count_string_total_size), | |
| 2994 | 4644 make_int (lcrecord_stats[lrecord_type_vector].bytes_in_use + |
| 4645 lcrecord_stats[lrecord_type_vector].bytes_freed), | |
| 4646 object_memory_usage_stats (1)); | |
| 3263 | 4647 #endif /* not NEW_GC */ |
| 2994 | 4648 #else /* not ALLOC_TYPE_STATS */ |
| 4649 return Qnil; | |
| 4650 #endif /* ALLOC_TYPE_STATS */ | |
| 4651 } | |
| 428 | 4652 |
| 4653 DEFUN ("consing-since-gc", Fconsing_since_gc, 0, 0, "", /* | |
| 4654 Return the number of bytes consed since the last garbage collection. | |
| 4655 \"Consed\" is a misnomer in that this actually counts allocation | |
| 4656 of all different kinds of objects, not just conses. | |
| 4657 | |
| 4658 If this value exceeds `gc-cons-threshold', a garbage collection happens. | |
| 4659 */ | |
| 4660 ()) | |
| 4661 { | |
| 4662 return make_int (consing_since_gc); | |
| 4663 } | |
| 4664 | |
| 440 | 4665 #if 0 |
| 444 | 4666 DEFUN ("memory-limit", Fmemory_limit, 0, 0, 0, /* |
| 801 | 4667 Return the address of the last byte XEmacs has allocated, divided by 1024. |
| 4668 This may be helpful in debugging XEmacs's memory usage. | |
| 428 | 4669 The value is divided by 1024 to make sure it will fit in a lisp integer. |
| 4670 */ | |
| 4671 ()) | |
| 4672 { | |
| 4673 return make_int ((EMACS_INT) sbrk (0) / 1024); | |
| 4674 } | |
| 440 | 4675 #endif |
| 428 | 4676 |
| 2994 | 4677 DEFUN ("total-memory-usage", Ftotal_memory_usage, 0, 0, 0, /* |
| 801 | 4678 Return the total number of bytes used by the data segment in XEmacs. |
| 4679 This may be helpful in debugging XEmacs's memory usage. | |
| 2994 | 4680 NOTE: This may or may not be accurate! It is hard to determine this |
| 4681 value in a system-independent fashion. On Windows, for example, the | |
| 4682 returned number tends to be much greater than reality. | |
| 801 | 4683 */ |
| 4684 ()) | |
| 4685 { | |
| 4686 return make_int (total_data_usage ()); | |
| 4687 } | |
| 4688 | |
| 2994 | 4689 #ifdef ALLOC_TYPE_STATS |
| 4690 DEFUN ("object-memory-usage", Fobject_memory_usage, 0, 0, 0, /* | |
| 4691 Return total number of bytes used for object storage in XEmacs. | |
| 4692 This may be helpful in debugging XEmacs's memory usage. | |
| 4693 See also `consing-since-gc' and `object-memory-usage-stats'. | |
| 4694 */ | |
| 4695 ()) | |
| 4696 { | |
| 4697 return make_int (total_gc_usage + consing_since_gc); | |
| 4698 } | |
| 4699 #endif /* ALLOC_TYPE_STATS */ | |
| 4700 | |
| 851 | 4701 void |
| 4702 recompute_funcall_allocation_flag (void) | |
| 4703 { | |
| 887 | 4704 funcall_allocation_flag = |
| 4705 need_to_garbage_collect || | |
| 4706 need_to_check_c_alloca || | |
| 4707 need_to_signal_post_gc; | |
| 851 | 4708 } |
| 4709 | |
| 428 | 4710 |
| 4711 int | |
| 4712 object_dead_p (Lisp_Object obj) | |
| 4713 { | |
| 4714 return ((BUFFERP (obj) && !BUFFER_LIVE_P (XBUFFER (obj))) || | |
| 4715 (FRAMEP (obj) && !FRAME_LIVE_P (XFRAME (obj))) || | |
| 4716 (WINDOWP (obj) && !WINDOW_LIVE_P (XWINDOW (obj))) || | |
| 4717 (DEVICEP (obj) && !DEVICE_LIVE_P (XDEVICE (obj))) || | |
| 4718 (CONSOLEP (obj) && !CONSOLE_LIVE_P (XCONSOLE (obj))) || | |
| 4719 (EVENTP (obj) && !EVENT_LIVE_P (XEVENT (obj))) || | |
| 4720 (EXTENTP (obj) && !EXTENT_LIVE_P (XEXTENT (obj)))); | |
| 4721 } | |
| 4722 | |
| 4723 #ifdef MEMORY_USAGE_STATS | |
| 4724 | |
| 4725 /* Attempt to determine the actual amount of space that is used for | |
| 4726 the block allocated starting at PTR, supposedly of size "CLAIMED_SIZE". | |
| 4727 | |
| 4728 It seems that the following holds: | |
| 4729 | |
| 4730 1. When using the old allocator (malloc.c): | |
| 4731 | |
| 4732 -- blocks are always allocated in chunks of powers of two. For | |
| 4733 each block, there is an overhead of 8 bytes if rcheck is not | |
| 4734 defined, 20 bytes if it is defined. In other words, a | |
| 4735 one-byte allocation needs 8 bytes of overhead for a total of | |
| 4736 9 bytes, and needs to have 16 bytes of memory chunked out for | |
| 4737 it. | |
| 4738 | |
| 4739 2. When using the new allocator (gmalloc.c): | |
| 4740 | |
| 4741 -- blocks are always allocated in chunks of powers of two up | |
| 4742 to 4096 bytes. Larger blocks are allocated in chunks of | |
| 4743 an integral multiple of 4096 bytes. The minimum block | |
| 4744 size is 2*sizeof (void *), or 16 bytes if SUNOS_LOCALTIME_BUG | |
| 4745 is defined. There is no per-block overhead, but there | |
| 4746 is an overhead of 3*sizeof (size_t) for each 4096 bytes | |
| 4747 allocated. | |
| 4748 | |
| 4749 3. When using the system malloc, anything goes, but they are | |
| 4750 generally slower and more space-efficient than the GNU | |
| 4751 allocators. One possibly reasonable assumption to make | |
| 4752 for want of better data is that sizeof (void *), or maybe | |
| 4753 2 * sizeof (void *), is required as overhead and that | |
| 4754 blocks are allocated in the minimum required size except | |
| 4755 that some minimum block size is imposed (e.g. 16 bytes). */ | |
| 4756 | |
| 665 | 4757 Bytecount |
| 2286 | 4758 malloced_storage_size (void *UNUSED (ptr), Bytecount claimed_size, |
| 428 | 4759 struct overhead_stats *stats) |
| 4760 { | |
| 665 | 4761 Bytecount orig_claimed_size = claimed_size; |
| 428 | 4762 |
|
4735
80d74fed5399
Remove "old" GNU malloc in src/malloc.c, and all references to it. Drop the
Jerry James <james@xemacs.org>
parents:
4693
diff
changeset
|
4763 #ifndef SYSTEM_MALLOC |
| 665 | 4764 if (claimed_size < (Bytecount) (2 * sizeof (void *))) |
| 428 | 4765 claimed_size = 2 * sizeof (void *); |
| 4766 # ifdef SUNOS_LOCALTIME_BUG | |
| 4767 if (claimed_size < 16) | |
| 4768 claimed_size = 16; | |
| 4769 # endif | |
| 4770 if (claimed_size < 4096) | |
| 4771 { | |
| 2260 | 4772 /* fxg: rename log->log2 to supress gcc3 shadow warning */ |
| 4773 int log2 = 1; | |
| 428 | 4774 |
| 4775 /* compute the log base two, more or less, then use it to compute | |
| 4776 the block size needed. */ | |
| 4777 claimed_size--; | |
| 4778 /* It's big, it's heavy, it's wood! */ | |
| 4779 while ((claimed_size /= 2) != 0) | |
| 2260 | 4780 ++log2; |
| 428 | 4781 claimed_size = 1; |
| 4782 /* It's better than bad, it's good! */ | |
| 2260 | 4783 while (log2 > 0) |
| 428 | 4784 { |
| 4785 claimed_size *= 2; | |
| 2260 | 4786 log2--; |
| 428 | 4787 } |
| 4788 /* We have to come up with some average about the amount of | |
| 4789 blocks used. */ | |
| 665 | 4790 if ((Bytecount) (rand () & 4095) < claimed_size) |
| 428 | 4791 claimed_size += 3 * sizeof (void *); |
| 4792 } | |
| 4793 else | |
| 4794 { | |
| 4795 claimed_size += 4095; | |
| 4796 claimed_size &= ~4095; | |
| 4797 claimed_size += (claimed_size / 4096) * 3 * sizeof (size_t); | |
| 4798 } | |
| 4799 | |
|
4735
80d74fed5399
Remove "old" GNU malloc in src/malloc.c, and all references to it. Drop the
Jerry James <james@xemacs.org>
parents:
4693
diff
changeset
|
4800 #else |
| 428 | 4801 |
| 4802 if (claimed_size < 16) | |
| 4803 claimed_size = 16; | |
| 4804 claimed_size += 2 * sizeof (void *); | |
| 4805 | |
|
4735
80d74fed5399
Remove "old" GNU malloc in src/malloc.c, and all references to it. Drop the
Jerry James <james@xemacs.org>
parents:
4693
diff
changeset
|
4806 #endif /* system allocator */ |
| 428 | 4807 |
| 4808 if (stats) | |
| 4809 { | |
| 4810 stats->was_requested += orig_claimed_size; | |
| 4811 stats->malloc_overhead += claimed_size - orig_claimed_size; | |
| 4812 } | |
| 4813 return claimed_size; | |
| 4814 } | |
| 4815 | |
| 3263 | 4816 #ifndef NEW_GC |
| 665 | 4817 Bytecount |
| 4818 fixed_type_block_overhead (Bytecount size) | |
| 428 | 4819 { |
| 665 | 4820 Bytecount per_block = TYPE_ALLOC_SIZE (cons, unsigned char); |
| 4821 Bytecount overhead = 0; | |
| 4822 Bytecount storage_size = malloced_storage_size (0, per_block, 0); | |
| 428 | 4823 while (size >= per_block) |
| 4824 { | |
| 4825 size -= per_block; | |
| 4826 overhead += sizeof (void *) + per_block - storage_size; | |
| 4827 } | |
| 4828 if (rand () % per_block < size) | |
| 4829 overhead += sizeof (void *) + per_block - storage_size; | |
| 4830 return overhead; | |
| 4831 } | |
| 3263 | 4832 #endif /* not NEW_GC */ |
| 428 | 4833 #endif /* MEMORY_USAGE_STATS */ |
| 4834 | |
| 4835 | |
| 4836 /* Initialization */ | |
| 771 | 4837 static void |
| 1204 | 4838 common_init_alloc_early (void) |
| 428 | 4839 { |
| 771 | 4840 #ifndef Qzero |
| 4841 Qzero = make_int (0); /* Only used if Lisp_Object is a union type */ | |
| 4842 #endif | |
| 4843 | |
| 4844 #ifndef Qnull_pointer | |
| 4845 /* C guarantees that Qnull_pointer will be initialized to all 0 bits, | |
| 4846 so the following is actually a no-op. */ | |
| 793 | 4847 Qnull_pointer = wrap_pointer_1 (0); |
| 771 | 4848 #endif |
| 4849 | |
| 3263 | 4850 #ifndef NEW_GC |
| 428 | 4851 breathing_space = 0; |
| 4852 all_lcrecords = 0; | |
| 3263 | 4853 #endif /* not NEW_GC */ |
| 428 | 4854 ignore_malloc_warnings = 1; |
| 4855 #ifdef DOUG_LEA_MALLOC | |
| 4856 mallopt (M_TRIM_THRESHOLD, 128*1024); /* trim threshold */ | |
| 4857 mallopt (M_MMAP_THRESHOLD, 64*1024); /* mmap threshold */ | |
| 4858 #if 0 /* Moved to emacs.c */ | |
| 4859 mallopt (M_MMAP_MAX, 64); /* max. number of mmap'ed areas */ | |
| 4860 #endif | |
| 4861 #endif | |
| 3092 | 4862 #ifndef NEW_GC |
| 2720 | 4863 init_string_chars_alloc (); |
| 428 | 4864 init_string_alloc (); |
| 4865 init_string_chars_alloc (); | |
| 4866 init_cons_alloc (); | |
| 4867 init_symbol_alloc (); | |
| 4868 init_compiled_function_alloc (); | |
| 4869 init_float_alloc (); | |
| 1983 | 4870 #ifdef HAVE_BIGNUM |
| 4871 init_bignum_alloc (); | |
| 4872 #endif | |
| 4873 #ifdef HAVE_RATIO | |
| 4874 init_ratio_alloc (); | |
| 4875 #endif | |
| 4876 #ifdef HAVE_BIGFLOAT | |
| 4877 init_bigfloat_alloc (); | |
| 4878 #endif | |
| 428 | 4879 init_marker_alloc (); |
| 4880 init_extent_alloc (); | |
| 4881 init_event_alloc (); | |
| 1204 | 4882 #ifdef EVENT_DATA_AS_OBJECTS |
| 934 | 4883 init_key_data_alloc (); |
| 4884 init_button_data_alloc (); | |
| 4885 init_motion_data_alloc (); | |
| 4886 init_process_data_alloc (); | |
| 4887 init_timeout_data_alloc (); | |
| 4888 init_magic_data_alloc (); | |
| 4889 init_magic_eval_data_alloc (); | |
| 4890 init_eval_data_alloc (); | |
| 4891 init_misc_user_data_alloc (); | |
| 1204 | 4892 #endif /* EVENT_DATA_AS_OBJECTS */ |
| 3263 | 4893 #endif /* not NEW_GC */ |
| 428 | 4894 |
| 4895 ignore_malloc_warnings = 0; | |
| 4896 | |
| 452 | 4897 if (staticpros_nodump) |
| 4898 Dynarr_free (staticpros_nodump); | |
| 4899 staticpros_nodump = Dynarr_new2 (Lisp_Object_ptr_dynarr, Lisp_Object *); | |
| 4900 Dynarr_resize (staticpros_nodump, 100); /* merely a small optimization */ | |
| 771 | 4901 #ifdef DEBUG_XEMACS |
| 4902 if (staticpro_nodump_names) | |
| 4903 Dynarr_free (staticpro_nodump_names); | |
| 4904 staticpro_nodump_names = Dynarr_new2 (char_ptr_dynarr, char *); | |
| 4905 Dynarr_resize (staticpro_nodump_names, 100); /* ditto */ | |
| 4906 #endif | |
| 428 | 4907 |
| 3263 | 4908 #ifdef NEW_GC |
| 2720 | 4909 mcpros = Dynarr_new2 (Lisp_Object_dynarr, Lisp_Object); |
| 4910 Dynarr_resize (mcpros, 1410); /* merely a small optimization */ | |
| 4911 dump_add_root_block_ptr (&mcpros, &mcpros_description); | |
| 4912 #ifdef DEBUG_XEMACS | |
| 4913 mcpro_names = Dynarr_new2 (char_ptr_dynarr, char *); | |
| 4914 Dynarr_resize (mcpro_names, 1410); /* merely a small optimization */ | |
| 4915 dump_add_root_block_ptr (&mcpro_names, &mcpro_names_description); | |
| 4916 #endif | |
| 3263 | 4917 #endif /* NEW_GC */ |
| 2720 | 4918 |
| 428 | 4919 consing_since_gc = 0; |
| 851 | 4920 need_to_check_c_alloca = 0; |
| 4921 funcall_allocation_flag = 0; | |
| 4922 funcall_alloca_count = 0; | |
| 814 | 4923 |
| 428 | 4924 lrecord_uid_counter = 259; |
| 3263 | 4925 #ifndef NEW_GC |
| 428 | 4926 debug_string_purity = 0; |
| 3263 | 4927 #endif /* not NEW_GC */ |
| 428 | 4928 |
| 800 | 4929 #ifdef ERROR_CHECK_TYPES |
| 428 | 4930 ERROR_ME.really_unlikely_name_to_have_accidentally_in_a_non_errb_structure = |
| 4931 666; | |
| 4932 ERROR_ME_NOT. | |
| 4933 really_unlikely_name_to_have_accidentally_in_a_non_errb_structure = 42; | |
| 4934 ERROR_ME_WARN. | |
| 4935 really_unlikely_name_to_have_accidentally_in_a_non_errb_structure = | |
| 4936 3333632; | |
| 793 | 4937 ERROR_ME_DEBUG_WARN. |
| 4938 really_unlikely_name_to_have_accidentally_in_a_non_errb_structure = | |
| 4939 8675309; | |
| 800 | 4940 #endif /* ERROR_CHECK_TYPES */ |
| 428 | 4941 } |
| 4942 | |
| 3263 | 4943 #ifndef NEW_GC |
| 771 | 4944 static void |
| 4945 init_lcrecord_lists (void) | |
| 4946 { | |
| 4947 int i; | |
| 4948 | |
| 4949 for (i = 0; i < countof (lrecord_implementations_table); i++) | |
| 4950 { | |
| 4951 all_lcrecord_lists[i] = Qzero; /* Qnil not yet set */ | |
| 4952 staticpro_nodump (&all_lcrecord_lists[i]); | |
| 4953 } | |
| 4954 } | |
| 3263 | 4955 #endif /* not NEW_GC */ |
| 771 | 4956 |
| 4957 void | |
| 1204 | 4958 init_alloc_early (void) |
| 771 | 4959 { |
| 1204 | 4960 #if defined (__cplusplus) && defined (ERROR_CHECK_GC) |
| 4961 static struct gcpro initial_gcpro; | |
| 4962 | |
| 4963 initial_gcpro.next = 0; | |
| 4964 initial_gcpro.var = &Qnil; | |
| 4965 initial_gcpro.nvars = 1; | |
| 4966 gcprolist = &initial_gcpro; | |
| 4967 #else | |
| 4968 gcprolist = 0; | |
| 4969 #endif /* defined (__cplusplus) && defined (ERROR_CHECK_GC) */ | |
| 4970 } | |
| 4971 | |
| 4972 void | |
| 4973 reinit_alloc_early (void) | |
| 4974 { | |
| 4975 common_init_alloc_early (); | |
| 3263 | 4976 #ifndef NEW_GC |
| 771 | 4977 init_lcrecord_lists (); |
| 3263 | 4978 #endif /* not NEW_GC */ |
| 771 | 4979 } |
| 4980 | |
| 428 | 4981 void |
| 4982 init_alloc_once_early (void) | |
| 4983 { | |
| 1204 | 4984 common_init_alloc_early (); |
| 428 | 4985 |
| 442 | 4986 { |
| 4987 int i; | |
| 4988 for (i = 0; i < countof (lrecord_implementations_table); i++) | |
| 4989 lrecord_implementations_table[i] = 0; | |
| 4990 } | |
| 4991 | |
| 4992 INIT_LRECORD_IMPLEMENTATION (cons); | |
| 4993 INIT_LRECORD_IMPLEMENTATION (vector); | |
| 4994 INIT_LRECORD_IMPLEMENTATION (string); | |
| 3092 | 4995 #ifdef NEW_GC |
| 4996 INIT_LRECORD_IMPLEMENTATION (string_indirect_data); | |
| 4997 INIT_LRECORD_IMPLEMENTATION (string_direct_data); | |
| 4998 #endif /* NEW_GC */ | |
| 3263 | 4999 #ifndef NEW_GC |
| 442 | 5000 INIT_LRECORD_IMPLEMENTATION (lcrecord_list); |
| 1204 | 5001 INIT_LRECORD_IMPLEMENTATION (free); |
| 3263 | 5002 #endif /* not NEW_GC */ |
| 428 | 5003 |
| 452 | 5004 staticpros = Dynarr_new2 (Lisp_Object_ptr_dynarr, Lisp_Object *); |
| 5005 Dynarr_resize (staticpros, 1410); /* merely a small optimization */ | |
| 2367 | 5006 dump_add_root_block_ptr (&staticpros, &staticpros_description); |
| 771 | 5007 #ifdef DEBUG_XEMACS |
| 5008 staticpro_names = Dynarr_new2 (char_ptr_dynarr, char *); | |
| 5009 Dynarr_resize (staticpro_names, 1410); /* merely a small optimization */ | |
| 2367 | 5010 dump_add_root_block_ptr (&staticpro_names, &staticpro_names_description); |
| 771 | 5011 #endif |
| 5012 | |
| 3263 | 5013 #ifdef NEW_GC |
| 2720 | 5014 mcpros = Dynarr_new2 (Lisp_Object_dynarr, Lisp_Object); |
| 5015 Dynarr_resize (mcpros, 1410); /* merely a small optimization */ | |
| 5016 dump_add_root_block_ptr (&mcpros, &mcpros_description); | |
| 5017 #ifdef DEBUG_XEMACS | |
| 5018 mcpro_names = Dynarr_new2 (char_ptr_dynarr, char *); | |
| 5019 Dynarr_resize (mcpro_names, 1410); /* merely a small optimization */ | |
| 5020 dump_add_root_block_ptr (&mcpro_names, &mcpro_names_description); | |
| 5021 #endif | |
| 3263 | 5022 #else /* not NEW_GC */ |
| 771 | 5023 init_lcrecord_lists (); |
| 3263 | 5024 #endif /* not NEW_GC */ |
| 428 | 5025 } |
| 5026 | |
| 5027 void | |
| 5028 syms_of_alloc (void) | |
| 5029 { | |
| 442 | 5030 DEFSYMBOL (Qgarbage_collecting); |
| 428 | 5031 |
| 5032 DEFSUBR (Fcons); | |
| 5033 DEFSUBR (Flist); | |
| 5034 DEFSUBR (Fvector); | |
| 5035 DEFSUBR (Fbit_vector); | |
| 5036 DEFSUBR (Fmake_byte_code); | |
| 5037 DEFSUBR (Fmake_list); | |
| 5038 DEFSUBR (Fmake_vector); | |
| 5039 DEFSUBR (Fmake_bit_vector); | |
| 5040 DEFSUBR (Fmake_string); | |
| 5041 DEFSUBR (Fstring); | |
| 5042 DEFSUBR (Fmake_symbol); | |
| 5043 DEFSUBR (Fmake_marker); | |
| 5044 DEFSUBR (Fpurecopy); | |
| 2994 | 5045 #ifdef ALLOC_TYPE_STATS |
| 5046 DEFSUBR (Fobject_memory_usage_stats); | |
| 5047 DEFSUBR (Fobject_memory_usage); | |
| 5048 #endif /* ALLOC_TYPE_STATS */ | |
| 428 | 5049 DEFSUBR (Fgarbage_collect); |
| 440 | 5050 #if 0 |
| 428 | 5051 DEFSUBR (Fmemory_limit); |
| 440 | 5052 #endif |
| 2994 | 5053 DEFSUBR (Ftotal_memory_usage); |
| 428 | 5054 DEFSUBR (Fconsing_since_gc); |
| 5055 } | |
| 5056 | |
| 5057 void | |
| 5058 vars_of_alloc (void) | |
| 5059 { | |
| 5060 #ifdef DEBUG_XEMACS | |
| 5061 DEFVAR_INT ("debug-allocation", &debug_allocation /* | |
| 5062 If non-zero, print out information to stderr about all objects allocated. | |
| 5063 See also `debug-allocation-backtrace-length'. | |
| 5064 */ ); | |
| 5065 debug_allocation = 0; | |
| 5066 | |
| 5067 DEFVAR_INT ("debug-allocation-backtrace-length", | |
| 5068 &debug_allocation_backtrace_length /* | |
| 5069 Length (in stack frames) of short backtrace printed out by `debug-allocation'. | |
| 5070 */ ); | |
| 5071 debug_allocation_backtrace_length = 2; | |
| 5072 #endif | |
| 5073 | |
| 5074 DEFVAR_BOOL ("purify-flag", &purify_flag /* | |
| 5075 Non-nil means loading Lisp code in order to dump an executable. | |
| 5076 This means that certain objects should be allocated in readonly space. | |
| 5077 */ ); | |
| 5078 } |
