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