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 #ifndef standalone
|
|
45 #include "backtrace.h"
|
|
46 #include "buffer.h"
|
|
47 #include "bytecode.h"
|
70
|
48 #include "chartab.h"
|
0
|
49 #include "device.h"
|
|
50 #include "elhash.h"
|
|
51 #include "events.h"
|
|
52 #include "extents.h"
|
|
53 #include "frame.h"
|
|
54 #include "glyphs.h"
|
|
55 #include "redisplay.h"
|
|
56 #include "specifier.h"
|
272
|
57 #include "sysfile.h"
|
0
|
58 #include "window.h"
|
|
59 #endif
|
|
60
|
255
|
61 #ifdef DOUG_LEA_MALLOC
|
|
62 #include <malloc.h>
|
|
63 #endif
|
|
64
|
272
|
65 EXFUN (Fgarbage_collect, 0);
|
|
66
|
0
|
67 /* #define GDB_SUCKS */
|
|
68
|
255
|
69 #if 0 /* this is _way_ too slow to be part of the standard debug options */
|
|
70 #if defined(DEBUG_XEMACS) && defined(MULE)
|
|
71 #define VERIFY_STRING_CHARS_INTEGRITY
|
|
72 #endif
|
|
73 #endif
|
0
|
74
|
|
75 /* Define this to see where all that space is going... */
|
207
|
76 /* But the length of the printout is obnoxious, so limit it to testers */
|
255
|
77 /* If somebody wants to see this they can ask for it.
|
207
|
78 #ifdef DEBUG_XEMACS
|
0
|
79 #define PURESTAT
|
207
|
80 #endif
|
255
|
81 */
|
0
|
82
|
|
83 /* Define this to use malloc/free with no freelist for all datatypes,
|
|
84 the hope being that some debugging tools may help detect
|
|
85 freed memory references */
|
177
|
86 #ifdef USE_DEBUG_MALLOC /* Taking the above comment at face value -slb */
|
|
87 #include <dmalloc.h>
|
|
88 #define ALLOC_NO_POOLS
|
|
89 #endif
|
0
|
90
|
|
91 #include "puresize.h"
|
|
92
|
|
93 #ifdef DEBUG_XEMACS
|
|
94 int debug_allocation;
|
|
95
|
|
96 int debug_allocation_backtrace_length;
|
|
97 #endif
|
|
98
|
|
99 /* Number of bytes of consing done since the last gc */
|
|
100 EMACS_INT consing_since_gc;
|
|
101 #ifdef EMACS_BTL
|
|
102 extern void cadillac_record_backtrace ();
|
|
103 #define INCREMENT_CONS_COUNTER_1(size) \
|
|
104 do { \
|
|
105 EMACS_INT __sz__ = ((EMACS_INT) (size)); \
|
|
106 consing_since_gc += __sz__; \
|
|
107 cadillac_record_backtrace (2, __sz__); \
|
|
108 } while (0)
|
|
109 #else
|
|
110 #define INCREMENT_CONS_COUNTER_1(size) (consing_since_gc += (size))
|
183
|
111 #endif /* EMACS_BTL */
|
0
|
112
|
|
113 #define debug_allocation_backtrace() \
|
|
114 do { \
|
|
115 if (debug_allocation_backtrace_length > 0) \
|
|
116 debug_short_backtrace (debug_allocation_backtrace_length); \
|
|
117 } while (0)
|
|
118
|
|
119 #ifdef DEBUG_XEMACS
|
|
120 #define INCREMENT_CONS_COUNTER(foosize, type) \
|
|
121 do { \
|
|
122 if (debug_allocation) \
|
|
123 { \
|
|
124 stderr_out ("allocating %s (size %ld)\n", type, (long)foosize); \
|
|
125 debug_allocation_backtrace (); \
|
|
126 } \
|
|
127 INCREMENT_CONS_COUNTER_1 (foosize); \
|
|
128 } while (0)
|
|
129 #define NOSEEUM_INCREMENT_CONS_COUNTER(foosize, type) \
|
|
130 do { \
|
|
131 if (debug_allocation > 1) \
|
|
132 { \
|
|
133 stderr_out ("allocating noseeum %s (size %ld)\n", type, (long)foosize); \
|
|
134 debug_allocation_backtrace (); \
|
|
135 } \
|
|
136 INCREMENT_CONS_COUNTER_1 (foosize); \
|
|
137 } while (0)
|
|
138 #else
|
|
139 #define INCREMENT_CONS_COUNTER(size, type) INCREMENT_CONS_COUNTER_1 (size)
|
|
140 #define NOSEEUM_INCREMENT_CONS_COUNTER(size, type) \
|
|
141 INCREMENT_CONS_COUNTER_1 (size)
|
|
142 #endif
|
|
143
|
|
144 #define DECREMENT_CONS_COUNTER(size) \
|
|
145 do { \
|
|
146 EMACS_INT __sz__ = ((EMACS_INT) (size)); \
|
|
147 if (consing_since_gc >= __sz__) \
|
|
148 consing_since_gc -= __sz__; \
|
|
149 else \
|
|
150 consing_since_gc = 0; \
|
|
151 } while (0)
|
|
152
|
|
153 /* Number of bytes of consing since gc before another gc should be done. */
|
|
154 EMACS_INT gc_cons_threshold;
|
|
155
|
|
156 /* Nonzero during gc */
|
|
157 int gc_in_progress;
|
|
158
|
|
159 /* Number of times GC has happened at this level or below.
|
|
160 * Level 0 is most volatile, contrary to usual convention.
|
|
161 * (Of course, there's only one level at present) */
|
|
162 EMACS_INT gc_generation_number[1];
|
|
163
|
|
164 /* This is just for use by the printer, to allow things to print uniquely */
|
|
165 static int lrecord_uid_counter;
|
|
166
|
|
167 /* Nonzero when calling certain hooks or doing other things where
|
|
168 a GC would be bad */
|
|
169 int gc_currently_forbidden;
|
|
170
|
|
171 /* Hooks. */
|
|
172 Lisp_Object Vpre_gc_hook, Qpre_gc_hook;
|
|
173 Lisp_Object Vpost_gc_hook, Qpost_gc_hook;
|
|
174
|
|
175 /* "Garbage collecting" */
|
|
176 Lisp_Object Vgc_message;
|
|
177 Lisp_Object Vgc_pointer_glyph;
|
|
178 static CONST char gc_default_message[] = "Garbage collecting";
|
|
179 Lisp_Object Qgarbage_collecting;
|
|
180
|
|
181 #ifndef VIRT_ADDR_VARIES
|
|
182 extern
|
|
183 #endif /* VIRT_ADDR_VARIES */
|
|
184 EMACS_INT malloc_sbrk_used;
|
|
185
|
|
186 #ifndef VIRT_ADDR_VARIES
|
|
187 extern
|
|
188 #endif /* VIRT_ADDR_VARIES */
|
|
189 EMACS_INT malloc_sbrk_unused;
|
|
190
|
|
191 /* Non-zero means defun should do purecopy on the function definition */
|
|
192 int purify_flag;
|
|
193
|
251
|
194 #ifdef HEAP_IN_DATA
|
|
195 extern void sheap_adjust_h();
|
|
196 #endif
|
|
197
|
272
|
198 #define PUREBEG ((char *) pure)
|
0
|
199
|
255
|
200 #if 0 /* This is breathing_space in XEmacs */
|
|
201 /* Points to memory space allocated as "spare",
|
|
202 to be freed if we run out of memory. */
|
|
203 static char *spare_memory;
|
|
204
|
|
205 /* Amount of spare memory to keep in reserve. */
|
|
206 #define SPARE_MEMORY (1 << 14)
|
|
207 #endif
|
|
208
|
0
|
209 /* Index in pure at which next pure object will be allocated. */
|
272
|
210 static size_t pure_bytes_used;
|
|
211
|
|
212 #define PURIFIED(ptr) \
|
|
213 ((char *) (ptr) >= PUREBEG && \
|
|
214 (char *) (ptr) < PUREBEG + get_PURESIZE())
|
|
215
|
|
216 /* Non-zero if pure_bytes_used > get_PURESIZE(); accounts for excess purespace needs. */
|
|
217 static size_t pure_lossage;
|
0
|
218
|
|
219 #ifdef ERROR_CHECK_TYPECHECK
|
|
220
|
|
221 Error_behavior ERROR_ME, ERROR_ME_NOT, ERROR_ME_WARN;
|
|
222
|
|
223 #endif
|
|
224
|
|
225 int
|
|
226 purified (Lisp_Object obj)
|
|
227 {
|
272
|
228 return POINTER_TYPE_P (XGCTYPE (obj)) && PURIFIED (XPNTR (obj));
|
0
|
229 }
|
|
230
|
272
|
231 size_t
|
0
|
232 purespace_usage (void)
|
|
233 {
|
272
|
234 return pure_bytes_used;
|
0
|
235 }
|
|
236
|
|
237 static int
|
272
|
238 check_purespace (size_t size)
|
0
|
239 {
|
|
240 if (pure_lossage)
|
|
241 {
|
|
242 pure_lossage += size;
|
173
|
243 return 0;
|
0
|
244 }
|
272
|
245 else if (pure_bytes_used + size > get_PURESIZE())
|
0
|
246 {
|
104
|
247 /* This can cause recursive bad behavior, we'll yell at the end */
|
|
248 /* when we're done. */
|
|
249 /* message ("\nERROR: Pure Lisp storage exhausted!\n"); */
|
0
|
250 pure_lossage = size;
|
173
|
251 return 0;
|
0
|
252 }
|
|
253 else
|
173
|
254 return 1;
|
0
|
255 }
|
|
256
|
|
257
|
|
258
|
|
259 #ifndef PURESTAT
|
|
260
|
272
|
261 #define bump_purestat(p,b) DO_NOTHING
|
0
|
262
|
|
263 #else /* PURESTAT */
|
|
264
|
|
265 static int purecopying_for_bytecode;
|
|
266
|
272
|
267 static size_t pure_sizeof (Lisp_Object /*, int recurse */);
|
0
|
268
|
|
269 /* Keep statistics on how much of what is in purespace */
|
183
|
270 static struct purestat
|
0
|
271 {
|
|
272 int nobjects;
|
|
273 int nbytes;
|
|
274 CONST char *name;
|
183
|
275 }
|
|
276 purestat_cons = {0, 0, "cons cells"},
|
|
277 purestat_float = {0, 0, "float objects"},
|
|
278 purestat_string_pname = {0, 0, "symbol-name strings"},
|
|
279 purestat_bytecode = {0, 0, "compiled-function objects"},
|
|
280 purestat_string_bytecodes = {0, 0, "byte-code strings"},
|
|
281 purestat_vector_bytecode_constants = {0, 0, "byte-constant vectors"},
|
|
282 purestat_string_interactive = {0, 0, "interactive strings"},
|
0
|
283 #ifdef I18N3
|
183
|
284 purestat_string_domain = {0, 0, "domain strings"},
|
0
|
285 #endif
|
183
|
286 purestat_string_documentation = {0, 0, "documentation strings"},
|
|
287 purestat_string_other_function = {0, 0, "other function strings"},
|
|
288 purestat_vector_other = {0, 0, "other vectors"},
|
|
289 purestat_string_other = {0, 0, "other strings"},
|
|
290 purestat_string_all = {0, 0, "all strings"},
|
|
291 purestat_vector_all = {0, 0, "all vectors"};
|
0
|
292
|
|
293 static struct purestat *purestats[] =
|
|
294 {
|
|
295 &purestat_cons,
|
|
296 &purestat_float,
|
|
297 &purestat_string_pname,
|
|
298 &purestat_bytecode,
|
|
299 &purestat_string_bytecodes,
|
|
300 &purestat_vector_bytecode_constants,
|
|
301 &purestat_string_interactive,
|
|
302 #ifdef I18N3
|
|
303 &purestat_string_domain,
|
|
304 #endif
|
|
305 &purestat_string_documentation,
|
|
306 &purestat_string_other_function,
|
|
307 &purestat_vector_other,
|
|
308 &purestat_string_other,
|
|
309 0,
|
|
310 &purestat_string_all,
|
|
311 &purestat_vector_all
|
|
312 };
|
|
313
|
|
314 static void
|
272
|
315 bump_purestat (struct purestat *purestat, size_t nbytes)
|
0
|
316 {
|
|
317 if (pure_lossage) return;
|
|
318 purestat->nobjects += 1;
|
|
319 purestat->nbytes += nbytes;
|
|
320 }
|
|
321 #endif /* PURESTAT */
|
|
322
|
|
323
|
|
324 /* Maximum amount of C stack to save when a GC happens. */
|
|
325
|
|
326 #ifndef MAX_SAVE_STACK
|
|
327 #define MAX_SAVE_STACK 16000
|
|
328 #endif
|
|
329
|
|
330 /* Non-zero means ignore malloc warnings. Set during initialization. */
|
|
331 int ignore_malloc_warnings;
|
|
332
|
|
333
|
|
334 static void *breathing_space;
|
|
335
|
|
336 void
|
|
337 release_breathing_space (void)
|
|
338 {
|
183
|
339 if (breathing_space)
|
0
|
340 {
|
|
341 void *tmp = breathing_space;
|
|
342 breathing_space = 0;
|
|
343 xfree (tmp);
|
|
344 }
|
|
345 }
|
|
346
|
|
347 /* malloc calls this if it finds we are near exhausting storage */
|
|
348 void
|
|
349 malloc_warning (CONST char *str)
|
|
350 {
|
|
351 if (ignore_malloc_warnings)
|
|
352 return;
|
|
353
|
|
354 warn_when_safe
|
|
355 (Qmemory, Qcritical,
|
|
356 "%s\n"
|
|
357 "Killing some buffers may delay running out of memory.\n"
|
|
358 "However, certainly by the time you receive the 95%% warning,\n"
|
|
359 "you should clean up, kill this Emacs, and start a new one.",
|
|
360 str);
|
|
361 }
|
|
362
|
|
363 /* Called if malloc returns zero */
|
|
364 DOESNT_RETURN
|
|
365 memory_full (void)
|
|
366 {
|
|
367 /* Force a GC next time eval is called.
|
|
368 It's better to loop garbage-collecting (we might reclaim enough
|
|
369 to win) than to loop beeping and barfing "Memory exhausted"
|
|
370 */
|
|
371 consing_since_gc = gc_cons_threshold + 1;
|
|
372 release_breathing_space ();
|
|
373
|
|
374 #ifndef standalone
|
|
375 /* Flush some histories which might conceivably contain
|
|
376 * garbalogical inhibitors */
|
|
377 if (!NILP (Fboundp (Qvalues)))
|
|
378 Fset (Qvalues, Qnil);
|
|
379 Vcommand_history = Qnil;
|
|
380 #endif
|
|
381
|
|
382 error ("Memory exhausted");
|
|
383 }
|
|
384
|
|
385 /* like malloc and realloc but check for no memory left, and block input. */
|
|
386
|
177
|
387 #ifdef xmalloc
|
|
388 #undef xmalloc
|
|
389 #endif
|
|
390
|
0
|
391 void *
|
185
|
392 xmalloc (size_t size)
|
0
|
393 {
|
185
|
394 void *val = (void *) malloc (size);
|
0
|
395
|
|
396 if (!val && (size != 0)) memory_full ();
|
|
397 return val;
|
|
398 }
|
|
399
|
|
400 void *
|
185
|
401 xmalloc_and_zero (size_t size)
|
0
|
402 {
|
|
403 void *val = xmalloc (size);
|
|
404 memset (val, 0, size);
|
|
405 return val;
|
|
406 }
|
|
407
|
177
|
408 #ifdef xrealloc
|
|
409 #undef xrealloc
|
|
410 #endif
|
|
411
|
0
|
412 void *
|
185
|
413 xrealloc (void *block, size_t size)
|
0
|
414 {
|
|
415 /* We must call malloc explicitly when BLOCK is 0, since some
|
|
416 reallocs don't do this. */
|
185
|
417 void *val = (void *) (block ? realloc (block, size) : malloc (size));
|
0
|
418
|
|
419 if (!val && (size != 0)) memory_full ();
|
|
420 return val;
|
|
421 }
|
|
422
|
|
423 void
|
|
424 #ifdef ERROR_CHECK_MALLOC
|
|
425 xfree_1 (void *block)
|
|
426 #else
|
|
427 xfree (void *block)
|
|
428 #endif
|
|
429 {
|
|
430 #ifdef ERROR_CHECK_MALLOC
|
|
431 /* Unbelievably, calling free() on 0xDEADBEEF doesn't cause an
|
|
432 error until much later on for many system mallocs, such as
|
|
433 the one that comes with Solaris 2.3. FMH!! */
|
|
434 assert (block != (void *) 0xDEADBEEF);
|
|
435 assert (block);
|
183
|
436 #endif /* ERROR_CHECK_MALLOC */
|
0
|
437 free (block);
|
|
438 }
|
|
439
|
272
|
440 #ifdef ERROR_CHECK_GC
|
|
441
|
|
442 #if SIZEOF_INT == 4
|
|
443 typedef unsigned int four_byte_t;
|
|
444 #elif SIZEOF_LONG == 4
|
|
445 typedef unsigned long four_byte_t;
|
|
446 #elif SIZEOF_SHORT == 4
|
|
447 typedef unsigned short four_byte_t;
|
0
|
448 #else
|
|
449 What kind of strange-ass system are we running on?
|
|
450 #endif
|
|
451
|
|
452 static void
|
272
|
453 deadbeef_memory (void *ptr, size_t size)
|
0
|
454 {
|
272
|
455 four_byte_t *ptr4 = (four_byte_t *) ptr;
|
|
456 size_t beefs = size >> 2;
|
|
457
|
|
458 /* In practice, size will always be a multiple of four. */
|
|
459 while (beefs--)
|
|
460 (*ptr4++) = 0xDEADBEEF;
|
0
|
461 }
|
|
462
|
183
|
463 #else /* !ERROR_CHECK_GC */
|
|
464
|
0
|
465
|
|
466 #define deadbeef_memory(ptr, size)
|
|
467
|
183
|
468 #endif /* !ERROR_CHECK_GC */
|
0
|
469
|
177
|
470 #ifdef xstrdup
|
|
471 #undef xstrdup
|
|
472 #endif
|
|
473
|
0
|
474 char *
|
|
475 xstrdup (CONST char *str)
|
|
476 {
|
|
477 int len = strlen (str) + 1; /* for stupid terminating 0 */
|
|
478
|
185
|
479 void *val = xmalloc (len);
|
0
|
480 if (val == 0) return 0;
|
|
481 memcpy (val, str, len);
|
185
|
482 return (char *) val;
|
0
|
483 }
|
|
484
|
|
485 #ifdef NEED_STRDUP
|
|
486 char *
|
|
487 strdup (CONST char *s)
|
|
488 {
|
|
489 return xstrdup (s);
|
|
490 }
|
|
491 #endif /* NEED_STRDUP */
|
|
492
|
|
493
|
|
494 static void *
|
272
|
495 allocate_lisp_storage (size_t size)
|
0
|
496 {
|
|
497 void *p = xmalloc (size);
|
272
|
498 #ifndef USE_MINIMAL_TAGBITS
|
0
|
499 char *lim = ((char *) p) + size;
|
272
|
500 Lisp_Object val;
|
0
|
501
|
207
|
502 XSETOBJ (val, Lisp_Type_Record, lim);
|
|
503 if ((char *) XPNTR (val) != lim)
|
0
|
504 {
|
|
505 xfree (p);
|
|
506 memory_full ();
|
|
507 }
|
272
|
508 #endif /* ! USE_MINIMAL_TAGBITS */
|
173
|
509 return p;
|
0
|
510 }
|
|
511
|
|
512
|
|
513 /* lrecords are chained together through their "next.v" field.
|
|
514 * After doing the mark phase, the GC will walk this linked
|
272
|
515 * list and free any record which hasn't been marked.
|
0
|
516 */
|
|
517 static struct lcrecord_header *all_lcrecords;
|
|
518
|
|
519 void *
|
272
|
520 alloc_lcrecord (size_t size, CONST struct lrecord_implementation *implementation)
|
0
|
521 {
|
|
522 struct lcrecord_header *lcheader;
|
|
523
|
|
524 if (size <= 0) abort ();
|
|
525 if (implementation->static_size == 0)
|
|
526 {
|
|
527 if (!implementation->size_in_bytes_method)
|
|
528 abort ();
|
|
529 }
|
|
530 else if (implementation->static_size != size)
|
|
531 abort ();
|
|
532
|
185
|
533 lcheader = (struct lcrecord_header *) allocate_lisp_storage (size);
|
211
|
534 set_lheader_implementation(&(lcheader->lheader), implementation);
|
0
|
535 lcheader->next = all_lcrecords;
|
|
536 #if 1 /* mly prefers to see small ID numbers */
|
|
537 lcheader->uid = lrecord_uid_counter++;
|
|
538 #else /* jwz prefers to see real addrs */
|
|
539 lcheader->uid = (int) &lcheader;
|
|
540 #endif
|
|
541 lcheader->free = 0;
|
|
542 all_lcrecords = lcheader;
|
|
543 INCREMENT_CONS_COUNTER (size, implementation->name);
|
173
|
544 return lcheader;
|
0
|
545 }
|
|
546
|
|
547 #if 0 /* Presently unused */
|
|
548 /* Very, very poor man's EGC?
|
|
549 * This may be slow and thrash pages all over the place.
|
|
550 * Only call it if you really feel you must (and if the
|
|
551 * lrecord was fairly recently allocated).
|
|
552 * Otherwise, just let the GC do its job -- that's what it's there for
|
|
553 */
|
|
554 void
|
|
555 free_lcrecord (struct lcrecord_header *lcrecord)
|
|
556 {
|
|
557 if (all_lcrecords == lcrecord)
|
|
558 {
|
|
559 all_lcrecords = lcrecord->next;
|
|
560 }
|
|
561 else
|
|
562 {
|
|
563 struct lrecord_header *header = all_lcrecords;
|
|
564 for (;;)
|
|
565 {
|
|
566 struct lrecord_header *next = header->next;
|
|
567 if (next == lcrecord)
|
|
568 {
|
|
569 header->next = lrecord->next;
|
|
570 break;
|
|
571 }
|
|
572 else if (next == 0)
|
|
573 abort ();
|
|
574 else
|
|
575 header = next;
|
|
576 }
|
|
577 }
|
|
578 if (lrecord->implementation->finalizer)
|
|
579 ((lrecord->implementation->finalizer) (lrecord, 0));
|
|
580 xfree (lrecord);
|
|
581 return;
|
|
582 }
|
|
583 #endif /* Unused */
|
|
584
|
|
585
|
|
586 static void
|
|
587 disksave_object_finalization_1 (void)
|
|
588 {
|
|
589 struct lcrecord_header *header;
|
|
590
|
|
591 for (header = all_lcrecords; header; header = header->next)
|
|
592 {
|
211
|
593 if (LHEADER_IMPLEMENTATION(&header->lheader)->finalizer &&
|
|
594 !header->free)
|
|
595 ((LHEADER_IMPLEMENTATION(&header->lheader)->finalizer)
|
|
596 (header, 1));
|
0
|
597 }
|
|
598 }
|
183
|
599
|
0
|
600
|
|
601 /* This must not be called -- it just serves as for EQ test
|
|
602 * If lheader->implementation->finalizer is this_marks_a_marked_record,
|
|
603 * then lrecord has been marked by the GC sweeper
|
|
604 * header->implementation is put back to its correct value by
|
|
605 * sweep_records */
|
|
606 void
|
|
607 this_marks_a_marked_record (void *dummy0, int dummy1)
|
|
608 {
|
|
609 abort ();
|
|
610 }
|
|
611
|
|
612 /* Semi-kludge -- lrecord_symbol_value_forward objects get stuck
|
|
613 in CONST space and you get SEGV's if you attempt to mark them.
|
|
614 This sits in lheader->implementation->marker. */
|
|
615
|
|
616 Lisp_Object
|
|
617 this_one_is_unmarkable (Lisp_Object obj, void (*markobj) (Lisp_Object))
|
|
618 {
|
|
619 abort ();
|
|
620 return Qnil;
|
|
621 }
|
|
622
|
|
623 /* XGCTYPE for records */
|
|
624 int
|
|
625 gc_record_type_p (Lisp_Object frob, CONST struct lrecord_implementation *type)
|
|
626 {
|
272
|
627 CONST struct lrecord_implementation *imp;
|
|
628
|
|
629 if (XGCTYPE (frob) != Lisp_Type_Record)
|
|
630 return 0;
|
|
631
|
|
632 imp = XRECORD_LHEADER_IMPLEMENTATION (frob);
|
211
|
633 #ifdef USE_INDEXED_LRECORD_IMPLEMENTATION
|
272
|
634 return imp == type;
|
211
|
635 #else
|
272
|
636 return imp == type || imp == type + 1;
|
211
|
637 #endif
|
0
|
638 }
|
|
639
|
|
640
|
|
641 /**********************************************************************/
|
272
|
642 /* Debugger support */
|
|
643 /**********************************************************************/
|
|
644 /* Give gdb/dbx enough information to decode Lisp Objects.
|
|
645 We make sure certain symbols are defined, so gdb doesn't complain
|
|
646 about expressions in src/gdbinit. Values are randomly chosen.
|
|
647 See src/gdbinit or src/dbxrc to see how this is used. */
|
|
648
|
|
649 enum dbg_constants
|
|
650 {
|
|
651 #ifdef USE_MINIMAL_TAGBITS
|
|
652 dbg_valmask = (EMACS_INT) (((1UL << VALBITS) - 1) << GCBITS),
|
|
653 dbg_typemask = (EMACS_INT) ((1UL << GCTYPEBITS) - 1),
|
|
654 dbg_USE_MINIMAL_TAGBITS = 1,
|
|
655 dbg_Lisp_Type_Int = 100,
|
|
656 #else /* ! USE_MIMIMAL_TAGBITS */
|
|
657 dbg_valmask = (EMACS_INT) ((1UL << VALBITS) - 1),
|
|
658 dbg_typemask = (EMACS_INT) (((1UL << GCTYPEBITS) - 1) << (VALBITS + GCMARKBITS)),
|
|
659 dbg_USE_MINIMAL_TAGBITS = 0,
|
|
660 dbg_Lisp_Type_Int = Lisp_Type_Int,
|
|
661 #endif /* ! USE_MIMIMAL_TAGBITS */
|
|
662 #ifdef USE_INDEXED_LRECORD_IMPLEMENTATION
|
|
663 dbg_USE_INDEXED_LRECORD_IMPLEMENTATION = 1,
|
|
664 #else
|
|
665 dbg_USE_INDEXED_LRECORD_IMPLEMENTATION = 0,
|
|
666 #endif
|
|
667 dbg_Lisp_Type_Char = Lisp_Type_Char,
|
|
668 dbg_Lisp_Type_Record = Lisp_Type_Record,
|
|
669 #ifdef LRECORD_CONS
|
|
670 dbg_Lisp_Type_Cons = 101,
|
|
671 #else
|
|
672 dbg_Lisp_Type_Cons = Lisp_Type_Cons,
|
|
673 lrecord_cons = 201,
|
|
674 #endif
|
|
675 #ifdef LRECORD_STRING
|
|
676 dbg_Lisp_Type_String = 102,
|
|
677 #else
|
|
678 dbg_Lisp_Type_String = Lisp_Type_String,
|
|
679 lrecord_string = 202,
|
|
680 #endif
|
|
681 #ifdef LRECORD_VECTOR
|
|
682 dbg_Lisp_Type_Vector = 103,
|
|
683 #else
|
|
684 dbg_Lisp_Type_Vector = Lisp_Type_Vector,
|
|
685 lrecord_vector = 203,
|
|
686 #endif
|
|
687 #ifdef LRECORD_SYMBOL
|
|
688 dbg_Lisp_Type_Symbol = 104,
|
|
689 #else
|
|
690 dbg_Lisp_Type_Symbol = Lisp_Type_Symbol,
|
|
691 lrecord_symbol = 204,
|
|
692 #endif
|
|
693 #ifndef MULE
|
|
694 lrecord_char_table_entry = 205,
|
|
695 lrecord_charset = 206,
|
|
696 lrecord_coding_system = 207,
|
|
697 #endif
|
|
698 #ifndef HAVE_TOOLBARS
|
|
699 lrecord_toolbar_button = 208,
|
|
700 lrecord_toolbar_data = 209,
|
|
701 #endif
|
|
702 #ifndef HAVE_TOOLTALK
|
|
703 lrecord_tooltalk_message = 210,
|
|
704 lrecord_tooltalk_pattern = 211,
|
|
705 #endif
|
|
706 #ifndef HAVE_DATABASE
|
|
707 lrecord_database = 212,
|
|
708 #endif
|
|
709 dbg_valbits = VALBITS,
|
|
710 dbg_gctypebits = GCTYPEBITS
|
|
711 /* If we don't have an actual object of this enum, pgcc (and perhaps
|
|
712 other compilers) might optimize away the entire type declaration :-( */
|
|
713 } dbg_dummy;
|
|
714
|
|
715
|
|
716 /**********************************************************************/
|
0
|
717 /* Fixed-size type macros */
|
|
718 /**********************************************************************/
|
|
719
|
|
720 /* For fixed-size types that are commonly used, we malloc() large blocks
|
|
721 of memory at a time and subdivide them into chunks of the correct
|
|
722 size for an object of that type. This is more efficient than
|
|
723 malloc()ing each object separately because we save on malloc() time
|
|
724 and overhead due to the fewer number of malloc()ed blocks, and
|
|
725 also because we don't need any extra pointers within each object
|
|
726 to keep them threaded together for GC purposes. For less common
|
|
727 (and frequently large-size) types, we use lcrecords, which are
|
|
728 malloc()ed individually and chained together through a pointer
|
|
729 in the lcrecord header. lcrecords do not need to be fixed-size
|
|
730 (i.e. two objects of the same type need not have the same size;
|
|
731 however, the size of a particular object cannot vary dynamically).
|
|
732 It is also much easier to create a new lcrecord type because no
|
|
733 additional code needs to be added to alloc.c. Finally, lcrecords
|
|
734 may be more efficient when there are only a small number of them.
|
|
735
|
|
736 The types that are stored in these large blocks (or "frob blocks")
|
|
737 are cons, float, compiled-function, symbol, marker, extent, event,
|
|
738 and string.
|
|
739
|
|
740 Note that strings are special in that they are actually stored in
|
|
741 two parts: a structure containing information about the string, and
|
|
742 the actual data associated with the string. The former structure
|
|
743 (a struct Lisp_String) is a fixed-size structure and is managed the
|
|
744 same way as all the other such types. This structure contains a
|
|
745 pointer to the actual string data, which is stored in structures of
|
|
746 type struct string_chars_block. Each string_chars_block consists
|
|
747 of a pointer to a struct Lisp_String, followed by the data for that
|
|
748 string, followed by another pointer to a struct Lisp_String,
|
|
749 followed by the data for that string, etc. At GC time, the data in
|
|
750 these blocks is compacted by searching sequentially through all the
|
|
751 blocks and compressing out any holes created by unmarked strings.
|
|
752 Strings that are more than a certain size (bigger than the size of
|
|
753 a string_chars_block, although something like half as big might
|
|
754 make more sense) are malloc()ed separately and not stored in
|
|
755 string_chars_blocks. Furthermore, no one string stretches across
|
|
756 two string_chars_blocks.
|
|
757
|
|
758 Vectors are each malloc()ed separately, similar to lcrecords.
|
183
|
759
|
0
|
760 In the following discussion, we use conses, but it applies equally
|
|
761 well to the other fixed-size types.
|
|
762
|
|
763 We store cons cells inside of cons_blocks, allocating a new
|
|
764 cons_block with malloc() whenever necessary. Cons cells reclaimed
|
|
765 by GC are put on a free list to be reallocated before allocating
|
|
766 any new cons cells from the latest cons_block. Each cons_block is
|
|
767 just under 2^n - MALLOC_OVERHEAD bytes long, since malloc (at least
|
|
768 the versions in malloc.c and gmalloc.c) really allocates in units
|
|
769 of powers of two and uses 4 bytes for its own overhead.
|
|
770
|
|
771 What GC actually does is to search through all the cons_blocks,
|
|
772 from the most recently allocated to the oldest, and put all
|
|
773 cons cells that are not marked (whether or not they're already
|
|
774 free) on a cons_free_list. The cons_free_list is a stack, and
|
|
775 so the cons cells in the oldest-allocated cons_block end up
|
|
776 at the head of the stack and are the first to be reallocated.
|
|
777 If any cons_block is entirely free, it is freed with free()
|
|
778 and its cons cells removed from the cons_free_list. Because
|
|
779 the cons_free_list ends up basically in memory order, we have
|
|
780 a high locality of reference (assuming a reasonable turnover
|
|
781 of allocating and freeing) and have a reasonable probability
|
|
782 of entirely freeing up cons_blocks that have been more recently
|
|
783 allocated. This stage is called the "sweep stage" of GC, and
|
|
784 is executed after the "mark stage", which involves starting
|
|
785 from all places that are known to point to in-use Lisp objects
|
|
786 (e.g. the obarray, where are all symbols are stored; the
|
|
787 current catches and condition-cases; the backtrace list of
|
|
788 currently executing functions; the gcpro list; etc.) and
|
|
789 recursively marking all objects that are accessible.
|
|
790
|
|
791 At the beginning of the sweep stage, the conses in the cons
|
|
792 blocks are in one of three states: in use and marked, in use
|
|
793 but not marked, and not in use (already freed). Any conses
|
|
794 that are marked have been marked in the mark stage just
|
|
795 executed, because as part of the sweep stage we unmark any
|
|
796 marked objects. The way we tell whether or not a cons cell
|
|
797 is in use is through the FREE_STRUCT_P macro. This basically
|
|
798 looks at the first 4 bytes (or however many bytes a pointer
|
|
799 fits in) to see if all the bits in those bytes are 1. The
|
|
800 resulting value (0xFFFFFFFF) is not a valid pointer and is
|
|
801 not a valid Lisp_Object. All current fixed-size types have
|
|
802 a pointer or Lisp_Object as their first element with the
|
|
803 exception of strings; they have a size value, which can
|
|
804 never be less than zero, and so 0xFFFFFFFF is invalid for
|
|
805 strings as well. Now assuming that a cons cell is in use,
|
|
806 the way we tell whether or not it is marked is to look at
|
|
807 the mark bit of its car (each Lisp_Object has one bit
|
|
808 reserved as a mark bit, in case it's needed). Note that
|
|
809 different types of objects use different fields to indicate
|
|
810 whether the object is marked, but the principle is the same.
|
|
811
|
|
812 Conses on the free_cons_list are threaded through a pointer
|
|
813 stored in the bytes directly after the bytes that are set
|
|
814 to 0xFFFFFFFF (we cannot overwrite these because the cons
|
|
815 is still in a cons_block and needs to remain marked as
|
|
816 not in use for the next time that GC happens). This
|
|
817 implies that all fixed-size types must be at least big
|
|
818 enough to store two pointers, which is indeed the case
|
|
819 for all current fixed-size types.
|
|
820
|
|
821 Some types of objects need additional "finalization" done
|
|
822 when an object is converted from in use to not in use;
|
|
823 this is the purpose of the ADDITIONAL_FREE_type macro.
|
|
824 For example, markers need to be removed from the chain
|
|
825 of markers that is kept in each buffer. This is because
|
|
826 markers in a buffer automatically disappear if the marker
|
|
827 is no longer referenced anywhere (the same does not
|
|
828 apply to extents, however).
|
|
829
|
|
830 WARNING: Things are in an extremely bizarre state when
|
|
831 the ADDITIONAL_FREE_type macros are called, so beware!
|
|
832
|
|
833 When ERROR_CHECK_GC is defined, we do things differently
|
|
834 so as to maximize our chances of catching places where
|
|
835 there is insufficient GCPROing. The thing we want to
|
|
836 avoid is having an object that we're using but didn't
|
|
837 GCPRO get freed by GC and then reallocated while we're
|
|
838 in the process of using it -- this will result in something
|
|
839 seemingly unrelated getting trashed, and is extremely
|
|
840 difficult to track down. If the object gets freed but
|
|
841 not reallocated, we can usually catch this because we
|
|
842 set all bytes of a freed object to 0xDEADBEEF. (The
|
|
843 first four bytes, however, are 0xFFFFFFFF, and the next
|
|
844 four are a pointer used to chain freed objects together;
|
|
845 we play some tricks with this pointer to make it more
|
|
846 bogus, so crashes are more likely to occur right away.)
|
|
847
|
|
848 We want freed objects to stay free as long as possible,
|
|
849 so instead of doing what we do above, we maintain the
|
|
850 free objects in a first-in first-out queue. We also
|
|
851 don't recompute the free list each GC, unlike above;
|
|
852 this ensures that the queue ordering is preserved.
|
|
853 [This means that we are likely to have worse locality
|
|
854 of reference, and that we can never free a frob block
|
|
855 once it's allocated. (Even if we know that all cells
|
|
856 in it are free, there's no easy way to remove all those
|
|
857 cells from the free list because the objects on the
|
|
858 free list are unlikely to be in memory order.)]
|
|
859 Furthermore, we never take objects off the free list
|
|
860 unless there's a large number (usually 1000, but
|
|
861 varies depending on type) of them already on the list.
|
|
862 This way, we ensure that an object that gets freed will
|
|
863 remain free for the next 1000 (or whatever) times that
|
|
864 an object of that type is allocated.
|
|
865 */
|
|
866
|
|
867 #ifndef MALLOC_OVERHEAD
|
|
868 #ifdef GNU_MALLOC
|
|
869 #define MALLOC_OVERHEAD 0
|
|
870 #elif defined (rcheck)
|
|
871 #define MALLOC_OVERHEAD 20
|
|
872 #else
|
|
873 #define MALLOC_OVERHEAD 8
|
|
874 #endif
|
183
|
875 #endif /* MALLOC_OVERHEAD */
|
0
|
876
|
255
|
877 #if !defined(HAVE_MMAP) || defined(DOUG_LEA_MALLOC)
|
|
878 /* If we released our reserve (due to running out of memory),
|
|
879 and we have a fair amount free once again,
|
|
880 try to set aside another reserve in case we run out once more.
|
|
881
|
|
882 This is called when a relocatable block is freed in ralloc.c. */
|
272
|
883 void refill_memory_reserve (void);
|
255
|
884 void
|
|
885 refill_memory_reserve ()
|
|
886 {
|
|
887 if (breathing_space == 0)
|
|
888 breathing_space = (char *) malloc (4096 - MALLOC_OVERHEAD);
|
|
889 }
|
|
890 #endif
|
|
891
|
0
|
892 #ifdef ALLOC_NO_POOLS
|
|
893 # define TYPE_ALLOC_SIZE(type, structtype) 1
|
|
894 #else
|
|
895 # define TYPE_ALLOC_SIZE(type, structtype) \
|
|
896 ((2048 - MALLOC_OVERHEAD - sizeof (struct type##_block *)) \
|
|
897 / sizeof (structtype))
|
183
|
898 #endif /* ALLOC_NO_POOLS */
|
0
|
899
|
|
900 #define DECLARE_FIXED_TYPE_ALLOC(type, structtype) \
|
|
901 \
|
|
902 struct type##_block \
|
|
903 { \
|
|
904 struct type##_block *prev; \
|
|
905 structtype block[TYPE_ALLOC_SIZE (type, structtype)]; \
|
|
906 }; \
|
|
907 \
|
|
908 static struct type##_block *current_##type##_block; \
|
|
909 static int current_##type##_block_index; \
|
|
910 \
|
|
911 static structtype *type##_free_list; \
|
|
912 static structtype *type##_free_list_tail; \
|
|
913 \
|
|
914 static void \
|
|
915 init_##type##_alloc (void) \
|
|
916 { \
|
|
917 current_##type##_block = 0; \
|
|
918 current_##type##_block_index = countof (current_##type##_block->block); \
|
|
919 type##_free_list = 0; \
|
|
920 type##_free_list_tail = 0; \
|
|
921 } \
|
|
922 \
|
|
923 static int gc_count_num_##type##_in_use, gc_count_num_##type##_freelist
|
|
924
|
|
925 #define ALLOCATE_FIXED_TYPE_FROM_BLOCK(type, result) \
|
|
926 do { \
|
|
927 if (current_##type##_block_index \
|
|
928 == countof (current_##type##_block->block)) \
|
|
929 { \
|
185
|
930 struct type##_block *__new__ = (struct type##_block *) \
|
|
931 allocate_lisp_storage (sizeof (struct type##_block)); \
|
0
|
932 __new__->prev = current_##type##_block; \
|
|
933 current_##type##_block = __new__; \
|
|
934 current_##type##_block_index = 0; \
|
|
935 } \
|
|
936 (result) = \
|
|
937 &(current_##type##_block->block[current_##type##_block_index++]); \
|
|
938 } while (0)
|
|
939
|
|
940 /* Allocate an instance of a type that is stored in blocks.
|
|
941 TYPE is the "name" of the type, STRUCTTYPE is the corresponding
|
|
942 structure type. */
|
|
943
|
|
944 #ifdef ERROR_CHECK_GC
|
|
945
|
|
946 /* Note: if you get crashes in this function, suspect incorrect calls
|
|
947 to free_cons() and friends. This happened once because the cons
|
|
948 cell was not GC-protected and was getting collected before
|
|
949 free_cons() was called. */
|
|
950
|
|
951 #define ALLOCATE_FIXED_TYPE_1(type, structtype, result) \
|
|
952 do \
|
|
953 { \
|
|
954 if (gc_count_num_##type##_freelist > \
|
|
955 MINIMUM_ALLOWED_FIXED_TYPE_CELLS_##type) \
|
|
956 { \
|
|
957 result = type##_free_list; \
|
|
958 /* Before actually using the chain pointer, we complement all its \
|
|
959 bits; see FREE_FIXED_TYPE(). */ \
|
|
960 type##_free_list = \
|
|
961 (structtype *) ~(unsigned long) \
|
|
962 (* (structtype **) ((char *) result + sizeof (void *))); \
|
|
963 gc_count_num_##type##_freelist--; \
|
|
964 } \
|
|
965 else \
|
|
966 ALLOCATE_FIXED_TYPE_FROM_BLOCK (type, result); \
|
|
967 MARK_STRUCT_AS_NOT_FREE (result); \
|
|
968 } while (0)
|
|
969
|
183
|
970 #else /* !ERROR_CHECK_GC */
|
0
|
971
|
|
972 #define ALLOCATE_FIXED_TYPE_1(type, structtype, result) \
|
|
973 do \
|
|
974 { \
|
|
975 if (type##_free_list) \
|
|
976 { \
|
|
977 result = type##_free_list; \
|
|
978 type##_free_list = \
|
|
979 * (structtype **) ((char *) result + sizeof (void *)); \
|
|
980 } \
|
|
981 else \
|
|
982 ALLOCATE_FIXED_TYPE_FROM_BLOCK (type, result); \
|
|
983 MARK_STRUCT_AS_NOT_FREE (result); \
|
|
984 } while (0)
|
|
985
|
183
|
986 #endif /* !ERROR_CHECK_GC */
|
0
|
987
|
|
988 #define ALLOCATE_FIXED_TYPE(type, structtype, result) \
|
|
989 do \
|
|
990 { \
|
|
991 ALLOCATE_FIXED_TYPE_1 (type, structtype, result); \
|
|
992 INCREMENT_CONS_COUNTER (sizeof (structtype), #type); \
|
|
993 } while (0)
|
|
994
|
|
995 #define NOSEEUM_ALLOCATE_FIXED_TYPE(type, structtype, result) \
|
|
996 do \
|
|
997 { \
|
|
998 ALLOCATE_FIXED_TYPE_1 (type, structtype, result); \
|
|
999 NOSEEUM_INCREMENT_CONS_COUNTER (sizeof (structtype), #type); \
|
|
1000 } while (0)
|
|
1001
|
|
1002 /* INVALID_POINTER_VALUE should be a value that is invalid as a pointer
|
|
1003 to a Lisp object and invalid as an actual Lisp_Object value. We have
|
|
1004 to make sure that this value cannot be an integer in Lisp_Object form.
|
|
1005 0xFFFFFFFF could be so on a 64-bit system, so we extend it to 64 bits.
|
|
1006 On a 32-bit system, the type bits will be non-zero, making the value
|
|
1007 be a pointer, and the pointer will be misaligned.
|
|
1008
|
|
1009 Even if Emacs is run on some weirdo system that allows and allocates
|
|
1010 byte-aligned pointers, this pointer is at the very top of the address
|
|
1011 space and so it's almost inconceivable that it could ever be valid. */
|
183
|
1012
|
0
|
1013 #if INTBITS == 32
|
|
1014 # define INVALID_POINTER_VALUE 0xFFFFFFFF
|
|
1015 #elif INTBITS == 48
|
|
1016 # define INVALID_POINTER_VALUE 0xFFFFFFFFFFFF
|
|
1017 #elif INTBITS == 64
|
|
1018 # define INVALID_POINTER_VALUE 0xFFFFFFFFFFFFFFFF
|
|
1019 #else
|
|
1020 You have some weird system and need to supply a reasonable value here.
|
|
1021 #endif
|
|
1022
|
|
1023 #define FREE_STRUCT_P(ptr) \
|
|
1024 (* (void **) ptr == (void *) INVALID_POINTER_VALUE)
|
|
1025 #define MARK_STRUCT_AS_FREE(ptr) \
|
|
1026 (* (void **) ptr = (void *) INVALID_POINTER_VALUE)
|
|
1027 #define MARK_STRUCT_AS_NOT_FREE(ptr) \
|
|
1028 (* (void **) ptr = 0)
|
|
1029
|
|
1030 #ifdef ERROR_CHECK_GC
|
|
1031
|
|
1032 #define PUT_FIXED_TYPE_ON_FREE_LIST(type, structtype, ptr) \
|
|
1033 do { if (type##_free_list_tail) \
|
|
1034 { \
|
|
1035 /* When we store the chain pointer, we complement all \
|
|
1036 its bits; this should significantly increase its \
|
|
1037 bogosity in case someone tries to use the value, and \
|
|
1038 should make us dump faster if someone stores something \
|
|
1039 over the pointer because when it gets un-complemented in \
|
|
1040 ALLOCATED_FIXED_TYPE(), the resulting pointer will be \
|
|
1041 extremely bogus. */ \
|
|
1042 * (structtype **) \
|
|
1043 ((char *) type##_free_list_tail + sizeof (void *)) = \
|
|
1044 (structtype *) ~(unsigned long) ptr; \
|
|
1045 } \
|
|
1046 else \
|
|
1047 type##_free_list = ptr; \
|
|
1048 type##_free_list_tail = ptr; \
|
|
1049 } while (0)
|
|
1050
|
183
|
1051 #else /* !ERROR_CHECK_GC */
|
0
|
1052
|
|
1053 #define PUT_FIXED_TYPE_ON_FREE_LIST(type, structtype, ptr) \
|
|
1054 do { * (structtype **) ((char *) ptr + sizeof (void *)) = \
|
|
1055 type##_free_list; \
|
|
1056 type##_free_list = ptr; \
|
|
1057 } while (0)
|
|
1058
|
183
|
1059 #endif /* !ERROR_CHECK_GC */
|
0
|
1060
|
|
1061 /* TYPE and STRUCTTYPE are the same as in ALLOCATE_FIXED_TYPE(). */
|
|
1062
|
|
1063 #define FREE_FIXED_TYPE(type, structtype, ptr) \
|
|
1064 do { structtype *_weird_ = (ptr); \
|
|
1065 ADDITIONAL_FREE_##type (_weird_); \
|
|
1066 deadbeef_memory (ptr, sizeof (structtype)); \
|
|
1067 PUT_FIXED_TYPE_ON_FREE_LIST (type, structtype, ptr); \
|
|
1068 MARK_STRUCT_AS_FREE (_weird_); \
|
|
1069 } while (0)
|
|
1070
|
|
1071 /* Like FREE_FIXED_TYPE() but used when we are explicitly
|
|
1072 freeing a structure through free_cons(), free_marker(), etc.
|
|
1073 rather than through the normal process of sweeping.
|
|
1074 We attempt to undo the changes made to the allocation counters
|
|
1075 as a result of this structure being allocated. This is not
|
|
1076 completely necessary but helps keep things saner: e.g. this way,
|
|
1077 repeatedly allocating and freeing a cons will not result in
|
|
1078 the consing-since-gc counter advancing, which would cause a GC
|
|
1079 and somewhat defeat the purpose of explicitly freeing. */
|
|
1080
|
|
1081 #define FREE_FIXED_TYPE_WHEN_NOT_IN_GC(type, structtype, ptr) \
|
|
1082 do { FREE_FIXED_TYPE (type, structtype, ptr); \
|
|
1083 DECREMENT_CONS_COUNTER (sizeof (structtype)); \
|
|
1084 gc_count_num_##type##_freelist++; \
|
|
1085 } while (0)
|
183
|
1086
|
0
|
1087
|
|
1088
|
|
1089 /**********************************************************************/
|
|
1090 /* Cons allocation */
|
|
1091 /**********************************************************************/
|
|
1092
|
|
1093 DECLARE_FIXED_TYPE_ALLOC (cons, struct Lisp_Cons);
|
|
1094 /* conses are used and freed so often that we set this really high */
|
|
1095 /* #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_cons 20000 */
|
|
1096 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_cons 2000
|
|
1097
|
207
|
1098 #ifdef LRECORD_CONS
|
|
1099 static Lisp_Object
|
|
1100 mark_cons (Lisp_Object obj, void (*markobj) (Lisp_Object))
|
|
1101 {
|
|
1102 if (NILP (XCDR (obj)))
|
|
1103 return XCAR (obj);
|
272
|
1104
|
|
1105 (markobj) (XCAR (obj));
|
207
|
1106 return XCDR (obj);
|
|
1107 }
|
|
1108
|
|
1109 static int
|
|
1110 cons_equal (Lisp_Object ob1, Lisp_Object ob2, int depth)
|
|
1111 {
|
|
1112 while (internal_equal (XCAR (ob1), XCAR (ob2), depth + 1))
|
|
1113 {
|
272
|
1114 ob1 = XCDR (ob1);
|
|
1115 ob2 = XCDR (ob2);
|
207
|
1116 if (! CONSP (ob1) || ! CONSP (ob2))
|
|
1117 return internal_equal (ob1, ob2, depth + 1);
|
|
1118 }
|
|
1119 return 0;
|
|
1120 }
|
243
|
1121
|
|
1122 DEFINE_BASIC_LRECORD_IMPLEMENTATION ("cons", cons,
|
|
1123 mark_cons, print_cons, 0,
|
|
1124 cons_equal,
|
|
1125 /*
|
|
1126 * No `hash' method needed.
|
|
1127 * internal_hash knows how to
|
|
1128 * handle conses.
|
|
1129 */
|
|
1130 0,
|
|
1131 struct Lisp_Cons);
|
207
|
1132 #endif /* LRECORD_CONS */
|
|
1133
|
20
|
1134 DEFUN ("cons", Fcons, 2, 2, 0, /*
|
0
|
1135 Create a new cons, give it CAR and CDR as components, and return it.
|
20
|
1136 */
|
|
1137 (car, cdr))
|
0
|
1138 {
|
|
1139 /* This cannot GC. */
|
272
|
1140 Lisp_Object val;
|
0
|
1141 struct Lisp_Cons *c;
|
|
1142
|
|
1143 ALLOCATE_FIXED_TYPE (cons, struct Lisp_Cons, c);
|
207
|
1144 #ifdef LRECORD_CONS
|
|
1145 set_lheader_implementation (&(c->lheader), lrecord_cons);
|
|
1146 #endif
|
0
|
1147 XSETCONS (val, c);
|
207
|
1148 c->car = car;
|
|
1149 c->cdr = cdr;
|
0
|
1150 return val;
|
|
1151 }
|
|
1152
|
|
1153 /* This is identical to Fcons() but it used for conses that we're
|
|
1154 going to free later, and is useful when trying to track down
|
|
1155 "real" consing. */
|
|
1156 Lisp_Object
|
|
1157 noseeum_cons (Lisp_Object car, Lisp_Object cdr)
|
|
1158 {
|
272
|
1159 Lisp_Object val;
|
0
|
1160 struct Lisp_Cons *c;
|
|
1161
|
|
1162 NOSEEUM_ALLOCATE_FIXED_TYPE (cons, struct Lisp_Cons, c);
|
207
|
1163 #ifdef LRECORD_CONS
|
|
1164 set_lheader_implementation (&(c->lheader), lrecord_cons);
|
|
1165 #endif
|
0
|
1166 XSETCONS (val, c);
|
|
1167 XCAR (val) = car;
|
|
1168 XCDR (val) = cdr;
|
|
1169 return val;
|
|
1170 }
|
|
1171
|
20
|
1172 DEFUN ("list", Flist, 0, MANY, 0, /*
|
0
|
1173 Return a newly created list with specified arguments as elements.
|
|
1174 Any number of arguments, even zero arguments, are allowed.
|
20
|
1175 */
|
|
1176 (int nargs, Lisp_Object *args))
|
0
|
1177 {
|
165
|
1178 Lisp_Object val = Qnil;
|
|
1179 Lisp_Object *argp = args + nargs;
|
|
1180
|
|
1181 while (nargs-- > 0)
|
|
1182 val = Fcons (*--argp, val);
|
0
|
1183 return val;
|
|
1184 }
|
|
1185
|
|
1186 Lisp_Object
|
|
1187 list1 (Lisp_Object obj0)
|
|
1188 {
|
|
1189 /* This cannot GC. */
|
173
|
1190 return Fcons (obj0, Qnil);
|
0
|
1191 }
|
|
1192
|
|
1193 Lisp_Object
|
|
1194 list2 (Lisp_Object obj0, Lisp_Object obj1)
|
|
1195 {
|
|
1196 /* This cannot GC. */
|
272
|
1197 return Fcons (obj0, Fcons (obj1, Qnil));
|
0
|
1198 }
|
|
1199
|
|
1200 Lisp_Object
|
|
1201 list3 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2)
|
|
1202 {
|
|
1203 /* This cannot GC. */
|
272
|
1204 return Fcons (obj0, Fcons (obj1, Fcons (obj2, Qnil)));
|
0
|
1205 }
|
|
1206
|
272
|
1207 Lisp_Object
|
0
|
1208 cons3 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2)
|
|
1209 {
|
|
1210 /* This cannot GC. */
|
|
1211 return Fcons (obj0, Fcons (obj1, obj2));
|
|
1212 }
|
|
1213
|
|
1214 Lisp_Object
|
272
|
1215 acons (Lisp_Object key, Lisp_Object value, Lisp_Object alist)
|
|
1216 {
|
|
1217 return Fcons (Fcons (key, value), alist);
|
|
1218 }
|
|
1219
|
|
1220 Lisp_Object
|
0
|
1221 list4 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2, Lisp_Object obj3)
|
|
1222 {
|
|
1223 /* This cannot GC. */
|
272
|
1224 return Fcons (obj0, Fcons (obj1, Fcons (obj2, Fcons (obj3, Qnil))));
|
0
|
1225 }
|
|
1226
|
|
1227 Lisp_Object
|
|
1228 list5 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2, Lisp_Object obj3,
|
|
1229 Lisp_Object obj4)
|
|
1230 {
|
|
1231 /* This cannot GC. */
|
272
|
1232 return Fcons (obj0, Fcons (obj1, Fcons (obj2, Fcons (obj3, Fcons (obj4, Qnil)))));
|
0
|
1233 }
|
|
1234
|
|
1235 Lisp_Object
|
|
1236 list6 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2, Lisp_Object obj3,
|
|
1237 Lisp_Object obj4, Lisp_Object obj5)
|
|
1238 {
|
|
1239 /* This cannot GC. */
|
272
|
1240 return Fcons (obj0, Fcons (obj1, Fcons (obj2, Fcons (obj3, Fcons (obj4, Fcons (obj5, Qnil))))));
|
0
|
1241 }
|
|
1242
|
20
|
1243 DEFUN ("make-list", Fmake_list, 2, 2, 0, /*
|
272
|
1244 Return a new list of length LENGTH, with each element being INIT.
|
20
|
1245 */
|
|
1246 (length, init))
|
0
|
1247 {
|
|
1248 CHECK_NATNUM (length);
|
272
|
1249
|
|
1250 {
|
|
1251 Lisp_Object val = Qnil;
|
|
1252 int size = XINT (length);
|
|
1253
|
|
1254 while (size-- > 0)
|
|
1255 val = Fcons (init, val);
|
|
1256 return val;
|
|
1257 }
|
0
|
1258 }
|
|
1259
|
|
1260
|
|
1261 /**********************************************************************/
|
|
1262 /* Float allocation */
|
|
1263 /**********************************************************************/
|
|
1264
|
|
1265 #ifdef LISP_FLOAT_TYPE
|
|
1266
|
|
1267 DECLARE_FIXED_TYPE_ALLOC (float, struct Lisp_Float);
|
|
1268 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_float 1000
|
|
1269
|
|
1270 Lisp_Object
|
|
1271 make_float (double float_value)
|
|
1272 {
|
|
1273 Lisp_Object val;
|
|
1274 struct Lisp_Float *f;
|
|
1275
|
|
1276 ALLOCATE_FIXED_TYPE (float, struct Lisp_Float, f);
|
211
|
1277 set_lheader_implementation (&(f->lheader), lrecord_float);
|
0
|
1278 float_next (f) = ((struct Lisp_Float *) -1);
|
|
1279 float_data (f) = float_value;
|
|
1280 XSETFLOAT (val, f);
|
173
|
1281 return val;
|
0
|
1282 }
|
|
1283
|
|
1284 #endif /* LISP_FLOAT_TYPE */
|
|
1285
|
|
1286
|
|
1287 /**********************************************************************/
|
|
1288 /* Vector allocation */
|
|
1289 /**********************************************************************/
|
|
1290
|
207
|
1291 #ifdef LRECORD_VECTOR
|
|
1292 static Lisp_Object
|
|
1293 mark_vector (Lisp_Object obj, void (*markobj) (Lisp_Object))
|
|
1294 {
|
|
1295 struct Lisp_Vector *ptr = XVECTOR (obj);
|
|
1296 int len = vector_length (ptr);
|
|
1297 int i;
|
|
1298
|
|
1299 for (i = 0; i < len - 1; i++)
|
|
1300 (markobj) (ptr->contents[i]);
|
|
1301 return (len > 0) ? ptr->contents[len - 1] : Qnil;
|
|
1302 }
|
|
1303
|
272
|
1304 static size_t
|
207
|
1305 size_vector (CONST void *lheader)
|
|
1306 {
|
272
|
1307 /* * -1 because struct Lisp_Vector includes 1 slot */
|
207
|
1308 return sizeof (struct Lisp_Vector) +
|
272
|
1309 ((((struct Lisp_Vector *) lheader)->size - 1) * sizeof (Lisp_Object));
|
207
|
1310 }
|
|
1311
|
|
1312 static int
|
|
1313 vector_equal (Lisp_Object o1, Lisp_Object o2, int depth)
|
|
1314 {
|
|
1315 int indice;
|
|
1316 int len = XVECTOR_LENGTH (o1);
|
|
1317 if (len != XVECTOR_LENGTH (o2))
|
|
1318 return 0;
|
|
1319 for (indice = 0; indice < len; indice++)
|
|
1320 {
|
|
1321 if (!internal_equal (XVECTOR_DATA (o1) [indice],
|
|
1322 XVECTOR_DATA (o2) [indice],
|
|
1323 depth + 1))
|
|
1324 return 0;
|
|
1325 }
|
|
1326 return 1;
|
|
1327 }
|
|
1328
|
243
|
1329 DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION("vector", vector,
|
|
1330 mark_vector, print_vector, 0,
|
|
1331 vector_equal,
|
|
1332 /*
|
|
1333 * No `hash' method needed for
|
|
1334 * vectors. internal_hash
|
|
1335 * knows how to handle vectors.
|
|
1336 */
|
|
1337 0,
|
|
1338 size_vector, struct Lisp_Vector);
|
|
1339
|
207
|
1340 /* #### should allocate `small' vectors from a frob-block */
|
|
1341 static struct Lisp_Vector *
|
272
|
1342 make_vector_internal (size_t sizei)
|
207
|
1343 {
|
272
|
1344 size_t sizem = (sizeof (struct Lisp_Vector)
|
|
1345 /* -1 because struct Lisp_Vector includes 1 slot */
|
|
1346 + (sizei - 1) * sizeof (Lisp_Object));
|
|
1347 struct Lisp_Vector *p =
|
|
1348 (struct Lisp_Vector *) alloc_lcrecord (sizem, lrecord_vector);
|
207
|
1349
|
|
1350 p->size = sizei;
|
|
1351 return p;
|
|
1352 }
|
|
1353
|
|
1354 #else /* ! LRECORD_VECTOR */
|
|
1355
|
0
|
1356 static Lisp_Object all_vectors;
|
|
1357
|
|
1358 /* #### should allocate `small' vectors from a frob-block */
|
|
1359 static struct Lisp_Vector *
|
272
|
1360 make_vector_internal (size_t sizei)
|
0
|
1361 {
|
272
|
1362 size_t sizem = (sizeof (struct Lisp_Vector)
|
|
1363 /* -1 because struct Lisp_Vector includes 1 slot,
|
|
1364 * +1 to account for vector_next */
|
|
1365 + (sizei - 1 + 1) * sizeof (Lisp_Object));
|
185
|
1366 struct Lisp_Vector *p = (struct Lisp_Vector *) allocate_lisp_storage (sizem);
|
0
|
1367
|
|
1368 INCREMENT_CONS_COUNTER (sizem, "vector");
|
|
1369
|
|
1370 p->size = sizei;
|
|
1371 vector_next (p) = all_vectors;
|
|
1372 XSETVECTOR (all_vectors, p);
|
173
|
1373 return p;
|
0
|
1374 }
|
|
1375
|
243
|
1376 #endif /* ! LRECORD_VECTOR */
|
207
|
1377
|
0
|
1378 Lisp_Object
|
|
1379 make_vector (EMACS_INT length, Lisp_Object init)
|
|
1380 {
|
272
|
1381 int elt;
|
|
1382 Lisp_Object vector;
|
0
|
1383 struct Lisp_Vector *p;
|
|
1384
|
|
1385 if (length < 0)
|
|
1386 length = XINT (wrong_type_argument (Qnatnump, make_int (length)));
|
|
1387
|
|
1388 p = make_vector_internal (length);
|
|
1389 XSETVECTOR (vector, p);
|
|
1390
|
|
1391 #if 0
|
|
1392 /* Initialize big arrays full of 0's quickly, for what that's worth */
|
|
1393 {
|
|
1394 char *travesty = (char *) &init;
|
|
1395 for (i = 1; i < sizeof (Lisp_Object); i++)
|
|
1396 {
|
|
1397 if (travesty[i] != travesty[0])
|
|
1398 goto fill;
|
|
1399 }
|
|
1400 memset (vector_data (p), travesty[0], length * sizeof (Lisp_Object));
|
173
|
1401 return vector;
|
0
|
1402 }
|
|
1403 fill:
|
|
1404 #endif
|
|
1405 for (elt = 0; elt < length; elt++)
|
|
1406 vector_data(p)[elt] = init;
|
|
1407
|
173
|
1408 return vector;
|
0
|
1409 }
|
|
1410
|
20
|
1411 DEFUN ("make-vector", Fmake_vector, 2, 2, 0, /*
|
272
|
1412 Return a new vector of length LENGTH, with each element being INIT.
|
0
|
1413 See also the function `vector'.
|
20
|
1414 */
|
|
1415 (length, init))
|
0
|
1416 {
|
272
|
1417 CHECK_NATNUM (length);
|
173
|
1418 return make_vector (XINT (length), init);
|
0
|
1419 }
|
|
1420
|
20
|
1421 DEFUN ("vector", Fvector, 0, MANY, 0, /*
|
0
|
1422 Return a newly created vector with specified arguments as elements.
|
|
1423 Any number of arguments, even zero arguments, are allowed.
|
20
|
1424 */
|
|
1425 (int nargs, Lisp_Object *args))
|
0
|
1426 {
|
272
|
1427 Lisp_Object vector;
|
0
|
1428 int elt;
|
272
|
1429 struct Lisp_Vector *p = make_vector_internal (nargs);
|
0
|
1430
|
|
1431 for (elt = 0; elt < nargs; elt++)
|
|
1432 vector_data(p)[elt] = args[elt];
|
|
1433
|
272
|
1434 XSETVECTOR (vector, p);
|
173
|
1435 return vector;
|
0
|
1436 }
|
|
1437
|
|
1438 Lisp_Object
|
|
1439 vector1 (Lisp_Object obj0)
|
|
1440 {
|
|
1441 return Fvector (1, &obj0);
|
|
1442 }
|
|
1443
|
|
1444 Lisp_Object
|
|
1445 vector2 (Lisp_Object obj0, Lisp_Object obj1)
|
|
1446 {
|
|
1447 Lisp_Object args[2];
|
|
1448 args[0] = obj0;
|
|
1449 args[1] = obj1;
|
|
1450 return Fvector (2, args);
|
|
1451 }
|
|
1452
|
|
1453 Lisp_Object
|
|
1454 vector3 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2)
|
|
1455 {
|
|
1456 Lisp_Object args[3];
|
|
1457 args[0] = obj0;
|
|
1458 args[1] = obj1;
|
|
1459 args[2] = obj2;
|
|
1460 return Fvector (3, args);
|
|
1461 }
|
|
1462
|
272
|
1463 #if 0 /* currently unused */
|
|
1464
|
0
|
1465 Lisp_Object
|
|
1466 vector4 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2,
|
|
1467 Lisp_Object obj3)
|
|
1468 {
|
|
1469 Lisp_Object args[4];
|
|
1470 args[0] = obj0;
|
|
1471 args[1] = obj1;
|
|
1472 args[2] = obj2;
|
|
1473 args[3] = obj3;
|
|
1474 return Fvector (4, args);
|
|
1475 }
|
|
1476
|
|
1477 Lisp_Object
|
|
1478 vector5 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2,
|
|
1479 Lisp_Object obj3, Lisp_Object obj4)
|
|
1480 {
|
|
1481 Lisp_Object args[5];
|
|
1482 args[0] = obj0;
|
|
1483 args[1] = obj1;
|
|
1484 args[2] = obj2;
|
|
1485 args[3] = obj3;
|
|
1486 args[4] = obj4;
|
|
1487 return Fvector (5, args);
|
|
1488 }
|
|
1489
|
|
1490 Lisp_Object
|
|
1491 vector6 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2,
|
|
1492 Lisp_Object obj3, Lisp_Object obj4, Lisp_Object obj5)
|
|
1493 {
|
|
1494 Lisp_Object args[6];
|
|
1495 args[0] = obj0;
|
|
1496 args[1] = obj1;
|
|
1497 args[2] = obj2;
|
|
1498 args[3] = obj3;
|
|
1499 args[4] = obj4;
|
|
1500 args[5] = obj5;
|
|
1501 return Fvector (6, args);
|
|
1502 }
|
|
1503
|
|
1504 Lisp_Object
|
|
1505 vector7 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2,
|
|
1506 Lisp_Object obj3, Lisp_Object obj4, Lisp_Object obj5,
|
|
1507 Lisp_Object obj6)
|
|
1508 {
|
|
1509 Lisp_Object args[7];
|
|
1510 args[0] = obj0;
|
|
1511 args[1] = obj1;
|
|
1512 args[2] = obj2;
|
|
1513 args[3] = obj3;
|
|
1514 args[4] = obj4;
|
|
1515 args[5] = obj5;
|
|
1516 args[6] = obj6;
|
|
1517 return Fvector (7, args);
|
|
1518 }
|
|
1519
|
|
1520 Lisp_Object
|
|
1521 vector8 (Lisp_Object obj0, Lisp_Object obj1, Lisp_Object obj2,
|
|
1522 Lisp_Object obj3, Lisp_Object obj4, Lisp_Object obj5,
|
|
1523 Lisp_Object obj6, Lisp_Object obj7)
|
|
1524 {
|
|
1525 Lisp_Object args[8];
|
|
1526 args[0] = obj0;
|
|
1527 args[1] = obj1;
|
|
1528 args[2] = obj2;
|
|
1529 args[3] = obj3;
|
|
1530 args[4] = obj4;
|
|
1531 args[5] = obj5;
|
|
1532 args[6] = obj6;
|
|
1533 args[7] = obj7;
|
|
1534 return Fvector (8, args);
|
|
1535 }
|
272
|
1536 #endif /* unused */
|
0
|
1537
|
|
1538 /**********************************************************************/
|
|
1539 /* Bit Vector allocation */
|
|
1540 /**********************************************************************/
|
|
1541
|
|
1542 static Lisp_Object all_bit_vectors;
|
|
1543
|
|
1544 /* #### should allocate `small' bit vectors from a frob-block */
|
|
1545 static struct Lisp_Bit_Vector *
|
272
|
1546 make_bit_vector_internal (size_t sizei)
|
0
|
1547 {
|
272
|
1548 size_t sizem = sizeof (struct Lisp_Bit_Vector) +
|
|
1549 /* -1 because struct Lisp_Bit_Vector includes 1 slot */
|
|
1550 sizeof (long) * (BIT_VECTOR_LONG_STORAGE (sizei) - 1);
|
185
|
1551 struct Lisp_Bit_Vector *p =
|
|
1552 (struct Lisp_Bit_Vector *) allocate_lisp_storage (sizem);
|
0
|
1553 set_lheader_implementation (&(p->lheader), lrecord_bit_vector);
|
|
1554
|
|
1555 INCREMENT_CONS_COUNTER (sizem, "bit-vector");
|
|
1556
|
|
1557 bit_vector_length (p) = sizei;
|
|
1558 bit_vector_next (p) = all_bit_vectors;
|
|
1559 /* make sure the extra bits in the last long are 0; the calling
|
|
1560 functions might not set them. */
|
|
1561 p->bits[BIT_VECTOR_LONG_STORAGE (sizei) - 1] = 0;
|
|
1562 XSETBIT_VECTOR (all_bit_vectors, p);
|
173
|
1563 return p;
|
0
|
1564 }
|
|
1565
|
|
1566 Lisp_Object
|
|
1567 make_bit_vector (EMACS_INT length, Lisp_Object init)
|
|
1568 {
|
272
|
1569 Lisp_Object bit_vector;
|
0
|
1570 struct Lisp_Bit_Vector *p;
|
|
1571 EMACS_INT num_longs;
|
|
1572
|
|
1573 CHECK_BIT (init);
|
|
1574
|
|
1575 num_longs = BIT_VECTOR_LONG_STORAGE (length);
|
|
1576 p = make_bit_vector_internal (length);
|
|
1577 XSETBIT_VECTOR (bit_vector, p);
|
|
1578
|
|
1579 if (ZEROP (init))
|
|
1580 memset (p->bits, 0, num_longs * sizeof (long));
|
|
1581 else
|
|
1582 {
|
|
1583 EMACS_INT bits_in_last = length & (LONGBITS_POWER_OF_2 - 1);
|
|
1584 memset (p->bits, ~0, num_longs * sizeof (long));
|
|
1585 /* But we have to make sure that the unused bits in the
|
|
1586 last integer are 0, so that equal/hash is easy. */
|
|
1587 if (bits_in_last)
|
|
1588 p->bits[num_longs - 1] &= (1 << bits_in_last) - 1;
|
|
1589 }
|
|
1590
|
173
|
1591 return bit_vector;
|
0
|
1592 }
|
|
1593
|
|
1594 Lisp_Object
|
|
1595 make_bit_vector_from_byte_vector (unsigned char *bytevec, EMACS_INT length)
|
|
1596 {
|
272
|
1597 Lisp_Object bit_vector;
|
0
|
1598 struct Lisp_Bit_Vector *p;
|
272
|
1599 int i;
|
0
|
1600
|
|
1601 if (length < 0)
|
|
1602 length = XINT (wrong_type_argument (Qnatnump, make_int (length)));
|
|
1603
|
|
1604 p = make_bit_vector_internal (length);
|
|
1605 XSETBIT_VECTOR (bit_vector, p);
|
|
1606
|
|
1607 for (i = 0; i < length; i++)
|
|
1608 set_bit_vector_bit (p, i, bytevec[i]);
|
|
1609
|
|
1610 return bit_vector;
|
|
1611 }
|
|
1612
|
20
|
1613 DEFUN ("make-bit-vector", Fmake_bit_vector, 2, 2, 0, /*
|
272
|
1614 Return a new bit vector of length LENGTH. with each bit being INIT.
|
0
|
1615 Each element is set to INIT. See also the function `bit-vector'.
|
20
|
1616 */
|
|
1617 (length, init))
|
0
|
1618 {
|
272
|
1619 CONCHECK_NATNUM (length);
|
0
|
1620
|
173
|
1621 return make_bit_vector (XINT (length), init);
|
0
|
1622 }
|
|
1623
|
20
|
1624 DEFUN ("bit-vector", Fbit_vector, 0, MANY, 0, /*
|
0
|
1625 Return a newly created bit vector with specified arguments as elements.
|
|
1626 Any number of arguments, even zero arguments, are allowed.
|
20
|
1627 */
|
|
1628 (int nargs, Lisp_Object *args))
|
0
|
1629 {
|
272
|
1630 Lisp_Object bit_vector;
|
0
|
1631 int elt;
|
|
1632 struct Lisp_Bit_Vector *p;
|
|
1633
|
|
1634 for (elt = 0; elt < nargs; elt++)
|
|
1635 CHECK_BIT (args[elt]);
|
|
1636
|
|
1637 p = make_bit_vector_internal (nargs);
|
|
1638
|
|
1639 for (elt = 0; elt < nargs; elt++)
|
|
1640 set_bit_vector_bit (p, elt, !ZEROP (args[elt]));
|
|
1641
|
272
|
1642 XSETBIT_VECTOR (bit_vector, p);
|
173
|
1643 return bit_vector;
|
0
|
1644 }
|
|
1645
|
|
1646
|
|
1647 /**********************************************************************/
|
|
1648 /* Compiled-function allocation */
|
|
1649 /**********************************************************************/
|
|
1650
|
|
1651 DECLARE_FIXED_TYPE_ALLOC (compiled_function, struct Lisp_Compiled_Function);
|
|
1652 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_compiled_function 1000
|
|
1653
|
|
1654 static Lisp_Object
|
|
1655 make_compiled_function (int make_pure)
|
|
1656 {
|
|
1657 struct Lisp_Compiled_Function *b;
|
|
1658 Lisp_Object new;
|
272
|
1659 size_t size = sizeof (struct Lisp_Compiled_Function);
|
0
|
1660
|
|
1661 if (make_pure && check_purespace (size))
|
|
1662 {
|
272
|
1663 b = (struct Lisp_Compiled_Function *) (PUREBEG + pure_bytes_used);
|
0
|
1664 set_lheader_implementation (&(b->lheader), lrecord_compiled_function);
|
211
|
1665 #ifdef USE_INDEXED_LRECORD_IMPLEMENTATION
|
|
1666 b->lheader.pure = 1;
|
|
1667 #endif
|
272
|
1668 pure_bytes_used += size;
|
0
|
1669 bump_purestat (&purestat_bytecode, size);
|
|
1670 }
|
|
1671 else
|
|
1672 {
|
|
1673 ALLOCATE_FIXED_TYPE (compiled_function, struct Lisp_Compiled_Function,
|
|
1674 b);
|
|
1675 set_lheader_implementation (&(b->lheader), lrecord_compiled_function);
|
|
1676 }
|
|
1677 b->maxdepth = 0;
|
|
1678 b->flags.documentationp = 0;
|
|
1679 b->flags.interactivep = 0;
|
|
1680 b->flags.domainp = 0; /* I18N3 */
|
|
1681 b->bytecodes = Qzero;
|
|
1682 b->constants = Qzero;
|
|
1683 b->arglist = Qnil;
|
|
1684 b->doc_and_interactive = Qnil;
|
|
1685 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK
|
|
1686 b->annotated = Qnil;
|
|
1687 #endif
|
|
1688 XSETCOMPILED_FUNCTION (new, b);
|
173
|
1689 return new;
|
0
|
1690 }
|
|
1691
|
20
|
1692 DEFUN ("make-byte-code", Fmake_byte_code, 4, MANY, 0, /*
|
272
|
1693 Return a new compiled-function object.
|
0
|
1694 Usage: (arglist instructions constants stack-size
|
|
1695 &optional doc-string interactive-spec)
|
|
1696 Note that, unlike all other emacs-lisp functions, calling this with five
|
|
1697 arguments is NOT the same as calling it with six arguments, the last of
|
|
1698 which is nil. If the INTERACTIVE arg is specified as nil, then that means
|
|
1699 that this function was defined with `(interactive)'. If the arg is not
|
|
1700 specified, then that means the function is not interactive.
|
|
1701 This is terrible behavior which is retained for compatibility with old
|
|
1702 `.elc' files which expected these semantics.
|
20
|
1703 */
|
|
1704 (int nargs, Lisp_Object *args))
|
0
|
1705 {
|
|
1706 /* In a non-insane world this function would have this arglist...
|
|
1707 (arglist, instructions, constants, stack_size, doc_string, interactive)
|
|
1708 Lisp_Object arglist, instructions, constants, stack_size, doc_string,
|
|
1709 interactive;
|
|
1710 */
|
|
1711 Lisp_Object arglist = args[0];
|
|
1712 Lisp_Object instructions = args[1];
|
|
1713 Lisp_Object constants = args[2];
|
|
1714 Lisp_Object stack_size = args[3];
|
272
|
1715 Lisp_Object doc_string = (nargs > 4) ? args[4] : Qnil;
|
|
1716 Lisp_Object interactive = (nargs > 5) ? args[5] : Qunbound;
|
0
|
1717 /* Don't purecopy the doc references in instructions because it's
|
|
1718 wasteful; they will get fixed up later.
|
|
1719
|
|
1720 #### If something goes wrong and they don't get fixed up,
|
|
1721 we're screwed, because pure stuff isn't marked and thus the
|
|
1722 cons references won't be marked and will get reused.
|
|
1723
|
|
1724 Note: there will be a window after the byte code is created and
|
|
1725 before the doc references are fixed up in which there will be
|
|
1726 impure objects inside a pure object, which apparently won't
|
|
1727 get marked, leading the trouble. But during that entire window,
|
|
1728 the objects are sitting on Vload_force_doc_string_list, which
|
|
1729 is staticpro'd, so we're OK. */
|
|
1730 int purecopy_instructions = 1;
|
|
1731
|
|
1732 if (nargs > 6)
|
183
|
1733 return Fsignal (Qwrong_number_of_arguments,
|
0
|
1734 list2 (intern ("make-byte-code"), make_int (nargs)));
|
|
1735
|
|
1736 CHECK_LIST (arglist);
|
|
1737 /* instructions is a string or a cons (string . int) for a
|
|
1738 lazy-loaded function. */
|
|
1739 if (CONSP (instructions))
|
|
1740 {
|
|
1741 CHECK_STRING (XCAR (instructions));
|
|
1742 CHECK_INT (XCDR (instructions));
|
|
1743 if (!NILP (constants))
|
|
1744 CHECK_VECTOR (constants);
|
|
1745 purecopy_instructions = 0;
|
|
1746 }
|
|
1747 else
|
|
1748 {
|
|
1749 CHECK_STRING (instructions);
|
|
1750 CHECK_VECTOR (constants);
|
|
1751 }
|
|
1752 CHECK_NATNUM (stack_size);
|
|
1753 /* doc_string may be nil, string, int, or a cons (string . int). */
|
|
1754
|
|
1755 /* interactive may be list or string (or unbound). */
|
|
1756
|
|
1757 if (purify_flag)
|
|
1758 {
|
|
1759 if (!purified (arglist))
|
|
1760 arglist = Fpurecopy (arglist);
|
|
1761 if (purecopy_instructions && !purified (instructions))
|
|
1762 instructions = Fpurecopy (instructions);
|
|
1763 if (!purified (doc_string))
|
|
1764 doc_string = Fpurecopy (doc_string);
|
|
1765 if (!purified (interactive) && !UNBOUNDP (interactive))
|
|
1766 interactive = Fpurecopy (interactive);
|
|
1767
|
|
1768 /* Statistics are kept differently for the constants */
|
|
1769 if (!purified (constants))
|
|
1770 #ifdef PURESTAT
|
|
1771 {
|
|
1772 int old = purecopying_for_bytecode;
|
|
1773 purecopying_for_bytecode = 1;
|
|
1774 constants = Fpurecopy (constants);
|
|
1775 purecopying_for_bytecode = old;
|
|
1776 }
|
|
1777 #else
|
|
1778 constants = Fpurecopy (constants);
|
|
1779 #endif /* PURESTAT */
|
|
1780
|
|
1781 #ifdef PURESTAT
|
|
1782 if (STRINGP (instructions))
|
|
1783 bump_purestat (&purestat_string_bytecodes, pure_sizeof (instructions));
|
|
1784 if (VECTORP (constants))
|
|
1785 bump_purestat (&purestat_vector_bytecode_constants,
|
|
1786 pure_sizeof (constants));
|
|
1787 if (STRINGP (doc_string))
|
|
1788 /* These should be have been snagged by make-docfile... */
|
|
1789 bump_purestat (&purestat_string_documentation,
|
|
1790 pure_sizeof (doc_string));
|
|
1791 if (STRINGP (interactive))
|
|
1792 bump_purestat (&purestat_string_interactive,
|
|
1793 pure_sizeof (interactive));
|
|
1794 #endif /* PURESTAT */
|
|
1795 }
|
183
|
1796
|
0
|
1797 {
|
|
1798 int docp = !NILP (doc_string);
|
|
1799 int intp = !UNBOUNDP (interactive);
|
|
1800 #ifdef I18N3
|
|
1801 int domp = !NILP (Vfile_domain);
|
|
1802 #endif
|
|
1803 Lisp_Object val = make_compiled_function (purify_flag);
|
|
1804 struct Lisp_Compiled_Function *b = XCOMPILED_FUNCTION (val);
|
|
1805 b->flags.documentationp = docp;
|
|
1806 b->flags.interactivep = intp;
|
|
1807 #ifdef I18N3
|
|
1808 b->flags.domainp = domp;
|
|
1809 #endif
|
|
1810 b->maxdepth = XINT (stack_size);
|
|
1811 b->bytecodes = instructions;
|
|
1812 b->constants = constants;
|
|
1813 b->arglist = arglist;
|
|
1814 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK
|
|
1815 if (!NILP (Vcurrent_compiled_function_annotation))
|
|
1816 b->annotated = Fpurecopy (Vcurrent_compiled_function_annotation);
|
|
1817 else if (!NILP (Vload_file_name_internal_the_purecopy))
|
|
1818 b->annotated = Vload_file_name_internal_the_purecopy;
|
|
1819 else if (!NILP (Vload_file_name_internal))
|
|
1820 {
|
177
|
1821 struct gcpro gcpro1;
|
|
1822 GCPRO1(val); /* don't let val or b get reaped */
|
0
|
1823 Vload_file_name_internal_the_purecopy =
|
|
1824 Fpurecopy (Ffile_name_nondirectory (Vload_file_name_internal));
|
|
1825 b->annotated = Vload_file_name_internal_the_purecopy;
|
177
|
1826 UNGCPRO;
|
0
|
1827 }
|
183
|
1828 #endif /* COMPILED_FUNCTION_ANNOTATION_HACK */
|
0
|
1829
|
|
1830 #ifdef I18N3
|
|
1831 if (docp && intp && domp)
|
|
1832 b->doc_and_interactive = (((purify_flag) ? pure_cons : Fcons)
|
|
1833 (doc_string,
|
|
1834 (((purify_flag) ? pure_cons : Fcons)
|
|
1835 (interactive, Vfile_domain))));
|
|
1836 else if (docp && domp)
|
|
1837 b->doc_and_interactive = (((purify_flag) ? pure_cons : Fcons)
|
|
1838 (doc_string, Vfile_domain));
|
|
1839 else if (intp && domp)
|
|
1840 b->doc_and_interactive = (((purify_flag) ? pure_cons : Fcons)
|
|
1841 (interactive, Vfile_domain));
|
|
1842 else
|
|
1843 #endif
|
|
1844 if (docp && intp)
|
|
1845 b->doc_and_interactive = (((purify_flag) ? pure_cons : Fcons)
|
|
1846 (doc_string, interactive));
|
|
1847 else if (intp)
|
|
1848 b->doc_and_interactive = interactive;
|
|
1849 #ifdef I18N3
|
|
1850 else if (domp)
|
|
1851 b->doc_and_interactive = Vfile_domain;
|
|
1852 #endif
|
|
1853 else
|
|
1854 b->doc_and_interactive = doc_string;
|
|
1855
|
173
|
1856 return val;
|
0
|
1857 }
|
|
1858 }
|
|
1859
|
|
1860
|
|
1861 /**********************************************************************/
|
|
1862 /* Symbol allocation */
|
|
1863 /**********************************************************************/
|
|
1864
|
|
1865 DECLARE_FIXED_TYPE_ALLOC (symbol, struct Lisp_Symbol);
|
|
1866 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_symbol 1000
|
|
1867
|
20
|
1868 DEFUN ("make-symbol", Fmake_symbol, 1, 1, 0, /*
|
0
|
1869 Return a newly allocated uninterned symbol whose name is NAME.
|
|
1870 Its value and function definition are void, and its property list is nil.
|
20
|
1871 */
|
|
1872 (str))
|
0
|
1873 {
|
|
1874 Lisp_Object val;
|
|
1875 struct Lisp_Symbol *p;
|
|
1876
|
|
1877 CHECK_STRING (str);
|
|
1878
|
|
1879 ALLOCATE_FIXED_TYPE (symbol, struct Lisp_Symbol, p);
|
|
1880 #ifdef LRECORD_SYMBOL
|
|
1881 set_lheader_implementation (&(p->lheader), lrecord_symbol);
|
|
1882 #endif
|
|
1883 p->name = XSTRING (str);
|
|
1884 p->plist = Qnil;
|
|
1885 p->value = Qunbound;
|
|
1886 p->function = Qunbound;
|
245
|
1887 p->obarray = Qnil;
|
0
|
1888 symbol_next (p) = 0;
|
|
1889 XSETSYMBOL (val, p);
|
|
1890 return val;
|
|
1891 }
|
|
1892
|
|
1893
|
|
1894 /**********************************************************************/
|
|
1895 /* Extent allocation */
|
|
1896 /**********************************************************************/
|
|
1897
|
|
1898 DECLARE_FIXED_TYPE_ALLOC (extent, struct extent);
|
|
1899 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_extent 1000
|
|
1900
|
|
1901 struct extent *
|
|
1902 allocate_extent (void)
|
|
1903 {
|
|
1904 struct extent *e;
|
|
1905
|
|
1906 ALLOCATE_FIXED_TYPE (extent, struct extent, e);
|
272
|
1907 /* xzero (*e); */
|
0
|
1908 set_lheader_implementation (&(e->lheader), lrecord_extent);
|
|
1909 extent_object (e) = Qnil;
|
|
1910 set_extent_start (e, -1);
|
|
1911 set_extent_end (e, -1);
|
|
1912 e->plist = Qnil;
|
|
1913
|
272
|
1914 xzero (e->flags);
|
0
|
1915
|
|
1916 extent_face (e) = Qnil;
|
|
1917 e->flags.end_open = 1; /* default is for endpoints to behave like markers */
|
|
1918 e->flags.detachable = 1;
|
|
1919
|
173
|
1920 return e;
|
0
|
1921 }
|
|
1922
|
|
1923
|
|
1924 /**********************************************************************/
|
|
1925 /* Event allocation */
|
|
1926 /**********************************************************************/
|
|
1927
|
|
1928 DECLARE_FIXED_TYPE_ALLOC (event, struct Lisp_Event);
|
|
1929 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_event 1000
|
|
1930
|
|
1931 Lisp_Object
|
|
1932 allocate_event (void)
|
|
1933 {
|
|
1934 Lisp_Object val;
|
|
1935 struct Lisp_Event *e;
|
|
1936
|
|
1937 ALLOCATE_FIXED_TYPE (event, struct Lisp_Event, e);
|
|
1938 set_lheader_implementation (&(e->lheader), lrecord_event);
|
|
1939
|
|
1940 XSETEVENT (val, e);
|
|
1941 return val;
|
|
1942 }
|
183
|
1943
|
0
|
1944
|
|
1945 /**********************************************************************/
|
|
1946 /* Marker allocation */
|
|
1947 /**********************************************************************/
|
|
1948
|
|
1949 DECLARE_FIXED_TYPE_ALLOC (marker, struct Lisp_Marker);
|
|
1950 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_marker 1000
|
|
1951
|
20
|
1952 DEFUN ("make-marker", Fmake_marker, 0, 0, 0, /*
|
272
|
1953 Return a new marker which does not point at any place.
|
20
|
1954 */
|
|
1955 ())
|
0
|
1956 {
|
|
1957 Lisp_Object val;
|
|
1958 struct Lisp_Marker *p;
|
|
1959
|
|
1960 ALLOCATE_FIXED_TYPE (marker, struct Lisp_Marker, p);
|
|
1961 set_lheader_implementation (&(p->lheader), lrecord_marker);
|
|
1962 p->buffer = 0;
|
|
1963 p->memind = 0;
|
|
1964 marker_next (p) = 0;
|
|
1965 marker_prev (p) = 0;
|
|
1966 p->insertion_type = 0;
|
|
1967 XSETMARKER (val, p);
|
|
1968 return val;
|
|
1969 }
|
|
1970
|
|
1971 Lisp_Object
|
|
1972 noseeum_make_marker (void)
|
|
1973 {
|
|
1974 Lisp_Object val;
|
|
1975 struct Lisp_Marker *p;
|
|
1976
|
|
1977 NOSEEUM_ALLOCATE_FIXED_TYPE (marker, struct Lisp_Marker, p);
|
|
1978 set_lheader_implementation (&(p->lheader), lrecord_marker);
|
|
1979 p->buffer = 0;
|
|
1980 p->memind = 0;
|
|
1981 marker_next (p) = 0;
|
|
1982 marker_prev (p) = 0;
|
|
1983 p->insertion_type = 0;
|
|
1984 XSETMARKER (val, p);
|
|
1985 return val;
|
|
1986 }
|
|
1987
|
|
1988
|
|
1989 /**********************************************************************/
|
|
1990 /* String allocation */
|
|
1991 /**********************************************************************/
|
|
1992
|
183
|
1993 /* The data for "short" strings generally resides inside of structs of type
|
|
1994 string_chars_block. The Lisp_String structure is allocated just like any
|
0
|
1995 other Lisp object (except for vectors), and these are freelisted when
|
|
1996 they get garbage collected. The data for short strings get compacted,
|
183
|
1997 but the data for large strings do not.
|
0
|
1998
|
|
1999 Previously Lisp_String structures were relocated, but this caused a lot
|
|
2000 of bus-errors because the C code didn't include enough GCPRO's for
|
|
2001 strings (since EVERY REFERENCE to a short string needed to be GCPRO'd so
|
|
2002 that the reference would get relocated).
|
|
2003
|
|
2004 This new method makes things somewhat bigger, but it is MUCH safer. */
|
|
2005
|
|
2006 DECLARE_FIXED_TYPE_ALLOC (string, struct Lisp_String);
|
|
2007 /* strings are used and freed quite often */
|
|
2008 /* #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_string 10000 */
|
|
2009 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_string 1000
|
|
2010
|
207
|
2011 #ifdef LRECORD_STRING
|
|
2012 static Lisp_Object
|
|
2013 mark_string (Lisp_Object obj, void (*markobj) (Lisp_Object))
|
|
2014 {
|
|
2015 struct Lisp_String *ptr = XSTRING (obj);
|
|
2016
|
|
2017 if (GC_CONSP (ptr->plist) && GC_EXTENT_INFOP (XCAR (ptr->plist)))
|
|
2018 flush_cached_extent_info (XCAR (ptr->plist));
|
|
2019 return ptr->plist;
|
|
2020 }
|
|
2021
|
|
2022 static int
|
|
2023 string_equal (Lisp_Object o1, Lisp_Object o2, int depth)
|
|
2024 {
|
272
|
2025 Bytecount len;
|
|
2026 return (((len = XSTRING_LENGTH (o1)) == XSTRING_LENGTH (o2)) &&
|
|
2027 !memcmp (XSTRING_DATA (o1), XSTRING_DATA (o2), len));
|
207
|
2028 }
|
243
|
2029
|
|
2030 DEFINE_BASIC_LRECORD_IMPLEMENTATION ("string", string,
|
|
2031 mark_string, print_string,
|
|
2032 /*
|
|
2033 * No `finalize', or `hash' methods.
|
|
2034 * internal_hash already knows how
|
|
2035 * to hash strings and finalization
|
|
2036 * is done with the
|
|
2037 * ADDITIONAL_FREE_string macro,
|
|
2038 * which is the standard way to do
|
|
2039 * finalization when using
|
|
2040 * SWEEP_FIXED_TYPE_BLOCK().
|
|
2041 */
|
|
2042 0, string_equal, 0,
|
|
2043 struct Lisp_String);
|
207
|
2044 #endif /* LRECORD_STRING */
|
|
2045
|
0
|
2046 /* String blocks contain this many useful bytes. */
|
272
|
2047 #define STRING_CHARS_BLOCK_SIZE \
|
|
2048 ((Bytecount) (8192 - MALLOC_OVERHEAD - \
|
|
2049 ((2 * sizeof (struct string_chars_block *)) \
|
|
2050 + sizeof (EMACS_INT))))
|
0
|
2051 /* Block header for small strings. */
|
|
2052 struct string_chars_block
|
|
2053 {
|
|
2054 EMACS_INT pos;
|
|
2055 struct string_chars_block *next;
|
|
2056 struct string_chars_block *prev;
|
|
2057 /* Contents of string_chars_block->string_chars are interleaved
|
|
2058 string_chars structures (see below) and the actual string data */
|
|
2059 unsigned char string_chars[STRING_CHARS_BLOCK_SIZE];
|
|
2060 };
|
|
2061
|
|
2062 struct string_chars_block *first_string_chars_block;
|
|
2063 struct string_chars_block *current_string_chars_block;
|
|
2064
|
|
2065 /* If SIZE is the length of a string, this returns how many bytes
|
|
2066 * the string occupies in string_chars_block->string_chars
|
|
2067 * (including alignment padding).
|
|
2068 */
|
|
2069 #define STRING_FULLSIZE(s) \
|
|
2070 ALIGN_SIZE (((s) + 1 + sizeof (struct Lisp_String *)),\
|
|
2071 ALIGNOF (struct Lisp_String *))
|
|
2072
|
|
2073 #define BIG_STRING_FULLSIZE_P(fullsize) ((fullsize) >= STRING_CHARS_BLOCK_SIZE)
|
|
2074 #define BIG_STRING_SIZE_P(size) (BIG_STRING_FULLSIZE_P (STRING_FULLSIZE(size)))
|
|
2075
|
|
2076 #define CHARS_TO_STRING_CHAR(x) \
|
|
2077 ((struct string_chars *) \
|
173
|
2078 (((char *) (x)) - (slot_offset (struct string_chars, chars[0]))))
|
0
|
2079
|
|
2080
|
|
2081 struct string_chars
|
|
2082 {
|
|
2083 struct Lisp_String *string;
|
|
2084 unsigned char chars[1];
|
|
2085 };
|
|
2086
|
|
2087 struct unused_string_chars
|
|
2088 {
|
|
2089 struct Lisp_String *string;
|
|
2090 EMACS_INT fullsize;
|
|
2091 };
|
|
2092
|
|
2093 static void
|
|
2094 init_string_chars_alloc (void)
|
|
2095 {
|
185
|
2096 first_string_chars_block = xnew (struct string_chars_block);
|
0
|
2097 first_string_chars_block->prev = 0;
|
|
2098 first_string_chars_block->next = 0;
|
|
2099 first_string_chars_block->pos = 0;
|
|
2100 current_string_chars_block = first_string_chars_block;
|
|
2101 }
|
|
2102
|
|
2103 static struct string_chars *
|
|
2104 allocate_string_chars_struct (struct Lisp_String *string_it_goes_with,
|
|
2105 EMACS_INT fullsize)
|
|
2106 {
|
|
2107 struct string_chars *s_chars;
|
|
2108
|
|
2109 /* Allocate the string's actual data */
|
|
2110 if (BIG_STRING_FULLSIZE_P (fullsize))
|
|
2111 {
|
|
2112 s_chars = (struct string_chars *) xmalloc (fullsize);
|
|
2113 }
|
|
2114 else if (fullsize <=
|
|
2115 (countof (current_string_chars_block->string_chars)
|
|
2116 - current_string_chars_block->pos))
|
|
2117 {
|
|
2118 /* This string can fit in the current string chars block */
|
|
2119 s_chars = (struct string_chars *)
|
|
2120 (current_string_chars_block->string_chars
|
|
2121 + current_string_chars_block->pos);
|
|
2122 current_string_chars_block->pos += fullsize;
|
|
2123 }
|
|
2124 else
|
|
2125 {
|
|
2126 /* Make a new current string chars block */
|
185
|
2127 struct string_chars_block *new = xnew (struct string_chars_block);
|
183
|
2128
|
0
|
2129 current_string_chars_block->next = new;
|
|
2130 new->prev = current_string_chars_block;
|
|
2131 new->next = 0;
|
|
2132 current_string_chars_block = new;
|
|
2133 new->pos = fullsize;
|
|
2134 s_chars = (struct string_chars *)
|
|
2135 current_string_chars_block->string_chars;
|
|
2136 }
|
|
2137
|
|
2138 s_chars->string = string_it_goes_with;
|
|
2139
|
|
2140 INCREMENT_CONS_COUNTER (fullsize, "string chars");
|
|
2141
|
|
2142 return s_chars;
|
|
2143 }
|
|
2144
|
|
2145 Lisp_Object
|
|
2146 make_uninit_string (Bytecount length)
|
|
2147 {
|
|
2148 struct Lisp_String *s;
|
|
2149 struct string_chars *s_chars;
|
|
2150 EMACS_INT fullsize = STRING_FULLSIZE (length);
|
|
2151 Lisp_Object val;
|
|
2152
|
|
2153 if ((length < 0) || (fullsize <= 0))
|
|
2154 abort ();
|
|
2155
|
|
2156 /* Allocate the string header */
|
|
2157 ALLOCATE_FIXED_TYPE (string, struct Lisp_String, s);
|
207
|
2158 #ifdef LRECORD_STRING
|
|
2159 set_lheader_implementation (&(s->lheader), lrecord_string);
|
|
2160 #endif
|
0
|
2161
|
|
2162 s_chars = allocate_string_chars_struct (s, fullsize);
|
|
2163
|
|
2164 set_string_data (s, &(s_chars->chars[0]));
|
|
2165 set_string_length (s, length);
|
|
2166 s->plist = Qnil;
|
183
|
2167
|
0
|
2168 set_string_byte (s, length, 0);
|
|
2169
|
|
2170 XSETSTRING (val, s);
|
173
|
2171 return val;
|
0
|
2172 }
|
|
2173
|
|
2174 #ifdef VERIFY_STRING_CHARS_INTEGRITY
|
|
2175 static void verify_string_chars_integrity (void);
|
|
2176 #endif
|
183
|
2177
|
0
|
2178 /* Resize the string S so that DELTA bytes can be inserted starting
|
|
2179 at POS. If DELTA < 0, it means deletion starting at POS. If
|
|
2180 POS < 0, resize the string but don't copy any characters. Use
|
|
2181 this if you're planning on completely overwriting the string.
|
|
2182 */
|
|
2183
|
|
2184 void
|
|
2185 resize_string (struct Lisp_String *s, Bytecount pos, Bytecount delta)
|
|
2186 {
|
|
2187 #ifdef VERIFY_STRING_CHARS_INTEGRITY
|
|
2188 verify_string_chars_integrity ();
|
|
2189 #endif
|
|
2190
|
|
2191 #ifdef ERROR_CHECK_BUFPOS
|
|
2192 if (pos >= 0)
|
|
2193 {
|
|
2194 assert (pos <= string_length (s));
|
|
2195 if (delta < 0)
|
|
2196 assert (pos + (-delta) <= string_length (s));
|
|
2197 }
|
|
2198 else
|
|
2199 {
|
|
2200 if (delta < 0)
|
|
2201 assert ((-delta) <= string_length (s));
|
|
2202 }
|
183
|
2203 #endif /* ERROR_CHECK_BUFPOS */
|
0
|
2204
|
|
2205 if (pos >= 0 && delta < 0)
|
|
2206 /* If DELTA < 0, the functions below will delete the characters
|
|
2207 before POS. We want to delete characters *after* POS, however,
|
|
2208 so convert this to the appropriate form. */
|
|
2209 pos += -delta;
|
|
2210
|
|
2211 if (delta == 0)
|
|
2212 /* simplest case: no size change. */
|
|
2213 return;
|
|
2214 else
|
|
2215 {
|
272
|
2216 Bytecount oldfullsize = STRING_FULLSIZE (string_length (s));
|
|
2217 Bytecount newfullsize = STRING_FULLSIZE (string_length (s) + delta);
|
0
|
2218
|
|
2219 if (oldfullsize == newfullsize)
|
|
2220 {
|
|
2221 /* next simplest case; size change but the necessary
|
|
2222 allocation size won't change (up or down; code somewhere
|
|
2223 depends on there not being any unused allocation space,
|
|
2224 modulo any alignment constraints). */
|
|
2225 if (pos >= 0)
|
|
2226 {
|
183
|
2227 Bufbyte *addroff = pos + string_data (s);
|
0
|
2228
|
|
2229 memmove (addroff + delta, addroff,
|
|
2230 /* +1 due to zero-termination. */
|
|
2231 string_length (s) + 1 - pos);
|
|
2232 }
|
|
2233 }
|
|
2234 else if (BIG_STRING_FULLSIZE_P (oldfullsize) &&
|
|
2235 BIG_STRING_FULLSIZE_P (newfullsize))
|
|
2236 {
|
|
2237 /* next simplest case; the string is big enough to be malloc()ed
|
|
2238 itself, so we just realloc.
|
|
2239
|
|
2240 It's important not to let the string get below the threshold
|
|
2241 for making big strings and still remain malloc()ed; if that
|
|
2242 were the case, repeated calls to this function on the same
|
|
2243 string could result in memory leakage. */
|
|
2244 set_string_data (s, (Bufbyte *) xrealloc (string_data (s),
|
|
2245 newfullsize));
|
|
2246 if (pos >= 0)
|
|
2247 {
|
183
|
2248 Bufbyte *addroff = pos + string_data (s);
|
0
|
2249
|
|
2250 memmove (addroff + delta, addroff,
|
|
2251 /* +1 due to zero-termination. */
|
|
2252 string_length (s) + 1 - pos);
|
|
2253 }
|
|
2254 }
|
|
2255 else
|
|
2256 {
|
|
2257 /* worst case. We make a new string_chars struct and copy
|
|
2258 the string's data into it, inserting/deleting the delta
|
|
2259 in the process. The old string data will either get
|
|
2260 freed by us (if it was malloc()ed) or will be reclaimed
|
|
2261 in the normal course of garbage collection. */
|
|
2262 struct string_chars *s_chars =
|
|
2263 allocate_string_chars_struct (s, newfullsize);
|
|
2264 Bufbyte *new_addr = &(s_chars->chars[0]);
|
|
2265 Bufbyte *old_addr = string_data (s);
|
|
2266 if (pos >= 0)
|
|
2267 {
|
|
2268 memcpy (new_addr, old_addr, pos);
|
|
2269 memcpy (new_addr + pos + delta, old_addr + pos,
|
|
2270 string_length (s) + 1 - pos);
|
|
2271 }
|
|
2272 set_string_data (s, new_addr);
|
|
2273 if (BIG_STRING_FULLSIZE_P (oldfullsize))
|
|
2274 xfree (old_addr);
|
|
2275 else
|
|
2276 {
|
|
2277 /* We need to mark this chunk of the string_chars_block
|
|
2278 as unused so that compact_string_chars() doesn't
|
|
2279 freak. */
|
|
2280 struct string_chars *old_s_chars =
|
|
2281 (struct string_chars *) ((char *) old_addr -
|
|
2282 sizeof (struct Lisp_String *));
|
|
2283 /* Sanity check to make sure we aren't hosed by strange
|
|
2284 alignment/padding. */
|
|
2285 assert (old_s_chars->string == s);
|
|
2286 MARK_STRUCT_AS_FREE (old_s_chars);
|
|
2287 ((struct unused_string_chars *) old_s_chars)->fullsize =
|
|
2288 oldfullsize;
|
|
2289 }
|
|
2290 }
|
|
2291
|
|
2292 set_string_length (s, string_length (s) + delta);
|
|
2293 /* If pos < 0, the string won't be zero-terminated.
|
|
2294 Terminate now just to make sure. */
|
|
2295 string_data (s)[string_length (s)] = '\0';
|
|
2296
|
|
2297 if (pos >= 0)
|
|
2298 {
|
272
|
2299 Lisp_Object string;
|
0
|
2300
|
|
2301 XSETSTRING (string, s);
|
|
2302 /* We also have to adjust all of the extent indices after the
|
|
2303 place we did the change. We say "pos - 1" because
|
|
2304 adjust_extents() is exclusive of the starting position
|
|
2305 passed to it. */
|
|
2306 adjust_extents (string, pos - 1, string_length (s),
|
|
2307 delta);
|
|
2308 }
|
|
2309 }
|
|
2310
|
|
2311 #ifdef VERIFY_STRING_CHARS_INTEGRITY
|
|
2312 verify_string_chars_integrity ();
|
|
2313 #endif
|
|
2314 }
|
|
2315
|
70
|
2316 #ifdef MULE
|
|
2317
|
|
2318 void
|
|
2319 set_string_char (struct Lisp_String *s, Charcount i, Emchar c)
|
|
2320 {
|
|
2321 Bytecount oldlen, newlen;
|
|
2322 Bufbyte newstr[MAX_EMCHAR_LEN];
|
|
2323 Bytecount bytoff = charcount_to_bytecount (string_data (s), i);
|
|
2324
|
|
2325 oldlen = charcount_to_bytecount (string_data (s) + bytoff, 1);
|
|
2326 newlen = set_charptr_emchar (newstr, c);
|
|
2327
|
|
2328 if (oldlen != newlen)
|
|
2329 resize_string (s, bytoff, newlen - oldlen);
|
185
|
2330 /* Remember, string_data (s) might have changed so we can't cache it. */
|
70
|
2331 memcpy (string_data (s) + bytoff, newstr, newlen);
|
|
2332 }
|
|
2333
|
|
2334 #endif /* MULE */
|
|
2335
|
20
|
2336 DEFUN ("make-string", Fmake_string, 2, 2, 0, /*
|
272
|
2337 Return a new string of length LENGTH, with each character being INIT.
|
0
|
2338 LENGTH must be an integer and INIT must be a character.
|
20
|
2339 */
|
|
2340 (length, init))
|
0
|
2341 {
|
|
2342 Lisp_Object val;
|
|
2343
|
|
2344 CHECK_NATNUM (length);
|
|
2345 CHECK_CHAR_COERCE_INT (init);
|
|
2346 {
|
|
2347 Bufbyte str[MAX_EMCHAR_LEN];
|
|
2348 int len = set_charptr_emchar (str, XCHAR (init));
|
|
2349
|
|
2350 val = make_uninit_string (len * XINT (length));
|
|
2351 if (len == 1)
|
|
2352 /* Optimize the single-byte case */
|
14
|
2353 memset (XSTRING_DATA (val), XCHAR (init), XSTRING_LENGTH (val));
|
0
|
2354 else
|
|
2355 {
|
|
2356 int i, j, k;
|
14
|
2357 Bufbyte *ptr = XSTRING_DATA (val);
|
0
|
2358
|
|
2359 k = 0;
|
|
2360 for (i = 0; i < XINT (length); i++)
|
|
2361 for (j = 0; j < len; j++)
|
|
2362 ptr[k++] = str[j];
|
|
2363 }
|
|
2364 }
|
173
|
2365 return val;
|
0
|
2366 }
|
|
2367
|
|
2368 /* Take some raw memory, which MUST already be in internal format,
|
272
|
2369 and package it up into a Lisp string. */
|
0
|
2370 Lisp_Object
|
|
2371 make_string (CONST Bufbyte *contents, Bytecount length)
|
|
2372 {
|
|
2373 Lisp_Object val;
|
70
|
2374
|
|
2375 /* Make sure we find out about bad make_string's when they happen */
|
|
2376 #if defined (ERROR_CHECK_BUFPOS) && defined (MULE)
|
|
2377 bytecount_to_charcount (contents, length); /* Just for the assertions */
|
|
2378 #endif
|
183
|
2379
|
0
|
2380 val = make_uninit_string (length);
|
14
|
2381 memcpy (XSTRING_DATA (val), contents, length);
|
173
|
2382 return val;
|
0
|
2383 }
|
|
2384
|
|
2385 /* Take some raw memory, encoded in some external data format,
|
|
2386 and convert it into a Lisp string. */
|
|
2387 Lisp_Object
|
|
2388 make_ext_string (CONST Extbyte *contents, EMACS_INT length,
|
|
2389 enum external_data_format fmt)
|
|
2390 {
|
272
|
2391 Bufbyte *intstr;
|
0
|
2392 Bytecount intlen;
|
|
2393
|
|
2394 GET_CHARPTR_INT_DATA_ALLOCA (contents, length, fmt, intstr, intlen);
|
|
2395 return make_string (intstr, intlen);
|
|
2396 }
|
|
2397
|
|
2398 Lisp_Object
|
|
2399 build_string (CONST char *str)
|
|
2400 {
|
185
|
2401 /* Some strlen's crash and burn if passed null. */
|
|
2402 return make_string ((CONST Bufbyte *) str, (str ? strlen(str) : 0));
|
0
|
2403 }
|
|
2404
|
|
2405 Lisp_Object
|
|
2406 build_ext_string (CONST char *str, enum external_data_format fmt)
|
|
2407 {
|
185
|
2408 /* Some strlen's crash and burn if passed null. */
|
272
|
2409 return make_ext_string ((CONST Extbyte *) str, (str ? strlen(str) : 0), fmt);
|
0
|
2410 }
|
|
2411
|
|
2412 Lisp_Object
|
|
2413 build_translated_string (CONST char *str)
|
|
2414 {
|
|
2415 return build_string (GETTEXT (str));
|
|
2416 }
|
|
2417
|
|
2418
|
|
2419 /************************************************************************/
|
|
2420 /* lcrecord lists */
|
|
2421 /************************************************************************/
|
|
2422
|
|
2423 /* Lcrecord lists are used to manage the allocation of particular
|
|
2424 sorts of lcrecords, to avoid calling alloc_lcrecord() (and thus
|
|
2425 malloc() and garbage-collection junk) as much as possible.
|
|
2426 It is similar to the Blocktype class.
|
|
2427
|
|
2428 It works like this:
|
|
2429
|
|
2430 1) Create an lcrecord-list object using make_lcrecord_list().
|
|
2431 This is often done at initialization. Remember to staticpro
|
|
2432 this object! The arguments to make_lcrecord_list() are the
|
|
2433 same as would be passed to alloc_lcrecord().
|
|
2434 2) Instead of calling alloc_lcrecord(), call allocate_managed_lcrecord()
|
|
2435 and pass the lcrecord-list earlier created.
|
|
2436 3) When done with the lcrecord, call free_managed_lcrecord().
|
|
2437 The standard freeing caveats apply: ** make sure there are no
|
|
2438 pointers to the object anywhere! **
|
|
2439 4) Calling free_managed_lcrecord() is just like kissing the
|
|
2440 lcrecord goodbye as if it were garbage-collected. This means:
|
|
2441 -- the contents of the freed lcrecord are undefined, and the
|
|
2442 contents of something produced by allocate_managed_lcrecord()
|
|
2443 are undefined, just like for alloc_lcrecord().
|
|
2444 -- the mark method for the lcrecord's type will *NEVER* be called
|
|
2445 on freed lcrecords.
|
|
2446 -- the finalize method for the lcrecord's type will be called
|
|
2447 at the time that free_managed_lcrecord() is called.
|
|
2448
|
|
2449 */
|
|
2450
|
|
2451 static Lisp_Object
|
|
2452 mark_lcrecord_list (Lisp_Object obj, void (*markobj) (Lisp_Object))
|
|
2453 {
|
|
2454 struct lcrecord_list *list = XLCRECORD_LIST (obj);
|
|
2455 Lisp_Object chain = list->free;
|
|
2456
|
|
2457 while (!NILP (chain))
|
|
2458 {
|
|
2459 struct lrecord_header *lheader = XRECORD_LHEADER (chain);
|
|
2460 struct free_lcrecord_header *free_header =
|
|
2461 (struct free_lcrecord_header *) lheader;
|
14
|
2462
|
|
2463 #ifdef ERROR_CHECK_GC
|
0
|
2464 CONST struct lrecord_implementation *implementation
|
211
|
2465 = LHEADER_IMPLEMENTATION(lheader);
|
80
|
2466
|
0
|
2467 /* There should be no other pointers to the free list. */
|
|
2468 assert (!MARKED_RECORD_HEADER_P (lheader));
|
|
2469 /* Only lcrecords should be here. */
|
|
2470 assert (!implementation->basic_p);
|
|
2471 /* Only free lcrecords should be here. */
|
|
2472 assert (free_header->lcheader.free);
|
|
2473 /* The type of the lcrecord must be right. */
|
|
2474 assert (implementation == list->implementation);
|
|
2475 /* So must the size. */
|
|
2476 assert (implementation->static_size == 0
|
|
2477 || implementation->static_size == list->size);
|
14
|
2478 #endif /* ERROR_CHECK_GC */
|
80
|
2479
|
0
|
2480 MARK_RECORD_HEADER (lheader);
|
|
2481 chain = free_header->chain;
|
|
2482 }
|
183
|
2483
|
0
|
2484 return Qnil;
|
|
2485 }
|
|
2486
|
243
|
2487 DEFINE_LRECORD_IMPLEMENTATION ("lcrecord-list", lcrecord_list,
|
|
2488 mark_lcrecord_list, internal_object_printer,
|
|
2489 0, 0, 0, struct lcrecord_list);
|
0
|
2490 Lisp_Object
|
272
|
2491 make_lcrecord_list (size_t size,
|
0
|
2492 CONST struct lrecord_implementation *implementation)
|
|
2493 {
|
185
|
2494 struct lcrecord_list *p = alloc_lcrecord_type (struct lcrecord_list,
|
|
2495 lrecord_lcrecord_list);
|
272
|
2496 Lisp_Object val;
|
0
|
2497
|
|
2498 p->implementation = implementation;
|
|
2499 p->size = size;
|
|
2500 p->free = Qnil;
|
|
2501 XSETLCRECORD_LIST (val, p);
|
|
2502 return val;
|
|
2503 }
|
|
2504
|
|
2505 Lisp_Object
|
|
2506 allocate_managed_lcrecord (Lisp_Object lcrecord_list)
|
|
2507 {
|
|
2508 struct lcrecord_list *list = XLCRECORD_LIST (lcrecord_list);
|
|
2509 if (!NILP (list->free))
|
|
2510 {
|
|
2511 Lisp_Object val = list->free;
|
|
2512 struct free_lcrecord_header *free_header =
|
|
2513 (struct free_lcrecord_header *) XPNTR (val);
|
|
2514
|
|
2515 #ifdef ERROR_CHECK_GC
|
|
2516 struct lrecord_header *lheader =
|
|
2517 (struct lrecord_header *) free_header;
|
|
2518 CONST struct lrecord_implementation *implementation
|
211
|
2519 = LHEADER_IMPLEMENTATION (lheader);
|
0
|
2520
|
|
2521 /* There should be no other pointers to the free list. */
|
|
2522 assert (!MARKED_RECORD_HEADER_P (lheader));
|
|
2523 /* Only lcrecords should be here. */
|
|
2524 assert (!implementation->basic_p);
|
|
2525 /* Only free lcrecords should be here. */
|
|
2526 assert (free_header->lcheader.free);
|
|
2527 /* The type of the lcrecord must be right. */
|
|
2528 assert (implementation == list->implementation);
|
|
2529 /* So must the size. */
|
|
2530 assert (implementation->static_size == 0
|
|
2531 || implementation->static_size == list->size);
|
183
|
2532 #endif /* ERROR_CHECK_GC */
|
0
|
2533 list->free = free_header->chain;
|
|
2534 free_header->lcheader.free = 0;
|
|
2535 return val;
|
|
2536 }
|
|
2537 else
|
|
2538 {
|
272
|
2539 Lisp_Object val;
|
185
|
2540
|
|
2541 XSETOBJ (val, Lisp_Type_Record,
|
0
|
2542 alloc_lcrecord (list->size, list->implementation));
|
185
|
2543 return val;
|
0
|
2544 }
|
|
2545 }
|
|
2546
|
|
2547 void
|
|
2548 free_managed_lcrecord (Lisp_Object lcrecord_list, Lisp_Object lcrecord)
|
|
2549 {
|
|
2550 struct lcrecord_list *list = XLCRECORD_LIST (lcrecord_list);
|
|
2551 struct free_lcrecord_header *free_header =
|
|
2552 (struct free_lcrecord_header *) XPNTR (lcrecord);
|
|
2553 struct lrecord_header *lheader =
|
|
2554 (struct lrecord_header *) free_header;
|
|
2555 CONST struct lrecord_implementation *implementation
|
211
|
2556 = LHEADER_IMPLEMENTATION (lheader);
|
0
|
2557
|
|
2558 #ifdef ERROR_CHECK_GC
|
|
2559 /* Make sure the size is correct. This will catch, for example,
|
|
2560 putting a window configuration on the wrong free list. */
|
|
2561 if (implementation->size_in_bytes_method)
|
|
2562 assert (((implementation->size_in_bytes_method) (lheader))
|
|
2563 == list->size);
|
|
2564 else
|
|
2565 assert (implementation->static_size == list->size);
|
183
|
2566 #endif /* ERROR_CHECK_GC */
|
0
|
2567
|
|
2568 if (implementation->finalizer)
|
|
2569 ((implementation->finalizer) (lheader, 0));
|
|
2570 free_header->chain = list->free;
|
|
2571 free_header->lcheader.free = 1;
|
|
2572 list->free = lcrecord;
|
|
2573 }
|
|
2574
|
|
2575
|
|
2576 /**********************************************************************/
|
|
2577 /* Purity of essence, peace on earth */
|
|
2578 /**********************************************************************/
|
|
2579
|
|
2580 static int symbols_initialized;
|
|
2581
|
|
2582 Lisp_Object
|
|
2583 make_pure_string (CONST Bufbyte *data, Bytecount length,
|
|
2584 Lisp_Object plist, int no_need_to_copy_data)
|
|
2585 {
|
|
2586 Lisp_Object new;
|
|
2587 struct Lisp_String *s;
|
272
|
2588 size_t size = sizeof (struct Lisp_String) +
|
|
2589 (no_need_to_copy_data ? 0 : (length + 1)); /* + 1 for terminating 0 */
|
0
|
2590 size = ALIGN_SIZE (size, ALIGNOF (Lisp_Object));
|
|
2591
|
|
2592 if (symbols_initialized && !pure_lossage)
|
|
2593 {
|
|
2594 /* Try to share some names. Saves a few kbytes. */
|
|
2595 Lisp_Object tem = oblookup (Vobarray, data, length);
|
|
2596 if (SYMBOLP (tem))
|
|
2597 {
|
|
2598 s = XSYMBOL (tem)->name;
|
|
2599 if (!PURIFIED (s)) abort ();
|
|
2600 XSETSTRING (new, s);
|
173
|
2601 return new;
|
0
|
2602 }
|
|
2603 }
|
|
2604
|
|
2605 if (!check_purespace (size))
|
173
|
2606 return make_string (data, length);
|
0
|
2607
|
272
|
2608 s = (struct Lisp_String *) (PUREBEG + pure_bytes_used);
|
207
|
2609 #ifdef LRECORD_STRING
|
|
2610 set_lheader_implementation (&(s->lheader), lrecord_string);
|
211
|
2611 #ifdef USE_INDEXED_LRECORD_IMPLEMENTATION
|
|
2612 s->lheader.pure = 1;
|
|
2613 #endif
|
207
|
2614 #endif
|
0
|
2615 set_string_length (s, length);
|
|
2616 if (no_need_to_copy_data)
|
|
2617 {
|
|
2618 set_string_data (s, (Bufbyte *) data);
|
|
2619 }
|
|
2620 else
|
|
2621 {
|
|
2622 set_string_data (s, (Bufbyte *) s + sizeof (struct Lisp_String));
|
|
2623 memcpy (string_data (s), data, length);
|
|
2624 set_string_byte (s, length, 0);
|
|
2625 }
|
|
2626 s->plist = Qnil;
|
272
|
2627 pure_bytes_used += size;
|
0
|
2628
|
|
2629 #ifdef PURESTAT
|
|
2630 bump_purestat (&purestat_string_all, size);
|
|
2631 if (purecopying_for_bytecode)
|
|
2632 bump_purestat (&purestat_string_other_function, size);
|
183
|
2633 #endif /* PURESTAT */
|
0
|
2634
|
|
2635 /* Do this after the official "completion" of the purecopying. */
|
|
2636 s->plist = Fpurecopy (plist);
|
|
2637
|
|
2638 XSETSTRING (new, s);
|
173
|
2639 return new;
|
0
|
2640 }
|
|
2641
|
|
2642
|
|
2643 Lisp_Object
|
|
2644 make_pure_pname (CONST Bufbyte *data, Bytecount length,
|
|
2645 int no_need_to_copy_data)
|
|
2646 {
|
|
2647 Lisp_Object name = make_pure_string (data, length, Qnil,
|
|
2648 no_need_to_copy_data);
|
|
2649 bump_purestat (&purestat_string_pname, pure_sizeof (name));
|
|
2650
|
|
2651 /* We've made (at least) Qnil now, and Vobarray will soon be set up. */
|
|
2652 symbols_initialized = 1;
|
|
2653
|
173
|
2654 return name;
|
0
|
2655 }
|
|
2656
|
|
2657
|
|
2658 Lisp_Object
|
|
2659 pure_cons (Lisp_Object car, Lisp_Object cdr)
|
|
2660 {
|
|
2661 Lisp_Object new;
|
207
|
2662 struct Lisp_Cons *c;
|
0
|
2663
|
|
2664 if (!check_purespace (sizeof (struct Lisp_Cons)))
|
173
|
2665 return Fcons (Fpurecopy (car), Fpurecopy (cdr));
|
0
|
2666
|
272
|
2667 c = (struct Lisp_Cons *) (PUREBEG + pure_bytes_used);
|
207
|
2668 #ifdef LRECORD_CONS
|
|
2669 set_lheader_implementation (&(c->lheader), lrecord_cons);
|
211
|
2670 #ifdef USE_INDEXED_LRECORD_IMPLEMENTATION
|
|
2671 c->lheader.pure = 1;
|
|
2672 #endif
|
207
|
2673 #endif
|
272
|
2674 pure_bytes_used += sizeof (struct Lisp_Cons);
|
0
|
2675 bump_purestat (&purestat_cons, sizeof (struct Lisp_Cons));
|
|
2676
|
207
|
2677 c->car = Fpurecopy (car);
|
|
2678 c->cdr = Fpurecopy (cdr);
|
|
2679 XSETCONS (new, c);
|
173
|
2680 return new;
|
0
|
2681 }
|
|
2682
|
|
2683 Lisp_Object
|
|
2684 pure_list (int nargs, Lisp_Object *args)
|
|
2685 {
|
272
|
2686 Lisp_Object val = Qnil;
|
0
|
2687
|
|
2688 for (--nargs; nargs >= 0; nargs--)
|
272
|
2689 val = pure_cons (args[nargs], val);
|
|
2690
|
|
2691 return val;
|
0
|
2692 }
|
|
2693
|
|
2694 #ifdef LISP_FLOAT_TYPE
|
|
2695
|
272
|
2696 static Lisp_Object
|
0
|
2697 make_pure_float (double num)
|
|
2698 {
|
|
2699 struct Lisp_Float *f;
|
|
2700 Lisp_Object val;
|
|
2701
|
272
|
2702 /* Make sure that PUREBEG + pure_bytes_used is aligned on at least a sizeof
|
0
|
2703 (double) boundary. Some architectures (like the sparc) require
|
|
2704 this, and I suspect that floats are rare enough that it's no
|
|
2705 tragedy for those that don't. */
|
|
2706 {
|
|
2707 #if defined (__GNUC__) && (__GNUC__ >= 2)
|
|
2708 /* In gcc, we can directly ask what the alignment constraints of a
|
|
2709 structure are, but in general, that's not possible... Arrgh!!
|
|
2710 */
|
|
2711 int alignment = __alignof (struct Lisp_Float);
|
|
2712 #else /* !GNUC */
|
|
2713 /* Best guess is to make the `double' slot be aligned to the size
|
|
2714 of double (which is probably 8 bytes). This assumes that it's
|
|
2715 ok to align the beginning of the structure to the same boundary
|
|
2716 that the `double' slot in it is supposed to be aligned to; this
|
|
2717 should be ok because presumably there is padding in the layout
|
|
2718 of the struct to account for this.
|
|
2719 */
|
|
2720 int alignment = sizeof (float_data (f));
|
183
|
2721 #endif /* !GNUC */
|
272
|
2722 char *p = ((char *) PUREBEG + pure_bytes_used);
|
|
2723
|
|
2724 p = (char *) (((EMACS_UINT) p + alignment - 1) & - alignment);
|
|
2725 pure_bytes_used = p - (char *) PUREBEG;
|
0
|
2726 }
|
|
2727
|
|
2728 if (!check_purespace (sizeof (struct Lisp_Float)))
|
173
|
2729 return make_float (num);
|
0
|
2730
|
272
|
2731 f = (struct Lisp_Float *) (PUREBEG + pure_bytes_used);
|
0
|
2732 set_lheader_implementation (&(f->lheader), lrecord_float);
|
211
|
2733 #ifdef USE_INDEXED_LRECORD_IMPLEMENTATION
|
|
2734 f->lheader.pure = 1;
|
|
2735 #endif
|
272
|
2736 pure_bytes_used += sizeof (struct Lisp_Float);
|
0
|
2737 bump_purestat (&purestat_float, sizeof (struct Lisp_Float));
|
|
2738
|
|
2739 float_next (f) = ((struct Lisp_Float *) -1);
|
|
2740 float_data (f) = num;
|
|
2741 XSETFLOAT (val, f);
|
173
|
2742 return val;
|
0
|
2743 }
|
|
2744
|
|
2745 #endif /* LISP_FLOAT_TYPE */
|
|
2746
|
|
2747 Lisp_Object
|
272
|
2748 make_pure_vector (size_t len, Lisp_Object init)
|
0
|
2749 {
|
|
2750 Lisp_Object new;
|
207
|
2751 struct Lisp_Vector *v;
|
272
|
2752 size_t size = (sizeof (struct Lisp_Vector)
|
|
2753 + (len - 1) * sizeof (Lisp_Object));
|
0
|
2754
|
|
2755 init = Fpurecopy (init);
|
|
2756
|
|
2757 if (!check_purespace (size))
|
173
|
2758 return make_vector (len, init);
|
0
|
2759
|
272
|
2760 v = (struct Lisp_Vector *) (PUREBEG + pure_bytes_used);
|
207
|
2761 #ifdef LRECORD_VECTOR
|
|
2762 set_lheader_implementation (&(v->header.lheader), lrecord_vector);
|
211
|
2763 #ifdef USE_INDEXED_LRECORD_IMPLEMENTATION
|
|
2764 v->header.lheader.pure = 1;
|
|
2765 #endif
|
207
|
2766 #endif
|
272
|
2767 pure_bytes_used += size;
|
0
|
2768 bump_purestat (&purestat_vector_all, size);
|
|
2769
|
207
|
2770 v->size = len;
|
0
|
2771
|
|
2772 for (size = 0; size < len; size++)
|
207
|
2773 v->contents[size] = init;
|
|
2774
|
243
|
2775 XSETVECTOR (new, v);
|
173
|
2776 return new;
|
0
|
2777 }
|
|
2778
|
|
2779 #if 0
|
|
2780 /* Presently unused */
|
|
2781 void *
|
|
2782 alloc_pure_lrecord (int size, struct lrecord_implementation *implementation)
|
|
2783 {
|
272
|
2784 struct lrecord_header *header = (void *) (PUREBEG + pure_bytes_used);
|
|
2785
|
|
2786 if (pure_bytes_used + size > get_PURESIZE())
|
0
|
2787 pure_storage_exhausted ();
|
|
2788
|
|
2789 set_lheader_implementation (header, implementation);
|
|
2790 header->next = 0;
|
173
|
2791 return header;
|
0
|
2792 }
|
183
|
2793 #endif /* unused */
|
0
|
2794
|
|
2795
|
|
2796
|
20
|
2797 DEFUN ("purecopy", Fpurecopy, 1, 1, 0, /*
|
0
|
2798 Make a copy of OBJECT in pure storage.
|
|
2799 Recursively copies contents of vectors and cons cells.
|
|
2800 Does not copy symbols.
|
20
|
2801 */
|
|
2802 (obj))
|
0
|
2803 {
|
|
2804 int i;
|
|
2805 if (!purify_flag)
|
173
|
2806 return obj;
|
0
|
2807
|
|
2808 if (!POINTER_TYPE_P (XTYPE (obj))
|
207
|
2809 || PURIFIED (XPNTR (obj))
|
|
2810 /* happens when bootstrapping Qnil */
|
|
2811 || EQ (obj, Qnull_pointer))
|
173
|
2812 return obj;
|
0
|
2813
|
|
2814 switch (XTYPE (obj))
|
|
2815 {
|
207
|
2816 #ifndef LRECORD_CONS
|
185
|
2817 case Lisp_Type_Cons:
|
0
|
2818 return pure_cons (XCAR (obj), XCDR (obj));
|
207
|
2819 #endif
|
|
2820
|
|
2821 #ifndef LRECORD_STRING
|
185
|
2822 case Lisp_Type_String:
|
16
|
2823 return make_pure_string (XSTRING_DATA (obj),
|
|
2824 XSTRING_LENGTH (obj),
|
0
|
2825 XSTRING (obj)->plist,
|
|
2826 0);
|
207
|
2827 #endif /* ! LRECORD_STRING */
|
0
|
2828
|
185
|
2829 #ifndef LRECORD_VECTOR
|
|
2830 case Lisp_Type_Vector:
|
0
|
2831 {
|
|
2832 struct Lisp_Vector *o = XVECTOR (obj);
|
|
2833 Lisp_Object new = make_pure_vector (vector_length (o), Qnil);
|
|
2834 for (i = 0; i < vector_length (o); i++)
|
173
|
2835 XVECTOR_DATA (new)[i] = Fpurecopy (o->contents[i]);
|
|
2836 return new;
|
0
|
2837 }
|
185
|
2838 #endif /* !LRECORD_VECTOR */
|
0
|
2839
|
|
2840 default:
|
|
2841 {
|
|
2842 if (COMPILED_FUNCTIONP (obj))
|
|
2843 {
|
|
2844 struct Lisp_Compiled_Function *o = XCOMPILED_FUNCTION (obj);
|
|
2845 Lisp_Object new = make_compiled_function (1);
|
104
|
2846 /* How on earth could this code have worked before? -sb */
|
|
2847 struct Lisp_Compiled_Function *n = XCOMPILED_FUNCTION (new);
|
0
|
2848 n->flags = o->flags;
|
|
2849 n->bytecodes = Fpurecopy (o->bytecodes);
|
|
2850 n->constants = Fpurecopy (o->constants);
|
|
2851 n->arglist = Fpurecopy (o->arglist);
|
|
2852 n->doc_and_interactive = Fpurecopy (o->doc_and_interactive);
|
104
|
2853 n->maxdepth = o->maxdepth;
|
173
|
2854 return new;
|
0
|
2855 }
|
207
|
2856 #ifdef LRECORD_CONS
|
|
2857 else if (CONSP (obj))
|
|
2858 return pure_cons (XCAR (obj), XCDR (obj));
|
|
2859 #endif /* LRECORD_CONS */
|
|
2860 #ifdef LRECORD_VECTOR
|
|
2861 else if (VECTORP (obj))
|
|
2862 {
|
|
2863 struct Lisp_Vector *o = XVECTOR (obj);
|
|
2864 Lisp_Object new = make_pure_vector (vector_length (o), Qnil);
|
|
2865 for (i = 0; i < vector_length (o); i++)
|
|
2866 XVECTOR_DATA (new)[i] = Fpurecopy (o->contents[i]);
|
|
2867 return new;
|
|
2868 }
|
|
2869 #endif /* LRECORD_VECTOR */
|
|
2870 #ifdef LRECORD_STRING
|
|
2871 else if (STRINGP (obj))
|
|
2872 {
|
|
2873 return make_pure_string (XSTRING_DATA (obj),
|
|
2874 XSTRING_LENGTH (obj),
|
|
2875 XSTRING (obj)->plist,
|
|
2876 0);
|
|
2877 }
|
|
2878 #endif /* LRECORD_STRING */
|
0
|
2879 #ifdef LISP_FLOAT_TYPE
|
|
2880 else if (FLOATP (obj))
|
|
2881 return make_pure_float (float_data (XFLOAT (obj)));
|
|
2882 #endif /* LISP_FLOAT_TYPE */
|
247
|
2883 else if (SYMBOLP (obj))
|
|
2884 {
|
|
2885 /*
|
|
2886 * Symbols can't be made pure (and thus read-only),
|
|
2887 * because assigning to their function, value or plist
|
|
2888 * slots would produced a SEGV in the dumped XEmacs. So
|
|
2889 * we previously would just return the symbol unchanged.
|
|
2890 *
|
|
2891 * But purified aggregate objects like lists and vectors
|
|
2892 * can contain uninterned symbols. If there are no
|
|
2893 * other non-pure references to the symbol, then the
|
249
|
2894 * symbol is not protected from garbage collection
|
247
|
2895 * because the collector does not mark the contents of
|
|
2896 * purified objects. So to protect the symbols, an impure
|
|
2897 * reference has to be kept for each uninterned symbol
|
|
2898 * that is referenced by a pure object. All such
|
|
2899 * symbols are stored in the hashtable pointed to by
|
249
|
2900 * Vpure_uninterned_symbol_table, which is itself
|
247
|
2901 * staticpro'd.
|
|
2902 */
|
251
|
2903 if (!NILP (XSYMBOL (obj)->obarray))
|
247
|
2904 return obj;
|
263
|
2905 Fputhash (obj, Qnil, Vpure_uninterned_symbol_table);
|
247
|
2906 return obj;
|
|
2907 }
|
|
2908 else
|
0
|
2909 signal_simple_error ("Can't purecopy %S", obj);
|
|
2910 }
|
|
2911 }
|
173
|
2912 return obj;
|
0
|
2913 }
|
|
2914
|
|
2915
|
|
2916
|
102
|
2917 static void
|
272
|
2918 puresize_adjust_h (size_t puresize)
|
102
|
2919 {
|
183
|
2920 FILE *stream = fopen ("puresize-adjust.h", "w");
|
|
2921
|
|
2922 if (stream == NULL)
|
|
2923 report_file_error ("Opening puresize adjustment file",
|
|
2924 Fcons (build_string ("puresize-adjust.h"), Qnil));
|
|
2925
|
|
2926 fprintf (stream,
|
|
2927 "/*\tDo not edit this file!\n"
|
|
2928 "\tAutomatically generated by XEmacs */\n"
|
|
2929 "# define PURESIZE_ADJUSTMENT (%ld)\n",
|
272
|
2930 (long) (puresize - RAW_PURESIZE));
|
183
|
2931 fclose (stream);
|
102
|
2932 }
|
|
2933
|
0
|
2934 void
|
|
2935 report_pure_usage (int report_impurities,
|
|
2936 int die_if_pure_storage_exceeded)
|
|
2937 {
|
102
|
2938 int rc = 0;
|
|
2939
|
0
|
2940 if (pure_lossage)
|
|
2941 {
|
|
2942 message ("\n****\tPure Lisp storage exhausted!\n"
|
183
|
2943 "\tPurespace usage: %ld of %ld\n"
|
|
2944 "****",
|
272
|
2945 (long) get_PURESIZE() + pure_lossage,
|
|
2946 (long) get_PURESIZE());
|
|
2947 if (die_if_pure_storage_exceeded)
|
|
2948 {
|
|
2949 puresize_adjust_h (get_PURESIZE() + pure_lossage);
|
251
|
2950 #ifdef HEAP_IN_DATA
|
272
|
2951 sheap_adjust_h();
|
251
|
2952 #endif
|
272
|
2953 rc = -1;
|
|
2954 }
|
0
|
2955 }
|
|
2956 else
|
|
2957 {
|
272
|
2958 size_t lost = (get_PURESIZE() - pure_bytes_used) / 1024;
|
0
|
2959 char buf[200];
|
251
|
2960 /* extern Lisp_Object Vemacs_beta_version; */
|
247
|
2961 /* This used to be NILP(Vemacs_beta_version) ? 512 : 4; */
|
|
2962 #ifndef PURESIZE_SLOP
|
249
|
2963 #define PURESIZE_SLOP 0
|
247
|
2964 #endif
|
272
|
2965 size_t slop = PURESIZE_SLOP;
|
0
|
2966
|
|
2967 sprintf (buf, "Purespace usage: %ld of %ld (%d%%",
|
272
|
2968 (long) pure_bytes_used,
|
|
2969 (long) get_PURESIZE(),
|
|
2970 (int) (pure_bytes_used / (get_PURESIZE() / 100.0) + 0.5));
|
249
|
2971 if (lost > ((slop ? slop : 1) / 1024)) {
|
274
|
2972 sprintf (buf + strlen (buf), " -- %ldk wasted", (long)lost);
|
102
|
2973 if (die_if_pure_storage_exceeded) {
|
272
|
2974 puresize_adjust_h (pure_bytes_used + slop);
|
251
|
2975 #ifdef HEAP_IN_DATA
|
|
2976 sheap_adjust_h();
|
|
2977 #endif
|
102
|
2978 rc = -1;
|
|
2979 }
|
|
2980 }
|
|
2981
|
0
|
2982 strcat (buf, ").");
|
|
2983 message ("%s", buf);
|
|
2984 }
|
|
2985
|
|
2986 #ifdef PURESTAT
|
183
|
2987
|
|
2988 purestat_vector_other.nbytes =
|
|
2989 purestat_vector_all.nbytes -
|
|
2990 purestat_vector_bytecode_constants.nbytes;
|
|
2991 purestat_vector_other.nobjects =
|
|
2992 purestat_vector_all.nobjects -
|
|
2993 purestat_vector_bytecode_constants.nobjects;
|
|
2994
|
|
2995 purestat_string_other.nbytes =
|
|
2996 purestat_string_all.nbytes -
|
|
2997 (purestat_string_pname.nbytes +
|
|
2998 purestat_string_bytecodes.nbytes +
|
|
2999 purestat_string_interactive.nbytes +
|
|
3000 purestat_string_documentation.nbytes +
|
0
|
3001 #ifdef I18N3
|
183
|
3002 purestat_string_domain.nbytes +
|
0
|
3003 #endif
|
183
|
3004 purestat_string_other_function.nbytes);
|
|
3005
|
|
3006 purestat_string_other.nobjects =
|
|
3007 purestat_string_all.nobjects -
|
|
3008 (purestat_string_pname.nobjects +
|
|
3009 purestat_string_bytecodes.nobjects +
|
|
3010 purestat_string_interactive.nobjects +
|
|
3011 purestat_string_documentation.nobjects +
|
0
|
3012 #ifdef I18N3
|
183
|
3013 purestat_string_domain.nobjects +
|
0
|
3014 #endif
|
183
|
3015 purestat_string_other_function.nobjects);
|
|
3016
|
|
3017 message (" %-26s Total Bytes", "");
|
|
3018
|
|
3019 {
|
|
3020 int j;
|
|
3021
|
|
3022 for (j = 0; j < countof (purestats); j++)
|
|
3023 if (!purestats[j])
|
0
|
3024 clear_message ();
|
|
3025 else
|
183
|
3026 {
|
|
3027 char buf [100];
|
|
3028 sprintf(buf, "%s:", purestats[j]->name);
|
|
3029 message (" %-26s %5d %7d %2d%%",
|
|
3030 buf,
|
|
3031 purestats[j]->nobjects,
|
|
3032 purestats[j]->nbytes,
|
272
|
3033 (int) (purestats[j]->nbytes / (pure_bytes_used / 100.0) + 0.5));
|
183
|
3034 }
|
0
|
3035 }
|
|
3036 #endif /* PURESTAT */
|
|
3037
|
|
3038
|
|
3039 if (report_impurities)
|
|
3040 {
|
|
3041 Lisp_Object tem = Felt (Fgarbage_collect (), make_int (5));
|
|
3042 struct gcpro gcpro1;
|
|
3043 GCPRO1 (tem);
|
|
3044 message ("\nImpurities:");
|
|
3045 while (!NILP (tem))
|
|
3046 {
|
|
3047 if (CONSP (tem) && SYMBOLP (Fcar (tem)) && CONSP (Fcdr (tem)))
|
|
3048 {
|
|
3049 int total = XINT (Fcar (Fcdr (tem)));
|
|
3050 if (total > 0)
|
|
3051 {
|
|
3052 char buf [100];
|
|
3053 char *s = buf;
|
|
3054 memcpy (buf, string_data (XSYMBOL (Fcar (tem))->name),
|
|
3055 string_length (XSYMBOL (Fcar (tem))->name) + 1);
|
|
3056 while (*s++) if (*s == '-') *s = ' ';
|
|
3057 s--; *s++ = ':'; *s = 0;
|
183
|
3058 message (" %-33s %6d", buf, total);
|
0
|
3059 }
|
|
3060 tem = Fcdr (Fcdr (tem));
|
|
3061 }
|
|
3062 else /* WTF?! */
|
|
3063 {
|
|
3064 Fprin1 (tem, Qexternal_debugging_output);
|
|
3065 tem = Qnil;
|
|
3066 }
|
|
3067 }
|
|
3068 UNGCPRO;
|
|
3069 garbage_collect_1 (); /* GC garbage_collect's garbage */
|
|
3070 }
|
|
3071 clear_message ();
|
|
3072
|
102
|
3073 if (rc < 0) {
|
183
|
3074 unlink("SATISFIED");
|
118
|
3075 /* Current build process on NT does */
|
|
3076 /* not know how to restart itself. */
|
183
|
3077 /* --marcpa */
|
118
|
3078 #ifndef WINDOWSNT
|
171
|
3079 fatal ("Pure size adjusted, Don't Panic! I will restart the `make'");
|
118
|
3080 #endif
|
102
|
3081 } else if (pure_lossage && die_if_pure_storage_exceeded) {
|
0
|
3082 fatal ("Pure storage exhausted");
|
102
|
3083 }
|
0
|
3084 }
|
|
3085
|
|
3086
|
|
3087 /**********************************************************************/
|
|
3088 /* staticpro */
|
|
3089 /**********************************************************************/
|
|
3090
|
|
3091 struct gcpro *gcprolist;
|
|
3092
|
|
3093 /* 415 used Mly 29-Jun-93 */
|
261
|
3094 /* 1327 used slb 28-Feb-98 */
|
263
|
3095 #ifdef HAVE_SHLIB
|
|
3096 #define NSTATICS 4000
|
|
3097 #else
|
|
3098 #define NSTATICS 2000
|
|
3099 #endif
|
0
|
3100 /* Not "static" because of linker lossage on some systems */
|
|
3101 Lisp_Object *staticvec[NSTATICS]
|
|
3102 /* Force it into data space! */
|
|
3103 = {0};
|
|
3104 static int staticidx;
|
|
3105
|
|
3106 /* Put an entry in staticvec, pointing at the variable whose address is given
|
|
3107 */
|
|
3108 void
|
|
3109 staticpro (Lisp_Object *varaddress)
|
|
3110 {
|
|
3111 if (staticidx >= countof (staticvec))
|
263
|
3112 /* #### This is now a dubious abort() since this routine may be called */
|
|
3113 /* by Lisp attempting to load a DLL. */
|
0
|
3114 abort ();
|
|
3115 staticvec[staticidx++] = varaddress;
|
|
3116 }
|
|
3117
|
|
3118
|
|
3119 /* Mark reference to a Lisp_Object. If the object referred to has not been
|
|
3120 seen yet, recursively mark all the references contained in it. */
|
183
|
3121
|
0
|
3122 static void
|
|
3123 mark_object (Lisp_Object obj)
|
|
3124 {
|
|
3125 tail_recurse:
|
|
3126
|
207
|
3127 if (EQ (obj, Qnull_pointer))
|
|
3128 return;
|
0
|
3129 if (!POINTER_TYPE_P (XGCTYPE (obj)))
|
|
3130 return;
|
|
3131 if (PURIFIED (XPNTR (obj)))
|
|
3132 return;
|
|
3133 switch (XGCTYPE (obj))
|
|
3134 {
|
207
|
3135 #ifndef LRECORD_CONS
|
185
|
3136 case Lisp_Type_Cons:
|
0
|
3137 {
|
|
3138 struct Lisp_Cons *ptr = XCONS (obj);
|
|
3139 if (CONS_MARKED_P (ptr))
|
|
3140 break;
|
|
3141 MARK_CONS (ptr);
|
|
3142 /* If the cdr is nil, tail-recurse on the car. */
|
|
3143 if (NILP (ptr->cdr))
|
|
3144 {
|
|
3145 obj = ptr->car;
|
|
3146 }
|
|
3147 else
|
|
3148 {
|
|
3149 mark_object (ptr->car);
|
|
3150 obj = ptr->cdr;
|
|
3151 }
|
|
3152 goto tail_recurse;
|
|
3153 }
|
207
|
3154 #endif
|
0
|
3155
|
185
|
3156 case Lisp_Type_Record:
|
0
|
3157 /* case Lisp_Symbol_Value_Magic: */
|
|
3158 {
|
|
3159 struct lrecord_header *lheader = XRECORD_LHEADER (obj);
|
|
3160 CONST struct lrecord_implementation *implementation
|
211
|
3161 = LHEADER_IMPLEMENTATION (lheader);
|
0
|
3162
|
|
3163 if (! MARKED_RECORD_HEADER_P (lheader) &&
|
|
3164 ! UNMARKABLE_RECORD_HEADER_P (lheader))
|
|
3165 {
|
|
3166 MARK_RECORD_HEADER (lheader);
|
|
3167 #ifdef ERROR_CHECK_GC
|
|
3168 if (!implementation->basic_p)
|
|
3169 assert (! ((struct lcrecord_header *) lheader)->free);
|
|
3170 #endif
|
|
3171 if (implementation->marker != 0)
|
|
3172 {
|
|
3173 obj = ((implementation->marker) (obj, mark_object));
|
|
3174 if (!NILP (obj)) goto tail_recurse;
|
|
3175 }
|
|
3176 }
|
|
3177 }
|
|
3178 break;
|
|
3179
|
207
|
3180 #ifndef LRECORD_STRING
|
185
|
3181 case Lisp_Type_String:
|
0
|
3182 {
|
|
3183 struct Lisp_String *ptr = XSTRING (obj);
|
|
3184
|
|
3185 if (!XMARKBIT (ptr->plist))
|
|
3186 {
|
|
3187 if (CONSP (ptr->plist) &&
|
|
3188 EXTENT_INFOP (XCAR (ptr->plist)))
|
|
3189 flush_cached_extent_info (XCAR (ptr->plist));
|
|
3190 XMARK (ptr->plist);
|
|
3191 obj = ptr->plist;
|
|
3192 goto tail_recurse;
|
|
3193 }
|
|
3194 }
|
|
3195 break;
|
207
|
3196 #endif /* ! LRECORD_STRING */
|
0
|
3197
|
185
|
3198 #ifndef LRECORD_VECTOR
|
|
3199 case Lisp_Type_Vector:
|
0
|
3200 {
|
|
3201 struct Lisp_Vector *ptr = XVECTOR (obj);
|
|
3202 int len = vector_length (ptr);
|
|
3203 int i;
|
|
3204
|
|
3205 if (len < 0)
|
|
3206 break; /* Already marked */
|
|
3207 ptr->size = -1 - len; /* Else mark it */
|
|
3208 for (i = 0; i < len - 1; i++) /* and then mark its elements */
|
|
3209 mark_object (ptr->contents[i]);
|
|
3210 if (len > 0)
|
|
3211 {
|
|
3212 obj = ptr->contents[len - 1];
|
|
3213 goto tail_recurse;
|
|
3214 }
|
|
3215 }
|
|
3216 break;
|
185
|
3217 #endif /* !LRECORD_VECTOR */
|
0
|
3218
|
|
3219 #ifndef LRECORD_SYMBOL
|
185
|
3220 case Lisp_Type_Symbol:
|
0
|
3221 {
|
|
3222 struct Lisp_Symbol *sym = XSYMBOL (obj);
|
|
3223
|
|
3224 while (!XMARKBIT (sym->plist))
|
|
3225 {
|
|
3226 XMARK (sym->plist);
|
|
3227 mark_object (sym->value);
|
|
3228 mark_object (sym->function);
|
|
3229 {
|
207
|
3230 /*
|
|
3231 * symbol->name is a struct Lisp_String *, not a
|
|
3232 * Lisp_Object. Fix it up and pass to mark_object.
|
|
3233 */
|
|
3234 Lisp_Object symname;
|
|
3235 XSETSTRING(symname, sym->name);
|
|
3236 mark_object(symname);
|
0
|
3237 }
|
|
3238 if (!symbol_next (sym))
|
|
3239 {
|
|
3240 obj = sym->plist;
|
|
3241 goto tail_recurse;
|
|
3242 }
|
|
3243 mark_object (sym->plist);
|
|
3244 /* Mark the rest of the symbols in the hash-chain */
|
|
3245 sym = symbol_next (sym);
|
|
3246 }
|
|
3247 }
|
|
3248 break;
|
|
3249 #endif /* !LRECORD_SYMBOL */
|
|
3250
|
|
3251 default:
|
|
3252 abort ();
|
|
3253 }
|
|
3254 }
|
|
3255
|
|
3256 /* mark all of the conses in a list and mark the final cdr; but
|
|
3257 DO NOT mark the cars.
|
|
3258
|
|
3259 Use only for internal lists! There should never be other pointers
|
|
3260 to the cons cells, because if so, the cars will remain unmarked
|
|
3261 even when they maybe should be marked. */
|
|
3262 void
|
|
3263 mark_conses_in_list (Lisp_Object obj)
|
|
3264 {
|
|
3265 Lisp_Object rest;
|
|
3266
|
|
3267 for (rest = obj; CONSP (rest); rest = XCDR (rest))
|
|
3268 {
|
|
3269 if (CONS_MARKED_P (XCONS (rest)))
|
|
3270 return;
|
|
3271 MARK_CONS (XCONS (rest));
|
|
3272 }
|
|
3273
|
|
3274 mark_object (rest);
|
|
3275 }
|
|
3276
|
|
3277
|
|
3278 #ifdef PURESTAT
|
183
|
3279 /* Simpler than mark-object, because pure structure can't
|
|
3280 have any circularities */
|
0
|
3281
|
|
3282 #if 0 /* unused */
|
|
3283 static int idiot_c_doesnt_have_closures;
|
|
3284 static void
|
|
3285 idiot_c (Lisp_Object obj)
|
|
3286 {
|
|
3287 idiot_c_doesnt_have_closures += pure_sizeof (obj, 1);
|
|
3288 }
|
|
3289 #endif /* unused */
|
|
3290
|
272
|
3291 static size_t
|
|
3292 pure_string_sizeof (Lisp_Object obj)
|
207
|
3293 {
|
|
3294 struct Lisp_String *ptr = XSTRING (obj);
|
272
|
3295
|
|
3296 if (string_data (ptr) != (Bufbyte *) ptr + sizeof (*ptr))
|
207
|
3297 {
|
|
3298 /* string-data not allocated contiguously.
|
|
3299 Probably (better be!!) a pointer constant "C" data. */
|
272
|
3300 return sizeof (*ptr);
|
207
|
3301 }
|
|
3302 else
|
|
3303 {
|
272
|
3304 size_t size = sizeof (*ptr) + string_length (ptr) + 1;
|
207
|
3305 size = ALIGN_SIZE (size, sizeof (Lisp_Object));
|
272
|
3306 return size;
|
207
|
3307 }
|
|
3308 }
|
|
3309
|
0
|
3310 /* recurse arg isn't actually used */
|
272
|
3311 static size_t
|
0
|
3312 pure_sizeof (Lisp_Object obj /*, int recurse */)
|
|
3313 {
|
272
|
3314 size_t total = 0;
|
0
|
3315
|
|
3316 /*tail_recurse: */
|
|
3317 if (!POINTER_TYPE_P (XTYPE (obj))
|
|
3318 || !PURIFIED (XPNTR (obj)))
|
173
|
3319 return total;
|
0
|
3320
|
|
3321 /* symbol's sizes are accounted for separately */
|
|
3322 if (SYMBOLP (obj))
|
173
|
3323 return total;
|
0
|
3324
|
|
3325 switch (XTYPE (obj))
|
|
3326 {
|
207
|
3327
|
|
3328 #ifndef LRECORD_STRING
|
185
|
3329 case Lisp_Type_String:
|
272
|
3330 total += pure_string_sizeof (obj);
|
0
|
3331 break;
|
207
|
3332 #endif /* ! LRECORD_STRING */
|
0
|
3333
|
185
|
3334 #ifndef LRECORD_VECTOR
|
|
3335 case Lisp_Type_Vector:
|
0
|
3336 {
|
|
3337 struct Lisp_Vector *ptr = XVECTOR (obj);
|
|
3338 int len = vector_length (ptr);
|
|
3339
|
|
3340 total += (sizeof (struct Lisp_Vector)
|
|
3341 + (len - 1) * sizeof (Lisp_Object));
|
|
3342 #if 0 /* unused */
|
|
3343 if (!recurse)
|
|
3344 break;
|
183
|
3345 {
|
0
|
3346 int i;
|
|
3347 for (i = 0; i < len - 1; i++)
|
|
3348 total += pure_sizeof (ptr->contents[i], 1);
|
|
3349 }
|
|
3350 if (len > 0)
|
|
3351 {
|
|
3352 obj = ptr->contents[len - 1];
|
|
3353 goto tail_recurse;
|
|
3354 }
|
|
3355 #endif /* unused */
|
|
3356 }
|
|
3357 break;
|
207
|
3358 #endif /* !LRECORD_VECTOR */
|
185
|
3359
|
|
3360 case Lisp_Type_Record:
|
0
|
3361 {
|
|
3362 struct lrecord_header *lheader = XRECORD_LHEADER (obj);
|
|
3363 CONST struct lrecord_implementation *implementation
|
211
|
3364 = LHEADER_IMPLEMENTATION (lheader);
|
0
|
3365
|
207
|
3366 #ifdef LRECORD_STRING
|
|
3367 if (STRINGP (obj))
|
|
3368 total += pure_string_sizeof (obj);
|
|
3369 else
|
|
3370 #endif
|
0
|
3371 if (implementation->size_in_bytes_method)
|
|
3372 total += ((implementation->size_in_bytes_method) (lheader));
|
|
3373 else
|
|
3374 total += implementation->static_size;
|
|
3375
|
|
3376 #if 0 /* unused */
|
|
3377 if (!recurse)
|
|
3378 break;
|
|
3379
|
|
3380 if (implementation->marker != 0)
|
|
3381 {
|
|
3382 int old = idiot_c_doesnt_have_closures;
|
|
3383
|
|
3384 idiot_c_doesnt_have_closures = 0;
|
|
3385 obj = ((implementation->marker) (obj, idiot_c));
|
|
3386 total += idiot_c_doesnt_have_closures;
|
|
3387 idiot_c_doesnt_have_closures = old;
|
183
|
3388
|
0
|
3389 if (!NILP (obj)) goto tail_recurse;
|
|
3390 }
|
|
3391 #endif /* unused */
|
|
3392 }
|
|
3393 break;
|
|
3394
|
207
|
3395 #ifndef LRECORD_CONS
|
185
|
3396 case Lisp_Type_Cons:
|
0
|
3397 {
|
|
3398 struct Lisp_Cons *ptr = XCONS (obj);
|
|
3399 total += sizeof (*ptr);
|
|
3400 #if 0 /* unused */
|
|
3401 if (!recurse)
|
|
3402 break;
|
|
3403 /* If the cdr is nil, tail-recurse on the car. */
|
|
3404 if (NILP (ptr->cdr))
|
|
3405 {
|
|
3406 obj = ptr->car;
|
|
3407 }
|
|
3408 else
|
|
3409 {
|
|
3410 total += pure_sizeof (ptr->car, 1);
|
|
3411 obj = ptr->cdr;
|
|
3412 }
|
|
3413 goto tail_recurse;
|
|
3414 #endif /* unused */
|
|
3415 }
|
|
3416 break;
|
207
|
3417 #endif
|
0
|
3418
|
|
3419 /* Others can't be purified */
|
|
3420 default:
|
|
3421 abort ();
|
|
3422 }
|
173
|
3423 return total;
|
0
|
3424 }
|
|
3425 #endif /* PURESTAT */
|
|
3426
|
|
3427
|
|
3428
|
|
3429
|
|
3430 /* Find all structures not marked, and free them. */
|
|
3431
|
207
|
3432 #ifndef LRECORD_VECTOR
|
0
|
3433 static int gc_count_num_vector_used, gc_count_vector_total_size;
|
|
3434 static int gc_count_vector_storage;
|
207
|
3435 #endif
|
0
|
3436 static int gc_count_num_bit_vector_used, gc_count_bit_vector_total_size;
|
|
3437 static int gc_count_bit_vector_storage;
|
|
3438 static int gc_count_num_short_string_in_use;
|
|
3439 static int gc_count_string_total_size;
|
|
3440 static int gc_count_short_string_total_size;
|
|
3441
|
|
3442 /* static int gc_count_total_records_used, gc_count_records_total_size; */
|
|
3443
|
|
3444
|
|
3445 /* This will be used more extensively In The Future */
|
|
3446 static int last_lrecord_type_index_assigned;
|
|
3447
|
211
|
3448 CONST struct lrecord_implementation *lrecord_implementations_table[128];
|
0
|
3449 #define max_lrecord_type (countof (lrecord_implementations_table) - 1)
|
|
3450
|
211
|
3451 int
|
0
|
3452 lrecord_type_index (CONST struct lrecord_implementation *implementation)
|
|
3453 {
|
|
3454 int type_index = *(implementation->lrecord_type_index);
|
272
|
3455 /* Have to do this circuitous validation test because of problems
|
0
|
3456 dumping out initialized variables (ie can't set xxx_type_index to -1
|
|
3457 because that would make xxx_type_index read-only in a dumped emacs. */
|
|
3458 if (type_index < 0 || type_index > max_lrecord_type
|
|
3459 || lrecord_implementations_table[type_index] != implementation)
|
|
3460 {
|
|
3461 if (last_lrecord_type_index_assigned == max_lrecord_type)
|
|
3462 abort ();
|
|
3463 type_index = ++last_lrecord_type_index_assigned;
|
|
3464 lrecord_implementations_table[type_index] = implementation;
|
|
3465 *(implementation->lrecord_type_index) = type_index;
|
|
3466 }
|
173
|
3467 return type_index;
|
0
|
3468 }
|
|
3469
|
|
3470 /* stats on lcrecords in use - kinda kludgy */
|
|
3471
|
|
3472 static struct
|
|
3473 {
|
|
3474 int instances_in_use;
|
|
3475 int bytes_in_use;
|
|
3476 int instances_freed;
|
|
3477 int bytes_freed;
|
|
3478 int instances_on_free_list;
|
|
3479 } lcrecord_stats [countof (lrecord_implementations_table)];
|
|
3480
|
|
3481
|
|
3482 static void
|
|
3483 reset_lcrecord_stats (void)
|
|
3484 {
|
|
3485 int i;
|
|
3486 for (i = 0; i < countof (lcrecord_stats); i++)
|
|
3487 {
|
|
3488 lcrecord_stats[i].instances_in_use = 0;
|
|
3489 lcrecord_stats[i].bytes_in_use = 0;
|
|
3490 lcrecord_stats[i].instances_freed = 0;
|
|
3491 lcrecord_stats[i].bytes_freed = 0;
|
|
3492 lcrecord_stats[i].instances_on_free_list = 0;
|
|
3493 }
|
|
3494 }
|
|
3495
|
|
3496 static void
|
|
3497 tick_lcrecord_stats (CONST struct lrecord_header *h, int free_p)
|
|
3498 {
|
211
|
3499 CONST struct lrecord_implementation *implementation =
|
|
3500 LHEADER_IMPLEMENTATION (h);
|
0
|
3501 int type_index = lrecord_type_index (implementation);
|
|
3502
|
|
3503 if (((struct lcrecord_header *) h)->free)
|
|
3504 {
|
|
3505 assert (!free_p);
|
|
3506 lcrecord_stats[type_index].instances_on_free_list++;
|
|
3507 }
|
|
3508 else
|
|
3509 {
|
272
|
3510 size_t sz = (implementation->size_in_bytes_method
|
|
3511 ? ((implementation->size_in_bytes_method) (h))
|
|
3512 : implementation->static_size);
|
0
|
3513
|
|
3514 if (free_p)
|
|
3515 {
|
|
3516 lcrecord_stats[type_index].instances_freed++;
|
|
3517 lcrecord_stats[type_index].bytes_freed += sz;
|
|
3518 }
|
|
3519 else
|
|
3520 {
|
|
3521 lcrecord_stats[type_index].instances_in_use++;
|
|
3522 lcrecord_stats[type_index].bytes_in_use += sz;
|
|
3523 }
|
|
3524 }
|
|
3525 }
|
|
3526
|
|
3527
|
|
3528 /* Free all unmarked records */
|
|
3529 static void
|
|
3530 sweep_lcrecords_1 (struct lcrecord_header **prev, int *used)
|
|
3531 {
|
|
3532 struct lcrecord_header *header;
|
|
3533 int num_used = 0;
|
|
3534 /* int total_size = 0; */
|
|
3535 reset_lcrecord_stats ();
|
|
3536
|
|
3537 /* First go through and call all the finalize methods.
|
|
3538 Then go through and free the objects. There used to
|
|
3539 be only one loop here, with the call to the finalizer
|
|
3540 occurring directly before the xfree() below. That
|
|
3541 is marginally faster but much less safe -- if the
|
|
3542 finalize method for an object needs to reference any
|
|
3543 other objects contained within it (and many do),
|
|
3544 we could easily be screwed by having already freed that
|
|
3545 other object. */
|
|
3546
|
|
3547 for (header = *prev; header; header = header->next)
|
|
3548 {
|
|
3549 struct lrecord_header *h = &(header->lheader);
|
|
3550 if (!MARKED_RECORD_HEADER_P (h) && ! (header->free))
|
|
3551 {
|
211
|
3552 if (LHEADER_IMPLEMENTATION (h)->finalizer)
|
|
3553 ((LHEADER_IMPLEMENTATION (h)->finalizer) (h, 0));
|
0
|
3554 }
|
|
3555 }
|
|
3556
|
|
3557 for (header = *prev; header; )
|
|
3558 {
|
|
3559 struct lrecord_header *h = &(header->lheader);
|
|
3560 if (MARKED_RECORD_HEADER_P (h))
|
|
3561 {
|
|
3562 UNMARK_RECORD_HEADER (h);
|
|
3563 num_used++;
|
|
3564 /* total_size += ((n->implementation->size_in_bytes) (h));*/
|
|
3565 prev = &(header->next);
|
|
3566 header = *prev;
|
|
3567 tick_lcrecord_stats (h, 0);
|
|
3568 }
|
|
3569 else
|
|
3570 {
|
|
3571 struct lcrecord_header *next = header->next;
|
|
3572 *prev = next;
|
|
3573 tick_lcrecord_stats (h, 1);
|
|
3574 /* used to call finalizer right here. */
|
|
3575 xfree (header);
|
|
3576 header = next;
|
|
3577 }
|
|
3578 }
|
|
3579 *used = num_used;
|
|
3580 /* *total = total_size; */
|
|
3581 }
|
|
3582
|
207
|
3583 #ifndef LRECORD_VECTOR
|
|
3584
|
0
|
3585 static void
|
183
|
3586 sweep_vectors_1 (Lisp_Object *prev,
|
0
|
3587 int *used, int *total, int *storage)
|
|
3588 {
|
|
3589 Lisp_Object vector;
|
|
3590 int num_used = 0;
|
|
3591 int total_size = 0;
|
|
3592 int total_storage = 0;
|
|
3593
|
|
3594 for (vector = *prev; VECTORP (vector); )
|
|
3595 {
|
|
3596 struct Lisp_Vector *v = XVECTOR (vector);
|
|
3597 int len = v->size;
|
|
3598 if (len < 0) /* marked */
|
|
3599 {
|
|
3600 len = - (len + 1);
|
|
3601 v->size = len;
|
|
3602 total_size += len;
|
|
3603 total_storage += (MALLOC_OVERHEAD
|
|
3604 + sizeof (struct Lisp_Vector)
|
|
3605 + (len - 1 + 1) * sizeof (Lisp_Object));
|
|
3606 num_used++;
|
|
3607 prev = &(vector_next (v));
|
|
3608 vector = *prev;
|
|
3609 }
|
|
3610 else
|
|
3611 {
|
|
3612 Lisp_Object next = vector_next (v);
|
|
3613 *prev = next;
|
|
3614 xfree (v);
|
|
3615 vector = next;
|
|
3616 }
|
|
3617 }
|
|
3618 *used = num_used;
|
|
3619 *total = total_size;
|
|
3620 *storage = total_storage;
|
|
3621 }
|
|
3622
|
207
|
3623 #endif /* ! LRECORD_VECTOR */
|
|
3624
|
0
|
3625 static void
|
183
|
3626 sweep_bit_vectors_1 (Lisp_Object *prev,
|
0
|
3627 int *used, int *total, int *storage)
|
|
3628 {
|
|
3629 Lisp_Object bit_vector;
|
|
3630 int num_used = 0;
|
|
3631 int total_size = 0;
|
|
3632 int total_storage = 0;
|
|
3633
|
|
3634 /* BIT_VECTORP fails because the objects are marked, which changes
|
|
3635 their implementation */
|
|
3636 for (bit_vector = *prev; !EQ (bit_vector, Qzero); )
|
|
3637 {
|
|
3638 struct Lisp_Bit_Vector *v = XBIT_VECTOR (bit_vector);
|
|
3639 int len = v->size;
|
|
3640 if (MARKED_RECORD_P (bit_vector))
|
|
3641 {
|
|
3642 UNMARK_RECORD_HEADER (&(v->lheader));
|
|
3643 total_size += len;
|
|
3644 total_storage += (MALLOC_OVERHEAD
|
|
3645 + sizeof (struct Lisp_Bit_Vector)
|
|
3646 + (BIT_VECTOR_LONG_STORAGE (len) - 1)
|
|
3647 * sizeof (long));
|
|
3648 num_used++;
|
|
3649 prev = &(bit_vector_next (v));
|
|
3650 bit_vector = *prev;
|
|
3651 }
|
|
3652 else
|
|
3653 {
|
|
3654 Lisp_Object next = bit_vector_next (v);
|
|
3655 *prev = next;
|
|
3656 xfree (v);
|
|
3657 bit_vector = next;
|
|
3658 }
|
|
3659 }
|
|
3660 *used = num_used;
|
|
3661 *total = total_size;
|
|
3662 *storage = total_storage;
|
|
3663 }
|
|
3664
|
|
3665 /* And the Lord said: Thou shalt use the `c-backslash-region' command
|
|
3666 to make macros prettier. */
|
|
3667
|
|
3668 #ifdef ERROR_CHECK_GC
|
|
3669
|
|
3670 #define SWEEP_FIXED_TYPE_BLOCK(typename, obj_type) \
|
|
3671 do { \
|
|
3672 struct typename##_block *_frob_current; \
|
|
3673 struct typename##_block **_frob_prev; \
|
|
3674 int _frob_limit; \
|
|
3675 int num_free = 0, num_used = 0; \
|
|
3676 \
|
|
3677 for (_frob_prev = ¤t_##typename##_block, \
|
|
3678 _frob_current = current_##typename##_block, \
|
|
3679 _frob_limit = current_##typename##_block_index; \
|
|
3680 _frob_current; \
|
|
3681 ) \
|
|
3682 { \
|
|
3683 int _frob_iii; \
|
|
3684 \
|
|
3685 for (_frob_iii = 0; _frob_iii < _frob_limit; _frob_iii++) \
|
|
3686 { \
|
|
3687 obj_type *_frob_victim = &(_frob_current->block[_frob_iii]); \
|
|
3688 \
|
|
3689 if (FREE_STRUCT_P (_frob_victim)) \
|
|
3690 { \
|
|
3691 num_free++; \
|
|
3692 } \
|
|
3693 else if (!MARKED_##typename##_P (_frob_victim)) \
|
|
3694 { \
|
|
3695 num_free++; \
|
|
3696 FREE_FIXED_TYPE (typename, obj_type, _frob_victim); \
|
|
3697 } \
|
|
3698 else \
|
|
3699 { \
|
|
3700 num_used++; \
|
|
3701 UNMARK_##typename (_frob_victim); \
|
|
3702 } \
|
|
3703 } \
|
|
3704 _frob_prev = &(_frob_current->prev); \
|
|
3705 _frob_current = _frob_current->prev; \
|
|
3706 _frob_limit = countof (current_##typename##_block->block); \
|
|
3707 } \
|
|
3708 \
|
|
3709 gc_count_num_##typename##_in_use = num_used; \
|
|
3710 gc_count_num_##typename##_freelist = num_free; \
|
|
3711 } while (0)
|
|
3712
|
183
|
3713 #else /* !ERROR_CHECK_GC */
|
0
|
3714
|
|
3715 #define SWEEP_FIXED_TYPE_BLOCK(typename, obj_type) \
|
|
3716 do { \
|
|
3717 struct typename##_block *_frob_current; \
|
|
3718 struct typename##_block **_frob_prev; \
|
|
3719 int _frob_limit; \
|
|
3720 int num_free = 0, num_used = 0; \
|
|
3721 \
|
|
3722 typename##_free_list = 0; \
|
|
3723 \
|
|
3724 for (_frob_prev = ¤t_##typename##_block, \
|
|
3725 _frob_current = current_##typename##_block, \
|
|
3726 _frob_limit = current_##typename##_block_index; \
|
|
3727 _frob_current; \
|
|
3728 ) \
|
|
3729 { \
|
|
3730 int _frob_iii; \
|
|
3731 int _frob_empty = 1; \
|
|
3732 obj_type *_frob_old_free_list = typename##_free_list; \
|
|
3733 \
|
|
3734 for (_frob_iii = 0; _frob_iii < _frob_limit; _frob_iii++) \
|
|
3735 { \
|
|
3736 obj_type *_frob_victim = &(_frob_current->block[_frob_iii]); \
|
|
3737 \
|
|
3738 if (FREE_STRUCT_P (_frob_victim)) \
|
|
3739 { \
|
|
3740 num_free++; \
|
|
3741 PUT_FIXED_TYPE_ON_FREE_LIST (typename, obj_type, _frob_victim); \
|
|
3742 } \
|
|
3743 else if (!MARKED_##typename##_P (_frob_victim)) \
|
|
3744 { \
|
|
3745 num_free++; \
|
|
3746 FREE_FIXED_TYPE (typename, obj_type, _frob_victim); \
|
|
3747 } \
|
|
3748 else \
|
|
3749 { \
|
|
3750 _frob_empty = 0; \
|
|
3751 num_used++; \
|
|
3752 UNMARK_##typename (_frob_victim); \
|
|
3753 } \
|
|
3754 } \
|
|
3755 if (!_frob_empty) \
|
|
3756 { \
|
|
3757 _frob_prev = &(_frob_current->prev); \
|
|
3758 _frob_current = _frob_current->prev; \
|
|
3759 } \
|
|
3760 else if (_frob_current == current_##typename##_block \
|
|
3761 && !_frob_current->prev) \
|
|
3762 { \
|
|
3763 /* No real point in freeing sole allocation block */ \
|
|
3764 break; \
|
|
3765 } \
|
|
3766 else \
|
|
3767 { \
|
|
3768 struct typename##_block *_frob_victim_block = _frob_current; \
|
|
3769 if (_frob_victim_block == current_##typename##_block) \
|
|
3770 current_##typename##_block_index \
|
|
3771 = countof (current_##typename##_block->block); \
|
|
3772 _frob_current = _frob_current->prev; \
|
|
3773 { \
|
|
3774 *_frob_prev = _frob_current; \
|
|
3775 xfree (_frob_victim_block); \
|
|
3776 /* Restore free list to what it was before victim was swept */ \
|
|
3777 typename##_free_list = _frob_old_free_list; \
|
|
3778 num_free -= _frob_limit; \
|
|
3779 } \
|
|
3780 } \
|
|
3781 _frob_limit = countof (current_##typename##_block->block); \
|
|
3782 } \
|
|
3783 \
|
|
3784 gc_count_num_##typename##_in_use = num_used; \
|
|
3785 gc_count_num_##typename##_freelist = num_free; \
|
|
3786 } while (0)
|
|
3787
|
183
|
3788 #endif /* !ERROR_CHECK_GC */
|
0
|
3789
|
|
3790
|
|
3791
|
|
3792
|
|
3793 static void
|
|
3794 sweep_conses (void)
|
|
3795 {
|
207
|
3796 #ifndef LRECORD_CONS
|
|
3797 # define MARKED_cons_P(ptr) XMARKBIT ((ptr)->car)
|
|
3798 # define UNMARK_cons(ptr) do { XUNMARK ((ptr)->car); } while (0)
|
|
3799 #else /* LRECORD_CONS */
|
|
3800 # define MARKED_cons_P(ptr) MARKED_RECORD_HEADER_P (&((ptr)->lheader))
|
|
3801 # define UNMARK_cons(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
|
|
3802 #endif /* LRECORD_CONS */
|
0
|
3803 #define ADDITIONAL_FREE_cons(ptr)
|
|
3804
|
|
3805 SWEEP_FIXED_TYPE_BLOCK (cons, struct Lisp_Cons);
|
|
3806 }
|
|
3807
|
|
3808 /* Explicitly free a cons cell. */
|
|
3809 void
|
|
3810 free_cons (struct Lisp_Cons *ptr)
|
|
3811 {
|
|
3812 #ifdef ERROR_CHECK_GC
|
|
3813 /* If the CAR is not an int, then it will be a pointer, which will
|
|
3814 always be four-byte aligned. If this cons cell has already been
|
|
3815 placed on the free list, however, its car will probably contain
|
|
3816 a chain pointer to the next cons on the list, which has cleverly
|
|
3817 had all its 0's and 1's inverted. This allows for a quick
|
|
3818 check to make sure we're not freeing something already freed. */
|
|
3819 if (POINTER_TYPE_P (XTYPE (ptr->car)))
|
|
3820 ASSERT_VALID_POINTER (XPNTR (ptr->car));
|
183
|
3821 #endif /* ERROR_CHECK_GC */
|
|
3822
|
0
|
3823 #ifndef ALLOC_NO_POOLS
|
|
3824 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (cons, struct Lisp_Cons, ptr);
|
|
3825 #endif /* ALLOC_NO_POOLS */
|
|
3826 }
|
|
3827
|
|
3828 /* explicitly free a list. You **must make sure** that you have
|
|
3829 created all the cons cells that make up this list and that there
|
|
3830 are no pointers to any of these cons cells anywhere else. If there
|
|
3831 are, you will lose. */
|
|
3832
|
|
3833 void
|
|
3834 free_list (Lisp_Object list)
|
|
3835 {
|
|
3836 Lisp_Object rest, next;
|
|
3837
|
|
3838 for (rest = list; !NILP (rest); rest = next)
|
|
3839 {
|
|
3840 next = XCDR (rest);
|
|
3841 free_cons (XCONS (rest));
|
|
3842 }
|
|
3843 }
|
|
3844
|
|
3845 /* explicitly free an alist. You **must make sure** that you have
|
|
3846 created all the cons cells that make up this alist and that there
|
|
3847 are no pointers to any of these cons cells anywhere else. If there
|
|
3848 are, you will lose. */
|
|
3849
|
|
3850 void
|
|
3851 free_alist (Lisp_Object alist)
|
|
3852 {
|
|
3853 Lisp_Object rest, next;
|
|
3854
|
|
3855 for (rest = alist; !NILP (rest); rest = next)
|
|
3856 {
|
|
3857 next = XCDR (rest);
|
|
3858 free_cons (XCONS (XCAR (rest)));
|
|
3859 free_cons (XCONS (rest));
|
|
3860 }
|
|
3861 }
|
|
3862
|
|
3863 static void
|
|
3864 sweep_compiled_functions (void)
|
|
3865 {
|
|
3866 #define MARKED_compiled_function_P(ptr) \
|
|
3867 MARKED_RECORD_HEADER_P (&((ptr)->lheader))
|
|
3868 #define UNMARK_compiled_function(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
|
|
3869 #define ADDITIONAL_FREE_compiled_function(ptr)
|
|
3870
|
|
3871 SWEEP_FIXED_TYPE_BLOCK (compiled_function, struct Lisp_Compiled_Function);
|
|
3872 }
|
|
3873
|
|
3874
|
|
3875 #ifdef LISP_FLOAT_TYPE
|
|
3876 static void
|
|
3877 sweep_floats (void)
|
|
3878 {
|
|
3879 #define MARKED_float_P(ptr) MARKED_RECORD_HEADER_P (&((ptr)->lheader))
|
|
3880 #define UNMARK_float(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
|
|
3881 #define ADDITIONAL_FREE_float(ptr)
|
|
3882
|
|
3883 SWEEP_FIXED_TYPE_BLOCK (float, struct Lisp_Float);
|
|
3884 }
|
|
3885 #endif /* LISP_FLOAT_TYPE */
|
|
3886
|
|
3887 static void
|
|
3888 sweep_symbols (void)
|
|
3889 {
|
|
3890 #ifndef LRECORD_SYMBOL
|
|
3891 # define MARKED_symbol_P(ptr) XMARKBIT ((ptr)->plist)
|
|
3892 # define UNMARK_symbol(ptr) do { XUNMARK ((ptr)->plist); } while (0)
|
|
3893 #else
|
|
3894 # define MARKED_symbol_P(ptr) MARKED_RECORD_HEADER_P (&((ptr)->lheader))
|
|
3895 # define UNMARK_symbol(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
|
|
3896 #endif /* !LRECORD_SYMBOL */
|
|
3897 #define ADDITIONAL_FREE_symbol(ptr)
|
|
3898
|
|
3899 SWEEP_FIXED_TYPE_BLOCK (symbol, struct Lisp_Symbol);
|
|
3900 }
|
|
3901
|
|
3902
|
|
3903 #ifndef standalone
|
|
3904
|
|
3905 static void
|
|
3906 sweep_extents (void)
|
|
3907 {
|
|
3908 #define MARKED_extent_P(ptr) MARKED_RECORD_HEADER_P (&((ptr)->lheader))
|
|
3909 #define UNMARK_extent(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
|
|
3910 #define ADDITIONAL_FREE_extent(ptr)
|
|
3911
|
|
3912 SWEEP_FIXED_TYPE_BLOCK (extent, struct extent);
|
|
3913 }
|
|
3914
|
|
3915 static void
|
|
3916 sweep_events (void)
|
|
3917 {
|
|
3918 #define MARKED_event_P(ptr) MARKED_RECORD_HEADER_P (&((ptr)->lheader))
|
|
3919 #define UNMARK_event(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
|
|
3920 #define ADDITIONAL_FREE_event(ptr)
|
|
3921
|
|
3922 SWEEP_FIXED_TYPE_BLOCK (event, struct Lisp_Event);
|
|
3923 }
|
|
3924
|
|
3925 static void
|
|
3926 sweep_markers (void)
|
|
3927 {
|
|
3928 #define MARKED_marker_P(ptr) MARKED_RECORD_HEADER_P (&((ptr)->lheader))
|
|
3929 #define UNMARK_marker(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
|
|
3930 #define ADDITIONAL_FREE_marker(ptr) \
|
|
3931 do { Lisp_Object tem; \
|
|
3932 XSETMARKER (tem, ptr); \
|
|
3933 unchain_marker (tem); \
|
|
3934 } while (0)
|
|
3935
|
|
3936 SWEEP_FIXED_TYPE_BLOCK (marker, struct Lisp_Marker);
|
|
3937 }
|
|
3938
|
|
3939 /* Explicitly free a marker. */
|
|
3940 void
|
|
3941 free_marker (struct Lisp_Marker *ptr)
|
|
3942 {
|
|
3943 #ifdef ERROR_CHECK_GC
|
|
3944 /* Perhaps this will catch freeing an already-freed marker. */
|
|
3945 Lisp_Object temmy;
|
|
3946 XSETMARKER (temmy, ptr);
|
|
3947 assert (GC_MARKERP (temmy));
|
183
|
3948 #endif /* ERROR_CHECK_GC */
|
|
3949
|
0
|
3950 #ifndef ALLOC_NO_POOLS
|
|
3951 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (marker, struct Lisp_Marker, ptr);
|
|
3952 #endif /* ALLOC_NO_POOLS */
|
|
3953 }
|
|
3954
|
|
3955 #endif /* not standalone */
|
|
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 #ifndef standalone
|
|
5014 init_marker_alloc ();
|
|
5015 init_extent_alloc ();
|
|
5016 init_event_alloc ();
|
|
5017 #endif
|
|
5018 ignore_malloc_warnings = 0;
|
|
5019 staticidx = 0;
|
|
5020 consing_since_gc = 0;
|
|
5021 #if 1
|
|
5022 gc_cons_threshold = 500000; /* XEmacs change */
|
|
5023 #else
|
|
5024 gc_cons_threshold = 15000; /* debugging */
|
|
5025 #endif
|
|
5026 #ifdef VIRT_ADDR_VARIES
|
|
5027 malloc_sbrk_unused = 1<<22; /* A large number */
|
|
5028 malloc_sbrk_used = 100000; /* as reasonable as any number */
|
|
5029 #endif /* VIRT_ADDR_VARIES */
|
|
5030 lrecord_uid_counter = 259;
|
|
5031 debug_string_purity = 0;
|
|
5032 gcprolist = 0;
|
|
5033
|
|
5034 gc_currently_forbidden = 0;
|
|
5035 gc_hooks_inhibited = 0;
|
|
5036
|
|
5037 #ifdef ERROR_CHECK_TYPECHECK
|
|
5038 ERROR_ME.really_unlikely_name_to_have_accidentally_in_a_non_errb_structure =
|
|
5039 666;
|
|
5040 ERROR_ME_NOT.
|
|
5041 really_unlikely_name_to_have_accidentally_in_a_non_errb_structure = 42;
|
|
5042 ERROR_ME_WARN.
|
|
5043 really_unlikely_name_to_have_accidentally_in_a_non_errb_structure =
|
|
5044 3333632;
|
183
|
5045 #endif /* ERROR_CHECK_TYPECHECK */
|
0
|
5046 }
|
|
5047
|
|
5048 void
|
|
5049 reinit_alloc (void)
|
|
5050 {
|
|
5051 gcprolist = 0;
|
|
5052 }
|
|
5053
|
|
5054 void
|
|
5055 syms_of_alloc (void)
|
|
5056 {
|
|
5057 defsymbol (&Qpre_gc_hook, "pre-gc-hook");
|
|
5058 defsymbol (&Qpost_gc_hook, "post-gc-hook");
|
|
5059 defsymbol (&Qgarbage_collecting, "garbage-collecting");
|
|
5060
|
20
|
5061 DEFSUBR (Fcons);
|
|
5062 DEFSUBR (Flist);
|
|
5063 DEFSUBR (Fvector);
|
|
5064 DEFSUBR (Fbit_vector);
|
|
5065 DEFSUBR (Fmake_byte_code);
|
|
5066 DEFSUBR (Fmake_list);
|
|
5067 DEFSUBR (Fmake_vector);
|
|
5068 DEFSUBR (Fmake_bit_vector);
|
|
5069 DEFSUBR (Fmake_string);
|
|
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 }
|