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