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