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