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