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
|
|
166 Error_behavior ERROR_ME, ERROR_ME_NOT, ERROR_ME_WARN;
|
|
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
|
|
239 error ("Memory exhausted");
|
|
240 }
|
|
241
|
|
242 /* like malloc and realloc but check for no memory left, and block input. */
|
|
243
|
|
244 #undef xmalloc
|
|
245 void *
|
|
246 xmalloc (size_t size)
|
|
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 *
|
|
256 xcalloc (size_t nelem, size_t elsize)
|
|
257 {
|
|
258 void *val = calloc (nelem, elsize);
|
|
259
|
|
260 if (!val && (nelem != 0)) memory_full ();
|
|
261 return val;
|
|
262 }
|
|
263
|
|
264 void *
|
|
265 xmalloc_and_zero (size_t size)
|
|
266 {
|
|
267 return xcalloc (size, sizeof (char));
|
|
268 }
|
|
269
|
|
270 #undef xrealloc
|
|
271 void *
|
|
272 xrealloc (void *block, size_t size)
|
|
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
|
|
310 deadbeef_memory (void *ptr, size_t size)
|
|
311 {
|
|
312 four_byte_t *ptr4 = (four_byte_t *) ptr;
|
|
313 size_t beefs = size >> 2;
|
|
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 *
|
|
348 allocate_lisp_storage (size_t size)
|
|
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 *
|
442
|
360 alloc_lcrecord (size_t 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;
|
|
1000 size_t size = XINT (length);
|
|
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
|
|
1055 static size_t
|
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
|
442
|
1079 static hashcode_t
|
|
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 *
|
|
1103 make_vector_internal (size_t sizei)
|
|
1104 {
|
|
1105 /* no vector_next */
|
456
|
1106 size_t sizem = FLEXIBLE_ARRAY_STRUCT_SIZEOF (Lisp_Vector, Lisp_Object,
|
|
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
|
444
|
1115 make_vector (size_t 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 *
|
428
|
1267 make_bit_vector_internal (size_t sizei)
|
|
1268 {
|
|
1269 size_t num_longs = BIT_VECTOR_LONG_STORAGE (sizei);
|
456
|
1270 size_t sizem = FLEXIBLE_ARRAY_STRUCT_SIZEOF (Lisp_Bit_Vector, unsigned long,
|
|
1271 bits, num_longs);
|
428
|
1272 Lisp_Bit_Vector *p = (Lisp_Bit_Vector *) allocate_lisp_storage (sizem);
|
442
|
1273 set_lheader_implementation (&p->lheader, &lrecord_bit_vector);
|
428
|
1274
|
|
1275 INCREMENT_CONS_COUNTER (sizem, "bit-vector");
|
|
1276
|
|
1277 bit_vector_length (p) = sizei;
|
|
1278 bit_vector_next (p) = all_bit_vectors;
|
|
1279 /* make sure the extra bits in the last long are 0; the calling
|
|
1280 functions might not set them. */
|
|
1281 p->bits[num_longs - 1] = 0;
|
|
1282 XSETBIT_VECTOR (all_bit_vectors, p);
|
|
1283 return p;
|
|
1284 }
|
|
1285
|
|
1286 Lisp_Object
|
444
|
1287 make_bit_vector (size_t length, Lisp_Object bit)
|
428
|
1288 {
|
440
|
1289 Lisp_Bit_Vector *p = make_bit_vector_internal (length);
|
428
|
1290 size_t num_longs = BIT_VECTOR_LONG_STORAGE (length);
|
|
1291
|
444
|
1292 CHECK_BIT (bit);
|
|
1293
|
|
1294 if (ZEROP (bit))
|
428
|
1295 memset (p->bits, 0, num_longs * sizeof (long));
|
|
1296 else
|
|
1297 {
|
|
1298 size_t bits_in_last = length & (LONGBITS_POWER_OF_2 - 1);
|
|
1299 memset (p->bits, ~0, num_longs * sizeof (long));
|
|
1300 /* But we have to make sure that the unused bits in the
|
|
1301 last long are 0, so that equal/hash is easy. */
|
|
1302 if (bits_in_last)
|
|
1303 p->bits[num_longs - 1] &= (1 << bits_in_last) - 1;
|
|
1304 }
|
|
1305
|
|
1306 {
|
|
1307 Lisp_Object bit_vector;
|
|
1308 XSETBIT_VECTOR (bit_vector, p);
|
|
1309 return bit_vector;
|
|
1310 }
|
|
1311 }
|
|
1312
|
|
1313 Lisp_Object
|
|
1314 make_bit_vector_from_byte_vector (unsigned char *bytevec, size_t length)
|
|
1315 {
|
460
|
1316 size_t i;
|
428
|
1317 Lisp_Bit_Vector *p = make_bit_vector_internal (length);
|
|
1318
|
|
1319 for (i = 0; i < length; i++)
|
|
1320 set_bit_vector_bit (p, i, bytevec[i]);
|
|
1321
|
|
1322 {
|
|
1323 Lisp_Object bit_vector;
|
|
1324 XSETBIT_VECTOR (bit_vector, p);
|
|
1325 return bit_vector;
|
|
1326 }
|
|
1327 }
|
|
1328
|
|
1329 DEFUN ("make-bit-vector", Fmake_bit_vector, 2, 2, 0, /*
|
444
|
1330 Return a new bit vector of length LENGTH. with each bit set to BIT.
|
|
1331 BIT must be one of the integers 0 or 1. See also the function `bit-vector'.
|
428
|
1332 */
|
444
|
1333 (length, bit))
|
428
|
1334 {
|
|
1335 CONCHECK_NATNUM (length);
|
|
1336
|
444
|
1337 return make_bit_vector (XINT (length), bit);
|
428
|
1338 }
|
|
1339
|
|
1340 DEFUN ("bit-vector", Fbit_vector, 0, MANY, 0, /*
|
|
1341 Return a newly created bit vector with specified arguments as elements.
|
|
1342 Any number of arguments, even zero arguments, are allowed.
|
444
|
1343 Each argument must be one of the integers 0 or 1.
|
428
|
1344 */
|
|
1345 (int nargs, Lisp_Object *args))
|
|
1346 {
|
|
1347 int i;
|
|
1348 Lisp_Bit_Vector *p = make_bit_vector_internal (nargs);
|
|
1349
|
|
1350 for (i = 0; i < nargs; i++)
|
|
1351 {
|
|
1352 CHECK_BIT (args[i]);
|
|
1353 set_bit_vector_bit (p, i, !ZEROP (args[i]));
|
|
1354 }
|
|
1355
|
|
1356 {
|
|
1357 Lisp_Object bit_vector;
|
|
1358 XSETBIT_VECTOR (bit_vector, p);
|
|
1359 return bit_vector;
|
|
1360 }
|
|
1361 }
|
|
1362
|
|
1363
|
|
1364 /************************************************************************/
|
|
1365 /* Compiled-function allocation */
|
|
1366 /************************************************************************/
|
|
1367
|
|
1368 DECLARE_FIXED_TYPE_ALLOC (compiled_function, Lisp_Compiled_Function);
|
|
1369 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_compiled_function 1000
|
|
1370
|
|
1371 static Lisp_Object
|
|
1372 make_compiled_function (void)
|
|
1373 {
|
|
1374 Lisp_Compiled_Function *f;
|
|
1375 Lisp_Object fun;
|
|
1376
|
|
1377 ALLOCATE_FIXED_TYPE (compiled_function, Lisp_Compiled_Function, f);
|
442
|
1378 set_lheader_implementation (&f->lheader, &lrecord_compiled_function);
|
428
|
1379
|
|
1380 f->stack_depth = 0;
|
|
1381 f->specpdl_depth = 0;
|
|
1382 f->flags.documentationp = 0;
|
|
1383 f->flags.interactivep = 0;
|
|
1384 f->flags.domainp = 0; /* I18N3 */
|
|
1385 f->instructions = Qzero;
|
|
1386 f->constants = Qzero;
|
|
1387 f->arglist = Qnil;
|
|
1388 f->doc_and_interactive = Qnil;
|
|
1389 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK
|
|
1390 f->annotated = Qnil;
|
|
1391 #endif
|
|
1392 XSETCOMPILED_FUNCTION (fun, f);
|
|
1393 return fun;
|
|
1394 }
|
|
1395
|
|
1396 DEFUN ("make-byte-code", Fmake_byte_code, 4, MANY, 0, /*
|
|
1397 Return a new compiled-function object.
|
|
1398 Usage: (arglist instructions constants stack-depth
|
|
1399 &optional doc-string interactive)
|
|
1400 Note that, unlike all other emacs-lisp functions, calling this with five
|
|
1401 arguments is NOT the same as calling it with six arguments, the last of
|
|
1402 which is nil. If the INTERACTIVE arg is specified as nil, then that means
|
|
1403 that this function was defined with `(interactive)'. If the arg is not
|
|
1404 specified, then that means the function is not interactive.
|
|
1405 This is terrible behavior which is retained for compatibility with old
|
|
1406 `.elc' files which expect these semantics.
|
|
1407 */
|
|
1408 (int nargs, Lisp_Object *args))
|
|
1409 {
|
|
1410 /* In a non-insane world this function would have this arglist...
|
|
1411 (arglist instructions constants stack_depth &optional doc_string interactive)
|
|
1412 */
|
|
1413 Lisp_Object fun = make_compiled_function ();
|
|
1414 Lisp_Compiled_Function *f = XCOMPILED_FUNCTION (fun);
|
|
1415
|
|
1416 Lisp_Object arglist = args[0];
|
|
1417 Lisp_Object instructions = args[1];
|
|
1418 Lisp_Object constants = args[2];
|
|
1419 Lisp_Object stack_depth = args[3];
|
|
1420 Lisp_Object doc_string = (nargs > 4) ? args[4] : Qnil;
|
|
1421 Lisp_Object interactive = (nargs > 5) ? args[5] : Qunbound;
|
|
1422
|
|
1423 if (nargs < 4 || nargs > 6)
|
|
1424 return Fsignal (Qwrong_number_of_arguments,
|
|
1425 list2 (intern ("make-byte-code"), make_int (nargs)));
|
|
1426
|
|
1427 /* Check for valid formal parameter list now, to allow us to use
|
|
1428 SPECBIND_FAST_UNSAFE() later in funcall_compiled_function(). */
|
|
1429 {
|
|
1430 EXTERNAL_LIST_LOOP_3 (symbol, arglist, tail)
|
|
1431 {
|
|
1432 CHECK_SYMBOL (symbol);
|
|
1433 if (EQ (symbol, Qt) ||
|
|
1434 EQ (symbol, Qnil) ||
|
|
1435 SYMBOL_IS_KEYWORD (symbol))
|
|
1436 signal_simple_error_2
|
|
1437 ("Invalid constant symbol in formal parameter list",
|
|
1438 symbol, arglist);
|
|
1439 }
|
|
1440 }
|
|
1441 f->arglist = arglist;
|
|
1442
|
|
1443 /* `instructions' is a string or a cons (string . int) for a
|
|
1444 lazy-loaded function. */
|
|
1445 if (CONSP (instructions))
|
|
1446 {
|
|
1447 CHECK_STRING (XCAR (instructions));
|
|
1448 CHECK_INT (XCDR (instructions));
|
|
1449 }
|
|
1450 else
|
|
1451 {
|
|
1452 CHECK_STRING (instructions);
|
|
1453 }
|
|
1454 f->instructions = instructions;
|
|
1455
|
|
1456 if (!NILP (constants))
|
|
1457 CHECK_VECTOR (constants);
|
|
1458 f->constants = constants;
|
|
1459
|
|
1460 CHECK_NATNUM (stack_depth);
|
442
|
1461 f->stack_depth = (unsigned short) XINT (stack_depth);
|
428
|
1462
|
|
1463 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK
|
|
1464 if (!NILP (Vcurrent_compiled_function_annotation))
|
|
1465 f->annotated = Fcopy (Vcurrent_compiled_function_annotation);
|
|
1466 else if (!NILP (Vload_file_name_internal_the_purecopy))
|
|
1467 f->annotated = Vload_file_name_internal_the_purecopy;
|
|
1468 else if (!NILP (Vload_file_name_internal))
|
|
1469 {
|
|
1470 struct gcpro gcpro1;
|
|
1471 GCPRO1 (fun); /* don't let fun get reaped */
|
|
1472 Vload_file_name_internal_the_purecopy =
|
|
1473 Ffile_name_nondirectory (Vload_file_name_internal);
|
|
1474 f->annotated = Vload_file_name_internal_the_purecopy;
|
|
1475 UNGCPRO;
|
|
1476 }
|
|
1477 #endif /* COMPILED_FUNCTION_ANNOTATION_HACK */
|
|
1478
|
|
1479 /* doc_string may be nil, string, int, or a cons (string . int).
|
|
1480 interactive may be list or string (or unbound). */
|
|
1481 f->doc_and_interactive = Qunbound;
|
|
1482 #ifdef I18N3
|
|
1483 if ((f->flags.domainp = !NILP (Vfile_domain)) != 0)
|
|
1484 f->doc_and_interactive = Vfile_domain;
|
|
1485 #endif
|
|
1486 if ((f->flags.interactivep = !UNBOUNDP (interactive)) != 0)
|
|
1487 {
|
|
1488 f->doc_and_interactive
|
|
1489 = (UNBOUNDP (f->doc_and_interactive) ? interactive :
|
|
1490 Fcons (interactive, f->doc_and_interactive));
|
|
1491 }
|
|
1492 if ((f->flags.documentationp = !NILP (doc_string)) != 0)
|
|
1493 {
|
|
1494 f->doc_and_interactive
|
|
1495 = (UNBOUNDP (f->doc_and_interactive) ? doc_string :
|
|
1496 Fcons (doc_string, f->doc_and_interactive));
|
|
1497 }
|
|
1498 if (UNBOUNDP (f->doc_and_interactive))
|
|
1499 f->doc_and_interactive = Qnil;
|
|
1500
|
|
1501 return fun;
|
|
1502 }
|
|
1503
|
|
1504
|
|
1505 /************************************************************************/
|
|
1506 /* Symbol allocation */
|
|
1507 /************************************************************************/
|
|
1508
|
440
|
1509 DECLARE_FIXED_TYPE_ALLOC (symbol, Lisp_Symbol);
|
428
|
1510 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_symbol 1000
|
|
1511
|
|
1512 DEFUN ("make-symbol", Fmake_symbol, 1, 1, 0, /*
|
|
1513 Return a newly allocated uninterned symbol whose name is NAME.
|
|
1514 Its value and function definition are void, and its property list is nil.
|
|
1515 */
|
|
1516 (name))
|
|
1517 {
|
|
1518 Lisp_Object val;
|
440
|
1519 Lisp_Symbol *p;
|
428
|
1520
|
|
1521 CHECK_STRING (name);
|
|
1522
|
440
|
1523 ALLOCATE_FIXED_TYPE (symbol, Lisp_Symbol, p);
|
442
|
1524 set_lheader_implementation (&p->lheader, &lrecord_symbol);
|
428
|
1525 p->name = XSTRING (name);
|
|
1526 p->plist = Qnil;
|
|
1527 p->value = Qunbound;
|
|
1528 p->function = Qunbound;
|
|
1529 symbol_next (p) = 0;
|
|
1530 XSETSYMBOL (val, p);
|
|
1531 return val;
|
|
1532 }
|
|
1533
|
|
1534
|
|
1535 /************************************************************************/
|
|
1536 /* Extent allocation */
|
|
1537 /************************************************************************/
|
|
1538
|
|
1539 DECLARE_FIXED_TYPE_ALLOC (extent, struct extent);
|
|
1540 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_extent 1000
|
|
1541
|
|
1542 struct extent *
|
|
1543 allocate_extent (void)
|
|
1544 {
|
|
1545 struct extent *e;
|
|
1546
|
|
1547 ALLOCATE_FIXED_TYPE (extent, struct extent, e);
|
442
|
1548 set_lheader_implementation (&e->lheader, &lrecord_extent);
|
428
|
1549 extent_object (e) = Qnil;
|
|
1550 set_extent_start (e, -1);
|
|
1551 set_extent_end (e, -1);
|
|
1552 e->plist = Qnil;
|
|
1553
|
|
1554 xzero (e->flags);
|
|
1555
|
|
1556 extent_face (e) = Qnil;
|
|
1557 e->flags.end_open = 1; /* default is for endpoints to behave like markers */
|
|
1558 e->flags.detachable = 1;
|
|
1559
|
|
1560 return e;
|
|
1561 }
|
|
1562
|
|
1563
|
|
1564 /************************************************************************/
|
|
1565 /* Event allocation */
|
|
1566 /************************************************************************/
|
|
1567
|
440
|
1568 DECLARE_FIXED_TYPE_ALLOC (event, Lisp_Event);
|
428
|
1569 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_event 1000
|
|
1570
|
|
1571 Lisp_Object
|
|
1572 allocate_event (void)
|
|
1573 {
|
|
1574 Lisp_Object val;
|
440
|
1575 Lisp_Event *e;
|
|
1576
|
|
1577 ALLOCATE_FIXED_TYPE (event, Lisp_Event, e);
|
442
|
1578 set_lheader_implementation (&e->lheader, &lrecord_event);
|
428
|
1579
|
|
1580 XSETEVENT (val, e);
|
|
1581 return val;
|
|
1582 }
|
|
1583
|
|
1584
|
|
1585 /************************************************************************/
|
|
1586 /* Marker allocation */
|
|
1587 /************************************************************************/
|
|
1588
|
440
|
1589 DECLARE_FIXED_TYPE_ALLOC (marker, Lisp_Marker);
|
428
|
1590 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_marker 1000
|
|
1591
|
|
1592 DEFUN ("make-marker", Fmake_marker, 0, 0, 0, /*
|
|
1593 Return a new marker which does not point at any place.
|
|
1594 */
|
|
1595 ())
|
|
1596 {
|
|
1597 Lisp_Object val;
|
440
|
1598 Lisp_Marker *p;
|
|
1599
|
|
1600 ALLOCATE_FIXED_TYPE (marker, Lisp_Marker, p);
|
442
|
1601 set_lheader_implementation (&p->lheader, &lrecord_marker);
|
428
|
1602 p->buffer = 0;
|
|
1603 p->memind = 0;
|
|
1604 marker_next (p) = 0;
|
|
1605 marker_prev (p) = 0;
|
|
1606 p->insertion_type = 0;
|
|
1607 XSETMARKER (val, p);
|
|
1608 return val;
|
|
1609 }
|
|
1610
|
|
1611 Lisp_Object
|
|
1612 noseeum_make_marker (void)
|
|
1613 {
|
|
1614 Lisp_Object val;
|
440
|
1615 Lisp_Marker *p;
|
|
1616
|
|
1617 NOSEEUM_ALLOCATE_FIXED_TYPE (marker, Lisp_Marker, p);
|
442
|
1618 set_lheader_implementation (&p->lheader, &lrecord_marker);
|
428
|
1619 p->buffer = 0;
|
|
1620 p->memind = 0;
|
|
1621 marker_next (p) = 0;
|
|
1622 marker_prev (p) = 0;
|
|
1623 p->insertion_type = 0;
|
|
1624 XSETMARKER (val, p);
|
|
1625 return val;
|
|
1626 }
|
|
1627
|
|
1628
|
|
1629 /************************************************************************/
|
|
1630 /* String allocation */
|
|
1631 /************************************************************************/
|
|
1632
|
|
1633 /* The data for "short" strings generally resides inside of structs of type
|
|
1634 string_chars_block. The Lisp_String structure is allocated just like any
|
|
1635 other Lisp object (except for vectors), and these are freelisted when
|
|
1636 they get garbage collected. The data for short strings get compacted,
|
|
1637 but the data for large strings do not.
|
|
1638
|
|
1639 Previously Lisp_String structures were relocated, but this caused a lot
|
|
1640 of bus-errors because the C code didn't include enough GCPRO's for
|
|
1641 strings (since EVERY REFERENCE to a short string needed to be GCPRO'd so
|
|
1642 that the reference would get relocated).
|
|
1643
|
|
1644 This new method makes things somewhat bigger, but it is MUCH safer. */
|
|
1645
|
438
|
1646 DECLARE_FIXED_TYPE_ALLOC (string, Lisp_String);
|
428
|
1647 /* strings are used and freed quite often */
|
|
1648 /* #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_string 10000 */
|
|
1649 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_string 1000
|
|
1650
|
|
1651 static Lisp_Object
|
|
1652 mark_string (Lisp_Object obj)
|
|
1653 {
|
438
|
1654 Lisp_String *ptr = XSTRING (obj);
|
428
|
1655
|
|
1656 if (CONSP (ptr->plist) && EXTENT_INFOP (XCAR (ptr->plist)))
|
|
1657 flush_cached_extent_info (XCAR (ptr->plist));
|
|
1658 return ptr->plist;
|
|
1659 }
|
|
1660
|
|
1661 static int
|
|
1662 string_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
|
|
1663 {
|
|
1664 Bytecount len;
|
|
1665 return (((len = XSTRING_LENGTH (obj1)) == XSTRING_LENGTH (obj2)) &&
|
|
1666 !memcmp (XSTRING_DATA (obj1), XSTRING_DATA (obj2), len));
|
|
1667 }
|
|
1668
|
|
1669 static const struct lrecord_description string_description[] = {
|
440
|
1670 { XD_BYTECOUNT, offsetof (Lisp_String, size) },
|
|
1671 { XD_OPAQUE_DATA_PTR, offsetof (Lisp_String, data), XD_INDIRECT(0, 1) },
|
|
1672 { XD_LISP_OBJECT, offsetof (Lisp_String, plist) },
|
428
|
1673 { XD_END }
|
|
1674 };
|
|
1675
|
442
|
1676 /* We store the string's extent info as the first element of the string's
|
|
1677 property list; and the string's MODIFF as the first or second element
|
|
1678 of the string's property list (depending on whether the extent info
|
|
1679 is present), but only if the string has been modified. This is ugly
|
|
1680 but it reduces the memory allocated for the string in the vast
|
|
1681 majority of cases, where the string is never modified and has no
|
|
1682 extent info.
|
|
1683
|
|
1684 #### This means you can't use an int as a key in a string's plist. */
|
|
1685
|
|
1686 static Lisp_Object *
|
|
1687 string_plist_ptr (Lisp_Object string)
|
|
1688 {
|
|
1689 Lisp_Object *ptr = &XSTRING (string)->plist;
|
|
1690
|
|
1691 if (CONSP (*ptr) && EXTENT_INFOP (XCAR (*ptr)))
|
|
1692 ptr = &XCDR (*ptr);
|
|
1693 if (CONSP (*ptr) && INTP (XCAR (*ptr)))
|
|
1694 ptr = &XCDR (*ptr);
|
|
1695 return ptr;
|
|
1696 }
|
|
1697
|
|
1698 static Lisp_Object
|
|
1699 string_getprop (Lisp_Object string, Lisp_Object property)
|
|
1700 {
|
|
1701 return external_plist_get (string_plist_ptr (string), property, 0, ERROR_ME);
|
|
1702 }
|
|
1703
|
|
1704 static int
|
|
1705 string_putprop (Lisp_Object string, Lisp_Object property, Lisp_Object value)
|
|
1706 {
|
|
1707 external_plist_put (string_plist_ptr (string), property, value, 0, ERROR_ME);
|
|
1708 return 1;
|
|
1709 }
|
|
1710
|
|
1711 static int
|
|
1712 string_remprop (Lisp_Object string, Lisp_Object property)
|
|
1713 {
|
|
1714 return external_remprop (string_plist_ptr (string), property, 0, ERROR_ME);
|
|
1715 }
|
|
1716
|
|
1717 static Lisp_Object
|
|
1718 string_plist (Lisp_Object string)
|
|
1719 {
|
|
1720 return *string_plist_ptr (string);
|
|
1721 }
|
|
1722
|
|
1723 /* No `finalize', or `hash' methods.
|
|
1724 internal_hash() already knows how to hash strings and finalization
|
|
1725 is done with the ADDITIONAL_FREE_string macro, which is the
|
|
1726 standard way to do finalization when using
|
|
1727 SWEEP_FIXED_TYPE_BLOCK(). */
|
|
1728 DEFINE_BASIC_LRECORD_IMPLEMENTATION_WITH_PROPS ("string", string,
|
|
1729 mark_string, print_string,
|
|
1730 0, string_equal, 0,
|
|
1731 string_description,
|
|
1732 string_getprop,
|
|
1733 string_putprop,
|
|
1734 string_remprop,
|
|
1735 string_plist,
|
|
1736 Lisp_String);
|
428
|
1737
|
|
1738 /* String blocks contain this many useful bytes. */
|
|
1739 #define STRING_CHARS_BLOCK_SIZE \
|
|
1740 ((Bytecount) (8192 - MALLOC_OVERHEAD - \
|
|
1741 ((2 * sizeof (struct string_chars_block *)) \
|
|
1742 + sizeof (EMACS_INT))))
|
|
1743 /* Block header for small strings. */
|
|
1744 struct string_chars_block
|
|
1745 {
|
|
1746 EMACS_INT pos;
|
|
1747 struct string_chars_block *next;
|
|
1748 struct string_chars_block *prev;
|
|
1749 /* Contents of string_chars_block->string_chars are interleaved
|
|
1750 string_chars structures (see below) and the actual string data */
|
|
1751 unsigned char string_chars[STRING_CHARS_BLOCK_SIZE];
|
|
1752 };
|
|
1753
|
|
1754 static struct string_chars_block *first_string_chars_block;
|
|
1755 static struct string_chars_block *current_string_chars_block;
|
|
1756
|
|
1757 /* If SIZE is the length of a string, this returns how many bytes
|
|
1758 * the string occupies in string_chars_block->string_chars
|
|
1759 * (including alignment padding).
|
|
1760 */
|
438
|
1761 #define STRING_FULLSIZE(size) \
|
|
1762 ALIGN_SIZE (((size) + 1 + sizeof (Lisp_String *)),\
|
|
1763 ALIGNOF (Lisp_String *))
|
428
|
1764
|
|
1765 #define BIG_STRING_FULLSIZE_P(fullsize) ((fullsize) >= STRING_CHARS_BLOCK_SIZE)
|
|
1766 #define BIG_STRING_SIZE_P(size) (BIG_STRING_FULLSIZE_P (STRING_FULLSIZE(size)))
|
|
1767
|
454
|
1768 #define STRING_CHARS_FREE_P(ptr) ((ptr)->string == NULL)
|
|
1769 #define MARK_STRING_CHARS_AS_FREE(ptr) ((void) ((ptr)->string = NULL))
|
|
1770
|
428
|
1771 struct string_chars
|
|
1772 {
|
438
|
1773 Lisp_String *string;
|
428
|
1774 unsigned char chars[1];
|
|
1775 };
|
|
1776
|
|
1777 struct unused_string_chars
|
|
1778 {
|
438
|
1779 Lisp_String *string;
|
428
|
1780 EMACS_INT fullsize;
|
|
1781 };
|
|
1782
|
|
1783 static void
|
|
1784 init_string_chars_alloc (void)
|
|
1785 {
|
|
1786 first_string_chars_block = xnew (struct string_chars_block);
|
|
1787 first_string_chars_block->prev = 0;
|
|
1788 first_string_chars_block->next = 0;
|
|
1789 first_string_chars_block->pos = 0;
|
|
1790 current_string_chars_block = first_string_chars_block;
|
|
1791 }
|
|
1792
|
|
1793 static struct string_chars *
|
438
|
1794 allocate_string_chars_struct (Lisp_String *string_it_goes_with,
|
428
|
1795 EMACS_INT fullsize)
|
|
1796 {
|
|
1797 struct string_chars *s_chars;
|
|
1798
|
438
|
1799 if (fullsize <=
|
|
1800 (countof (current_string_chars_block->string_chars)
|
|
1801 - current_string_chars_block->pos))
|
428
|
1802 {
|
|
1803 /* This string can fit in the current string chars block */
|
|
1804 s_chars = (struct string_chars *)
|
|
1805 (current_string_chars_block->string_chars
|
|
1806 + current_string_chars_block->pos);
|
|
1807 current_string_chars_block->pos += fullsize;
|
|
1808 }
|
|
1809 else
|
|
1810 {
|
|
1811 /* Make a new current string chars block */
|
|
1812 struct string_chars_block *new_scb = xnew (struct string_chars_block);
|
|
1813
|
|
1814 current_string_chars_block->next = new_scb;
|
|
1815 new_scb->prev = current_string_chars_block;
|
|
1816 new_scb->next = 0;
|
|
1817 current_string_chars_block = new_scb;
|
|
1818 new_scb->pos = fullsize;
|
|
1819 s_chars = (struct string_chars *)
|
|
1820 current_string_chars_block->string_chars;
|
|
1821 }
|
|
1822
|
|
1823 s_chars->string = string_it_goes_with;
|
|
1824
|
|
1825 INCREMENT_CONS_COUNTER (fullsize, "string chars");
|
|
1826
|
|
1827 return s_chars;
|
|
1828 }
|
|
1829
|
|
1830 Lisp_Object
|
|
1831 make_uninit_string (Bytecount length)
|
|
1832 {
|
438
|
1833 Lisp_String *s;
|
428
|
1834 EMACS_INT fullsize = STRING_FULLSIZE (length);
|
|
1835 Lisp_Object val;
|
|
1836
|
438
|
1837 assert (length >= 0 && fullsize > 0);
|
428
|
1838
|
|
1839 /* Allocate the string header */
|
438
|
1840 ALLOCATE_FIXED_TYPE (string, Lisp_String, s);
|
442
|
1841 set_lheader_implementation (&s->lheader, &lrecord_string);
|
428
|
1842
|
438
|
1843 set_string_data (s, BIG_STRING_FULLSIZE_P (fullsize)
|
|
1844 ? xnew_array (Bufbyte, length + 1)
|
|
1845 : allocate_string_chars_struct (s, fullsize)->chars);
|
|
1846
|
428
|
1847 set_string_length (s, length);
|
|
1848 s->plist = Qnil;
|
|
1849
|
|
1850 set_string_byte (s, length, 0);
|
|
1851
|
|
1852 XSETSTRING (val, s);
|
|
1853 return val;
|
|
1854 }
|
|
1855
|
|
1856 #ifdef VERIFY_STRING_CHARS_INTEGRITY
|
|
1857 static void verify_string_chars_integrity (void);
|
|
1858 #endif
|
|
1859
|
|
1860 /* Resize the string S so that DELTA bytes can be inserted starting
|
|
1861 at POS. If DELTA < 0, it means deletion starting at POS. If
|
|
1862 POS < 0, resize the string but don't copy any characters. Use
|
|
1863 this if you're planning on completely overwriting the string.
|
|
1864 */
|
|
1865
|
|
1866 void
|
438
|
1867 resize_string (Lisp_String *s, Bytecount pos, Bytecount delta)
|
428
|
1868 {
|
438
|
1869 Bytecount oldfullsize, newfullsize;
|
428
|
1870 #ifdef VERIFY_STRING_CHARS_INTEGRITY
|
|
1871 verify_string_chars_integrity ();
|
|
1872 #endif
|
|
1873
|
|
1874 #ifdef ERROR_CHECK_BUFPOS
|
|
1875 if (pos >= 0)
|
|
1876 {
|
|
1877 assert (pos <= string_length (s));
|
|
1878 if (delta < 0)
|
|
1879 assert (pos + (-delta) <= string_length (s));
|
|
1880 }
|
|
1881 else
|
|
1882 {
|
|
1883 if (delta < 0)
|
|
1884 assert ((-delta) <= string_length (s));
|
|
1885 }
|
|
1886 #endif /* ERROR_CHECK_BUFPOS */
|
|
1887
|
|
1888 if (delta == 0)
|
|
1889 /* simplest case: no size change. */
|
|
1890 return;
|
438
|
1891
|
|
1892 if (pos >= 0 && delta < 0)
|
|
1893 /* If DELTA < 0, the functions below will delete the characters
|
|
1894 before POS. We want to delete characters *after* POS, however,
|
|
1895 so convert this to the appropriate form. */
|
|
1896 pos += -delta;
|
|
1897
|
|
1898 oldfullsize = STRING_FULLSIZE (string_length (s));
|
|
1899 newfullsize = STRING_FULLSIZE (string_length (s) + delta);
|
|
1900
|
|
1901 if (BIG_STRING_FULLSIZE_P (oldfullsize))
|
428
|
1902 {
|
438
|
1903 if (BIG_STRING_FULLSIZE_P (newfullsize))
|
428
|
1904 {
|
440
|
1905 /* Both strings are big. We can just realloc().
|
|
1906 But careful! If the string is shrinking, we have to
|
|
1907 memmove() _before_ realloc(), and if growing, we have to
|
|
1908 memmove() _after_ realloc() - otherwise the access is
|
|
1909 illegal, and we might crash. */
|
|
1910 Bytecount len = string_length (s) + 1 - pos;
|
|
1911
|
|
1912 if (delta < 0 && pos >= 0)
|
|
1913 memmove (string_data (s) + pos + delta, string_data (s) + pos, len);
|
438
|
1914 set_string_data (s, (Bufbyte *) xrealloc (string_data (s),
|
|
1915 string_length (s) + delta + 1));
|
440
|
1916 if (delta > 0 && pos >= 0)
|
|
1917 memmove (string_data (s) + pos + delta, string_data (s) + pos, len);
|
428
|
1918 }
|
438
|
1919 else /* String has been demoted from BIG_STRING. */
|
428
|
1920 {
|
438
|
1921 Bufbyte *new_data =
|
|
1922 allocate_string_chars_struct (s, newfullsize)->chars;
|
|
1923 Bufbyte *old_data = string_data (s);
|
|
1924
|
|
1925 if (pos >= 0)
|
|
1926 {
|
|
1927 memcpy (new_data, old_data, pos);
|
|
1928 memcpy (new_data + pos + delta, old_data + pos,
|
|
1929 string_length (s) + 1 - pos);
|
|
1930 }
|
|
1931 set_string_data (s, new_data);
|
|
1932 xfree (old_data);
|
|
1933 }
|
|
1934 }
|
|
1935 else /* old string is small */
|
|
1936 {
|
|
1937 if (oldfullsize == newfullsize)
|
|
1938 {
|
|
1939 /* special case; size change but the necessary
|
|
1940 allocation size won't change (up or down; code
|
|
1941 somewhere depends on there not being any unused
|
|
1942 allocation space, modulo any alignment
|
|
1943 constraints). */
|
428
|
1944 if (pos >= 0)
|
|
1945 {
|
|
1946 Bufbyte *addroff = pos + string_data (s);
|
|
1947
|
|
1948 memmove (addroff + delta, addroff,
|
|
1949 /* +1 due to zero-termination. */
|
|
1950 string_length (s) + 1 - pos);
|
|
1951 }
|
|
1952 }
|
|
1953 else
|
|
1954 {
|
438
|
1955 Bufbyte *old_data = string_data (s);
|
|
1956 Bufbyte *new_data =
|
|
1957 BIG_STRING_FULLSIZE_P (newfullsize)
|
|
1958 ? xnew_array (Bufbyte, string_length (s) + delta + 1)
|
|
1959 : allocate_string_chars_struct (s, newfullsize)->chars;
|
|
1960
|
428
|
1961 if (pos >= 0)
|
|
1962 {
|
438
|
1963 memcpy (new_data, old_data, pos);
|
|
1964 memcpy (new_data + pos + delta, old_data + pos,
|
428
|
1965 string_length (s) + 1 - pos);
|
|
1966 }
|
438
|
1967 set_string_data (s, new_data);
|
|
1968
|
|
1969 {
|
|
1970 /* We need to mark this chunk of the string_chars_block
|
|
1971 as unused so that compact_string_chars() doesn't
|
|
1972 freak. */
|
|
1973 struct string_chars *old_s_chars = (struct string_chars *)
|
|
1974 ((char *) old_data - offsetof (struct string_chars, chars));
|
|
1975 /* Sanity check to make sure we aren't hosed by strange
|
|
1976 alignment/padding. */
|
|
1977 assert (old_s_chars->string == s);
|
454
|
1978 MARK_STRING_CHARS_AS_FREE (old_s_chars);
|
438
|
1979 ((struct unused_string_chars *) old_s_chars)->fullsize =
|
|
1980 oldfullsize;
|
|
1981 }
|
428
|
1982 }
|
438
|
1983 }
|
|
1984
|
|
1985 set_string_length (s, string_length (s) + delta);
|
|
1986 /* If pos < 0, the string won't be zero-terminated.
|
|
1987 Terminate now just to make sure. */
|
|
1988 string_data (s)[string_length (s)] = '\0';
|
|
1989
|
|
1990 if (pos >= 0)
|
|
1991 {
|
|
1992 Lisp_Object string;
|
|
1993
|
|
1994 XSETSTRING (string, s);
|
|
1995 /* We also have to adjust all of the extent indices after the
|
|
1996 place we did the change. We say "pos - 1" because
|
|
1997 adjust_extents() is exclusive of the starting position
|
|
1998 passed to it. */
|
|
1999 adjust_extents (string, pos - 1, string_length (s),
|
|
2000 delta);
|
428
|
2001 }
|
|
2002
|
|
2003 #ifdef VERIFY_STRING_CHARS_INTEGRITY
|
|
2004 verify_string_chars_integrity ();
|
|
2005 #endif
|
|
2006 }
|
|
2007
|
|
2008 #ifdef MULE
|
|
2009
|
|
2010 void
|
438
|
2011 set_string_char (Lisp_String *s, Charcount i, Emchar c)
|
428
|
2012 {
|
|
2013 Bufbyte newstr[MAX_EMCHAR_LEN];
|
|
2014 Bytecount bytoff = charcount_to_bytecount (string_data (s), i);
|
|
2015 Bytecount oldlen = charcount_to_bytecount (string_data (s) + bytoff, 1);
|
|
2016 Bytecount newlen = set_charptr_emchar (newstr, c);
|
|
2017
|
|
2018 if (oldlen != newlen)
|
|
2019 resize_string (s, bytoff, newlen - oldlen);
|
|
2020 /* Remember, string_data (s) might have changed so we can't cache it. */
|
|
2021 memcpy (string_data (s) + bytoff, newstr, newlen);
|
|
2022 }
|
|
2023
|
|
2024 #endif /* MULE */
|
|
2025
|
|
2026 DEFUN ("make-string", Fmake_string, 2, 2, 0, /*
|
444
|
2027 Return a new string consisting of LENGTH copies of CHARACTER.
|
|
2028 LENGTH must be a non-negative integer.
|
428
|
2029 */
|
444
|
2030 (length, character))
|
428
|
2031 {
|
|
2032 CHECK_NATNUM (length);
|
444
|
2033 CHECK_CHAR_COERCE_INT (character);
|
428
|
2034 {
|
|
2035 Bufbyte init_str[MAX_EMCHAR_LEN];
|
444
|
2036 int len = set_charptr_emchar (init_str, XCHAR (character));
|
428
|
2037 Lisp_Object val = make_uninit_string (len * XINT (length));
|
|
2038
|
|
2039 if (len == 1)
|
|
2040 /* Optimize the single-byte case */
|
444
|
2041 memset (XSTRING_DATA (val), XCHAR (character), XSTRING_LENGTH (val));
|
428
|
2042 else
|
|
2043 {
|
|
2044 size_t i;
|
|
2045 Bufbyte *ptr = XSTRING_DATA (val);
|
|
2046
|
|
2047 for (i = XINT (length); i; i--)
|
|
2048 {
|
|
2049 Bufbyte *init_ptr = init_str;
|
|
2050 switch (len)
|
|
2051 {
|
|
2052 case 4: *ptr++ = *init_ptr++;
|
|
2053 case 3: *ptr++ = *init_ptr++;
|
|
2054 case 2: *ptr++ = *init_ptr++;
|
|
2055 case 1: *ptr++ = *init_ptr++;
|
|
2056 }
|
|
2057 }
|
|
2058 }
|
|
2059 return val;
|
|
2060 }
|
|
2061 }
|
|
2062
|
|
2063 DEFUN ("string", Fstring, 0, MANY, 0, /*
|
|
2064 Concatenate all the argument characters and make the result a string.
|
|
2065 */
|
|
2066 (int nargs, Lisp_Object *args))
|
|
2067 {
|
|
2068 Bufbyte *storage = alloca_array (Bufbyte, nargs * MAX_EMCHAR_LEN);
|
|
2069 Bufbyte *p = storage;
|
|
2070
|
|
2071 for (; nargs; nargs--, args++)
|
|
2072 {
|
|
2073 Lisp_Object lisp_char = *args;
|
|
2074 CHECK_CHAR_COERCE_INT (lisp_char);
|
|
2075 p += set_charptr_emchar (p, XCHAR (lisp_char));
|
|
2076 }
|
|
2077 return make_string (storage, p - storage);
|
|
2078 }
|
|
2079
|
|
2080
|
|
2081 /* Take some raw memory, which MUST already be in internal format,
|
|
2082 and package it up into a Lisp string. */
|
|
2083 Lisp_Object
|
442
|
2084 make_string (const Bufbyte *contents, Bytecount length)
|
428
|
2085 {
|
|
2086 Lisp_Object val;
|
|
2087
|
|
2088 /* Make sure we find out about bad make_string's when they happen */
|
|
2089 #if defined (ERROR_CHECK_BUFPOS) && defined (MULE)
|
|
2090 bytecount_to_charcount (contents, length); /* Just for the assertions */
|
|
2091 #endif
|
|
2092
|
|
2093 val = make_uninit_string (length);
|
|
2094 memcpy (XSTRING_DATA (val), contents, length);
|
|
2095 return val;
|
|
2096 }
|
|
2097
|
|
2098 /* Take some raw memory, encoded in some external data format,
|
|
2099 and convert it into a Lisp string. */
|
|
2100 Lisp_Object
|
442
|
2101 make_ext_string (const Extbyte *contents, EMACS_INT length,
|
440
|
2102 Lisp_Object coding_system)
|
428
|
2103 {
|
440
|
2104 Lisp_Object string;
|
|
2105 TO_INTERNAL_FORMAT (DATA, (contents, length),
|
|
2106 LISP_STRING, string,
|
|
2107 coding_system);
|
|
2108 return string;
|
428
|
2109 }
|
|
2110
|
|
2111 Lisp_Object
|
442
|
2112 build_string (const char *str)
|
428
|
2113 {
|
|
2114 /* Some strlen's crash and burn if passed null. */
|
442
|
2115 return make_string ((const Bufbyte *) str, (str ? strlen(str) : 0));
|
428
|
2116 }
|
|
2117
|
|
2118 Lisp_Object
|
442
|
2119 build_ext_string (const char *str, Lisp_Object coding_system)
|
428
|
2120 {
|
|
2121 /* Some strlen's crash and burn if passed null. */
|
442
|
2122 return make_ext_string ((const Extbyte *) str, (str ? strlen(str) : 0),
|
440
|
2123 coding_system);
|
428
|
2124 }
|
|
2125
|
|
2126 Lisp_Object
|
442
|
2127 build_translated_string (const char *str)
|
428
|
2128 {
|
|
2129 return build_string (GETTEXT (str));
|
|
2130 }
|
|
2131
|
|
2132 Lisp_Object
|
442
|
2133 make_string_nocopy (const Bufbyte *contents, Bytecount length)
|
428
|
2134 {
|
438
|
2135 Lisp_String *s;
|
428
|
2136 Lisp_Object val;
|
|
2137
|
|
2138 /* Make sure we find out about bad make_string_nocopy's when they happen */
|
|
2139 #if defined (ERROR_CHECK_BUFPOS) && defined (MULE)
|
|
2140 bytecount_to_charcount (contents, length); /* Just for the assertions */
|
|
2141 #endif
|
|
2142
|
|
2143 /* Allocate the string header */
|
438
|
2144 ALLOCATE_FIXED_TYPE (string, Lisp_String, s);
|
442
|
2145 set_lheader_implementation (&s->lheader, &lrecord_string);
|
428
|
2146 SET_C_READONLY_RECORD_HEADER (&s->lheader);
|
|
2147 s->plist = Qnil;
|
|
2148 set_string_data (s, (Bufbyte *)contents);
|
|
2149 set_string_length (s, length);
|
|
2150
|
|
2151 XSETSTRING (val, s);
|
|
2152 return val;
|
|
2153 }
|
|
2154
|
|
2155
|
|
2156 /************************************************************************/
|
|
2157 /* lcrecord lists */
|
|
2158 /************************************************************************/
|
|
2159
|
|
2160 /* Lcrecord lists are used to manage the allocation of particular
|
|
2161 sorts of lcrecords, to avoid calling alloc_lcrecord() (and thus
|
|
2162 malloc() and garbage-collection junk) as much as possible.
|
|
2163 It is similar to the Blocktype class.
|
|
2164
|
|
2165 It works like this:
|
|
2166
|
|
2167 1) Create an lcrecord-list object using make_lcrecord_list().
|
|
2168 This is often done at initialization. Remember to staticpro_nodump
|
|
2169 this object! The arguments to make_lcrecord_list() are the
|
|
2170 same as would be passed to alloc_lcrecord().
|
|
2171 2) Instead of calling alloc_lcrecord(), call allocate_managed_lcrecord()
|
|
2172 and pass the lcrecord-list earlier created.
|
|
2173 3) When done with the lcrecord, call free_managed_lcrecord().
|
|
2174 The standard freeing caveats apply: ** make sure there are no
|
|
2175 pointers to the object anywhere! **
|
|
2176 4) Calling free_managed_lcrecord() is just like kissing the
|
|
2177 lcrecord goodbye as if it were garbage-collected. This means:
|
|
2178 -- the contents of the freed lcrecord are undefined, and the
|
|
2179 contents of something produced by allocate_managed_lcrecord()
|
|
2180 are undefined, just like for alloc_lcrecord().
|
|
2181 -- the mark method for the lcrecord's type will *NEVER* be called
|
|
2182 on freed lcrecords.
|
|
2183 -- the finalize method for the lcrecord's type will be called
|
|
2184 at the time that free_managed_lcrecord() is called.
|
|
2185
|
|
2186 */
|
|
2187
|
|
2188 static Lisp_Object
|
|
2189 mark_lcrecord_list (Lisp_Object obj)
|
|
2190 {
|
|
2191 struct lcrecord_list *list = XLCRECORD_LIST (obj);
|
|
2192 Lisp_Object chain = list->free;
|
|
2193
|
|
2194 while (!NILP (chain))
|
|
2195 {
|
|
2196 struct lrecord_header *lheader = XRECORD_LHEADER (chain);
|
|
2197 struct free_lcrecord_header *free_header =
|
|
2198 (struct free_lcrecord_header *) lheader;
|
|
2199
|
442
|
2200 gc_checking_assert
|
|
2201 (/* There should be no other pointers to the free list. */
|
|
2202 ! MARKED_RECORD_HEADER_P (lheader)
|
|
2203 &&
|
|
2204 /* Only lcrecords should be here. */
|
|
2205 ! LHEADER_IMPLEMENTATION (lheader)->basic_p
|
|
2206 &&
|
|
2207 /* Only free lcrecords should be here. */
|
|
2208 free_header->lcheader.free
|
|
2209 &&
|
|
2210 /* The type of the lcrecord must be right. */
|
|
2211 LHEADER_IMPLEMENTATION (lheader) == list->implementation
|
|
2212 &&
|
|
2213 /* So must the size. */
|
|
2214 (LHEADER_IMPLEMENTATION (lheader)->static_size == 0 ||
|
|
2215 LHEADER_IMPLEMENTATION (lheader)->static_size == list->size)
|
|
2216 );
|
428
|
2217
|
|
2218 MARK_RECORD_HEADER (lheader);
|
|
2219 chain = free_header->chain;
|
|
2220 }
|
|
2221
|
|
2222 return Qnil;
|
|
2223 }
|
|
2224
|
|
2225 DEFINE_LRECORD_IMPLEMENTATION ("lcrecord-list", lcrecord_list,
|
|
2226 mark_lcrecord_list, internal_object_printer,
|
|
2227 0, 0, 0, 0, struct lcrecord_list);
|
|
2228 Lisp_Object
|
|
2229 make_lcrecord_list (size_t size,
|
442
|
2230 const struct lrecord_implementation *implementation)
|
428
|
2231 {
|
|
2232 struct lcrecord_list *p = alloc_lcrecord_type (struct lcrecord_list,
|
|
2233 &lrecord_lcrecord_list);
|
|
2234 Lisp_Object val;
|
|
2235
|
|
2236 p->implementation = implementation;
|
|
2237 p->size = size;
|
|
2238 p->free = Qnil;
|
|
2239 XSETLCRECORD_LIST (val, p);
|
|
2240 return val;
|
|
2241 }
|
|
2242
|
|
2243 Lisp_Object
|
|
2244 allocate_managed_lcrecord (Lisp_Object lcrecord_list)
|
|
2245 {
|
|
2246 struct lcrecord_list *list = XLCRECORD_LIST (lcrecord_list);
|
|
2247 if (!NILP (list->free))
|
|
2248 {
|
|
2249 Lisp_Object val = list->free;
|
|
2250 struct free_lcrecord_header *free_header =
|
|
2251 (struct free_lcrecord_header *) XPNTR (val);
|
|
2252
|
|
2253 #ifdef ERROR_CHECK_GC
|
442
|
2254 struct lrecord_header *lheader = &free_header->lcheader.lheader;
|
428
|
2255
|
|
2256 /* There should be no other pointers to the free list. */
|
442
|
2257 assert (! MARKED_RECORD_HEADER_P (lheader));
|
428
|
2258 /* Only lcrecords should be here. */
|
442
|
2259 assert (! LHEADER_IMPLEMENTATION (lheader)->basic_p);
|
428
|
2260 /* Only free lcrecords should be here. */
|
|
2261 assert (free_header->lcheader.free);
|
|
2262 /* The type of the lcrecord must be right. */
|
442
|
2263 assert (LHEADER_IMPLEMENTATION (lheader) == list->implementation);
|
428
|
2264 /* So must the size. */
|
442
|
2265 assert (LHEADER_IMPLEMENTATION (lheader)->static_size == 0 ||
|
|
2266 LHEADER_IMPLEMENTATION (lheader)->static_size == list->size);
|
428
|
2267 #endif /* ERROR_CHECK_GC */
|
442
|
2268
|
428
|
2269 list->free = free_header->chain;
|
|
2270 free_header->lcheader.free = 0;
|
|
2271 return val;
|
|
2272 }
|
|
2273 else
|
|
2274 {
|
|
2275 Lisp_Object val;
|
|
2276
|
442
|
2277 XSETOBJ (val, alloc_lcrecord (list->size, list->implementation));
|
428
|
2278 return val;
|
|
2279 }
|
|
2280 }
|
|
2281
|
|
2282 void
|
|
2283 free_managed_lcrecord (Lisp_Object lcrecord_list, Lisp_Object lcrecord)
|
|
2284 {
|
|
2285 struct lcrecord_list *list = XLCRECORD_LIST (lcrecord_list);
|
|
2286 struct free_lcrecord_header *free_header =
|
|
2287 (struct free_lcrecord_header *) XPNTR (lcrecord);
|
442
|
2288 struct lrecord_header *lheader = &free_header->lcheader.lheader;
|
|
2289 const struct lrecord_implementation *implementation
|
428
|
2290 = LHEADER_IMPLEMENTATION (lheader);
|
|
2291
|
|
2292 /* Make sure the size is correct. This will catch, for example,
|
|
2293 putting a window configuration on the wrong free list. */
|
442
|
2294 gc_checking_assert ((implementation->size_in_bytes_method ?
|
|
2295 implementation->size_in_bytes_method (lheader) :
|
|
2296 implementation->static_size)
|
|
2297 == list->size);
|
428
|
2298
|
|
2299 if (implementation->finalizer)
|
|
2300 implementation->finalizer (lheader, 0);
|
|
2301 free_header->chain = list->free;
|
|
2302 free_header->lcheader.free = 1;
|
|
2303 list->free = lcrecord;
|
|
2304 }
|
|
2305
|
|
2306
|
|
2307
|
|
2308
|
|
2309 DEFUN ("purecopy", Fpurecopy, 1, 1, 0, /*
|
|
2310 Kept for compatibility, returns its argument.
|
|
2311 Old:
|
|
2312 Make a copy of OBJECT in pure storage.
|
|
2313 Recursively copies contents of vectors and cons cells.
|
|
2314 Does not copy symbols.
|
|
2315 */
|
444
|
2316 (object))
|
428
|
2317 {
|
444
|
2318 return object;
|
428
|
2319 }
|
|
2320
|
|
2321
|
|
2322 /************************************************************************/
|
|
2323 /* Garbage Collection */
|
|
2324 /************************************************************************/
|
|
2325
|
442
|
2326 /* All the built-in lisp object types are enumerated in `enum lrecord_type'.
|
|
2327 Additional ones may be defined by a module (none yet). We leave some
|
|
2328 room in `lrecord_implementations_table' for such new lisp object types. */
|
|
2329 const struct lrecord_implementation *lrecord_implementations_table[(unsigned int)lrecord_type_last_built_in_type + MODULE_DEFINABLE_TYPE_COUNT];
|
|
2330 unsigned int lrecord_type_count = (unsigned int)lrecord_type_last_built_in_type;
|
|
2331 /* Object marker functions are in the lrecord_implementation structure.
|
|
2332 But copying them to a parallel array is much more cache-friendly.
|
|
2333 This hack speeds up (garbage-collect) by about 5%. */
|
|
2334 Lisp_Object (*lrecord_markers[countof (lrecord_implementations_table)]) (Lisp_Object);
|
428
|
2335
|
|
2336 struct gcpro *gcprolist;
|
|
2337
|
452
|
2338 /* We want the staticpros relocated, but not the pointers found therein.
|
|
2339 Hence we use a trivial description, as for pointerless objects. */
|
|
2340 static const struct lrecord_description staticpro_description_1[] = {
|
|
2341 { XD_END }
|
|
2342 };
|
|
2343
|
|
2344 static const struct struct_description staticpro_description = {
|
|
2345 sizeof (Lisp_Object *),
|
|
2346 staticpro_description_1
|
|
2347 };
|
|
2348
|
|
2349 static const struct lrecord_description staticpros_description_1[] = {
|
|
2350 XD_DYNARR_DESC (Lisp_Object_ptr_dynarr, &staticpro_description),
|
|
2351 { XD_END }
|
|
2352 };
|
|
2353
|
|
2354 static const struct struct_description staticpros_description = {
|
|
2355 sizeof (Lisp_Object_ptr_dynarr),
|
|
2356 staticpros_description_1
|
|
2357 };
|
|
2358
|
|
2359 Lisp_Object_ptr_dynarr *staticpros;
|
|
2360
|
|
2361 /* Mark the Lisp_Object at non-heap VARADDRESS as a root object for
|
|
2362 garbage collection, and for dumping. */
|
428
|
2363 void
|
|
2364 staticpro (Lisp_Object *varaddress)
|
|
2365 {
|
452
|
2366 Dynarr_add (staticpros, varaddress);
|
|
2367 dump_add_root_object (varaddress);
|
428
|
2368 }
|
|
2369
|
442
|
2370
|
452
|
2371 Lisp_Object_ptr_dynarr *staticpros_nodump;
|
|
2372
|
|
2373 /* Mark the Lisp_Object at non-heap VARADDRESS as a root object for
|
|
2374 garbage collection, but not for dumping. */
|
428
|
2375 void
|
|
2376 staticpro_nodump (Lisp_Object *varaddress)
|
|
2377 {
|
452
|
2378 Dynarr_add (staticpros_nodump, varaddress);
|
428
|
2379 }
|
|
2380
|
442
|
2381 #ifdef ERROR_CHECK_GC
|
|
2382 #define GC_CHECK_LHEADER_INVARIANTS(lheader) do { \
|
|
2383 struct lrecord_header * GCLI_lh = (lheader); \
|
|
2384 assert (GCLI_lh != 0); \
|
|
2385 assert (GCLI_lh->type < lrecord_type_count); \
|
|
2386 assert (! C_READONLY_RECORD_HEADER_P (GCLI_lh) || \
|
|
2387 (MARKED_RECORD_HEADER_P (GCLI_lh) && \
|
|
2388 LISP_READONLY_RECORD_HEADER_P (GCLI_lh))); \
|
|
2389 } while (0)
|
|
2390 #else
|
|
2391 #define GC_CHECK_LHEADER_INVARIANTS(lheader)
|
|
2392 #endif
|
|
2393
|
428
|
2394
|
|
2395 /* Mark reference to a Lisp_Object. If the object referred to has not been
|
|
2396 seen yet, recursively mark all the references contained in it. */
|
|
2397
|
|
2398 void
|
|
2399 mark_object (Lisp_Object obj)
|
|
2400 {
|
|
2401 tail_recurse:
|
|
2402
|
|
2403 /* Checks we used to perform */
|
|
2404 /* if (EQ (obj, Qnull_pointer)) return; */
|
|
2405 /* if (!POINTER_TYPE_P (XGCTYPE (obj))) return; */
|
|
2406 /* if (PURIFIED (XPNTR (obj))) return; */
|
|
2407
|
|
2408 if (XTYPE (obj) == Lisp_Type_Record)
|
|
2409 {
|
|
2410 struct lrecord_header *lheader = XRECORD_LHEADER (obj);
|
442
|
2411
|
|
2412 GC_CHECK_LHEADER_INVARIANTS (lheader);
|
|
2413
|
|
2414 gc_checking_assert (LHEADER_IMPLEMENTATION (lheader)->basic_p ||
|
|
2415 ! ((struct lcrecord_header *) lheader)->free);
|
|
2416
|
|
2417 /* All c_readonly objects have their mark bit set,
|
|
2418 so that we only need to check the mark bit here. */
|
|
2419 if (! MARKED_RECORD_HEADER_P (lheader))
|
428
|
2420 {
|
|
2421 MARK_RECORD_HEADER (lheader);
|
442
|
2422
|
|
2423 if (RECORD_MARKER (lheader))
|
428
|
2424 {
|
442
|
2425 obj = RECORD_MARKER (lheader) (obj);
|
428
|
2426 if (!NILP (obj)) goto tail_recurse;
|
|
2427 }
|
|
2428 }
|
|
2429 }
|
|
2430 }
|
|
2431
|
|
2432 /* mark all of the conses in a list and mark the final cdr; but
|
|
2433 DO NOT mark the cars.
|
|
2434
|
|
2435 Use only for internal lists! There should never be other pointers
|
|
2436 to the cons cells, because if so, the cars will remain unmarked
|
|
2437 even when they maybe should be marked. */
|
|
2438 void
|
|
2439 mark_conses_in_list (Lisp_Object obj)
|
|
2440 {
|
|
2441 Lisp_Object rest;
|
|
2442
|
|
2443 for (rest = obj; CONSP (rest); rest = XCDR (rest))
|
|
2444 {
|
|
2445 if (CONS_MARKED_P (XCONS (rest)))
|
|
2446 return;
|
|
2447 MARK_CONS (XCONS (rest));
|
|
2448 }
|
|
2449
|
|
2450 mark_object (rest);
|
|
2451 }
|
|
2452
|
|
2453
|
|
2454 /* Find all structures not marked, and free them. */
|
|
2455
|
|
2456 static int gc_count_num_bit_vector_used, gc_count_bit_vector_total_size;
|
|
2457 static int gc_count_bit_vector_storage;
|
|
2458 static int gc_count_num_short_string_in_use;
|
|
2459 static int gc_count_string_total_size;
|
|
2460 static int gc_count_short_string_total_size;
|
|
2461
|
|
2462 /* static int gc_count_total_records_used, gc_count_records_total_size; */
|
|
2463
|
|
2464
|
|
2465 /* stats on lcrecords in use - kinda kludgy */
|
|
2466
|
|
2467 static struct
|
|
2468 {
|
|
2469 int instances_in_use;
|
|
2470 int bytes_in_use;
|
|
2471 int instances_freed;
|
|
2472 int bytes_freed;
|
|
2473 int instances_on_free_list;
|
|
2474 } lcrecord_stats [countof (lrecord_implementations_table)];
|
|
2475
|
|
2476 static void
|
442
|
2477 tick_lcrecord_stats (const struct lrecord_header *h, int free_p)
|
428
|
2478 {
|
442
|
2479 unsigned int type_index = h->type;
|
428
|
2480
|
|
2481 if (((struct lcrecord_header *) h)->free)
|
|
2482 {
|
442
|
2483 gc_checking_assert (!free_p);
|
428
|
2484 lcrecord_stats[type_index].instances_on_free_list++;
|
|
2485 }
|
|
2486 else
|
|
2487 {
|
442
|
2488 const struct lrecord_implementation *implementation =
|
|
2489 LHEADER_IMPLEMENTATION (h);
|
|
2490
|
|
2491 size_t sz = (implementation->size_in_bytes_method ?
|
|
2492 implementation->size_in_bytes_method (h) :
|
|
2493 implementation->static_size);
|
428
|
2494 if (free_p)
|
|
2495 {
|
|
2496 lcrecord_stats[type_index].instances_freed++;
|
|
2497 lcrecord_stats[type_index].bytes_freed += sz;
|
|
2498 }
|
|
2499 else
|
|
2500 {
|
|
2501 lcrecord_stats[type_index].instances_in_use++;
|
|
2502 lcrecord_stats[type_index].bytes_in_use += sz;
|
|
2503 }
|
|
2504 }
|
|
2505 }
|
|
2506
|
|
2507
|
|
2508 /* Free all unmarked records */
|
|
2509 static void
|
|
2510 sweep_lcrecords_1 (struct lcrecord_header **prev, int *used)
|
|
2511 {
|
|
2512 struct lcrecord_header *header;
|
|
2513 int num_used = 0;
|
|
2514 /* int total_size = 0; */
|
|
2515
|
|
2516 xzero (lcrecord_stats); /* Reset all statistics to 0. */
|
|
2517
|
|
2518 /* First go through and call all the finalize methods.
|
|
2519 Then go through and free the objects. There used to
|
|
2520 be only one loop here, with the call to the finalizer
|
|
2521 occurring directly before the xfree() below. That
|
|
2522 is marginally faster but much less safe -- if the
|
|
2523 finalize method for an object needs to reference any
|
|
2524 other objects contained within it (and many do),
|
|
2525 we could easily be screwed by having already freed that
|
|
2526 other object. */
|
|
2527
|
|
2528 for (header = *prev; header; header = header->next)
|
|
2529 {
|
|
2530 struct lrecord_header *h = &(header->lheader);
|
442
|
2531
|
|
2532 GC_CHECK_LHEADER_INVARIANTS (h);
|
|
2533
|
|
2534 if (! MARKED_RECORD_HEADER_P (h) && ! header->free)
|
428
|
2535 {
|
|
2536 if (LHEADER_IMPLEMENTATION (h)->finalizer)
|
|
2537 LHEADER_IMPLEMENTATION (h)->finalizer (h, 0);
|
|
2538 }
|
|
2539 }
|
|
2540
|
|
2541 for (header = *prev; header; )
|
|
2542 {
|
|
2543 struct lrecord_header *h = &(header->lheader);
|
442
|
2544 if (MARKED_RECORD_HEADER_P (h))
|
428
|
2545 {
|
442
|
2546 if (! C_READONLY_RECORD_HEADER_P (h))
|
428
|
2547 UNMARK_RECORD_HEADER (h);
|
|
2548 num_used++;
|
|
2549 /* total_size += n->implementation->size_in_bytes (h);*/
|
440
|
2550 /* #### May modify header->next on a C_READONLY lcrecord */
|
428
|
2551 prev = &(header->next);
|
|
2552 header = *prev;
|
|
2553 tick_lcrecord_stats (h, 0);
|
|
2554 }
|
|
2555 else
|
|
2556 {
|
|
2557 struct lcrecord_header *next = header->next;
|
|
2558 *prev = next;
|
|
2559 tick_lcrecord_stats (h, 1);
|
|
2560 /* used to call finalizer right here. */
|
|
2561 xfree (header);
|
|
2562 header = next;
|
|
2563 }
|
|
2564 }
|
|
2565 *used = num_used;
|
|
2566 /* *total = total_size; */
|
|
2567 }
|
|
2568
|
|
2569
|
|
2570 static void
|
|
2571 sweep_bit_vectors_1 (Lisp_Object *prev,
|
|
2572 int *used, int *total, int *storage)
|
|
2573 {
|
|
2574 Lisp_Object bit_vector;
|
|
2575 int num_used = 0;
|
|
2576 int total_size = 0;
|
|
2577 int total_storage = 0;
|
|
2578
|
|
2579 /* BIT_VECTORP fails because the objects are marked, which changes
|
|
2580 their implementation */
|
|
2581 for (bit_vector = *prev; !EQ (bit_vector, Qzero); )
|
|
2582 {
|
|
2583 Lisp_Bit_Vector *v = XBIT_VECTOR (bit_vector);
|
|
2584 int len = v->size;
|
442
|
2585 if (MARKED_RECORD_P (bit_vector))
|
428
|
2586 {
|
442
|
2587 if (! C_READONLY_RECORD_HEADER_P(&(v->lheader)))
|
428
|
2588 UNMARK_RECORD_HEADER (&(v->lheader));
|
|
2589 total_size += len;
|
|
2590 total_storage +=
|
|
2591 MALLOC_OVERHEAD +
|
456
|
2592 FLEXIBLE_ARRAY_STRUCT_SIZEOF (Lisp_Bit_Vector, unsigned long,
|
|
2593 bits, BIT_VECTOR_LONG_STORAGE (len));
|
428
|
2594 num_used++;
|
440
|
2595 /* #### May modify next on a C_READONLY bitvector */
|
428
|
2596 prev = &(bit_vector_next (v));
|
|
2597 bit_vector = *prev;
|
|
2598 }
|
|
2599 else
|
|
2600 {
|
|
2601 Lisp_Object next = bit_vector_next (v);
|
|
2602 *prev = next;
|
|
2603 xfree (v);
|
|
2604 bit_vector = next;
|
|
2605 }
|
|
2606 }
|
|
2607 *used = num_used;
|
|
2608 *total = total_size;
|
|
2609 *storage = total_storage;
|
|
2610 }
|
|
2611
|
|
2612 /* And the Lord said: Thou shalt use the `c-backslash-region' command
|
|
2613 to make macros prettier. */
|
|
2614
|
|
2615 #ifdef ERROR_CHECK_GC
|
|
2616
|
|
2617 #define SWEEP_FIXED_TYPE_BLOCK(typename, obj_type) \
|
|
2618 do { \
|
|
2619 struct typename##_block *SFTB_current; \
|
|
2620 int SFTB_limit; \
|
|
2621 int num_free = 0, num_used = 0; \
|
|
2622 \
|
444
|
2623 for (SFTB_current = current_##typename##_block, \
|
428
|
2624 SFTB_limit = current_##typename##_block_index; \
|
|
2625 SFTB_current; \
|
|
2626 ) \
|
|
2627 { \
|
|
2628 int SFTB_iii; \
|
|
2629 \
|
|
2630 for (SFTB_iii = 0; SFTB_iii < SFTB_limit; SFTB_iii++) \
|
|
2631 { \
|
|
2632 obj_type *SFTB_victim = &(SFTB_current->block[SFTB_iii]); \
|
|
2633 \
|
454
|
2634 if (LRECORD_FREE_P (SFTB_victim)) \
|
428
|
2635 { \
|
|
2636 num_free++; \
|
|
2637 } \
|
|
2638 else if (C_READONLY_RECORD_HEADER_P (&SFTB_victim->lheader)) \
|
|
2639 { \
|
|
2640 num_used++; \
|
|
2641 } \
|
442
|
2642 else if (! MARKED_RECORD_HEADER_P (&SFTB_victim->lheader)) \
|
428
|
2643 { \
|
|
2644 num_free++; \
|
|
2645 FREE_FIXED_TYPE (typename, obj_type, SFTB_victim); \
|
|
2646 } \
|
|
2647 else \
|
|
2648 { \
|
|
2649 num_used++; \
|
|
2650 UNMARK_##typename (SFTB_victim); \
|
|
2651 } \
|
|
2652 } \
|
|
2653 SFTB_current = SFTB_current->prev; \
|
|
2654 SFTB_limit = countof (current_##typename##_block->block); \
|
|
2655 } \
|
|
2656 \
|
|
2657 gc_count_num_##typename##_in_use = num_used; \
|
|
2658 gc_count_num_##typename##_freelist = num_free; \
|
|
2659 } while (0)
|
|
2660
|
|
2661 #else /* !ERROR_CHECK_GC */
|
|
2662
|
|
2663 #define SWEEP_FIXED_TYPE_BLOCK(typename, obj_type) \
|
|
2664 do { \
|
|
2665 struct typename##_block *SFTB_current; \
|
|
2666 struct typename##_block **SFTB_prev; \
|
|
2667 int SFTB_limit; \
|
|
2668 int num_free = 0, num_used = 0; \
|
|
2669 \
|
|
2670 typename##_free_list = 0; \
|
|
2671 \
|
|
2672 for (SFTB_prev = ¤t_##typename##_block, \
|
|
2673 SFTB_current = current_##typename##_block, \
|
|
2674 SFTB_limit = current_##typename##_block_index; \
|
|
2675 SFTB_current; \
|
|
2676 ) \
|
|
2677 { \
|
|
2678 int SFTB_iii; \
|
|
2679 int SFTB_empty = 1; \
|
454
|
2680 Lisp_Free *SFTB_old_free_list = typename##_free_list; \
|
428
|
2681 \
|
|
2682 for (SFTB_iii = 0; SFTB_iii < SFTB_limit; SFTB_iii++) \
|
|
2683 { \
|
|
2684 obj_type *SFTB_victim = &(SFTB_current->block[SFTB_iii]); \
|
|
2685 \
|
454
|
2686 if (LRECORD_FREE_P (SFTB_victim)) \
|
428
|
2687 { \
|
|
2688 num_free++; \
|
|
2689 PUT_FIXED_TYPE_ON_FREE_LIST (typename, obj_type, SFTB_victim); \
|
|
2690 } \
|
|
2691 else if (C_READONLY_RECORD_HEADER_P (&SFTB_victim->lheader)) \
|
|
2692 { \
|
|
2693 SFTB_empty = 0; \
|
|
2694 num_used++; \
|
|
2695 } \
|
442
|
2696 else if (! MARKED_RECORD_HEADER_P (&SFTB_victim->lheader)) \
|
428
|
2697 { \
|
|
2698 num_free++; \
|
|
2699 FREE_FIXED_TYPE (typename, obj_type, SFTB_victim); \
|
|
2700 } \
|
|
2701 else \
|
|
2702 { \
|
|
2703 SFTB_empty = 0; \
|
|
2704 num_used++; \
|
|
2705 UNMARK_##typename (SFTB_victim); \
|
|
2706 } \
|
|
2707 } \
|
|
2708 if (!SFTB_empty) \
|
|
2709 { \
|
|
2710 SFTB_prev = &(SFTB_current->prev); \
|
|
2711 SFTB_current = SFTB_current->prev; \
|
|
2712 } \
|
|
2713 else if (SFTB_current == current_##typename##_block \
|
|
2714 && !SFTB_current->prev) \
|
|
2715 { \
|
|
2716 /* No real point in freeing sole allocation block */ \
|
|
2717 break; \
|
|
2718 } \
|
|
2719 else \
|
|
2720 { \
|
|
2721 struct typename##_block *SFTB_victim_block = SFTB_current; \
|
|
2722 if (SFTB_victim_block == current_##typename##_block) \
|
|
2723 current_##typename##_block_index \
|
|
2724 = countof (current_##typename##_block->block); \
|
|
2725 SFTB_current = SFTB_current->prev; \
|
|
2726 { \
|
|
2727 *SFTB_prev = SFTB_current; \
|
|
2728 xfree (SFTB_victim_block); \
|
|
2729 /* Restore free list to what it was before victim was swept */ \
|
|
2730 typename##_free_list = SFTB_old_free_list; \
|
|
2731 num_free -= SFTB_limit; \
|
|
2732 } \
|
|
2733 } \
|
|
2734 SFTB_limit = countof (current_##typename##_block->block); \
|
|
2735 } \
|
|
2736 \
|
|
2737 gc_count_num_##typename##_in_use = num_used; \
|
|
2738 gc_count_num_##typename##_freelist = num_free; \
|
|
2739 } while (0)
|
|
2740
|
|
2741 #endif /* !ERROR_CHECK_GC */
|
|
2742
|
|
2743
|
|
2744
|
|
2745
|
|
2746 static void
|
|
2747 sweep_conses (void)
|
|
2748 {
|
|
2749 #define UNMARK_cons(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
|
|
2750 #define ADDITIONAL_FREE_cons(ptr)
|
|
2751
|
440
|
2752 SWEEP_FIXED_TYPE_BLOCK (cons, Lisp_Cons);
|
428
|
2753 }
|
|
2754
|
|
2755 /* Explicitly free a cons cell. */
|
|
2756 void
|
440
|
2757 free_cons (Lisp_Cons *ptr)
|
428
|
2758 {
|
|
2759 #ifdef ERROR_CHECK_GC
|
|
2760 /* If the CAR is not an int, then it will be a pointer, which will
|
|
2761 always be four-byte aligned. If this cons cell has already been
|
|
2762 placed on the free list, however, its car will probably contain
|
|
2763 a chain pointer to the next cons on the list, which has cleverly
|
|
2764 had all its 0's and 1's inverted. This allows for a quick
|
|
2765 check to make sure we're not freeing something already freed. */
|
|
2766 if (POINTER_TYPE_P (XTYPE (ptr->car)))
|
|
2767 ASSERT_VALID_POINTER (XPNTR (ptr->car));
|
|
2768 #endif /* ERROR_CHECK_GC */
|
|
2769
|
|
2770 #ifndef ALLOC_NO_POOLS
|
440
|
2771 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (cons, Lisp_Cons, ptr);
|
428
|
2772 #endif /* ALLOC_NO_POOLS */
|
|
2773 }
|
|
2774
|
|
2775 /* explicitly free a list. You **must make sure** that you have
|
|
2776 created all the cons cells that make up this list and that there
|
|
2777 are no pointers to any of these cons cells anywhere else. If there
|
|
2778 are, you will lose. */
|
|
2779
|
|
2780 void
|
|
2781 free_list (Lisp_Object list)
|
|
2782 {
|
|
2783 Lisp_Object rest, next;
|
|
2784
|
|
2785 for (rest = list; !NILP (rest); rest = next)
|
|
2786 {
|
|
2787 next = XCDR (rest);
|
|
2788 free_cons (XCONS (rest));
|
|
2789 }
|
|
2790 }
|
|
2791
|
|
2792 /* explicitly free an alist. You **must make sure** that you have
|
|
2793 created all the cons cells that make up this alist and that there
|
|
2794 are no pointers to any of these cons cells anywhere else. If there
|
|
2795 are, you will lose. */
|
|
2796
|
|
2797 void
|
|
2798 free_alist (Lisp_Object alist)
|
|
2799 {
|
|
2800 Lisp_Object rest, next;
|
|
2801
|
|
2802 for (rest = alist; !NILP (rest); rest = next)
|
|
2803 {
|
|
2804 next = XCDR (rest);
|
|
2805 free_cons (XCONS (XCAR (rest)));
|
|
2806 free_cons (XCONS (rest));
|
|
2807 }
|
|
2808 }
|
|
2809
|
|
2810 static void
|
|
2811 sweep_compiled_functions (void)
|
|
2812 {
|
|
2813 #define UNMARK_compiled_function(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
|
|
2814 #define ADDITIONAL_FREE_compiled_function(ptr)
|
|
2815
|
|
2816 SWEEP_FIXED_TYPE_BLOCK (compiled_function, Lisp_Compiled_Function);
|
|
2817 }
|
|
2818
|
|
2819
|
|
2820 #ifdef LISP_FLOAT_TYPE
|
|
2821 static void
|
|
2822 sweep_floats (void)
|
|
2823 {
|
|
2824 #define UNMARK_float(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
|
|
2825 #define ADDITIONAL_FREE_float(ptr)
|
|
2826
|
440
|
2827 SWEEP_FIXED_TYPE_BLOCK (float, Lisp_Float);
|
428
|
2828 }
|
|
2829 #endif /* LISP_FLOAT_TYPE */
|
|
2830
|
|
2831 static void
|
|
2832 sweep_symbols (void)
|
|
2833 {
|
|
2834 #define UNMARK_symbol(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
|
|
2835 #define ADDITIONAL_FREE_symbol(ptr)
|
|
2836
|
440
|
2837 SWEEP_FIXED_TYPE_BLOCK (symbol, Lisp_Symbol);
|
428
|
2838 }
|
|
2839
|
|
2840 static void
|
|
2841 sweep_extents (void)
|
|
2842 {
|
|
2843 #define UNMARK_extent(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
|
|
2844 #define ADDITIONAL_FREE_extent(ptr)
|
|
2845
|
|
2846 SWEEP_FIXED_TYPE_BLOCK (extent, struct extent);
|
|
2847 }
|
|
2848
|
|
2849 static void
|
|
2850 sweep_events (void)
|
|
2851 {
|
|
2852 #define UNMARK_event(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
|
|
2853 #define ADDITIONAL_FREE_event(ptr)
|
|
2854
|
440
|
2855 SWEEP_FIXED_TYPE_BLOCK (event, Lisp_Event);
|
428
|
2856 }
|
|
2857
|
|
2858 static void
|
|
2859 sweep_markers (void)
|
|
2860 {
|
|
2861 #define UNMARK_marker(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
|
|
2862 #define ADDITIONAL_FREE_marker(ptr) \
|
|
2863 do { Lisp_Object tem; \
|
|
2864 XSETMARKER (tem, ptr); \
|
|
2865 unchain_marker (tem); \
|
|
2866 } while (0)
|
|
2867
|
440
|
2868 SWEEP_FIXED_TYPE_BLOCK (marker, Lisp_Marker);
|
428
|
2869 }
|
|
2870
|
|
2871 /* Explicitly free a marker. */
|
|
2872 void
|
440
|
2873 free_marker (Lisp_Marker *ptr)
|
428
|
2874 {
|
|
2875 /* Perhaps this will catch freeing an already-freed marker. */
|
444
|
2876 gc_checking_assert (ptr->lheader.type == lrecord_type_marker);
|
428
|
2877
|
|
2878 #ifndef ALLOC_NO_POOLS
|
440
|
2879 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (marker, Lisp_Marker, ptr);
|
428
|
2880 #endif /* ALLOC_NO_POOLS */
|
|
2881 }
|
|
2882
|
|
2883
|
|
2884 #if defined (MULE) && defined (VERIFY_STRING_CHARS_INTEGRITY)
|
|
2885
|
|
2886 static void
|
|
2887 verify_string_chars_integrity (void)
|
|
2888 {
|
|
2889 struct string_chars_block *sb;
|
|
2890
|
|
2891 /* Scan each existing string block sequentially, string by string. */
|
|
2892 for (sb = first_string_chars_block; sb; sb = sb->next)
|
|
2893 {
|
|
2894 int pos = 0;
|
|
2895 /* POS is the index of the next string in the block. */
|
|
2896 while (pos < sb->pos)
|
|
2897 {
|
|
2898 struct string_chars *s_chars =
|
|
2899 (struct string_chars *) &(sb->string_chars[pos]);
|
438
|
2900 Lisp_String *string;
|
428
|
2901 int size;
|
|
2902 int fullsize;
|
|
2903
|
454
|
2904 /* If the string_chars struct is marked as free (i.e. the
|
|
2905 STRING pointer is NULL) then this is an unused chunk of
|
|
2906 string storage. (See below.) */
|
|
2907
|
|
2908 if (STRING_CHARS_FREE_P (s_chars))
|
428
|
2909 {
|
|
2910 fullsize = ((struct unused_string_chars *) s_chars)->fullsize;
|
|
2911 pos += fullsize;
|
|
2912 continue;
|
|
2913 }
|
|
2914
|
|
2915 string = s_chars->string;
|
|
2916 /* Must be 32-bit aligned. */
|
|
2917 assert ((((int) string) & 3) == 0);
|
|
2918
|
|
2919 size = string_length (string);
|
|
2920 fullsize = STRING_FULLSIZE (size);
|
|
2921
|
|
2922 assert (!BIG_STRING_FULLSIZE_P (fullsize));
|
|
2923 assert (string_data (string) == s_chars->chars);
|
|
2924 pos += fullsize;
|
|
2925 }
|
|
2926 assert (pos == sb->pos);
|
|
2927 }
|
|
2928 }
|
|
2929
|
|
2930 #endif /* MULE && ERROR_CHECK_GC */
|
|
2931
|
|
2932 /* Compactify string chars, relocating the reference to each --
|
|
2933 free any empty string_chars_block we see. */
|
|
2934 static void
|
|
2935 compact_string_chars (void)
|
|
2936 {
|
|
2937 struct string_chars_block *to_sb = first_string_chars_block;
|
|
2938 int to_pos = 0;
|
|
2939 struct string_chars_block *from_sb;
|
|
2940
|
|
2941 /* Scan each existing string block sequentially, string by string. */
|
|
2942 for (from_sb = first_string_chars_block; from_sb; from_sb = from_sb->next)
|
|
2943 {
|
|
2944 int from_pos = 0;
|
|
2945 /* FROM_POS is the index of the next string in the block. */
|
|
2946 while (from_pos < from_sb->pos)
|
|
2947 {
|
|
2948 struct string_chars *from_s_chars =
|
|
2949 (struct string_chars *) &(from_sb->string_chars[from_pos]);
|
|
2950 struct string_chars *to_s_chars;
|
438
|
2951 Lisp_String *string;
|
428
|
2952 int size;
|
|
2953 int fullsize;
|
|
2954
|
454
|
2955 /* If the string_chars struct is marked as free (i.e. the
|
|
2956 STRING pointer is NULL) then this is an unused chunk of
|
|
2957 string storage. This happens under Mule when a string's
|
|
2958 size changes in such a way that its fullsize changes.
|
|
2959 (Strings can change size because a different-length
|
|
2960 character can be substituted for another character.)
|
|
2961 In this case, after the bogus string pointer is the
|
|
2962 "fullsize" of this entry, i.e. how many bytes to skip. */
|
|
2963
|
|
2964 if (STRING_CHARS_FREE_P (from_s_chars))
|
428
|
2965 {
|
|
2966 fullsize = ((struct unused_string_chars *) from_s_chars)->fullsize;
|
|
2967 from_pos += fullsize;
|
|
2968 continue;
|
|
2969 }
|
|
2970
|
|
2971 string = from_s_chars->string;
|
454
|
2972 assert (!(LRECORD_FREE_P (string)));
|
428
|
2973
|
|
2974 size = string_length (string);
|
|
2975 fullsize = STRING_FULLSIZE (size);
|
|
2976
|
442
|
2977 gc_checking_assert (! BIG_STRING_FULLSIZE_P (fullsize));
|
428
|
2978
|
|
2979 /* Just skip it if it isn't marked. */
|
|
2980 if (! MARKED_RECORD_HEADER_P (&(string->lheader)))
|
|
2981 {
|
|
2982 from_pos += fullsize;
|
|
2983 continue;
|
|
2984 }
|
|
2985
|
|
2986 /* If it won't fit in what's left of TO_SB, close TO_SB out
|
|
2987 and go on to the next string_chars_block. We know that TO_SB
|
|
2988 cannot advance past FROM_SB here since FROM_SB is large enough
|
|
2989 to currently contain this string. */
|
|
2990 if ((to_pos + fullsize) > countof (to_sb->string_chars))
|
|
2991 {
|
|
2992 to_sb->pos = to_pos;
|
|
2993 to_sb = to_sb->next;
|
|
2994 to_pos = 0;
|
|
2995 }
|
|
2996
|
|
2997 /* Compute new address of this string
|
|
2998 and update TO_POS for the space being used. */
|
|
2999 to_s_chars = (struct string_chars *) &(to_sb->string_chars[to_pos]);
|
|
3000
|
|
3001 /* Copy the string_chars to the new place. */
|
|
3002 if (from_s_chars != to_s_chars)
|
|
3003 memmove (to_s_chars, from_s_chars, fullsize);
|
|
3004
|
|
3005 /* Relocate FROM_S_CHARS's reference */
|
|
3006 set_string_data (string, &(to_s_chars->chars[0]));
|
|
3007
|
|
3008 from_pos += fullsize;
|
|
3009 to_pos += fullsize;
|
|
3010 }
|
|
3011 }
|
|
3012
|
|
3013 /* Set current to the last string chars block still used and
|
|
3014 free any that follow. */
|
|
3015 {
|
|
3016 struct string_chars_block *victim;
|
|
3017
|
|
3018 for (victim = to_sb->next; victim; )
|
|
3019 {
|
|
3020 struct string_chars_block *next = victim->next;
|
|
3021 xfree (victim);
|
|
3022 victim = next;
|
|
3023 }
|
|
3024
|
|
3025 current_string_chars_block = to_sb;
|
|
3026 current_string_chars_block->pos = to_pos;
|
|
3027 current_string_chars_block->next = 0;
|
|
3028 }
|
|
3029 }
|
|
3030
|
|
3031 #if 1 /* Hack to debug missing purecopy's */
|
|
3032 static int debug_string_purity;
|
|
3033
|
|
3034 static void
|
438
|
3035 debug_string_purity_print (Lisp_String *p)
|
428
|
3036 {
|
|
3037 Charcount i;
|
|
3038 Charcount s = string_char_length (p);
|
442
|
3039 stderr_out ("\"");
|
428
|
3040 for (i = 0; i < s; i++)
|
|
3041 {
|
|
3042 Emchar ch = string_char (p, i);
|
|
3043 if (ch < 32 || ch >= 126)
|
|
3044 stderr_out ("\\%03o", ch);
|
|
3045 else if (ch == '\\' || ch == '\"')
|
|
3046 stderr_out ("\\%c", ch);
|
|
3047 else
|
|
3048 stderr_out ("%c", ch);
|
|
3049 }
|
|
3050 stderr_out ("\"\n");
|
|
3051 }
|
|
3052 #endif /* 1 */
|
|
3053
|
|
3054
|
|
3055 static void
|
|
3056 sweep_strings (void)
|
|
3057 {
|
|
3058 int num_small_used = 0, num_small_bytes = 0, num_bytes = 0;
|
|
3059 int debug = debug_string_purity;
|
|
3060
|
438
|
3061 #define UNMARK_string(ptr) do { \
|
|
3062 Lisp_String *p = (ptr); \
|
|
3063 size_t size = string_length (p); \
|
|
3064 UNMARK_RECORD_HEADER (&(p->lheader)); \
|
|
3065 num_bytes += size; \
|
|
3066 if (!BIG_STRING_SIZE_P (size)) \
|
442
|
3067 { \
|
|
3068 num_small_bytes += size; \
|
|
3069 num_small_used++; \
|
438
|
3070 } \
|
|
3071 if (debug) \
|
|
3072 debug_string_purity_print (p); \
|
|
3073 } while (0)
|
|
3074 #define ADDITIONAL_FREE_string(ptr) do { \
|
|
3075 size_t size = string_length (ptr); \
|
|
3076 if (BIG_STRING_SIZE_P (size)) \
|
|
3077 xfree (ptr->data); \
|
|
3078 } while (0)
|
|
3079
|
|
3080 SWEEP_FIXED_TYPE_BLOCK (string, Lisp_String);
|
428
|
3081
|
|
3082 gc_count_num_short_string_in_use = num_small_used;
|
|
3083 gc_count_string_total_size = num_bytes;
|
|
3084 gc_count_short_string_total_size = num_small_bytes;
|
|
3085 }
|
|
3086
|
|
3087
|
|
3088 /* I hate duplicating all this crap! */
|
|
3089 int
|
|
3090 marked_p (Lisp_Object obj)
|
|
3091 {
|
|
3092 /* Checks we used to perform. */
|
|
3093 /* if (EQ (obj, Qnull_pointer)) return 1; */
|
|
3094 /* if (!POINTER_TYPE_P (XGCTYPE (obj))) return 1; */
|
|
3095 /* if (PURIFIED (XPNTR (obj))) return 1; */
|
|
3096
|
|
3097 if (XTYPE (obj) == Lisp_Type_Record)
|
|
3098 {
|
|
3099 struct lrecord_header *lheader = XRECORD_LHEADER (obj);
|
442
|
3100
|
|
3101 GC_CHECK_LHEADER_INVARIANTS (lheader);
|
|
3102
|
|
3103 return MARKED_RECORD_HEADER_P (lheader);
|
428
|
3104 }
|
|
3105 return 1;
|
|
3106 }
|
|
3107
|
|
3108 static void
|
|
3109 gc_sweep (void)
|
|
3110 {
|
|
3111 /* Free all unmarked records. Do this at the very beginning,
|
|
3112 before anything else, so that the finalize methods can safely
|
|
3113 examine items in the objects. sweep_lcrecords_1() makes
|
|
3114 sure to call all the finalize methods *before* freeing anything,
|
|
3115 to complete the safety. */
|
|
3116 {
|
|
3117 int ignored;
|
|
3118 sweep_lcrecords_1 (&all_lcrecords, &ignored);
|
|
3119 }
|
|
3120
|
|
3121 compact_string_chars ();
|
|
3122
|
|
3123 /* Finalize methods below (called through the ADDITIONAL_FREE_foo
|
|
3124 macros) must be *extremely* careful to make sure they're not
|
|
3125 referencing freed objects. The only two existing finalize
|
|
3126 methods (for strings and markers) pass muster -- the string
|
|
3127 finalizer doesn't look at anything but its own specially-
|
|
3128 created block, and the marker finalizer only looks at live
|
|
3129 buffers (which will never be freed) and at the markers before
|
|
3130 and after it in the chain (which, by induction, will never be
|
|
3131 freed because if so, they would have already removed themselves
|
|
3132 from the chain). */
|
|
3133
|
|
3134 /* Put all unmarked strings on free list, free'ing the string chars
|
|
3135 of large unmarked strings */
|
|
3136 sweep_strings ();
|
|
3137
|
|
3138 /* Put all unmarked conses on free list */
|
|
3139 sweep_conses ();
|
|
3140
|
|
3141 /* Free all unmarked bit vectors */
|
|
3142 sweep_bit_vectors_1 (&all_bit_vectors,
|
|
3143 &gc_count_num_bit_vector_used,
|
|
3144 &gc_count_bit_vector_total_size,
|
|
3145 &gc_count_bit_vector_storage);
|
|
3146
|
|
3147 /* Free all unmarked compiled-function objects */
|
|
3148 sweep_compiled_functions ();
|
|
3149
|
|
3150 #ifdef LISP_FLOAT_TYPE
|
|
3151 /* Put all unmarked floats on free list */
|
|
3152 sweep_floats ();
|
|
3153 #endif
|
|
3154
|
|
3155 /* Put all unmarked symbols on free list */
|
|
3156 sweep_symbols ();
|
|
3157
|
|
3158 /* Put all unmarked extents on free list */
|
|
3159 sweep_extents ();
|
|
3160
|
|
3161 /* Put all unmarked markers on free list.
|
|
3162 Dechain each one first from the buffer into which it points. */
|
|
3163 sweep_markers ();
|
|
3164
|
|
3165 sweep_events ();
|
|
3166
|
|
3167 #ifdef PDUMP
|
442
|
3168 pdump_objects_unmark ();
|
428
|
3169 #endif
|
|
3170 }
|
|
3171
|
|
3172 /* Clearing for disksave. */
|
|
3173
|
|
3174 void
|
|
3175 disksave_object_finalization (void)
|
|
3176 {
|
|
3177 /* It's important that certain information from the environment not get
|
|
3178 dumped with the executable (pathnames, environment variables, etc.).
|
|
3179 To make it easier to tell when this has happened with strings(1) we
|
|
3180 clear some known-to-be-garbage blocks of memory, so that leftover
|
|
3181 results of old evaluation don't look like potential problems.
|
|
3182 But first we set some notable variables to nil and do one more GC,
|
|
3183 to turn those strings into garbage.
|
440
|
3184 */
|
428
|
3185
|
|
3186 /* Yeah, this list is pretty ad-hoc... */
|
|
3187 Vprocess_environment = Qnil;
|
|
3188 Vexec_directory = Qnil;
|
|
3189 Vdata_directory = Qnil;
|
|
3190 Vsite_directory = Qnil;
|
|
3191 Vdoc_directory = Qnil;
|
|
3192 Vconfigure_info_directory = Qnil;
|
|
3193 Vexec_path = Qnil;
|
|
3194 Vload_path = Qnil;
|
|
3195 /* Vdump_load_path = Qnil; */
|
|
3196 /* Release hash tables for locate_file */
|
|
3197 Flocate_file_clear_hashing (Qt);
|
|
3198 uncache_home_directory();
|
|
3199
|
|
3200 #if defined(LOADHIST) && !(defined(LOADHIST_DUMPED) || \
|
|
3201 defined(LOADHIST_BUILTIN))
|
|
3202 Vload_history = Qnil;
|
|
3203 #endif
|
|
3204 Vshell_file_name = Qnil;
|
|
3205
|
|
3206 garbage_collect_1 ();
|
|
3207
|
|
3208 /* Run the disksave finalization methods of all live objects. */
|
|
3209 disksave_object_finalization_1 ();
|
|
3210
|
|
3211 /* Zero out the uninitialized (really, unused) part of the containers
|
|
3212 for the live strings. */
|
|
3213 {
|
|
3214 struct string_chars_block *scb;
|
|
3215 for (scb = first_string_chars_block; scb; scb = scb->next)
|
|
3216 {
|
|
3217 int count = sizeof (scb->string_chars) - scb->pos;
|
|
3218
|
|
3219 assert (count >= 0 && count < STRING_CHARS_BLOCK_SIZE);
|
440
|
3220 if (count != 0)
|
|
3221 {
|
|
3222 /* from the block's fill ptr to the end */
|
|
3223 memset ((scb->string_chars + scb->pos), 0, count);
|
|
3224 }
|
428
|
3225 }
|
|
3226 }
|
|
3227
|
|
3228 /* There, that ought to be enough... */
|
|
3229
|
|
3230 }
|
|
3231
|
|
3232
|
|
3233 Lisp_Object
|
|
3234 restore_gc_inhibit (Lisp_Object val)
|
|
3235 {
|
|
3236 gc_currently_forbidden = XINT (val);
|
|
3237 return val;
|
|
3238 }
|
|
3239
|
|
3240 /* Maybe we want to use this when doing a "panic" gc after memory_full()? */
|
|
3241 static int gc_hooks_inhibited;
|
|
3242
|
|
3243
|
|
3244 void
|
|
3245 garbage_collect_1 (void)
|
|
3246 {
|
|
3247 #if MAX_SAVE_STACK > 0
|
|
3248 char stack_top_variable;
|
|
3249 extern char *stack_bottom;
|
|
3250 #endif
|
|
3251 struct frame *f;
|
|
3252 int speccount;
|
|
3253 int cursor_changed;
|
|
3254 Lisp_Object pre_gc_cursor;
|
|
3255 struct gcpro gcpro1;
|
|
3256
|
|
3257 if (gc_in_progress
|
|
3258 || gc_currently_forbidden
|
|
3259 || in_display
|
|
3260 || preparing_for_armageddon)
|
|
3261 return;
|
|
3262
|
|
3263 /* We used to call selected_frame() here.
|
|
3264
|
|
3265 The following functions cannot be called inside GC
|
|
3266 so we move to after the above tests. */
|
|
3267 {
|
|
3268 Lisp_Object frame;
|
|
3269 Lisp_Object device = Fselected_device (Qnil);
|
|
3270 if (NILP (device)) /* Could happen during startup, eg. if always_gc */
|
|
3271 return;
|
|
3272 frame = DEVICE_SELECTED_FRAME (XDEVICE (device));
|
|
3273 if (NILP (frame))
|
|
3274 signal_simple_error ("No frames exist on device", device);
|
|
3275 f = XFRAME (frame);
|
|
3276 }
|
|
3277
|
|
3278 pre_gc_cursor = Qnil;
|
|
3279 cursor_changed = 0;
|
|
3280
|
|
3281 GCPRO1 (pre_gc_cursor);
|
|
3282
|
|
3283 /* Very important to prevent GC during any of the following
|
|
3284 stuff that might run Lisp code; otherwise, we'll likely
|
|
3285 have infinite GC recursion. */
|
|
3286 speccount = specpdl_depth ();
|
|
3287 record_unwind_protect (restore_gc_inhibit,
|
|
3288 make_int (gc_currently_forbidden));
|
|
3289 gc_currently_forbidden = 1;
|
|
3290
|
|
3291 if (!gc_hooks_inhibited)
|
|
3292 run_hook_trapping_errors ("Error in pre-gc-hook", Qpre_gc_hook);
|
|
3293
|
|
3294 /* Now show the GC cursor/message. */
|
|
3295 if (!noninteractive)
|
|
3296 {
|
|
3297 if (FRAME_WIN_P (f))
|
|
3298 {
|
|
3299 Lisp_Object frame = make_frame (f);
|
|
3300 Lisp_Object cursor = glyph_image_instance (Vgc_pointer_glyph,
|
|
3301 FRAME_SELECTED_WINDOW (f),
|
|
3302 ERROR_ME_NOT, 1);
|
|
3303 pre_gc_cursor = f->pointer;
|
|
3304 if (POINTER_IMAGE_INSTANCEP (cursor)
|
|
3305 /* don't change if we don't know how to change back. */
|
|
3306 && POINTER_IMAGE_INSTANCEP (pre_gc_cursor))
|
|
3307 {
|
|
3308 cursor_changed = 1;
|
|
3309 Fset_frame_pointer (frame, cursor);
|
|
3310 }
|
|
3311 }
|
|
3312
|
|
3313 /* Don't print messages to the stream device. */
|
|
3314 if (!cursor_changed && !FRAME_STREAM_P (f))
|
|
3315 {
|
|
3316 char *msg = (STRINGP (Vgc_message)
|
|
3317 ? GETTEXT ((char *) XSTRING_DATA (Vgc_message))
|
|
3318 : 0);
|
|
3319 Lisp_Object args[2], whole_msg;
|
|
3320 args[0] = build_string (msg ? msg :
|
442
|
3321 GETTEXT ((const char *) gc_default_message));
|
428
|
3322 args[1] = build_string ("...");
|
|
3323 whole_msg = Fconcat (2, args);
|
|
3324 echo_area_message (f, (Bufbyte *) 0, whole_msg, 0, -1,
|
|
3325 Qgarbage_collecting);
|
|
3326 }
|
|
3327 }
|
|
3328
|
|
3329 /***** Now we actually start the garbage collection. */
|
|
3330
|
|
3331 gc_in_progress = 1;
|
|
3332
|
|
3333 gc_generation_number[0]++;
|
|
3334
|
|
3335 #if MAX_SAVE_STACK > 0
|
|
3336
|
|
3337 /* Save a copy of the contents of the stack, for debugging. */
|
|
3338 if (!purify_flag)
|
|
3339 {
|
|
3340 /* Static buffer in which we save a copy of the C stack at each GC. */
|
|
3341 static char *stack_copy;
|
|
3342 static size_t stack_copy_size;
|
|
3343
|
|
3344 ptrdiff_t stack_diff = &stack_top_variable - stack_bottom;
|
|
3345 size_t stack_size = (stack_diff > 0 ? stack_diff : -stack_diff);
|
|
3346 if (stack_size < MAX_SAVE_STACK)
|
|
3347 {
|
|
3348 if (stack_copy_size < stack_size)
|
|
3349 {
|
|
3350 stack_copy = (char *) xrealloc (stack_copy, stack_size);
|
|
3351 stack_copy_size = stack_size;
|
|
3352 }
|
|
3353
|
|
3354 memcpy (stack_copy,
|
|
3355 stack_diff > 0 ? stack_bottom : &stack_top_variable,
|
|
3356 stack_size);
|
|
3357 }
|
|
3358 }
|
|
3359 #endif /* MAX_SAVE_STACK > 0 */
|
|
3360
|
|
3361 /* Do some totally ad-hoc resource clearing. */
|
|
3362 /* #### generalize this? */
|
|
3363 clear_event_resource ();
|
|
3364 cleanup_specifiers ();
|
|
3365
|
|
3366 /* Mark all the special slots that serve as the roots of accessibility. */
|
|
3367
|
|
3368 { /* staticpro() */
|
452
|
3369 Lisp_Object **p = Dynarr_begin (staticpros);
|
|
3370 size_t count;
|
|
3371 for (count = Dynarr_length (staticpros); count; count--)
|
|
3372 mark_object (**p++);
|
|
3373 }
|
|
3374
|
|
3375 { /* staticpro_nodump() */
|
|
3376 Lisp_Object **p = Dynarr_begin (staticpros_nodump);
|
|
3377 size_t count;
|
|
3378 for (count = Dynarr_length (staticpros_nodump); count; count--)
|
|
3379 mark_object (**p++);
|
428
|
3380 }
|
|
3381
|
|
3382 { /* GCPRO() */
|
|
3383 struct gcpro *tail;
|
|
3384 int i;
|
|
3385 for (tail = gcprolist; tail; tail = tail->next)
|
|
3386 for (i = 0; i < tail->nvars; i++)
|
|
3387 mark_object (tail->var[i]);
|
|
3388 }
|
|
3389
|
|
3390 { /* specbind() */
|
|
3391 struct specbinding *bind;
|
|
3392 for (bind = specpdl; bind != specpdl_ptr; bind++)
|
|
3393 {
|
|
3394 mark_object (bind->symbol);
|
|
3395 mark_object (bind->old_value);
|
|
3396 }
|
|
3397 }
|
|
3398
|
|
3399 {
|
|
3400 struct catchtag *catch;
|
|
3401 for (catch = catchlist; catch; catch = catch->next)
|
|
3402 {
|
|
3403 mark_object (catch->tag);
|
|
3404 mark_object (catch->val);
|
|
3405 }
|
|
3406 }
|
|
3407
|
|
3408 {
|
|
3409 struct backtrace *backlist;
|
|
3410 for (backlist = backtrace_list; backlist; backlist = backlist->next)
|
|
3411 {
|
|
3412 int nargs = backlist->nargs;
|
|
3413 int i;
|
|
3414
|
|
3415 mark_object (*backlist->function);
|
452
|
3416 if (nargs < 0 /* nargs == UNEVALLED || nargs == MANY */)
|
428
|
3417 mark_object (backlist->args[0]);
|
|
3418 else
|
|
3419 for (i = 0; i < nargs; i++)
|
|
3420 mark_object (backlist->args[i]);
|
|
3421 }
|
|
3422 }
|
|
3423
|
|
3424 mark_redisplay ();
|
|
3425 mark_profiling_info ();
|
|
3426
|
|
3427 /* OK, now do the after-mark stuff. This is for things that
|
|
3428 are only marked when something else is marked (e.g. weak hash tables).
|
|
3429 There may be complex dependencies between such objects -- e.g.
|
|
3430 a weak hash table might be unmarked, but after processing a later
|
|
3431 weak hash table, the former one might get marked. So we have to
|
|
3432 iterate until nothing more gets marked. */
|
|
3433
|
|
3434 while (finish_marking_weak_hash_tables () > 0 ||
|
|
3435 finish_marking_weak_lists () > 0)
|
|
3436 ;
|
|
3437
|
|
3438 /* And prune (this needs to be called after everything else has been
|
|
3439 marked and before we do any sweeping). */
|
|
3440 /* #### this is somewhat ad-hoc and should probably be an object
|
|
3441 method */
|
|
3442 prune_weak_hash_tables ();
|
|
3443 prune_weak_lists ();
|
|
3444 prune_specifiers ();
|
|
3445 prune_syntax_tables ();
|
|
3446
|
|
3447 gc_sweep ();
|
|
3448
|
|
3449 consing_since_gc = 0;
|
|
3450 #ifndef DEBUG_XEMACS
|
|
3451 /* Allow you to set it really fucking low if you really want ... */
|
|
3452 if (gc_cons_threshold < 10000)
|
|
3453 gc_cons_threshold = 10000;
|
|
3454 #endif
|
|
3455
|
|
3456 gc_in_progress = 0;
|
|
3457
|
|
3458 /******* End of garbage collection ********/
|
|
3459
|
|
3460 run_hook_trapping_errors ("Error in post-gc-hook", Qpost_gc_hook);
|
|
3461
|
|
3462 /* Now remove the GC cursor/message */
|
|
3463 if (!noninteractive)
|
|
3464 {
|
|
3465 if (cursor_changed)
|
|
3466 Fset_frame_pointer (make_frame (f), pre_gc_cursor);
|
|
3467 else if (!FRAME_STREAM_P (f))
|
|
3468 {
|
|
3469 char *msg = (STRINGP (Vgc_message)
|
|
3470 ? GETTEXT ((char *) XSTRING_DATA (Vgc_message))
|
|
3471 : 0);
|
|
3472
|
|
3473 /* Show "...done" only if the echo area would otherwise be empty. */
|
|
3474 if (NILP (clear_echo_area (selected_frame (),
|
|
3475 Qgarbage_collecting, 0)))
|
|
3476 {
|
|
3477 Lisp_Object args[2], whole_msg;
|
|
3478 args[0] = build_string (msg ? msg :
|
442
|
3479 GETTEXT ((const char *)
|
428
|
3480 gc_default_message));
|
|
3481 args[1] = build_string ("... done");
|
|
3482 whole_msg = Fconcat (2, args);
|
|
3483 echo_area_message (selected_frame (), (Bufbyte *) 0,
|
|
3484 whole_msg, 0, -1,
|
|
3485 Qgarbage_collecting);
|
|
3486 }
|
|
3487 }
|
|
3488 }
|
|
3489
|
|
3490 /* now stop inhibiting GC */
|
|
3491 unbind_to (speccount, Qnil);
|
|
3492
|
|
3493 if (!breathing_space)
|
|
3494 {
|
|
3495 breathing_space = malloc (4096 - MALLOC_OVERHEAD);
|
|
3496 }
|
|
3497
|
|
3498 UNGCPRO;
|
|
3499 return;
|
|
3500 }
|
|
3501
|
|
3502 /* Debugging aids. */
|
|
3503
|
|
3504 static Lisp_Object
|
442
|
3505 gc_plist_hack (const char *name, int value, Lisp_Object tail)
|
428
|
3506 {
|
|
3507 /* C doesn't have local functions (or closures, or GC, or readable syntax,
|
|
3508 or portable numeric datatypes, or bit-vectors, or characters, or
|
|
3509 arrays, or exceptions, or ...) */
|
|
3510 return cons3 (intern (name), make_int (value), tail);
|
|
3511 }
|
|
3512
|
|
3513 #define HACK_O_MATIC(type, name, pl) do { \
|
|
3514 int s = 0; \
|
|
3515 struct type##_block *x = current_##type##_block; \
|
|
3516 while (x) { s += sizeof (*x) + MALLOC_OVERHEAD; x = x->prev; } \
|
|
3517 (pl) = gc_plist_hack ((name), s, (pl)); \
|
|
3518 } while (0)
|
|
3519
|
|
3520 DEFUN ("garbage-collect", Fgarbage_collect, 0, 0, "", /*
|
|
3521 Reclaim storage for Lisp objects no longer needed.
|
|
3522 Return info on amount of space in use:
|
|
3523 ((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS)
|
|
3524 (USED-MARKERS . FREE-MARKERS) USED-STRING-CHARS USED-VECTOR-SLOTS
|
|
3525 PLIST)
|
|
3526 where `PLIST' is a list of alternating keyword/value pairs providing
|
|
3527 more detailed information.
|
|
3528 Garbage collection happens automatically if you cons more than
|
|
3529 `gc-cons-threshold' bytes of Lisp data since previous garbage collection.
|
|
3530 */
|
|
3531 ())
|
|
3532 {
|
|
3533 Lisp_Object pl = Qnil;
|
460
|
3534 unsigned int i;
|
428
|
3535 int gc_count_vector_total_size = 0;
|
|
3536
|
|
3537 garbage_collect_1 ();
|
|
3538
|
442
|
3539 for (i = 0; i < lrecord_type_count; i++)
|
428
|
3540 {
|
|
3541 if (lcrecord_stats[i].bytes_in_use != 0
|
|
3542 || lcrecord_stats[i].bytes_freed != 0
|
|
3543 || lcrecord_stats[i].instances_on_free_list != 0)
|
|
3544 {
|
|
3545 char buf [255];
|
442
|
3546 const char *name = lrecord_implementations_table[i]->name;
|
428
|
3547 int len = strlen (name);
|
|
3548 /* save this for the FSFmacs-compatible part of the summary */
|
460
|
3549 if (i == lrecord_type_vector)
|
428
|
3550 gc_count_vector_total_size =
|
|
3551 lcrecord_stats[i].bytes_in_use + lcrecord_stats[i].bytes_freed;
|
|
3552
|
|
3553 sprintf (buf, "%s-storage", name);
|
|
3554 pl = gc_plist_hack (buf, lcrecord_stats[i].bytes_in_use, pl);
|
|
3555 /* Okay, simple pluralization check for `symbol-value-varalias' */
|
|
3556 if (name[len-1] == 's')
|
|
3557 sprintf (buf, "%ses-freed", name);
|
|
3558 else
|
|
3559 sprintf (buf, "%ss-freed", name);
|
|
3560 if (lcrecord_stats[i].instances_freed != 0)
|
|
3561 pl = gc_plist_hack (buf, lcrecord_stats[i].instances_freed, pl);
|
|
3562 if (name[len-1] == 's')
|
|
3563 sprintf (buf, "%ses-on-free-list", name);
|
|
3564 else
|
|
3565 sprintf (buf, "%ss-on-free-list", name);
|
|
3566 if (lcrecord_stats[i].instances_on_free_list != 0)
|
|
3567 pl = gc_plist_hack (buf, lcrecord_stats[i].instances_on_free_list,
|
|
3568 pl);
|
|
3569 if (name[len-1] == 's')
|
|
3570 sprintf (buf, "%ses-used", name);
|
|
3571 else
|
|
3572 sprintf (buf, "%ss-used", name);
|
|
3573 pl = gc_plist_hack (buf, lcrecord_stats[i].instances_in_use, pl);
|
|
3574 }
|
|
3575 }
|
|
3576
|
|
3577 HACK_O_MATIC (extent, "extent-storage", pl);
|
|
3578 pl = gc_plist_hack ("extents-free", gc_count_num_extent_freelist, pl);
|
|
3579 pl = gc_plist_hack ("extents-used", gc_count_num_extent_in_use, pl);
|
|
3580 HACK_O_MATIC (event, "event-storage", pl);
|
|
3581 pl = gc_plist_hack ("events-free", gc_count_num_event_freelist, pl);
|
|
3582 pl = gc_plist_hack ("events-used", gc_count_num_event_in_use, pl);
|
|
3583 HACK_O_MATIC (marker, "marker-storage", pl);
|
|
3584 pl = gc_plist_hack ("markers-free", gc_count_num_marker_freelist, pl);
|
|
3585 pl = gc_plist_hack ("markers-used", gc_count_num_marker_in_use, pl);
|
|
3586 #ifdef LISP_FLOAT_TYPE
|
|
3587 HACK_O_MATIC (float, "float-storage", pl);
|
|
3588 pl = gc_plist_hack ("floats-free", gc_count_num_float_freelist, pl);
|
|
3589 pl = gc_plist_hack ("floats-used", gc_count_num_float_in_use, pl);
|
|
3590 #endif /* LISP_FLOAT_TYPE */
|
|
3591 HACK_O_MATIC (string, "string-header-storage", pl);
|
|
3592 pl = gc_plist_hack ("long-strings-total-length",
|
|
3593 gc_count_string_total_size
|
|
3594 - gc_count_short_string_total_size, pl);
|
|
3595 HACK_O_MATIC (string_chars, "short-string-storage", pl);
|
|
3596 pl = gc_plist_hack ("short-strings-total-length",
|
|
3597 gc_count_short_string_total_size, pl);
|
|
3598 pl = gc_plist_hack ("strings-free", gc_count_num_string_freelist, pl);
|
|
3599 pl = gc_plist_hack ("long-strings-used",
|
|
3600 gc_count_num_string_in_use
|
|
3601 - gc_count_num_short_string_in_use, pl);
|
|
3602 pl = gc_plist_hack ("short-strings-used",
|
|
3603 gc_count_num_short_string_in_use, pl);
|
|
3604
|
|
3605 HACK_O_MATIC (compiled_function, "compiled-function-storage", pl);
|
|
3606 pl = gc_plist_hack ("compiled-functions-free",
|
|
3607 gc_count_num_compiled_function_freelist, pl);
|
|
3608 pl = gc_plist_hack ("compiled-functions-used",
|
|
3609 gc_count_num_compiled_function_in_use, pl);
|
|
3610
|
|
3611 pl = gc_plist_hack ("bit-vector-storage", gc_count_bit_vector_storage, pl);
|
|
3612 pl = gc_plist_hack ("bit-vectors-total-length",
|
|
3613 gc_count_bit_vector_total_size, pl);
|
|
3614 pl = gc_plist_hack ("bit-vectors-used", gc_count_num_bit_vector_used, pl);
|
|
3615
|
|
3616 HACK_O_MATIC (symbol, "symbol-storage", pl);
|
|
3617 pl = gc_plist_hack ("symbols-free", gc_count_num_symbol_freelist, pl);
|
|
3618 pl = gc_plist_hack ("symbols-used", gc_count_num_symbol_in_use, pl);
|
|
3619
|
|
3620 HACK_O_MATIC (cons, "cons-storage", pl);
|
|
3621 pl = gc_plist_hack ("conses-free", gc_count_num_cons_freelist, pl);
|
|
3622 pl = gc_plist_hack ("conses-used", gc_count_num_cons_in_use, pl);
|
|
3623
|
|
3624 /* The things we do for backwards-compatibility */
|
|
3625 return
|
|
3626 list6 (Fcons (make_int (gc_count_num_cons_in_use),
|
|
3627 make_int (gc_count_num_cons_freelist)),
|
|
3628 Fcons (make_int (gc_count_num_symbol_in_use),
|
|
3629 make_int (gc_count_num_symbol_freelist)),
|
|
3630 Fcons (make_int (gc_count_num_marker_in_use),
|
|
3631 make_int (gc_count_num_marker_freelist)),
|
|
3632 make_int (gc_count_string_total_size),
|
|
3633 make_int (gc_count_vector_total_size),
|
|
3634 pl);
|
|
3635 }
|
|
3636 #undef HACK_O_MATIC
|
|
3637
|
|
3638 DEFUN ("consing-since-gc", Fconsing_since_gc, 0, 0, "", /*
|
|
3639 Return the number of bytes consed since the last garbage collection.
|
|
3640 \"Consed\" is a misnomer in that this actually counts allocation
|
|
3641 of all different kinds of objects, not just conses.
|
|
3642
|
|
3643 If this value exceeds `gc-cons-threshold', a garbage collection happens.
|
|
3644 */
|
|
3645 ())
|
|
3646 {
|
|
3647 return make_int (consing_since_gc);
|
|
3648 }
|
|
3649
|
440
|
3650 #if 0
|
444
|
3651 DEFUN ("memory-limit", Fmemory_limit, 0, 0, 0, /*
|
428
|
3652 Return the address of the last byte Emacs has allocated, divided by 1024.
|
|
3653 This may be helpful in debugging Emacs's memory usage.
|
|
3654 The value is divided by 1024 to make sure it will fit in a lisp integer.
|
|
3655 */
|
|
3656 ())
|
|
3657 {
|
|
3658 return make_int ((EMACS_INT) sbrk (0) / 1024);
|
|
3659 }
|
440
|
3660 #endif
|
428
|
3661
|
|
3662
|
|
3663 int
|
|
3664 object_dead_p (Lisp_Object obj)
|
|
3665 {
|
|
3666 return ((BUFFERP (obj) && !BUFFER_LIVE_P (XBUFFER (obj))) ||
|
|
3667 (FRAMEP (obj) && !FRAME_LIVE_P (XFRAME (obj))) ||
|
|
3668 (WINDOWP (obj) && !WINDOW_LIVE_P (XWINDOW (obj))) ||
|
|
3669 (DEVICEP (obj) && !DEVICE_LIVE_P (XDEVICE (obj))) ||
|
|
3670 (CONSOLEP (obj) && !CONSOLE_LIVE_P (XCONSOLE (obj))) ||
|
|
3671 (EVENTP (obj) && !EVENT_LIVE_P (XEVENT (obj))) ||
|
|
3672 (EXTENTP (obj) && !EXTENT_LIVE_P (XEXTENT (obj))));
|
|
3673 }
|
|
3674
|
|
3675 #ifdef MEMORY_USAGE_STATS
|
|
3676
|
|
3677 /* Attempt to determine the actual amount of space that is used for
|
|
3678 the block allocated starting at PTR, supposedly of size "CLAIMED_SIZE".
|
|
3679
|
|
3680 It seems that the following holds:
|
|
3681
|
|
3682 1. When using the old allocator (malloc.c):
|
|
3683
|
|
3684 -- blocks are always allocated in chunks of powers of two. For
|
|
3685 each block, there is an overhead of 8 bytes if rcheck is not
|
|
3686 defined, 20 bytes if it is defined. In other words, a
|
|
3687 one-byte allocation needs 8 bytes of overhead for a total of
|
|
3688 9 bytes, and needs to have 16 bytes of memory chunked out for
|
|
3689 it.
|
|
3690
|
|
3691 2. When using the new allocator (gmalloc.c):
|
|
3692
|
|
3693 -- blocks are always allocated in chunks of powers of two up
|
|
3694 to 4096 bytes. Larger blocks are allocated in chunks of
|
|
3695 an integral multiple of 4096 bytes. The minimum block
|
|
3696 size is 2*sizeof (void *), or 16 bytes if SUNOS_LOCALTIME_BUG
|
|
3697 is defined. There is no per-block overhead, but there
|
|
3698 is an overhead of 3*sizeof (size_t) for each 4096 bytes
|
|
3699 allocated.
|
|
3700
|
|
3701 3. When using the system malloc, anything goes, but they are
|
|
3702 generally slower and more space-efficient than the GNU
|
|
3703 allocators. One possibly reasonable assumption to make
|
|
3704 for want of better data is that sizeof (void *), or maybe
|
|
3705 2 * sizeof (void *), is required as overhead and that
|
|
3706 blocks are allocated in the minimum required size except
|
|
3707 that some minimum block size is imposed (e.g. 16 bytes). */
|
|
3708
|
|
3709 size_t
|
|
3710 malloced_storage_size (void *ptr, size_t claimed_size,
|
|
3711 struct overhead_stats *stats)
|
|
3712 {
|
|
3713 size_t orig_claimed_size = claimed_size;
|
|
3714
|
|
3715 #ifdef GNU_MALLOC
|
|
3716
|
|
3717 if (claimed_size < 2 * sizeof (void *))
|
|
3718 claimed_size = 2 * sizeof (void *);
|
|
3719 # ifdef SUNOS_LOCALTIME_BUG
|
|
3720 if (claimed_size < 16)
|
|
3721 claimed_size = 16;
|
|
3722 # endif
|
|
3723 if (claimed_size < 4096)
|
|
3724 {
|
|
3725 int log = 1;
|
|
3726
|
|
3727 /* compute the log base two, more or less, then use it to compute
|
|
3728 the block size needed. */
|
|
3729 claimed_size--;
|
|
3730 /* It's big, it's heavy, it's wood! */
|
|
3731 while ((claimed_size /= 2) != 0)
|
|
3732 ++log;
|
|
3733 claimed_size = 1;
|
|
3734 /* It's better than bad, it's good! */
|
|
3735 while (log > 0)
|
|
3736 {
|
|
3737 claimed_size *= 2;
|
|
3738 log--;
|
|
3739 }
|
|
3740 /* We have to come up with some average about the amount of
|
|
3741 blocks used. */
|
|
3742 if ((size_t) (rand () & 4095) < claimed_size)
|
|
3743 claimed_size += 3 * sizeof (void *);
|
|
3744 }
|
|
3745 else
|
|
3746 {
|
|
3747 claimed_size += 4095;
|
|
3748 claimed_size &= ~4095;
|
|
3749 claimed_size += (claimed_size / 4096) * 3 * sizeof (size_t);
|
|
3750 }
|
|
3751
|
|
3752 #elif defined (SYSTEM_MALLOC)
|
|
3753
|
|
3754 if (claimed_size < 16)
|
|
3755 claimed_size = 16;
|
|
3756 claimed_size += 2 * sizeof (void *);
|
|
3757
|
|
3758 #else /* old GNU allocator */
|
|
3759
|
|
3760 # ifdef rcheck /* #### may not be defined here */
|
|
3761 claimed_size += 20;
|
|
3762 # else
|
|
3763 claimed_size += 8;
|
|
3764 # endif
|
|
3765 {
|
|
3766 int log = 1;
|
|
3767
|
|
3768 /* compute the log base two, more or less, then use it to compute
|
|
3769 the block size needed. */
|
|
3770 claimed_size--;
|
|
3771 /* It's big, it's heavy, it's wood! */
|
|
3772 while ((claimed_size /= 2) != 0)
|
|
3773 ++log;
|
|
3774 claimed_size = 1;
|
|
3775 /* It's better than bad, it's good! */
|
|
3776 while (log > 0)
|
|
3777 {
|
|
3778 claimed_size *= 2;
|
|
3779 log--;
|
|
3780 }
|
|
3781 }
|
|
3782
|
|
3783 #endif /* old GNU allocator */
|
|
3784
|
|
3785 if (stats)
|
|
3786 {
|
|
3787 stats->was_requested += orig_claimed_size;
|
|
3788 stats->malloc_overhead += claimed_size - orig_claimed_size;
|
|
3789 }
|
|
3790 return claimed_size;
|
|
3791 }
|
|
3792
|
|
3793 size_t
|
|
3794 fixed_type_block_overhead (size_t size)
|
|
3795 {
|
|
3796 size_t per_block = TYPE_ALLOC_SIZE (cons, unsigned char);
|
|
3797 size_t overhead = 0;
|
|
3798 size_t storage_size = malloced_storage_size (0, per_block, 0);
|
|
3799 while (size >= per_block)
|
|
3800 {
|
|
3801 size -= per_block;
|
|
3802 overhead += sizeof (void *) + per_block - storage_size;
|
|
3803 }
|
|
3804 if (rand () % per_block < size)
|
|
3805 overhead += sizeof (void *) + per_block - storage_size;
|
|
3806 return overhead;
|
|
3807 }
|
|
3808
|
|
3809 #endif /* MEMORY_USAGE_STATS */
|
|
3810
|
|
3811
|
|
3812 /* Initialization */
|
|
3813 void
|
|
3814 reinit_alloc_once_early (void)
|
|
3815 {
|
|
3816 gc_generation_number[0] = 0;
|
|
3817 breathing_space = 0;
|
|
3818 XSETINT (all_bit_vectors, 0); /* Qzero may not be set yet. */
|
|
3819 XSETINT (Vgc_message, 0);
|
|
3820 all_lcrecords = 0;
|
|
3821 ignore_malloc_warnings = 1;
|
|
3822 #ifdef DOUG_LEA_MALLOC
|
|
3823 mallopt (M_TRIM_THRESHOLD, 128*1024); /* trim threshold */
|
|
3824 mallopt (M_MMAP_THRESHOLD, 64*1024); /* mmap threshold */
|
|
3825 #if 0 /* Moved to emacs.c */
|
|
3826 mallopt (M_MMAP_MAX, 64); /* max. number of mmap'ed areas */
|
|
3827 #endif
|
|
3828 #endif
|
|
3829 init_string_alloc ();
|
|
3830 init_string_chars_alloc ();
|
|
3831 init_cons_alloc ();
|
|
3832 init_symbol_alloc ();
|
|
3833 init_compiled_function_alloc ();
|
|
3834 #ifdef LISP_FLOAT_TYPE
|
|
3835 init_float_alloc ();
|
|
3836 #endif /* LISP_FLOAT_TYPE */
|
|
3837 init_marker_alloc ();
|
|
3838 init_extent_alloc ();
|
|
3839 init_event_alloc ();
|
|
3840
|
|
3841 ignore_malloc_warnings = 0;
|
|
3842
|
452
|
3843 if (staticpros_nodump)
|
|
3844 Dynarr_free (staticpros_nodump);
|
|
3845 staticpros_nodump = Dynarr_new2 (Lisp_Object_ptr_dynarr, Lisp_Object *);
|
|
3846 Dynarr_resize (staticpros_nodump, 100); /* merely a small optimization */
|
428
|
3847
|
|
3848 consing_since_gc = 0;
|
|
3849 #if 1
|
|
3850 gc_cons_threshold = 500000; /* XEmacs change */
|
|
3851 #else
|
|
3852 gc_cons_threshold = 15000; /* debugging */
|
|
3853 #endif
|
|
3854 lrecord_uid_counter = 259;
|
|
3855 debug_string_purity = 0;
|
|
3856 gcprolist = 0;
|
|
3857
|
|
3858 gc_currently_forbidden = 0;
|
|
3859 gc_hooks_inhibited = 0;
|
|
3860
|
|
3861 #ifdef ERROR_CHECK_TYPECHECK
|
|
3862 ERROR_ME.really_unlikely_name_to_have_accidentally_in_a_non_errb_structure =
|
|
3863 666;
|
|
3864 ERROR_ME_NOT.
|
|
3865 really_unlikely_name_to_have_accidentally_in_a_non_errb_structure = 42;
|
|
3866 ERROR_ME_WARN.
|
|
3867 really_unlikely_name_to_have_accidentally_in_a_non_errb_structure =
|
|
3868 3333632;
|
|
3869 #endif /* ERROR_CHECK_TYPECHECK */
|
|
3870 }
|
|
3871
|
|
3872 void
|
|
3873 init_alloc_once_early (void)
|
|
3874 {
|
|
3875 reinit_alloc_once_early ();
|
|
3876
|
442
|
3877 {
|
|
3878 int i;
|
|
3879 for (i = 0; i < countof (lrecord_implementations_table); i++)
|
|
3880 lrecord_implementations_table[i] = 0;
|
|
3881 }
|
|
3882
|
|
3883 INIT_LRECORD_IMPLEMENTATION (cons);
|
|
3884 INIT_LRECORD_IMPLEMENTATION (vector);
|
|
3885 INIT_LRECORD_IMPLEMENTATION (string);
|
|
3886 INIT_LRECORD_IMPLEMENTATION (lcrecord_list);
|
428
|
3887
|
452
|
3888 staticpros = Dynarr_new2 (Lisp_Object_ptr_dynarr, Lisp_Object *);
|
|
3889 Dynarr_resize (staticpros, 1410); /* merely a small optimization */
|
|
3890 dump_add_root_struct_ptr (&staticpros, &staticpros_description);
|
428
|
3891 }
|
|
3892
|
|
3893 void
|
|
3894 reinit_alloc (void)
|
|
3895 {
|
|
3896 gcprolist = 0;
|
|
3897 }
|
|
3898
|
|
3899 void
|
|
3900 syms_of_alloc (void)
|
|
3901 {
|
442
|
3902 DEFSYMBOL (Qpre_gc_hook);
|
|
3903 DEFSYMBOL (Qpost_gc_hook);
|
|
3904 DEFSYMBOL (Qgarbage_collecting);
|
428
|
3905
|
|
3906 DEFSUBR (Fcons);
|
|
3907 DEFSUBR (Flist);
|
|
3908 DEFSUBR (Fvector);
|
|
3909 DEFSUBR (Fbit_vector);
|
|
3910 DEFSUBR (Fmake_byte_code);
|
|
3911 DEFSUBR (Fmake_list);
|
|
3912 DEFSUBR (Fmake_vector);
|
|
3913 DEFSUBR (Fmake_bit_vector);
|
|
3914 DEFSUBR (Fmake_string);
|
|
3915 DEFSUBR (Fstring);
|
|
3916 DEFSUBR (Fmake_symbol);
|
|
3917 DEFSUBR (Fmake_marker);
|
|
3918 DEFSUBR (Fpurecopy);
|
|
3919 DEFSUBR (Fgarbage_collect);
|
440
|
3920 #if 0
|
428
|
3921 DEFSUBR (Fmemory_limit);
|
440
|
3922 #endif
|
428
|
3923 DEFSUBR (Fconsing_since_gc);
|
|
3924 }
|
|
3925
|
|
3926 void
|
|
3927 vars_of_alloc (void)
|
|
3928 {
|
|
3929 DEFVAR_INT ("gc-cons-threshold", &gc_cons_threshold /*
|
|
3930 *Number of bytes of consing between garbage collections.
|
|
3931 \"Consing\" is a misnomer in that this actually counts allocation
|
|
3932 of all different kinds of objects, not just conses.
|
|
3933 Garbage collection can happen automatically once this many bytes have been
|
|
3934 allocated since the last garbage collection. All data types count.
|
|
3935
|
|
3936 Garbage collection happens automatically when `eval' or `funcall' are
|
|
3937 called. (Note that `funcall' is called implicitly as part of evaluation.)
|
|
3938 By binding this temporarily to a large number, you can effectively
|
|
3939 prevent garbage collection during a part of the program.
|
|
3940
|
|
3941 See also `consing-since-gc'.
|
|
3942 */ );
|
|
3943
|
|
3944 #ifdef DEBUG_XEMACS
|
|
3945 DEFVAR_INT ("debug-allocation", &debug_allocation /*
|
|
3946 If non-zero, print out information to stderr about all objects allocated.
|
|
3947 See also `debug-allocation-backtrace-length'.
|
|
3948 */ );
|
|
3949 debug_allocation = 0;
|
|
3950
|
|
3951 DEFVAR_INT ("debug-allocation-backtrace-length",
|
|
3952 &debug_allocation_backtrace_length /*
|
|
3953 Length (in stack frames) of short backtrace printed out by `debug-allocation'.
|
|
3954 */ );
|
|
3955 debug_allocation_backtrace_length = 2;
|
|
3956 #endif
|
|
3957
|
|
3958 DEFVAR_BOOL ("purify-flag", &purify_flag /*
|
|
3959 Non-nil means loading Lisp code in order to dump an executable.
|
|
3960 This means that certain objects should be allocated in readonly space.
|
|
3961 */ );
|
|
3962
|
|
3963 DEFVAR_LISP ("pre-gc-hook", &Vpre_gc_hook /*
|
|
3964 Function or functions to be run just before each garbage collection.
|
|
3965 Interrupts, garbage collection, and errors are inhibited while this hook
|
|
3966 runs, so be extremely careful in what you add here. In particular, avoid
|
|
3967 consing, and do not interact with the user.
|
|
3968 */ );
|
|
3969 Vpre_gc_hook = Qnil;
|
|
3970
|
|
3971 DEFVAR_LISP ("post-gc-hook", &Vpost_gc_hook /*
|
|
3972 Function or functions to be run just after each garbage collection.
|
|
3973 Interrupts, garbage collection, and errors are inhibited while this hook
|
|
3974 runs, so be extremely careful in what you add here. In particular, avoid
|
|
3975 consing, and do not interact with the user.
|
|
3976 */ );
|
|
3977 Vpost_gc_hook = Qnil;
|
|
3978
|
|
3979 DEFVAR_LISP ("gc-message", &Vgc_message /*
|
|
3980 String to print to indicate that a garbage collection is in progress.
|
|
3981 This is printed in the echo area. If the selected frame is on a
|
|
3982 window system and `gc-pointer-glyph' specifies a value (i.e. a pointer
|
|
3983 image instance) in the domain of the selected frame, the mouse pointer
|
|
3984 will change instead of this message being printed.
|
|
3985 */ );
|
|
3986 Vgc_message = build_string (gc_default_message);
|
|
3987
|
|
3988 DEFVAR_LISP ("gc-pointer-glyph", &Vgc_pointer_glyph /*
|
|
3989 Pointer glyph used to indicate that a garbage collection is in progress.
|
|
3990 If the selected window is on a window system and this glyph specifies a
|
|
3991 value (i.e. a pointer image instance) in the domain of the selected
|
|
3992 window, the pointer will be changed as specified during garbage collection.
|
|
3993 Otherwise, a message will be printed in the echo area, as controlled
|
|
3994 by `gc-message'.
|
|
3995 */ );
|
|
3996 }
|
|
3997
|
|
3998 void
|
|
3999 complex_vars_of_alloc (void)
|
|
4000 {
|
|
4001 Vgc_pointer_glyph = Fmake_glyph_internal (Qpointer);
|
|
4002 }
|