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