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