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