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