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