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