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