300
|
1 /* Storage allocation and gc for GNU Emacs Lisp interpreter.
|
|
2 Copyright (C) 1985, 1986, 1988 Free Software Foundation, Inc.
|
|
3
|
|
4 This file is part of GNU Emacs.
|
|
5
|
|
6 GNU Emacs is free software; you can redistribute it and/or modify
|
|
7 it under the terms of the GNU General Public License as published by
|
|
8 the Free Software Foundation; either version 1, or (at your option)
|
|
9 any later version.
|
|
10
|
|
11 GNU Emacs is distributed in the hope that it will be useful,
|
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 GNU General Public License for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with GNU Emacs; see the file COPYING. If not, write to
|
|
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
|
|
19
|
|
20
|
|
21 #include "config.h"
|
|
22 #include "lisp.h"
|
356
|
23 #include "puresize.h"
|
300
|
24 #ifndef standalone
|
|
25 #include "buffer.h"
|
|
26 #include "window.h"
|
|
27 #ifdef HAVE_X_WINDOWS
|
|
28 #include "xterm.h"
|
|
29 #ifdef MULTI_SCREEN
|
|
30 #include "screen.h"
|
|
31 #endif /* MULTI_SCREEN */
|
|
32 #endif /* HAVE_X_WINDOWS */
|
|
33 #endif
|
|
34
|
|
35 #define max(A,B) ((A) > (B) ? (A) : (B))
|
|
36
|
|
37 /* Macro to verify that storage intended for Lisp objects is not
|
|
38 out of range to fit in the space for a pointer.
|
|
39 ADDRESS is the start of the block, and SIZE
|
|
40 is the amount of space within which objects can start. */
|
|
41 #define VALIDATE_LISP_STORAGE(address, size) \
|
|
42 do \
|
|
43 { \
|
|
44 Lisp_Object val; \
|
|
45 XSET (val, Lisp_Cons, (char *) address + size); \
|
|
46 if ((char *) XCONS (val) != (char *) address + size) \
|
|
47 { \
|
|
48 free (address); \
|
|
49 memory_full (); \
|
|
50 } \
|
|
51 } while (0)
|
|
52
|
|
53 /* Number of bytes of consing done since the last gc */
|
|
54 int consing_since_gc;
|
|
55
|
|
56 /* Number of bytes of consing since gc before another gc should be done. */
|
|
57 int gc_cons_threshold;
|
|
58
|
|
59 /* Nonzero during gc */
|
|
60 int gc_in_progress;
|
|
61
|
|
62 #ifndef VIRT_ADDR_VARIES
|
|
63 extern
|
|
64 #endif /* VIRT_ADDR_VARIES */
|
|
65 int malloc_sbrk_used;
|
|
66
|
|
67 #ifndef VIRT_ADDR_VARIES
|
|
68 extern
|
|
69 #endif /* VIRT_ADDR_VARIES */
|
|
70 int malloc_sbrk_unused;
|
|
71
|
|
72 /* Two thresholds controlling how much undo information to keep. */
|
|
73 int undo_threshold;
|
|
74 int undo_high_threshold;
|
|
75
|
|
76 /* Non-nil means defun should do purecopy on the function definition */
|
|
77 Lisp_Object Vpurify_flag;
|
|
78
|
|
79 #ifndef HAVE_SHM
|
|
80 int pure[PURESIZE / sizeof (int)] = {0,}; /* Force it into data space! */
|
|
81 #define PUREBEG (char *) pure
|
|
82 #else
|
|
83 #define pure PURE_SEG_BITS /* Use shared memory segment */
|
|
84 #define PUREBEG (char *)PURE_SEG_BITS
|
356
|
85
|
|
86 /* This variable is used only by the XPNTR macro when HAVE_SHM is
|
|
87 defined. If we used the PURESIZE macro directly there, that would
|
|
88 make most of emacs dependent on puresize.h, which we don't want -
|
|
89 you should be able to change that without too much recompilation.
|
|
90 So map_in_data initializes pure_size, and the dependencies work
|
|
91 out. */
|
|
92 int pure_size;
|
300
|
93 #endif /* not HAVE_SHM */
|
|
94
|
|
95 /* Index in pure at which next pure object will be allocated. */
|
|
96 int pureptr;
|
|
97
|
|
98 /* If nonzero, this is a warning delivered by malloc and not yet displayed. */
|
|
99 char *pending_malloc_warning;
|
|
100
|
|
101 /* Maximum amount of C stack to save when a GC happens. */
|
|
102
|
|
103 #ifndef MAX_SAVE_STACK
|
|
104 #define MAX_SAVE_STACK 16000
|
|
105 #endif
|
|
106
|
|
107 /* Buffer in which we save a copy of the C stack at each GC. */
|
|
108
|
|
109 char *stack_copy;
|
|
110 int stack_copy_size;
|
|
111
|
|
112 /* Non-zero means ignore malloc warnings. Set during initialization. */
|
|
113 int ignore_warnings;
|
|
114
|
|
115 Lisp_Object
|
|
116 malloc_warning_1 (str)
|
|
117 Lisp_Object str;
|
|
118 {
|
|
119 Fprinc (str, Vstandard_output);
|
|
120 write_string ("\nKilling some buffers may delay running out of memory.\n", -1);
|
|
121 write_string ("However, certainly by the time you receive the 95% warning,\n", -1);
|
|
122 write_string ("you should clean up, kill this Emacs, and start a new one.", -1);
|
|
123 return Qnil;
|
|
124 }
|
|
125
|
|
126 /* malloc calls this if it finds we are near exhausting storage */
|
|
127 malloc_warning (str)
|
|
128 char *str;
|
|
129 {
|
|
130 pending_malloc_warning = str;
|
|
131 }
|
|
132
|
|
133 display_malloc_warning ()
|
|
134 {
|
|
135 register Lisp_Object val;
|
|
136
|
|
137 val = build_string (pending_malloc_warning);
|
|
138 pending_malloc_warning = 0;
|
|
139 internal_with_output_to_temp_buffer (" *Danger*", malloc_warning_1, val);
|
|
140 }
|
|
141
|
|
142 /* Called if malloc returns zero */
|
|
143 memory_full ()
|
|
144 {
|
|
145 error ("Memory exhausted");
|
|
146 }
|
|
147
|
|
148 /* like malloc and realloc but check for no memory left */
|
|
149
|
|
150 long *
|
|
151 xmalloc (size)
|
|
152 int size;
|
|
153 {
|
|
154 register long *val;
|
|
155
|
|
156 val = (long *) malloc (size);
|
|
157
|
|
158 if (!val && size) memory_full ();
|
|
159 return val;
|
|
160 }
|
|
161
|
|
162 long *
|
|
163 xrealloc (block, size)
|
|
164 long *block;
|
|
165 int size;
|
|
166 {
|
|
167 register long *val;
|
|
168
|
|
169 val = (long *) realloc (block, size);
|
|
170
|
|
171 if (!val && size) memory_full ();
|
|
172 return val;
|
|
173 }
|
|
174
|
|
175 #ifdef LISP_FLOAT_TYPE
|
|
176 /* Allocation of float cells, just like conses */
|
|
177 /* We store float cells inside of float_blocks, allocating a new
|
|
178 float_block with malloc whenever necessary. Float cells reclaimed by
|
|
179 GC are put on a free list to be reallocated before allocating
|
|
180 any new float cells from the latest float_block.
|
|
181
|
|
182 Each float_block is just under 1020 bytes long,
|
|
183 since malloc really allocates in units of powers of two
|
|
184 and uses 4 bytes for its own overhead. */
|
|
185
|
|
186 #define FLOAT_BLOCK_SIZE \
|
|
187 ((1020 - sizeof (struct float_block *)) / sizeof (struct Lisp_Float))
|
|
188
|
|
189 struct float_block
|
|
190 {
|
|
191 struct float_block *next;
|
|
192 struct Lisp_Float floats[FLOAT_BLOCK_SIZE];
|
|
193 };
|
|
194
|
|
195 struct float_block *float_block;
|
|
196 int float_block_index;
|
|
197
|
|
198 struct Lisp_Float *float_free_list;
|
|
199
|
|
200 void
|
|
201 init_float ()
|
|
202 {
|
|
203 float_block = (struct float_block *) malloc (sizeof (struct float_block));
|
|
204 float_block->next = 0;
|
|
205 bzero (float_block->floats, sizeof float_block->floats);
|
|
206 float_block_index = 0;
|
|
207 float_free_list = 0;
|
|
208 }
|
|
209
|
|
210 /* Explicitly free a float cell. */
|
|
211 free_float (ptr)
|
|
212 struct Lisp_Float *ptr;
|
|
213 {
|
|
214 XFASTINT (ptr->type) = (int) float_free_list;
|
|
215 float_free_list = ptr;
|
|
216 }
|
|
217
|
|
218 Lisp_Object
|
|
219 make_float (float_value)
|
|
220 double float_value;
|
|
221 {
|
|
222 register Lisp_Object val;
|
|
223
|
|
224 if (float_free_list)
|
|
225 {
|
|
226 XSET (val, Lisp_Float, float_free_list);
|
|
227 float_free_list = (struct Lisp_Float *) XFASTINT (float_free_list->type);
|
|
228 }
|
|
229 else
|
|
230 {
|
|
231 if (float_block_index == FLOAT_BLOCK_SIZE)
|
|
232 {
|
|
233 register struct float_block *new = (struct float_block *) malloc (sizeof (struct float_block));
|
|
234 if (!new) memory_full ();
|
|
235 VALIDATE_LISP_STORAGE (new, sizeof *new);
|
|
236 new->next = float_block;
|
|
237 float_block = new;
|
|
238 float_block_index = 0;
|
|
239 }
|
|
240 XSET (val, Lisp_Float, &float_block->floats[float_block_index++]);
|
|
241 }
|
|
242 XFLOAT (val)->data = float_value;
|
|
243 XFLOAT (val)->type = 0; /* bug chasing -wsr */
|
|
244 consing_since_gc += sizeof (struct Lisp_Float);
|
|
245 return val;
|
|
246 }
|
|
247
|
|
248 #endif /* LISP_FLOAT_TYPE */
|
|
249
|
|
250 /* Allocation of cons cells */
|
|
251 /* We store cons cells inside of cons_blocks, allocating a new
|
|
252 cons_block with malloc whenever necessary. Cons cells reclaimed by
|
|
253 GC are put on a free list to be reallocated before allocating
|
|
254 any new cons cells from the latest cons_block.
|
|
255
|
|
256 Each cons_block is just under 1020 bytes long,
|
|
257 since malloc really allocates in units of powers of two
|
|
258 and uses 4 bytes for its own overhead. */
|
|
259
|
|
260 #define CONS_BLOCK_SIZE \
|
|
261 ((1020 - sizeof (struct cons_block *)) / sizeof (struct Lisp_Cons))
|
|
262
|
|
263 struct cons_block
|
|
264 {
|
|
265 struct cons_block *next;
|
|
266 struct Lisp_Cons conses[CONS_BLOCK_SIZE];
|
|
267 };
|
|
268
|
|
269 struct cons_block *cons_block;
|
|
270 int cons_block_index;
|
|
271
|
|
272 struct Lisp_Cons *cons_free_list;
|
|
273
|
|
274 void
|
|
275 init_cons ()
|
|
276 {
|
|
277 cons_block = (struct cons_block *) malloc (sizeof (struct cons_block));
|
|
278 cons_block->next = 0;
|
|
279 bzero (cons_block->conses, sizeof cons_block->conses);
|
|
280 cons_block_index = 0;
|
|
281 cons_free_list = 0;
|
|
282 }
|
|
283
|
|
284 /* Explicitly free a cons cell. */
|
|
285 free_cons (ptr)
|
|
286 struct Lisp_Cons *ptr;
|
|
287 {
|
|
288 XFASTINT (ptr->car) = (int) cons_free_list;
|
|
289 cons_free_list = ptr;
|
|
290 }
|
|
291
|
|
292 DEFUN ("cons", Fcons, Scons, 2, 2, 0,
|
|
293 "Create a new cons, give it CAR and CDR as components, and return it.")
|
|
294 (car, cdr)
|
|
295 Lisp_Object car, cdr;
|
|
296 {
|
|
297 register Lisp_Object val;
|
|
298
|
|
299 if (cons_free_list)
|
|
300 {
|
|
301 XSET (val, Lisp_Cons, cons_free_list);
|
|
302 cons_free_list = (struct Lisp_Cons *) XFASTINT (cons_free_list->car);
|
|
303 }
|
|
304 else
|
|
305 {
|
|
306 if (cons_block_index == CONS_BLOCK_SIZE)
|
|
307 {
|
|
308 register struct cons_block *new = (struct cons_block *) malloc (sizeof (struct cons_block));
|
|
309 if (!new) memory_full ();
|
|
310 VALIDATE_LISP_STORAGE (new, sizeof *new);
|
|
311 new->next = cons_block;
|
|
312 cons_block = new;
|
|
313 cons_block_index = 0;
|
|
314 }
|
|
315 XSET (val, Lisp_Cons, &cons_block->conses[cons_block_index++]);
|
|
316 }
|
|
317 XCONS (val)->car = car;
|
|
318 XCONS (val)->cdr = cdr;
|
|
319 consing_since_gc += sizeof (struct Lisp_Cons);
|
|
320 return val;
|
|
321 }
|
|
322
|
|
323 DEFUN ("list", Flist, Slist, 0, MANY, 0,
|
|
324 "Return a newly created list with specified arguments as elements.\n\
|
|
325 Any number of arguments, even zero arguments, are allowed.")
|
|
326 (nargs, args)
|
|
327 int nargs;
|
|
328 register Lisp_Object *args;
|
|
329 {
|
|
330 register Lisp_Object len, val, val_tail;
|
|
331
|
|
332 XFASTINT (len) = nargs;
|
|
333 val = Fmake_list (len, Qnil);
|
|
334 val_tail = val;
|
|
335 while (!NULL (val_tail))
|
|
336 {
|
|
337 XCONS (val_tail)->car = *args++;
|
|
338 val_tail = XCONS (val_tail)->cdr;
|
|
339 }
|
|
340 return val;
|
|
341 }
|
|
342
|
|
343 DEFUN ("make-list", Fmake_list, Smake_list, 2, 2, 0,
|
|
344 "Return a newly created list of length LENGTH, with each element being INIT.")
|
|
345 (length, init)
|
|
346 register Lisp_Object length, init;
|
|
347 {
|
|
348 register Lisp_Object val;
|
|
349 register int size;
|
|
350
|
|
351 if (XTYPE (length) != Lisp_Int || XINT (length) < 0)
|
|
352 length = wrong_type_argument (Qnatnump, length);
|
|
353 size = XINT (length);
|
|
354
|
|
355 val = Qnil;
|
|
356 while (size-- > 0)
|
|
357 val = Fcons (init, val);
|
|
358 return val;
|
|
359 }
|
|
360
|
|
361 /* Allocation of vectors */
|
|
362
|
|
363 struct Lisp_Vector *all_vectors;
|
|
364
|
|
365 DEFUN ("make-vector", Fmake_vector, Smake_vector, 2, 2, 0,
|
|
366 "Return a newly created vector of length LENGTH, with each element being INIT.\n\
|
|
367 See also the function `vector'.")
|
|
368 (length, init)
|
|
369 register Lisp_Object length, init;
|
|
370 {
|
|
371 register int sizei, index;
|
|
372 register Lisp_Object vector;
|
|
373 register struct Lisp_Vector *p;
|
|
374
|
|
375 if (XTYPE (length) != Lisp_Int || XINT (length) < 0)
|
|
376 length = wrong_type_argument (Qnatnump, length);
|
|
377 sizei = XINT (length);
|
|
378
|
|
379 p = (struct Lisp_Vector *) malloc (sizeof (struct Lisp_Vector) + (sizei - 1) * sizeof (Lisp_Object));
|
|
380 if (p == 0)
|
|
381 memory_full ();
|
|
382 VALIDATE_LISP_STORAGE (p, 0);
|
|
383
|
|
384 XSET (vector, Lisp_Vector, p);
|
|
385 consing_since_gc += sizeof (struct Lisp_Vector) + (sizei - 1) * sizeof (Lisp_Object);
|
|
386
|
|
387 p->size = sizei;
|
|
388 p->next = all_vectors;
|
|
389 all_vectors = p;
|
|
390
|
|
391 for (index = 0; index < sizei; index++)
|
|
392 p->contents[index] = init;
|
|
393
|
|
394 return vector;
|
|
395 }
|
|
396
|
|
397 DEFUN ("vector", Fvector, Svector, 0, MANY, 0,
|
|
398 "Return a newly created vector with specified arguments as elements.\n\
|
|
399 Any number of arguments, even zero arguments, are allowed.")
|
|
400 (nargs, args)
|
|
401 register int nargs;
|
|
402 Lisp_Object *args;
|
|
403 {
|
|
404 register Lisp_Object len, val;
|
|
405 register int index;
|
|
406 register struct Lisp_Vector *p;
|
|
407
|
|
408 XFASTINT (len) = nargs;
|
|
409 val = Fmake_vector (len, Qnil);
|
|
410 p = XVECTOR (val);
|
|
411 for (index = 0; index < nargs; index++)
|
|
412 p->contents[index] = args[index];
|
|
413 return val;
|
|
414 }
|
|
415
|
|
416 DEFUN ("make-byte-code", Fmake_byte_code, Smake_byte_code, 4, MANY, 0,
|
|
417 "Create a byte-code object with specified arguments as elements.\n\
|
|
418 The arguments should be the arglist, bytecode-string, constant vector,\n\
|
|
419 stack size, (optional) doc string, and (optional) interactive spec.\n\
|
|
420 The first four arguments are required; at most six have any\n\
|
|
421 significance.")
|
|
422 (nargs, args)
|
|
423 register int nargs;
|
|
424 Lisp_Object *args;
|
|
425 {
|
|
426 register Lisp_Object len, val;
|
|
427 register int index;
|
|
428 register struct Lisp_Vector *p;
|
|
429
|
|
430 XFASTINT (len) = nargs;
|
|
431 if (!NULL (Vpurify_flag))
|
|
432 val = make_pure_vector (len);
|
|
433 else
|
|
434 val = Fmake_vector (len, Qnil);
|
|
435 p = XVECTOR (val);
|
|
436 for (index = 0; index < nargs; index++)
|
|
437 {
|
|
438 if (!NULL (Vpurify_flag))
|
|
439 args[index] = Fpurecopy (args[index]);
|
|
440 p->contents[index] = args[index];
|
|
441 }
|
|
442 XSETTYPE (val, Lisp_Compiled);
|
|
443 return val;
|
|
444 }
|
|
445
|
|
446 /* Allocation of symbols.
|
|
447 Just like allocation of conses!
|
|
448
|
|
449 Each symbol_block is just under 1020 bytes long,
|
|
450 since malloc really allocates in units of powers of two
|
|
451 and uses 4 bytes for its own overhead. */
|
|
452
|
|
453 #define SYMBOL_BLOCK_SIZE \
|
|
454 ((1020 - sizeof (struct symbol_block *)) / sizeof (struct Lisp_Symbol))
|
|
455
|
|
456 struct symbol_block
|
|
457 {
|
|
458 struct symbol_block *next;
|
|
459 struct Lisp_Symbol symbols[SYMBOL_BLOCK_SIZE];
|
|
460 };
|
|
461
|
|
462 struct symbol_block *symbol_block;
|
|
463 int symbol_block_index;
|
|
464
|
|
465 struct Lisp_Symbol *symbol_free_list;
|
|
466
|
|
467 void
|
|
468 init_symbol ()
|
|
469 {
|
|
470 symbol_block = (struct symbol_block *) malloc (sizeof (struct symbol_block));
|
|
471 symbol_block->next = 0;
|
|
472 bzero (symbol_block->symbols, sizeof symbol_block->symbols);
|
|
473 symbol_block_index = 0;
|
|
474 symbol_free_list = 0;
|
|
475 }
|
|
476
|
|
477 DEFUN ("make-symbol", Fmake_symbol, Smake_symbol, 1, 1, 0,
|
|
478 "Return a newly allocated uninterned symbol whose name is NAME.\n\
|
|
479 Its value and function definition are void, and its property list is nil.")
|
|
480 (str)
|
|
481 Lisp_Object str;
|
|
482 {
|
|
483 register Lisp_Object val;
|
|
484 register struct Lisp_Symbol *p;
|
|
485
|
|
486 CHECK_STRING (str, 0);
|
|
487
|
|
488 if (symbol_free_list)
|
|
489 {
|
|
490 XSET (val, Lisp_Symbol, symbol_free_list);
|
|
491 symbol_free_list
|
|
492 = (struct Lisp_Symbol *) XFASTINT (symbol_free_list->value);
|
|
493 }
|
|
494 else
|
|
495 {
|
|
496 if (symbol_block_index == SYMBOL_BLOCK_SIZE)
|
|
497 {
|
|
498 struct symbol_block *new = (struct symbol_block *) malloc (sizeof (struct symbol_block));
|
|
499 if (!new) memory_full ();
|
|
500 VALIDATE_LISP_STORAGE (new, sizeof *new);
|
|
501 new->next = symbol_block;
|
|
502 symbol_block = new;
|
|
503 symbol_block_index = 0;
|
|
504 }
|
|
505 XSET (val, Lisp_Symbol, &symbol_block->symbols[symbol_block_index++]);
|
|
506 }
|
|
507 p = XSYMBOL (val);
|
|
508 p->name = XSTRING (str);
|
|
509 p->plist = Qnil;
|
|
510 p->value = Qunbound;
|
|
511 p->function = Qunbound;
|
|
512 p->next = 0;
|
|
513 consing_since_gc += sizeof (struct Lisp_Symbol);
|
|
514 return val;
|
|
515 }
|
|
516
|
|
517 /* Allocation of markers.
|
|
518 Works like allocation of conses. */
|
|
519
|
|
520 #define MARKER_BLOCK_SIZE \
|
|
521 ((1020 - sizeof (struct marker_block *)) / sizeof (struct Lisp_Marker))
|
|
522
|
|
523 struct marker_block
|
|
524 {
|
|
525 struct marker_block *next;
|
|
526 struct Lisp_Marker markers[MARKER_BLOCK_SIZE];
|
|
527 };
|
|
528
|
|
529 struct marker_block *marker_block;
|
|
530 int marker_block_index;
|
|
531
|
|
532 struct Lisp_Marker *marker_free_list;
|
|
533
|
|
534 void
|
|
535 init_marker ()
|
|
536 {
|
|
537 marker_block = (struct marker_block *) malloc (sizeof (struct marker_block));
|
|
538 marker_block->next = 0;
|
|
539 bzero (marker_block->markers, sizeof marker_block->markers);
|
|
540 marker_block_index = 0;
|
|
541 marker_free_list = 0;
|
|
542 }
|
|
543
|
|
544 DEFUN ("make-marker", Fmake_marker, Smake_marker, 0, 0, 0,
|
|
545 "Return a newly allocated marker which does not point at any place.")
|
|
546 ()
|
|
547 {
|
|
548 register Lisp_Object val;
|
|
549 register struct Lisp_Marker *p;
|
|
550 /* Detact the bug that seems to have caused this to be called from
|
|
551 a signal handler. */
|
|
552 int mask = sigsetmask (-1);
|
|
553 sigsetmask (mask);
|
|
554 if (mask != 0)
|
|
555 abort ();
|
|
556
|
|
557 if (marker_free_list)
|
|
558 {
|
|
559 XSET (val, Lisp_Marker, marker_free_list);
|
|
560 marker_free_list
|
|
561 = (struct Lisp_Marker *) XFASTINT (marker_free_list->chain);
|
|
562 }
|
|
563 else
|
|
564 {
|
|
565 if (marker_block_index == MARKER_BLOCK_SIZE)
|
|
566 {
|
|
567 struct marker_block *new = (struct marker_block *) malloc (sizeof (struct marker_block));
|
|
568 if (!new) memory_full ();
|
|
569 VALIDATE_LISP_STORAGE (new, sizeof *new);
|
|
570 new->next = marker_block;
|
|
571 marker_block = new;
|
|
572 marker_block_index = 0;
|
|
573 }
|
|
574 XSET (val, Lisp_Marker, &marker_block->markers[marker_block_index++]);
|
|
575 }
|
|
576 p = XMARKER (val);
|
|
577 p->buffer = 0;
|
|
578 p->bufpos = 0;
|
|
579 p->chain = Qnil;
|
|
580 consing_since_gc += sizeof (struct Lisp_Marker);
|
|
581 return val;
|
|
582 }
|
|
583
|
|
584 /* Allocation of strings */
|
|
585
|
|
586 /* Strings reside inside of string_blocks. The entire data of the string,
|
|
587 both the size and the contents, live in part of the `chars' component of a string_block.
|
|
588 The `pos' component is the index within `chars' of the first free byte.
|
|
589
|
|
590 first_string_block points to the first string_block ever allocated.
|
|
591 Each block points to the next one with its `next' field.
|
|
592 The `prev' fields chain in reverse order.
|
|
593 The last one allocated is the one currently being filled.
|
|
594 current_string_block points to it.
|
|
595
|
|
596 The string_blocks that hold individual large strings
|
|
597 go in a separate chain, started by large_string_blocks. */
|
|
598
|
|
599
|
|
600 /* String blocks contain this many useful bytes.
|
|
601 8188 is power of 2, minus 4 for malloc overhead. */
|
|
602 #define STRING_BLOCK_SIZE (8188 - sizeof (struct string_block_head))
|
|
603
|
|
604 /* A string bigger than this gets its own specially-made string block
|
|
605 if it doesn't fit in the current one. */
|
|
606 #define STRING_BLOCK_OUTSIZE 1024
|
|
607
|
|
608 struct string_block_head
|
|
609 {
|
|
610 struct string_block *next, *prev;
|
|
611 int pos;
|
|
612 };
|
|
613
|
|
614 struct string_block
|
|
615 {
|
|
616 struct string_block *next, *prev;
|
|
617 int pos;
|
|
618 char chars[STRING_BLOCK_SIZE];
|
|
619 };
|
|
620
|
|
621 /* This points to the string block we are now allocating strings. */
|
|
622
|
|
623 struct string_block *current_string_block;
|
|
624
|
|
625 /* This points to the oldest string block, the one that starts the chain. */
|
|
626
|
|
627 struct string_block *first_string_block;
|
|
628
|
|
629 /* Last string block in chain of those made for individual large strings. */
|
|
630
|
|
631 struct string_block *large_string_blocks;
|
|
632
|
|
633 /* If SIZE is the length of a string, this returns how many bytes
|
|
634 the string occupies in a string_block (including padding). */
|
|
635
|
|
636 #define STRING_FULLSIZE(size) (((size) + sizeof (struct Lisp_String) + PAD) \
|
|
637 & ~(PAD - 1))
|
|
638 #define PAD (sizeof (int))
|
|
639
|
|
640 #if 0
|
|
641 #define STRING_FULLSIZE(SIZE) \
|
|
642 (((SIZE) + 2 * sizeof (int)) & ~(sizeof (int) - 1))
|
|
643 #endif
|
|
644
|
|
645 void
|
|
646 init_strings ()
|
|
647 {
|
|
648 current_string_block = (struct string_block *) malloc (sizeof (struct string_block));
|
|
649 first_string_block = current_string_block;
|
|
650 consing_since_gc += sizeof (struct string_block);
|
|
651 current_string_block->next = 0;
|
|
652 current_string_block->prev = 0;
|
|
653 current_string_block->pos = 0;
|
|
654 large_string_blocks = 0;
|
|
655 }
|
|
656
|
|
657 DEFUN ("make-string", Fmake_string, Smake_string, 2, 2, 0,
|
|
658 "Return a newly created string of length LENGTH, with each element being INIT.\n\
|
|
659 Both LENGTH and INIT must be numbers.")
|
|
660 (length, init)
|
|
661 Lisp_Object length, init;
|
|
662 {
|
|
663 register Lisp_Object val;
|
|
664 register unsigned char *p, *end, c;
|
|
665
|
|
666 if (XTYPE (length) != Lisp_Int || XINT (length) < 0)
|
|
667 length = wrong_type_argument (Qnatnump, length);
|
|
668 CHECK_NUMBER (init, 1);
|
|
669 val = make_uninit_string (XINT (length));
|
|
670 c = XINT (init);
|
|
671 p = XSTRING (val)->data;
|
|
672 end = p + XSTRING (val)->size;
|
|
673 while (p != end)
|
|
674 *p++ = c;
|
|
675 *p = 0;
|
|
676 return val;
|
|
677 }
|
|
678
|
|
679 Lisp_Object
|
|
680 make_string (contents, length)
|
|
681 char *contents;
|
|
682 int length;
|
|
683 {
|
|
684 register Lisp_Object val;
|
|
685 val = make_uninit_string (length);
|
|
686 bcopy (contents, XSTRING (val)->data, length);
|
|
687 return val;
|
|
688 }
|
|
689
|
|
690 Lisp_Object
|
|
691 build_string (str)
|
|
692 char *str;
|
|
693 {
|
|
694 return make_string (str, strlen (str));
|
|
695 }
|
|
696
|
|
697 Lisp_Object
|
|
698 make_uninit_string (length)
|
|
699 int length;
|
|
700 {
|
|
701 register Lisp_Object val;
|
|
702 register int fullsize = STRING_FULLSIZE (length);
|
|
703
|
|
704 if (length < 0) abort ();
|
|
705
|
|
706 if (fullsize <= STRING_BLOCK_SIZE - current_string_block->pos)
|
|
707 /* This string can fit in the current string block */
|
|
708 {
|
|
709 XSET (val, Lisp_String,
|
|
710 (struct Lisp_String *) (current_string_block->chars + current_string_block->pos));
|
|
711 current_string_block->pos += fullsize;
|
|
712 }
|
|
713 else if (fullsize > STRING_BLOCK_OUTSIZE)
|
|
714 /* This string gets its own string block */
|
|
715 {
|
|
716 register struct string_block *new
|
|
717 = (struct string_block *) malloc (sizeof (struct string_block_head) + fullsize);
|
|
718 VALIDATE_LISP_STORAGE (new, 0);
|
|
719 if (!new) memory_full ();
|
|
720 consing_since_gc += sizeof (struct string_block_head) + fullsize;
|
|
721 new->pos = fullsize;
|
|
722 new->next = large_string_blocks;
|
|
723 large_string_blocks = new;
|
|
724 XSET (val, Lisp_String,
|
|
725 (struct Lisp_String *) ((struct string_block_head *)new + 1));
|
|
726 }
|
|
727 else
|
|
728 /* Make a new current string block and start it off with this string */
|
|
729 {
|
|
730 register struct string_block *new
|
|
731 = (struct string_block *) malloc (sizeof (struct string_block));
|
|
732 if (!new) memory_full ();
|
|
733 VALIDATE_LISP_STORAGE (new, sizeof *new);
|
|
734 consing_since_gc += sizeof (struct string_block);
|
|
735 current_string_block->next = new;
|
|
736 new->prev = current_string_block;
|
|
737 new->next = 0;
|
|
738 current_string_block = new;
|
|
739 new->pos = fullsize;
|
|
740 XSET (val, Lisp_String,
|
|
741 (struct Lisp_String *) current_string_block->chars);
|
|
742 }
|
|
743
|
|
744 XSTRING (val)->size = length;
|
|
745 XSTRING (val)->data[length] = 0;
|
|
746
|
|
747 return val;
|
|
748 }
|
|
749
|
|
750 /* Return a newly created vector or string with specified arguments as
|
|
751 elements. If all the arguments are characters, make a string;
|
|
752 otherwise, make a vector. Any number of arguments, even zero
|
|
753 arguments, are allowed. */
|
|
754
|
|
755 Lisp_Object
|
|
756 make_sequence (nargs, args)
|
|
757 register int nargs;
|
|
758 Lisp_Object *args;
|
|
759 {
|
|
760 int i;
|
|
761
|
|
762 for (i = 0; i < nargs; i++)
|
|
763 if (XTYPE (args[i]) != Lisp_Int
|
|
764 || (unsigned) XINT (args[i]) >= 0400)
|
|
765 return Fvector (nargs, args);
|
|
766
|
|
767 /* Since the loop exited, we know that all the things in it are
|
|
768 characters, so we can make a string. */
|
|
769 {
|
|
770 Lisp_Object result = Fmake_string (nargs, make_number (0));
|
|
771
|
|
772 for (i = 0; i < nargs; i++)
|
|
773 XSTRING (result)->data[i] = XINT (args[i]);
|
|
774
|
|
775 return result;
|
|
776 }
|
|
777 }
|
|
778
|
|
779 /* Note: the user cannot manipulate ropes portably by referring
|
|
780 to the chars of the string, because combining two chars to make a GLYPH
|
|
781 depends on endianness. */
|
|
782
|
|
783 DEFUN ("make-rope", Fmake_rope, Smake_rope, 0, MANY, 0,
|
363
|
784 "Return a newly created rope containing the arguments of this function.\n\
|
300
|
785 A rope is a string, except that its contents will be treated as an\n\
|
|
786 array of glyphs, where a glyph is an integer type that may be larger\n\
|
|
787 than a character. Emacs is normally configured to use 8-bit glyphs,\n\
|
|
788 so ropes are normally no different from strings. But Emacs may be\n\
|
|
789 configured to use 16-bit glyphs, to allow the use of larger fonts.\n\
|
|
790 \n\
|
|
791 Each argument (which must be an integer) specifies one glyph, whatever\n\
|
|
792 size glyphs may be.\n\
|
|
793 \n\
|
|
794 See variable `buffer-display-table' for the uses of ropes.")
|
|
795 (nargs, args)
|
|
796 register int nargs;
|
|
797 Lisp_Object *args;
|
|
798 {
|
|
799 register int i;
|
|
800 register Lisp_Object val;
|
|
801 register GLYPH *p;
|
|
802
|
|
803 val = make_uninit_string (nargs * sizeof (GLYPH));
|
|
804
|
|
805 p = (GLYPH *) XSTRING (val)->data;
|
|
806 for (i = 0; i < nargs; i++)
|
|
807 {
|
|
808 CHECK_NUMBER (args[i], i);
|
|
809 p[i] = XFASTINT (args[i]);
|
|
810 }
|
|
811 return val;
|
|
812 }
|
|
813
|
|
814 DEFUN ("rope-elt", Frope_elt, Srope_elt, 2, 2, 0,
|
|
815 "Return an element of rope R at index N.\n\
|
|
816 A rope is a string in which each pair of bytes is considered an element.\n\
|
|
817 See variable `buffer-display-table' for the uses of ropes.")
|
|
818 (r, n)
|
|
819 {
|
|
820 CHECK_STRING (r, 0);
|
|
821 CHECK_NUMBER (n, 1);
|
|
822 if ((XSTRING (r)->size / sizeof (GLYPH)) <= XINT (n) || XINT (n) < 0)
|
|
823 args_out_of_range (r, n);
|
|
824 return ((GLYPH *) XSTRING (r)->data)[XFASTINT (n)];
|
|
825 }
|
|
826
|
|
827 /* Must get an error if pure storage is full,
|
|
828 since if it cannot hold a large string
|
|
829 it may be able to hold conses that point to that string;
|
|
830 then the string is not protected from gc. */
|
|
831
|
|
832 Lisp_Object
|
|
833 make_pure_string (data, length)
|
|
834 char *data;
|
|
835 int length;
|
|
836 {
|
|
837 register Lisp_Object new;
|
|
838 register int size = sizeof (int) + length + 1;
|
|
839
|
|
840 if (pureptr + size > PURESIZE)
|
|
841 error ("Pure Lisp storage exhausted");
|
|
842 XSET (new, Lisp_String, PUREBEG + pureptr);
|
|
843 XSTRING (new)->size = length;
|
|
844 bcopy (data, XSTRING (new)->data, length);
|
|
845 XSTRING (new)->data[length] = 0;
|
|
846 pureptr += (size + sizeof (int) - 1)
|
|
847 / sizeof (int) * sizeof (int);
|
|
848 return new;
|
|
849 }
|
|
850
|
|
851 Lisp_Object
|
|
852 pure_cons (car, cdr)
|
|
853 Lisp_Object car, cdr;
|
|
854 {
|
|
855 register Lisp_Object new;
|
|
856
|
|
857 if (pureptr + sizeof (struct Lisp_Cons) > PURESIZE)
|
|
858 error ("Pure Lisp storage exhausted");
|
|
859 XSET (new, Lisp_Cons, PUREBEG + pureptr);
|
|
860 pureptr += sizeof (struct Lisp_Cons);
|
|
861 XCONS (new)->car = Fpurecopy (car);
|
|
862 XCONS (new)->cdr = Fpurecopy (cdr);
|
|
863 return new;
|
|
864 }
|
|
865
|
|
866 #ifdef LISP_FLOAT_TYPE
|
|
867
|
|
868 Lisp_Object
|
|
869 make_pure_float (num)
|
|
870 double num;
|
|
871 {
|
|
872 register Lisp_Object new;
|
|
873
|
|
874 if (pureptr + sizeof (struct Lisp_Float) > PURESIZE)
|
|
875 error ("Pure Lisp storage exhausted");
|
|
876 XSET (new, Lisp_Float, PUREBEG + pureptr);
|
|
877 pureptr += sizeof (struct Lisp_Float);
|
|
878 XFLOAT (new)->data = num;
|
|
879 XFLOAT (new)->type = 0; /* bug chasing -wsr */
|
|
880 return new;
|
|
881 }
|
|
882
|
|
883 #endif /* LISP_FLOAT_TYPE */
|
|
884
|
|
885 Lisp_Object
|
|
886 make_pure_vector (len)
|
|
887 int len;
|
|
888 {
|
|
889 register Lisp_Object new;
|
|
890 register int size = sizeof (struct Lisp_Vector) + (len - 1) * sizeof (Lisp_Object);
|
|
891
|
|
892 if (pureptr + size > PURESIZE)
|
|
893 error ("Pure Lisp storage exhausted");
|
|
894
|
|
895 XSET (new, Lisp_Vector, PUREBEG + pureptr);
|
|
896 pureptr += size;
|
|
897 XVECTOR (new)->size = len;
|
|
898 return new;
|
|
899 }
|
|
900
|
|
901 DEFUN ("purecopy", Fpurecopy, Spurecopy, 1, 1, 0,
|
|
902 "Make a copy of OBJECT in pure storage.\n\
|
|
903 Recursively copies contents of vectors and cons cells.\n\
|
|
904 Does not copy symbols.")
|
|
905 (obj)
|
|
906 register Lisp_Object obj;
|
|
907 {
|
|
908 register Lisp_Object new, tem;
|
|
909 register int i;
|
|
910
|
|
911 if (NULL (Vpurify_flag))
|
|
912 return obj;
|
|
913
|
|
914 if ((PNTR_COMPARISON_TYPE) XPNTR (obj) < (PNTR_COMPARISON_TYPE) ((char *) pure + PURESIZE)
|
|
915 && (PNTR_COMPARISON_TYPE) XPNTR (obj) >= (PNTR_COMPARISON_TYPE) pure)
|
|
916 return obj;
|
|
917
|
|
918 #ifdef SWITCH_ENUM_BUG
|
|
919 switch ((int) XTYPE (obj))
|
|
920 #else
|
|
921 switch (XTYPE (obj))
|
|
922 #endif
|
|
923 {
|
|
924 case Lisp_Marker:
|
|
925 error ("Attempt to copy a marker to pure storage");
|
|
926
|
|
927 case Lisp_Cons:
|
|
928 return pure_cons (XCONS (obj)->car, XCONS (obj)->cdr);
|
|
929
|
|
930 #ifdef LISP_FLOAT_TYPE
|
|
931 case Lisp_Float:
|
|
932 return make_pure_float (XFLOAT (obj)->data);
|
|
933 #endif /* LISP_FLOAT_TYPE */
|
|
934
|
|
935 case Lisp_String:
|
|
936 return make_pure_string (XSTRING (obj)->data, XSTRING (obj)->size);
|
|
937
|
|
938 case Lisp_Compiled:
|
|
939 case Lisp_Vector:
|
|
940 new = make_pure_vector (XVECTOR (obj)->size);
|
|
941 for (i = 0; i < XVECTOR (obj)->size; i++)
|
|
942 {
|
|
943 tem = XVECTOR (obj)->contents[i];
|
|
944 XVECTOR (new)->contents[i] = Fpurecopy (tem);
|
|
945 }
|
|
946 XSETTYPE (new, XTYPE (obj));
|
|
947 return new;
|
|
948
|
|
949 default:
|
|
950 return obj;
|
|
951 }
|
|
952 }
|
|
953
|
|
954 /* Recording what needs to be marked for gc. */
|
|
955
|
|
956 struct gcpro *gcprolist;
|
|
957
|
|
958 #define NSTATICS 256
|
|
959
|
|
960 Lisp_Object *staticvec[NSTATICS] = {0};
|
|
961
|
|
962 int staticidx = 0;
|
|
963
|
|
964 /* Put an entry in staticvec, pointing at the variable whose address is given */
|
|
965
|
|
966 void
|
|
967 staticpro (varaddress)
|
|
968 Lisp_Object *varaddress;
|
|
969 {
|
|
970 staticvec[staticidx++] = varaddress;
|
|
971 if (staticidx >= NSTATICS)
|
|
972 abort ();
|
|
973 }
|
|
974
|
|
975 struct catchtag
|
|
976 {
|
|
977 Lisp_Object tag;
|
|
978 Lisp_Object val;
|
|
979 struct catchtag *next;
|
|
980 /* jmp_buf jmp; /* We don't need this for GC purposes */
|
|
981 };
|
|
982
|
|
983 struct backtrace
|
|
984 {
|
|
985 struct backtrace *next;
|
|
986 Lisp_Object *function;
|
|
987 Lisp_Object *args; /* Points to vector of args. */
|
|
988 int nargs; /* length of vector */
|
|
989 /* if nargs is UNEVALLED, args points to slot holding list of unevalled args */
|
|
990 char evalargs;
|
|
991 };
|
|
992
|
|
993 /* Two flags that are set during GC in the `size' component
|
|
994 of a string or vector. On some machines, these flags
|
|
995 are defined by the m- file to be different bits. */
|
|
996
|
|
997 /* On vector, means it has been marked.
|
|
998 On string size field or a reference to a string,
|
|
999 means not the last reference in the chain. */
|
|
1000
|
|
1001 #ifndef ARRAY_MARK_FLAG
|
|
1002 #define ARRAY_MARK_FLAG ((MARKBIT >> 1) & ~MARKBIT)
|
|
1003 #endif /* no ARRAY_MARK_FLAG */
|
|
1004
|
|
1005 /* Any slot that is a Lisp_Object can point to a string
|
|
1006 and thus can be put on a string's reference-chain
|
|
1007 and thus may need to have its ARRAY_MARK_FLAG set.
|
|
1008 This includes the slots whose markbits are used to mark
|
|
1009 the containing objects. */
|
|
1010
|
|
1011 #if ARRAY_MARK_FLAG == MARKBIT
|
|
1012 you lose
|
|
1013 #endif
|
|
1014
|
|
1015 int total_conses, total_markers, total_symbols, total_string_size, total_vector_size;
|
|
1016 int total_free_conses, total_free_markers, total_free_symbols;
|
|
1017 #ifdef LISP_FLOAT_TYPE
|
|
1018 int total_free_floats, total_floats;
|
|
1019 #endif /* LISP_FLOAT_TYPE */
|
|
1020
|
|
1021 static void mark_object (), mark_buffer ();
|
|
1022 static void clear_marks (), gc_sweep ();
|
|
1023 static void compact_strings ();
|
|
1024
|
|
1025 DEFUN ("garbage-collect", Fgarbage_collect, Sgarbage_collect, 0, 0, "",
|
|
1026 "Reclaim storage for Lisp objects no longer needed.\n\
|
|
1027 Returns info on amount of space in use:\n\
|
|
1028 ((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS)\n\
|
|
1029 (USED-MARKERS . FREE-MARKERS) USED-STRING-CHARS USED-VECTOR-SLOTS\n\
|
|
1030 (USED-FLOATS . FREE-FLOATS))\n\
|
|
1031 Garbage collection happens automatically if you cons more than\n\
|
|
1032 `gc-cons-threshold' bytes of Lisp data since previous garbage collection.")
|
|
1033 ()
|
|
1034 {
|
|
1035 register struct gcpro *tail;
|
|
1036 register struct specbinding *bind;
|
|
1037 struct catchtag *catch;
|
|
1038 struct handler *handler;
|
|
1039 register struct backtrace *backlist;
|
|
1040 register Lisp_Object tem;
|
|
1041 char *omessage = echo_area_glyphs;
|
|
1042 char stack_top_variable;
|
|
1043 register int i;
|
|
1044
|
|
1045 BLOCK_INPUT;
|
|
1046
|
|
1047 /* Save a copy of the contents of the stack, for debugging. */
|
|
1048 #if MAX_SAVE_STACK > 0
|
|
1049 if (NULL (Vpurify_flag))
|
|
1050 {
|
|
1051 i = &stack_top_variable - stack_bottom;
|
|
1052 if (i < 0) i = -i;
|
|
1053 if (i < MAX_SAVE_STACK)
|
|
1054 {
|
|
1055 if (stack_copy == 0)
|
|
1056 stack_copy = (char *) malloc (stack_copy_size = i);
|
|
1057 else if (stack_copy_size < i)
|
|
1058 stack_copy = (char *) realloc (stack_copy, (stack_copy_size = i));
|
|
1059 if (stack_copy)
|
|
1060 {
|
|
1061 if ((int) (&stack_top_variable - stack_bottom) > 0)
|
|
1062 bcopy (stack_bottom, stack_copy, i);
|
|
1063 else
|
|
1064 bcopy (&stack_top_variable, stack_copy, i);
|
|
1065 }
|
|
1066 }
|
|
1067 }
|
|
1068 #endif /* MAX_SAVE_STACK > 0 */
|
|
1069
|
|
1070 if (!noninteractive)
|
|
1071 message1 ("Garbage collecting...");
|
|
1072
|
|
1073 /* Don't keep command history around forever */
|
|
1074 tem = Fnthcdr (make_number (30), Vcommand_history);
|
|
1075 if (CONSP (tem))
|
|
1076 XCONS (tem)->cdr = Qnil;
|
|
1077 /* Likewise for undo information. */
|
|
1078 {
|
|
1079 register struct buffer *nextb = all_buffers;
|
|
1080
|
|
1081 while (nextb)
|
|
1082 {
|
|
1083 nextb->undo_list
|
|
1084 = truncate_undo_list (nextb->undo_list, undo_threshold,
|
|
1085 undo_high_threshold);
|
|
1086 nextb = nextb->next;
|
|
1087 }
|
|
1088 }
|
|
1089
|
|
1090 gc_in_progress = 1;
|
|
1091
|
|
1092 /* clear_marks (); */
|
|
1093
|
|
1094 /* In each "large string", set the MARKBIT of the size field.
|
|
1095 That enables mark_object to recognize them. */
|
|
1096 {
|
|
1097 register struct string_block *b;
|
|
1098 for (b = large_string_blocks; b; b = b->next)
|
|
1099 ((struct Lisp_String *)(&b->chars[0]))->size |= MARKBIT;
|
|
1100 }
|
|
1101
|
|
1102 /* Mark all the special slots that serve as the roots of accessibility.
|
|
1103
|
|
1104 Usually the special slots to mark are contained in particular structures.
|
|
1105 Then we know no slot is marked twice because the structures don't overlap.
|
|
1106 In some cases, the structures point to the slots to be marked.
|
|
1107 For these, we use MARKBIT to avoid double marking of the slot. */
|
|
1108
|
|
1109 for (i = 0; i < staticidx; i++)
|
|
1110 mark_object (staticvec[i]);
|
|
1111 for (tail = gcprolist; tail; tail = tail->next)
|
|
1112 for (i = 0; i < tail->nvars; i++)
|
|
1113 if (!XMARKBIT (tail->var[i]))
|
|
1114 {
|
|
1115 mark_object (&tail->var[i]);
|
|
1116 XMARK (tail->var[i]);
|
|
1117 }
|
|
1118 for (bind = specpdl; bind != specpdl_ptr; bind++)
|
|
1119 {
|
|
1120 mark_object (&bind->symbol);
|
|
1121 mark_object (&bind->old_value);
|
|
1122 }
|
|
1123 for (catch = catchlist; catch; catch = catch->next)
|
|
1124 {
|
|
1125 mark_object (&catch->tag);
|
|
1126 mark_object (&catch->val);
|
|
1127 }
|
|
1128 for (handler = handlerlist; handler; handler = handler->next)
|
|
1129 {
|
|
1130 mark_object (&handler->handler);
|
|
1131 mark_object (&handler->var);
|
|
1132 }
|
|
1133 for (backlist = backtrace_list; backlist; backlist = backlist->next)
|
|
1134 {
|
|
1135 if (!XMARKBIT (*backlist->function))
|
|
1136 {
|
|
1137 mark_object (backlist->function);
|
|
1138 XMARK (*backlist->function);
|
|
1139 }
|
|
1140 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
|
|
1141 i = 0;
|
|
1142 else
|
|
1143 i = backlist->nargs - 1;
|
|
1144 for (; i >= 0; i--)
|
|
1145 if (!XMARKBIT (backlist->args[i]))
|
|
1146 {
|
|
1147 mark_object (&backlist->args[i]);
|
|
1148 XMARK (backlist->args[i]);
|
|
1149 }
|
|
1150 }
|
|
1151
|
|
1152 gc_sweep ();
|
|
1153
|
|
1154 /* Clear the mark bits that we set in certain root slots. */
|
|
1155
|
|
1156 for (tail = gcprolist; tail; tail = tail->next)
|
|
1157 for (i = 0; i < tail->nvars; i++)
|
|
1158 XUNMARK (tail->var[i]);
|
|
1159 for (backlist = backtrace_list; backlist; backlist = backlist->next)
|
|
1160 {
|
|
1161 XUNMARK (*backlist->function);
|
|
1162 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
|
|
1163 i = 0;
|
|
1164 else
|
|
1165 i = backlist->nargs - 1;
|
|
1166 for (; i >= 0; i--)
|
|
1167 XUNMARK (backlist->args[i]);
|
|
1168 }
|
|
1169 XUNMARK (buffer_defaults.name);
|
|
1170 XUNMARK (buffer_local_symbols.name);
|
|
1171
|
|
1172 /* clear_marks (); */
|
|
1173 gc_in_progress = 0;
|
|
1174
|
|
1175 consing_since_gc = 0;
|
|
1176 if (gc_cons_threshold < 10000)
|
|
1177 gc_cons_threshold = 10000;
|
|
1178
|
|
1179 if (omessage)
|
|
1180 message1 (omessage);
|
|
1181 else if (!noninteractive)
|
|
1182 message1 ("Garbage collecting...done");
|
|
1183
|
|
1184 #ifdef HAVE_X_WINDOWS
|
|
1185 UNBLOCK_INPUT;
|
|
1186 #endif
|
|
1187
|
|
1188 return Fcons (Fcons (make_number (total_conses),
|
|
1189 make_number (total_free_conses)),
|
|
1190 Fcons (Fcons (make_number (total_symbols),
|
|
1191 make_number (total_free_symbols)),
|
|
1192 Fcons (Fcons (make_number (total_markers),
|
|
1193 make_number (total_free_markers)),
|
|
1194 Fcons (make_number (total_string_size),
|
|
1195 Fcons (make_number (total_vector_size),
|
|
1196
|
|
1197 #ifdef LISP_FLOAT_TYPE
|
|
1198 Fcons (Fcons (make_number (total_floats),
|
|
1199 make_number (total_free_floats)),
|
|
1200 Qnil)
|
|
1201 #else /* not LISP_FLOAT_TYPE */
|
|
1202 Qnil
|
|
1203 #endif /* not LISP_FLOAT_TYPE */
|
|
1204 )))));
|
|
1205 }
|
|
1206
|
|
1207 #if 0
|
|
1208 static void
|
|
1209 clear_marks ()
|
|
1210 {
|
|
1211 /* Clear marks on all conses */
|
|
1212 {
|
|
1213 register struct cons_block *cblk;
|
|
1214 register int lim = cons_block_index;
|
|
1215
|
|
1216 for (cblk = cons_block; cblk; cblk = cblk->next)
|
|
1217 {
|
|
1218 register int i;
|
|
1219 for (i = 0; i < lim; i++)
|
|
1220 XUNMARK (cblk->conses[i].car);
|
|
1221 lim = CONS_BLOCK_SIZE;
|
|
1222 }
|
|
1223 }
|
|
1224 /* Clear marks on all symbols */
|
|
1225 {
|
|
1226 register struct symbol_block *sblk;
|
|
1227 register int lim = symbol_block_index;
|
|
1228
|
|
1229 for (sblk = symbol_block; sblk; sblk = sblk->next)
|
|
1230 {
|
|
1231 register int i;
|
|
1232 for (i = 0; i < lim; i++)
|
|
1233 {
|
|
1234 XUNMARK (sblk->symbols[i].plist);
|
|
1235 }
|
|
1236 lim = SYMBOL_BLOCK_SIZE;
|
|
1237 }
|
|
1238 }
|
|
1239 /* Clear marks on all markers */
|
|
1240 {
|
|
1241 register struct marker_block *sblk;
|
|
1242 register int lim = marker_block_index;
|
|
1243
|
|
1244 for (sblk = marker_block; sblk; sblk = sblk->next)
|
|
1245 {
|
|
1246 register int i;
|
|
1247 for (i = 0; i < lim; i++)
|
|
1248 XUNMARK (sblk->markers[i].chain);
|
|
1249 lim = MARKER_BLOCK_SIZE;
|
|
1250 }
|
|
1251 }
|
|
1252 /* Clear mark bits on all buffers */
|
|
1253 {
|
|
1254 register struct buffer *nextb = all_buffers;
|
|
1255
|
|
1256 while (nextb)
|
|
1257 {
|
|
1258 XUNMARK (nextb->name);
|
|
1259 nextb = nextb->next;
|
|
1260 }
|
|
1261 }
|
|
1262 }
|
|
1263 #endif
|
|
1264
|
|
1265 /* Mark reference to a Lisp_Object. If the object referred to
|
|
1266 has not been seen yet, recursively mark all the references contained in it.
|
|
1267
|
|
1268 If the object referenced is a short string, the referrencing slot
|
|
1269 is threaded into a chain of such slots, pointed to from
|
|
1270 the `size' field of the string. The actual string size
|
|
1271 lives in the last slot in the chain. We recognize the end
|
|
1272 because it is < (unsigned) STRING_BLOCK_SIZE. */
|
|
1273
|
|
1274 static void
|
|
1275 mark_object (objptr)
|
|
1276 Lisp_Object *objptr;
|
|
1277 {
|
|
1278 register Lisp_Object obj;
|
|
1279
|
|
1280 obj = *objptr;
|
|
1281 XUNMARK (obj);
|
|
1282
|
|
1283 loop:
|
|
1284
|
|
1285 if ((PNTR_COMPARISON_TYPE) XPNTR (obj) < (PNTR_COMPARISON_TYPE) ((char *) pure + PURESIZE)
|
|
1286 && (PNTR_COMPARISON_TYPE) XPNTR (obj) >= (PNTR_COMPARISON_TYPE) pure)
|
|
1287 return;
|
|
1288
|
|
1289 #ifdef SWITCH_ENUM_BUG
|
|
1290 switch ((int) XGCTYPE (obj))
|
|
1291 #else
|
|
1292 switch (XGCTYPE (obj))
|
|
1293 #endif
|
|
1294 {
|
|
1295 case Lisp_String:
|
|
1296 {
|
|
1297 register struct Lisp_String *ptr = XSTRING (obj);
|
|
1298
|
|
1299 if (ptr->size & MARKBIT)
|
|
1300 /* A large string. Just set ARRAY_MARK_FLAG. */
|
|
1301 ptr->size |= ARRAY_MARK_FLAG;
|
|
1302 else
|
|
1303 {
|
|
1304 /* A small string. Put this reference
|
|
1305 into the chain of references to it.
|
|
1306 The address OBJPTR is even, so if the address
|
|
1307 includes MARKBIT, put it in the low bit
|
|
1308 when we store OBJPTR into the size field. */
|
|
1309
|
|
1310 if (XMARKBIT (*objptr))
|
|
1311 {
|
|
1312 XFASTINT (*objptr) = ptr->size;
|
|
1313 XMARK (*objptr);
|
|
1314 }
|
|
1315 else
|
|
1316 XFASTINT (*objptr) = ptr->size;
|
|
1317 if ((int)objptr & 1) abort ();
|
|
1318 ptr->size = (int) objptr & ~MARKBIT;
|
|
1319 if ((int) objptr & MARKBIT)
|
|
1320 ptr->size ++;
|
|
1321 }
|
|
1322 }
|
|
1323 break;
|
|
1324
|
|
1325 case Lisp_Vector:
|
|
1326 case Lisp_Window:
|
|
1327 case Lisp_Process:
|
|
1328 case Lisp_Window_Configuration:
|
|
1329 case Lisp_Compiled:
|
|
1330 {
|
|
1331 register struct Lisp_Vector *ptr = XVECTOR (obj);
|
|
1332 register int size = ptr->size;
|
|
1333 register int i;
|
|
1334
|
|
1335 if (size & ARRAY_MARK_FLAG) break; /* Already marked */
|
|
1336 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
|
|
1337 for (i = 0; i < size; i++) /* and then mark its elements */
|
|
1338 mark_object (&ptr->contents[i]);
|
|
1339 }
|
|
1340 break;
|
|
1341
|
|
1342 #ifdef MULTI_SCREEN
|
|
1343 case Lisp_Screen:
|
|
1344 {
|
|
1345 register struct screen *ptr = XSCREEN (obj);
|
|
1346 register int size = ptr->size;
|
|
1347 register int i;
|
|
1348
|
|
1349 if (size & ARRAY_MARK_FLAG) break; /* Already marked */
|
|
1350 ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
|
|
1351
|
|
1352 mark_object (&ptr->name);
|
356
|
1353 mark_object (&ptr->focus_screen);
|
300
|
1354 mark_object (&ptr->width);
|
|
1355 mark_object (&ptr->height);
|
|
1356 mark_object (&ptr->selected_window);
|
|
1357 mark_object (&ptr->minibuffer_window);
|
|
1358 mark_object (&ptr->param_alist);
|
|
1359 }
|
|
1360 break;
|
|
1361 #endif /* MULTI_SCREEN */
|
|
1362
|
|
1363 #if 0
|
|
1364 case Lisp_Temp_Vector:
|
|
1365 {
|
|
1366 register struct Lisp_Vector *ptr = XVECTOR (obj);
|
|
1367 register int size = ptr->size;
|
|
1368 register int i;
|
|
1369
|
|
1370 for (i = 0; i < size; i++) /* and then mark its elements */
|
|
1371 mark_object (&ptr->contents[i]);
|
|
1372 }
|
|
1373 break;
|
|
1374 #endif /* 0 */
|
|
1375
|
|
1376 case Lisp_Symbol:
|
|
1377 {
|
|
1378 register struct Lisp_Symbol *ptr = XSYMBOL (obj);
|
|
1379 struct Lisp_Symbol *ptrx;
|
|
1380
|
|
1381 if (XMARKBIT (ptr->plist)) break;
|
|
1382 XMARK (ptr->plist);
|
|
1383 XSETTYPE (*(Lisp_Object *) &ptr->name, Lisp_String);
|
|
1384 mark_object (&ptr->name);
|
|
1385 mark_object ((Lisp_Object *) &ptr->value);
|
|
1386 mark_object (&ptr->function);
|
|
1387 mark_object (&ptr->plist);
|
|
1388 ptr = ptr->next;
|
|
1389 if (ptr)
|
|
1390 {
|
|
1391 ptrx = ptr; /* Use pf ptrx avoids compiler bug on Sun */
|
|
1392 XSETSYMBOL (obj, ptrx);
|
|
1393 goto loop;
|
|
1394 }
|
|
1395 }
|
|
1396 break;
|
|
1397
|
|
1398 case Lisp_Marker:
|
|
1399 XMARK (XMARKER (obj)->chain);
|
|
1400 /* DO NOT mark thru the marker's chain.
|
|
1401 The buffer's markers chain does not preserve markers from gc;
|
|
1402 instead, markers are removed from the chain when they are freed by gc. */
|
|
1403 break;
|
|
1404
|
|
1405 case Lisp_Cons:
|
|
1406 case Lisp_Buffer_Local_Value:
|
|
1407 case Lisp_Some_Buffer_Local_Value:
|
|
1408 {
|
|
1409 register struct Lisp_Cons *ptr = XCONS (obj);
|
|
1410 if (XMARKBIT (ptr->car)) break;
|
|
1411 XMARK (ptr->car);
|
|
1412 mark_object (&ptr->car);
|
|
1413 objptr = &ptr->cdr;
|
|
1414 obj = ptr->cdr;
|
|
1415 goto loop;
|
|
1416 }
|
|
1417
|
|
1418 #ifdef LISP_FLOAT_TYPE
|
|
1419 case Lisp_Float:
|
|
1420 XMARK (XFLOAT (obj)->type);
|
|
1421 break;
|
|
1422 #endif /* LISP_FLOAT_TYPE */
|
|
1423
|
|
1424 case Lisp_Buffer:
|
|
1425 if (!XMARKBIT (XBUFFER (obj)->name))
|
|
1426 mark_buffer (obj);
|
|
1427 break;
|
|
1428
|
|
1429 case Lisp_Int:
|
|
1430 case Lisp_Void:
|
|
1431 case Lisp_Subr:
|
|
1432 case Lisp_Intfwd:
|
|
1433 case Lisp_Boolfwd:
|
|
1434 case Lisp_Objfwd:
|
|
1435 case Lisp_Buffer_Objfwd:
|
|
1436 case Lisp_Internal_Stream:
|
|
1437 /* Don't bother with Lisp_Buffer_Objfwd,
|
|
1438 since all markable slots in current buffer marked anyway. */
|
|
1439 /* Don't need to do Lisp_Objfwd, since the places they point
|
|
1440 are protected with staticpro. */
|
|
1441 break;
|
|
1442
|
|
1443 default:
|
|
1444 abort ();
|
|
1445 }
|
|
1446 }
|
|
1447
|
|
1448 /* Mark the pointers in a buffer structure. */
|
|
1449
|
|
1450 static void
|
|
1451 mark_buffer (buf)
|
|
1452 Lisp_Object buf;
|
|
1453 {
|
|
1454 Lisp_Object tem;
|
|
1455 register struct buffer *buffer = XBUFFER (buf);
|
|
1456 register Lisp_Object *ptr;
|
|
1457
|
|
1458 /* This is the buffer's markbit */
|
|
1459 mark_object (&buffer->name);
|
|
1460 XMARK (buffer->name);
|
|
1461
|
|
1462 #if 0
|
|
1463 mark_object (buffer->syntax_table);
|
|
1464
|
|
1465 /* Mark the various string-pointers in the buffer object.
|
|
1466 Since the strings may be relocated, we must mark them
|
|
1467 in their actual slots. So gc_sweep must convert each slot
|
|
1468 back to an ordinary C pointer. */
|
|
1469 XSET (*(Lisp_Object *)&buffer->upcase_table,
|
|
1470 Lisp_String, buffer->upcase_table);
|
|
1471 mark_object ((Lisp_Object *)&buffer->upcase_table);
|
|
1472 XSET (*(Lisp_Object *)&buffer->downcase_table,
|
|
1473 Lisp_String, buffer->downcase_table);
|
|
1474 mark_object ((Lisp_Object *)&buffer->downcase_table);
|
|
1475
|
|
1476 XSET (*(Lisp_Object *)&buffer->sort_table,
|
|
1477 Lisp_String, buffer->sort_table);
|
|
1478 mark_object ((Lisp_Object *)&buffer->sort_table);
|
|
1479 XSET (*(Lisp_Object *)&buffer->folding_sort_table,
|
|
1480 Lisp_String, buffer->folding_sort_table);
|
|
1481 mark_object ((Lisp_Object *)&buffer->folding_sort_table);
|
|
1482 #endif
|
|
1483
|
|
1484 for (ptr = &buffer->name + 1;
|
|
1485 (char *)ptr < (char *)buffer + sizeof (struct buffer);
|
|
1486 ptr++)
|
|
1487 mark_object (ptr);
|
|
1488 }
|
|
1489
|
|
1490 /* Find all structures not marked, and free them. */
|
|
1491
|
|
1492 static void
|
|
1493 gc_sweep ()
|
|
1494 {
|
|
1495 total_string_size = 0;
|
|
1496 compact_strings ();
|
|
1497
|
|
1498 /* Put all unmarked conses on free list */
|
|
1499 {
|
|
1500 register struct cons_block *cblk;
|
|
1501 register int lim = cons_block_index;
|
|
1502 register int num_free = 0, num_used = 0;
|
|
1503
|
|
1504 cons_free_list = 0;
|
|
1505
|
|
1506 for (cblk = cons_block; cblk; cblk = cblk->next)
|
|
1507 {
|
|
1508 register int i;
|
|
1509 for (i = 0; i < lim; i++)
|
|
1510 if (!XMARKBIT (cblk->conses[i].car))
|
|
1511 {
|
|
1512 XFASTINT (cblk->conses[i].car) = (int) cons_free_list;
|
|
1513 num_free++;
|
|
1514 cons_free_list = &cblk->conses[i];
|
|
1515 }
|
|
1516 else
|
|
1517 {
|
|
1518 num_used++;
|
|
1519 XUNMARK (cblk->conses[i].car);
|
|
1520 }
|
|
1521 lim = CONS_BLOCK_SIZE;
|
|
1522 }
|
|
1523 total_conses = num_used;
|
|
1524 total_free_conses = num_free;
|
|
1525 }
|
|
1526
|
|
1527 #ifdef LISP_FLOAT_TYPE
|
|
1528 /* Put all unmarked floats on free list */
|
|
1529 {
|
|
1530 register struct float_block *fblk;
|
|
1531 register int lim = float_block_index;
|
|
1532 register int num_free = 0, num_used = 0;
|
|
1533
|
|
1534 float_free_list = 0;
|
|
1535
|
|
1536 for (fblk = float_block; fblk; fblk = fblk->next)
|
|
1537 {
|
|
1538 register int i;
|
|
1539 for (i = 0; i < lim; i++)
|
|
1540 if (!XMARKBIT (fblk->floats[i].type))
|
|
1541 {
|
|
1542 XFASTINT (fblk->floats[i].type) = (int) float_free_list;
|
|
1543 num_free++;
|
|
1544 float_free_list = &fblk->floats[i];
|
|
1545 }
|
|
1546 else
|
|
1547 {
|
|
1548 num_used++;
|
|
1549 XUNMARK (fblk->floats[i].type);
|
|
1550 }
|
|
1551 lim = FLOAT_BLOCK_SIZE;
|
|
1552 }
|
|
1553 total_floats = num_used;
|
|
1554 total_free_floats = num_free;
|
|
1555 }
|
|
1556 #endif /* LISP_FLOAT_TYPE */
|
|
1557
|
|
1558 /* Put all unmarked symbols on free list */
|
|
1559 {
|
|
1560 register struct symbol_block *sblk;
|
|
1561 register int lim = symbol_block_index;
|
|
1562 register int num_free = 0, num_used = 0;
|
|
1563
|
|
1564 symbol_free_list = 0;
|
|
1565
|
|
1566 for (sblk = symbol_block; sblk; sblk = sblk->next)
|
|
1567 {
|
|
1568 register int i;
|
|
1569 for (i = 0; i < lim; i++)
|
|
1570 if (!XMARKBIT (sblk->symbols[i].plist))
|
|
1571 {
|
|
1572 XFASTINT (sblk->symbols[i].value) = (int) symbol_free_list;
|
|
1573 symbol_free_list = &sblk->symbols[i];
|
|
1574 num_free++;
|
|
1575 }
|
|
1576 else
|
|
1577 {
|
|
1578 num_used++;
|
|
1579 sblk->symbols[i].name
|
|
1580 = XSTRING (*(Lisp_Object *) &sblk->symbols[i].name);
|
|
1581 XUNMARK (sblk->symbols[i].plist);
|
|
1582 }
|
|
1583 lim = SYMBOL_BLOCK_SIZE;
|
|
1584 }
|
|
1585 total_symbols = num_used;
|
|
1586 total_free_symbols = num_free;
|
|
1587 }
|
|
1588
|
|
1589 #ifndef standalone
|
|
1590 /* Put all unmarked markers on free list.
|
|
1591 Dechain each one first from the buffer it points into. */
|
|
1592 {
|
|
1593 register struct marker_block *mblk;
|
|
1594 struct Lisp_Marker *tem1;
|
|
1595 register int lim = marker_block_index;
|
|
1596 register int num_free = 0, num_used = 0;
|
|
1597
|
|
1598 marker_free_list = 0;
|
|
1599
|
|
1600 for (mblk = marker_block; mblk; mblk = mblk->next)
|
|
1601 {
|
|
1602 register int i;
|
|
1603 for (i = 0; i < lim; i++)
|
|
1604 if (!XMARKBIT (mblk->markers[i].chain))
|
|
1605 {
|
|
1606 Lisp_Object tem;
|
|
1607 tem1 = &mblk->markers[i]; /* tem1 avoids Sun compiler bug */
|
|
1608 XSET (tem, Lisp_Marker, tem1);
|
|
1609 unchain_marker (tem);
|
|
1610 XFASTINT (mblk->markers[i].chain) = (int) marker_free_list;
|
|
1611 marker_free_list = &mblk->markers[i];
|
|
1612 num_free++;
|
|
1613 }
|
|
1614 else
|
|
1615 {
|
|
1616 num_used++;
|
|
1617 XUNMARK (mblk->markers[i].chain);
|
|
1618 }
|
|
1619 lim = MARKER_BLOCK_SIZE;
|
|
1620 }
|
|
1621
|
|
1622 total_markers = num_used;
|
|
1623 total_free_markers = num_free;
|
|
1624 }
|
|
1625
|
|
1626 /* Free all unmarked buffers */
|
|
1627 {
|
|
1628 register struct buffer *buffer = all_buffers, *prev = 0, *next;
|
|
1629
|
|
1630 while (buffer)
|
|
1631 if (!XMARKBIT (buffer->name))
|
|
1632 {
|
|
1633 if (prev)
|
|
1634 prev->next = buffer->next;
|
|
1635 else
|
|
1636 all_buffers = buffer->next;
|
|
1637 next = buffer->next;
|
|
1638 free (buffer);
|
|
1639 buffer = next;
|
|
1640 }
|
|
1641 else
|
|
1642 {
|
|
1643 XUNMARK (buffer->name);
|
|
1644
|
|
1645 #if 0
|
|
1646 /* Each `struct Lisp_String *' was turned into a Lisp_Object
|
|
1647 for purposes of marking and relocation.
|
|
1648 Turn them back into C pointers now. */
|
|
1649 buffer->upcase_table
|
|
1650 = XSTRING (*(Lisp_Object *)&buffer->upcase_table);
|
|
1651 buffer->downcase_table
|
|
1652 = XSTRING (*(Lisp_Object *)&buffer->downcase_table);
|
|
1653 buffer->sort_table
|
|
1654 = XSTRING (*(Lisp_Object *)&buffer->sort_table);
|
|
1655 buffer->folding_sort_table
|
|
1656 = XSTRING (*(Lisp_Object *)&buffer->folding_sort_table);
|
|
1657 #endif
|
|
1658
|
|
1659 prev = buffer, buffer = buffer->next;
|
|
1660 }
|
|
1661 }
|
|
1662
|
|
1663 #endif /* standalone */
|
|
1664
|
|
1665 /* Free all unmarked vectors */
|
|
1666 {
|
|
1667 register struct Lisp_Vector *vector = all_vectors, *prev = 0, *next;
|
|
1668 total_vector_size = 0;
|
|
1669
|
|
1670 while (vector)
|
|
1671 if (!(vector->size & ARRAY_MARK_FLAG))
|
|
1672 {
|
|
1673 if (prev)
|
|
1674 prev->next = vector->next;
|
|
1675 else
|
|
1676 all_vectors = vector->next;
|
|
1677 next = vector->next;
|
|
1678 free (vector);
|
|
1679 vector = next;
|
|
1680 }
|
|
1681 else
|
|
1682 {
|
|
1683 vector->size &= ~ARRAY_MARK_FLAG;
|
|
1684 total_vector_size += vector->size;
|
|
1685 prev = vector, vector = vector->next;
|
|
1686 }
|
|
1687 }
|
|
1688
|
|
1689 /* Free all "large strings" not marked with ARRAY_MARK_FLAG. */
|
|
1690 {
|
|
1691 register struct string_block *sb = large_string_blocks, *prev = 0, *next;
|
|
1692
|
|
1693 while (sb)
|
|
1694 if (!(((struct Lisp_String *)(&sb->chars[0]))->size & ARRAY_MARK_FLAG))
|
|
1695 {
|
|
1696 if (prev)
|
|
1697 prev->next = sb->next;
|
|
1698 else
|
|
1699 large_string_blocks = sb->next;
|
|
1700 next = sb->next;
|
|
1701 free (sb);
|
|
1702 sb = next;
|
|
1703 }
|
|
1704 else
|
|
1705 {
|
|
1706 ((struct Lisp_String *)(&sb->chars[0]))->size
|
|
1707 &= ~ARRAY_MARK_FLAG & ~MARKBIT;
|
|
1708 total_string_size += ((struct Lisp_String *)(&sb->chars[0]))->size;
|
|
1709 prev = sb, sb = sb->next;
|
|
1710 }
|
|
1711 }
|
|
1712 }
|
|
1713
|
|
1714 /* Compactify strings, relocate references to them, and
|
|
1715 free any string blocks that become empty. */
|
|
1716
|
|
1717 static void
|
|
1718 compact_strings ()
|
|
1719 {
|
|
1720 /* String block of old strings we are scanning. */
|
|
1721 register struct string_block *from_sb;
|
|
1722 /* A preceding string block (or maybe the same one)
|
|
1723 where we are copying the still-live strings to. */
|
|
1724 register struct string_block *to_sb;
|
|
1725 int pos;
|
|
1726 int to_pos;
|
|
1727
|
|
1728 to_sb = first_string_block;
|
|
1729 to_pos = 0;
|
|
1730
|
|
1731 /* Scan each existing string block sequentially, string by string. */
|
|
1732 for (from_sb = first_string_block; from_sb; from_sb = from_sb->next)
|
|
1733 {
|
|
1734 pos = 0;
|
|
1735 /* POS is the index of the next string in the block. */
|
|
1736 while (pos < from_sb->pos)
|
|
1737 {
|
|
1738 register struct Lisp_String *nextstr
|
|
1739 = (struct Lisp_String *) &from_sb->chars[pos];
|
|
1740
|
|
1741 register struct Lisp_String *newaddr;
|
|
1742 register int size = nextstr->size;
|
|
1743
|
|
1744 /* NEXTSTR is the old address of the next string.
|
|
1745 Just skip it if it isn't marked. */
|
|
1746 if ((unsigned) size > STRING_BLOCK_SIZE)
|
|
1747 {
|
|
1748 /* It is marked, so its size field is really a chain of refs.
|
|
1749 Find the end of the chain, where the actual size lives. */
|
|
1750 while ((unsigned) size > STRING_BLOCK_SIZE)
|
|
1751 {
|
|
1752 if (size & 1) size ^= MARKBIT | 1;
|
|
1753 size = *(int *)size & ~MARKBIT;
|
|
1754 }
|
|
1755
|
|
1756 total_string_size += size;
|
|
1757
|
|
1758 /* If it won't fit in TO_SB, close it out,
|
|
1759 and move to the next sb. Keep doing so until
|
|
1760 TO_SB reaches a large enough, empty enough string block.
|
|
1761 We know that TO_SB cannot advance past FROM_SB here
|
|
1762 since FROM_SB is large enough to contain this string.
|
|
1763 Any string blocks skipped here
|
|
1764 will be patched out and freed later. */
|
|
1765 while (to_pos + STRING_FULLSIZE (size)
|
|
1766 > max (to_sb->pos, STRING_BLOCK_SIZE))
|
|
1767 {
|
|
1768 to_sb->pos = to_pos;
|
|
1769 to_sb = to_sb->next;
|
|
1770 to_pos = 0;
|
|
1771 }
|
|
1772 /* Compute new address of this string
|
|
1773 and update TO_POS for the space being used. */
|
|
1774 newaddr = (struct Lisp_String *) &to_sb->chars[to_pos];
|
|
1775 to_pos += STRING_FULLSIZE (size);
|
|
1776
|
|
1777 /* Copy the string itself to the new place. */
|
|
1778 if (nextstr != newaddr)
|
|
1779 bcopy (nextstr, newaddr, size + 1 + sizeof (int));
|
|
1780
|
|
1781 /* Go through NEXTSTR's chain of references
|
|
1782 and make each slot in the chain point to
|
|
1783 the new address of this string. */
|
|
1784 size = newaddr->size;
|
|
1785 while ((unsigned) size > STRING_BLOCK_SIZE)
|
|
1786 {
|
|
1787 register Lisp_Object *objptr;
|
|
1788 if (size & 1) size ^= MARKBIT | 1;
|
|
1789 objptr = (Lisp_Object *)size;
|
|
1790
|
|
1791 size = XFASTINT (*objptr) & ~MARKBIT;
|
|
1792 if (XMARKBIT (*objptr))
|
|
1793 {
|
|
1794 XSET (*objptr, Lisp_String, newaddr);
|
|
1795 XMARK (*objptr);
|
|
1796 }
|
|
1797 else
|
|
1798 XSET (*objptr, Lisp_String, newaddr);
|
|
1799 }
|
|
1800 /* Store the actual size in the size field. */
|
|
1801 newaddr->size = size;
|
|
1802 }
|
|
1803 pos += STRING_FULLSIZE (size);
|
|
1804 }
|
|
1805 }
|
|
1806
|
|
1807 /* Close out the last string block still used and free any that follow. */
|
|
1808 to_sb->pos = to_pos;
|
|
1809 current_string_block = to_sb;
|
|
1810
|
|
1811 from_sb = to_sb->next;
|
|
1812 to_sb->next = 0;
|
|
1813 while (from_sb)
|
|
1814 {
|
|
1815 to_sb = from_sb->next;
|
|
1816 free (from_sb);
|
|
1817 from_sb = to_sb;
|
|
1818 }
|
|
1819
|
|
1820 /* Free any empty string blocks further back in the chain.
|
|
1821 This loop will never free first_string_block, but it is very
|
|
1822 unlikely that that one will become empty, so why bother checking? */
|
|
1823
|
|
1824 from_sb = first_string_block;
|
|
1825 while (to_sb = from_sb->next)
|
|
1826 {
|
|
1827 if (to_sb->pos == 0)
|
|
1828 {
|
|
1829 if (from_sb->next = to_sb->next)
|
|
1830 from_sb->next->prev = from_sb;
|
|
1831 free (to_sb);
|
|
1832 }
|
|
1833 else
|
|
1834 from_sb = to_sb;
|
|
1835 }
|
|
1836 }
|
|
1837
|
|
1838 /* Initialization */
|
|
1839
|
|
1840 init_alloc_once ()
|
|
1841 {
|
|
1842 /* Used to do Vpurify_flag = Qt here, but Qt isn't set up yet! */
|
|
1843 pureptr = 0;
|
356
|
1844 #ifdef HAVE_SHM
|
|
1845 pure_size = PURESIZE;
|
|
1846 #endif
|
300
|
1847 all_vectors = 0;
|
|
1848 ignore_warnings = 1;
|
|
1849 init_strings ();
|
|
1850 init_cons ();
|
|
1851 init_symbol ();
|
|
1852 init_marker ();
|
|
1853 #ifdef LISP_FLOAT_TYPE
|
|
1854 init_float ();
|
|
1855 #endif /* LISP_FLOAT_TYPE */
|
|
1856 ignore_warnings = 0;
|
|
1857 gcprolist = 0;
|
|
1858 staticidx = 0;
|
|
1859 consing_since_gc = 0;
|
|
1860 gc_cons_threshold = 100000;
|
|
1861 #ifdef VIRT_ADDR_VARIES
|
|
1862 malloc_sbrk_unused = 1<<22; /* A large number */
|
|
1863 malloc_sbrk_used = 100000; /* as reasonable as any number */
|
|
1864 #endif /* VIRT_ADDR_VARIES */
|
|
1865 }
|
|
1866
|
|
1867 init_alloc ()
|
|
1868 {
|
|
1869 gcprolist = 0;
|
|
1870 }
|
|
1871
|
|
1872 void
|
|
1873 syms_of_alloc ()
|
|
1874 {
|
|
1875 DEFVAR_INT ("gc-cons-threshold", &gc_cons_threshold,
|
|
1876 "*Number of bytes of consing between garbage collections.\n\
|
|
1877 Garbage collection can happen automatically once this many bytes have been\n\
|
|
1878 allocated since the last garbage collection. All data types count.\n\n\
|
|
1879 Garbage collection happens automatically only when `eval' is called.\n\n\
|
|
1880 By binding this temporarily to a large number, you can effectively\n\
|
|
1881 prevent garbage collection during a part of the program.");
|
|
1882
|
|
1883 DEFVAR_INT ("pure-bytes-used", &pureptr,
|
|
1884 "Number of bytes of sharable Lisp data allocated so far.");
|
|
1885
|
|
1886 #if 0
|
|
1887 DEFVAR_INT ("data-bytes-used", &malloc_sbrk_used,
|
|
1888 "Number of bytes of unshared memory allocated in this session.");
|
|
1889
|
|
1890 DEFVAR_INT ("data-bytes-free", &malloc_sbrk_unused,
|
|
1891 "Number of bytes of unshared memory remaining available in this session.");
|
|
1892 #endif
|
|
1893
|
|
1894 DEFVAR_LISP ("purify-flag", &Vpurify_flag,
|
|
1895 "Non-nil means loading Lisp code in order to dump an executable.\n\
|
|
1896 This means that certain objects should be allocated in shared (pure) space.");
|
|
1897
|
|
1898 DEFVAR_INT ("undo-threshold", &undo_threshold,
|
|
1899 "Keep no more undo information once it exceeds this size.\n\
|
|
1900 This threshold is applied when garbage collection happens.\n\
|
|
1901 The size is counted as the number of bytes occupied,\n\
|
|
1902 which includes both saved text and other data.");
|
|
1903 undo_threshold = 20000;
|
|
1904
|
|
1905 DEFVAR_INT ("undo-high-threshold", &undo_high_threshold,
|
|
1906 "Don't keep more than this much size of undo information.\n\
|
|
1907 A command which pushes past this size is itself forgotten.\n\
|
|
1908 This threshold is applied when garbage collection happens.\n\
|
|
1909 The size is counted as the number of bytes occupied,\n\
|
|
1910 which includes both saved text and other data.");
|
|
1911 undo_high_threshold = 30000;
|
|
1912
|
|
1913 defsubr (&Scons);
|
|
1914 defsubr (&Slist);
|
|
1915 defsubr (&Svector);
|
|
1916 defsubr (&Smake_byte_code);
|
|
1917 defsubr (&Smake_list);
|
|
1918 defsubr (&Smake_vector);
|
|
1919 defsubr (&Smake_string);
|
|
1920 defsubr (&Smake_rope);
|
|
1921 defsubr (&Srope_elt);
|
|
1922 defsubr (&Smake_symbol);
|
|
1923 defsubr (&Smake_marker);
|
|
1924 defsubr (&Spurecopy);
|
|
1925 defsubr (&Sgarbage_collect);
|
|
1926 }
|