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