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