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