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