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