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 {
|
|
1197 struct Lisp_Vector *p = lheader;
|
|
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;
|
|
1786 symbol_next (p) = 0;
|
|
1787 XSETSYMBOL (val, p);
|
|
1788 return val;
|
|
1789 }
|
|
1790
|
|
1791
|
|
1792 /**********************************************************************/
|
|
1793 /* Extent allocation */
|
|
1794 /**********************************************************************/
|
|
1795
|
|
1796 DECLARE_FIXED_TYPE_ALLOC (extent, struct extent);
|
|
1797 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_extent 1000
|
|
1798
|
|
1799 struct extent *
|
|
1800 allocate_extent (void)
|
|
1801 {
|
|
1802 struct extent *e;
|
|
1803
|
|
1804 ALLOCATE_FIXED_TYPE (extent, struct extent, e);
|
|
1805 /* memset (e, 0, sizeof (struct extent)); */
|
|
1806 set_lheader_implementation (&(e->lheader), lrecord_extent);
|
|
1807 extent_object (e) = Qnil;
|
|
1808 set_extent_start (e, -1);
|
|
1809 set_extent_end (e, -1);
|
|
1810 e->plist = Qnil;
|
|
1811
|
|
1812 memset (&e->flags, 0, sizeof (e->flags));
|
|
1813
|
|
1814 extent_face (e) = Qnil;
|
|
1815 e->flags.end_open = 1; /* default is for endpoints to behave like markers */
|
|
1816 e->flags.detachable = 1;
|
|
1817
|
173
|
1818 return e;
|
0
|
1819 }
|
|
1820
|
|
1821
|
|
1822 /**********************************************************************/
|
|
1823 /* Event allocation */
|
|
1824 /**********************************************************************/
|
|
1825
|
|
1826 DECLARE_FIXED_TYPE_ALLOC (event, struct Lisp_Event);
|
|
1827 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_event 1000
|
|
1828
|
|
1829 Lisp_Object
|
|
1830 allocate_event (void)
|
|
1831 {
|
|
1832 Lisp_Object val;
|
|
1833 struct Lisp_Event *e;
|
|
1834
|
|
1835 ALLOCATE_FIXED_TYPE (event, struct Lisp_Event, e);
|
|
1836 set_lheader_implementation (&(e->lheader), lrecord_event);
|
|
1837
|
|
1838 XSETEVENT (val, e);
|
|
1839 return val;
|
|
1840 }
|
183
|
1841
|
0
|
1842
|
|
1843 /**********************************************************************/
|
|
1844 /* Marker allocation */
|
|
1845 /**********************************************************************/
|
|
1846
|
|
1847 DECLARE_FIXED_TYPE_ALLOC (marker, struct Lisp_Marker);
|
|
1848 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_marker 1000
|
|
1849
|
20
|
1850 DEFUN ("make-marker", Fmake_marker, 0, 0, 0, /*
|
0
|
1851 Return a newly allocated marker which does not point at any place.
|
20
|
1852 */
|
|
1853 ())
|
0
|
1854 {
|
|
1855 Lisp_Object val;
|
|
1856 struct Lisp_Marker *p;
|
|
1857
|
|
1858 ALLOCATE_FIXED_TYPE (marker, struct Lisp_Marker, p);
|
|
1859 set_lheader_implementation (&(p->lheader), lrecord_marker);
|
|
1860 p->buffer = 0;
|
|
1861 p->memind = 0;
|
|
1862 marker_next (p) = 0;
|
|
1863 marker_prev (p) = 0;
|
|
1864 p->insertion_type = 0;
|
|
1865 XSETMARKER (val, p);
|
|
1866 return val;
|
|
1867 }
|
|
1868
|
|
1869 Lisp_Object
|
|
1870 noseeum_make_marker (void)
|
|
1871 {
|
|
1872 Lisp_Object val;
|
|
1873 struct Lisp_Marker *p;
|
|
1874
|
|
1875 NOSEEUM_ALLOCATE_FIXED_TYPE (marker, struct Lisp_Marker, p);
|
|
1876 set_lheader_implementation (&(p->lheader), lrecord_marker);
|
|
1877 p->buffer = 0;
|
|
1878 p->memind = 0;
|
|
1879 marker_next (p) = 0;
|
|
1880 marker_prev (p) = 0;
|
|
1881 p->insertion_type = 0;
|
|
1882 XSETMARKER (val, p);
|
|
1883 return val;
|
|
1884 }
|
|
1885
|
|
1886
|
|
1887 /**********************************************************************/
|
|
1888 /* String allocation */
|
|
1889 /**********************************************************************/
|
|
1890
|
183
|
1891 /* The data for "short" strings generally resides inside of structs of type
|
|
1892 string_chars_block. The Lisp_String structure is allocated just like any
|
0
|
1893 other Lisp object (except for vectors), and these are freelisted when
|
|
1894 they get garbage collected. The data for short strings get compacted,
|
183
|
1895 but the data for large strings do not.
|
0
|
1896
|
|
1897 Previously Lisp_String structures were relocated, but this caused a lot
|
|
1898 of bus-errors because the C code didn't include enough GCPRO's for
|
|
1899 strings (since EVERY REFERENCE to a short string needed to be GCPRO'd so
|
|
1900 that the reference would get relocated).
|
|
1901
|
|
1902 This new method makes things somewhat bigger, but it is MUCH safer. */
|
|
1903
|
|
1904 DECLARE_FIXED_TYPE_ALLOC (string, struct Lisp_String);
|
|
1905 /* strings are used and freed quite often */
|
|
1906 /* #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_string 10000 */
|
|
1907 #define MINIMUM_ALLOWED_FIXED_TYPE_CELLS_string 1000
|
|
1908
|
207
|
1909 #ifdef LRECORD_STRING
|
|
1910 static Lisp_Object
|
|
1911 mark_string (Lisp_Object obj, void (*markobj) (Lisp_Object))
|
|
1912 {
|
|
1913 struct Lisp_String *ptr = XSTRING (obj);
|
|
1914
|
|
1915 if (GC_CONSP (ptr->plist) && GC_EXTENT_INFOP (XCAR (ptr->plist)))
|
|
1916 flush_cached_extent_info (XCAR (ptr->plist));
|
|
1917 return ptr->plist;
|
|
1918 }
|
|
1919
|
|
1920 static int
|
|
1921 string_equal (Lisp_Object o1, Lisp_Object o2, int depth)
|
|
1922 {
|
|
1923 Bytecount len = XSTRING_LENGTH (o1);
|
|
1924 if (len != XSTRING_LENGTH (o2))
|
|
1925 return 0;
|
|
1926 if (memcmp (XSTRING_DATA (o1), XSTRING_DATA (o2), len))
|
|
1927 return 0;
|
|
1928 return 1;
|
|
1929 }
|
243
|
1930
|
|
1931 DEFINE_BASIC_LRECORD_IMPLEMENTATION ("string", string,
|
|
1932 mark_string, print_string,
|
|
1933 /*
|
|
1934 * No `finalize', or `hash' methods.
|
|
1935 * internal_hash already knows how
|
|
1936 * to hash strings and finalization
|
|
1937 * is done with the
|
|
1938 * ADDITIONAL_FREE_string macro,
|
|
1939 * which is the standard way to do
|
|
1940 * finalization when using
|
|
1941 * SWEEP_FIXED_TYPE_BLOCK().
|
|
1942 */
|
|
1943 0, string_equal, 0,
|
|
1944 struct Lisp_String);
|
207
|
1945 #endif /* LRECORD_STRING */
|
|
1946
|
0
|
1947 /* String blocks contain this many useful bytes. */
|
|
1948 #define STRING_CHARS_BLOCK_SIZE \
|
|
1949 (8192 - MALLOC_OVERHEAD - ((2 * sizeof (struct string_chars_block *)) \
|
|
1950 + sizeof (EMACS_INT)))
|
|
1951 /* Block header for small strings. */
|
|
1952 struct string_chars_block
|
|
1953 {
|
|
1954 EMACS_INT pos;
|
|
1955 struct string_chars_block *next;
|
|
1956 struct string_chars_block *prev;
|
|
1957 /* Contents of string_chars_block->string_chars are interleaved
|
|
1958 string_chars structures (see below) and the actual string data */
|
|
1959 unsigned char string_chars[STRING_CHARS_BLOCK_SIZE];
|
|
1960 };
|
|
1961
|
|
1962 struct string_chars_block *first_string_chars_block;
|
|
1963 struct string_chars_block *current_string_chars_block;
|
|
1964
|
|
1965 /* If SIZE is the length of a string, this returns how many bytes
|
|
1966 * the string occupies in string_chars_block->string_chars
|
|
1967 * (including alignment padding).
|
|
1968 */
|
|
1969 #define STRING_FULLSIZE(s) \
|
|
1970 ALIGN_SIZE (((s) + 1 + sizeof (struct Lisp_String *)),\
|
|
1971 ALIGNOF (struct Lisp_String *))
|
|
1972
|
|
1973 #define BIG_STRING_FULLSIZE_P(fullsize) ((fullsize) >= STRING_CHARS_BLOCK_SIZE)
|
|
1974 #define BIG_STRING_SIZE_P(size) (BIG_STRING_FULLSIZE_P (STRING_FULLSIZE(size)))
|
|
1975
|
|
1976 #define CHARS_TO_STRING_CHAR(x) \
|
|
1977 ((struct string_chars *) \
|
173
|
1978 (((char *) (x)) - (slot_offset (struct string_chars, chars[0]))))
|
0
|
1979
|
|
1980
|
|
1981 struct string_chars
|
|
1982 {
|
|
1983 struct Lisp_String *string;
|
|
1984 unsigned char chars[1];
|
|
1985 };
|
|
1986
|
|
1987 struct unused_string_chars
|
|
1988 {
|
|
1989 struct Lisp_String *string;
|
|
1990 EMACS_INT fullsize;
|
|
1991 };
|
|
1992
|
|
1993 static void
|
|
1994 init_string_chars_alloc (void)
|
|
1995 {
|
185
|
1996 first_string_chars_block = xnew (struct string_chars_block);
|
0
|
1997 first_string_chars_block->prev = 0;
|
|
1998 first_string_chars_block->next = 0;
|
|
1999 first_string_chars_block->pos = 0;
|
|
2000 current_string_chars_block = first_string_chars_block;
|
|
2001 }
|
|
2002
|
|
2003 static struct string_chars *
|
|
2004 allocate_string_chars_struct (struct Lisp_String *string_it_goes_with,
|
|
2005 EMACS_INT fullsize)
|
|
2006 {
|
|
2007 struct string_chars *s_chars;
|
|
2008
|
|
2009 /* Allocate the string's actual data */
|
|
2010 if (BIG_STRING_FULLSIZE_P (fullsize))
|
|
2011 {
|
|
2012 s_chars = (struct string_chars *) xmalloc (fullsize);
|
|
2013 }
|
|
2014 else if (fullsize <=
|
|
2015 (countof (current_string_chars_block->string_chars)
|
|
2016 - current_string_chars_block->pos))
|
|
2017 {
|
|
2018 /* This string can fit in the current string chars block */
|
|
2019 s_chars = (struct string_chars *)
|
|
2020 (current_string_chars_block->string_chars
|
|
2021 + current_string_chars_block->pos);
|
|
2022 current_string_chars_block->pos += fullsize;
|
|
2023 }
|
|
2024 else
|
|
2025 {
|
|
2026 /* Make a new current string chars block */
|
185
|
2027 struct string_chars_block *new = xnew (struct string_chars_block);
|
183
|
2028
|
0
|
2029 current_string_chars_block->next = new;
|
|
2030 new->prev = current_string_chars_block;
|
|
2031 new->next = 0;
|
|
2032 current_string_chars_block = new;
|
|
2033 new->pos = fullsize;
|
|
2034 s_chars = (struct string_chars *)
|
|
2035 current_string_chars_block->string_chars;
|
|
2036 }
|
|
2037
|
|
2038 s_chars->string = string_it_goes_with;
|
|
2039
|
|
2040 INCREMENT_CONS_COUNTER (fullsize, "string chars");
|
|
2041
|
|
2042 return s_chars;
|
|
2043 }
|
|
2044
|
|
2045 Lisp_Object
|
|
2046 make_uninit_string (Bytecount length)
|
|
2047 {
|
|
2048 struct Lisp_String *s;
|
|
2049 struct string_chars *s_chars;
|
|
2050 EMACS_INT fullsize = STRING_FULLSIZE (length);
|
|
2051 Lisp_Object val;
|
|
2052
|
|
2053 if ((length < 0) || (fullsize <= 0))
|
|
2054 abort ();
|
|
2055
|
|
2056 /* Allocate the string header */
|
|
2057 ALLOCATE_FIXED_TYPE (string, struct Lisp_String, s);
|
207
|
2058 #ifdef LRECORD_STRING
|
|
2059 set_lheader_implementation (&(s->lheader), lrecord_string);
|
|
2060 #endif
|
0
|
2061
|
|
2062 s_chars = allocate_string_chars_struct (s, fullsize);
|
|
2063
|
|
2064 set_string_data (s, &(s_chars->chars[0]));
|
|
2065 set_string_length (s, length);
|
|
2066 s->plist = Qnil;
|
183
|
2067
|
0
|
2068 set_string_byte (s, length, 0);
|
|
2069
|
|
2070 XSETSTRING (val, s);
|
173
|
2071 return val;
|
0
|
2072 }
|
|
2073
|
|
2074 #ifdef VERIFY_STRING_CHARS_INTEGRITY
|
|
2075 static void verify_string_chars_integrity (void);
|
|
2076 #endif
|
183
|
2077
|
0
|
2078 /* Resize the string S so that DELTA bytes can be inserted starting
|
|
2079 at POS. If DELTA < 0, it means deletion starting at POS. If
|
|
2080 POS < 0, resize the string but don't copy any characters. Use
|
|
2081 this if you're planning on completely overwriting the string.
|
|
2082 */
|
|
2083
|
|
2084 void
|
|
2085 resize_string (struct Lisp_String *s, Bytecount pos, Bytecount delta)
|
|
2086 {
|
|
2087 #ifdef VERIFY_STRING_CHARS_INTEGRITY
|
|
2088 verify_string_chars_integrity ();
|
|
2089 #endif
|
|
2090
|
|
2091 #ifdef ERROR_CHECK_BUFPOS
|
|
2092 if (pos >= 0)
|
|
2093 {
|
|
2094 assert (pos <= string_length (s));
|
|
2095 if (delta < 0)
|
|
2096 assert (pos + (-delta) <= string_length (s));
|
|
2097 }
|
|
2098 else
|
|
2099 {
|
|
2100 if (delta < 0)
|
|
2101 assert ((-delta) <= string_length (s));
|
|
2102 }
|
183
|
2103 #endif /* ERROR_CHECK_BUFPOS */
|
0
|
2104
|
|
2105 if (pos >= 0 && delta < 0)
|
|
2106 /* If DELTA < 0, the functions below will delete the characters
|
|
2107 before POS. We want to delete characters *after* POS, however,
|
|
2108 so convert this to the appropriate form. */
|
|
2109 pos += -delta;
|
|
2110
|
|
2111 if (delta == 0)
|
|
2112 /* simplest case: no size change. */
|
|
2113 return;
|
|
2114 else
|
|
2115 {
|
|
2116 EMACS_INT oldfullsize = STRING_FULLSIZE (string_length (s));
|
|
2117 EMACS_INT newfullsize = STRING_FULLSIZE (string_length (s) + delta);
|
|
2118
|
|
2119 if (oldfullsize == newfullsize)
|
|
2120 {
|
|
2121 /* next simplest case; size change but the necessary
|
|
2122 allocation size won't change (up or down; code somewhere
|
|
2123 depends on there not being any unused allocation space,
|
|
2124 modulo any alignment constraints). */
|
|
2125 if (pos >= 0)
|
|
2126 {
|
183
|
2127 Bufbyte *addroff = pos + string_data (s);
|
0
|
2128
|
|
2129 memmove (addroff + delta, addroff,
|
|
2130 /* +1 due to zero-termination. */
|
|
2131 string_length (s) + 1 - pos);
|
|
2132 }
|
|
2133 }
|
|
2134 else if (BIG_STRING_FULLSIZE_P (oldfullsize) &&
|
|
2135 BIG_STRING_FULLSIZE_P (newfullsize))
|
|
2136 {
|
|
2137 /* next simplest case; the string is big enough to be malloc()ed
|
|
2138 itself, so we just realloc.
|
|
2139
|
|
2140 It's important not to let the string get below the threshold
|
|
2141 for making big strings and still remain malloc()ed; if that
|
|
2142 were the case, repeated calls to this function on the same
|
|
2143 string could result in memory leakage. */
|
|
2144 set_string_data (s, (Bufbyte *) xrealloc (string_data (s),
|
|
2145 newfullsize));
|
|
2146 if (pos >= 0)
|
|
2147 {
|
183
|
2148 Bufbyte *addroff = pos + string_data (s);
|
0
|
2149
|
|
2150 memmove (addroff + delta, addroff,
|
|
2151 /* +1 due to zero-termination. */
|
|
2152 string_length (s) + 1 - pos);
|
|
2153 }
|
|
2154 }
|
|
2155 else
|
|
2156 {
|
|
2157 /* worst case. We make a new string_chars struct and copy
|
|
2158 the string's data into it, inserting/deleting the delta
|
|
2159 in the process. The old string data will either get
|
|
2160 freed by us (if it was malloc()ed) or will be reclaimed
|
|
2161 in the normal course of garbage collection. */
|
|
2162 struct string_chars *s_chars =
|
|
2163 allocate_string_chars_struct (s, newfullsize);
|
|
2164 Bufbyte *new_addr = &(s_chars->chars[0]);
|
|
2165 Bufbyte *old_addr = string_data (s);
|
|
2166 if (pos >= 0)
|
|
2167 {
|
|
2168 memcpy (new_addr, old_addr, pos);
|
|
2169 memcpy (new_addr + pos + delta, old_addr + pos,
|
|
2170 string_length (s) + 1 - pos);
|
|
2171 }
|
|
2172 set_string_data (s, new_addr);
|
|
2173 if (BIG_STRING_FULLSIZE_P (oldfullsize))
|
|
2174 xfree (old_addr);
|
|
2175 else
|
|
2176 {
|
|
2177 /* We need to mark this chunk of the string_chars_block
|
|
2178 as unused so that compact_string_chars() doesn't
|
|
2179 freak. */
|
|
2180 struct string_chars *old_s_chars =
|
|
2181 (struct string_chars *) ((char *) old_addr -
|
|
2182 sizeof (struct Lisp_String *));
|
|
2183 /* Sanity check to make sure we aren't hosed by strange
|
|
2184 alignment/padding. */
|
|
2185 assert (old_s_chars->string == s);
|
|
2186 MARK_STRUCT_AS_FREE (old_s_chars);
|
|
2187 ((struct unused_string_chars *) old_s_chars)->fullsize =
|
|
2188 oldfullsize;
|
|
2189 }
|
|
2190 }
|
|
2191
|
|
2192 set_string_length (s, string_length (s) + delta);
|
|
2193 /* If pos < 0, the string won't be zero-terminated.
|
|
2194 Terminate now just to make sure. */
|
|
2195 string_data (s)[string_length (s)] = '\0';
|
|
2196
|
|
2197 if (pos >= 0)
|
|
2198 {
|
|
2199 Lisp_Object string = Qnil;
|
|
2200
|
|
2201 XSETSTRING (string, s);
|
|
2202 /* We also have to adjust all of the extent indices after the
|
|
2203 place we did the change. We say "pos - 1" because
|
|
2204 adjust_extents() is exclusive of the starting position
|
|
2205 passed to it. */
|
|
2206 adjust_extents (string, pos - 1, string_length (s),
|
|
2207 delta);
|
|
2208 }
|
|
2209 }
|
|
2210
|
|
2211 #ifdef VERIFY_STRING_CHARS_INTEGRITY
|
|
2212 verify_string_chars_integrity ();
|
|
2213 #endif
|
|
2214 }
|
|
2215
|
70
|
2216 #ifdef MULE
|
|
2217
|
|
2218 void
|
|
2219 set_string_char (struct Lisp_String *s, Charcount i, Emchar c)
|
|
2220 {
|
|
2221 Bytecount oldlen, newlen;
|
|
2222 Bufbyte newstr[MAX_EMCHAR_LEN];
|
|
2223 Bytecount bytoff = charcount_to_bytecount (string_data (s), i);
|
|
2224
|
|
2225 oldlen = charcount_to_bytecount (string_data (s) + bytoff, 1);
|
|
2226 newlen = set_charptr_emchar (newstr, c);
|
|
2227
|
|
2228 if (oldlen != newlen)
|
|
2229 resize_string (s, bytoff, newlen - oldlen);
|
185
|
2230 /* Remember, string_data (s) might have changed so we can't cache it. */
|
70
|
2231 memcpy (string_data (s) + bytoff, newstr, newlen);
|
|
2232 }
|
|
2233
|
|
2234 #endif /* MULE */
|
|
2235
|
20
|
2236 DEFUN ("make-string", Fmake_string, 2, 2, 0, /*
|
0
|
2237 Return a newly created string of length LENGTH, with each element being INIT.
|
|
2238 LENGTH must be an integer and INIT must be a character.
|
20
|
2239 */
|
|
2240 (length, init))
|
0
|
2241 {
|
|
2242 Lisp_Object val;
|
|
2243
|
|
2244 CHECK_NATNUM (length);
|
|
2245 CHECK_CHAR_COERCE_INT (init);
|
|
2246 {
|
|
2247 Bufbyte str[MAX_EMCHAR_LEN];
|
|
2248 int len = set_charptr_emchar (str, XCHAR (init));
|
|
2249
|
|
2250 val = make_uninit_string (len * XINT (length));
|
|
2251 if (len == 1)
|
|
2252 /* Optimize the single-byte case */
|
14
|
2253 memset (XSTRING_DATA (val), XCHAR (init), XSTRING_LENGTH (val));
|
0
|
2254 else
|
|
2255 {
|
|
2256 int i, j, k;
|
14
|
2257 Bufbyte *ptr = XSTRING_DATA (val);
|
0
|
2258
|
|
2259 k = 0;
|
|
2260 for (i = 0; i < XINT (length); i++)
|
|
2261 for (j = 0; j < len; j++)
|
|
2262 ptr[k++] = str[j];
|
|
2263 }
|
|
2264 }
|
173
|
2265 return val;
|
0
|
2266 }
|
|
2267
|
|
2268 /* Take some raw memory, which MUST already be in internal format,
|
|
2269 and package it up it into a Lisp string. */
|
|
2270 Lisp_Object
|
|
2271 make_string (CONST Bufbyte *contents, Bytecount length)
|
|
2272 {
|
|
2273 Lisp_Object val;
|
70
|
2274
|
|
2275 /* Make sure we find out about bad make_string's when they happen */
|
|
2276 #if defined (ERROR_CHECK_BUFPOS) && defined (MULE)
|
|
2277 bytecount_to_charcount (contents, length); /* Just for the assertions */
|
|
2278 #endif
|
183
|
2279
|
0
|
2280 val = make_uninit_string (length);
|
14
|
2281 memcpy (XSTRING_DATA (val), contents, length);
|
173
|
2282 return val;
|
0
|
2283 }
|
|
2284
|
|
2285 /* Take some raw memory, encoded in some external data format,
|
|
2286 and convert it into a Lisp string. */
|
|
2287 Lisp_Object
|
|
2288 make_ext_string (CONST Extbyte *contents, EMACS_INT length,
|
|
2289 enum external_data_format fmt)
|
|
2290 {
|
|
2291 CONST Bufbyte *intstr;
|
|
2292 Bytecount intlen;
|
|
2293
|
|
2294 GET_CHARPTR_INT_DATA_ALLOCA (contents, length, fmt, intstr, intlen);
|
|
2295 return make_string (intstr, intlen);
|
|
2296 }
|
|
2297
|
|
2298 Lisp_Object
|
|
2299 build_string (CONST char *str)
|
|
2300 {
|
185
|
2301 /* Some strlen's crash and burn if passed null. */
|
|
2302 return make_string ((CONST Bufbyte *) str, (str ? strlen(str) : 0));
|
0
|
2303 }
|
|
2304
|
|
2305 Lisp_Object
|
|
2306 build_ext_string (CONST char *str, enum external_data_format fmt)
|
|
2307 {
|
185
|
2308 /* Some strlen's crash and burn if passed null. */
|
|
2309 return make_ext_string ((Extbyte *) str, (str ? strlen(str) : 0), fmt);
|
0
|
2310 }
|
|
2311
|
|
2312 Lisp_Object
|
|
2313 build_translated_string (CONST char *str)
|
|
2314 {
|
|
2315 return build_string (GETTEXT (str));
|
|
2316 }
|
|
2317
|
|
2318
|
|
2319 /************************************************************************/
|
|
2320 /* lcrecord lists */
|
|
2321 /************************************************************************/
|
|
2322
|
|
2323 /* Lcrecord lists are used to manage the allocation of particular
|
|
2324 sorts of lcrecords, to avoid calling alloc_lcrecord() (and thus
|
|
2325 malloc() and garbage-collection junk) as much as possible.
|
|
2326 It is similar to the Blocktype class.
|
|
2327
|
|
2328 It works like this:
|
|
2329
|
|
2330 1) Create an lcrecord-list object using make_lcrecord_list().
|
|
2331 This is often done at initialization. Remember to staticpro
|
|
2332 this object! The arguments to make_lcrecord_list() are the
|
|
2333 same as would be passed to alloc_lcrecord().
|
|
2334 2) Instead of calling alloc_lcrecord(), call allocate_managed_lcrecord()
|
|
2335 and pass the lcrecord-list earlier created.
|
|
2336 3) When done with the lcrecord, call free_managed_lcrecord().
|
|
2337 The standard freeing caveats apply: ** make sure there are no
|
|
2338 pointers to the object anywhere! **
|
|
2339 4) Calling free_managed_lcrecord() is just like kissing the
|
|
2340 lcrecord goodbye as if it were garbage-collected. This means:
|
|
2341 -- the contents of the freed lcrecord are undefined, and the
|
|
2342 contents of something produced by allocate_managed_lcrecord()
|
|
2343 are undefined, just like for alloc_lcrecord().
|
|
2344 -- the mark method for the lcrecord's type will *NEVER* be called
|
|
2345 on freed lcrecords.
|
|
2346 -- the finalize method for the lcrecord's type will be called
|
|
2347 at the time that free_managed_lcrecord() is called.
|
|
2348
|
|
2349 */
|
|
2350
|
|
2351 static Lisp_Object
|
|
2352 mark_lcrecord_list (Lisp_Object obj, void (*markobj) (Lisp_Object))
|
|
2353 {
|
|
2354 struct lcrecord_list *list = XLCRECORD_LIST (obj);
|
|
2355 Lisp_Object chain = list->free;
|
|
2356
|
|
2357 while (!NILP (chain))
|
|
2358 {
|
|
2359 struct lrecord_header *lheader = XRECORD_LHEADER (chain);
|
|
2360 struct free_lcrecord_header *free_header =
|
|
2361 (struct free_lcrecord_header *) lheader;
|
14
|
2362
|
|
2363 #ifdef ERROR_CHECK_GC
|
0
|
2364 CONST struct lrecord_implementation *implementation
|
211
|
2365 = LHEADER_IMPLEMENTATION(lheader);
|
80
|
2366
|
0
|
2367 /* There should be no other pointers to the free list. */
|
|
2368 assert (!MARKED_RECORD_HEADER_P (lheader));
|
|
2369 /* Only lcrecords should be here. */
|
|
2370 assert (!implementation->basic_p);
|
|
2371 /* Only free lcrecords should be here. */
|
|
2372 assert (free_header->lcheader.free);
|
|
2373 /* The type of the lcrecord must be right. */
|
|
2374 assert (implementation == list->implementation);
|
|
2375 /* So must the size. */
|
|
2376 assert (implementation->static_size == 0
|
|
2377 || implementation->static_size == list->size);
|
14
|
2378 #endif /* ERROR_CHECK_GC */
|
80
|
2379
|
0
|
2380 MARK_RECORD_HEADER (lheader);
|
|
2381 chain = free_header->chain;
|
|
2382 }
|
183
|
2383
|
0
|
2384 return Qnil;
|
|
2385 }
|
|
2386
|
243
|
2387 DEFINE_LRECORD_IMPLEMENTATION ("lcrecord-list", lcrecord_list,
|
|
2388 mark_lcrecord_list, internal_object_printer,
|
|
2389 0, 0, 0, struct lcrecord_list);
|
0
|
2390 Lisp_Object
|
|
2391 make_lcrecord_list (int size,
|
|
2392 CONST struct lrecord_implementation *implementation)
|
|
2393 {
|
185
|
2394 struct lcrecord_list *p = alloc_lcrecord_type (struct lcrecord_list,
|
|
2395 lrecord_lcrecord_list);
|
0
|
2396 Lisp_Object val = Qnil;
|
|
2397
|
|
2398 p->implementation = implementation;
|
|
2399 p->size = size;
|
|
2400 p->free = Qnil;
|
|
2401 XSETLCRECORD_LIST (val, p);
|
|
2402 return val;
|
|
2403 }
|
|
2404
|
|
2405 Lisp_Object
|
|
2406 allocate_managed_lcrecord (Lisp_Object lcrecord_list)
|
|
2407 {
|
|
2408 struct lcrecord_list *list = XLCRECORD_LIST (lcrecord_list);
|
|
2409 if (!NILP (list->free))
|
|
2410 {
|
|
2411 Lisp_Object val = list->free;
|
|
2412 struct free_lcrecord_header *free_header =
|
|
2413 (struct free_lcrecord_header *) XPNTR (val);
|
|
2414
|
|
2415 #ifdef ERROR_CHECK_GC
|
|
2416 struct lrecord_header *lheader =
|
|
2417 (struct lrecord_header *) free_header;
|
|
2418 CONST struct lrecord_implementation *implementation
|
211
|
2419 = LHEADER_IMPLEMENTATION (lheader);
|
0
|
2420
|
|
2421 /* There should be no other pointers to the free list. */
|
|
2422 assert (!MARKED_RECORD_HEADER_P (lheader));
|
|
2423 /* Only lcrecords should be here. */
|
|
2424 assert (!implementation->basic_p);
|
|
2425 /* Only free lcrecords should be here. */
|
|
2426 assert (free_header->lcheader.free);
|
|
2427 /* The type of the lcrecord must be right. */
|
|
2428 assert (implementation == list->implementation);
|
|
2429 /* So must the size. */
|
|
2430 assert (implementation->static_size == 0
|
|
2431 || implementation->static_size == list->size);
|
183
|
2432 #endif /* ERROR_CHECK_GC */
|
0
|
2433 list->free = free_header->chain;
|
|
2434 free_header->lcheader.free = 0;
|
|
2435 return val;
|
|
2436 }
|
|
2437 else
|
|
2438 {
|
185
|
2439 Lisp_Object val = Qnil;
|
|
2440
|
|
2441 XSETOBJ (val, Lisp_Type_Record,
|
0
|
2442 alloc_lcrecord (list->size, list->implementation));
|
185
|
2443 return val;
|
0
|
2444 }
|
|
2445 }
|
|
2446
|
|
2447 void
|
|
2448 free_managed_lcrecord (Lisp_Object lcrecord_list, Lisp_Object lcrecord)
|
|
2449 {
|
|
2450 struct lcrecord_list *list = XLCRECORD_LIST (lcrecord_list);
|
|
2451 struct free_lcrecord_header *free_header =
|
|
2452 (struct free_lcrecord_header *) XPNTR (lcrecord);
|
|
2453 struct lrecord_header *lheader =
|
|
2454 (struct lrecord_header *) free_header;
|
|
2455 CONST struct lrecord_implementation *implementation
|
211
|
2456 = LHEADER_IMPLEMENTATION (lheader);
|
0
|
2457
|
|
2458 #ifdef ERROR_CHECK_GC
|
|
2459 /* Make sure the size is correct. This will catch, for example,
|
|
2460 putting a window configuration on the wrong free list. */
|
|
2461 if (implementation->size_in_bytes_method)
|
|
2462 assert (((implementation->size_in_bytes_method) (lheader))
|
|
2463 == list->size);
|
|
2464 else
|
|
2465 assert (implementation->static_size == list->size);
|
183
|
2466 #endif /* ERROR_CHECK_GC */
|
0
|
2467
|
|
2468 if (implementation->finalizer)
|
|
2469 ((implementation->finalizer) (lheader, 0));
|
|
2470 free_header->chain = list->free;
|
|
2471 free_header->lcheader.free = 1;
|
|
2472 list->free = lcrecord;
|
|
2473 }
|
|
2474
|
|
2475
|
|
2476 /**********************************************************************/
|
|
2477 /* Purity of essence, peace on earth */
|
|
2478 /**********************************************************************/
|
|
2479
|
|
2480 static int symbols_initialized;
|
|
2481
|
|
2482 Lisp_Object
|
|
2483 make_pure_string (CONST Bufbyte *data, Bytecount length,
|
|
2484 Lisp_Object plist, int no_need_to_copy_data)
|
|
2485 {
|
|
2486 Lisp_Object new;
|
|
2487 struct Lisp_String *s;
|
183
|
2488 int size = (sizeof (struct Lisp_String) + ((no_need_to_copy_data)
|
|
2489 ? 0
|
0
|
2490 /* + 1 for terminating 0 */
|
|
2491 : (length + 1)));
|
|
2492 size = ALIGN_SIZE (size, ALIGNOF (Lisp_Object));
|
|
2493
|
|
2494 if (symbols_initialized && !pure_lossage)
|
|
2495 {
|
|
2496 /* Try to share some names. Saves a few kbytes. */
|
|
2497 Lisp_Object tem = oblookup (Vobarray, data, length);
|
|
2498 if (SYMBOLP (tem))
|
|
2499 {
|
|
2500 s = XSYMBOL (tem)->name;
|
|
2501 if (!PURIFIED (s)) abort ();
|
|
2502 XSETSTRING (new, s);
|
173
|
2503 return new;
|
0
|
2504 }
|
|
2505 }
|
|
2506
|
|
2507 if (!check_purespace (size))
|
173
|
2508 return make_string (data, length);
|
0
|
2509
|
|
2510 s = (struct Lisp_String *) (PUREBEG + pureptr);
|
207
|
2511 #ifdef LRECORD_STRING
|
|
2512 set_lheader_implementation (&(s->lheader), lrecord_string);
|
211
|
2513 #ifdef USE_INDEXED_LRECORD_IMPLEMENTATION
|
|
2514 s->lheader.pure = 1;
|
|
2515 #endif
|
207
|
2516 #endif
|
0
|
2517 set_string_length (s, length);
|
|
2518 if (no_need_to_copy_data)
|
|
2519 {
|
|
2520 set_string_data (s, (Bufbyte *) data);
|
|
2521 }
|
|
2522 else
|
|
2523 {
|
|
2524 set_string_data (s, (Bufbyte *) s + sizeof (struct Lisp_String));
|
|
2525 memcpy (string_data (s), data, length);
|
|
2526 set_string_byte (s, length, 0);
|
|
2527 }
|
|
2528 s->plist = Qnil;
|
|
2529 pureptr += size;
|
|
2530
|
|
2531 #ifdef PURESTAT
|
|
2532 bump_purestat (&purestat_string_all, size);
|
|
2533 if (purecopying_for_bytecode)
|
|
2534 bump_purestat (&purestat_string_other_function, size);
|
183
|
2535 #endif /* PURESTAT */
|
0
|
2536
|
|
2537 /* Do this after the official "completion" of the purecopying. */
|
|
2538 s->plist = Fpurecopy (plist);
|
|
2539
|
|
2540 XSETSTRING (new, s);
|
173
|
2541 return new;
|
0
|
2542 }
|
|
2543
|
|
2544
|
|
2545 Lisp_Object
|
|
2546 make_pure_pname (CONST Bufbyte *data, Bytecount length,
|
|
2547 int no_need_to_copy_data)
|
|
2548 {
|
|
2549 Lisp_Object name = make_pure_string (data, length, Qnil,
|
|
2550 no_need_to_copy_data);
|
|
2551 bump_purestat (&purestat_string_pname, pure_sizeof (name));
|
|
2552
|
|
2553 /* We've made (at least) Qnil now, and Vobarray will soon be set up. */
|
|
2554 symbols_initialized = 1;
|
|
2555
|
173
|
2556 return name;
|
0
|
2557 }
|
|
2558
|
|
2559
|
|
2560 Lisp_Object
|
|
2561 pure_cons (Lisp_Object car, Lisp_Object cdr)
|
|
2562 {
|
|
2563 Lisp_Object new;
|
207
|
2564 struct Lisp_Cons *c;
|
0
|
2565
|
|
2566 if (!check_purespace (sizeof (struct Lisp_Cons)))
|
173
|
2567 return Fcons (Fpurecopy (car), Fpurecopy (cdr));
|
0
|
2568
|
207
|
2569 c = (struct Lisp_Cons *) (PUREBEG + pureptr);
|
|
2570 #ifdef LRECORD_CONS
|
|
2571 set_lheader_implementation (&(c->lheader), lrecord_cons);
|
211
|
2572 #ifdef USE_INDEXED_LRECORD_IMPLEMENTATION
|
|
2573 c->lheader.pure = 1;
|
|
2574 #endif
|
207
|
2575 #endif
|
0
|
2576 pureptr += sizeof (struct Lisp_Cons);
|
|
2577 bump_purestat (&purestat_cons, sizeof (struct Lisp_Cons));
|
|
2578
|
207
|
2579 c->car = Fpurecopy (car);
|
|
2580 c->cdr = Fpurecopy (cdr);
|
|
2581 XSETCONS (new, c);
|
173
|
2582 return new;
|
0
|
2583 }
|
|
2584
|
|
2585 Lisp_Object
|
|
2586 pure_list (int nargs, Lisp_Object *args)
|
|
2587 {
|
|
2588 Lisp_Object foo = Qnil;
|
|
2589
|
|
2590 for (--nargs; nargs >= 0; nargs--)
|
|
2591 foo = pure_cons (args[nargs], foo);
|
|
2592
|
|
2593 return foo;
|
|
2594 }
|
|
2595
|
|
2596 #ifdef LISP_FLOAT_TYPE
|
|
2597
|
|
2598 Lisp_Object
|
|
2599 make_pure_float (double num)
|
|
2600 {
|
|
2601 struct Lisp_Float *f;
|
|
2602 Lisp_Object val;
|
|
2603
|
|
2604 /* Make sure that PUREBEG + pureptr is aligned on at least a sizeof
|
|
2605 (double) boundary. Some architectures (like the sparc) require
|
|
2606 this, and I suspect that floats are rare enough that it's no
|
|
2607 tragedy for those that don't. */
|
|
2608 {
|
|
2609 #if defined (__GNUC__) && (__GNUC__ >= 2)
|
|
2610 /* In gcc, we can directly ask what the alignment constraints of a
|
|
2611 structure are, but in general, that's not possible... Arrgh!!
|
|
2612 */
|
|
2613 int alignment = __alignof (struct Lisp_Float);
|
|
2614 #else /* !GNUC */
|
|
2615 /* Best guess is to make the `double' slot be aligned to the size
|
|
2616 of double (which is probably 8 bytes). This assumes that it's
|
|
2617 ok to align the beginning of the structure to the same boundary
|
|
2618 that the `double' slot in it is supposed to be aligned to; this
|
|
2619 should be ok because presumably there is padding in the layout
|
|
2620 of the struct to account for this.
|
|
2621 */
|
|
2622 int alignment = sizeof (float_data (f));
|
183
|
2623 #endif /* !GNUC */
|
0
|
2624 char *p = ((char *) PUREBEG + pureptr);
|
|
2625
|
|
2626 p = (char *) (((unsigned EMACS_INT) p + alignment - 1) & - alignment);
|
|
2627 pureptr = p - (char *) PUREBEG;
|
|
2628 }
|
|
2629
|
|
2630 if (!check_purespace (sizeof (struct Lisp_Float)))
|
173
|
2631 return make_float (num);
|
0
|
2632
|
|
2633 f = (struct Lisp_Float *) (PUREBEG + pureptr);
|
|
2634 set_lheader_implementation (&(f->lheader), lrecord_float);
|
211
|
2635 #ifdef USE_INDEXED_LRECORD_IMPLEMENTATION
|
|
2636 f->lheader.pure = 1;
|
|
2637 #endif
|
0
|
2638 pureptr += sizeof (struct Lisp_Float);
|
|
2639 bump_purestat (&purestat_float, sizeof (struct Lisp_Float));
|
|
2640
|
|
2641 float_next (f) = ((struct Lisp_Float *) -1);
|
|
2642 float_data (f) = num;
|
|
2643 XSETFLOAT (val, f);
|
173
|
2644 return val;
|
0
|
2645 }
|
|
2646
|
|
2647 #endif /* LISP_FLOAT_TYPE */
|
|
2648
|
|
2649 Lisp_Object
|
|
2650 make_pure_vector (EMACS_INT len, Lisp_Object init)
|
|
2651 {
|
|
2652 Lisp_Object new;
|
207
|
2653 struct Lisp_Vector *v;
|
0
|
2654 EMACS_INT size = (sizeof (struct Lisp_Vector)
|
|
2655 + (len - 1) * sizeof (Lisp_Object));
|
|
2656
|
|
2657 init = Fpurecopy (init);
|
|
2658
|
|
2659 if (!check_purespace (size))
|
173
|
2660 return make_vector (len, init);
|
0
|
2661
|
207
|
2662 v = (struct Lisp_Vector *) (PUREBEG + pureptr);
|
|
2663 #ifdef LRECORD_VECTOR
|
|
2664 set_lheader_implementation (&(v->header.lheader), lrecord_vector);
|
211
|
2665 #ifdef USE_INDEXED_LRECORD_IMPLEMENTATION
|
|
2666 v->header.lheader.pure = 1;
|
|
2667 #endif
|
207
|
2668 #endif
|
0
|
2669 pureptr += size;
|
|
2670 bump_purestat (&purestat_vector_all, size);
|
|
2671
|
207
|
2672 v->size = len;
|
0
|
2673
|
|
2674 for (size = 0; size < len; size++)
|
207
|
2675 v->contents[size] = init;
|
|
2676
|
243
|
2677 XSETVECTOR (new, v);
|
173
|
2678 return new;
|
0
|
2679 }
|
|
2680
|
|
2681 #if 0
|
|
2682 /* Presently unused */
|
|
2683 void *
|
|
2684 alloc_pure_lrecord (int size, struct lrecord_implementation *implementation)
|
|
2685 {
|
|
2686 struct lrecord_header *header = (void *) (PUREBEG + pureptr);
|
|
2687
|
171
|
2688 if (pureptr + size > get_PURESIZE())
|
0
|
2689 pure_storage_exhausted ();
|
|
2690
|
|
2691 set_lheader_implementation (header, implementation);
|
|
2692 header->next = 0;
|
173
|
2693 return header;
|
0
|
2694 }
|
183
|
2695 #endif /* unused */
|
0
|
2696
|
|
2697
|
|
2698
|
20
|
2699 DEFUN ("purecopy", Fpurecopy, 1, 1, 0, /*
|
0
|
2700 Make a copy of OBJECT in pure storage.
|
|
2701 Recursively copies contents of vectors and cons cells.
|
|
2702 Does not copy symbols.
|
20
|
2703 */
|
|
2704 (obj))
|
0
|
2705 {
|
|
2706 int i;
|
|
2707 if (!purify_flag)
|
173
|
2708 return obj;
|
0
|
2709
|
|
2710 if (!POINTER_TYPE_P (XTYPE (obj))
|
207
|
2711 || PURIFIED (XPNTR (obj))
|
|
2712 /* happens when bootstrapping Qnil */
|
|
2713 || EQ (obj, Qnull_pointer))
|
173
|
2714 return obj;
|
0
|
2715
|
|
2716 switch (XTYPE (obj))
|
|
2717 {
|
207
|
2718 #ifndef LRECORD_CONS
|
185
|
2719 case Lisp_Type_Cons:
|
0
|
2720 return pure_cons (XCAR (obj), XCDR (obj));
|
207
|
2721 #endif
|
|
2722
|
|
2723 #ifndef LRECORD_STRING
|
185
|
2724 case Lisp_Type_String:
|
16
|
2725 return make_pure_string (XSTRING_DATA (obj),
|
|
2726 XSTRING_LENGTH (obj),
|
0
|
2727 XSTRING (obj)->plist,
|
|
2728 0);
|
207
|
2729 #endif /* ! LRECORD_STRING */
|
0
|
2730
|
185
|
2731 #ifndef LRECORD_VECTOR
|
|
2732 case Lisp_Type_Vector:
|
0
|
2733 {
|
|
2734 struct Lisp_Vector *o = XVECTOR (obj);
|
|
2735 Lisp_Object new = make_pure_vector (vector_length (o), Qnil);
|
|
2736 for (i = 0; i < vector_length (o); i++)
|
173
|
2737 XVECTOR_DATA (new)[i] = Fpurecopy (o->contents[i]);
|
|
2738 return new;
|
0
|
2739 }
|
185
|
2740 #endif /* !LRECORD_VECTOR */
|
0
|
2741
|
|
2742 default:
|
|
2743 {
|
|
2744 if (COMPILED_FUNCTIONP (obj))
|
|
2745 {
|
|
2746 struct Lisp_Compiled_Function *o = XCOMPILED_FUNCTION (obj);
|
|
2747 Lisp_Object new = make_compiled_function (1);
|
104
|
2748 /* How on earth could this code have worked before? -sb */
|
|
2749 struct Lisp_Compiled_Function *n = XCOMPILED_FUNCTION (new);
|
0
|
2750 n->flags = o->flags;
|
|
2751 n->bytecodes = Fpurecopy (o->bytecodes);
|
|
2752 n->constants = Fpurecopy (o->constants);
|
|
2753 n->arglist = Fpurecopy (o->arglist);
|
|
2754 n->doc_and_interactive = Fpurecopy (o->doc_and_interactive);
|
104
|
2755 n->maxdepth = o->maxdepth;
|
173
|
2756 return new;
|
0
|
2757 }
|
207
|
2758 #ifdef LRECORD_CONS
|
|
2759 else if (CONSP (obj))
|
|
2760 return pure_cons (XCAR (obj), XCDR (obj));
|
|
2761 #endif /* LRECORD_CONS */
|
|
2762 #ifdef LRECORD_VECTOR
|
|
2763 else if (VECTORP (obj))
|
|
2764 {
|
|
2765 struct Lisp_Vector *o = XVECTOR (obj);
|
|
2766 Lisp_Object new = make_pure_vector (vector_length (o), Qnil);
|
|
2767 for (i = 0; i < vector_length (o); i++)
|
|
2768 XVECTOR_DATA (new)[i] = Fpurecopy (o->contents[i]);
|
|
2769 return new;
|
|
2770 }
|
|
2771 #endif /* LRECORD_VECTOR */
|
|
2772 #ifdef LRECORD_STRING
|
|
2773 else if (STRINGP (obj))
|
|
2774 {
|
|
2775 return make_pure_string (XSTRING_DATA (obj),
|
|
2776 XSTRING_LENGTH (obj),
|
|
2777 XSTRING (obj)->plist,
|
|
2778 0);
|
|
2779 }
|
|
2780 #endif /* LRECORD_STRING */
|
0
|
2781 #ifdef LISP_FLOAT_TYPE
|
|
2782 else if (FLOATP (obj))
|
|
2783 return make_pure_float (float_data (XFLOAT (obj)));
|
|
2784 #endif /* LISP_FLOAT_TYPE */
|
|
2785 else if (!SYMBOLP (obj))
|
|
2786 signal_simple_error ("Can't purecopy %S", obj);
|
|
2787 }
|
|
2788 }
|
173
|
2789 return obj;
|
0
|
2790 }
|
|
2791
|
|
2792
|
|
2793
|
102
|
2794 static void
|
183
|
2795 puresize_adjust_h (long int puresize)
|
102
|
2796 {
|
183
|
2797 FILE *stream = fopen ("puresize-adjust.h", "w");
|
|
2798
|
|
2799 if (stream == NULL)
|
|
2800 report_file_error ("Opening puresize adjustment file",
|
|
2801 Fcons (build_string ("puresize-adjust.h"), Qnil));
|
|
2802
|
|
2803 fprintf (stream,
|
|
2804 "/*\tDo not edit this file!\n"
|
|
2805 "\tAutomatically generated by XEmacs */\n"
|
|
2806 "# define PURESIZE_ADJUSTMENT (%ld)\n",
|
|
2807 puresize - RAW_PURESIZE);
|
|
2808 fclose (stream);
|
102
|
2809 }
|
|
2810
|
0
|
2811 void
|
|
2812 report_pure_usage (int report_impurities,
|
|
2813 int die_if_pure_storage_exceeded)
|
|
2814 {
|
102
|
2815 int rc = 0;
|
|
2816
|
0
|
2817 if (pure_lossage)
|
|
2818 {
|
|
2819 message ("\n****\tPure Lisp storage exhausted!\n"
|
183
|
2820 "\tPurespace usage: %ld of %ld\n"
|
|
2821 "****",
|
171
|
2822 get_PURESIZE()+pure_lossage, (long) get_PURESIZE());
|
102
|
2823 if (die_if_pure_storage_exceeded) {
|
183
|
2824 puresize_adjust_h (get_PURESIZE() + pure_lossage);
|
102
|
2825 rc = -1;
|
|
2826 }
|
0
|
2827 }
|
|
2828 else
|
|
2829 {
|
171
|
2830 int lost = (get_PURESIZE() - pureptr) / 1024;
|
0
|
2831 char buf[200];
|
195
|
2832 extern Lisp_Object Vemacs_beta_version;
|
|
2833 int slop = NILP(Vemacs_beta_version) ? 512 : 4;
|
0
|
2834
|
|
2835 sprintf (buf, "Purespace usage: %ld of %ld (%d%%",
|
171
|
2836 pureptr, (long) get_PURESIZE(),
|
|
2837 (int) (pureptr / (get_PURESIZE() / 100.0) + 0.5));
|
102
|
2838 if (lost > 2) {
|
0
|
2839 sprintf (buf + strlen (buf), " -- %dk wasted", lost);
|
102
|
2840 if (die_if_pure_storage_exceeded) {
|
195
|
2841 puresize_adjust_h (pureptr + slop);
|
102
|
2842 rc = -1;
|
|
2843 }
|
|
2844 }
|
|
2845
|
0
|
2846 strcat (buf, ").");
|
|
2847 message ("%s", buf);
|
|
2848 }
|
|
2849
|
|
2850 #ifdef PURESTAT
|
183
|
2851
|
|
2852 purestat_vector_other.nbytes =
|
|
2853 purestat_vector_all.nbytes -
|
|
2854 purestat_vector_bytecode_constants.nbytes;
|
|
2855 purestat_vector_other.nobjects =
|
|
2856 purestat_vector_all.nobjects -
|
|
2857 purestat_vector_bytecode_constants.nobjects;
|
|
2858
|
|
2859 purestat_string_other.nbytes =
|
|
2860 purestat_string_all.nbytes -
|
|
2861 (purestat_string_pname.nbytes +
|
|
2862 purestat_string_bytecodes.nbytes +
|
|
2863 purestat_string_interactive.nbytes +
|
|
2864 purestat_string_documentation.nbytes +
|
0
|
2865 #ifdef I18N3
|
183
|
2866 purestat_string_domain.nbytes +
|
0
|
2867 #endif
|
183
|
2868 purestat_string_other_function.nbytes);
|
|
2869
|
|
2870 purestat_string_other.nobjects =
|
|
2871 purestat_string_all.nobjects -
|
|
2872 (purestat_string_pname.nobjects +
|
|
2873 purestat_string_bytecodes.nobjects +
|
|
2874 purestat_string_interactive.nobjects +
|
|
2875 purestat_string_documentation.nobjects +
|
0
|
2876 #ifdef I18N3
|
183
|
2877 purestat_string_domain.nobjects +
|
0
|
2878 #endif
|
183
|
2879 purestat_string_other_function.nobjects);
|
|
2880
|
|
2881 message (" %-26s Total Bytes", "");
|
|
2882
|
|
2883 {
|
|
2884 int j;
|
|
2885
|
|
2886 for (j = 0; j < countof (purestats); j++)
|
|
2887 if (!purestats[j])
|
0
|
2888 clear_message ();
|
|
2889 else
|
183
|
2890 {
|
|
2891 char buf [100];
|
|
2892 sprintf(buf, "%s:", purestats[j]->name);
|
|
2893 message (" %-26s %5d %7d %2d%%",
|
|
2894 buf,
|
|
2895 purestats[j]->nobjects,
|
|
2896 purestats[j]->nbytes,
|
|
2897 (int) (purestats[j]->nbytes / (pureptr / 100.0) + 0.5));
|
|
2898 }
|
0
|
2899 }
|
|
2900 #endif /* PURESTAT */
|
|
2901
|
|
2902
|
|
2903 if (report_impurities)
|
|
2904 {
|
|
2905 Lisp_Object tem = Felt (Fgarbage_collect (), make_int (5));
|
|
2906 struct gcpro gcpro1;
|
|
2907 GCPRO1 (tem);
|
|
2908 message ("\nImpurities:");
|
|
2909 while (!NILP (tem))
|
|
2910 {
|
|
2911 if (CONSP (tem) && SYMBOLP (Fcar (tem)) && CONSP (Fcdr (tem)))
|
|
2912 {
|
|
2913 int total = XINT (Fcar (Fcdr (tem)));
|
|
2914 if (total > 0)
|
|
2915 {
|
|
2916 char buf [100];
|
|
2917 char *s = buf;
|
|
2918 memcpy (buf, string_data (XSYMBOL (Fcar (tem))->name),
|
|
2919 string_length (XSYMBOL (Fcar (tem))->name) + 1);
|
|
2920 while (*s++) if (*s == '-') *s = ' ';
|
|
2921 s--; *s++ = ':'; *s = 0;
|
183
|
2922 message (" %-33s %6d", buf, total);
|
0
|
2923 }
|
|
2924 tem = Fcdr (Fcdr (tem));
|
|
2925 }
|
|
2926 else /* WTF?! */
|
|
2927 {
|
|
2928 Fprin1 (tem, Qexternal_debugging_output);
|
|
2929 tem = Qnil;
|
|
2930 }
|
|
2931 }
|
|
2932 UNGCPRO;
|
|
2933 garbage_collect_1 (); /* GC garbage_collect's garbage */
|
|
2934 }
|
|
2935 clear_message ();
|
|
2936
|
102
|
2937 if (rc < 0) {
|
183
|
2938 unlink("SATISFIED");
|
118
|
2939 /* Current build process on NT does */
|
|
2940 /* not know how to restart itself. */
|
183
|
2941 /* --marcpa */
|
118
|
2942 #ifndef WINDOWSNT
|
171
|
2943 fatal ("Pure size adjusted, Don't Panic! I will restart the `make'");
|
118
|
2944 #endif
|
102
|
2945 } else if (pure_lossage && die_if_pure_storage_exceeded) {
|
0
|
2946 fatal ("Pure storage exhausted");
|
102
|
2947 }
|
0
|
2948 }
|
|
2949
|
|
2950
|
|
2951 /**********************************************************************/
|
|
2952 /* staticpro */
|
|
2953 /**********************************************************************/
|
|
2954
|
|
2955 struct gcpro *gcprolist;
|
|
2956
|
|
2957 /* 415 used Mly 29-Jun-93 */
|
|
2958 #define NSTATICS 1500
|
|
2959 /* Not "static" because of linker lossage on some systems */
|
|
2960 Lisp_Object *staticvec[NSTATICS]
|
|
2961 /* Force it into data space! */
|
|
2962 = {0};
|
|
2963 static int staticidx;
|
|
2964
|
|
2965 /* Put an entry in staticvec, pointing at the variable whose address is given
|
|
2966 */
|
|
2967 void
|
|
2968 staticpro (Lisp_Object *varaddress)
|
|
2969 {
|
|
2970 if (staticidx >= countof (staticvec))
|
|
2971 abort ();
|
|
2972 staticvec[staticidx++] = varaddress;
|
|
2973 }
|
|
2974
|
|
2975
|
|
2976 /* Mark reference to a Lisp_Object. If the object referred to has not been
|
|
2977 seen yet, recursively mark all the references contained in it. */
|
183
|
2978
|
0
|
2979 static void
|
|
2980 mark_object (Lisp_Object obj)
|
|
2981 {
|
|
2982 tail_recurse:
|
|
2983
|
207
|
2984 if (EQ (obj, Qnull_pointer))
|
|
2985 return;
|
0
|
2986 if (!POINTER_TYPE_P (XGCTYPE (obj)))
|
|
2987 return;
|
|
2988 if (PURIFIED (XPNTR (obj)))
|
|
2989 return;
|
|
2990 switch (XGCTYPE (obj))
|
|
2991 {
|
207
|
2992 #ifndef LRECORD_CONS
|
185
|
2993 case Lisp_Type_Cons:
|
0
|
2994 {
|
|
2995 struct Lisp_Cons *ptr = XCONS (obj);
|
|
2996 if (CONS_MARKED_P (ptr))
|
|
2997 break;
|
|
2998 MARK_CONS (ptr);
|
|
2999 /* If the cdr is nil, tail-recurse on the car. */
|
|
3000 if (NILP (ptr->cdr))
|
|
3001 {
|
|
3002 obj = ptr->car;
|
|
3003 }
|
|
3004 else
|
|
3005 {
|
|
3006 mark_object (ptr->car);
|
|
3007 obj = ptr->cdr;
|
|
3008 }
|
|
3009 goto tail_recurse;
|
|
3010 }
|
207
|
3011 #endif
|
0
|
3012
|
185
|
3013 case Lisp_Type_Record:
|
0
|
3014 /* case Lisp_Symbol_Value_Magic: */
|
|
3015 {
|
|
3016 struct lrecord_header *lheader = XRECORD_LHEADER (obj);
|
|
3017 CONST struct lrecord_implementation *implementation
|
211
|
3018 = LHEADER_IMPLEMENTATION (lheader);
|
0
|
3019
|
|
3020 if (! MARKED_RECORD_HEADER_P (lheader) &&
|
|
3021 ! UNMARKABLE_RECORD_HEADER_P (lheader))
|
|
3022 {
|
|
3023 MARK_RECORD_HEADER (lheader);
|
|
3024 #ifdef ERROR_CHECK_GC
|
|
3025 if (!implementation->basic_p)
|
|
3026 assert (! ((struct lcrecord_header *) lheader)->free);
|
|
3027 #endif
|
|
3028 if (implementation->marker != 0)
|
|
3029 {
|
|
3030 obj = ((implementation->marker) (obj, mark_object));
|
|
3031 if (!NILP (obj)) goto tail_recurse;
|
|
3032 }
|
|
3033 }
|
|
3034 }
|
|
3035 break;
|
|
3036
|
207
|
3037 #ifndef LRECORD_STRING
|
185
|
3038 case Lisp_Type_String:
|
0
|
3039 {
|
|
3040 struct Lisp_String *ptr = XSTRING (obj);
|
|
3041
|
|
3042 if (!XMARKBIT (ptr->plist))
|
|
3043 {
|
|
3044 if (CONSP (ptr->plist) &&
|
|
3045 EXTENT_INFOP (XCAR (ptr->plist)))
|
|
3046 flush_cached_extent_info (XCAR (ptr->plist));
|
|
3047 XMARK (ptr->plist);
|
|
3048 obj = ptr->plist;
|
|
3049 goto tail_recurse;
|
|
3050 }
|
|
3051 }
|
|
3052 break;
|
207
|
3053 #endif /* ! LRECORD_STRING */
|
0
|
3054
|
185
|
3055 #ifndef LRECORD_VECTOR
|
|
3056 case Lisp_Type_Vector:
|
0
|
3057 {
|
|
3058 struct Lisp_Vector *ptr = XVECTOR (obj);
|
|
3059 int len = vector_length (ptr);
|
|
3060 int i;
|
|
3061
|
|
3062 if (len < 0)
|
|
3063 break; /* Already marked */
|
|
3064 ptr->size = -1 - len; /* Else mark it */
|
|
3065 for (i = 0; i < len - 1; i++) /* and then mark its elements */
|
|
3066 mark_object (ptr->contents[i]);
|
|
3067 if (len > 0)
|
|
3068 {
|
|
3069 obj = ptr->contents[len - 1];
|
|
3070 goto tail_recurse;
|
|
3071 }
|
|
3072 }
|
|
3073 break;
|
185
|
3074 #endif /* !LRECORD_VECTOR */
|
0
|
3075
|
|
3076 #ifndef LRECORD_SYMBOL
|
185
|
3077 case Lisp_Type_Symbol:
|
0
|
3078 {
|
|
3079 struct Lisp_Symbol *sym = XSYMBOL (obj);
|
|
3080
|
|
3081 while (!XMARKBIT (sym->plist))
|
|
3082 {
|
|
3083 XMARK (sym->plist);
|
|
3084 mark_object (sym->value);
|
|
3085 mark_object (sym->function);
|
|
3086 {
|
207
|
3087 /*
|
|
3088 * symbol->name is a struct Lisp_String *, not a
|
|
3089 * Lisp_Object. Fix it up and pass to mark_object.
|
|
3090 */
|
|
3091 Lisp_Object symname;
|
|
3092 XSETSTRING(symname, sym->name);
|
|
3093 mark_object(symname);
|
0
|
3094 }
|
|
3095 if (!symbol_next (sym))
|
|
3096 {
|
|
3097 obj = sym->plist;
|
|
3098 goto tail_recurse;
|
|
3099 }
|
|
3100 mark_object (sym->plist);
|
|
3101 /* Mark the rest of the symbols in the hash-chain */
|
|
3102 sym = symbol_next (sym);
|
|
3103 }
|
|
3104 }
|
|
3105 break;
|
|
3106 #endif /* !LRECORD_SYMBOL */
|
|
3107
|
|
3108 default:
|
|
3109 abort ();
|
|
3110 }
|
|
3111 }
|
|
3112
|
|
3113 /* mark all of the conses in a list and mark the final cdr; but
|
|
3114 DO NOT mark the cars.
|
|
3115
|
|
3116 Use only for internal lists! There should never be other pointers
|
|
3117 to the cons cells, because if so, the cars will remain unmarked
|
|
3118 even when they maybe should be marked. */
|
|
3119 void
|
|
3120 mark_conses_in_list (Lisp_Object obj)
|
|
3121 {
|
|
3122 Lisp_Object rest;
|
|
3123
|
|
3124 for (rest = obj; CONSP (rest); rest = XCDR (rest))
|
|
3125 {
|
|
3126 if (CONS_MARKED_P (XCONS (rest)))
|
|
3127 return;
|
|
3128 MARK_CONS (XCONS (rest));
|
|
3129 }
|
|
3130
|
|
3131 mark_object (rest);
|
|
3132 }
|
|
3133
|
|
3134
|
|
3135 #ifdef PURESTAT
|
183
|
3136 /* Simpler than mark-object, because pure structure can't
|
|
3137 have any circularities */
|
0
|
3138
|
|
3139 #if 0 /* unused */
|
|
3140 static int idiot_c_doesnt_have_closures;
|
|
3141 static void
|
|
3142 idiot_c (Lisp_Object obj)
|
|
3143 {
|
|
3144 idiot_c_doesnt_have_closures += pure_sizeof (obj, 1);
|
|
3145 }
|
|
3146 #endif /* unused */
|
|
3147
|
207
|
3148 static int
|
|
3149 pure_string_sizeof(Lisp_Object obj)
|
|
3150 {
|
|
3151 struct Lisp_String *ptr = XSTRING (obj);
|
|
3152 int size = string_length (ptr);
|
|
3153
|
|
3154 if (string_data (ptr) !=
|
|
3155 (unsigned char *) ptr + sizeof (struct Lisp_String))
|
|
3156 {
|
|
3157 /* string-data not allocated contiguously.
|
|
3158 Probably (better be!!) a pointer constant "C" data. */
|
|
3159 size = sizeof (struct Lisp_String);
|
|
3160 }
|
|
3161 else
|
|
3162 {
|
|
3163 size = sizeof (struct Lisp_String) + size + 1;
|
|
3164 size = ALIGN_SIZE (size, sizeof (Lisp_Object));
|
|
3165 }
|
|
3166 return size;
|
|
3167 }
|
|
3168
|
0
|
3169 /* recurse arg isn't actually used */
|
|
3170 static int
|
|
3171 pure_sizeof (Lisp_Object obj /*, int recurse */)
|
|
3172 {
|
|
3173 int total = 0;
|
|
3174
|
|
3175 /*tail_recurse: */
|
|
3176 if (!POINTER_TYPE_P (XTYPE (obj))
|
|
3177 || !PURIFIED (XPNTR (obj)))
|
173
|
3178 return total;
|
0
|
3179
|
|
3180 /* symbol's sizes are accounted for separately */
|
|
3181 if (SYMBOLP (obj))
|
173
|
3182 return total;
|
0
|
3183
|
|
3184 switch (XTYPE (obj))
|
|
3185 {
|
207
|
3186
|
|
3187 #ifndef LRECORD_STRING
|
185
|
3188 case Lisp_Type_String:
|
0
|
3189 {
|
207
|
3190 total += pure_string_sizeof (obj);
|
0
|
3191 }
|
|
3192 break;
|
207
|
3193 #endif /* ! LRECORD_STRING */
|
0
|
3194
|
185
|
3195 #ifndef LRECORD_VECTOR
|
|
3196 case Lisp_Type_Vector:
|
0
|
3197 {
|
|
3198 struct Lisp_Vector *ptr = XVECTOR (obj);
|
|
3199 int len = vector_length (ptr);
|
|
3200
|
|
3201 total += (sizeof (struct Lisp_Vector)
|
|
3202 + (len - 1) * sizeof (Lisp_Object));
|
|
3203 #if 0 /* unused */
|
|
3204 if (!recurse)
|
|
3205 break;
|
183
|
3206 {
|
0
|
3207 int i;
|
|
3208 for (i = 0; i < len - 1; i++)
|
|
3209 total += pure_sizeof (ptr->contents[i], 1);
|
|
3210 }
|
|
3211 if (len > 0)
|
|
3212 {
|
|
3213 obj = ptr->contents[len - 1];
|
|
3214 goto tail_recurse;
|
|
3215 }
|
|
3216 #endif /* unused */
|
|
3217 }
|
|
3218 break;
|
207
|
3219 #endif /* !LRECORD_VECTOR */
|
185
|
3220
|
|
3221 case Lisp_Type_Record:
|
0
|
3222 {
|
|
3223 struct lrecord_header *lheader = XRECORD_LHEADER (obj);
|
|
3224 CONST struct lrecord_implementation *implementation
|
211
|
3225 = LHEADER_IMPLEMENTATION (lheader);
|
0
|
3226
|
207
|
3227 #ifdef LRECORD_STRING
|
|
3228 if (STRINGP (obj))
|
|
3229 total += pure_string_sizeof (obj);
|
|
3230 else
|
|
3231 #endif
|
0
|
3232 if (implementation->size_in_bytes_method)
|
|
3233 total += ((implementation->size_in_bytes_method) (lheader));
|
|
3234 else
|
|
3235 total += implementation->static_size;
|
|
3236
|
|
3237 #if 0 /* unused */
|
|
3238 if (!recurse)
|
|
3239 break;
|
|
3240
|
|
3241 if (implementation->marker != 0)
|
|
3242 {
|
|
3243 int old = idiot_c_doesnt_have_closures;
|
|
3244
|
|
3245 idiot_c_doesnt_have_closures = 0;
|
|
3246 obj = ((implementation->marker) (obj, idiot_c));
|
|
3247 total += idiot_c_doesnt_have_closures;
|
|
3248 idiot_c_doesnt_have_closures = old;
|
183
|
3249
|
0
|
3250 if (!NILP (obj)) goto tail_recurse;
|
|
3251 }
|
|
3252 #endif /* unused */
|
|
3253 }
|
|
3254 break;
|
|
3255
|
207
|
3256 #ifndef LRECORD_CONS
|
185
|
3257 case Lisp_Type_Cons:
|
0
|
3258 {
|
|
3259 struct Lisp_Cons *ptr = XCONS (obj);
|
|
3260 total += sizeof (*ptr);
|
|
3261 #if 0 /* unused */
|
|
3262 if (!recurse)
|
|
3263 break;
|
|
3264 /* If the cdr is nil, tail-recurse on the car. */
|
|
3265 if (NILP (ptr->cdr))
|
|
3266 {
|
|
3267 obj = ptr->car;
|
|
3268 }
|
|
3269 else
|
|
3270 {
|
|
3271 total += pure_sizeof (ptr->car, 1);
|
|
3272 obj = ptr->cdr;
|
|
3273 }
|
|
3274 goto tail_recurse;
|
|
3275 #endif /* unused */
|
|
3276 }
|
|
3277 break;
|
207
|
3278 #endif
|
0
|
3279
|
|
3280 /* Others can't be purified */
|
|
3281 default:
|
|
3282 abort ();
|
|
3283 }
|
173
|
3284 return total;
|
0
|
3285 }
|
|
3286 #endif /* PURESTAT */
|
|
3287
|
|
3288
|
|
3289
|
|
3290
|
|
3291 /* Find all structures not marked, and free them. */
|
|
3292
|
207
|
3293 #ifndef LRECORD_VECTOR
|
0
|
3294 static int gc_count_num_vector_used, gc_count_vector_total_size;
|
|
3295 static int gc_count_vector_storage;
|
207
|
3296 #endif
|
0
|
3297 static int gc_count_num_bit_vector_used, gc_count_bit_vector_total_size;
|
|
3298 static int gc_count_bit_vector_storage;
|
|
3299 static int gc_count_num_short_string_in_use;
|
|
3300 static int gc_count_string_total_size;
|
|
3301 static int gc_count_short_string_total_size;
|
|
3302
|
|
3303 /* static int gc_count_total_records_used, gc_count_records_total_size; */
|
|
3304
|
|
3305
|
|
3306 /* This will be used more extensively In The Future */
|
|
3307 static int last_lrecord_type_index_assigned;
|
|
3308
|
211
|
3309 CONST struct lrecord_implementation *lrecord_implementations_table[128];
|
0
|
3310 #define max_lrecord_type (countof (lrecord_implementations_table) - 1)
|
|
3311
|
211
|
3312 int
|
0
|
3313 lrecord_type_index (CONST struct lrecord_implementation *implementation)
|
|
3314 {
|
|
3315 int type_index = *(implementation->lrecord_type_index);
|
|
3316 /* Have to do this circuitous and validation test because of problems
|
|
3317 dumping out initialized variables (ie can't set xxx_type_index to -1
|
|
3318 because that would make xxx_type_index read-only in a dumped emacs. */
|
|
3319 if (type_index < 0 || type_index > max_lrecord_type
|
|
3320 || lrecord_implementations_table[type_index] != implementation)
|
|
3321 {
|
|
3322 if (last_lrecord_type_index_assigned == max_lrecord_type)
|
|
3323 abort ();
|
|
3324 type_index = ++last_lrecord_type_index_assigned;
|
|
3325 lrecord_implementations_table[type_index] = implementation;
|
|
3326 *(implementation->lrecord_type_index) = type_index;
|
|
3327 }
|
173
|
3328 return type_index;
|
0
|
3329 }
|
|
3330
|
|
3331 /* stats on lcrecords in use - kinda kludgy */
|
|
3332
|
|
3333 static struct
|
|
3334 {
|
|
3335 int instances_in_use;
|
|
3336 int bytes_in_use;
|
|
3337 int instances_freed;
|
|
3338 int bytes_freed;
|
|
3339 int instances_on_free_list;
|
|
3340 } lcrecord_stats [countof (lrecord_implementations_table)];
|
|
3341
|
|
3342
|
|
3343 static void
|
|
3344 reset_lcrecord_stats (void)
|
|
3345 {
|
|
3346 int i;
|
|
3347 for (i = 0; i < countof (lcrecord_stats); i++)
|
|
3348 {
|
|
3349 lcrecord_stats[i].instances_in_use = 0;
|
|
3350 lcrecord_stats[i].bytes_in_use = 0;
|
|
3351 lcrecord_stats[i].instances_freed = 0;
|
|
3352 lcrecord_stats[i].bytes_freed = 0;
|
|
3353 lcrecord_stats[i].instances_on_free_list = 0;
|
|
3354 }
|
|
3355 }
|
|
3356
|
|
3357 static void
|
|
3358 tick_lcrecord_stats (CONST struct lrecord_header *h, int free_p)
|
|
3359 {
|
211
|
3360 CONST struct lrecord_implementation *implementation =
|
|
3361 LHEADER_IMPLEMENTATION (h);
|
0
|
3362 int type_index = lrecord_type_index (implementation);
|
|
3363
|
|
3364 if (((struct lcrecord_header *) h)->free)
|
|
3365 {
|
|
3366 assert (!free_p);
|
|
3367 lcrecord_stats[type_index].instances_on_free_list++;
|
|
3368 }
|
|
3369 else
|
|
3370 {
|
|
3371 unsigned int sz = (implementation->size_in_bytes_method
|
|
3372 ? ((implementation->size_in_bytes_method) (h))
|
|
3373 : implementation->static_size);
|
|
3374
|
|
3375 if (free_p)
|
|
3376 {
|
|
3377 lcrecord_stats[type_index].instances_freed++;
|
|
3378 lcrecord_stats[type_index].bytes_freed += sz;
|
|
3379 }
|
|
3380 else
|
|
3381 {
|
|
3382 lcrecord_stats[type_index].instances_in_use++;
|
|
3383 lcrecord_stats[type_index].bytes_in_use += sz;
|
|
3384 }
|
|
3385 }
|
|
3386 }
|
|
3387
|
|
3388
|
|
3389 /* Free all unmarked records */
|
|
3390 static void
|
|
3391 sweep_lcrecords_1 (struct lcrecord_header **prev, int *used)
|
|
3392 {
|
|
3393 struct lcrecord_header *header;
|
|
3394 int num_used = 0;
|
|
3395 /* int total_size = 0; */
|
|
3396 reset_lcrecord_stats ();
|
|
3397
|
|
3398 /* First go through and call all the finalize methods.
|
|
3399 Then go through and free the objects. There used to
|
|
3400 be only one loop here, with the call to the finalizer
|
|
3401 occurring directly before the xfree() below. That
|
|
3402 is marginally faster but much less safe -- if the
|
|
3403 finalize method for an object needs to reference any
|
|
3404 other objects contained within it (and many do),
|
|
3405 we could easily be screwed by having already freed that
|
|
3406 other object. */
|
|
3407
|
|
3408 for (header = *prev; header; header = header->next)
|
|
3409 {
|
|
3410 struct lrecord_header *h = &(header->lheader);
|
|
3411 if (!MARKED_RECORD_HEADER_P (h) && ! (header->free))
|
|
3412 {
|
211
|
3413 if (LHEADER_IMPLEMENTATION (h)->finalizer)
|
|
3414 ((LHEADER_IMPLEMENTATION (h)->finalizer) (h, 0));
|
0
|
3415 }
|
|
3416 }
|
|
3417
|
|
3418 for (header = *prev; header; )
|
|
3419 {
|
|
3420 struct lrecord_header *h = &(header->lheader);
|
|
3421 if (MARKED_RECORD_HEADER_P (h))
|
|
3422 {
|
|
3423 UNMARK_RECORD_HEADER (h);
|
|
3424 num_used++;
|
|
3425 /* total_size += ((n->implementation->size_in_bytes) (h));*/
|
|
3426 prev = &(header->next);
|
|
3427 header = *prev;
|
|
3428 tick_lcrecord_stats (h, 0);
|
|
3429 }
|
|
3430 else
|
|
3431 {
|
|
3432 struct lcrecord_header *next = header->next;
|
|
3433 *prev = next;
|
|
3434 tick_lcrecord_stats (h, 1);
|
|
3435 /* used to call finalizer right here. */
|
|
3436 xfree (header);
|
|
3437 header = next;
|
|
3438 }
|
|
3439 }
|
|
3440 *used = num_used;
|
|
3441 /* *total = total_size; */
|
|
3442 }
|
|
3443
|
207
|
3444 #ifndef LRECORD_VECTOR
|
|
3445
|
0
|
3446 static void
|
183
|
3447 sweep_vectors_1 (Lisp_Object *prev,
|
0
|
3448 int *used, int *total, int *storage)
|
|
3449 {
|
|
3450 Lisp_Object vector;
|
|
3451 int num_used = 0;
|
|
3452 int total_size = 0;
|
|
3453 int total_storage = 0;
|
|
3454
|
|
3455 for (vector = *prev; VECTORP (vector); )
|
|
3456 {
|
|
3457 struct Lisp_Vector *v = XVECTOR (vector);
|
|
3458 int len = v->size;
|
|
3459 if (len < 0) /* marked */
|
|
3460 {
|
|
3461 len = - (len + 1);
|
|
3462 v->size = len;
|
|
3463 total_size += len;
|
|
3464 total_storage += (MALLOC_OVERHEAD
|
|
3465 + sizeof (struct Lisp_Vector)
|
|
3466 + (len - 1 + 1) * sizeof (Lisp_Object));
|
|
3467 num_used++;
|
|
3468 prev = &(vector_next (v));
|
|
3469 vector = *prev;
|
|
3470 }
|
|
3471 else
|
|
3472 {
|
|
3473 Lisp_Object next = vector_next (v);
|
|
3474 *prev = next;
|
|
3475 xfree (v);
|
|
3476 vector = next;
|
|
3477 }
|
|
3478 }
|
|
3479 *used = num_used;
|
|
3480 *total = total_size;
|
|
3481 *storage = total_storage;
|
|
3482 }
|
|
3483
|
207
|
3484 #endif /* ! LRECORD_VECTOR */
|
|
3485
|
0
|
3486 static void
|
183
|
3487 sweep_bit_vectors_1 (Lisp_Object *prev,
|
0
|
3488 int *used, int *total, int *storage)
|
|
3489 {
|
|
3490 Lisp_Object bit_vector;
|
|
3491 int num_used = 0;
|
|
3492 int total_size = 0;
|
|
3493 int total_storage = 0;
|
|
3494
|
|
3495 /* BIT_VECTORP fails because the objects are marked, which changes
|
|
3496 their implementation */
|
|
3497 for (bit_vector = *prev; !EQ (bit_vector, Qzero); )
|
|
3498 {
|
|
3499 struct Lisp_Bit_Vector *v = XBIT_VECTOR (bit_vector);
|
|
3500 int len = v->size;
|
|
3501 if (MARKED_RECORD_P (bit_vector))
|
|
3502 {
|
|
3503 UNMARK_RECORD_HEADER (&(v->lheader));
|
|
3504 total_size += len;
|
|
3505 total_storage += (MALLOC_OVERHEAD
|
|
3506 + sizeof (struct Lisp_Bit_Vector)
|
|
3507 + (BIT_VECTOR_LONG_STORAGE (len) - 1)
|
|
3508 * sizeof (long));
|
|
3509 num_used++;
|
|
3510 prev = &(bit_vector_next (v));
|
|
3511 bit_vector = *prev;
|
|
3512 }
|
|
3513 else
|
|
3514 {
|
|
3515 Lisp_Object next = bit_vector_next (v);
|
|
3516 *prev = next;
|
|
3517 xfree (v);
|
|
3518 bit_vector = next;
|
|
3519 }
|
|
3520 }
|
|
3521 *used = num_used;
|
|
3522 *total = total_size;
|
|
3523 *storage = total_storage;
|
|
3524 }
|
|
3525
|
|
3526 /* And the Lord said: Thou shalt use the `c-backslash-region' command
|
|
3527 to make macros prettier. */
|
|
3528
|
|
3529 #ifdef ERROR_CHECK_GC
|
|
3530
|
|
3531 #define SWEEP_FIXED_TYPE_BLOCK(typename, obj_type) \
|
|
3532 do { \
|
|
3533 struct typename##_block *_frob_current; \
|
|
3534 struct typename##_block **_frob_prev; \
|
|
3535 int _frob_limit; \
|
|
3536 int num_free = 0, num_used = 0; \
|
|
3537 \
|
|
3538 for (_frob_prev = ¤t_##typename##_block, \
|
|
3539 _frob_current = current_##typename##_block, \
|
|
3540 _frob_limit = current_##typename##_block_index; \
|
|
3541 _frob_current; \
|
|
3542 ) \
|
|
3543 { \
|
|
3544 int _frob_iii; \
|
|
3545 \
|
|
3546 for (_frob_iii = 0; _frob_iii < _frob_limit; _frob_iii++) \
|
|
3547 { \
|
|
3548 obj_type *_frob_victim = &(_frob_current->block[_frob_iii]); \
|
|
3549 \
|
|
3550 if (FREE_STRUCT_P (_frob_victim)) \
|
|
3551 { \
|
|
3552 num_free++; \
|
|
3553 } \
|
|
3554 else if (!MARKED_##typename##_P (_frob_victim)) \
|
|
3555 { \
|
|
3556 num_free++; \
|
|
3557 FREE_FIXED_TYPE (typename, obj_type, _frob_victim); \
|
|
3558 } \
|
|
3559 else \
|
|
3560 { \
|
|
3561 num_used++; \
|
|
3562 UNMARK_##typename (_frob_victim); \
|
|
3563 } \
|
|
3564 } \
|
|
3565 _frob_prev = &(_frob_current->prev); \
|
|
3566 _frob_current = _frob_current->prev; \
|
|
3567 _frob_limit = countof (current_##typename##_block->block); \
|
|
3568 } \
|
|
3569 \
|
|
3570 gc_count_num_##typename##_in_use = num_used; \
|
|
3571 gc_count_num_##typename##_freelist = num_free; \
|
|
3572 } while (0)
|
|
3573
|
183
|
3574 #else /* !ERROR_CHECK_GC */
|
0
|
3575
|
|
3576 #define SWEEP_FIXED_TYPE_BLOCK(typename, obj_type) \
|
|
3577 do { \
|
|
3578 struct typename##_block *_frob_current; \
|
|
3579 struct typename##_block **_frob_prev; \
|
|
3580 int _frob_limit; \
|
|
3581 int num_free = 0, num_used = 0; \
|
|
3582 \
|
|
3583 typename##_free_list = 0; \
|
|
3584 \
|
|
3585 for (_frob_prev = ¤t_##typename##_block, \
|
|
3586 _frob_current = current_##typename##_block, \
|
|
3587 _frob_limit = current_##typename##_block_index; \
|
|
3588 _frob_current; \
|
|
3589 ) \
|
|
3590 { \
|
|
3591 int _frob_iii; \
|
|
3592 int _frob_empty = 1; \
|
|
3593 obj_type *_frob_old_free_list = typename##_free_list; \
|
|
3594 \
|
|
3595 for (_frob_iii = 0; _frob_iii < _frob_limit; _frob_iii++) \
|
|
3596 { \
|
|
3597 obj_type *_frob_victim = &(_frob_current->block[_frob_iii]); \
|
|
3598 \
|
|
3599 if (FREE_STRUCT_P (_frob_victim)) \
|
|
3600 { \
|
|
3601 num_free++; \
|
|
3602 PUT_FIXED_TYPE_ON_FREE_LIST (typename, obj_type, _frob_victim); \
|
|
3603 } \
|
|
3604 else if (!MARKED_##typename##_P (_frob_victim)) \
|
|
3605 { \
|
|
3606 num_free++; \
|
|
3607 FREE_FIXED_TYPE (typename, obj_type, _frob_victim); \
|
|
3608 } \
|
|
3609 else \
|
|
3610 { \
|
|
3611 _frob_empty = 0; \
|
|
3612 num_used++; \
|
|
3613 UNMARK_##typename (_frob_victim); \
|
|
3614 } \
|
|
3615 } \
|
|
3616 if (!_frob_empty) \
|
|
3617 { \
|
|
3618 _frob_prev = &(_frob_current->prev); \
|
|
3619 _frob_current = _frob_current->prev; \
|
|
3620 } \
|
|
3621 else if (_frob_current == current_##typename##_block \
|
|
3622 && !_frob_current->prev) \
|
|
3623 { \
|
|
3624 /* No real point in freeing sole allocation block */ \
|
|
3625 break; \
|
|
3626 } \
|
|
3627 else \
|
|
3628 { \
|
|
3629 struct typename##_block *_frob_victim_block = _frob_current; \
|
|
3630 if (_frob_victim_block == current_##typename##_block) \
|
|
3631 current_##typename##_block_index \
|
|
3632 = countof (current_##typename##_block->block); \
|
|
3633 _frob_current = _frob_current->prev; \
|
|
3634 { \
|
|
3635 *_frob_prev = _frob_current; \
|
|
3636 xfree (_frob_victim_block); \
|
|
3637 /* Restore free list to what it was before victim was swept */ \
|
|
3638 typename##_free_list = _frob_old_free_list; \
|
|
3639 num_free -= _frob_limit; \
|
|
3640 } \
|
|
3641 } \
|
|
3642 _frob_limit = countof (current_##typename##_block->block); \
|
|
3643 } \
|
|
3644 \
|
|
3645 gc_count_num_##typename##_in_use = num_used; \
|
|
3646 gc_count_num_##typename##_freelist = num_free; \
|
|
3647 } while (0)
|
|
3648
|
183
|
3649 #endif /* !ERROR_CHECK_GC */
|
0
|
3650
|
|
3651
|
|
3652
|
|
3653
|
|
3654 static void
|
|
3655 sweep_conses (void)
|
|
3656 {
|
207
|
3657 #ifndef LRECORD_CONS
|
|
3658 # define MARKED_cons_P(ptr) XMARKBIT ((ptr)->car)
|
|
3659 # define UNMARK_cons(ptr) do { XUNMARK ((ptr)->car); } while (0)
|
|
3660 #else /* LRECORD_CONS */
|
|
3661 # define MARKED_cons_P(ptr) MARKED_RECORD_HEADER_P (&((ptr)->lheader))
|
|
3662 # define UNMARK_cons(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
|
|
3663 #endif /* LRECORD_CONS */
|
0
|
3664 #define ADDITIONAL_FREE_cons(ptr)
|
|
3665
|
|
3666 SWEEP_FIXED_TYPE_BLOCK (cons, struct Lisp_Cons);
|
|
3667 }
|
|
3668
|
|
3669 /* Explicitly free a cons cell. */
|
|
3670 void
|
|
3671 free_cons (struct Lisp_Cons *ptr)
|
|
3672 {
|
|
3673 #ifdef ERROR_CHECK_GC
|
|
3674 /* If the CAR is not an int, then it will be a pointer, which will
|
|
3675 always be four-byte aligned. If this cons cell has already been
|
|
3676 placed on the free list, however, its car will probably contain
|
|
3677 a chain pointer to the next cons on the list, which has cleverly
|
|
3678 had all its 0's and 1's inverted. This allows for a quick
|
|
3679 check to make sure we're not freeing something already freed. */
|
|
3680 if (POINTER_TYPE_P (XTYPE (ptr->car)))
|
|
3681 ASSERT_VALID_POINTER (XPNTR (ptr->car));
|
183
|
3682 #endif /* ERROR_CHECK_GC */
|
|
3683
|
0
|
3684 #ifndef ALLOC_NO_POOLS
|
|
3685 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (cons, struct Lisp_Cons, ptr);
|
|
3686 #endif /* ALLOC_NO_POOLS */
|
|
3687 }
|
|
3688
|
|
3689 /* explicitly free a list. You **must make sure** that you have
|
|
3690 created all the cons cells that make up this list and that there
|
|
3691 are no pointers to any of these cons cells anywhere else. If there
|
|
3692 are, you will lose. */
|
|
3693
|
|
3694 void
|
|
3695 free_list (Lisp_Object list)
|
|
3696 {
|
|
3697 Lisp_Object rest, next;
|
|
3698
|
|
3699 for (rest = list; !NILP (rest); rest = next)
|
|
3700 {
|
|
3701 next = XCDR (rest);
|
|
3702 free_cons (XCONS (rest));
|
|
3703 }
|
|
3704 }
|
|
3705
|
|
3706 /* explicitly free an alist. You **must make sure** that you have
|
|
3707 created all the cons cells that make up this alist and that there
|
|
3708 are no pointers to any of these cons cells anywhere else. If there
|
|
3709 are, you will lose. */
|
|
3710
|
|
3711 void
|
|
3712 free_alist (Lisp_Object alist)
|
|
3713 {
|
|
3714 Lisp_Object rest, next;
|
|
3715
|
|
3716 for (rest = alist; !NILP (rest); rest = next)
|
|
3717 {
|
|
3718 next = XCDR (rest);
|
|
3719 free_cons (XCONS (XCAR (rest)));
|
|
3720 free_cons (XCONS (rest));
|
|
3721 }
|
|
3722 }
|
|
3723
|
|
3724 static void
|
|
3725 sweep_compiled_functions (void)
|
|
3726 {
|
|
3727 #define MARKED_compiled_function_P(ptr) \
|
|
3728 MARKED_RECORD_HEADER_P (&((ptr)->lheader))
|
|
3729 #define UNMARK_compiled_function(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
|
|
3730 #define ADDITIONAL_FREE_compiled_function(ptr)
|
|
3731
|
|
3732 SWEEP_FIXED_TYPE_BLOCK (compiled_function, struct Lisp_Compiled_Function);
|
|
3733 }
|
|
3734
|
|
3735
|
|
3736 #ifdef LISP_FLOAT_TYPE
|
|
3737 static void
|
|
3738 sweep_floats (void)
|
|
3739 {
|
|
3740 #define MARKED_float_P(ptr) MARKED_RECORD_HEADER_P (&((ptr)->lheader))
|
|
3741 #define UNMARK_float(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
|
|
3742 #define ADDITIONAL_FREE_float(ptr)
|
|
3743
|
|
3744 SWEEP_FIXED_TYPE_BLOCK (float, struct Lisp_Float);
|
|
3745 }
|
|
3746 #endif /* LISP_FLOAT_TYPE */
|
|
3747
|
|
3748 static void
|
|
3749 sweep_symbols (void)
|
|
3750 {
|
|
3751 #ifndef LRECORD_SYMBOL
|
|
3752 # define MARKED_symbol_P(ptr) XMARKBIT ((ptr)->plist)
|
|
3753 # define UNMARK_symbol(ptr) do { XUNMARK ((ptr)->plist); } while (0)
|
|
3754 #else
|
|
3755 # define MARKED_symbol_P(ptr) MARKED_RECORD_HEADER_P (&((ptr)->lheader))
|
|
3756 # define UNMARK_symbol(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
|
|
3757 #endif /* !LRECORD_SYMBOL */
|
|
3758 #define ADDITIONAL_FREE_symbol(ptr)
|
|
3759
|
|
3760 SWEEP_FIXED_TYPE_BLOCK (symbol, struct Lisp_Symbol);
|
|
3761 }
|
|
3762
|
|
3763
|
|
3764 #ifndef standalone
|
|
3765
|
|
3766 static void
|
|
3767 sweep_extents (void)
|
|
3768 {
|
|
3769 #define MARKED_extent_P(ptr) MARKED_RECORD_HEADER_P (&((ptr)->lheader))
|
|
3770 #define UNMARK_extent(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
|
|
3771 #define ADDITIONAL_FREE_extent(ptr)
|
|
3772
|
|
3773 SWEEP_FIXED_TYPE_BLOCK (extent, struct extent);
|
|
3774 }
|
|
3775
|
|
3776 static void
|
|
3777 sweep_events (void)
|
|
3778 {
|
|
3779 #define MARKED_event_P(ptr) MARKED_RECORD_HEADER_P (&((ptr)->lheader))
|
|
3780 #define UNMARK_event(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
|
|
3781 #define ADDITIONAL_FREE_event(ptr)
|
|
3782
|
|
3783 SWEEP_FIXED_TYPE_BLOCK (event, struct Lisp_Event);
|
|
3784 }
|
|
3785
|
|
3786 static void
|
|
3787 sweep_markers (void)
|
|
3788 {
|
|
3789 #define MARKED_marker_P(ptr) MARKED_RECORD_HEADER_P (&((ptr)->lheader))
|
|
3790 #define UNMARK_marker(ptr) UNMARK_RECORD_HEADER (&((ptr)->lheader))
|
|
3791 #define ADDITIONAL_FREE_marker(ptr) \
|
|
3792 do { Lisp_Object tem; \
|
|
3793 XSETMARKER (tem, ptr); \
|
|
3794 unchain_marker (tem); \
|
|
3795 } while (0)
|
|
3796
|
|
3797 SWEEP_FIXED_TYPE_BLOCK (marker, struct Lisp_Marker);
|
|
3798 }
|
|
3799
|
|
3800 /* Explicitly free a marker. */
|
|
3801 void
|
|
3802 free_marker (struct Lisp_Marker *ptr)
|
|
3803 {
|
|
3804 #ifdef ERROR_CHECK_GC
|
|
3805 /* Perhaps this will catch freeing an already-freed marker. */
|
|
3806 Lisp_Object temmy;
|
|
3807 XSETMARKER (temmy, ptr);
|
|
3808 assert (GC_MARKERP (temmy));
|
183
|
3809 #endif /* ERROR_CHECK_GC */
|
|
3810
|
0
|
3811 #ifndef ALLOC_NO_POOLS
|
|
3812 FREE_FIXED_TYPE_WHEN_NOT_IN_GC (marker, struct Lisp_Marker, ptr);
|
|
3813 #endif /* ALLOC_NO_POOLS */
|
|
3814 }
|
|
3815
|
|
3816 #endif /* not standalone */
|
|
3817
|
|
3818
|
70
|
3819
|
|
3820 #if defined (MULE) && defined (VERIFY_STRING_CHARS_INTEGRITY)
|
|
3821
|
|
3822 static void
|
|
3823 verify_string_chars_integrity (void)
|
|
3824 {
|
|
3825 struct string_chars_block *sb;
|
|
3826
|
|
3827 /* Scan each existing string block sequentially, string by string. */
|
|
3828 for (sb = first_string_chars_block; sb; sb = sb->next)
|
|
3829 {
|
|
3830 int pos = 0;
|
|
3831 /* POS is the index of the next string in the block. */
|
|
3832 while (pos < sb->pos)
|
|
3833 {
|
183
|
3834 struct string_chars *s_chars =
|
70
|
3835 (struct string_chars *) &(sb->string_chars[pos]);
|
|
3836 struct Lisp_String *string;
|
|
3837 int size;
|
|
3838 int fullsize;
|
|
3839
|
|
3840 /* If the string_chars struct is marked as free (i.e. the STRING
|
|
3841 pointer is 0xFFFFFFFF) then this is an unused chunk of string
|
|
3842 storage. (See below.) */
|
|
3843
|
|
3844 if (FREE_STRUCT_P (s_chars))
|
|
3845 {
|
|
3846 fullsize = ((struct unused_string_chars *) s_chars)->fullsize;
|
|
3847 pos += fullsize;
|
|
3848 continue;
|
|
3849 }
|
|
3850
|
|
3851 string = s_chars->string;
|
|
3852 /* Must be 32-bit aligned. */
|
|
3853 assert ((((int) string) & 3) == 0);
|
|
3854
|
|
3855 size = string_length (string);
|
|
3856 fullsize = STRING_FULLSIZE (size);
|
|
3857
|
|
3858 assert (!BIG_STRING_FULLSIZE_P (fullsize));
|
|
3859 assert (string_data (string) == s_chars->chars);
|
|
3860 pos += fullsize;
|
|
3861 }
|
|
3862 assert (pos == sb->pos);
|
|
3863 }
|
|
3864 }
|
|
3865
|
|
3866 #endif /* MULE && ERROR_CHECK_GC */
|
|
3867
|
0
|
3868 /* Compactify string chars, relocating the reference to each --
|
|
3869 free any empty string_chars_block we see. */
|
|
3870 static void
|
|
3871 compact_string_chars (void)
|
|
3872 {
|
|
3873 struct string_chars_block *to_sb = first_string_chars_block;
|
|
3874 int to_pos = 0;
|
|
3875 struct string_chars_block *from_sb;
|
|
3876
|
|
3877 /* Scan each existing string block sequentially, string by string. */
|
|
3878 for (from_sb = first_string_chars_block; from_sb; from_sb = from_sb->next)
|
|
3879 {
|
|
3880 int from_pos = 0;
|
|
3881 /* FROM_POS is the index of the next string in the block. */
|
|
3882 while (from_pos < from_sb->pos)
|
|
3883 {
|
183
|
3884 struct string_chars *from_s_chars =
|
0
|
3885 (struct string_chars *) &(from_sb->string_chars[from_pos]);
|
|
3886 struct string_chars *to_s_chars;
|
|
3887 struct Lisp_String *string;
|
|
3888 int size;
|
|
3889 int fullsize;
|
|
3890
|
|
3891 /* If the string_chars struct is marked as free (i.e. the STRING
|
|
3892 pointer is 0xFFFFFFFF) then this is an unused chunk of string
|
|
3893 storage. This happens under Mule when a string's size changes
|
|
3894 in such a way that its fullsize changes. (Strings can change
|
|
3895 size because a different-length character can be substituted
|
|
3896 for another character.) In this case, after the bogus string
|
|
3897 pointer is the "fullsize" of this entry, i.e. how many bytes
|
|
3898 to skip. */
|
|
3899
|
|
3900 if (FREE_STRUCT_P (from_s_chars))
|
|
3901 {
|
|
3902 fullsize = ((struct unused_string_chars *) from_s_chars)->fullsize;
|
|
3903 from_pos += fullsize;
|
|
3904 continue;
|
|
3905 }
|
|
3906
|
|
3907 string = from_s_chars->string;
|
|
3908 assert (!(FREE_STRUCT_P (string)));
|
|
3909
|
|
3910 size = string_length (string);
|
|
3911 fullsize = STRING_FULLSIZE (size);
|
|
3912
|
|
3913 if (BIG_STRING_FULLSIZE_P (fullsize))
|
|
3914 abort ();
|
|
3915
|
|
3916 /* Just skip it if it isn't marked. */
|
207
|
3917 #ifdef LRECORD_STRING
|
|
3918 if (! MARKED_RECORD_HEADER_P (&(string->lheader)))
|
|
3919 #else
|
0
|
3920 if (!XMARKBIT (string->plist))
|
207
|
3921 #endif
|
0
|
3922 {
|
|
3923 from_pos += fullsize;
|
|
3924 continue;
|
|
3925 }
|
|
3926
|
|
3927 /* If it won't fit in what's left of TO_SB, close TO_SB out
|
|
3928 and go on to the next string_chars_block. We know that TO_SB
|
|
3929 cannot advance past FROM_SB here since FROM_SB is large enough
|
|
3930 to currently contain this string. */
|
|
3931 if ((to_pos + fullsize) > countof (to_sb->string_chars))
|
|
3932 {
|
|
3933 to_sb->pos = to_pos;
|
|
3934 to_sb = to_sb->next;
|
|
3935 to_pos = 0;
|
|
3936 }
|
183
|
3937
|
0
|
3938 /* Compute new address of this string
|
|
3939 and update TO_POS for the space being used. */
|
|
3940 to_s_chars = (struct string_chars *) &(to_sb->string_chars[to_pos]);
|
|
3941
|
|
3942 /* Copy the string_chars to the new place. */
|
|
3943 if (from_s_chars != to_s_chars)
|
|
3944 memmove (to_s_chars, from_s_chars, fullsize);
|
|
3945
|
|
3946 /* Relocate FROM_S_CHARS's reference */
|
|
3947 set_string_data (string, &(to_s_chars->chars[0]));
|
183
|
3948
|
0
|
3949 from_pos += fullsize;
|
|
3950 to_pos += fullsize;
|
|
3951 }
|
|
3952 }
|
|
3953
|
183
|
3954 /* Set current to the last string chars block still used and
|
0
|
3955 free any that follow. */
|
|
3956 {
|
|
3957 struct string_chars_block *victim;
|
|
3958
|
|
3959 for (victim = to_sb->next; victim; )
|
|
3960 {
|
|
3961 struct string_chars_block *next = victim->next;
|
|
3962 xfree (victim);
|
|
3963 victim = next;
|
|
3964 }
|
|
3965
|
|
3966 current_string_chars_block = to_sb;
|
|
3967 current_string_chars_block->pos = to_pos;
|
|
3968 current_string_chars_block->next = 0;
|
|
3969 }
|
|
3970 }
|
|
3971
|
|
3972 #if 1 /* Hack to debug missing purecopy's */
|
|
3973 static int debug_string_purity;
|
|
3974
|
|
3975 static void
|
|
3976 debug_string_purity_print (struct Lisp_String *p)
|
|
3977 {
|
|
3978 Charcount i;
|
|
3979 Charcount s = string_char_length (p);
|
|
3980 putc ('\"', stderr);
|
|
3981 for (i = 0; i < s; i++)
|
|
3982 {
|
|
3983 Emchar ch = string_char (p, i);
|
|
3984 if (ch < 32 || ch >= 126)
|
|
3985 stderr_out ("\\%03o", ch);
|
|
3986 else if (ch == '\\' || ch == '\"')
|
|
3987 stderr_out ("\\%c", ch);
|
|
3988 else
|
|
3989 stderr_out ("%c", ch);
|
|
3990 }
|
|
3991 stderr_out ("\"\n");
|
|
3992 }
|
183
|
3993 #endif /* 1 */
|
0
|
3994
|
|
3995
|
|
3996 static void
|
|
3997 sweep_strings (void)
|
|
3998 {
|
|
3999 int num_small_used = 0, num_small_bytes = 0, num_bytes = 0;
|
|
4000 int debug = debug_string_purity;
|
|
4001
|
207
|
4002 #ifdef LRECORD_STRING
|
|
4003
|
|
4004 # define MARKED_string_P(ptr) MARKED_RECORD_HEADER_P (&((ptr)->lheader))
|
|
4005 # define UNMARK_string(ptr) \
|
|
4006 do { struct Lisp_String *p = (ptr); \
|
|
4007 int size = string_length (p); \
|
|
4008 UNMARK_RECORD_HEADER (&(p->lheader)); \
|
|
4009 num_bytes += size; \
|
|
4010 if (!BIG_STRING_SIZE_P (size)) \
|
|
4011 { num_small_bytes += size; \
|
|
4012 num_small_used++; \
|
|
4013 } \
|
|
4014 if (debug) debug_string_purity_print (p); \
|
|
4015 } while (0)
|
|
4016 # define ADDITIONAL_FREE_string(p) \
|
|
4017 do { int size = string_length (p); \
|
|
4018 if (BIG_STRING_SIZE_P (size)) \
|
|
4019 xfree_1 (CHARS_TO_STRING_CHAR (string_data (p))); \
|
|
4020 } while (0)
|
|
4021
|
|
4022 #else
|
|
4023
|
|
4024 # define MARKED_string_P(ptr) XMARKBIT ((ptr)->plist)
|
|
4025 # define UNMARK_string(ptr) \
|
0
|
4026 do { struct Lisp_String *p = (ptr); \
|
|
4027 int size = string_length (p); \
|
|
4028 XUNMARK (p->plist); \
|
|
4029 num_bytes += size; \
|
|
4030 if (!BIG_STRING_SIZE_P (size)) \
|
|
4031 { num_small_bytes += size; \
|
|
4032 num_small_used++; \
|
|
4033 } \
|
|
4034 if (debug) debug_string_purity_print (p); \
|
|
4035 } while (0)
|
207
|
4036 # define ADDITIONAL_FREE_string(p) \
|
0
|
4037 do { int size = string_length (p); \
|
|
4038 if (BIG_STRING_SIZE_P (size)) \
|
|
4039 xfree_1 (CHARS_TO_STRING_CHAR (string_data (p))); \
|
|
4040 } while (0)
|
|
4041
|
207
|
4042 #endif /* ! LRECORD_STRING */
|
|
4043
|
0
|
4044 SWEEP_FIXED_TYPE_BLOCK (string, struct Lisp_String);
|
|
4045
|
|
4046 gc_count_num_short_string_in_use = num_small_used;
|
|
4047 gc_count_string_total_size = num_bytes;
|
|
4048 gc_count_short_string_total_size = num_small_bytes;
|
|
4049 }
|
|
4050
|
|
4051
|
|
4052 /* I hate duplicating all this crap! */
|
|
4053 static int
|
|
4054 marked_p (Lisp_Object obj)
|
|
4055 {
|
207
|
4056 if (EQ (obj, Qnull_pointer)) return 1;
|
0
|
4057 if (!POINTER_TYPE_P (XGCTYPE (obj))) return 1;
|
|
4058 if (PURIFIED (XPNTR (obj))) return 1;
|
|
4059 switch (XGCTYPE (obj))
|
|
4060 {
|
207
|
4061 #ifndef LRECORD_CONS
|
185
|
4062 case Lisp_Type_Cons:
|
0
|
4063 return XMARKBIT (XCAR (obj));
|
207
|
4064 #endif
|
185
|
4065 case Lisp_Type_Record:
|
0
|
4066 return MARKED_RECORD_HEADER_P (XRECORD_LHEADER (obj));
|
207
|
4067 #ifndef LRECORD_STRING
|
185
|
4068 case Lisp_Type_String:
|
0
|
4069 return XMARKBIT (XSTRING (obj)->plist);
|
207
|
4070 #endif /* ! LRECORD_STRING */
|
185
|
4071 #ifndef LRECORD_VECTOR
|
|
4072 case Lisp_Type_Vector:
|
173
|
4073 return XVECTOR_LENGTH (obj) < 0;
|
185
|
4074 #endif /* !LRECORD_VECTOR */
|
0
|
4075 #ifndef LRECORD_SYMBOL
|
185
|
4076 case Lisp_Type_Symbol:
|
0
|
4077 return XMARKBIT (XSYMBOL (obj)->plist);
|
|
4078 #endif
|
|
4079 default:
|
|
4080 abort ();
|
|
4081 }
|
|
4082 return 0; /* suppress compiler warning */
|
|
4083 }
|
|
4084
|
|
4085 static void
|
|
4086 gc_sweep (void)
|
|
4087 {
|
|
4088 /* Free all unmarked records. Do this at the very beginning,
|
|
4089 before anything else, so that the finalize methods can safely
|
|
4090 examine items in the objects. sweep_lcrecords_1() makes
|
|
4091 sure to call all the finalize methods *before* freeing anything,
|
|
4092 to complete the safety. */
|
|
4093 {
|
|
4094 int ignored;
|
|
4095 sweep_lcrecords_1 (&all_lcrecords, &ignored);
|
|
4096 }
|
|
4097
|
|
4098 compact_string_chars ();
|
|
4099
|
|
4100 /* Finalize methods below (called through the ADDITIONAL_FREE_foo
|
|
4101 macros) must be *extremely* careful to make sure they're not
|
|
4102 referencing freed objects. The only two existing finalize
|
|
4103 methods (for strings and markers) pass muster -- the string
|
|
4104 finalizer doesn't look at anything but its own specially-
|
|
4105 created block, and the marker finalizer only looks at live
|
|
4106 buffers (which will never be freed) and at the markers before
|
|
4107 and after it in the chain (which, by induction, will never be
|
|
4108 freed because if so, they would have already removed themselves
|
|
4109 from the chain). */
|
|
4110
|
|
4111 /* Put all unmarked strings on free list, free'ing the string chars
|
|
4112 of large unmarked strings */
|
|
4113 sweep_strings ();
|
|
4114
|
|
4115 /* Put all unmarked conses on free list */
|
|
4116 sweep_conses ();
|
|
4117
|
207
|
4118 #ifndef LRECORD_VECTOR
|
0
|
4119 /* Free all unmarked vectors */
|
|
4120 sweep_vectors_1 (&all_vectors,
|
|
4121 &gc_count_num_vector_used, &gc_count_vector_total_size,
|
|
4122 &gc_count_vector_storage);
|
207
|
4123 #endif
|
0
|
4124
|
|
4125 /* Free all unmarked bit vectors */
|
|
4126 sweep_bit_vectors_1 (&all_bit_vectors,
|
|
4127 &gc_count_num_bit_vector_used,
|
|
4128 &gc_count_bit_vector_total_size,
|
|
4129 &gc_count_bit_vector_storage);
|
|
4130
|
|
4131 /* Free all unmarked compiled-function objects */
|
|
4132 sweep_compiled_functions ();
|
|
4133
|
|
4134 #ifdef LISP_FLOAT_TYPE
|
|
4135 /* Put all unmarked floats on free list */
|
|
4136 sweep_floats ();
|
|
4137 #endif
|
|
4138
|
|
4139 /* Put all unmarked symbols on free list */
|
|
4140 sweep_symbols ();
|
|
4141
|
|
4142 /* Put all unmarked extents on free list */
|
|
4143 sweep_extents ();
|
|
4144
|
|
4145 /* Put all unmarked markers on free list.
|
|
4146 Dechain each one first from the buffer into which it points. */
|
|
4147 sweep_markers ();
|
|
4148
|
|
4149 sweep_events ();
|
|
4150
|
|
4151 }
|
|
4152
|
|
4153 /* Clearing for disksave. */
|
|
4154
|
|
4155 extern Lisp_Object Vprocess_environment;
|
|
4156 extern Lisp_Object Vdoc_directory;
|
|
4157 extern Lisp_Object Vconfigure_info_directory;
|
|
4158 extern Lisp_Object Vload_path;
|
|
4159 extern Lisp_Object Vload_history;
|
|
4160 extern Lisp_Object Vshell_file_name;
|
|
4161
|
|
4162 void
|
|
4163 disksave_object_finalization (void)
|
|
4164 {
|
|
4165 /* It's important that certain information from the environment not get
|
|
4166 dumped with the executable (pathnames, environment variables, etc.).
|
|
4167 To make it easier to tell when this has happend with strings(1) we
|
|
4168 clear some known-to-be-garbage blocks of memory, so that leftover
|
|
4169 results of old evaluation don't look like potential problems.
|
|
4170 But first we set some notable variables to nil and do one more GC,
|
|
4171 to turn those strings into garbage.
|
|
4172 */
|
|
4173
|
|
4174 /* Yeah, this list is pretty ad-hoc... */
|
|
4175 Vprocess_environment = Qnil;
|
|
4176 Vexec_directory = Qnil;
|
|
4177 Vdata_directory = Qnil;
|
110
|
4178 Vsite_directory = Qnil;
|
0
|
4179 Vdoc_directory = Qnil;
|
|
4180 Vconfigure_info_directory = Qnil;
|
|
4181 Vexec_path = Qnil;
|
|
4182 Vload_path = Qnil;
|
|
4183 /* Vdump_load_path = Qnil; */
|
233
|
4184 #if defined(LOADHIST) && !(defined(LOADHIST_DUMPED) || \
|
|
4185 defined(LOADHIST_BUILTIN))
|
0
|
4186 Vload_history = Qnil;
|
233
|
4187 #endif
|
0
|
4188 Vshell_file_name = Qnil;
|
|
4189
|
|
4190 garbage_collect_1 ();
|
|
4191
|
|
4192 /* Run the disksave finalization methods of all live objects. */
|
|
4193 disksave_object_finalization_1 ();
|
|
4194
|
|
4195 /* Zero out the unused portion of purespace */
|
|
4196 if (!pure_lossage)
|
|
4197 memset ( (char *) (PUREBEG + pureptr), 0,
|
171
|
4198 (((char *) (PUREBEG + get_PURESIZE())) -
|
0
|
4199 ((char *) (PUREBEG + pureptr))));
|
|
4200
|
|
4201 /* Zero out the uninitialized (really, unused) part of the containers
|
|
4202 for the live strings. */
|
|
4203 {
|
|
4204 struct string_chars_block *scb;
|
|
4205 for (scb = first_string_chars_block; scb; scb = scb->next)
|
|
4206 /* from the block's fill ptr to the end */
|
|
4207 memset ((scb->string_chars + scb->pos), 0,
|
|
4208 sizeof (scb->string_chars) - scb->pos);
|
|
4209 }
|
|
4210
|
|
4211 /* There, that ought to be enough... */
|
|
4212
|
|
4213 }
|
|
4214
|
|
4215
|
|
4216 Lisp_Object
|
|
4217 restore_gc_inhibit (Lisp_Object val)
|
|
4218 {
|
|
4219 gc_currently_forbidden = XINT (val);
|
|
4220 return val;
|
|
4221 }
|
|
4222
|
|
4223 /* Maybe we want to use this when doing a "panic" gc after memory_full()? */
|
|
4224 static int gc_hooks_inhibited;
|
|
4225
|
|
4226
|
|
4227 void
|
|
4228 garbage_collect_1 (void)
|
|
4229 {
|
|
4230 char stack_top_variable;
|
|
4231 extern char *stack_bottom;
|
|
4232 int i;
|
163
|
4233 struct frame *f = selected_frame ();
|
0
|
4234 int speccount = specpdl_depth ();
|
|
4235 Lisp_Object pre_gc_cursor = Qnil;
|
|
4236 struct gcpro gcpro1;
|
|
4237
|
163
|
4238 int cursor_changed = 0;
|
0
|
4239
|
|
4240 if (gc_in_progress != 0)
|
|
4241 return;
|
|
4242
|
|
4243 if (gc_currently_forbidden || in_display)
|
|
4244 return;
|
|
4245
|
|
4246 if (preparing_for_armageddon)
|
|
4247 return;
|
|
4248
|
163
|
4249 GCPRO1 (pre_gc_cursor);
|
0
|
4250
|
|
4251 /* Very important to prevent GC during any of the following
|
|
4252 stuff that might run Lisp code; otherwise, we'll likely
|
|
4253 have infinite GC recursion. */
|
|
4254 record_unwind_protect (restore_gc_inhibit,
|
|
4255 make_int (gc_currently_forbidden));
|
|
4256 gc_currently_forbidden = 1;
|
|
4257
|
|
4258 if (!gc_hooks_inhibited)
|
|
4259 run_hook_trapping_errors ("Error in pre-gc-hook", Qpre_gc_hook);
|
|
4260
|
|
4261 /* Now show the GC cursor/message. */
|
|
4262 if (!noninteractive)
|
|
4263 {
|
163
|
4264 if (FRAME_WIN_P (f))
|
0
|
4265 {
|
163
|
4266 Lisp_Object frame = make_frame (f);
|
|
4267 Lisp_Object cursor = glyph_image_instance (Vgc_pointer_glyph,
|
|
4268 FRAME_SELECTED_WINDOW (f),
|
|
4269 ERROR_ME_NOT, 1);
|
|
4270 pre_gc_cursor = f->pointer;
|
|
4271 if (POINTER_IMAGE_INSTANCEP (cursor)
|
|
4272 /* don't change if we don't know how to change back. */
|
|
4273 && POINTER_IMAGE_INSTANCEP (pre_gc_cursor))
|
0
|
4274 {
|
163
|
4275 cursor_changed = 1;
|
|
4276 Fset_frame_pointer (frame, cursor);
|
0
|
4277 }
|
|
4278 }
|
163
|
4279
|
|
4280 /* Don't print messages to the stream device. */
|
|
4281 if (!cursor_changed && !FRAME_STREAM_P (f))
|
|
4282 {
|
|
4283 char *msg = (STRINGP (Vgc_message)
|
|
4284 ? GETTEXT ((char *) XSTRING_DATA (Vgc_message))
|
|
4285 : 0);
|
|
4286 Lisp_Object args[2], whole_msg;
|
|
4287 args[0] = build_string (msg ? msg :
|
|
4288 GETTEXT ((CONST char *) gc_default_message));
|
|
4289 args[1] = build_string ("...");
|
|
4290 whole_msg = Fconcat (2, args);
|
|
4291 echo_area_message (f, (Bufbyte *) 0, whole_msg, 0, -1,
|
|
4292 Qgarbage_collecting);
|
|
4293 }
|
0
|
4294 }
|
|
4295
|
|
4296 /***** Now we actually start the garbage collection. */
|
|
4297
|
|
4298 gc_in_progress = 1;
|
|
4299
|
|
4300 gc_generation_number[0]++;
|
|
4301
|
|
4302 #if MAX_SAVE_STACK > 0
|
|
4303
|
|
4304 /* Save a copy of the contents of the stack, for debugging. */
|
|
4305 if (!purify_flag)
|
|
4306 {
|
|
4307 i = &stack_top_variable - stack_bottom;
|
|
4308 if (i < 0) i = -i;
|
|
4309 if (i < MAX_SAVE_STACK)
|
|
4310 {
|
|
4311 if (stack_copy == 0)
|
|
4312 stack_copy = (char *) malloc (stack_copy_size = i);
|
|
4313 else if (stack_copy_size < i)
|
|
4314 stack_copy = (char *) realloc (stack_copy, (stack_copy_size = i));
|
|
4315 if (stack_copy)
|
|
4316 {
|
|
4317 if ((int) (&stack_top_variable - stack_bottom) > 0)
|
|
4318 memcpy (stack_copy, stack_bottom, i);
|
|
4319 else
|
|
4320 memcpy (stack_copy, &stack_top_variable, i);
|
|
4321 }
|
|
4322 }
|
|
4323 }
|
|
4324 #endif /* MAX_SAVE_STACK > 0 */
|
|
4325
|
|
4326 /* Do some totally ad-hoc resource clearing. */
|
|
4327 /* #### generalize this? */
|
|
4328 clear_event_resource ();
|
|
4329 cleanup_specifiers ();
|
|
4330
|
|
4331 /* Mark all the special slots that serve as the roots of accessibility. */
|
|
4332 {
|
|
4333 struct gcpro *tail;
|
|
4334 struct catchtag *catch;
|
|
4335 struct backtrace *backlist;
|
|
4336 struct specbinding *bind;
|
|
4337
|
|
4338 for (i = 0; i < staticidx; i++)
|
|
4339 {
|
|
4340 #ifdef GDB_SUCKS
|
|
4341 printf ("%d\n", i);
|
|
4342 debug_print (*staticvec[i]);
|
|
4343 #endif
|
|
4344 mark_object (*(staticvec[i]));
|
|
4345 }
|
|
4346
|
|
4347 for (tail = gcprolist; tail; tail = tail->next)
|
|
4348 {
|
|
4349 for (i = 0; i < tail->nvars; i++)
|
|
4350 mark_object (tail->var[i]);
|
|
4351 }
|
|
4352
|
|
4353 for (bind = specpdl; bind != specpdl_ptr; bind++)
|
|
4354 {
|
|
4355 mark_object (bind->symbol);
|
|
4356 mark_object (bind->old_value);
|
|
4357 }
|
|
4358
|
|
4359 for (catch = catchlist; catch; catch = catch->next)
|
|
4360 {
|
|
4361 mark_object (catch->tag);
|
|
4362 mark_object (catch->val);
|
|
4363 }
|
|
4364
|
|
4365 for (backlist = backtrace_list; backlist; backlist = backlist->next)
|
|
4366 {
|
|
4367 int nargs = backlist->nargs;
|
|
4368
|
|
4369 mark_object (*backlist->function);
|
|
4370 if (nargs == UNEVALLED || nargs == MANY)
|
|
4371 mark_object (backlist->args[0]);
|
|
4372 else
|
|
4373 for (i = 0; i < nargs; i++)
|
|
4374 mark_object (backlist->args[i]);
|
|
4375 }
|
|
4376
|
|
4377 mark_redisplay (mark_object);
|
100
|
4378 #ifndef WINDOWSNT
|
0
|
4379 mark_profiling_info (mark_object);
|
100
|
4380 #endif
|
0
|
4381 }
|
|
4382
|
|
4383 /* OK, now do the after-mark stuff. This is for things that
|
|
4384 are only marked when something else is marked (e.g. weak hashtables).
|
|
4385 There may be complex dependencies between such objects -- e.g.
|
|
4386 a weak hashtable might be unmarked, but after processing a later
|
|
4387 weak hashtable, the former one might get marked. So we have to
|
|
4388 iterate until nothing more gets marked. */
|
|
4389 {
|
|
4390 int did_mark;
|
|
4391 /* Need to iterate until there's nothing more to mark, in case
|
|
4392 of chains of mark dependencies. */
|
|
4393 do
|
|
4394 {
|
|
4395 did_mark = 0;
|
|
4396 did_mark += !!finish_marking_weak_hashtables (marked_p, mark_object);
|
|
4397 did_mark += !!finish_marking_weak_lists (marked_p, mark_object);
|
|
4398 }
|
|
4399 while (did_mark);
|
|
4400 }
|
|
4401
|
|
4402 /* And prune (this needs to be called after everything else has been
|
|
4403 marked and before we do any sweeping). */
|
|
4404 /* #### this is somewhat ad-hoc and should probably be an object
|
|
4405 method */
|
|
4406 prune_weak_hashtables (marked_p);
|
|
4407 prune_weak_lists (marked_p);
|
|
4408 prune_specifiers (marked_p);
|
70
|
4409 prune_syntax_tables (marked_p);
|
0
|
4410
|
|
4411 gc_sweep ();
|
|
4412
|
|
4413 consing_since_gc = 0;
|
|
4414 #ifndef DEBUG_XEMACS
|
|
4415 /* Allow you to set it really fucking low if you really want ... */
|
|
4416 if (gc_cons_threshold < 10000)
|
|
4417 gc_cons_threshold = 10000;
|
|
4418 #endif
|
|
4419
|
|
4420 gc_in_progress = 0;
|
|
4421
|
|
4422 /******* End of garbage collection ********/
|
|
4423
|
|
4424 run_hook_trapping_errors ("Error in post-gc-hook", Qpost_gc_hook);
|
|
4425
|
|
4426 /* Now remove the GC cursor/message */
|
|
4427 if (!noninteractive)
|
|
4428 {
|
163
|
4429 if (cursor_changed)
|
|
4430 Fset_frame_pointer (make_frame (f), pre_gc_cursor);
|
|
4431 else if (!FRAME_STREAM_P (f))
|
0
|
4432 {
|
|
4433 char *msg = (STRINGP (Vgc_message)
|
14
|
4434 ? GETTEXT ((char *) XSTRING_DATA (Vgc_message))
|
0
|
4435 : 0);
|
|
4436
|
|
4437 /* Show "...done" only if the echo area would otherwise be empty. */
|
183
|
4438 if (NILP (clear_echo_area (selected_frame (),
|
0
|
4439 Qgarbage_collecting, 0)))
|
|
4440 {
|
|
4441 Lisp_Object args[2], whole_msg;
|
|
4442 args[0] = build_string (msg ? msg :
|
|
4443 GETTEXT ((CONST char *)
|
|
4444 gc_default_message));
|
|
4445 args[1] = build_string ("... done");
|
|
4446 whole_msg = Fconcat (2, args);
|
|
4447 echo_area_message (selected_frame (), (Bufbyte *) 0,
|
|
4448 whole_msg, 0, -1,
|
|
4449 Qgarbage_collecting);
|
|
4450 }
|
|
4451 }
|
|
4452 }
|
|
4453
|
|
4454 /* now stop inhibiting GC */
|
|
4455 unbind_to (speccount, Qnil);
|
|
4456
|
|
4457 if (!breathing_space)
|
|
4458 {
|
|
4459 breathing_space = (void *) malloc (4096 - MALLOC_OVERHEAD);
|
|
4460 }
|
|
4461
|
|
4462 UNGCPRO;
|
|
4463 return;
|
|
4464 }
|
|
4465
|
|
4466 #ifdef EMACS_BTL
|
|
4467 /* This isn't actually called. BTL recognizes the stack frame of the top
|
|
4468 of the garbage collector by noting that PC is between &garbage_collect_1
|
|
4469 and &BTL_after_garbage_collect_1_stub. So this fn must be right here.
|
|
4470 There's not any other way to know the address of the end of a function.
|
|
4471 */
|
|
4472 void BTL_after_garbage_collect_1_stub () { abort (); }
|
183
|
4473 #endif /* EMACS_BTL */
|
0
|
4474
|
|
4475 /* Debugging aids. */
|
|
4476
|
|
4477 static Lisp_Object
|
|
4478 gc_plist_hack (CONST char *name, int value, Lisp_Object tail)
|
|
4479 {
|
|
4480 /* C doesn't have local functions (or closures, or GC, or readable syntax,
|
|
4481 or portable numeric datatypes, or bit-vectors, or characters, or
|
|
4482 arrays, or exceptions, or ...) */
|
173
|
4483 return cons3 (intern (name), make_int (value), tail);
|
0
|
4484 }
|
|
4485
|
|
4486 #define HACK_O_MATIC(type, name, pl) \
|
|
4487 { \
|
|
4488 int s = 0; \
|
|
4489 struct type##_block *x = current_##type##_block; \
|
|
4490 while (x) { s += sizeof (*x) + MALLOC_OVERHEAD; x = x->prev; } \
|
|
4491 (pl) = gc_plist_hack ((name), s, (pl)); \
|
|
4492 }
|
|
4493
|
20
|
4494 DEFUN ("garbage-collect", Fgarbage_collect, 0, 0, "", /*
|
0
|
4495 Reclaim storage for Lisp objects no longer needed.
|
|
4496 Returns info on amount of space in use:
|
|
4497 ((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS)
|
|
4498 (USED-MARKERS . FREE-MARKERS) USED-STRING-CHARS USED-VECTOR-SLOTS
|
183
|
4499 PLIST)
|
0
|
4500 where `PLIST' is a list of alternating keyword/value pairs providing
|
|
4501 more detailed information.
|
|
4502 Garbage collection happens automatically if you cons more than
|
|
4503 `gc-cons-threshold' bytes of Lisp data since previous garbage collection.
|
20
|
4504 */
|
|
4505 ())
|
0
|
4506 {
|
|
4507 Lisp_Object pl = Qnil;
|
|
4508 Lisp_Object ret[6];
|
|
4509 int i;
|
207
|
4510 #ifdef LRECORD_VECTOR
|
243
|
4511 int gc_count_vector_total_size = 0;
|
207
|
4512 #endif
|
0
|
4513
|
104
|
4514 if (purify_flag && pure_lossage)
|
|
4515 {
|
|
4516 return Qnil;
|
|
4517 }
|
|
4518
|
0
|
4519 garbage_collect_1 ();
|
|
4520
|
|
4521 for (i = 0; i < last_lrecord_type_index_assigned; i++)
|
|
4522 {
|
183
|
4523 if (lcrecord_stats[i].bytes_in_use != 0
|
0
|
4524 || lcrecord_stats[i].bytes_freed != 0
|
|
4525 || lcrecord_stats[i].instances_on_free_list != 0)
|
|
4526 {
|
|
4527 char buf [255];
|
|
4528 CONST char *name = lrecord_implementations_table[i]->name;
|
|
4529 int len = strlen (name);
|
207
|
4530 #ifdef LRECORD_VECTOR
|
|
4531 /* save this for the FSFmacs-compatible part of the summary */
|
|
4532 if (i == *lrecord_vector[0].lrecord_type_index)
|
|
4533 gc_count_vector_total_size =
|
|
4534 lcrecord_stats[i].bytes_in_use + lcrecord_stats[i].bytes_freed;
|
|
4535 #endif
|
0
|
4536 sprintf (buf, "%s-storage", name);
|
|
4537 pl = gc_plist_hack (buf, lcrecord_stats[i].bytes_in_use, pl);
|
|
4538 /* Okay, simple pluralization check for `symbol-value-varalias' */
|
|
4539 if (name[len-1] == 's')
|
|
4540 sprintf (buf, "%ses-freed", name);
|
|
4541 else
|
|
4542 sprintf (buf, "%ss-freed", name);
|
|
4543 if (lcrecord_stats[i].instances_freed != 0)
|
|
4544 pl = gc_plist_hack (buf, lcrecord_stats[i].instances_freed, pl);
|
|
4545 if (name[len-1] == 's')
|
|
4546 sprintf (buf, "%ses-on-free-list", name);
|
|
4547 else
|
|
4548 sprintf (buf, "%ss-on-free-list", name);
|
|
4549 if (lcrecord_stats[i].instances_on_free_list != 0)
|
|
4550 pl = gc_plist_hack (buf, lcrecord_stats[i].instances_on_free_list,
|
|
4551 pl);
|
|
4552 if (name[len-1] == 's')
|
|
4553 sprintf (buf, "%ses-used", name);
|
|
4554 else
|
|
4555 sprintf (buf, "%ss-used", name);
|
|
4556 pl = gc_plist_hack (buf, lcrecord_stats[i].instances_in_use, pl);
|
|
4557 }
|
|
4558 }
|
|
4559
|
|
4560 HACK_O_MATIC (extent, "extent-storage", pl);
|
|
4561 pl = gc_plist_hack ("extents-free", gc_count_num_extent_freelist, pl);
|
|
4562 pl = gc_plist_hack ("extents-used", gc_count_num_extent_in_use, pl);
|
|
4563 HACK_O_MATIC (event, "event-storage", pl);
|
|
4564 pl = gc_plist_hack ("events-free", gc_count_num_event_freelist, pl);
|
|
4565 pl = gc_plist_hack ("events-used", gc_count_num_event_in_use, pl);
|
|
4566 HACK_O_MATIC (marker, "marker-storage", pl);
|
|
4567 pl = gc_plist_hack ("markers-free", gc_count_num_marker_freelist, pl);
|
|
4568 pl = gc_plist_hack ("markers-used", gc_count_num_marker_in_use, pl);
|
|
4569 #ifdef LISP_FLOAT_TYPE
|
|
4570 HACK_O_MATIC (float, "float-storage", pl);
|
|
4571 pl = gc_plist_hack ("floats-free", gc_count_num_float_freelist, pl);
|
|
4572 pl = gc_plist_hack ("floats-used", gc_count_num_float_in_use, pl);
|
|
4573 #endif /* LISP_FLOAT_TYPE */
|
|
4574 HACK_O_MATIC (string, "string-header-storage", pl);
|
183
|
4575 pl = gc_plist_hack ("long-strings-total-length",
|
0
|
4576 gc_count_string_total_size
|
|
4577 - gc_count_short_string_total_size, pl);
|
|
4578 HACK_O_MATIC (string_chars, "short-string-storage", pl);
|
|
4579 pl = gc_plist_hack ("short-strings-total-length",
|
|
4580 gc_count_short_string_total_size, pl);
|
|
4581 pl = gc_plist_hack ("strings-free", gc_count_num_string_freelist, pl);
|
183
|
4582 pl = gc_plist_hack ("long-strings-used",
|
0
|
4583 gc_count_num_string_in_use
|
|
4584 - gc_count_num_short_string_in_use, pl);
|
183
|
4585 pl = gc_plist_hack ("short-strings-used",
|
0
|
4586 gc_count_num_short_string_in_use, pl);
|
|
4587
|
|
4588 HACK_O_MATIC (compiled_function, "compiled-function-storage", pl);
|
|
4589 pl = gc_plist_hack ("compiled-functions-free",
|
|
4590 gc_count_num_compiled_function_freelist, pl);
|
|
4591 pl = gc_plist_hack ("compiled-functions-used",
|
|
4592 gc_count_num_compiled_function_in_use, pl);
|
|
4593
|
207
|
4594 #ifndef LRECORD_VECTOR
|
0
|
4595 pl = gc_plist_hack ("vector-storage", gc_count_vector_storage, pl);
|
183
|
4596 pl = gc_plist_hack ("vectors-total-length",
|
0
|
4597 gc_count_vector_total_size, pl);
|
|
4598 pl = gc_plist_hack ("vectors-used", gc_count_num_vector_used, pl);
|
207
|
4599 #endif
|
0
|
4600
|
|
4601 pl = gc_plist_hack ("bit-vector-storage", gc_count_bit_vector_storage, pl);
|
183
|
4602 pl = gc_plist_hack ("bit-vectors-total-length",
|
0
|
4603 gc_count_bit_vector_total_size, pl);
|
|
4604 pl = gc_plist_hack ("bit-vectors-used", gc_count_num_bit_vector_used, pl);
|
|
4605
|
|
4606 HACK_O_MATIC (symbol, "symbol-storage", pl);
|
|
4607 pl = gc_plist_hack ("symbols-free", gc_count_num_symbol_freelist, pl);
|
|
4608 pl = gc_plist_hack ("symbols-used", gc_count_num_symbol_in_use, pl);
|
|
4609
|
|
4610 HACK_O_MATIC (cons, "cons-storage", pl);
|
|
4611 pl = gc_plist_hack ("conses-free", gc_count_num_cons_freelist, pl);
|
|
4612 pl = gc_plist_hack ("conses-used", gc_count_num_cons_in_use, pl);
|
|
4613
|
|
4614 /* The things we do for backwards-compatibility */
|
|
4615 ret[0] = Fcons (make_int (gc_count_num_cons_in_use),
|
|
4616 make_int (gc_count_num_cons_freelist));
|
|
4617 ret[1] = Fcons (make_int (gc_count_num_symbol_in_use),
|
|
4618 make_int (gc_count_num_symbol_freelist));
|
|
4619 ret[2] = Fcons (make_int (gc_count_num_marker_in_use),
|
|
4620 make_int (gc_count_num_marker_freelist));
|
|
4621 ret[3] = make_int (gc_count_string_total_size);
|
|
4622 ret[4] = make_int (gc_count_vector_total_size);
|
|
4623 ret[5] = pl;
|
173
|
4624 return Flist (6, ret);
|
0
|
4625 }
|
|
4626 #undef HACK_O_MATIC
|
|
4627
|
20
|
4628 DEFUN ("consing-since-gc", Fconsing_since_gc, 0, 0, "", /*
|
0
|
4629 Return the number of bytes consed since the last garbage collection.
|
|
4630 \"Consed\" is a misnomer in that this actually counts allocation
|
|
4631 of all different kinds of objects, not just conses.
|
|
4632
|
|
4633 If this value exceeds `gc-cons-threshold', a garbage collection happens.
|
20
|
4634 */
|
|
4635 ())
|
0
|
4636 {
|
173
|
4637 return make_int (consing_since_gc);
|
0
|
4638 }
|
183
|
4639
|
20
|
4640 DEFUN ("memory-limit", Fmemory_limit, 0, 0, "", /*
|
0
|
4641 Return the address of the last byte Emacs has allocated, divided by 1024.
|
|
4642 This may be helpful in debugging Emacs's memory usage.
|
|
4643 The value is divided by 1024 to make sure it will fit in a lisp integer.
|
20
|
4644 */
|
|
4645 ())
|
0
|
4646 {
|
173
|
4647 return make_int ((EMACS_INT) sbrk (0) / 1024);
|
0
|
4648 }
|
|
4649
|
|
4650
|
|
4651
|
|
4652 int
|
|
4653 object_dead_p (Lisp_Object obj)
|
|
4654 {
|
173
|
4655 return ((BUFFERP (obj) && !BUFFER_LIVE_P (XBUFFER (obj))) ||
|
|
4656 (FRAMEP (obj) && !FRAME_LIVE_P (XFRAME (obj))) ||
|
|
4657 (WINDOWP (obj) && !WINDOW_LIVE_P (XWINDOW (obj))) ||
|
|
4658 (DEVICEP (obj) && !DEVICE_LIVE_P (XDEVICE (obj))) ||
|
0
|
4659 (CONSOLEP (obj) && !CONSOLE_LIVE_P (XCONSOLE (obj))) ||
|
173
|
4660 (EVENTP (obj) && !EVENT_LIVE_P (XEVENT (obj))) ||
|
|
4661 (EXTENTP (obj) && !EXTENT_LIVE_P (XEXTENT (obj))));
|
183
|
4662
|
0
|
4663 }
|
|
4664
|
|
4665 #ifdef MEMORY_USAGE_STATS
|
|
4666
|
|
4667 /* Attempt to determine the actual amount of space that is used for
|
|
4668 the block allocated starting at PTR, supposedly of size "CLAIMED_SIZE".
|
|
4669
|
|
4670 It seems that the following holds:
|
|
4671
|
|
4672 1. When using the old allocator (malloc.c):
|
|
4673
|
|
4674 -- blocks are always allocated in chunks of powers of two. For
|
|
4675 each block, there is an overhead of 8 bytes if rcheck is not
|
|
4676 defined, 20 bytes if it is defined. In other words, a
|
|
4677 one-byte allocation needs 8 bytes of overhead for a total of
|
|
4678 9 bytes, and needs to have 16 bytes of memory chunked out for
|
|
4679 it.
|
|
4680
|
|
4681 2. When using the new allocator (gmalloc.c):
|
|
4682
|
|
4683 -- blocks are always allocated in chunks of powers of two up
|
|
4684 to 4096 bytes. Larger blocks are allocated in chunks of
|
|
4685 an integral multiple of 4096 bytes. The minimum block
|
|
4686 size is 2*sizeof (void *), or 16 bytes if SUNOS_LOCALTIME_BUG
|
|
4687 is defined. There is no per-block overhead, but there
|
|
4688 is an overhead of 3*sizeof (size_t) for each 4096 bytes
|
|
4689 allocated.
|
|
4690
|
|
4691 3. When using the system malloc, anything goes, but they are
|
|
4692 generally slower and more space-efficient than the GNU
|
|
4693 allocators. One possibly reasonable assumption to make
|
|
4694 for want of better data is that sizeof (void *), or maybe
|
|
4695 2 * sizeof (void *), is required as overhead and that
|
|
4696 blocks are allocated in the minimum required size except
|
|
4697 that some minimum block size is imposed (e.g. 16 bytes). */
|
|
4698
|
|
4699 int
|
|
4700 malloced_storage_size (void *ptr, int claimed_size,
|
|
4701 struct overhead_stats *stats)
|
|
4702 {
|
|
4703 int orig_claimed_size = claimed_size;
|
|
4704
|
|
4705 #ifdef GNU_MALLOC
|
|
4706
|
|
4707 if (claimed_size < 2 * sizeof (void *))
|
|
4708 claimed_size = 2 * sizeof (void *);
|
|
4709 # ifdef SUNOS_LOCALTIME_BUG
|
|
4710 if (claimed_size < 16)
|
|
4711 claimed_size = 16;
|
|
4712 # endif
|
|
4713 if (claimed_size < 4096)
|
|
4714 {
|
|
4715 int log = 1;
|
|
4716
|
|
4717 /* compute the log base two, more or less, then use it to compute
|
|
4718 the block size needed. */
|
|
4719 claimed_size--;
|
|
4720 /* It's big, it's heavy, it's wood! */
|
|
4721 while ((claimed_size /= 2) != 0)
|
|
4722 ++log;
|
|
4723 claimed_size = 1;
|
|
4724 /* It's better than bad, it's good! */
|
|
4725 while (log > 0)
|
|
4726 {
|
|
4727 claimed_size *= 2;
|
|
4728 log--;
|
|
4729 }
|
|
4730 /* We have to come up with some average about the amount of
|
|
4731 blocks used. */
|
|
4732 if ((rand () & 4095) < claimed_size)
|
|
4733 claimed_size += 3 * sizeof (void *);
|
|
4734 }
|
|
4735 else
|
|
4736 {
|
|
4737 claimed_size += 4095;
|
|
4738 claimed_size &= ~4095;
|
|
4739 claimed_size += (claimed_size / 4096) * 3 * sizeof (size_t);
|
|
4740 }
|
|
4741
|
|
4742 #elif defined (SYSTEM_MALLOC)
|
|
4743
|
|
4744 if (claimed_size < 16)
|
|
4745 claimed_size = 16;
|
|
4746 claimed_size += 2 * sizeof (void *);
|
|
4747
|
|
4748 #else /* old GNU allocator */
|
|
4749
|
|
4750 # ifdef rcheck /* #### may not be defined here */
|
|
4751 claimed_size += 20;
|
|
4752 # else
|
|
4753 claimed_size += 8;
|
|
4754 # endif
|
|
4755 {
|
|
4756 int log = 1;
|
|
4757
|
|
4758 /* compute the log base two, more or less, then use it to compute
|
|
4759 the block size needed. */
|
|
4760 claimed_size--;
|
|
4761 /* It's big, it's heavy, it's wood! */
|
|
4762 while ((claimed_size /= 2) != 0)
|
|
4763 ++log;
|
|
4764 claimed_size = 1;
|
|
4765 /* It's better than bad, it's good! */
|
|
4766 while (log > 0)
|
|
4767 {
|
|
4768 claimed_size *= 2;
|
|
4769 log--;
|
|
4770 }
|
|
4771 }
|
|
4772
|
|
4773 #endif /* old GNU allocator */
|
|
4774
|
|
4775 if (stats)
|
|
4776 {
|
|
4777 stats->was_requested += orig_claimed_size;
|
|
4778 stats->malloc_overhead += claimed_size - orig_claimed_size;
|
|
4779 }
|
|
4780 return claimed_size;
|
|
4781 }
|
|
4782
|
|
4783 int
|
|
4784 fixed_type_block_overhead (int size)
|
|
4785 {
|
|
4786 int per_block = TYPE_ALLOC_SIZE (cons, unsigned char);
|
|
4787 int overhead = 0;
|
|
4788 int storage_size = malloced_storage_size (0, per_block, 0);
|
|
4789 while (size >= per_block)
|
|
4790 {
|
|
4791 size -= per_block;
|
|
4792 overhead += sizeof (void *) + per_block - storage_size;
|
|
4793
|
|
4794 }
|
|
4795 if (rand () % per_block < size)
|
|
4796 overhead += sizeof (void *) + per_block - storage_size;
|
|
4797 return overhead;
|
|
4798 }
|
|
4799
|
|
4800 #endif /* MEMORY_USAGE_STATS */
|
|
4801
|
|
4802
|
|
4803 /* Initialization */
|
|
4804 void
|
|
4805 init_alloc_once_early (void)
|
|
4806 {
|
|
4807 int iii;
|
|
4808
|
|
4809 #ifdef PURESTAT
|
|
4810 for (iii = 0; iii < countof (purestats); iii++)
|
|
4811 {
|
|
4812 if (! purestats[iii]) continue;
|
|
4813 purestats[iii]->nobjects = 0;
|
|
4814 purestats[iii]->nbytes = 0;
|
|
4815 }
|
|
4816 purecopying_for_bytecode = 0;
|
183
|
4817 #endif /* PURESTAT */
|
|
4818
|
0
|
4819 last_lrecord_type_index_assigned = -1;
|
|
4820 for (iii = 0; iii < countof (lrecord_implementations_table); iii++)
|
|
4821 {
|
|
4822 lrecord_implementations_table[iii] = 0;
|
|
4823 }
|
183
|
4824
|
211
|
4825 #ifdef USE_INDEXED_LRECORD_IMPLEMENTATION
|
|
4826 /*
|
|
4827 * If USE_INDEXED_LRECORD_IMPLEMENTATION is defined, all the staticly
|
|
4828 * defined subr lrecords were initialized with lheader->type == 0.
|
|
4829 * See subr_lheader_initializer in lisp.h. Force type index 0 to be
|
|
4830 * assigned to lrecord_subr so that those predefined indexes match
|
|
4831 * reality.
|
|
4832 */
|
|
4833 (void) lrecord_type_index (lrecord_subr);
|
|
4834 assert (*(lrecord_subr[0].lrecord_type_index) == 0);
|
|
4835 /*
|
|
4836 * The same is true for symbol_value_forward objects, except the
|
|
4837 * type is 1.
|
|
4838 */
|
|
4839 (void) lrecord_type_index (lrecord_symbol_value_forward);
|
|
4840 assert (*(lrecord_symbol_value_forward[0].lrecord_type_index) == 1);
|
|
4841 #endif
|
|
4842
|
0
|
4843 symbols_initialized = 0;
|
183
|
4844
|
0
|
4845 gc_generation_number[0] = 0;
|
|
4846 /* purify_flag 1 is correct even if CANNOT_DUMP.
|
|
4847 * loadup.el will set to nil at end. */
|
|
4848 purify_flag = 1;
|
|
4849 pureptr = 0;
|
|
4850 pure_lossage = 0;
|
|
4851 breathing_space = 0;
|
207
|
4852 #ifndef LRECORD_VECTOR
|
0
|
4853 XSETINT (all_vectors, 0); /* Qzero may not be set yet. */
|
207
|
4854 #endif
|
0
|
4855 XSETINT (all_bit_vectors, 0); /* Qzero may not be set yet. */
|
|
4856 XSETINT (Vgc_message, 0);
|
|
4857 all_lcrecords = 0;
|
|
4858 ignore_malloc_warnings = 1;
|
|
4859 init_string_alloc ();
|
|
4860 init_string_chars_alloc ();
|
|
4861 init_cons_alloc ();
|
|
4862 init_symbol_alloc ();
|
|
4863 init_compiled_function_alloc ();
|
|
4864 #ifdef LISP_FLOAT_TYPE
|
|
4865 init_float_alloc ();
|
|
4866 #endif /* LISP_FLOAT_TYPE */
|
|
4867 #ifndef standalone
|
|
4868 init_marker_alloc ();
|
|
4869 init_extent_alloc ();
|
|
4870 init_event_alloc ();
|
|
4871 #endif
|
|
4872 ignore_malloc_warnings = 0;
|
|
4873 staticidx = 0;
|
|
4874 consing_since_gc = 0;
|
|
4875 #if 1
|
|
4876 gc_cons_threshold = 500000; /* XEmacs change */
|
|
4877 #else
|
|
4878 gc_cons_threshold = 15000; /* debugging */
|
|
4879 #endif
|
|
4880 #ifdef VIRT_ADDR_VARIES
|
|
4881 malloc_sbrk_unused = 1<<22; /* A large number */
|
|
4882 malloc_sbrk_used = 100000; /* as reasonable as any number */
|
|
4883 #endif /* VIRT_ADDR_VARIES */
|
|
4884 lrecord_uid_counter = 259;
|
|
4885 debug_string_purity = 0;
|
|
4886 gcprolist = 0;
|
|
4887
|
|
4888 gc_currently_forbidden = 0;
|
|
4889 gc_hooks_inhibited = 0;
|
|
4890
|
|
4891 #ifdef ERROR_CHECK_TYPECHECK
|
|
4892 ERROR_ME.really_unlikely_name_to_have_accidentally_in_a_non_errb_structure =
|
|
4893 666;
|
|
4894 ERROR_ME_NOT.
|
|
4895 really_unlikely_name_to_have_accidentally_in_a_non_errb_structure = 42;
|
|
4896 ERROR_ME_WARN.
|
|
4897 really_unlikely_name_to_have_accidentally_in_a_non_errb_structure =
|
|
4898 3333632;
|
183
|
4899 #endif /* ERROR_CHECK_TYPECHECK */
|
0
|
4900 }
|
|
4901
|
|
4902 void
|
|
4903 reinit_alloc (void)
|
|
4904 {
|
|
4905 gcprolist = 0;
|
|
4906 }
|
|
4907
|
|
4908 void
|
|
4909 syms_of_alloc (void)
|
|
4910 {
|
|
4911 defsymbol (&Qpre_gc_hook, "pre-gc-hook");
|
|
4912 defsymbol (&Qpost_gc_hook, "post-gc-hook");
|
|
4913 defsymbol (&Qgarbage_collecting, "garbage-collecting");
|
|
4914
|
20
|
4915 DEFSUBR (Fcons);
|
|
4916 DEFSUBR (Flist);
|
|
4917 DEFSUBR (Fvector);
|
|
4918 DEFSUBR (Fbit_vector);
|
|
4919 DEFSUBR (Fmake_byte_code);
|
|
4920 DEFSUBR (Fmake_list);
|
|
4921 DEFSUBR (Fmake_vector);
|
|
4922 DEFSUBR (Fmake_bit_vector);
|
|
4923 DEFSUBR (Fmake_string);
|
|
4924 DEFSUBR (Fmake_symbol);
|
|
4925 DEFSUBR (Fmake_marker);
|
|
4926 DEFSUBR (Fpurecopy);
|
|
4927 DEFSUBR (Fgarbage_collect);
|
|
4928 DEFSUBR (Fmemory_limit);
|
|
4929 DEFSUBR (Fconsing_since_gc);
|
0
|
4930 }
|
|
4931
|
|
4932 void
|
|
4933 vars_of_alloc (void)
|
|
4934 {
|
|
4935 DEFVAR_INT ("gc-cons-threshold", &gc_cons_threshold /*
|
|
4936 *Number of bytes of consing between garbage collections.
|
|
4937 \"Consing\" is a misnomer in that this actually counts allocation
|
|
4938 of all different kinds of objects, not just conses.
|
|
4939 Garbage collection can happen automatically once this many bytes have been
|
|
4940 allocated since the last garbage collection. All data types count.
|
|
4941
|
|
4942 Garbage collection happens automatically when `eval' or `funcall' are
|
|
4943 called. (Note that `funcall' is called implicitly as part of evaluation.)
|
|
4944 By binding this temporarily to a large number, you can effectively
|
|
4945 prevent garbage collection during a part of the program.
|
|
4946
|
|
4947 See also `consing-since-gc'.
|
|
4948 */ );
|
|
4949
|
|
4950 DEFVAR_INT ("pure-bytes-used", &pureptr /*
|
|
4951 Number of bytes of sharable Lisp data allocated so far.
|
|
4952 */ );
|
|
4953
|
|
4954 #if 0
|
|
4955 DEFVAR_INT ("data-bytes-used", &malloc_sbrk_used /*
|
|
4956 Number of bytes of unshared memory allocated in this session.
|
|
4957 */ );
|
|
4958
|
|
4959 DEFVAR_INT ("data-bytes-free", &malloc_sbrk_unused /*
|
|
4960 Number of bytes of unshared memory remaining available in this session.
|
|
4961 */ );
|
|
4962 #endif
|
|
4963
|
|
4964 #ifdef DEBUG_XEMACS
|
|
4965 DEFVAR_INT ("debug-allocation", &debug_allocation /*
|
|
4966 If non-zero, print out information to stderr about all objects allocated.
|
|
4967 See also `debug-allocation-backtrace-length'.
|
|
4968 */ );
|
|
4969 debug_allocation = 0;
|
|
4970
|
|
4971 DEFVAR_INT ("debug-allocation-backtrace-length",
|
|
4972 &debug_allocation_backtrace_length /*
|
|
4973 Length (in stack frames) of short backtrace printed out by `debug-allocation'.
|
|
4974 */ );
|
|
4975 debug_allocation_backtrace_length = 2;
|
|
4976 #endif
|
|
4977
|
|
4978 DEFVAR_BOOL ("purify-flag", &purify_flag /*
|
|
4979 Non-nil means loading Lisp code in order to dump an executable.
|
|
4980 This means that certain objects should be allocated in shared (pure) space.
|
|
4981 */ );
|
|
4982
|
|
4983 DEFVAR_LISP ("pre-gc-hook", &Vpre_gc_hook /*
|
|
4984 Function or functions to be run just before each garbage collection.
|
|
4985 Interrupts, garbage collection, and errors are inhibited while this hook
|
|
4986 runs, so be extremely careful in what you add here. In particular, avoid
|
|
4987 consing, and do not interact with the user.
|
|
4988 */ );
|
|
4989 Vpre_gc_hook = Qnil;
|
|
4990
|
|
4991 DEFVAR_LISP ("post-gc-hook", &Vpost_gc_hook /*
|
|
4992 Function or functions to be run just after each garbage collection.
|
|
4993 Interrupts, garbage collection, and errors are inhibited while this hook
|
|
4994 runs, so be extremely careful in what you add here. In particular, avoid
|
|
4995 consing, and do not interact with the user.
|
|
4996 */ );
|
|
4997 Vpost_gc_hook = Qnil;
|
|
4998
|
|
4999 DEFVAR_LISP ("gc-message", &Vgc_message /*
|
|
5000 String to print to indicate that a garbage collection is in progress.
|
|
5001 This is printed in the echo area. If the selected frame is on a
|
|
5002 window system and `gc-pointer-glyph' specifies a value (i.e. a pointer
|
|
5003 image instance) in the domain of the selected frame, the mouse pointer
|
|
5004 will change instead of this message being printed.
|
|
5005 */ );
|
|
5006 Vgc_message = make_pure_string ((CONST Bufbyte *) gc_default_message,
|
|
5007 countof (gc_default_message) - 1,
|
|
5008 Qnil, 1);
|
|
5009
|
|
5010 DEFVAR_LISP ("gc-pointer-glyph", &Vgc_pointer_glyph /*
|
|
5011 Pointer glyph used to indicate that a garbage collection is in progress.
|
|
5012 If the selected window is on a window system and this glyph specifies a
|
|
5013 value (i.e. a pointer image instance) in the domain of the selected
|
|
5014 window, the pointer will be changed as specified during garbage collection.
|
|
5015 Otherwise, a message will be printed in the echo area, as controlled
|
|
5016 by `gc-message'.
|
|
5017 */ );
|
|
5018 }
|
|
5019
|
|
5020 void
|
|
5021 complex_vars_of_alloc (void)
|
|
5022 {
|
|
5023 Vgc_pointer_glyph = Fmake_glyph_internal (Qpointer);
|
|
5024 }
|