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