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