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