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