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