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