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