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