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