Mercurial > hg > xemacs-beta
annotate src/alloc.c @ 5120:d1247f3cc363 ben-lisp-object
latest work on lisp-object workspace;
more changes eliminating LCRECORD in place of LISP_OBJECT;
now compiles and runs.
author | Ben Wing <ben@xemacs.org> |
---|---|
date | Mon, 28 Dec 2009 01:15:52 -0600 |
parents | e0db3c197671 |
children | 623d57b7fbe8 |
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 | |
5120
d1247f3cc363
latest work on lisp-object workspace;
Ben Wing <ben@xemacs.org>
parents:
5118
diff
changeset
|
1203 #define ALLOCATE_FIXED_TYPE_AND_SET_IMPL(type, lisp_type, var, lrec_ptr)\ |
3017 | 1204 do { \ |
5120
d1247f3cc363
latest work on lisp-object workspace;
Ben Wing <ben@xemacs.org>
parents:
5118
diff
changeset
|
1205 (var) = (lisp_type *) XPNTR (ALLOC_LISP_OBJECT (type)); \ |
3017 | 1206 } while (0) |
1207 #define NOSEEUM_ALLOCATE_FIXED_TYPE_AND_SET_IMPL(type, lisp_type, var, \ | |
1208 lrec_ptr) \ | |
1209 do { \ | |
5120
d1247f3cc363
latest work on lisp-object workspace;
Ben Wing <ben@xemacs.org>
parents:
5118
diff
changeset
|
1210 (var) = (lisp_type *) XPNTR (noseeum_alloc_lrecord (lrec_ptr)); \ |
3017 | 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 |
5120
d1247f3cc363
latest work on lisp-object workspace;
Ben Wing <ben@xemacs.org>
parents:
5118
diff
changeset
|
2541 s = XSTRING (ALLOC_LISP_OBJECT (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 |
5120
d1247f3cc363
latest work on lisp-object workspace;
Ben Wing <ben@xemacs.org>
parents:
5118
diff
changeset
|
2949 s = XSTRING (ALLOC_LISP_OBJECT (string)); |
2720 | 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); | |
5120
d1247f3cc363
latest work on lisp-object workspace;
Ben Wing <ben@xemacs.org>
parents:
5118
diff
changeset
|
2964 STRING_DATA_OBJECT (s) = ALLOC_LISP_OBJECT (string_indirect_data); |
3092 | 2965 XSTRING_INDIRECT_DATA_DATA (STRING_DATA_OBJECT (s)) = (Ibyte *) contents; |
2966 XSTRING_INDIRECT_DATA_SIZE (STRING_DATA_OBJECT (s)) = length; | |
2967 #else /* not NEW_GC */ | |
867 | 2968 set_lispstringp_data (s, (Ibyte *) contents); |
826 | 2969 set_lispstringp_length (s, length); |
3092 | 2970 #endif /* not NEW_GC */ |
793 | 2971 val = wrap_string (s); |
771 | 2972 init_string_ascii_begin (val); |
2973 sledgehammer_check_ascii_begin (val); | |
2974 | |
428 | 2975 return val; |
2976 } | |
2977 | |
2978 | |
3263 | 2979 #ifndef NEW_GC |
428 | 2980 /************************************************************************/ |
2981 /* lcrecord lists */ | |
2982 /************************************************************************/ | |
2983 | |
2984 /* 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
|
2985 sorts of lcrecords, to avoid calling ALLOC_LISP_OBJECT() (and thus |
428 | 2986 malloc() and garbage-collection junk) as much as possible. |
2987 It is similar to the Blocktype class. | |
2988 | |
1204 | 2989 See detailed comment in lcrecord.h. |
2990 */ | |
2991 | |
2992 const struct memory_description free_description[] = { | |
2551 | 2993 { XD_LISP_OBJECT, offsetof (struct free_lcrecord_header, chain), 0, { 0 }, |
1204 | 2994 XD_FLAG_FREE_LISP_OBJECT }, |
2995 { XD_END } | |
2996 }; | |
2997 | |
5118
e0db3c197671
merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
diff
changeset
|
2998 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
|
2999 struct free_lcrecord_header); |
1204 | 3000 |
3001 const struct memory_description lcrecord_list_description[] = { | |
2551 | 3002 { XD_LISP_OBJECT, offsetof (struct lcrecord_list, free), 0, { 0 }, |
1204 | 3003 XD_FLAG_FREE_LISP_OBJECT }, |
3004 { XD_END } | |
3005 }; | |
428 | 3006 |
3007 static Lisp_Object | |
3008 mark_lcrecord_list (Lisp_Object obj) | |
3009 { | |
3010 struct lcrecord_list *list = XLCRECORD_LIST (obj); | |
3011 Lisp_Object chain = list->free; | |
3012 | |
3013 while (!NILP (chain)) | |
3014 { | |
3015 struct lrecord_header *lheader = XRECORD_LHEADER (chain); | |
3016 struct free_lcrecord_header *free_header = | |
3017 (struct free_lcrecord_header *) lheader; | |
3018 | |
442 | 3019 gc_checking_assert |
3020 (/* There should be no other pointers to the free list. */ | |
3021 ! MARKED_RECORD_HEADER_P (lheader) | |
3022 && | |
3023 /* Only lcrecords should be here. */ | |
1204 | 3024 ! list->implementation->basic_p |
442 | 3025 && |
3026 /* Only free lcrecords should be here. */ | |
3027 free_header->lcheader.free | |
3028 && | |
3029 /* The type of the lcrecord must be right. */ | |
1204 | 3030 lheader->type == lrecord_type_free |
442 | 3031 && |
3032 /* So must the size. */ | |
1204 | 3033 (list->implementation->static_size == 0 || |
3034 list->implementation->static_size == list->size) | |
442 | 3035 ); |
428 | 3036 |
3037 MARK_RECORD_HEADER (lheader); | |
3038 chain = free_header->chain; | |
3039 } | |
3040 | |
3041 return Qnil; | |
3042 } | |
3043 | |
5118
e0db3c197671
merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
diff
changeset
|
3044 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
|
3045 mark_lcrecord_list, |
e0db3c197671
merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
diff
changeset
|
3046 lcrecord_list_description, |
e0db3c197671
merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
diff
changeset
|
3047 struct lcrecord_list); |
934 | 3048 |
428 | 3049 Lisp_Object |
665 | 3050 make_lcrecord_list (Elemcount size, |
442 | 3051 const struct lrecord_implementation *implementation) |
428 | 3052 { |
3024 | 3053 /* Don't use old_alloc_lcrecord_type() avoid infinite recursion |
1204 | 3054 allocating this, */ |
3055 struct lcrecord_list *p = (struct lcrecord_list *) | |
3024 | 3056 old_basic_alloc_lcrecord (sizeof (struct lcrecord_list), |
3057 &lrecord_lcrecord_list); | |
428 | 3058 |
3059 p->implementation = implementation; | |
3060 p->size = size; | |
3061 p->free = Qnil; | |
793 | 3062 return wrap_lcrecord_list (p); |
428 | 3063 } |
3064 | |
3065 Lisp_Object | |
1204 | 3066 alloc_managed_lcrecord (Lisp_Object lcrecord_list) |
428 | 3067 { |
3068 struct lcrecord_list *list = XLCRECORD_LIST (lcrecord_list); | |
3069 if (!NILP (list->free)) | |
3070 { | |
3071 Lisp_Object val = list->free; | |
3072 struct free_lcrecord_header *free_header = | |
3073 (struct free_lcrecord_header *) XPNTR (val); | |
1204 | 3074 struct lrecord_header *lheader = &free_header->lcheader.lheader; |
428 | 3075 |
3076 #ifdef ERROR_CHECK_GC | |
1204 | 3077 /* Major overkill here. */ |
428 | 3078 /* There should be no other pointers to the free list. */ |
442 | 3079 assert (! MARKED_RECORD_HEADER_P (lheader)); |
428 | 3080 /* Only free lcrecords should be here. */ |
3081 assert (free_header->lcheader.free); | |
1204 | 3082 assert (lheader->type == lrecord_type_free); |
3083 /* Only lcrecords should be here. */ | |
3084 assert (! (list->implementation->basic_p)); | |
3085 #if 0 /* Not used anymore, now that we set the type of the header to | |
3086 lrecord_type_free. */ | |
428 | 3087 /* The type of the lcrecord must be right. */ |
442 | 3088 assert (LHEADER_IMPLEMENTATION (lheader) == list->implementation); |
1204 | 3089 #endif /* 0 */ |
428 | 3090 /* So must the size. */ |
1204 | 3091 assert (list->implementation->static_size == 0 || |
3092 list->implementation->static_size == list->size); | |
428 | 3093 #endif /* ERROR_CHECK_GC */ |
442 | 3094 |
428 | 3095 list->free = free_header->chain; |
3096 free_header->lcheader.free = 0; | |
1204 | 3097 /* Put back the correct type, as we set it to lrecord_type_free. */ |
3098 lheader->type = list->implementation->lrecord_type_index; | |
3024 | 3099 old_zero_sized_lcrecord (free_header, list->size); |
428 | 3100 return val; |
3101 } | |
3102 else | |
3024 | 3103 return wrap_pointer_1 (old_basic_alloc_lcrecord (list->size, |
3104 list->implementation)); | |
428 | 3105 } |
3106 | |
771 | 3107 /* "Free" a Lisp object LCRECORD by placing it on its associated free list |
1204 | 3108 LCRECORD_LIST; next time alloc_managed_lcrecord() is called with the |
771 | 3109 same LCRECORD_LIST as its parameter, it will return an object from the |
3110 free list, which may be this one. Be VERY VERY SURE there are no | |
3111 pointers to this object hanging around anywhere where they might be | |
3112 used! | |
3113 | |
3114 The first thing this does before making any global state change is to | |
3115 call the finalize method of the object, if it exists. */ | |
3116 | |
428 | 3117 void |
3118 free_managed_lcrecord (Lisp_Object lcrecord_list, Lisp_Object lcrecord) | |
3119 { | |
3120 struct lcrecord_list *list = XLCRECORD_LIST (lcrecord_list); | |
3121 struct free_lcrecord_header *free_header = | |
3122 (struct free_lcrecord_header *) XPNTR (lcrecord); | |
442 | 3123 struct lrecord_header *lheader = &free_header->lcheader.lheader; |
3124 const struct lrecord_implementation *implementation | |
428 | 3125 = LHEADER_IMPLEMENTATION (lheader); |
3126 | |
771 | 3127 /* Finalizer methods may try to free objects within them, which typically |
3128 won't be marked and thus are scheduled for demolition. Putting them | |
3129 on the free list would be very bad, as we'd have xfree()d memory in | |
3130 the list. Even if for some reason the objects are still live | |
3131 (generally a logic error!), we still will have problems putting such | |
3132 an object on the free list right now (e.g. we'd have to avoid calling | |
3133 the finalizer twice, etc.). So basically, those finalizers should not | |
3134 be freeing any objects if during GC. Abort now to catch those | |
3135 problems. */ | |
3136 gc_checking_assert (!gc_in_progress); | |
3137 | |
428 | 3138 /* Make sure the size is correct. This will catch, for example, |
3139 putting a window configuration on the wrong free list. */ | |
1204 | 3140 gc_checking_assert (detagged_lisp_object_size (lheader) == list->size); |
771 | 3141 /* Make sure the object isn't already freed. */ |
3142 gc_checking_assert (!free_header->lcheader.free); | |
2367 | 3143 /* Freeing stuff in dumped memory is bad. If you trip this, you |
3144 may need to check for this before freeing. */ | |
3145 gc_checking_assert (!OBJECT_DUMPED_P (lcrecord)); | |
771 | 3146 |
428 | 3147 if (implementation->finalizer) |
3148 implementation->finalizer (lheader, 0); | |
1204 | 3149 /* Yes, there are two ways to indicate freeness -- the type is |
3150 lrecord_type_free or the ->free flag is set. We used to do only the | |
3151 latter; now we do the former as well for KKCC purposes. Probably | |
3152 safer in any case, as we will lose quicker this way than keeping | |
3153 around an lrecord of apparently correct type but bogus junk in it. */ | |
3154 MARK_LRECORD_AS_FREE (lheader); | |
428 | 3155 free_header->chain = list->free; |
3156 free_header->lcheader.free = 1; | |
3157 list->free = lcrecord; | |
3158 } | |
3159 | |
771 | 3160 static Lisp_Object all_lcrecord_lists[countof (lrecord_implementations_table)]; |
3161 | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3063
diff
changeset
|
3162 Lisp_Object |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3063
diff
changeset
|
3163 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
|
3164 const struct lrecord_implementation *imp) |
771 | 3165 { |
3166 if (EQ (all_lcrecord_lists[imp->lrecord_type_index], Qzero)) | |
3167 all_lcrecord_lists[imp->lrecord_type_index] = | |
3168 make_lcrecord_list (size, imp); | |
3169 | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3063
diff
changeset
|
3170 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
|
3171 } |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3063
diff
changeset
|
3172 |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3063
diff
changeset
|
3173 Lisp_Object |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3063
diff
changeset
|
3174 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
|
3175 { |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3063
diff
changeset
|
3176 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
|
3177 return alloc_automanaged_sized_lcrecord (imp->static_size, imp); |
771 | 3178 } |
3179 | |
3180 void | |
3024 | 3181 old_free_lcrecord (Lisp_Object rec) |
771 | 3182 { |
3183 int type = XRECORD_LHEADER (rec)->type; | |
3184 | |
3185 assert (!EQ (all_lcrecord_lists[type], Qzero)); | |
3186 | |
3187 free_managed_lcrecord (all_lcrecord_lists[type], rec); | |
3188 } | |
3263 | 3189 #endif /* not NEW_GC */ |
428 | 3190 |
3191 | |
3192 DEFUN ("purecopy", Fpurecopy, 1, 1, 0, /* | |
3193 Kept for compatibility, returns its argument. | |
3194 Old: | |
3195 Make a copy of OBJECT in pure storage. | |
3196 Recursively copies contents of vectors and cons cells. | |
3197 Does not copy symbols. | |
3198 */ | |
444 | 3199 (object)) |
428 | 3200 { |
444 | 3201 return object; |
428 | 3202 } |
3203 | |
3204 | |
3205 /************************************************************************/ | |
3206 /* Garbage Collection */ | |
3207 /************************************************************************/ | |
3208 | |
442 | 3209 /* All the built-in lisp object types are enumerated in `enum lrecord_type'. |
3210 Additional ones may be defined by a module (none yet). We leave some | |
3211 room in `lrecord_implementations_table' for such new lisp object types. */ | |
647 | 3212 const struct lrecord_implementation *lrecord_implementations_table[(int)lrecord_type_last_built_in_type + MODULE_DEFINABLE_TYPE_COUNT]; |
3213 int lrecord_type_count = lrecord_type_last_built_in_type; | |
1676 | 3214 #ifndef USE_KKCC |
442 | 3215 /* Object marker functions are in the lrecord_implementation structure. |
3216 But copying them to a parallel array is much more cache-friendly. | |
3217 This hack speeds up (garbage-collect) by about 5%. */ | |
3218 Lisp_Object (*lrecord_markers[countof (lrecord_implementations_table)]) (Lisp_Object); | |
1676 | 3219 #endif /* not USE_KKCC */ |
428 | 3220 |
3221 struct gcpro *gcprolist; | |
3222 | |
771 | 3223 /* We want the staticpro list relocated, but not the pointers found |
3224 therein, because they refer to locations in the global data segment, not | |
3225 in the heap; we only dump heap objects. Hence we use a trivial | |
3226 description, as for pointerless objects. (Note that the data segment | |
3227 objects, which are global variables like Qfoo or Vbar, themselves are | |
3228 pointers to heap objects. Each needs to be described to pdump as a | |
3229 "root pointer"; this happens in the call to staticpro(). */ | |
1204 | 3230 static const struct memory_description staticpro_description_1[] = { |
452 | 3231 { XD_END } |
3232 }; | |
3233 | |
1204 | 3234 static const struct sized_memory_description staticpro_description = { |
452 | 3235 sizeof (Lisp_Object *), |
3236 staticpro_description_1 | |
3237 }; | |
3238 | |
1204 | 3239 static const struct memory_description staticpros_description_1[] = { |
452 | 3240 XD_DYNARR_DESC (Lisp_Object_ptr_dynarr, &staticpro_description), |
3241 { XD_END } | |
3242 }; | |
3243 | |
1204 | 3244 static const struct sized_memory_description staticpros_description = { |
452 | 3245 sizeof (Lisp_Object_ptr_dynarr), |
3246 staticpros_description_1 | |
3247 }; | |
3248 | |
771 | 3249 #ifdef DEBUG_XEMACS |
3250 | |
1204 | 3251 static const struct memory_description staticpro_one_name_description_1[] = { |
2367 | 3252 { XD_ASCII_STRING, 0 }, |
771 | 3253 { XD_END } |
3254 }; | |
3255 | |
1204 | 3256 static const struct sized_memory_description staticpro_one_name_description = { |
771 | 3257 sizeof (char *), |
3258 staticpro_one_name_description_1 | |
3259 }; | |
3260 | |
1204 | 3261 static const struct memory_description staticpro_names_description_1[] = { |
771 | 3262 XD_DYNARR_DESC (char_ptr_dynarr, &staticpro_one_name_description), |
3263 { XD_END } | |
3264 }; | |
3265 | |
1204 | 3266 |
3267 extern const struct sized_memory_description staticpro_names_description; | |
3268 | |
3269 const struct sized_memory_description staticpro_names_description = { | |
771 | 3270 sizeof (char_ptr_dynarr), |
3271 staticpro_names_description_1 | |
3272 }; | |
3273 | |
3274 /* Help debug crashes gc-marking a staticpro'ed object. */ | |
3275 | |
3276 Lisp_Object_ptr_dynarr *staticpros; | |
3277 char_ptr_dynarr *staticpro_names; | |
3278 | |
3279 /* Mark the Lisp_Object at non-heap VARADDRESS as a root object for | |
3280 garbage collection, and for dumping. */ | |
3281 void | |
3282 staticpro_1 (Lisp_Object *varaddress, char *varname) | |
3283 { | |
3284 Dynarr_add (staticpros, varaddress); | |
3285 Dynarr_add (staticpro_names, varname); | |
1204 | 3286 dump_add_root_lisp_object (varaddress); |
771 | 3287 } |
3288 | |
3289 | |
3290 Lisp_Object_ptr_dynarr *staticpros_nodump; | |
3291 char_ptr_dynarr *staticpro_nodump_names; | |
3292 | |
3293 /* Mark the Lisp_Object at heap VARADDRESS as a root object for | |
3294 garbage collection, but not for dumping. (See below.) */ | |
3295 void | |
3296 staticpro_nodump_1 (Lisp_Object *varaddress, char *varname) | |
3297 { | |
3298 Dynarr_add (staticpros_nodump, varaddress); | |
3299 Dynarr_add (staticpro_nodump_names, varname); | |
3300 } | |
3301 | |
996 | 3302 #ifdef HAVE_SHLIB |
3303 /* Stop treating the Lisp_Object at non-heap VARADDRESS as a root object | |
3304 for garbage collection, but not for dumping. */ | |
3305 void | |
3306 unstaticpro_nodump_1 (Lisp_Object *varaddress, char *varname) | |
3307 { | |
3308 Dynarr_delete_object (staticpros, varaddress); | |
3309 Dynarr_delete_object (staticpro_names, varname); | |
3310 } | |
3311 #endif | |
3312 | |
771 | 3313 #else /* not DEBUG_XEMACS */ |
3314 | |
452 | 3315 Lisp_Object_ptr_dynarr *staticpros; |
3316 | |
3317 /* Mark the Lisp_Object at non-heap VARADDRESS as a root object for | |
3318 garbage collection, and for dumping. */ | |
428 | 3319 void |
3320 staticpro (Lisp_Object *varaddress) | |
3321 { | |
452 | 3322 Dynarr_add (staticpros, varaddress); |
1204 | 3323 dump_add_root_lisp_object (varaddress); |
428 | 3324 } |
3325 | |
442 | 3326 |
452 | 3327 Lisp_Object_ptr_dynarr *staticpros_nodump; |
3328 | |
771 | 3329 /* Mark the Lisp_Object at heap VARADDRESS as a root object for garbage |
3330 collection, but not for dumping. This is used for objects where the | |
3331 only sure pointer is in the heap (rather than in the global data | |
3332 segment, as must be the case for pdump root pointers), but not inside of | |
3333 another Lisp object (where it will be marked as a result of that Lisp | |
3334 object's mark method). The call to staticpro_nodump() must occur *BOTH* | |
3335 at initialization time and at "reinitialization" time (startup, after | |
3336 pdump load.) (For example, this is the case with the predicate symbols | |
3337 for specifier and coding system types. The pointer to this symbol is | |
3338 inside of a methods structure, which is allocated on the heap. The | |
3339 methods structure will be written out to the pdump data file, and may be | |
3340 reloaded at a different address.) | |
3341 | |
3342 #### The necessity for reinitialization is a bug in pdump. Pdump should | |
3343 automatically regenerate the staticpro()s for these symbols when it | |
3344 loads the data in. */ | |
3345 | |
428 | 3346 void |
3347 staticpro_nodump (Lisp_Object *varaddress) | |
3348 { | |
452 | 3349 Dynarr_add (staticpros_nodump, varaddress); |
428 | 3350 } |
3351 | |
996 | 3352 #ifdef HAVE_SHLIB |
3353 /* Unmark the Lisp_Object at non-heap VARADDRESS as a root object for | |
3354 garbage collection, but not for dumping. */ | |
3355 void | |
3356 unstaticpro_nodump (Lisp_Object *varaddress) | |
3357 { | |
3358 Dynarr_delete_object (staticpros, varaddress); | |
3359 } | |
3360 #endif | |
3361 | |
771 | 3362 #endif /* not DEBUG_XEMACS */ |
3363 | |
2720 | 3364 |
3365 | |
3366 | |
3367 | |
3263 | 3368 #ifdef NEW_GC |
2720 | 3369 static const struct memory_description mcpro_description_1[] = { |
3370 { XD_END } | |
3371 }; | |
3372 | |
3373 static const struct sized_memory_description mcpro_description = { | |
3374 sizeof (Lisp_Object *), | |
3375 mcpro_description_1 | |
3376 }; | |
3377 | |
3378 static const struct memory_description mcpros_description_1[] = { | |
3379 XD_DYNARR_DESC (Lisp_Object_dynarr, &mcpro_description), | |
3380 { XD_END } | |
3381 }; | |
3382 | |
3383 static const struct sized_memory_description mcpros_description = { | |
3384 sizeof (Lisp_Object_dynarr), | |
3385 mcpros_description_1 | |
3386 }; | |
3387 | |
3388 #ifdef DEBUG_XEMACS | |
3389 | |
3390 static const struct memory_description mcpro_one_name_description_1[] = { | |
3391 { XD_ASCII_STRING, 0 }, | |
3392 { XD_END } | |
3393 }; | |
3394 | |
3395 static const struct sized_memory_description mcpro_one_name_description = { | |
3396 sizeof (char *), | |
3397 mcpro_one_name_description_1 | |
3398 }; | |
3399 | |
3400 static const struct memory_description mcpro_names_description_1[] = { | |
3401 XD_DYNARR_DESC (char_ptr_dynarr, &mcpro_one_name_description), | |
3402 { XD_END } | |
3403 }; | |
3404 | |
3405 extern const struct sized_memory_description mcpro_names_description; | |
3406 | |
3407 const struct sized_memory_description mcpro_names_description = { | |
3408 sizeof (char_ptr_dynarr), | |
3409 mcpro_names_description_1 | |
3410 }; | |
3411 | |
3412 /* Help debug crashes gc-marking a mcpro'ed object. */ | |
3413 | |
3414 Lisp_Object_dynarr *mcpros; | |
3415 char_ptr_dynarr *mcpro_names; | |
3416 | |
3417 /* Mark the Lisp_Object at non-heap VARADDRESS as a root object for | |
3418 garbage collection, and for dumping. */ | |
3419 void | |
3420 mcpro_1 (Lisp_Object varaddress, char *varname) | |
3421 { | |
3422 Dynarr_add (mcpros, varaddress); | |
3423 Dynarr_add (mcpro_names, varname); | |
3424 } | |
3425 | |
3426 #else /* not DEBUG_XEMACS */ | |
3427 | |
3428 Lisp_Object_dynarr *mcpros; | |
3429 | |
3430 /* Mark the Lisp_Object at non-heap VARADDRESS as a root object for | |
3431 garbage collection, and for dumping. */ | |
3432 void | |
3433 mcpro (Lisp_Object varaddress) | |
3434 { | |
3435 Dynarr_add (mcpros, varaddress); | |
3436 } | |
3437 | |
3438 #endif /* not DEBUG_XEMACS */ | |
3263 | 3439 #endif /* NEW_GC */ |
3440 | |
3441 | |
3442 #ifndef NEW_GC | |
428 | 3443 static int gc_count_num_short_string_in_use; |
647 | 3444 static Bytecount gc_count_string_total_size; |
3445 static Bytecount gc_count_short_string_total_size; | |
428 | 3446 |
3447 /* static int gc_count_total_records_used, gc_count_records_total_size; */ | |
3448 | |
3449 | |
3450 /* stats on lcrecords in use - kinda kludgy */ | |
3451 | |
3452 static struct | |
3453 { | |
3454 int instances_in_use; | |
3455 int bytes_in_use; | |
3456 int instances_freed; | |
3457 int bytes_freed; | |
3458 int instances_on_free_list; | |
3461 | 3459 } lcrecord_stats [countof (lrecord_implementations_table)]; |
428 | 3460 |
3461 static void | |
442 | 3462 tick_lcrecord_stats (const struct lrecord_header *h, int free_p) |
428 | 3463 { |
647 | 3464 int type_index = h->type; |
428 | 3465 |
3024 | 3466 if (((struct old_lcrecord_header *) h)->free) |
428 | 3467 { |
442 | 3468 gc_checking_assert (!free_p); |
428 | 3469 lcrecord_stats[type_index].instances_on_free_list++; |
3470 } | |
3471 else | |
3472 { | |
1204 | 3473 Bytecount sz = detagged_lisp_object_size (h); |
3474 | |
428 | 3475 if (free_p) |
3476 { | |
3477 lcrecord_stats[type_index].instances_freed++; | |
3478 lcrecord_stats[type_index].bytes_freed += sz; | |
3479 } | |
3480 else | |
3481 { | |
3482 lcrecord_stats[type_index].instances_in_use++; | |
3483 lcrecord_stats[type_index].bytes_in_use += sz; | |
3484 } | |
3485 } | |
3486 } | |
3263 | 3487 #endif /* not NEW_GC */ |
428 | 3488 |
3489 | |
3263 | 3490 #ifndef NEW_GC |
428 | 3491 /* Free all unmarked records */ |
3492 static void | |
3024 | 3493 sweep_lcrecords_1 (struct old_lcrecord_header **prev, int *used) |
3494 { | |
3495 struct old_lcrecord_header *header; | |
428 | 3496 int num_used = 0; |
3497 /* int total_size = 0; */ | |
3498 | |
3499 xzero (lcrecord_stats); /* Reset all statistics to 0. */ | |
3500 | |
3501 /* First go through and call all the finalize methods. | |
3502 Then go through and free the objects. There used to | |
3503 be only one loop here, with the call to the finalizer | |
3504 occurring directly before the xfree() below. That | |
3505 is marginally faster but much less safe -- if the | |
3506 finalize method for an object needs to reference any | |
3507 other objects contained within it (and many do), | |
3508 we could easily be screwed by having already freed that | |
3509 other object. */ | |
3510 | |
3511 for (header = *prev; header; header = header->next) | |
3512 { | |
3513 struct lrecord_header *h = &(header->lheader); | |
442 | 3514 |
3515 GC_CHECK_LHEADER_INVARIANTS (h); | |
3516 | |
3517 if (! MARKED_RECORD_HEADER_P (h) && ! header->free) | |
428 | 3518 { |
3519 if (LHEADER_IMPLEMENTATION (h)->finalizer) | |
3520 LHEADER_IMPLEMENTATION (h)->finalizer (h, 0); | |
3521 } | |
3522 } | |
3523 | |
3524 for (header = *prev; header; ) | |
3525 { | |
3526 struct lrecord_header *h = &(header->lheader); | |
442 | 3527 if (MARKED_RECORD_HEADER_P (h)) |
428 | 3528 { |
442 | 3529 if (! C_READONLY_RECORD_HEADER_P (h)) |
428 | 3530 UNMARK_RECORD_HEADER (h); |
3531 num_used++; | |
3532 /* total_size += n->implementation->size_in_bytes (h);*/ | |
440 | 3533 /* #### May modify header->next on a C_READONLY lcrecord */ |
428 | 3534 prev = &(header->next); |
3535 header = *prev; | |
3536 tick_lcrecord_stats (h, 0); | |
3537 } | |
3538 else | |
3539 { | |
3024 | 3540 struct old_lcrecord_header *next = header->next; |
428 | 3541 *prev = next; |
3542 tick_lcrecord_stats (h, 1); | |
3543 /* used to call finalizer right here. */ | |
3024 | 3544 xfree (header, struct old_lcrecord_header *); |
428 | 3545 header = next; |
3546 } | |
3547 } | |
3548 *used = num_used; | |
3549 /* *total = total_size; */ | |
3550 } | |
3551 | |
3552 /* And the Lord said: Thou shalt use the `c-backslash-region' command | |
3553 to make macros prettier. */ | |
3554 | |
3555 #ifdef ERROR_CHECK_GC | |
3556 | |
771 | 3557 #define SWEEP_FIXED_TYPE_BLOCK_1(typename, obj_type, lheader) \ |
428 | 3558 do { \ |
3559 struct typename##_block *SFTB_current; \ | |
3560 int SFTB_limit; \ | |
3561 int num_free = 0, num_used = 0; \ | |
3562 \ | |
444 | 3563 for (SFTB_current = current_##typename##_block, \ |
428 | 3564 SFTB_limit = current_##typename##_block_index; \ |
3565 SFTB_current; \ | |
3566 ) \ | |
3567 { \ | |
3568 int SFTB_iii; \ | |
3569 \ | |
3570 for (SFTB_iii = 0; SFTB_iii < SFTB_limit; SFTB_iii++) \ | |
3571 { \ | |
3572 obj_type *SFTB_victim = &(SFTB_current->block[SFTB_iii]); \ | |
3573 \ | |
454 | 3574 if (LRECORD_FREE_P (SFTB_victim)) \ |
428 | 3575 { \ |
3576 num_free++; \ | |
3577 } \ | |
3578 else if (C_READONLY_RECORD_HEADER_P (&SFTB_victim->lheader)) \ | |
3579 { \ | |
3580 num_used++; \ | |
3581 } \ | |
442 | 3582 else if (! MARKED_RECORD_HEADER_P (&SFTB_victim->lheader)) \ |
428 | 3583 { \ |
3584 num_free++; \ | |
3585 FREE_FIXED_TYPE (typename, obj_type, SFTB_victim); \ | |
3586 } \ | |
3587 else \ | |
3588 { \ | |
3589 num_used++; \ | |
3590 UNMARK_##typename (SFTB_victim); \ | |
3591 } \ | |
3592 } \ | |
3593 SFTB_current = SFTB_current->prev; \ | |
3594 SFTB_limit = countof (current_##typename##_block->block); \ | |
3595 } \ | |
3596 \ | |
3597 gc_count_num_##typename##_in_use = num_used; \ | |
3598 gc_count_num_##typename##_freelist = num_free; \ | |
3599 } while (0) | |
3600 | |
3601 #else /* !ERROR_CHECK_GC */ | |
3602 | |
771 | 3603 #define SWEEP_FIXED_TYPE_BLOCK_1(typename, obj_type, lheader) \ |
3604 do { \ | |
3605 struct typename##_block *SFTB_current; \ | |
3606 struct typename##_block **SFTB_prev; \ | |
3607 int SFTB_limit; \ | |
3608 int num_free = 0, num_used = 0; \ | |
3609 \ | |
3610 typename##_free_list = 0; \ | |
3611 \ | |
3612 for (SFTB_prev = ¤t_##typename##_block, \ | |
3613 SFTB_current = current_##typename##_block, \ | |
3614 SFTB_limit = current_##typename##_block_index; \ | |
3615 SFTB_current; \ | |
3616 ) \ | |
3617 { \ | |
3618 int SFTB_iii; \ | |
3619 int SFTB_empty = 1; \ | |
3620 Lisp_Free *SFTB_old_free_list = typename##_free_list; \ | |
3621 \ | |
3622 for (SFTB_iii = 0; SFTB_iii < SFTB_limit; SFTB_iii++) \ | |
3623 { \ | |
3624 obj_type *SFTB_victim = &(SFTB_current->block[SFTB_iii]); \ | |
3625 \ | |
3626 if (LRECORD_FREE_P (SFTB_victim)) \ | |
3627 { \ | |
3628 num_free++; \ | |
3629 PUT_FIXED_TYPE_ON_FREE_LIST (typename, obj_type, SFTB_victim); \ | |
3630 } \ | |
3631 else if (C_READONLY_RECORD_HEADER_P (&SFTB_victim->lheader)) \ | |
3632 { \ | |
3633 SFTB_empty = 0; \ | |
3634 num_used++; \ | |
3635 } \ | |
3636 else if (! MARKED_RECORD_HEADER_P (&SFTB_victim->lheader)) \ | |
3637 { \ | |
3638 num_free++; \ | |
3639 FREE_FIXED_TYPE (typename, obj_type, SFTB_victim); \ | |
3640 } \ | |
3641 else \ | |
3642 { \ | |
3643 SFTB_empty = 0; \ | |
3644 num_used++; \ | |
3645 UNMARK_##typename (SFTB_victim); \ | |
3646 } \ | |
3647 } \ | |
3648 if (!SFTB_empty) \ | |
3649 { \ | |
3650 SFTB_prev = &(SFTB_current->prev); \ | |
3651 SFTB_current = SFTB_current->prev; \ | |
3652 } \ | |
3653 else if (SFTB_current == current_##typename##_block \ | |
3654 && !SFTB_current->prev) \ | |
3655 { \ | |
3656 /* No real point in freeing sole allocation block */ \ | |
3657 break; \ | |
3658 } \ | |
3659 else \ | |
3660 { \ | |
3661 struct typename##_block *SFTB_victim_block = SFTB_current; \ | |
3662 if (SFTB_victim_block == current_##typename##_block) \ | |
3663 current_##typename##_block_index \ | |
3664 = countof (current_##typename##_block->block); \ | |
3665 SFTB_current = SFTB_current->prev; \ | |
3666 { \ | |
3667 *SFTB_prev = SFTB_current; \ | |
1726 | 3668 xfree (SFTB_victim_block, struct typename##_block *); \ |
771 | 3669 /* Restore free list to what it was before victim was swept */ \ |
3670 typename##_free_list = SFTB_old_free_list; \ | |
3671 num_free -= SFTB_limit; \ | |
3672 } \ | |
3673 } \ | |
3674 SFTB_limit = countof (current_##typename##_block->block); \ | |
3675 } \ | |
3676 \ | |
3677 gc_count_num_##typename##_in_use = num_used; \ | |
3678 gc_count_num_##typename##_freelist = num_free; \ | |
428 | 3679 } while (0) |
3680 | |
3681 #endif /* !ERROR_CHECK_GC */ | |
3682 | |
771 | 3683 #define SWEEP_FIXED_TYPE_BLOCK(typename, obj_type) \ |
3684 SWEEP_FIXED_TYPE_BLOCK_1 (typename, obj_type, lheader) | |
3685 | |
3263 | 3686 #endif /* not NEW_GC */ |
2720 | 3687 |
428 | 3688 |
3263 | 3689 #ifndef NEW_GC |
428 | 3690 static void |
3691 sweep_conses (void) | |
3692 { | |
3693 #define UNMARK_cons(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
3694 #define ADDITIONAL_FREE_cons(ptr) | |
3695 | |
440 | 3696 SWEEP_FIXED_TYPE_BLOCK (cons, Lisp_Cons); |
428 | 3697 } |
3263 | 3698 #endif /* not NEW_GC */ |
428 | 3699 |
3700 /* Explicitly free a cons cell. */ | |
3701 void | |
853 | 3702 free_cons (Lisp_Object cons) |
428 | 3703 { |
3263 | 3704 #ifndef NEW_GC /* to avoid compiler warning */ |
853 | 3705 Lisp_Cons *ptr = XCONS (cons); |
3263 | 3706 #endif /* not NEW_GC */ |
853 | 3707 |
428 | 3708 #ifdef ERROR_CHECK_GC |
3263 | 3709 #ifdef NEW_GC |
2720 | 3710 Lisp_Cons *ptr = XCONS (cons); |
3263 | 3711 #endif /* NEW_GC */ |
428 | 3712 /* If the CAR is not an int, then it will be a pointer, which will |
3713 always be four-byte aligned. If this cons cell has already been | |
3714 placed on the free list, however, its car will probably contain | |
3715 a chain pointer to the next cons on the list, which has cleverly | |
3716 had all its 0's and 1's inverted. This allows for a quick | |
1204 | 3717 check to make sure we're not freeing something already freed. |
3718 | |
3719 NOTE: This check may not be necessary. Freeing an object sets its | |
3720 type to lrecord_type_free, which will trip up the XCONS() above -- as | |
3721 well as a check in FREE_FIXED_TYPE(). */ | |
853 | 3722 if (POINTER_TYPE_P (XTYPE (cons_car (ptr)))) |
3723 ASSERT_VALID_POINTER (XPNTR (cons_car (ptr))); | |
428 | 3724 #endif /* ERROR_CHECK_GC */ |
3725 | |
3263 | 3726 #ifdef NEW_GC |
2720 | 3727 free_lrecord (cons); |
3263 | 3728 #else /* not NEW_GC */ |
440 | 3729 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (cons, Lisp_Cons, ptr); |
3263 | 3730 #endif /* not NEW_GC */ |
428 | 3731 } |
3732 | |
3733 /* explicitly free a list. You **must make sure** that you have | |
3734 created all the cons cells that make up this list and that there | |
3735 are no pointers to any of these cons cells anywhere else. If there | |
3736 are, you will lose. */ | |
3737 | |
3738 void | |
3739 free_list (Lisp_Object list) | |
3740 { | |
3741 Lisp_Object rest, next; | |
3742 | |
3743 for (rest = list; !NILP (rest); rest = next) | |
3744 { | |
3745 next = XCDR (rest); | |
853 | 3746 free_cons (rest); |
428 | 3747 } |
3748 } | |
3749 | |
3750 /* explicitly free an alist. You **must make sure** that you have | |
3751 created all the cons cells that make up this alist and that there | |
3752 are no pointers to any of these cons cells anywhere else. If there | |
3753 are, you will lose. */ | |
3754 | |
3755 void | |
3756 free_alist (Lisp_Object alist) | |
3757 { | |
3758 Lisp_Object rest, next; | |
3759 | |
3760 for (rest = alist; !NILP (rest); rest = next) | |
3761 { | |
3762 next = XCDR (rest); | |
853 | 3763 free_cons (XCAR (rest)); |
3764 free_cons (rest); | |
428 | 3765 } |
3766 } | |
3767 | |
3263 | 3768 #ifndef NEW_GC |
428 | 3769 static void |
3770 sweep_compiled_functions (void) | |
3771 { | |
3772 #define UNMARK_compiled_function(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
945 | 3773 #define ADDITIONAL_FREE_compiled_function(ptr) \ |
1726 | 3774 if (ptr->args_in_array) xfree (ptr->args, Lisp_Object *) |
428 | 3775 |
3776 SWEEP_FIXED_TYPE_BLOCK (compiled_function, Lisp_Compiled_Function); | |
3777 } | |
3778 | |
3779 static void | |
3780 sweep_floats (void) | |
3781 { | |
3782 #define UNMARK_float(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
3783 #define ADDITIONAL_FREE_float(ptr) | |
3784 | |
440 | 3785 SWEEP_FIXED_TYPE_BLOCK (float, Lisp_Float); |
428 | 3786 } |
3787 | |
1983 | 3788 #ifdef HAVE_BIGNUM |
3789 static void | |
3790 sweep_bignums (void) | |
3791 { | |
3792 #define UNMARK_bignum(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
3793 #define ADDITIONAL_FREE_bignum(ptr) bignum_fini (ptr->data) | |
3794 | |
3795 SWEEP_FIXED_TYPE_BLOCK (bignum, Lisp_Bignum); | |
3796 } | |
3797 #endif /* HAVE_BIGNUM */ | |
3798 | |
3799 #ifdef HAVE_RATIO | |
3800 static void | |
3801 sweep_ratios (void) | |
3802 { | |
3803 #define UNMARK_ratio(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
3804 #define ADDITIONAL_FREE_ratio(ptr) ratio_fini (ptr->data) | |
3805 | |
3806 SWEEP_FIXED_TYPE_BLOCK (ratio, Lisp_Ratio); | |
3807 } | |
3808 #endif /* HAVE_RATIO */ | |
3809 | |
3810 #ifdef HAVE_BIGFLOAT | |
3811 static void | |
3812 sweep_bigfloats (void) | |
3813 { | |
3814 #define UNMARK_bigfloat(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
3815 #define ADDITIONAL_FREE_bigfloat(ptr) bigfloat_fini (ptr->bf) | |
3816 | |
3817 SWEEP_FIXED_TYPE_BLOCK (bigfloat, Lisp_Bigfloat); | |
3818 } | |
3819 #endif | |
3820 | |
428 | 3821 static void |
3822 sweep_symbols (void) | |
3823 { | |
3824 #define UNMARK_symbol(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
3825 #define ADDITIONAL_FREE_symbol(ptr) | |
3826 | |
440 | 3827 SWEEP_FIXED_TYPE_BLOCK (symbol, Lisp_Symbol); |
428 | 3828 } |
3829 | |
3830 static void | |
3831 sweep_extents (void) | |
3832 { | |
3833 #define UNMARK_extent(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
3834 #define ADDITIONAL_FREE_extent(ptr) | |
3835 | |
3836 SWEEP_FIXED_TYPE_BLOCK (extent, struct extent); | |
3837 } | |
3838 | |
3839 static void | |
3840 sweep_events (void) | |
3841 { | |
3842 #define UNMARK_event(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
3843 #define ADDITIONAL_FREE_event(ptr) | |
3844 | |
440 | 3845 SWEEP_FIXED_TYPE_BLOCK (event, Lisp_Event); |
428 | 3846 } |
3263 | 3847 #endif /* not NEW_GC */ |
428 | 3848 |
1204 | 3849 #ifdef EVENT_DATA_AS_OBJECTS |
934 | 3850 |
3263 | 3851 #ifndef NEW_GC |
934 | 3852 static void |
3853 sweep_key_data (void) | |
3854 { | |
3855 #define UNMARK_key_data(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
3856 #define ADDITIONAL_FREE_key_data(ptr) | |
3857 | |
3858 SWEEP_FIXED_TYPE_BLOCK (key_data, Lisp_Key_Data); | |
3859 } | |
3263 | 3860 #endif /* not NEW_GC */ |
934 | 3861 |
1204 | 3862 void |
3863 free_key_data (Lisp_Object ptr) | |
3864 { | |
3263 | 3865 #ifdef NEW_GC |
2720 | 3866 free_lrecord (ptr); |
3263 | 3867 #else /* not NEW_GC */ |
1204 | 3868 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (key_data, Lisp_Key_Data, XKEY_DATA (ptr)); |
3263 | 3869 #endif /* not NEW_GC */ |
2720 | 3870 } |
3871 | |
3263 | 3872 #ifndef NEW_GC |
934 | 3873 static void |
3874 sweep_button_data (void) | |
3875 { | |
3876 #define UNMARK_button_data(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
3877 #define ADDITIONAL_FREE_button_data(ptr) | |
3878 | |
3879 SWEEP_FIXED_TYPE_BLOCK (button_data, Lisp_Button_Data); | |
3880 } | |
3263 | 3881 #endif /* not NEW_GC */ |
934 | 3882 |
1204 | 3883 void |
3884 free_button_data (Lisp_Object ptr) | |
3885 { | |
3263 | 3886 #ifdef NEW_GC |
2720 | 3887 free_lrecord (ptr); |
3263 | 3888 #else /* not NEW_GC */ |
1204 | 3889 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (button_data, Lisp_Button_Data, XBUTTON_DATA (ptr)); |
3263 | 3890 #endif /* not NEW_GC */ |
2720 | 3891 } |
3892 | |
3263 | 3893 #ifndef NEW_GC |
934 | 3894 static void |
3895 sweep_motion_data (void) | |
3896 { | |
3897 #define UNMARK_motion_data(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
3898 #define ADDITIONAL_FREE_motion_data(ptr) | |
3899 | |
3900 SWEEP_FIXED_TYPE_BLOCK (motion_data, Lisp_Motion_Data); | |
3901 } | |
3263 | 3902 #endif /* not NEW_GC */ |
934 | 3903 |
1204 | 3904 void |
3905 free_motion_data (Lisp_Object ptr) | |
3906 { | |
3263 | 3907 #ifdef NEW_GC |
2720 | 3908 free_lrecord (ptr); |
3263 | 3909 #else /* not NEW_GC */ |
1204 | 3910 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (motion_data, Lisp_Motion_Data, XMOTION_DATA (ptr)); |
3263 | 3911 #endif /* not NEW_GC */ |
2720 | 3912 } |
3913 | |
3263 | 3914 #ifndef NEW_GC |
934 | 3915 static void |
3916 sweep_process_data (void) | |
3917 { | |
3918 #define UNMARK_process_data(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
3919 #define ADDITIONAL_FREE_process_data(ptr) | |
3920 | |
3921 SWEEP_FIXED_TYPE_BLOCK (process_data, Lisp_Process_Data); | |
3922 } | |
3263 | 3923 #endif /* not NEW_GC */ |
934 | 3924 |
1204 | 3925 void |
3926 free_process_data (Lisp_Object ptr) | |
3927 { | |
3263 | 3928 #ifdef NEW_GC |
2720 | 3929 free_lrecord (ptr); |
3263 | 3930 #else /* not NEW_GC */ |
1204 | 3931 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (process_data, Lisp_Process_Data, XPROCESS_DATA (ptr)); |
3263 | 3932 #endif /* not NEW_GC */ |
2720 | 3933 } |
3934 | |
3263 | 3935 #ifndef NEW_GC |
934 | 3936 static void |
3937 sweep_timeout_data (void) | |
3938 { | |
3939 #define UNMARK_timeout_data(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
3940 #define ADDITIONAL_FREE_timeout_data(ptr) | |
3941 | |
3942 SWEEP_FIXED_TYPE_BLOCK (timeout_data, Lisp_Timeout_Data); | |
3943 } | |
3263 | 3944 #endif /* not NEW_GC */ |
934 | 3945 |
1204 | 3946 void |
3947 free_timeout_data (Lisp_Object ptr) | |
3948 { | |
3263 | 3949 #ifdef NEW_GC |
2720 | 3950 free_lrecord (ptr); |
3263 | 3951 #else /* not NEW_GC */ |
1204 | 3952 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (timeout_data, Lisp_Timeout_Data, XTIMEOUT_DATA (ptr)); |
3263 | 3953 #endif /* not NEW_GC */ |
2720 | 3954 } |
3955 | |
3263 | 3956 #ifndef NEW_GC |
934 | 3957 static void |
3958 sweep_magic_data (void) | |
3959 { | |
3960 #define UNMARK_magic_data(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
3961 #define ADDITIONAL_FREE_magic_data(ptr) | |
3962 | |
3963 SWEEP_FIXED_TYPE_BLOCK (magic_data, Lisp_Magic_Data); | |
3964 } | |
3263 | 3965 #endif /* not NEW_GC */ |
934 | 3966 |
1204 | 3967 void |
3968 free_magic_data (Lisp_Object ptr) | |
3969 { | |
3263 | 3970 #ifdef NEW_GC |
2720 | 3971 free_lrecord (ptr); |
3263 | 3972 #else /* not NEW_GC */ |
1204 | 3973 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (magic_data, Lisp_Magic_Data, XMAGIC_DATA (ptr)); |
3263 | 3974 #endif /* not NEW_GC */ |
2720 | 3975 } |
3976 | |
3263 | 3977 #ifndef NEW_GC |
934 | 3978 static void |
3979 sweep_magic_eval_data (void) | |
3980 { | |
3981 #define UNMARK_magic_eval_data(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
3982 #define ADDITIONAL_FREE_magic_eval_data(ptr) | |
3983 | |
3984 SWEEP_FIXED_TYPE_BLOCK (magic_eval_data, Lisp_Magic_Eval_Data); | |
3985 } | |
3263 | 3986 #endif /* not NEW_GC */ |
934 | 3987 |
1204 | 3988 void |
3989 free_magic_eval_data (Lisp_Object ptr) | |
3990 { | |
3263 | 3991 #ifdef NEW_GC |
2720 | 3992 free_lrecord (ptr); |
3263 | 3993 #else /* not NEW_GC */ |
1204 | 3994 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (magic_eval_data, Lisp_Magic_Eval_Data, XMAGIC_EVAL_DATA (ptr)); |
3263 | 3995 #endif /* not NEW_GC */ |
2720 | 3996 } |
3997 | |
3263 | 3998 #ifndef NEW_GC |
934 | 3999 static void |
4000 sweep_eval_data (void) | |
4001 { | |
4002 #define UNMARK_eval_data(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
4003 #define ADDITIONAL_FREE_eval_data(ptr) | |
4004 | |
4005 SWEEP_FIXED_TYPE_BLOCK (eval_data, Lisp_Eval_Data); | |
4006 } | |
3263 | 4007 #endif /* not NEW_GC */ |
934 | 4008 |
1204 | 4009 void |
4010 free_eval_data (Lisp_Object ptr) | |
4011 { | |
3263 | 4012 #ifdef NEW_GC |
2720 | 4013 free_lrecord (ptr); |
3263 | 4014 #else /* not NEW_GC */ |
1204 | 4015 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (eval_data, Lisp_Eval_Data, XEVAL_DATA (ptr)); |
3263 | 4016 #endif /* not NEW_GC */ |
2720 | 4017 } |
4018 | |
3263 | 4019 #ifndef NEW_GC |
934 | 4020 static void |
4021 sweep_misc_user_data (void) | |
4022 { | |
4023 #define UNMARK_misc_user_data(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
4024 #define ADDITIONAL_FREE_misc_user_data(ptr) | |
4025 | |
4026 SWEEP_FIXED_TYPE_BLOCK (misc_user_data, Lisp_Misc_User_Data); | |
4027 } | |
3263 | 4028 #endif /* not NEW_GC */ |
934 | 4029 |
1204 | 4030 void |
4031 free_misc_user_data (Lisp_Object ptr) | |
4032 { | |
3263 | 4033 #ifdef NEW_GC |
2720 | 4034 free_lrecord (ptr); |
3263 | 4035 #else /* not NEW_GC */ |
1204 | 4036 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (misc_user_data, Lisp_Misc_User_Data, XMISC_USER_DATA (ptr)); |
3263 | 4037 #endif /* not NEW_GC */ |
1204 | 4038 } |
4039 | |
4040 #endif /* EVENT_DATA_AS_OBJECTS */ | |
934 | 4041 |
3263 | 4042 #ifndef NEW_GC |
428 | 4043 static void |
4044 sweep_markers (void) | |
4045 { | |
4046 #define UNMARK_marker(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader)) | |
4047 #define ADDITIONAL_FREE_marker(ptr) \ | |
4048 do { Lisp_Object tem; \ | |
793 | 4049 tem = wrap_marker (ptr); \ |
428 | 4050 unchain_marker (tem); \ |
4051 } while (0) | |
4052 | |
440 | 4053 SWEEP_FIXED_TYPE_BLOCK (marker, Lisp_Marker); |
428 | 4054 } |
3263 | 4055 #endif /* not NEW_GC */ |
428 | 4056 |
4057 /* Explicitly free a marker. */ | |
4058 void | |
1204 | 4059 free_marker (Lisp_Object ptr) |
428 | 4060 { |
3263 | 4061 #ifdef NEW_GC |
2720 | 4062 free_lrecord (ptr); |
3263 | 4063 #else /* not NEW_GC */ |
1204 | 4064 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (marker, Lisp_Marker, XMARKER (ptr)); |
3263 | 4065 #endif /* not NEW_GC */ |
428 | 4066 } |
4067 | |
4068 | |
4069 #if defined (MULE) && defined (VERIFY_STRING_CHARS_INTEGRITY) | |
4070 | |
4071 static void | |
4072 verify_string_chars_integrity (void) | |
4073 { | |
4074 struct string_chars_block *sb; | |
4075 | |
4076 /* Scan each existing string block sequentially, string by string. */ | |
4077 for (sb = first_string_chars_block; sb; sb = sb->next) | |
4078 { | |
4079 int pos = 0; | |
4080 /* POS is the index of the next string in the block. */ | |
4081 while (pos < sb->pos) | |
4082 { | |
4083 struct string_chars *s_chars = | |
4084 (struct string_chars *) &(sb->string_chars[pos]); | |
438 | 4085 Lisp_String *string; |
428 | 4086 int size; |
4087 int fullsize; | |
4088 | |
454 | 4089 /* If the string_chars struct is marked as free (i.e. the |
4090 STRING pointer is NULL) then this is an unused chunk of | |
4091 string storage. (See below.) */ | |
4092 | |
4093 if (STRING_CHARS_FREE_P (s_chars)) | |
428 | 4094 { |
4095 fullsize = ((struct unused_string_chars *) s_chars)->fullsize; | |
4096 pos += fullsize; | |
4097 continue; | |
4098 } | |
4099 | |
4100 string = s_chars->string; | |
4101 /* Must be 32-bit aligned. */ | |
4102 assert ((((int) string) & 3) == 0); | |
4103 | |
793 | 4104 size = string->size_; |
428 | 4105 fullsize = STRING_FULLSIZE (size); |
4106 | |
4107 assert (!BIG_STRING_FULLSIZE_P (fullsize)); | |
2720 | 4108 assert (XSTRING_DATA (string) == s_chars->chars); |
428 | 4109 pos += fullsize; |
4110 } | |
4111 assert (pos == sb->pos); | |
4112 } | |
4113 } | |
4114 | |
1204 | 4115 #endif /* defined (MULE) && defined (VERIFY_STRING_CHARS_INTEGRITY) */ |
428 | 4116 |
3092 | 4117 #ifndef NEW_GC |
428 | 4118 /* Compactify string chars, relocating the reference to each -- |
4119 free any empty string_chars_block we see. */ | |
3092 | 4120 void |
428 | 4121 compact_string_chars (void) |
4122 { | |
4123 struct string_chars_block *to_sb = first_string_chars_block; | |
4124 int to_pos = 0; | |
4125 struct string_chars_block *from_sb; | |
4126 | |
4127 /* Scan each existing string block sequentially, string by string. */ | |
4128 for (from_sb = first_string_chars_block; from_sb; from_sb = from_sb->next) | |
4129 { | |
4130 int from_pos = 0; | |
4131 /* FROM_POS is the index of the next string in the block. */ | |
4132 while (from_pos < from_sb->pos) | |
4133 { | |
4134 struct string_chars *from_s_chars = | |
4135 (struct string_chars *) &(from_sb->string_chars[from_pos]); | |
4136 struct string_chars *to_s_chars; | |
438 | 4137 Lisp_String *string; |
428 | 4138 int size; |
4139 int fullsize; | |
4140 | |
454 | 4141 /* If the string_chars struct is marked as free (i.e. the |
4142 STRING pointer is NULL) then this is an unused chunk of | |
4143 string storage. This happens under Mule when a string's | |
4144 size changes in such a way that its fullsize changes. | |
4145 (Strings can change size because a different-length | |
4146 character can be substituted for another character.) | |
4147 In this case, after the bogus string pointer is the | |
4148 "fullsize" of this entry, i.e. how many bytes to skip. */ | |
4149 | |
4150 if (STRING_CHARS_FREE_P (from_s_chars)) | |
428 | 4151 { |
4152 fullsize = ((struct unused_string_chars *) from_s_chars)->fullsize; | |
4153 from_pos += fullsize; | |
4154 continue; | |
4155 } | |
4156 | |
4157 string = from_s_chars->string; | |
1204 | 4158 gc_checking_assert (!(LRECORD_FREE_P (string))); |
428 | 4159 |
793 | 4160 size = string->size_; |
428 | 4161 fullsize = STRING_FULLSIZE (size); |
4162 | |
442 | 4163 gc_checking_assert (! BIG_STRING_FULLSIZE_P (fullsize)); |
428 | 4164 |
4165 /* Just skip it if it isn't marked. */ | |
771 | 4166 if (! MARKED_RECORD_HEADER_P (&(string->u.lheader))) |
428 | 4167 { |
4168 from_pos += fullsize; | |
4169 continue; | |
4170 } | |
4171 | |
4172 /* If it won't fit in what's left of TO_SB, close TO_SB out | |
4173 and go on to the next string_chars_block. We know that TO_SB | |
4174 cannot advance past FROM_SB here since FROM_SB is large enough | |
4175 to currently contain this string. */ | |
4176 if ((to_pos + fullsize) > countof (to_sb->string_chars)) | |
4177 { | |
4178 to_sb->pos = to_pos; | |
4179 to_sb = to_sb->next; | |
4180 to_pos = 0; | |
4181 } | |
4182 | |
4183 /* Compute new address of this string | |
4184 and update TO_POS for the space being used. */ | |
4185 to_s_chars = (struct string_chars *) &(to_sb->string_chars[to_pos]); | |
4186 | |
4187 /* Copy the string_chars to the new place. */ | |
4188 if (from_s_chars != to_s_chars) | |
4189 memmove (to_s_chars, from_s_chars, fullsize); | |
4190 | |
4191 /* Relocate FROM_S_CHARS's reference */ | |
826 | 4192 set_lispstringp_data (string, &(to_s_chars->chars[0])); |
428 | 4193 |
4194 from_pos += fullsize; | |
4195 to_pos += fullsize; | |
4196 } | |
4197 } | |
4198 | |
4199 /* Set current to the last string chars block still used and | |
4200 free any that follow. */ | |
4201 { | |
4202 struct string_chars_block *victim; | |
4203 | |
4204 for (victim = to_sb->next; victim; ) | |
4205 { | |
4206 struct string_chars_block *next = victim->next; | |
1726 | 4207 xfree (victim, struct string_chars_block *); |
428 | 4208 victim = next; |
4209 } | |
4210 | |
4211 current_string_chars_block = to_sb; | |
4212 current_string_chars_block->pos = to_pos; | |
4213 current_string_chars_block->next = 0; | |
4214 } | |
4215 } | |
3092 | 4216 #endif /* not NEW_GC */ |
428 | 4217 |
3263 | 4218 #ifndef NEW_GC |
428 | 4219 #if 1 /* Hack to debug missing purecopy's */ |
4220 static int debug_string_purity; | |
4221 | |
4222 static void | |
793 | 4223 debug_string_purity_print (Lisp_Object p) |
428 | 4224 { |
4225 Charcount i; | |
826 | 4226 Charcount s = string_char_length (p); |
442 | 4227 stderr_out ("\""); |
428 | 4228 for (i = 0; i < s; i++) |
4229 { | |
867 | 4230 Ichar ch = string_ichar (p, i); |
428 | 4231 if (ch < 32 || ch >= 126) |
4232 stderr_out ("\\%03o", ch); | |
4233 else if (ch == '\\' || ch == '\"') | |
4234 stderr_out ("\\%c", ch); | |
4235 else | |
4236 stderr_out ("%c", ch); | |
4237 } | |
4238 stderr_out ("\"\n"); | |
4239 } | |
4240 #endif /* 1 */ | |
3263 | 4241 #endif /* not NEW_GC */ |
4242 | |
4243 #ifndef NEW_GC | |
428 | 4244 static void |
4245 sweep_strings (void) | |
4246 { | |
647 | 4247 int num_small_used = 0; |
4248 Bytecount num_small_bytes = 0, num_bytes = 0; | |
428 | 4249 int debug = debug_string_purity; |
4250 | |
793 | 4251 #define UNMARK_string(ptr) do { \ |
4252 Lisp_String *p = (ptr); \ | |
4253 Bytecount size = p->size_; \ | |
4254 UNMARK_RECORD_HEADER (&(p->u.lheader)); \ | |
4255 num_bytes += size; \ | |
4256 if (!BIG_STRING_SIZE_P (size)) \ | |
4257 { \ | |
4258 num_small_bytes += size; \ | |
4259 num_small_used++; \ | |
4260 } \ | |
4261 if (debug) \ | |
4262 debug_string_purity_print (wrap_string (p)); \ | |
438 | 4263 } while (0) |
4264 #define ADDITIONAL_FREE_string(ptr) do { \ | |
793 | 4265 Bytecount size = ptr->size_; \ |
438 | 4266 if (BIG_STRING_SIZE_P (size)) \ |
1726 | 4267 xfree (ptr->data_, Ibyte *); \ |
438 | 4268 } while (0) |
4269 | |
771 | 4270 SWEEP_FIXED_TYPE_BLOCK_1 (string, Lisp_String, u.lheader); |
428 | 4271 |
4272 gc_count_num_short_string_in_use = num_small_used; | |
4273 gc_count_string_total_size = num_bytes; | |
4274 gc_count_short_string_total_size = num_small_bytes; | |
4275 } | |
3263 | 4276 #endif /* not NEW_GC */ |
428 | 4277 |
3092 | 4278 #ifndef NEW_GC |
4279 void | |
4280 gc_sweep_1 (void) | |
428 | 4281 { |
4282 /* Free all unmarked records. Do this at the very beginning, | |
4283 before anything else, so that the finalize methods can safely | |
4284 examine items in the objects. sweep_lcrecords_1() makes | |
4285 sure to call all the finalize methods *before* freeing anything, | |
4286 to complete the safety. */ | |
4287 { | |
4288 int ignored; | |
4289 sweep_lcrecords_1 (&all_lcrecords, &ignored); | |
4290 } | |
4291 | |
4292 compact_string_chars (); | |
4293 | |
4294 /* Finalize methods below (called through the ADDITIONAL_FREE_foo | |
4295 macros) must be *extremely* careful to make sure they're not | |
4296 referencing freed objects. The only two existing finalize | |
4297 methods (for strings and markers) pass muster -- the string | |
4298 finalizer doesn't look at anything but its own specially- | |
4299 created block, and the marker finalizer only looks at live | |
4300 buffers (which will never be freed) and at the markers before | |
4301 and after it in the chain (which, by induction, will never be | |
4302 freed because if so, they would have already removed themselves | |
4303 from the chain). */ | |
4304 | |
4305 /* Put all unmarked strings on free list, free'ing the string chars | |
4306 of large unmarked strings */ | |
4307 sweep_strings (); | |
4308 | |
4309 /* Put all unmarked conses on free list */ | |
4310 sweep_conses (); | |
4311 | |
4312 /* Free all unmarked compiled-function objects */ | |
4313 sweep_compiled_functions (); | |
4314 | |
4315 /* Put all unmarked floats on free list */ | |
4316 sweep_floats (); | |
4317 | |
1983 | 4318 #ifdef HAVE_BIGNUM |
4319 /* Put all unmarked bignums on free list */ | |
4320 sweep_bignums (); | |
4321 #endif | |
4322 | |
4323 #ifdef HAVE_RATIO | |
4324 /* Put all unmarked ratios on free list */ | |
4325 sweep_ratios (); | |
4326 #endif | |
4327 | |
4328 #ifdef HAVE_BIGFLOAT | |
4329 /* Put all unmarked bigfloats on free list */ | |
4330 sweep_bigfloats (); | |
4331 #endif | |
4332 | |
428 | 4333 /* Put all unmarked symbols on free list */ |
4334 sweep_symbols (); | |
4335 | |
4336 /* Put all unmarked extents on free list */ | |
4337 sweep_extents (); | |
4338 | |
4339 /* Put all unmarked markers on free list. | |
4340 Dechain each one first from the buffer into which it points. */ | |
4341 sweep_markers (); | |
4342 | |
4343 sweep_events (); | |
4344 | |
1204 | 4345 #ifdef EVENT_DATA_AS_OBJECTS |
934 | 4346 sweep_key_data (); |
4347 sweep_button_data (); | |
4348 sweep_motion_data (); | |
4349 sweep_process_data (); | |
4350 sweep_timeout_data (); | |
4351 sweep_magic_data (); | |
4352 sweep_magic_eval_data (); | |
4353 sweep_eval_data (); | |
4354 sweep_misc_user_data (); | |
1204 | 4355 #endif /* EVENT_DATA_AS_OBJECTS */ |
3263 | 4356 #endif /* not NEW_GC */ |
4357 | |
4358 #ifndef NEW_GC | |
428 | 4359 #ifdef PDUMP |
442 | 4360 pdump_objects_unmark (); |
428 | 4361 #endif |
4362 } | |
3092 | 4363 #endif /* not NEW_GC */ |
428 | 4364 |
4365 /* Clearing for disksave. */ | |
4366 | |
4367 void | |
4368 disksave_object_finalization (void) | |
4369 { | |
4370 /* It's important that certain information from the environment not get | |
4371 dumped with the executable (pathnames, environment variables, etc.). | |
4372 To make it easier to tell when this has happened with strings(1) we | |
4373 clear some known-to-be-garbage blocks of memory, so that leftover | |
4374 results of old evaluation don't look like potential problems. | |
4375 But first we set some notable variables to nil and do one more GC, | |
4376 to turn those strings into garbage. | |
440 | 4377 */ |
428 | 4378 |
4379 /* Yeah, this list is pretty ad-hoc... */ | |
4380 Vprocess_environment = Qnil; | |
771 | 4381 env_initted = 0; |
428 | 4382 Vexec_directory = Qnil; |
4383 Vdata_directory = Qnil; | |
4384 Vsite_directory = Qnil; | |
4385 Vdoc_directory = Qnil; | |
4386 Vexec_path = Qnil; | |
4387 Vload_path = Qnil; | |
4388 /* Vdump_load_path = Qnil; */ | |
4389 /* Release hash tables for locate_file */ | |
4390 Flocate_file_clear_hashing (Qt); | |
771 | 4391 uncache_home_directory (); |
776 | 4392 zero_out_command_line_status_vars (); |
872 | 4393 clear_default_devices (); |
428 | 4394 |
4395 #if defined(LOADHIST) && !(defined(LOADHIST_DUMPED) || \ | |
4396 defined(LOADHIST_BUILTIN)) | |
4397 Vload_history = Qnil; | |
4398 #endif | |
4399 Vshell_file_name = Qnil; | |
4400 | |
3092 | 4401 #ifdef NEW_GC |
4402 gc_full (); | |
4403 #else /* not NEW_GC */ | |
428 | 4404 garbage_collect_1 (); |
3092 | 4405 #endif /* not NEW_GC */ |
428 | 4406 |
4407 /* Run the disksave finalization methods of all live objects. */ | |
4408 disksave_object_finalization_1 (); | |
4409 | |
3092 | 4410 #ifndef NEW_GC |
428 | 4411 /* Zero out the uninitialized (really, unused) part of the containers |
4412 for the live strings. */ | |
4413 { | |
4414 struct string_chars_block *scb; | |
4415 for (scb = first_string_chars_block; scb; scb = scb->next) | |
4416 { | |
4417 int count = sizeof (scb->string_chars) - scb->pos; | |
4418 | |
4419 assert (count >= 0 && count < STRING_CHARS_BLOCK_SIZE); | |
440 | 4420 if (count != 0) |
4421 { | |
4422 /* from the block's fill ptr to the end */ | |
4423 memset ((scb->string_chars + scb->pos), 0, count); | |
4424 } | |
428 | 4425 } |
4426 } | |
3092 | 4427 #endif /* not NEW_GC */ |
428 | 4428 |
4429 /* There, that ought to be enough... */ | |
4430 | |
4431 } | |
4432 | |
2994 | 4433 #ifdef ALLOC_TYPE_STATS |
4434 | |
2720 | 4435 static Lisp_Object |
2994 | 4436 gc_plist_hack (const Ascbyte *name, EMACS_INT value, Lisp_Object tail) |
2720 | 4437 { |
4438 /* C doesn't have local functions (or closures, or GC, or readable syntax, | |
4439 or portable numeric datatypes, or bit-vectors, or characters, or | |
4440 arrays, or exceptions, or ...) */ | |
4441 return cons3 (intern (name), make_int (value), tail); | |
4442 } | |
2775 | 4443 |
2994 | 4444 static Lisp_Object |
4445 object_memory_usage_stats (int set_total_gc_usage) | |
2720 | 4446 { |
4447 Lisp_Object pl = Qnil; | |
4448 int i; | |
2994 | 4449 EMACS_INT tgu_val = 0; |
4450 | |
3263 | 4451 #ifdef NEW_GC |
2775 | 4452 |
3461 | 4453 for (i = 0; i < countof (lrecord_implementations_table); i++) |
2720 | 4454 { |
4455 if (lrecord_stats[i].instances_in_use != 0) | |
4456 { | |
4457 char buf [255]; | |
4458 const char *name = lrecord_implementations_table[i]->name; | |
4459 int len = strlen (name); | |
4460 | |
4461 if (lrecord_stats[i].bytes_in_use_including_overhead != | |
4462 lrecord_stats[i].bytes_in_use) | |
4463 { | |
4464 sprintf (buf, "%s-storage-including-overhead", name); | |
4465 pl = gc_plist_hack (buf, | |
4466 lrecord_stats[i] | |
4467 .bytes_in_use_including_overhead, | |
4468 pl); | |
4469 } | |
4470 | |
4471 sprintf (buf, "%s-storage", name); | |
4472 pl = gc_plist_hack (buf, | |
4473 lrecord_stats[i].bytes_in_use, | |
4474 pl); | |
2994 | 4475 tgu_val += lrecord_stats[i].bytes_in_use_including_overhead; |
2720 | 4476 |
4477 if (name[len-1] == 's') | |
4478 sprintf (buf, "%ses-used", name); | |
4479 else | |
4480 sprintf (buf, "%ss-used", name); | |
4481 pl = gc_plist_hack (buf, lrecord_stats[i].instances_in_use, pl); | |
4482 } | |
4483 } | |
2994 | 4484 |
3263 | 4485 #else /* not NEW_GC */ |
428 | 4486 |
4487 #define HACK_O_MATIC(type, name, pl) do { \ | |
2994 | 4488 EMACS_INT s = 0; \ |
428 | 4489 struct type##_block *x = current_##type##_block; \ |
4490 while (x) { s += sizeof (*x) + MALLOC_OVERHEAD; x = x->prev; } \ | |
2994 | 4491 tgu_val += s; \ |
428 | 4492 (pl) = gc_plist_hack ((name), s, (pl)); \ |
4493 } while (0) | |
4494 | |
442 | 4495 for (i = 0; i < lrecord_type_count; i++) |
428 | 4496 { |
4497 if (lcrecord_stats[i].bytes_in_use != 0 | |
4498 || lcrecord_stats[i].bytes_freed != 0 | |
4499 || lcrecord_stats[i].instances_on_free_list != 0) | |
4500 { | |
4501 char buf [255]; | |
442 | 4502 const char *name = lrecord_implementations_table[i]->name; |
428 | 4503 int len = strlen (name); |
4504 | |
4505 sprintf (buf, "%s-storage", name); | |
4506 pl = gc_plist_hack (buf, lcrecord_stats[i].bytes_in_use, pl); | |
2994 | 4507 tgu_val += lcrecord_stats[i].bytes_in_use; |
428 | 4508 /* Okay, simple pluralization check for `symbol-value-varalias' */ |
4509 if (name[len-1] == 's') | |
4510 sprintf (buf, "%ses-freed", name); | |
4511 else | |
4512 sprintf (buf, "%ss-freed", name); | |
4513 if (lcrecord_stats[i].instances_freed != 0) | |
4514 pl = gc_plist_hack (buf, lcrecord_stats[i].instances_freed, pl); | |
4515 if (name[len-1] == 's') | |
4516 sprintf (buf, "%ses-on-free-list", name); | |
4517 else | |
4518 sprintf (buf, "%ss-on-free-list", name); | |
4519 if (lcrecord_stats[i].instances_on_free_list != 0) | |
4520 pl = gc_plist_hack (buf, lcrecord_stats[i].instances_on_free_list, | |
4521 pl); | |
4522 if (name[len-1] == 's') | |
4523 sprintf (buf, "%ses-used", name); | |
4524 else | |
4525 sprintf (buf, "%ss-used", name); | |
4526 pl = gc_plist_hack (buf, lcrecord_stats[i].instances_in_use, pl); | |
4527 } | |
4528 } | |
4529 | |
4530 HACK_O_MATIC (extent, "extent-storage", pl); | |
4531 pl = gc_plist_hack ("extents-free", gc_count_num_extent_freelist, pl); | |
4532 pl = gc_plist_hack ("extents-used", gc_count_num_extent_in_use, pl); | |
4533 HACK_O_MATIC (event, "event-storage", pl); | |
4534 pl = gc_plist_hack ("events-free", gc_count_num_event_freelist, pl); | |
4535 pl = gc_plist_hack ("events-used", gc_count_num_event_in_use, pl); | |
4536 HACK_O_MATIC (marker, "marker-storage", pl); | |
4537 pl = gc_plist_hack ("markers-free", gc_count_num_marker_freelist, pl); | |
4538 pl = gc_plist_hack ("markers-used", gc_count_num_marker_in_use, pl); | |
4539 HACK_O_MATIC (float, "float-storage", pl); | |
4540 pl = gc_plist_hack ("floats-free", gc_count_num_float_freelist, pl); | |
4541 pl = gc_plist_hack ("floats-used", gc_count_num_float_in_use, pl); | |
1983 | 4542 #ifdef HAVE_BIGNUM |
4543 HACK_O_MATIC (bignum, "bignum-storage", pl); | |
4544 pl = gc_plist_hack ("bignums-free", gc_count_num_bignum_freelist, pl); | |
4545 pl = gc_plist_hack ("bignums-used", gc_count_num_bignum_in_use, pl); | |
4546 #endif /* HAVE_BIGNUM */ | |
4547 #ifdef HAVE_RATIO | |
4548 HACK_O_MATIC (ratio, "ratio-storage", pl); | |
4549 pl = gc_plist_hack ("ratios-free", gc_count_num_ratio_freelist, pl); | |
4550 pl = gc_plist_hack ("ratios-used", gc_count_num_ratio_in_use, pl); | |
4551 #endif /* HAVE_RATIO */ | |
4552 #ifdef HAVE_BIGFLOAT | |
4553 HACK_O_MATIC (bigfloat, "bigfloat-storage", pl); | |
4554 pl = gc_plist_hack ("bigfloats-free", gc_count_num_bigfloat_freelist, pl); | |
4555 pl = gc_plist_hack ("bigfloats-used", gc_count_num_bigfloat_in_use, pl); | |
4556 #endif /* HAVE_BIGFLOAT */ | |
428 | 4557 HACK_O_MATIC (string, "string-header-storage", pl); |
4558 pl = gc_plist_hack ("long-strings-total-length", | |
4559 gc_count_string_total_size | |
4560 - gc_count_short_string_total_size, pl); | |
4561 HACK_O_MATIC (string_chars, "short-string-storage", pl); | |
4562 pl = gc_plist_hack ("short-strings-total-length", | |
4563 gc_count_short_string_total_size, pl); | |
4564 pl = gc_plist_hack ("strings-free", gc_count_num_string_freelist, pl); | |
4565 pl = gc_plist_hack ("long-strings-used", | |
4566 gc_count_num_string_in_use | |
4567 - gc_count_num_short_string_in_use, pl); | |
4568 pl = gc_plist_hack ("short-strings-used", | |
4569 gc_count_num_short_string_in_use, pl); | |
4570 | |
4571 HACK_O_MATIC (compiled_function, "compiled-function-storage", pl); | |
4572 pl = gc_plist_hack ("compiled-functions-free", | |
4573 gc_count_num_compiled_function_freelist, pl); | |
4574 pl = gc_plist_hack ("compiled-functions-used", | |
4575 gc_count_num_compiled_function_in_use, pl); | |
4576 | |
4577 HACK_O_MATIC (symbol, "symbol-storage", pl); | |
4578 pl = gc_plist_hack ("symbols-free", gc_count_num_symbol_freelist, pl); | |
4579 pl = gc_plist_hack ("symbols-used", gc_count_num_symbol_in_use, pl); | |
4580 | |
4581 HACK_O_MATIC (cons, "cons-storage", pl); | |
4582 pl = gc_plist_hack ("conses-free", gc_count_num_cons_freelist, pl); | |
4583 pl = gc_plist_hack ("conses-used", gc_count_num_cons_in_use, pl); | |
4584 | |
2994 | 4585 #undef HACK_O_MATIC |
4586 | |
3263 | 4587 #endif /* NEW_GC */ |
2994 | 4588 |
4589 if (set_total_gc_usage) | |
4590 { | |
4591 total_gc_usage = tgu_val; | |
4592 total_gc_usage_set = 1; | |
4593 } | |
4594 | |
4595 return pl; | |
4596 } | |
4597 | |
4598 DEFUN("object-memory-usage-stats", Fobject_memory_usage_stats, 0, 0 ,"", /* | |
4599 Return statistics about memory usage of Lisp objects. | |
4600 */ | |
4601 ()) | |
4602 { | |
4603 return object_memory_usage_stats (0); | |
4604 } | |
4605 | |
4606 #endif /* ALLOC_TYPE_STATS */ | |
4607 | |
4608 /* Debugging aids. */ | |
4609 | |
4610 DEFUN ("garbage-collect", Fgarbage_collect, 0, 0, "", /* | |
4611 Reclaim storage for Lisp objects no longer needed. | |
4612 Return info on amount of space in use: | |
4613 ((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS) | |
4614 (USED-MARKERS . FREE-MARKERS) USED-STRING-CHARS USED-VECTOR-SLOTS | |
4615 PLIST) | |
4616 where `PLIST' is a list of alternating keyword/value pairs providing | |
4617 more detailed information. | |
4618 Garbage collection happens automatically if you cons more than | |
4619 `gc-cons-threshold' bytes of Lisp data since previous garbage collection. | |
4620 */ | |
4621 ()) | |
4622 { | |
4623 /* Record total usage for purposes of determining next GC */ | |
3092 | 4624 #ifdef NEW_GC |
4625 gc_full (); | |
4626 #else /* not NEW_GC */ | |
2994 | 4627 garbage_collect_1 (); |
3092 | 4628 #endif /* not NEW_GC */ |
2994 | 4629 |
4630 /* This will get set to 1, and total_gc_usage computed, as part of the | |
4631 call to object_memory_usage_stats() -- if ALLOC_TYPE_STATS is enabled. */ | |
4632 total_gc_usage_set = 0; | |
4633 #ifdef ALLOC_TYPE_STATS | |
428 | 4634 /* The things we do for backwards-compatibility */ |
3263 | 4635 #ifdef NEW_GC |
2994 | 4636 return |
4637 list6 | |
4638 (Fcons (make_int (lrecord_stats[lrecord_type_cons].instances_in_use), | |
4639 make_int (lrecord_stats[lrecord_type_cons] | |
4640 .bytes_in_use_including_overhead)), | |
4641 Fcons (make_int (lrecord_stats[lrecord_type_symbol].instances_in_use), | |
4642 make_int (lrecord_stats[lrecord_type_symbol] | |
4643 .bytes_in_use_including_overhead)), | |
4644 Fcons (make_int (lrecord_stats[lrecord_type_marker].instances_in_use), | |
4645 make_int (lrecord_stats[lrecord_type_marker] | |
4646 .bytes_in_use_including_overhead)), | |
4647 make_int (lrecord_stats[lrecord_type_string] | |
4648 .bytes_in_use_including_overhead), | |
4649 make_int (lrecord_stats[lrecord_type_vector] | |
4650 .bytes_in_use_including_overhead), | |
4651 object_memory_usage_stats (1)); | |
3263 | 4652 #else /* not NEW_GC */ |
428 | 4653 return |
4654 list6 (Fcons (make_int (gc_count_num_cons_in_use), | |
4655 make_int (gc_count_num_cons_freelist)), | |
4656 Fcons (make_int (gc_count_num_symbol_in_use), | |
4657 make_int (gc_count_num_symbol_freelist)), | |
4658 Fcons (make_int (gc_count_num_marker_in_use), | |
4659 make_int (gc_count_num_marker_freelist)), | |
4660 make_int (gc_count_string_total_size), | |
2994 | 4661 make_int (lcrecord_stats[lrecord_type_vector].bytes_in_use + |
4662 lcrecord_stats[lrecord_type_vector].bytes_freed), | |
4663 object_memory_usage_stats (1)); | |
3263 | 4664 #endif /* not NEW_GC */ |
2994 | 4665 #else /* not ALLOC_TYPE_STATS */ |
4666 return Qnil; | |
4667 #endif /* ALLOC_TYPE_STATS */ | |
4668 } | |
428 | 4669 |
4670 DEFUN ("consing-since-gc", Fconsing_since_gc, 0, 0, "", /* | |
4671 Return the number of bytes consed since the last garbage collection. | |
4672 \"Consed\" is a misnomer in that this actually counts allocation | |
4673 of all different kinds of objects, not just conses. | |
4674 | |
4675 If this value exceeds `gc-cons-threshold', a garbage collection happens. | |
4676 */ | |
4677 ()) | |
4678 { | |
4679 return make_int (consing_since_gc); | |
4680 } | |
4681 | |
440 | 4682 #if 0 |
444 | 4683 DEFUN ("memory-limit", Fmemory_limit, 0, 0, 0, /* |
801 | 4684 Return the address of the last byte XEmacs has allocated, divided by 1024. |
4685 This may be helpful in debugging XEmacs's memory usage. | |
428 | 4686 The value is divided by 1024 to make sure it will fit in a lisp integer. |
4687 */ | |
4688 ()) | |
4689 { | |
4690 return make_int ((EMACS_INT) sbrk (0) / 1024); | |
4691 } | |
440 | 4692 #endif |
428 | 4693 |
2994 | 4694 DEFUN ("total-memory-usage", Ftotal_memory_usage, 0, 0, 0, /* |
801 | 4695 Return the total number of bytes used by the data segment in XEmacs. |
4696 This may be helpful in debugging XEmacs's memory usage. | |
2994 | 4697 NOTE: This may or may not be accurate! It is hard to determine this |
4698 value in a system-independent fashion. On Windows, for example, the | |
4699 returned number tends to be much greater than reality. | |
801 | 4700 */ |
4701 ()) | |
4702 { | |
4703 return make_int (total_data_usage ()); | |
4704 } | |
4705 | |
2994 | 4706 #ifdef ALLOC_TYPE_STATS |
4707 DEFUN ("object-memory-usage", Fobject_memory_usage, 0, 0, 0, /* | |
4708 Return total number of bytes used for object storage in XEmacs. | |
4709 This may be helpful in debugging XEmacs's memory usage. | |
4710 See also `consing-since-gc' and `object-memory-usage-stats'. | |
4711 */ | |
4712 ()) | |
4713 { | |
4714 return make_int (total_gc_usage + consing_since_gc); | |
4715 } | |
4716 #endif /* ALLOC_TYPE_STATS */ | |
4717 | |
851 | 4718 void |
4719 recompute_funcall_allocation_flag (void) | |
4720 { | |
887 | 4721 funcall_allocation_flag = |
4722 need_to_garbage_collect || | |
4723 need_to_check_c_alloca || | |
4724 need_to_signal_post_gc; | |
851 | 4725 } |
4726 | |
428 | 4727 |
4728 int | |
4729 object_dead_p (Lisp_Object obj) | |
4730 { | |
4731 return ((BUFFERP (obj) && !BUFFER_LIVE_P (XBUFFER (obj))) || | |
4732 (FRAMEP (obj) && !FRAME_LIVE_P (XFRAME (obj))) || | |
4733 (WINDOWP (obj) && !WINDOW_LIVE_P (XWINDOW (obj))) || | |
4734 (DEVICEP (obj) && !DEVICE_LIVE_P (XDEVICE (obj))) || | |
4735 (CONSOLEP (obj) && !CONSOLE_LIVE_P (XCONSOLE (obj))) || | |
4736 (EVENTP (obj) && !EVENT_LIVE_P (XEVENT (obj))) || | |
4737 (EXTENTP (obj) && !EXTENT_LIVE_P (XEXTENT (obj)))); | |
4738 } | |
4739 | |
4740 #ifdef MEMORY_USAGE_STATS | |
4741 | |
4742 /* Attempt to determine the actual amount of space that is used for | |
4743 the block allocated starting at PTR, supposedly of size "CLAIMED_SIZE". | |
4744 | |
4745 It seems that the following holds: | |
4746 | |
4747 1. When using the old allocator (malloc.c): | |
4748 | |
4749 -- blocks are always allocated in chunks of powers of two. For | |
4750 each block, there is an overhead of 8 bytes if rcheck is not | |
4751 defined, 20 bytes if it is defined. In other words, a | |
4752 one-byte allocation needs 8 bytes of overhead for a total of | |
4753 9 bytes, and needs to have 16 bytes of memory chunked out for | |
4754 it. | |
4755 | |
4756 2. When using the new allocator (gmalloc.c): | |
4757 | |
4758 -- blocks are always allocated in chunks of powers of two up | |
4759 to 4096 bytes. Larger blocks are allocated in chunks of | |
4760 an integral multiple of 4096 bytes. The minimum block | |
4761 size is 2*sizeof (void *), or 16 bytes if SUNOS_LOCALTIME_BUG | |
4762 is defined. There is no per-block overhead, but there | |
4763 is an overhead of 3*sizeof (size_t) for each 4096 bytes | |
4764 allocated. | |
4765 | |
4766 3. When using the system malloc, anything goes, but they are | |
4767 generally slower and more space-efficient than the GNU | |
4768 allocators. One possibly reasonable assumption to make | |
4769 for want of better data is that sizeof (void *), or maybe | |
4770 2 * sizeof (void *), is required as overhead and that | |
4771 blocks are allocated in the minimum required size except | |
4772 that some minimum block size is imposed (e.g. 16 bytes). */ | |
4773 | |
665 | 4774 Bytecount |
2286 | 4775 malloced_storage_size (void *UNUSED (ptr), Bytecount claimed_size, |
428 | 4776 struct overhead_stats *stats) |
4777 { | |
665 | 4778 Bytecount orig_claimed_size = claimed_size; |
428 | 4779 |
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
|
4780 #ifndef SYSTEM_MALLOC |
665 | 4781 if (claimed_size < (Bytecount) (2 * sizeof (void *))) |
428 | 4782 claimed_size = 2 * sizeof (void *); |
4783 # ifdef SUNOS_LOCALTIME_BUG | |
4784 if (claimed_size < 16) | |
4785 claimed_size = 16; | |
4786 # endif | |
4787 if (claimed_size < 4096) | |
4788 { | |
2260 | 4789 /* fxg: rename log->log2 to supress gcc3 shadow warning */ |
4790 int log2 = 1; | |
428 | 4791 |
4792 /* compute the log base two, more or less, then use it to compute | |
4793 the block size needed. */ | |
4794 claimed_size--; | |
4795 /* It's big, it's heavy, it's wood! */ | |
4796 while ((claimed_size /= 2) != 0) | |
2260 | 4797 ++log2; |
428 | 4798 claimed_size = 1; |
4799 /* It's better than bad, it's good! */ | |
2260 | 4800 while (log2 > 0) |
428 | 4801 { |
4802 claimed_size *= 2; | |
2260 | 4803 log2--; |
428 | 4804 } |
4805 /* We have to come up with some average about the amount of | |
4806 blocks used. */ | |
665 | 4807 if ((Bytecount) (rand () & 4095) < claimed_size) |
428 | 4808 claimed_size += 3 * sizeof (void *); |
4809 } | |
4810 else | |
4811 { | |
4812 claimed_size += 4095; | |
4813 claimed_size &= ~4095; | |
4814 claimed_size += (claimed_size / 4096) * 3 * sizeof (size_t); | |
4815 } | |
4816 | |
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
|
4817 #else |
428 | 4818 |
4819 if (claimed_size < 16) | |
4820 claimed_size = 16; | |
4821 claimed_size += 2 * sizeof (void *); | |
4822 | |
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
|
4823 #endif /* system allocator */ |
428 | 4824 |
4825 if (stats) | |
4826 { | |
4827 stats->was_requested += orig_claimed_size; | |
4828 stats->malloc_overhead += claimed_size - orig_claimed_size; | |
4829 } | |
4830 return claimed_size; | |
4831 } | |
4832 | |
3263 | 4833 #ifndef NEW_GC |
665 | 4834 Bytecount |
4835 fixed_type_block_overhead (Bytecount size) | |
428 | 4836 { |
665 | 4837 Bytecount per_block = TYPE_ALLOC_SIZE (cons, unsigned char); |
4838 Bytecount overhead = 0; | |
4839 Bytecount storage_size = malloced_storage_size (0, per_block, 0); | |
428 | 4840 while (size >= per_block) |
4841 { | |
4842 size -= per_block; | |
4843 overhead += sizeof (void *) + per_block - storage_size; | |
4844 } | |
4845 if (rand () % per_block < size) | |
4846 overhead += sizeof (void *) + per_block - storage_size; | |
4847 return overhead; | |
4848 } | |
3263 | 4849 #endif /* not NEW_GC */ |
428 | 4850 #endif /* MEMORY_USAGE_STATS */ |
4851 | |
4852 | |
4853 /* Initialization */ | |
771 | 4854 static void |
1204 | 4855 common_init_alloc_early (void) |
428 | 4856 { |
771 | 4857 #ifndef Qzero |
4858 Qzero = make_int (0); /* Only used if Lisp_Object is a union type */ | |
4859 #endif | |
4860 | |
4861 #ifndef Qnull_pointer | |
4862 /* C guarantees that Qnull_pointer will be initialized to all 0 bits, | |
4863 so the following is actually a no-op. */ | |
793 | 4864 Qnull_pointer = wrap_pointer_1 (0); |
771 | 4865 #endif |
4866 | |
3263 | 4867 #ifndef NEW_GC |
428 | 4868 breathing_space = 0; |
4869 all_lcrecords = 0; | |
3263 | 4870 #endif /* not NEW_GC */ |
428 | 4871 ignore_malloc_warnings = 1; |
4872 #ifdef DOUG_LEA_MALLOC | |
4873 mallopt (M_TRIM_THRESHOLD, 128*1024); /* trim threshold */ | |
4874 mallopt (M_MMAP_THRESHOLD, 64*1024); /* mmap threshold */ | |
4875 #if 0 /* Moved to emacs.c */ | |
4876 mallopt (M_MMAP_MAX, 64); /* max. number of mmap'ed areas */ | |
4877 #endif | |
4878 #endif | |
3092 | 4879 #ifndef NEW_GC |
2720 | 4880 init_string_chars_alloc (); |
428 | 4881 init_string_alloc (); |
4882 init_string_chars_alloc (); | |
4883 init_cons_alloc (); | |
4884 init_symbol_alloc (); | |
4885 init_compiled_function_alloc (); | |
4886 init_float_alloc (); | |
1983 | 4887 #ifdef HAVE_BIGNUM |
4888 init_bignum_alloc (); | |
4889 #endif | |
4890 #ifdef HAVE_RATIO | |
4891 init_ratio_alloc (); | |
4892 #endif | |
4893 #ifdef HAVE_BIGFLOAT | |
4894 init_bigfloat_alloc (); | |
4895 #endif | |
428 | 4896 init_marker_alloc (); |
4897 init_extent_alloc (); | |
4898 init_event_alloc (); | |
1204 | 4899 #ifdef EVENT_DATA_AS_OBJECTS |
934 | 4900 init_key_data_alloc (); |
4901 init_button_data_alloc (); | |
4902 init_motion_data_alloc (); | |
4903 init_process_data_alloc (); | |
4904 init_timeout_data_alloc (); | |
4905 init_magic_data_alloc (); | |
4906 init_magic_eval_data_alloc (); | |
4907 init_eval_data_alloc (); | |
4908 init_misc_user_data_alloc (); | |
1204 | 4909 #endif /* EVENT_DATA_AS_OBJECTS */ |
3263 | 4910 #endif /* not NEW_GC */ |
428 | 4911 |
4912 ignore_malloc_warnings = 0; | |
4913 | |
452 | 4914 if (staticpros_nodump) |
4915 Dynarr_free (staticpros_nodump); | |
4916 staticpros_nodump = Dynarr_new2 (Lisp_Object_ptr_dynarr, Lisp_Object *); | |
4917 Dynarr_resize (staticpros_nodump, 100); /* merely a small optimization */ | |
771 | 4918 #ifdef DEBUG_XEMACS |
4919 if (staticpro_nodump_names) | |
4920 Dynarr_free (staticpro_nodump_names); | |
4921 staticpro_nodump_names = Dynarr_new2 (char_ptr_dynarr, char *); | |
4922 Dynarr_resize (staticpro_nodump_names, 100); /* ditto */ | |
4923 #endif | |
428 | 4924 |
3263 | 4925 #ifdef NEW_GC |
2720 | 4926 mcpros = Dynarr_new2 (Lisp_Object_dynarr, Lisp_Object); |
4927 Dynarr_resize (mcpros, 1410); /* merely a small optimization */ | |
4928 dump_add_root_block_ptr (&mcpros, &mcpros_description); | |
4929 #ifdef DEBUG_XEMACS | |
4930 mcpro_names = Dynarr_new2 (char_ptr_dynarr, char *); | |
4931 Dynarr_resize (mcpro_names, 1410); /* merely a small optimization */ | |
4932 dump_add_root_block_ptr (&mcpro_names, &mcpro_names_description); | |
4933 #endif | |
3263 | 4934 #endif /* NEW_GC */ |
2720 | 4935 |
428 | 4936 consing_since_gc = 0; |
851 | 4937 need_to_check_c_alloca = 0; |
4938 funcall_allocation_flag = 0; | |
4939 funcall_alloca_count = 0; | |
814 | 4940 |
428 | 4941 lrecord_uid_counter = 259; |
3263 | 4942 #ifndef NEW_GC |
428 | 4943 debug_string_purity = 0; |
3263 | 4944 #endif /* not NEW_GC */ |
428 | 4945 |
800 | 4946 #ifdef ERROR_CHECK_TYPES |
428 | 4947 ERROR_ME.really_unlikely_name_to_have_accidentally_in_a_non_errb_structure = |
4948 666; | |
4949 ERROR_ME_NOT. | |
4950 really_unlikely_name_to_have_accidentally_in_a_non_errb_structure = 42; | |
4951 ERROR_ME_WARN. | |
4952 really_unlikely_name_to_have_accidentally_in_a_non_errb_structure = | |
4953 3333632; | |
793 | 4954 ERROR_ME_DEBUG_WARN. |
4955 really_unlikely_name_to_have_accidentally_in_a_non_errb_structure = | |
4956 8675309; | |
800 | 4957 #endif /* ERROR_CHECK_TYPES */ |
428 | 4958 } |
4959 | |
3263 | 4960 #ifndef NEW_GC |
771 | 4961 static void |
4962 init_lcrecord_lists (void) | |
4963 { | |
4964 int i; | |
4965 | |
4966 for (i = 0; i < countof (lrecord_implementations_table); i++) | |
4967 { | |
4968 all_lcrecord_lists[i] = Qzero; /* Qnil not yet set */ | |
4969 staticpro_nodump (&all_lcrecord_lists[i]); | |
4970 } | |
4971 } | |
3263 | 4972 #endif /* not NEW_GC */ |
771 | 4973 |
4974 void | |
1204 | 4975 init_alloc_early (void) |
771 | 4976 { |
1204 | 4977 #if defined (__cplusplus) && defined (ERROR_CHECK_GC) |
4978 static struct gcpro initial_gcpro; | |
4979 | |
4980 initial_gcpro.next = 0; | |
4981 initial_gcpro.var = &Qnil; | |
4982 initial_gcpro.nvars = 1; | |
4983 gcprolist = &initial_gcpro; | |
4984 #else | |
4985 gcprolist = 0; | |
4986 #endif /* defined (__cplusplus) && defined (ERROR_CHECK_GC) */ | |
4987 } | |
4988 | |
4989 void | |
4990 reinit_alloc_early (void) | |
4991 { | |
4992 common_init_alloc_early (); | |
3263 | 4993 #ifndef NEW_GC |
771 | 4994 init_lcrecord_lists (); |
3263 | 4995 #endif /* not NEW_GC */ |
771 | 4996 } |
4997 | |
428 | 4998 void |
4999 init_alloc_once_early (void) | |
5000 { | |
1204 | 5001 common_init_alloc_early (); |
428 | 5002 |
442 | 5003 { |
5004 int i; | |
5005 for (i = 0; i < countof (lrecord_implementations_table); i++) | |
5006 lrecord_implementations_table[i] = 0; | |
5007 } | |
5008 | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3063
diff
changeset
|
5009 INIT_LISP_OBJECT (cons); |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3063
diff
changeset
|
5010 INIT_LISP_OBJECT (vector); |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3063
diff
changeset
|
5011 INIT_LISP_OBJECT (string); |
3092 | 5012 #ifdef NEW_GC |
5118
e0db3c197671
merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
diff
changeset
|
5013 INIT_LISP_OBJECT (string_indirect_data); |
e0db3c197671
merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
diff
changeset
|
5014 INIT_LISP_OBJECT (string_direct_data); |
3092 | 5015 #endif /* NEW_GC */ |
3263 | 5016 #ifndef NEW_GC |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3063
diff
changeset
|
5017 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
|
5018 INIT_LISP_OBJECT (free); |
3263 | 5019 #endif /* not NEW_GC */ |
428 | 5020 |
452 | 5021 staticpros = Dynarr_new2 (Lisp_Object_ptr_dynarr, Lisp_Object *); |
5022 Dynarr_resize (staticpros, 1410); /* merely a small optimization */ | |
2367 | 5023 dump_add_root_block_ptr (&staticpros, &staticpros_description); |
771 | 5024 #ifdef DEBUG_XEMACS |
5025 staticpro_names = Dynarr_new2 (char_ptr_dynarr, char *); | |
5026 Dynarr_resize (staticpro_names, 1410); /* merely a small optimization */ | |
2367 | 5027 dump_add_root_block_ptr (&staticpro_names, &staticpro_names_description); |
771 | 5028 #endif |
5029 | |
3263 | 5030 #ifdef NEW_GC |
2720 | 5031 mcpros = Dynarr_new2 (Lisp_Object_dynarr, Lisp_Object); |
5032 Dynarr_resize (mcpros, 1410); /* merely a small optimization */ | |
5033 dump_add_root_block_ptr (&mcpros, &mcpros_description); | |
5034 #ifdef DEBUG_XEMACS | |
5035 mcpro_names = Dynarr_new2 (char_ptr_dynarr, char *); | |
5036 Dynarr_resize (mcpro_names, 1410); /* merely a small optimization */ | |
5037 dump_add_root_block_ptr (&mcpro_names, &mcpro_names_description); | |
5038 #endif | |
3263 | 5039 #else /* not NEW_GC */ |
771 | 5040 init_lcrecord_lists (); |
3263 | 5041 #endif /* not NEW_GC */ |
428 | 5042 } |
5043 | |
5044 void | |
5045 syms_of_alloc (void) | |
5046 { | |
442 | 5047 DEFSYMBOL (Qgarbage_collecting); |
428 | 5048 |
5049 DEFSUBR (Fcons); | |
5050 DEFSUBR (Flist); | |
5051 DEFSUBR (Fvector); | |
5052 DEFSUBR (Fbit_vector); | |
5053 DEFSUBR (Fmake_byte_code); | |
5054 DEFSUBR (Fmake_list); | |
5055 DEFSUBR (Fmake_vector); | |
5056 DEFSUBR (Fmake_bit_vector); | |
5057 DEFSUBR (Fmake_string); | |
5058 DEFSUBR (Fstring); | |
5059 DEFSUBR (Fmake_symbol); | |
5060 DEFSUBR (Fmake_marker); | |
5061 DEFSUBR (Fpurecopy); | |
2994 | 5062 #ifdef ALLOC_TYPE_STATS |
5063 DEFSUBR (Fobject_memory_usage_stats); | |
5064 DEFSUBR (Fobject_memory_usage); | |
5065 #endif /* ALLOC_TYPE_STATS */ | |
428 | 5066 DEFSUBR (Fgarbage_collect); |
440 | 5067 #if 0 |
428 | 5068 DEFSUBR (Fmemory_limit); |
440 | 5069 #endif |
2994 | 5070 DEFSUBR (Ftotal_memory_usage); |
428 | 5071 DEFSUBR (Fconsing_since_gc); |
5072 } | |
5073 | |
5074 void | |
5075 vars_of_alloc (void) | |
5076 { | |
5077 #ifdef DEBUG_XEMACS | |
5078 DEFVAR_INT ("debug-allocation", &debug_allocation /* | |
5079 If non-zero, print out information to stderr about all objects allocated. | |
5080 See also `debug-allocation-backtrace-length'. | |
5081 */ ); | |
5082 debug_allocation = 0; | |
5083 | |
5084 DEFVAR_INT ("debug-allocation-backtrace-length", | |
5085 &debug_allocation_backtrace_length /* | |
5086 Length (in stack frames) of short backtrace printed out by `debug-allocation'. | |
5087 */ ); | |
5088 debug_allocation_backtrace_length = 2; | |
5089 #endif | |
5090 | |
5091 DEFVAR_BOOL ("purify-flag", &purify_flag /* | |
5092 Non-nil means loading Lisp code in order to dump an executable. | |
5093 This means that certain objects should be allocated in readonly space. | |
5094 */ ); | |
5095 } |