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