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