* gtkutil.c (xg_get_file_with_chooser): Handle local files only.
[bpt/emacs.git] / src / alloc.c
... / ...
CommitLineData
1/* Storage allocation and gc for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 1986, 1988, 1993, 1994, 1995, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
21
22#include <config.h>
23#include <stdio.h>
24#include <limits.h> /* For CHAR_BIT. */
25
26#ifdef ALLOC_DEBUG
27#undef INLINE
28#endif
29
30/* Note that this declares bzero on OSF/1. How dumb. */
31
32#include <signal.h>
33
34/* This file is part of the core Lisp implementation, and thus must
35 deal with the real data structures. If the Lisp implementation is
36 replaced, this file likely will not be used. */
37
38#undef HIDE_LISP_IMPLEMENTATION
39#include "lisp.h"
40#include "process.h"
41#include "intervals.h"
42#include "puresize.h"
43#include "buffer.h"
44#include "window.h"
45#include "keyboard.h"
46#include "frame.h"
47#include "blockinput.h"
48#include "charset.h"
49#include "syssignal.h"
50#include <setjmp.h>
51
52/* GC_MALLOC_CHECK defined means perform validity checks of malloc'd
53 memory. Can do this only if using gmalloc.c. */
54
55#if defined SYSTEM_MALLOC || defined DOUG_LEA_MALLOC
56#undef GC_MALLOC_CHECK
57#endif
58
59#ifdef HAVE_UNISTD_H
60#include <unistd.h>
61#else
62extern POINTER_TYPE *sbrk ();
63#endif
64
65#ifdef DOUG_LEA_MALLOC
66
67#include <malloc.h>
68/* malloc.h #defines this as size_t, at least in glibc2. */
69#ifndef __malloc_size_t
70#define __malloc_size_t int
71#endif
72
73/* Specify maximum number of areas to mmap. It would be nice to use a
74 value that explicitly means "no limit". */
75
76#define MMAP_MAX_AREAS 100000000
77
78#else /* not DOUG_LEA_MALLOC */
79
80/* The following come from gmalloc.c. */
81
82#define __malloc_size_t size_t
83extern __malloc_size_t _bytes_used;
84extern __malloc_size_t __malloc_extra_blocks;
85
86#endif /* not DOUG_LEA_MALLOC */
87
88/* Value of _bytes_used, when spare_memory was freed. */
89
90static __malloc_size_t bytes_used_when_full;
91
92/* Mark, unmark, query mark bit of a Lisp string. S must be a pointer
93 to a struct Lisp_String. */
94
95#define MARK_STRING(S) ((S)->size |= ARRAY_MARK_FLAG)
96#define UNMARK_STRING(S) ((S)->size &= ~ARRAY_MARK_FLAG)
97#define STRING_MARKED_P(S) ((S)->size & ARRAY_MARK_FLAG)
98
99#define VECTOR_MARK(V) ((V)->size |= ARRAY_MARK_FLAG)
100#define VECTOR_UNMARK(V) ((V)->size &= ~ARRAY_MARK_FLAG)
101#define VECTOR_MARKED_P(V) ((V)->size & ARRAY_MARK_FLAG)
102
103/* Value is the number of bytes/chars of S, a pointer to a struct
104 Lisp_String. This must be used instead of STRING_BYTES (S) or
105 S->size during GC, because S->size contains the mark bit for
106 strings. */
107
108#define GC_STRING_BYTES(S) (STRING_BYTES (S))
109#define GC_STRING_CHARS(S) ((S)->size & ~ARRAY_MARK_FLAG)
110
111/* Number of bytes of consing done since the last gc. */
112
113int consing_since_gc;
114
115/* Count the amount of consing of various sorts of space. */
116
117EMACS_INT cons_cells_consed;
118EMACS_INT floats_consed;
119EMACS_INT vector_cells_consed;
120EMACS_INT symbols_consed;
121EMACS_INT string_chars_consed;
122EMACS_INT misc_objects_consed;
123EMACS_INT intervals_consed;
124EMACS_INT strings_consed;
125
126/* Number of bytes of consing since GC before another GC should be done. */
127
128EMACS_INT gc_cons_threshold;
129
130/* Nonzero during GC. */
131
132int gc_in_progress;
133
134/* Nonzero means abort if try to GC.
135 This is for code which is written on the assumption that
136 no GC will happen, so as to verify that assumption. */
137
138int abort_on_gc;
139
140/* Nonzero means display messages at beginning and end of GC. */
141
142int garbage_collection_messages;
143
144#ifndef VIRT_ADDR_VARIES
145extern
146#endif /* VIRT_ADDR_VARIES */
147int malloc_sbrk_used;
148
149#ifndef VIRT_ADDR_VARIES
150extern
151#endif /* VIRT_ADDR_VARIES */
152int malloc_sbrk_unused;
153
154/* Two limits controlling how much undo information to keep. */
155
156EMACS_INT undo_limit;
157EMACS_INT undo_strong_limit;
158EMACS_INT undo_outer_limit;
159
160/* Number of live and free conses etc. */
161
162static int total_conses, total_markers, total_symbols, total_vector_size;
163static int total_free_conses, total_free_markers, total_free_symbols;
164static int total_free_floats, total_floats;
165
166/* Points to memory space allocated as "spare", to be freed if we run
167 out of memory. */
168
169static char *spare_memory;
170
171/* Amount of spare memory to keep in reserve. */
172
173#define SPARE_MEMORY (1 << 14)
174
175/* Number of extra blocks malloc should get when it needs more core. */
176
177static int malloc_hysteresis;
178
179/* Non-nil means defun should do purecopy on the function definition. */
180
181Lisp_Object Vpurify_flag;
182
183/* Non-nil means we are handling a memory-full error. */
184
185Lisp_Object Vmemory_full;
186
187#ifndef HAVE_SHM
188
189/* Initialize it to a nonzero value to force it into data space
190 (rather than bss space). That way unexec will remap it into text
191 space (pure), on some systems. We have not implemented the
192 remapping on more recent systems because this is less important
193 nowadays than in the days of small memories and timesharing. */
194
195EMACS_INT pure[PURESIZE / sizeof (EMACS_INT)] = {1,};
196#define PUREBEG (char *) pure
197
198#else /* HAVE_SHM */
199
200#define pure PURE_SEG_BITS /* Use shared memory segment */
201#define PUREBEG (char *)PURE_SEG_BITS
202
203#endif /* HAVE_SHM */
204
205/* Pointer to the pure area, and its size. */
206
207static char *purebeg;
208static size_t pure_size;
209
210/* Number of bytes of pure storage used before pure storage overflowed.
211 If this is non-zero, this implies that an overflow occurred. */
212
213static size_t pure_bytes_used_before_overflow;
214
215/* Value is non-zero if P points into pure space. */
216
217#define PURE_POINTER_P(P) \
218 (((PNTR_COMPARISON_TYPE) (P) \
219 < (PNTR_COMPARISON_TYPE) ((char *) purebeg + pure_size)) \
220 && ((PNTR_COMPARISON_TYPE) (P) \
221 >= (PNTR_COMPARISON_TYPE) purebeg))
222
223/* Index in pure at which next pure object will be allocated.. */
224
225EMACS_INT pure_bytes_used;
226
227/* If nonzero, this is a warning delivered by malloc and not yet
228 displayed. */
229
230char *pending_malloc_warning;
231
232/* Pre-computed signal argument for use when memory is exhausted. */
233
234Lisp_Object Vmemory_signal_data;
235
236/* Maximum amount of C stack to save when a GC happens. */
237
238#ifndef MAX_SAVE_STACK
239#define MAX_SAVE_STACK 16000
240#endif
241
242/* Buffer in which we save a copy of the C stack at each GC. */
243
244char *stack_copy;
245int stack_copy_size;
246
247/* Non-zero means ignore malloc warnings. Set during initialization.
248 Currently not used. */
249
250int ignore_warnings;
251
252Lisp_Object Qgc_cons_threshold, Qchar_table_extra_slots;
253
254/* Hook run after GC has finished. */
255
256Lisp_Object Vpost_gc_hook, Qpost_gc_hook;
257
258Lisp_Object Vgc_elapsed; /* accumulated elapsed time in GC */
259EMACS_INT gcs_done; /* accumulated GCs */
260
261static void mark_buffer P_ ((Lisp_Object));
262extern void mark_kboards P_ ((void));
263extern void mark_backtrace P_ ((void));
264static void gc_sweep P_ ((void));
265static void mark_glyph_matrix P_ ((struct glyph_matrix *));
266static void mark_face_cache P_ ((struct face_cache *));
267
268#ifdef HAVE_WINDOW_SYSTEM
269static void mark_image P_ ((struct image *));
270static void mark_image_cache P_ ((struct frame *));
271#endif /* HAVE_WINDOW_SYSTEM */
272
273static struct Lisp_String *allocate_string P_ ((void));
274static void compact_small_strings P_ ((void));
275static void free_large_strings P_ ((void));
276static void sweep_strings P_ ((void));
277
278extern int message_enable_multibyte;
279
280/* When scanning the C stack for live Lisp objects, Emacs keeps track
281 of what memory allocated via lisp_malloc is intended for what
282 purpose. This enumeration specifies the type of memory. */
283
284enum mem_type
285{
286 MEM_TYPE_NON_LISP,
287 MEM_TYPE_BUFFER,
288 MEM_TYPE_CONS,
289 MEM_TYPE_STRING,
290 MEM_TYPE_MISC,
291 MEM_TYPE_SYMBOL,
292 MEM_TYPE_FLOAT,
293 /* Keep the following vector-like types together, with
294 MEM_TYPE_WINDOW being the last, and MEM_TYPE_VECTOR the
295 first. Or change the code of live_vector_p, for instance. */
296 MEM_TYPE_VECTOR,
297 MEM_TYPE_PROCESS,
298 MEM_TYPE_HASH_TABLE,
299 MEM_TYPE_FRAME,
300 MEM_TYPE_WINDOW
301};
302
303#if GC_MARK_STACK || defined GC_MALLOC_CHECK
304
305#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
306#include <stdio.h> /* For fprintf. */
307#endif
308
309/* A unique object in pure space used to make some Lisp objects
310 on free lists recognizable in O(1). */
311
312Lisp_Object Vdead;
313
314#ifdef GC_MALLOC_CHECK
315
316enum mem_type allocated_mem_type;
317int dont_register_blocks;
318
319#endif /* GC_MALLOC_CHECK */
320
321/* A node in the red-black tree describing allocated memory containing
322 Lisp data. Each such block is recorded with its start and end
323 address when it is allocated, and removed from the tree when it
324 is freed.
325
326 A red-black tree is a balanced binary tree with the following
327 properties:
328
329 1. Every node is either red or black.
330 2. Every leaf is black.
331 3. If a node is red, then both of its children are black.
332 4. Every simple path from a node to a descendant leaf contains
333 the same number of black nodes.
334 5. The root is always black.
335
336 When nodes are inserted into the tree, or deleted from the tree,
337 the tree is "fixed" so that these properties are always true.
338
339 A red-black tree with N internal nodes has height at most 2
340 log(N+1). Searches, insertions and deletions are done in O(log N).
341 Please see a text book about data structures for a detailed
342 description of red-black trees. Any book worth its salt should
343 describe them. */
344
345struct mem_node
346{
347 /* Children of this node. These pointers are never NULL. When there
348 is no child, the value is MEM_NIL, which points to a dummy node. */
349 struct mem_node *left, *right;
350
351 /* The parent of this node. In the root node, this is NULL. */
352 struct mem_node *parent;
353
354 /* Start and end of allocated region. */
355 void *start, *end;
356
357 /* Node color. */
358 enum {MEM_BLACK, MEM_RED} color;
359
360 /* Memory type. */
361 enum mem_type type;
362};
363
364/* Base address of stack. Set in main. */
365
366Lisp_Object *stack_base;
367
368/* Root of the tree describing allocated Lisp memory. */
369
370static struct mem_node *mem_root;
371
372/* Lowest and highest known address in the heap. */
373
374static void *min_heap_address, *max_heap_address;
375
376/* Sentinel node of the tree. */
377
378static struct mem_node mem_z;
379#define MEM_NIL &mem_z
380
381static POINTER_TYPE *lisp_malloc P_ ((size_t, enum mem_type));
382static struct Lisp_Vector *allocate_vectorlike P_ ((EMACS_INT, enum mem_type));
383static void lisp_free P_ ((POINTER_TYPE *));
384static void mark_stack P_ ((void));
385static int live_vector_p P_ ((struct mem_node *, void *));
386static int live_buffer_p P_ ((struct mem_node *, void *));
387static int live_string_p P_ ((struct mem_node *, void *));
388static int live_cons_p P_ ((struct mem_node *, void *));
389static int live_symbol_p P_ ((struct mem_node *, void *));
390static int live_float_p P_ ((struct mem_node *, void *));
391static int live_misc_p P_ ((struct mem_node *, void *));
392static void mark_maybe_object P_ ((Lisp_Object));
393static void mark_memory P_ ((void *, void *));
394static void mem_init P_ ((void));
395static struct mem_node *mem_insert P_ ((void *, void *, enum mem_type));
396static void mem_insert_fixup P_ ((struct mem_node *));
397static void mem_rotate_left P_ ((struct mem_node *));
398static void mem_rotate_right P_ ((struct mem_node *));
399static void mem_delete P_ ((struct mem_node *));
400static void mem_delete_fixup P_ ((struct mem_node *));
401static INLINE struct mem_node *mem_find P_ ((void *));
402
403#if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
404static void check_gcpros P_ ((void));
405#endif
406
407#endif /* GC_MARK_STACK || GC_MALLOC_CHECK */
408
409/* Recording what needs to be marked for gc. */
410
411struct gcpro *gcprolist;
412
413/* Addresses of staticpro'd variables. Initialize it to a nonzero
414 value; otherwise some compilers put it into BSS. */
415
416#define NSTATICS 1280
417Lisp_Object *staticvec[NSTATICS] = {&Vpurify_flag};
418
419/* Index of next unused slot in staticvec. */
420
421int staticidx = 0;
422
423static POINTER_TYPE *pure_alloc P_ ((size_t, int));
424
425
426/* Value is SZ rounded up to the next multiple of ALIGNMENT.
427 ALIGNMENT must be a power of 2. */
428
429#define ALIGN(ptr, ALIGNMENT) \
430 ((POINTER_TYPE *) ((((EMACS_UINT)(ptr)) + (ALIGNMENT) - 1) \
431 & ~((ALIGNMENT) - 1)))
432
433
434\f
435/************************************************************************
436 Malloc
437 ************************************************************************/
438
439/* Function malloc calls this if it finds we are near exhausting storage. */
440
441void
442malloc_warning (str)
443 char *str;
444{
445 pending_malloc_warning = str;
446}
447
448
449/* Display an already-pending malloc warning. */
450
451void
452display_malloc_warning ()
453{
454 call3 (intern ("display-warning"),
455 intern ("alloc"),
456 build_string (pending_malloc_warning),
457 intern ("emergency"));
458 pending_malloc_warning = 0;
459}
460
461
462#ifdef DOUG_LEA_MALLOC
463# define BYTES_USED (mallinfo ().arena)
464#else
465# define BYTES_USED _bytes_used
466#endif
467
468
469/* Called if malloc returns zero. */
470
471void
472memory_full ()
473{
474 Vmemory_full = Qt;
475
476#ifndef SYSTEM_MALLOC
477 bytes_used_when_full = BYTES_USED;
478#endif
479
480 /* The first time we get here, free the spare memory. */
481 if (spare_memory)
482 {
483 free (spare_memory);
484 spare_memory = 0;
485 }
486
487 /* This used to call error, but if we've run out of memory, we could
488 get infinite recursion trying to build the string. */
489 while (1)
490 Fsignal (Qnil, Vmemory_signal_data);
491}
492
493
494/* Called if we can't allocate relocatable space for a buffer. */
495
496void
497buffer_memory_full ()
498{
499 /* If buffers use the relocating allocator, no need to free
500 spare_memory, because we may have plenty of malloc space left
501 that we could get, and if we don't, the malloc that fails will
502 itself cause spare_memory to be freed. If buffers don't use the
503 relocating allocator, treat this like any other failing
504 malloc. */
505
506#ifndef REL_ALLOC
507 memory_full ();
508#endif
509
510 Vmemory_full = Qt;
511
512 /* This used to call error, but if we've run out of memory, we could
513 get infinite recursion trying to build the string. */
514 while (1)
515 Fsignal (Qnil, Vmemory_signal_data);
516}
517
518
519#ifdef XMALLOC_OVERRUN_CHECK
520
521/* Check for overrun in malloc'ed buffers by wrapping a 16 byte header
522 and a 16 byte trailer around each block.
523
524 The header consists of 12 fixed bytes + a 4 byte integer contaning the
525 original block size, while the trailer consists of 16 fixed bytes.
526
527 The header is used to detect whether this block has been allocated
528 through these functions -- as it seems that some low-level libc
529 functions may bypass the malloc hooks.
530*/
531
532
533#define XMALLOC_OVERRUN_CHECK_SIZE 16
534
535static char xmalloc_overrun_check_header[XMALLOC_OVERRUN_CHECK_SIZE-4] =
536 { 0x9a, 0x9b, 0xae, 0xaf,
537 0xbf, 0xbe, 0xce, 0xcf,
538 0xea, 0xeb, 0xec, 0xed };
539
540static char xmalloc_overrun_check_trailer[XMALLOC_OVERRUN_CHECK_SIZE] =
541 { 0xaa, 0xab, 0xac, 0xad,
542 0xba, 0xbb, 0xbc, 0xbd,
543 0xca, 0xcb, 0xcc, 0xcd,
544 0xda, 0xdb, 0xdc, 0xdd };
545
546/* Macros to insert and extract the block size in the header. */
547
548#define XMALLOC_PUT_SIZE(ptr, size) \
549 (ptr[-1] = (size & 0xff), \
550 ptr[-2] = ((size >> 8) & 0xff), \
551 ptr[-3] = ((size >> 16) & 0xff), \
552 ptr[-4] = ((size >> 24) & 0xff))
553
554#define XMALLOC_GET_SIZE(ptr) \
555 (size_t)((unsigned)(ptr[-1]) | \
556 ((unsigned)(ptr[-2]) << 8) | \
557 ((unsigned)(ptr[-3]) << 16) | \
558 ((unsigned)(ptr[-4]) << 24))
559
560
561/* Like malloc, but wraps allocated block with header and trailer. */
562
563POINTER_TYPE *
564overrun_check_malloc (size)
565 size_t size;
566{
567 register unsigned char *val;
568
569 val = (unsigned char *) malloc (size + XMALLOC_OVERRUN_CHECK_SIZE*2);
570 if (val)
571 {
572 bcopy (xmalloc_overrun_check_header, val, XMALLOC_OVERRUN_CHECK_SIZE - 4);
573 val += XMALLOC_OVERRUN_CHECK_SIZE;
574 XMALLOC_PUT_SIZE(val, size);
575 bcopy (xmalloc_overrun_check_trailer, val + size, XMALLOC_OVERRUN_CHECK_SIZE);
576 }
577 return (POINTER_TYPE *)val;
578}
579
580
581/* Like realloc, but checks old block for overrun, and wraps new block
582 with header and trailer. */
583
584POINTER_TYPE *
585overrun_check_realloc (block, size)
586 POINTER_TYPE *block;
587 size_t size;
588{
589 register unsigned char *val = (unsigned char *)block;
590
591 if (val
592 && bcmp (xmalloc_overrun_check_header,
593 val - XMALLOC_OVERRUN_CHECK_SIZE,
594 XMALLOC_OVERRUN_CHECK_SIZE - 4) == 0)
595 {
596 size_t osize = XMALLOC_GET_SIZE (val);
597 if (bcmp (xmalloc_overrun_check_trailer,
598 val + osize,
599 XMALLOC_OVERRUN_CHECK_SIZE))
600 abort ();
601 bzero (val + osize, XMALLOC_OVERRUN_CHECK_SIZE);
602 val -= XMALLOC_OVERRUN_CHECK_SIZE;
603 bzero (val, XMALLOC_OVERRUN_CHECK_SIZE);
604 }
605
606 val = (unsigned char *) realloc ((POINTER_TYPE *)val, size + XMALLOC_OVERRUN_CHECK_SIZE*2);
607
608 if (val)
609 {
610 bcopy (xmalloc_overrun_check_header, val, XMALLOC_OVERRUN_CHECK_SIZE - 4);
611 val += XMALLOC_OVERRUN_CHECK_SIZE;
612 XMALLOC_PUT_SIZE(val, size);
613 bcopy (xmalloc_overrun_check_trailer, val + size, XMALLOC_OVERRUN_CHECK_SIZE);
614 }
615 return (POINTER_TYPE *)val;
616}
617
618/* Like free, but checks block for overrun. */
619
620void
621overrun_check_free (block)
622 POINTER_TYPE *block;
623{
624 unsigned char *val = (unsigned char *)block;
625
626 if (val
627 && bcmp (xmalloc_overrun_check_header,
628 val - XMALLOC_OVERRUN_CHECK_SIZE,
629 XMALLOC_OVERRUN_CHECK_SIZE - 4) == 0)
630 {
631 size_t osize = XMALLOC_GET_SIZE (val);
632 if (bcmp (xmalloc_overrun_check_trailer,
633 val + osize,
634 XMALLOC_OVERRUN_CHECK_SIZE))
635 abort ();
636 bzero (val + osize, XMALLOC_OVERRUN_CHECK_SIZE);
637 val -= XMALLOC_OVERRUN_CHECK_SIZE;
638 bzero (val, XMALLOC_OVERRUN_CHECK_SIZE);
639 }
640
641 free (val);
642}
643
644#undef malloc
645#undef realloc
646#undef free
647#define malloc overrun_check_malloc
648#define realloc overrun_check_realloc
649#define free overrun_check_free
650#endif
651
652
653/* Like malloc but check for no memory and block interrupt input.. */
654
655POINTER_TYPE *
656xmalloc (size)
657 size_t size;
658{
659 register POINTER_TYPE *val;
660
661 BLOCK_INPUT;
662 val = (POINTER_TYPE *) malloc (size);
663 UNBLOCK_INPUT;
664
665 if (!val && size)
666 memory_full ();
667 return val;
668}
669
670
671/* Like realloc but check for no memory and block interrupt input.. */
672
673POINTER_TYPE *
674xrealloc (block, size)
675 POINTER_TYPE *block;
676 size_t size;
677{
678 register POINTER_TYPE *val;
679
680 BLOCK_INPUT;
681 /* We must call malloc explicitly when BLOCK is 0, since some
682 reallocs don't do this. */
683 if (! block)
684 val = (POINTER_TYPE *) malloc (size);
685 else
686 val = (POINTER_TYPE *) realloc (block, size);
687 UNBLOCK_INPUT;
688
689 if (!val && size) memory_full ();
690 return val;
691}
692
693
694/* Like free but block interrupt input. */
695
696void
697xfree (block)
698 POINTER_TYPE *block;
699{
700 BLOCK_INPUT;
701 free (block);
702 UNBLOCK_INPUT;
703}
704
705
706/* Like strdup, but uses xmalloc. */
707
708char *
709xstrdup (s)
710 const char *s;
711{
712 size_t len = strlen (s) + 1;
713 char *p = (char *) xmalloc (len);
714 bcopy (s, p, len);
715 return p;
716}
717
718
719/* Unwind for SAFE_ALLOCA */
720
721Lisp_Object
722safe_alloca_unwind (arg)
723 Lisp_Object arg;
724{
725 register struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
726
727 p->dogc = 0;
728 xfree (p->pointer);
729 p->pointer = 0;
730 free_misc (arg);
731 return Qnil;
732}
733
734
735/* Like malloc but used for allocating Lisp data. NBYTES is the
736 number of bytes to allocate, TYPE describes the intended use of the
737 allcated memory block (for strings, for conses, ...). */
738
739#ifndef USE_LSB_TAG
740static void *lisp_malloc_loser;
741#endif
742
743static POINTER_TYPE *
744lisp_malloc (nbytes, type)
745 size_t nbytes;
746 enum mem_type type;
747{
748 register void *val;
749
750 BLOCK_INPUT;
751
752#ifdef GC_MALLOC_CHECK
753 allocated_mem_type = type;
754#endif
755
756 val = (void *) malloc (nbytes);
757
758#ifndef USE_LSB_TAG
759 /* If the memory just allocated cannot be addressed thru a Lisp
760 object's pointer, and it needs to be,
761 that's equivalent to running out of memory. */
762 if (val && type != MEM_TYPE_NON_LISP)
763 {
764 Lisp_Object tem;
765 XSETCONS (tem, (char *) val + nbytes - 1);
766 if ((char *) XCONS (tem) != (char *) val + nbytes - 1)
767 {
768 lisp_malloc_loser = val;
769 free (val);
770 val = 0;
771 }
772 }
773#endif
774
775#if GC_MARK_STACK && !defined GC_MALLOC_CHECK
776 if (val && type != MEM_TYPE_NON_LISP)
777 mem_insert (val, (char *) val + nbytes, type);
778#endif
779
780 UNBLOCK_INPUT;
781 if (!val && nbytes)
782 memory_full ();
783 return val;
784}
785
786/* Free BLOCK. This must be called to free memory allocated with a
787 call to lisp_malloc. */
788
789static void
790lisp_free (block)
791 POINTER_TYPE *block;
792{
793 BLOCK_INPUT;
794 free (block);
795#if GC_MARK_STACK && !defined GC_MALLOC_CHECK
796 mem_delete (mem_find (block));
797#endif
798 UNBLOCK_INPUT;
799}
800
801/* Allocation of aligned blocks of memory to store Lisp data. */
802/* The entry point is lisp_align_malloc which returns blocks of at most */
803/* BLOCK_BYTES and guarantees they are aligned on a BLOCK_ALIGN boundary. */
804
805
806/* BLOCK_ALIGN has to be a power of 2. */
807#define BLOCK_ALIGN (1 << 10)
808
809/* Padding to leave at the end of a malloc'd block. This is to give
810 malloc a chance to minimize the amount of memory wasted to alignment.
811 It should be tuned to the particular malloc library used.
812 On glibc-2.3.2, malloc never tries to align, so a padding of 0 is best.
813 posix_memalign on the other hand would ideally prefer a value of 4
814 because otherwise, there's 1020 bytes wasted between each ablocks.
815 But testing shows that those 1020 will most of the time be efficiently
816 used by malloc to place other objects, so a value of 0 is still preferable
817 unless you have a lot of cons&floats and virtually nothing else. */
818#define BLOCK_PADDING 0
819#define BLOCK_BYTES \
820 (BLOCK_ALIGN - sizeof (struct aligned_block *) - BLOCK_PADDING)
821
822/* Internal data structures and constants. */
823
824#define ABLOCKS_SIZE 16
825
826/* An aligned block of memory. */
827struct ablock
828{
829 union
830 {
831 char payload[BLOCK_BYTES];
832 struct ablock *next_free;
833 } x;
834 /* `abase' is the aligned base of the ablocks. */
835 /* It is overloaded to hold the virtual `busy' field that counts
836 the number of used ablock in the parent ablocks.
837 The first ablock has the `busy' field, the others have the `abase'
838 field. To tell the difference, we assume that pointers will have
839 integer values larger than 2 * ABLOCKS_SIZE. The lowest bit of `busy'
840 is used to tell whether the real base of the parent ablocks is `abase'
841 (if not, the word before the first ablock holds a pointer to the
842 real base). */
843 struct ablocks *abase;
844 /* The padding of all but the last ablock is unused. The padding of
845 the last ablock in an ablocks is not allocated. */
846#if BLOCK_PADDING
847 char padding[BLOCK_PADDING];
848#endif
849};
850
851/* A bunch of consecutive aligned blocks. */
852struct ablocks
853{
854 struct ablock blocks[ABLOCKS_SIZE];
855};
856
857/* Size of the block requested from malloc or memalign. */
858#define ABLOCKS_BYTES (sizeof (struct ablocks) - BLOCK_PADDING)
859
860#define ABLOCK_ABASE(block) \
861 (((unsigned long) (block)->abase) <= (1 + 2 * ABLOCKS_SIZE) \
862 ? (struct ablocks *)(block) \
863 : (block)->abase)
864
865/* Virtual `busy' field. */
866#define ABLOCKS_BUSY(abase) ((abase)->blocks[0].abase)
867
868/* Pointer to the (not necessarily aligned) malloc block. */
869#ifdef HAVE_POSIX_MEMALIGN
870#define ABLOCKS_BASE(abase) (abase)
871#else
872#define ABLOCKS_BASE(abase) \
873 (1 & (long) ABLOCKS_BUSY (abase) ? abase : ((void**)abase)[-1])
874#endif
875
876/* The list of free ablock. */
877static struct ablock *free_ablock;
878
879/* Allocate an aligned block of nbytes.
880 Alignment is on a multiple of BLOCK_ALIGN and `nbytes' has to be
881 smaller or equal to BLOCK_BYTES. */
882static POINTER_TYPE *
883lisp_align_malloc (nbytes, type)
884 size_t nbytes;
885 enum mem_type type;
886{
887 void *base, *val;
888 struct ablocks *abase;
889
890 eassert (nbytes <= BLOCK_BYTES);
891
892 BLOCK_INPUT;
893
894#ifdef GC_MALLOC_CHECK
895 allocated_mem_type = type;
896#endif
897
898 if (!free_ablock)
899 {
900 int i;
901 EMACS_INT aligned; /* int gets warning casting to 64-bit pointer. */
902
903#ifdef DOUG_LEA_MALLOC
904 /* Prevent mmap'ing the chunk. Lisp data may not be mmap'ed
905 because mapped region contents are not preserved in
906 a dumped Emacs. */
907 mallopt (M_MMAP_MAX, 0);
908#endif
909
910#ifdef HAVE_POSIX_MEMALIGN
911 {
912 int err = posix_memalign (&base, BLOCK_ALIGN, ABLOCKS_BYTES);
913 if (err)
914 base = NULL;
915 abase = base;
916 }
917#else
918 base = malloc (ABLOCKS_BYTES);
919 abase = ALIGN (base, BLOCK_ALIGN);
920#endif
921
922 if (base == 0)
923 {
924 UNBLOCK_INPUT;
925 memory_full ();
926 }
927
928 aligned = (base == abase);
929 if (!aligned)
930 ((void**)abase)[-1] = base;
931
932#ifdef DOUG_LEA_MALLOC
933 /* Back to a reasonable maximum of mmap'ed areas. */
934 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS);
935#endif
936
937#ifndef USE_LSB_TAG
938 /* If the memory just allocated cannot be addressed thru a Lisp
939 object's pointer, and it needs to be, that's equivalent to
940 running out of memory. */
941 if (type != MEM_TYPE_NON_LISP)
942 {
943 Lisp_Object tem;
944 char *end = (char *) base + ABLOCKS_BYTES - 1;
945 XSETCONS (tem, end);
946 if ((char *) XCONS (tem) != end)
947 {
948 lisp_malloc_loser = base;
949 free (base);
950 UNBLOCK_INPUT;
951 memory_full ();
952 }
953 }
954#endif
955
956 /* Initialize the blocks and put them on the free list.
957 Is `base' was not properly aligned, we can't use the last block. */
958 for (i = 0; i < (aligned ? ABLOCKS_SIZE : ABLOCKS_SIZE - 1); i++)
959 {
960 abase->blocks[i].abase = abase;
961 abase->blocks[i].x.next_free = free_ablock;
962 free_ablock = &abase->blocks[i];
963 }
964 ABLOCKS_BUSY (abase) = (struct ablocks *) (long) aligned;
965
966 eassert (0 == ((EMACS_UINT)abase) % BLOCK_ALIGN);
967 eassert (ABLOCK_ABASE (&abase->blocks[3]) == abase); /* 3 is arbitrary */
968 eassert (ABLOCK_ABASE (&abase->blocks[0]) == abase);
969 eassert (ABLOCKS_BASE (abase) == base);
970 eassert (aligned == (long) ABLOCKS_BUSY (abase));
971 }
972
973 abase = ABLOCK_ABASE (free_ablock);
974 ABLOCKS_BUSY (abase) = (struct ablocks *) (2 + (long) ABLOCKS_BUSY (abase));
975 val = free_ablock;
976 free_ablock = free_ablock->x.next_free;
977
978#if GC_MARK_STACK && !defined GC_MALLOC_CHECK
979 if (val && type != MEM_TYPE_NON_LISP)
980 mem_insert (val, (char *) val + nbytes, type);
981#endif
982
983 UNBLOCK_INPUT;
984 if (!val && nbytes)
985 memory_full ();
986
987 eassert (0 == ((EMACS_UINT)val) % BLOCK_ALIGN);
988 return val;
989}
990
991static void
992lisp_align_free (block)
993 POINTER_TYPE *block;
994{
995 struct ablock *ablock = block;
996 struct ablocks *abase = ABLOCK_ABASE (ablock);
997
998 BLOCK_INPUT;
999#if GC_MARK_STACK && !defined GC_MALLOC_CHECK
1000 mem_delete (mem_find (block));
1001#endif
1002 /* Put on free list. */
1003 ablock->x.next_free = free_ablock;
1004 free_ablock = ablock;
1005 /* Update busy count. */
1006 ABLOCKS_BUSY (abase) = (struct ablocks *) (-2 + (long) ABLOCKS_BUSY (abase));
1007
1008 if (2 > (long) ABLOCKS_BUSY (abase))
1009 { /* All the blocks are free. */
1010 int i = 0, aligned = (long) ABLOCKS_BUSY (abase);
1011 struct ablock **tem = &free_ablock;
1012 struct ablock *atop = &abase->blocks[aligned ? ABLOCKS_SIZE : ABLOCKS_SIZE - 1];
1013
1014 while (*tem)
1015 {
1016 if (*tem >= (struct ablock *) abase && *tem < atop)
1017 {
1018 i++;
1019 *tem = (*tem)->x.next_free;
1020 }
1021 else
1022 tem = &(*tem)->x.next_free;
1023 }
1024 eassert ((aligned & 1) == aligned);
1025 eassert (i == (aligned ? ABLOCKS_SIZE : ABLOCKS_SIZE - 1));
1026 free (ABLOCKS_BASE (abase));
1027 }
1028 UNBLOCK_INPUT;
1029}
1030
1031/* Return a new buffer structure allocated from the heap with
1032 a call to lisp_malloc. */
1033
1034struct buffer *
1035allocate_buffer ()
1036{
1037 struct buffer *b
1038 = (struct buffer *) lisp_malloc (sizeof (struct buffer),
1039 MEM_TYPE_BUFFER);
1040 return b;
1041}
1042
1043\f
1044/* Arranging to disable input signals while we're in malloc.
1045
1046 This only works with GNU malloc. To help out systems which can't
1047 use GNU malloc, all the calls to malloc, realloc, and free
1048 elsewhere in the code should be inside a BLOCK_INPUT/UNBLOCK_INPUT
1049 pairs; unfortunately, we have no idea what C library functions
1050 might call malloc, so we can't really protect them unless you're
1051 using GNU malloc. Fortunately, most of the major operating systems
1052 can use GNU malloc. */
1053
1054#ifndef SYSTEM_MALLOC
1055#ifndef DOUG_LEA_MALLOC
1056extern void * (*__malloc_hook) P_ ((size_t));
1057extern void * (*__realloc_hook) P_ ((void *, size_t));
1058extern void (*__free_hook) P_ ((void *));
1059/* Else declared in malloc.h, perhaps with an extra arg. */
1060#endif /* DOUG_LEA_MALLOC */
1061static void * (*old_malloc_hook) ();
1062static void * (*old_realloc_hook) ();
1063static void (*old_free_hook) ();
1064
1065/* This function is used as the hook for free to call. */
1066
1067static void
1068emacs_blocked_free (ptr)
1069 void *ptr;
1070{
1071 BLOCK_INPUT;
1072
1073#ifdef GC_MALLOC_CHECK
1074 if (ptr)
1075 {
1076 struct mem_node *m;
1077
1078 m = mem_find (ptr);
1079 if (m == MEM_NIL || m->start != ptr)
1080 {
1081 fprintf (stderr,
1082 "Freeing `%p' which wasn't allocated with malloc\n", ptr);
1083 abort ();
1084 }
1085 else
1086 {
1087 /* fprintf (stderr, "free %p...%p (%p)\n", m->start, m->end, ptr); */
1088 mem_delete (m);
1089 }
1090 }
1091#endif /* GC_MALLOC_CHECK */
1092
1093 __free_hook = old_free_hook;
1094 free (ptr);
1095
1096 /* If we released our reserve (due to running out of memory),
1097 and we have a fair amount free once again,
1098 try to set aside another reserve in case we run out once more. */
1099 if (spare_memory == 0
1100 /* Verify there is enough space that even with the malloc
1101 hysteresis this call won't run out again.
1102 The code here is correct as long as SPARE_MEMORY
1103 is substantially larger than the block size malloc uses. */
1104 && (bytes_used_when_full
1105 > BYTES_USED + max (malloc_hysteresis, 4) * SPARE_MEMORY))
1106 spare_memory = (char *) malloc ((size_t) SPARE_MEMORY);
1107
1108 __free_hook = emacs_blocked_free;
1109 UNBLOCK_INPUT;
1110}
1111
1112
1113/* If we released our reserve (due to running out of memory),
1114 and we have a fair amount free once again,
1115 try to set aside another reserve in case we run out once more.
1116
1117 This is called when a relocatable block is freed in ralloc.c. */
1118
1119void
1120refill_memory_reserve ()
1121{
1122 if (spare_memory == 0)
1123 spare_memory = (char *) malloc ((size_t) SPARE_MEMORY);
1124}
1125
1126
1127/* This function is the malloc hook that Emacs uses. */
1128
1129static void *
1130emacs_blocked_malloc (size)
1131 size_t size;
1132{
1133 void *value;
1134
1135 BLOCK_INPUT;
1136 __malloc_hook = old_malloc_hook;
1137#ifdef DOUG_LEA_MALLOC
1138 mallopt (M_TOP_PAD, malloc_hysteresis * 4096);
1139#else
1140 __malloc_extra_blocks = malloc_hysteresis;
1141#endif
1142
1143 value = (void *) malloc (size);
1144
1145#ifdef GC_MALLOC_CHECK
1146 {
1147 struct mem_node *m = mem_find (value);
1148 if (m != MEM_NIL)
1149 {
1150 fprintf (stderr, "Malloc returned %p which is already in use\n",
1151 value);
1152 fprintf (stderr, "Region in use is %p...%p, %u bytes, type %d\n",
1153 m->start, m->end, (char *) m->end - (char *) m->start,
1154 m->type);
1155 abort ();
1156 }
1157
1158 if (!dont_register_blocks)
1159 {
1160 mem_insert (value, (char *) value + max (1, size), allocated_mem_type);
1161 allocated_mem_type = MEM_TYPE_NON_LISP;
1162 }
1163 }
1164#endif /* GC_MALLOC_CHECK */
1165
1166 __malloc_hook = emacs_blocked_malloc;
1167 UNBLOCK_INPUT;
1168
1169 /* fprintf (stderr, "%p malloc\n", value); */
1170 return value;
1171}
1172
1173
1174/* This function is the realloc hook that Emacs uses. */
1175
1176static void *
1177emacs_blocked_realloc (ptr, size)
1178 void *ptr;
1179 size_t size;
1180{
1181 void *value;
1182
1183 BLOCK_INPUT;
1184 __realloc_hook = old_realloc_hook;
1185
1186#ifdef GC_MALLOC_CHECK
1187 if (ptr)
1188 {
1189 struct mem_node *m = mem_find (ptr);
1190 if (m == MEM_NIL || m->start != ptr)
1191 {
1192 fprintf (stderr,
1193 "Realloc of %p which wasn't allocated with malloc\n",
1194 ptr);
1195 abort ();
1196 }
1197
1198 mem_delete (m);
1199 }
1200
1201 /* fprintf (stderr, "%p -> realloc\n", ptr); */
1202
1203 /* Prevent malloc from registering blocks. */
1204 dont_register_blocks = 1;
1205#endif /* GC_MALLOC_CHECK */
1206
1207 value = (void *) realloc (ptr, size);
1208
1209#ifdef GC_MALLOC_CHECK
1210 dont_register_blocks = 0;
1211
1212 {
1213 struct mem_node *m = mem_find (value);
1214 if (m != MEM_NIL)
1215 {
1216 fprintf (stderr, "Realloc returns memory that is already in use\n");
1217 abort ();
1218 }
1219
1220 /* Can't handle zero size regions in the red-black tree. */
1221 mem_insert (value, (char *) value + max (size, 1), MEM_TYPE_NON_LISP);
1222 }
1223
1224 /* fprintf (stderr, "%p <- realloc\n", value); */
1225#endif /* GC_MALLOC_CHECK */
1226
1227 __realloc_hook = emacs_blocked_realloc;
1228 UNBLOCK_INPUT;
1229
1230 return value;
1231}
1232
1233
1234/* Called from main to set up malloc to use our hooks. */
1235
1236void
1237uninterrupt_malloc ()
1238{
1239 if (__free_hook != emacs_blocked_free)
1240 old_free_hook = __free_hook;
1241 __free_hook = emacs_blocked_free;
1242
1243 if (__malloc_hook != emacs_blocked_malloc)
1244 old_malloc_hook = __malloc_hook;
1245 __malloc_hook = emacs_blocked_malloc;
1246
1247 if (__realloc_hook != emacs_blocked_realloc)
1248 old_realloc_hook = __realloc_hook;
1249 __realloc_hook = emacs_blocked_realloc;
1250}
1251
1252#endif /* not SYSTEM_MALLOC */
1253
1254
1255\f
1256/***********************************************************************
1257 Interval Allocation
1258 ***********************************************************************/
1259
1260/* Number of intervals allocated in an interval_block structure.
1261 The 1020 is 1024 minus malloc overhead. */
1262
1263#define INTERVAL_BLOCK_SIZE \
1264 ((1020 - sizeof (struct interval_block *)) / sizeof (struct interval))
1265
1266/* Intervals are allocated in chunks in form of an interval_block
1267 structure. */
1268
1269struct interval_block
1270{
1271 /* Place `intervals' first, to preserve alignment. */
1272 struct interval intervals[INTERVAL_BLOCK_SIZE];
1273 struct interval_block *next;
1274};
1275
1276/* Current interval block. Its `next' pointer points to older
1277 blocks. */
1278
1279struct interval_block *interval_block;
1280
1281/* Index in interval_block above of the next unused interval
1282 structure. */
1283
1284static int interval_block_index;
1285
1286/* Number of free and live intervals. */
1287
1288static int total_free_intervals, total_intervals;
1289
1290/* List of free intervals. */
1291
1292INTERVAL interval_free_list;
1293
1294/* Total number of interval blocks now in use. */
1295
1296int n_interval_blocks;
1297
1298
1299/* Initialize interval allocation. */
1300
1301static void
1302init_intervals ()
1303{
1304 interval_block = NULL;
1305 interval_block_index = INTERVAL_BLOCK_SIZE;
1306 interval_free_list = 0;
1307 n_interval_blocks = 0;
1308}
1309
1310
1311/* Return a new interval. */
1312
1313INTERVAL
1314make_interval ()
1315{
1316 INTERVAL val;
1317
1318 if (interval_free_list)
1319 {
1320 val = interval_free_list;
1321 interval_free_list = INTERVAL_PARENT (interval_free_list);
1322 }
1323 else
1324 {
1325 if (interval_block_index == INTERVAL_BLOCK_SIZE)
1326 {
1327 register struct interval_block *newi;
1328
1329 newi = (struct interval_block *) lisp_malloc (sizeof *newi,
1330 MEM_TYPE_NON_LISP);
1331
1332 newi->next = interval_block;
1333 interval_block = newi;
1334 interval_block_index = 0;
1335 n_interval_blocks++;
1336 }
1337 val = &interval_block->intervals[interval_block_index++];
1338 }
1339 consing_since_gc += sizeof (struct interval);
1340 intervals_consed++;
1341 RESET_INTERVAL (val);
1342 val->gcmarkbit = 0;
1343 return val;
1344}
1345
1346
1347/* Mark Lisp objects in interval I. */
1348
1349static void
1350mark_interval (i, dummy)
1351 register INTERVAL i;
1352 Lisp_Object dummy;
1353{
1354 eassert (!i->gcmarkbit); /* Intervals are never shared. */
1355 i->gcmarkbit = 1;
1356 mark_object (i->plist);
1357}
1358
1359
1360/* Mark the interval tree rooted in TREE. Don't call this directly;
1361 use the macro MARK_INTERVAL_TREE instead. */
1362
1363static void
1364mark_interval_tree (tree)
1365 register INTERVAL tree;
1366{
1367 /* No need to test if this tree has been marked already; this
1368 function is always called through the MARK_INTERVAL_TREE macro,
1369 which takes care of that. */
1370
1371 traverse_intervals_noorder (tree, mark_interval, Qnil);
1372}
1373
1374
1375/* Mark the interval tree rooted in I. */
1376
1377#define MARK_INTERVAL_TREE(i) \
1378 do { \
1379 if (!NULL_INTERVAL_P (i) && !i->gcmarkbit) \
1380 mark_interval_tree (i); \
1381 } while (0)
1382
1383
1384#define UNMARK_BALANCE_INTERVALS(i) \
1385 do { \
1386 if (! NULL_INTERVAL_P (i)) \
1387 (i) = balance_intervals (i); \
1388 } while (0)
1389
1390\f
1391/* Number support. If NO_UNION_TYPE isn't in effect, we
1392 can't create number objects in macros. */
1393#ifndef make_number
1394Lisp_Object
1395make_number (n)
1396 int n;
1397{
1398 Lisp_Object obj;
1399 obj.s.val = n;
1400 obj.s.type = Lisp_Int;
1401 return obj;
1402}
1403#endif
1404\f
1405/***********************************************************************
1406 String Allocation
1407 ***********************************************************************/
1408
1409/* Lisp_Strings are allocated in string_block structures. When a new
1410 string_block is allocated, all the Lisp_Strings it contains are
1411 added to a free-list string_free_list. When a new Lisp_String is
1412 needed, it is taken from that list. During the sweep phase of GC,
1413 string_blocks that are entirely free are freed, except two which
1414 we keep.
1415
1416 String data is allocated from sblock structures. Strings larger
1417 than LARGE_STRING_BYTES, get their own sblock, data for smaller
1418 strings is sub-allocated out of sblocks of size SBLOCK_SIZE.
1419
1420 Sblocks consist internally of sdata structures, one for each
1421 Lisp_String. The sdata structure points to the Lisp_String it
1422 belongs to. The Lisp_String points back to the `u.data' member of
1423 its sdata structure.
1424
1425 When a Lisp_String is freed during GC, it is put back on
1426 string_free_list, and its `data' member and its sdata's `string'
1427 pointer is set to null. The size of the string is recorded in the
1428 `u.nbytes' member of the sdata. So, sdata structures that are no
1429 longer used, can be easily recognized, and it's easy to compact the
1430 sblocks of small strings which we do in compact_small_strings. */
1431
1432/* Size in bytes of an sblock structure used for small strings. This
1433 is 8192 minus malloc overhead. */
1434
1435#define SBLOCK_SIZE 8188
1436
1437/* Strings larger than this are considered large strings. String data
1438 for large strings is allocated from individual sblocks. */
1439
1440#define LARGE_STRING_BYTES 1024
1441
1442/* Structure describing string memory sub-allocated from an sblock.
1443 This is where the contents of Lisp strings are stored. */
1444
1445struct sdata
1446{
1447 /* Back-pointer to the string this sdata belongs to. If null, this
1448 structure is free, and the NBYTES member of the union below
1449 contains the string's byte size (the same value that STRING_BYTES
1450 would return if STRING were non-null). If non-null, STRING_BYTES
1451 (STRING) is the size of the data, and DATA contains the string's
1452 contents. */
1453 struct Lisp_String *string;
1454
1455#ifdef GC_CHECK_STRING_BYTES
1456
1457 EMACS_INT nbytes;
1458 unsigned char data[1];
1459
1460#define SDATA_NBYTES(S) (S)->nbytes
1461#define SDATA_DATA(S) (S)->data
1462
1463#else /* not GC_CHECK_STRING_BYTES */
1464
1465 union
1466 {
1467 /* When STRING in non-null. */
1468 unsigned char data[1];
1469
1470 /* When STRING is null. */
1471 EMACS_INT nbytes;
1472 } u;
1473
1474
1475#define SDATA_NBYTES(S) (S)->u.nbytes
1476#define SDATA_DATA(S) (S)->u.data
1477
1478#endif /* not GC_CHECK_STRING_BYTES */
1479};
1480
1481
1482/* Structure describing a block of memory which is sub-allocated to
1483 obtain string data memory for strings. Blocks for small strings
1484 are of fixed size SBLOCK_SIZE. Blocks for large strings are made
1485 as large as needed. */
1486
1487struct sblock
1488{
1489 /* Next in list. */
1490 struct sblock *next;
1491
1492 /* Pointer to the next free sdata block. This points past the end
1493 of the sblock if there isn't any space left in this block. */
1494 struct sdata *next_free;
1495
1496 /* Start of data. */
1497 struct sdata first_data;
1498};
1499
1500/* Number of Lisp strings in a string_block structure. The 1020 is
1501 1024 minus malloc overhead. */
1502
1503#define STRING_BLOCK_SIZE \
1504 ((1020 - sizeof (struct string_block *)) / sizeof (struct Lisp_String))
1505
1506/* Structure describing a block from which Lisp_String structures
1507 are allocated. */
1508
1509struct string_block
1510{
1511 /* Place `strings' first, to preserve alignment. */
1512 struct Lisp_String strings[STRING_BLOCK_SIZE];
1513 struct string_block *next;
1514};
1515
1516/* Head and tail of the list of sblock structures holding Lisp string
1517 data. We always allocate from current_sblock. The NEXT pointers
1518 in the sblock structures go from oldest_sblock to current_sblock. */
1519
1520static struct sblock *oldest_sblock, *current_sblock;
1521
1522/* List of sblocks for large strings. */
1523
1524static struct sblock *large_sblocks;
1525
1526/* List of string_block structures, and how many there are. */
1527
1528static struct string_block *string_blocks;
1529static int n_string_blocks;
1530
1531/* Free-list of Lisp_Strings. */
1532
1533static struct Lisp_String *string_free_list;
1534
1535/* Number of live and free Lisp_Strings. */
1536
1537static int total_strings, total_free_strings;
1538
1539/* Number of bytes used by live strings. */
1540
1541static int total_string_size;
1542
1543/* Given a pointer to a Lisp_String S which is on the free-list
1544 string_free_list, return a pointer to its successor in the
1545 free-list. */
1546
1547#define NEXT_FREE_LISP_STRING(S) (*(struct Lisp_String **) (S))
1548
1549/* Return a pointer to the sdata structure belonging to Lisp string S.
1550 S must be live, i.e. S->data must not be null. S->data is actually
1551 a pointer to the `u.data' member of its sdata structure; the
1552 structure starts at a constant offset in front of that. */
1553
1554#ifdef GC_CHECK_STRING_BYTES
1555
1556#define SDATA_OF_STRING(S) \
1557 ((struct sdata *) ((S)->data - sizeof (struct Lisp_String *) \
1558 - sizeof (EMACS_INT)))
1559
1560#else /* not GC_CHECK_STRING_BYTES */
1561
1562#define SDATA_OF_STRING(S) \
1563 ((struct sdata *) ((S)->data - sizeof (struct Lisp_String *)))
1564
1565#endif /* not GC_CHECK_STRING_BYTES */
1566
1567
1568#ifdef GC_CHECK_STRING_OVERRUN
1569
1570/* We check for overrun in string data blocks by appending a small
1571 "cookie" after each allocated string data block, and check for the
1572 presense of this cookie during GC. */
1573
1574#define GC_STRING_OVERRUN_COOKIE_SIZE 4
1575static char string_overrun_cookie[GC_STRING_OVERRUN_COOKIE_SIZE] =
1576 { 0xde, 0xad, 0xbe, 0xef };
1577
1578#else
1579#define GC_STRING_OVERRUN_COOKIE_SIZE 0
1580#endif
1581
1582/* Value is the size of an sdata structure large enough to hold NBYTES
1583 bytes of string data. The value returned includes a terminating
1584 NUL byte, the size of the sdata structure, and padding. */
1585
1586#ifdef GC_CHECK_STRING_BYTES
1587
1588#define SDATA_SIZE(NBYTES) \
1589 ((sizeof (struct Lisp_String *) \
1590 + (NBYTES) + 1 \
1591 + sizeof (EMACS_INT) \
1592 + sizeof (EMACS_INT) - 1) \
1593 & ~(sizeof (EMACS_INT) - 1))
1594
1595#else /* not GC_CHECK_STRING_BYTES */
1596
1597#define SDATA_SIZE(NBYTES) \
1598 ((sizeof (struct Lisp_String *) \
1599 + (NBYTES) + 1 \
1600 + sizeof (EMACS_INT) - 1) \
1601 & ~(sizeof (EMACS_INT) - 1))
1602
1603#endif /* not GC_CHECK_STRING_BYTES */
1604
1605/* Extra bytes to allocate for each string. */
1606
1607#define GC_STRING_EXTRA (GC_STRING_OVERRUN_COOKIE_SIZE)
1608
1609/* Initialize string allocation. Called from init_alloc_once. */
1610
1611void
1612init_strings ()
1613{
1614 total_strings = total_free_strings = total_string_size = 0;
1615 oldest_sblock = current_sblock = large_sblocks = NULL;
1616 string_blocks = NULL;
1617 n_string_blocks = 0;
1618 string_free_list = NULL;
1619}
1620
1621
1622#ifdef GC_CHECK_STRING_BYTES
1623
1624static int check_string_bytes_count;
1625
1626void check_string_bytes P_ ((int));
1627void check_sblock P_ ((struct sblock *));
1628
1629#define CHECK_STRING_BYTES(S) STRING_BYTES (S)
1630
1631
1632/* Like GC_STRING_BYTES, but with debugging check. */
1633
1634int
1635string_bytes (s)
1636 struct Lisp_String *s;
1637{
1638 int nbytes = (s->size_byte < 0 ? s->size & ~ARRAY_MARK_FLAG : s->size_byte);
1639 if (!PURE_POINTER_P (s)
1640 && s->data
1641 && nbytes != SDATA_NBYTES (SDATA_OF_STRING (s)))
1642 abort ();
1643 return nbytes;
1644}
1645
1646/* Check validity of Lisp strings' string_bytes member in B. */
1647
1648void
1649check_sblock (b)
1650 struct sblock *b;
1651{
1652 struct sdata *from, *end, *from_end;
1653
1654 end = b->next_free;
1655
1656 for (from = &b->first_data; from < end; from = from_end)
1657 {
1658 /* Compute the next FROM here because copying below may
1659 overwrite data we need to compute it. */
1660 int nbytes;
1661
1662 /* Check that the string size recorded in the string is the
1663 same as the one recorded in the sdata structure. */
1664 if (from->string)
1665 CHECK_STRING_BYTES (from->string);
1666
1667 if (from->string)
1668 nbytes = GC_STRING_BYTES (from->string);
1669 else
1670 nbytes = SDATA_NBYTES (from);
1671
1672 nbytes = SDATA_SIZE (nbytes);
1673 from_end = (struct sdata *) ((char *) from + nbytes + GC_STRING_EXTRA);
1674 }
1675}
1676
1677
1678/* Check validity of Lisp strings' string_bytes member. ALL_P
1679 non-zero means check all strings, otherwise check only most
1680 recently allocated strings. Used for hunting a bug. */
1681
1682void
1683check_string_bytes (all_p)
1684 int all_p;
1685{
1686 if (all_p)
1687 {
1688 struct sblock *b;
1689
1690 for (b = large_sblocks; b; b = b->next)
1691 {
1692 struct Lisp_String *s = b->first_data.string;
1693 if (s)
1694 CHECK_STRING_BYTES (s);
1695 }
1696
1697 for (b = oldest_sblock; b; b = b->next)
1698 check_sblock (b);
1699 }
1700 else
1701 check_sblock (current_sblock);
1702}
1703
1704#endif /* GC_CHECK_STRING_BYTES */
1705
1706#ifdef GC_CHECK_STRING_FREE_LIST
1707
1708/* Walk through the string free list looking for bogus next pointers.
1709 This may catch buffer overrun from a previous string. */
1710
1711static void
1712check_string_free_list ()
1713{
1714 struct Lisp_String *s;
1715
1716 /* Pop a Lisp_String off the free-list. */
1717 s = string_free_list;
1718 while (s != NULL)
1719 {
1720 if ((unsigned)s < 1024)
1721 abort();
1722 s = NEXT_FREE_LISP_STRING (s);
1723 }
1724}
1725#else
1726#define check_string_free_list()
1727#endif
1728
1729/* Return a new Lisp_String. */
1730
1731static struct Lisp_String *
1732allocate_string ()
1733{
1734 struct Lisp_String *s;
1735
1736 /* If the free-list is empty, allocate a new string_block, and
1737 add all the Lisp_Strings in it to the free-list. */
1738 if (string_free_list == NULL)
1739 {
1740 struct string_block *b;
1741 int i;
1742
1743 b = (struct string_block *) lisp_malloc (sizeof *b, MEM_TYPE_STRING);
1744 bzero (b, sizeof *b);
1745 b->next = string_blocks;
1746 string_blocks = b;
1747 ++n_string_blocks;
1748
1749 for (i = STRING_BLOCK_SIZE - 1; i >= 0; --i)
1750 {
1751 s = b->strings + i;
1752 NEXT_FREE_LISP_STRING (s) = string_free_list;
1753 string_free_list = s;
1754 }
1755
1756 total_free_strings += STRING_BLOCK_SIZE;
1757 }
1758
1759 check_string_free_list ();
1760
1761 /* Pop a Lisp_String off the free-list. */
1762 s = string_free_list;
1763 string_free_list = NEXT_FREE_LISP_STRING (s);
1764
1765 /* Probably not strictly necessary, but play it safe. */
1766 bzero (s, sizeof *s);
1767
1768 --total_free_strings;
1769 ++total_strings;
1770 ++strings_consed;
1771 consing_since_gc += sizeof *s;
1772
1773#ifdef GC_CHECK_STRING_BYTES
1774 if (!noninteractive
1775#ifdef MAC_OS8
1776 && current_sblock
1777#endif
1778 )
1779 {
1780 if (++check_string_bytes_count == 200)
1781 {
1782 check_string_bytes_count = 0;
1783 check_string_bytes (1);
1784 }
1785 else
1786 check_string_bytes (0);
1787 }
1788#endif /* GC_CHECK_STRING_BYTES */
1789
1790 return s;
1791}
1792
1793
1794/* Set up Lisp_String S for holding NCHARS characters, NBYTES bytes,
1795 plus a NUL byte at the end. Allocate an sdata structure for S, and
1796 set S->data to its `u.data' member. Store a NUL byte at the end of
1797 S->data. Set S->size to NCHARS and S->size_byte to NBYTES. Free
1798 S->data if it was initially non-null. */
1799
1800void
1801allocate_string_data (s, nchars, nbytes)
1802 struct Lisp_String *s;
1803 int nchars, nbytes;
1804{
1805 struct sdata *data, *old_data;
1806 struct sblock *b;
1807 int needed, old_nbytes;
1808
1809 /* Determine the number of bytes needed to store NBYTES bytes
1810 of string data. */
1811 needed = SDATA_SIZE (nbytes);
1812
1813 if (nbytes > LARGE_STRING_BYTES)
1814 {
1815 size_t size = sizeof *b - sizeof (struct sdata) + needed;
1816
1817#ifdef DOUG_LEA_MALLOC
1818 /* Prevent mmap'ing the chunk. Lisp data may not be mmap'ed
1819 because mapped region contents are not preserved in
1820 a dumped Emacs.
1821
1822 In case you think of allowing it in a dumped Emacs at the
1823 cost of not being able to re-dump, there's another reason:
1824 mmap'ed data typically have an address towards the top of the
1825 address space, which won't fit into an EMACS_INT (at least on
1826 32-bit systems with the current tagging scheme). --fx */
1827 mallopt (M_MMAP_MAX, 0);
1828#endif
1829
1830 b = (struct sblock *) lisp_malloc (size + GC_STRING_EXTRA, MEM_TYPE_NON_LISP);
1831
1832#ifdef DOUG_LEA_MALLOC
1833 /* Back to a reasonable maximum of mmap'ed areas. */
1834 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS);
1835#endif
1836
1837 b->next_free = &b->first_data;
1838 b->first_data.string = NULL;
1839 b->next = large_sblocks;
1840 large_sblocks = b;
1841 }
1842 else if (current_sblock == NULL
1843 || (((char *) current_sblock + SBLOCK_SIZE
1844 - (char *) current_sblock->next_free)
1845 < (needed + GC_STRING_EXTRA)))
1846 {
1847 /* Not enough room in the current sblock. */
1848 b = (struct sblock *) lisp_malloc (SBLOCK_SIZE, MEM_TYPE_NON_LISP);
1849 b->next_free = &b->first_data;
1850 b->first_data.string = NULL;
1851 b->next = NULL;
1852
1853 if (current_sblock)
1854 current_sblock->next = b;
1855 else
1856 oldest_sblock = b;
1857 current_sblock = b;
1858 }
1859 else
1860 b = current_sblock;
1861
1862 old_data = s->data ? SDATA_OF_STRING (s) : NULL;
1863 old_nbytes = GC_STRING_BYTES (s);
1864
1865 data = b->next_free;
1866 data->string = s;
1867 s->data = SDATA_DATA (data);
1868#ifdef GC_CHECK_STRING_BYTES
1869 SDATA_NBYTES (data) = nbytes;
1870#endif
1871 s->size = nchars;
1872 s->size_byte = nbytes;
1873 s->data[nbytes] = '\0';
1874#ifdef GC_CHECK_STRING_OVERRUN
1875 bcopy (string_overrun_cookie, (char *) data + needed,
1876 GC_STRING_OVERRUN_COOKIE_SIZE);
1877#endif
1878 b->next_free = (struct sdata *) ((char *) data + needed + GC_STRING_EXTRA);
1879
1880 /* If S had already data assigned, mark that as free by setting its
1881 string back-pointer to null, and recording the size of the data
1882 in it. */
1883 if (old_data)
1884 {
1885 SDATA_NBYTES (old_data) = old_nbytes;
1886 old_data->string = NULL;
1887 }
1888
1889 consing_since_gc += needed;
1890}
1891
1892
1893/* Sweep and compact strings. */
1894
1895static void
1896sweep_strings ()
1897{
1898 struct string_block *b, *next;
1899 struct string_block *live_blocks = NULL;
1900
1901 string_free_list = NULL;
1902 total_strings = total_free_strings = 0;
1903 total_string_size = 0;
1904
1905 /* Scan strings_blocks, free Lisp_Strings that aren't marked. */
1906 for (b = string_blocks; b; b = next)
1907 {
1908 int i, nfree = 0;
1909 struct Lisp_String *free_list_before = string_free_list;
1910
1911 next = b->next;
1912
1913 for (i = 0; i < STRING_BLOCK_SIZE; ++i)
1914 {
1915 struct Lisp_String *s = b->strings + i;
1916
1917 if (s->data)
1918 {
1919 /* String was not on free-list before. */
1920 if (STRING_MARKED_P (s))
1921 {
1922 /* String is live; unmark it and its intervals. */
1923 UNMARK_STRING (s);
1924
1925 if (!NULL_INTERVAL_P (s->intervals))
1926 UNMARK_BALANCE_INTERVALS (s->intervals);
1927
1928 ++total_strings;
1929 total_string_size += STRING_BYTES (s);
1930 }
1931 else
1932 {
1933 /* String is dead. Put it on the free-list. */
1934 struct sdata *data = SDATA_OF_STRING (s);
1935
1936 /* Save the size of S in its sdata so that we know
1937 how large that is. Reset the sdata's string
1938 back-pointer so that we know it's free. */
1939#ifdef GC_CHECK_STRING_BYTES
1940 if (GC_STRING_BYTES (s) != SDATA_NBYTES (data))
1941 abort ();
1942#else
1943 data->u.nbytes = GC_STRING_BYTES (s);
1944#endif
1945 data->string = NULL;
1946
1947 /* Reset the strings's `data' member so that we
1948 know it's free. */
1949 s->data = NULL;
1950
1951 /* Put the string on the free-list. */
1952 NEXT_FREE_LISP_STRING (s) = string_free_list;
1953 string_free_list = s;
1954 ++nfree;
1955 }
1956 }
1957 else
1958 {
1959 /* S was on the free-list before. Put it there again. */
1960 NEXT_FREE_LISP_STRING (s) = string_free_list;
1961 string_free_list = s;
1962 ++nfree;
1963 }
1964 }
1965
1966 /* Free blocks that contain free Lisp_Strings only, except
1967 the first two of them. */
1968 if (nfree == STRING_BLOCK_SIZE
1969 && total_free_strings > STRING_BLOCK_SIZE)
1970 {
1971 lisp_free (b);
1972 --n_string_blocks;
1973 string_free_list = free_list_before;
1974 }
1975 else
1976 {
1977 total_free_strings += nfree;
1978 b->next = live_blocks;
1979 live_blocks = b;
1980 }
1981 }
1982
1983 check_string_free_list ();
1984
1985 string_blocks = live_blocks;
1986 free_large_strings ();
1987 compact_small_strings ();
1988
1989 check_string_free_list ();
1990}
1991
1992
1993/* Free dead large strings. */
1994
1995static void
1996free_large_strings ()
1997{
1998 struct sblock *b, *next;
1999 struct sblock *live_blocks = NULL;
2000
2001 for (b = large_sblocks; b; b = next)
2002 {
2003 next = b->next;
2004
2005 if (b->first_data.string == NULL)
2006 lisp_free (b);
2007 else
2008 {
2009 b->next = live_blocks;
2010 live_blocks = b;
2011 }
2012 }
2013
2014 large_sblocks = live_blocks;
2015}
2016
2017
2018/* Compact data of small strings. Free sblocks that don't contain
2019 data of live strings after compaction. */
2020
2021static void
2022compact_small_strings ()
2023{
2024 struct sblock *b, *tb, *next;
2025 struct sdata *from, *to, *end, *tb_end;
2026 struct sdata *to_end, *from_end;
2027
2028 /* TB is the sblock we copy to, TO is the sdata within TB we copy
2029 to, and TB_END is the end of TB. */
2030 tb = oldest_sblock;
2031 tb_end = (struct sdata *) ((char *) tb + SBLOCK_SIZE);
2032 to = &tb->first_data;
2033
2034 /* Step through the blocks from the oldest to the youngest. We
2035 expect that old blocks will stabilize over time, so that less
2036 copying will happen this way. */
2037 for (b = oldest_sblock; b; b = b->next)
2038 {
2039 end = b->next_free;
2040 xassert ((char *) end <= (char *) b + SBLOCK_SIZE);
2041
2042 for (from = &b->first_data; from < end; from = from_end)
2043 {
2044 /* Compute the next FROM here because copying below may
2045 overwrite data we need to compute it. */
2046 int nbytes;
2047
2048#ifdef GC_CHECK_STRING_BYTES
2049 /* Check that the string size recorded in the string is the
2050 same as the one recorded in the sdata structure. */
2051 if (from->string
2052 && GC_STRING_BYTES (from->string) != SDATA_NBYTES (from))
2053 abort ();
2054#endif /* GC_CHECK_STRING_BYTES */
2055
2056 if (from->string)
2057 nbytes = GC_STRING_BYTES (from->string);
2058 else
2059 nbytes = SDATA_NBYTES (from);
2060
2061 if (nbytes > LARGE_STRING_BYTES)
2062 abort ();
2063
2064 nbytes = SDATA_SIZE (nbytes);
2065 from_end = (struct sdata *) ((char *) from + nbytes + GC_STRING_EXTRA);
2066
2067#ifdef GC_CHECK_STRING_OVERRUN
2068 if (bcmp (string_overrun_cookie,
2069 ((char *) from_end) - GC_STRING_OVERRUN_COOKIE_SIZE,
2070 GC_STRING_OVERRUN_COOKIE_SIZE))
2071 abort ();
2072#endif
2073
2074 /* FROM->string non-null means it's alive. Copy its data. */
2075 if (from->string)
2076 {
2077 /* If TB is full, proceed with the next sblock. */
2078 to_end = (struct sdata *) ((char *) to + nbytes + GC_STRING_EXTRA);
2079 if (to_end > tb_end)
2080 {
2081 tb->next_free = to;
2082 tb = tb->next;
2083 tb_end = (struct sdata *) ((char *) tb + SBLOCK_SIZE);
2084 to = &tb->first_data;
2085 to_end = (struct sdata *) ((char *) to + nbytes + GC_STRING_EXTRA);
2086 }
2087
2088 /* Copy, and update the string's `data' pointer. */
2089 if (from != to)
2090 {
2091 xassert (tb != b || to <= from);
2092 safe_bcopy ((char *) from, (char *) to, nbytes + GC_STRING_EXTRA);
2093 to->string->data = SDATA_DATA (to);
2094 }
2095
2096 /* Advance past the sdata we copied to. */
2097 to = to_end;
2098 }
2099 }
2100 }
2101
2102 /* The rest of the sblocks following TB don't contain live data, so
2103 we can free them. */
2104 for (b = tb->next; b; b = next)
2105 {
2106 next = b->next;
2107 lisp_free (b);
2108 }
2109
2110 tb->next_free = to;
2111 tb->next = NULL;
2112 current_sblock = tb;
2113}
2114
2115
2116DEFUN ("make-string", Fmake_string, Smake_string, 2, 2, 0,
2117 doc: /* Return a newly created string of length LENGTH, with INIT in each element.
2118LENGTH must be an integer.
2119INIT must be an integer that represents a character. */)
2120 (length, init)
2121 Lisp_Object length, init;
2122{
2123 register Lisp_Object val;
2124 register unsigned char *p, *end;
2125 int c, nbytes;
2126
2127 CHECK_NATNUM (length);
2128 CHECK_NUMBER (init);
2129
2130 c = XINT (init);
2131 if (SINGLE_BYTE_CHAR_P (c))
2132 {
2133 nbytes = XINT (length);
2134 val = make_uninit_string (nbytes);
2135 p = SDATA (val);
2136 end = p + SCHARS (val);
2137 while (p != end)
2138 *p++ = c;
2139 }
2140 else
2141 {
2142 unsigned char str[MAX_MULTIBYTE_LENGTH];
2143 int len = CHAR_STRING (c, str);
2144
2145 nbytes = len * XINT (length);
2146 val = make_uninit_multibyte_string (XINT (length), nbytes);
2147 p = SDATA (val);
2148 end = p + nbytes;
2149 while (p != end)
2150 {
2151 bcopy (str, p, len);
2152 p += len;
2153 }
2154 }
2155
2156 *p = 0;
2157 return val;
2158}
2159
2160
2161DEFUN ("make-bool-vector", Fmake_bool_vector, Smake_bool_vector, 2, 2, 0,
2162 doc: /* Return a new bool-vector of length LENGTH, using INIT for as each element.
2163LENGTH must be a number. INIT matters only in whether it is t or nil. */)
2164 (length, init)
2165 Lisp_Object length, init;
2166{
2167 register Lisp_Object val;
2168 struct Lisp_Bool_Vector *p;
2169 int real_init, i;
2170 int length_in_chars, length_in_elts, bits_per_value;
2171
2172 CHECK_NATNUM (length);
2173
2174 bits_per_value = sizeof (EMACS_INT) * BOOL_VECTOR_BITS_PER_CHAR;
2175
2176 length_in_elts = (XFASTINT (length) + bits_per_value - 1) / bits_per_value;
2177 length_in_chars = ((XFASTINT (length) + BOOL_VECTOR_BITS_PER_CHAR - 1)
2178 / BOOL_VECTOR_BITS_PER_CHAR);
2179
2180 /* We must allocate one more elements than LENGTH_IN_ELTS for the
2181 slot `size' of the struct Lisp_Bool_Vector. */
2182 val = Fmake_vector (make_number (length_in_elts + 1), Qnil);
2183 p = XBOOL_VECTOR (val);
2184
2185 /* Get rid of any bits that would cause confusion. */
2186 p->vector_size = 0;
2187 XSETBOOL_VECTOR (val, p);
2188 p->size = XFASTINT (length);
2189
2190 real_init = (NILP (init) ? 0 : -1);
2191 for (i = 0; i < length_in_chars ; i++)
2192 p->data[i] = real_init;
2193
2194 /* Clear the extraneous bits in the last byte. */
2195 if (XINT (length) != length_in_chars * BOOL_VECTOR_BITS_PER_CHAR)
2196 XBOOL_VECTOR (val)->data[length_in_chars - 1]
2197 &= (1 << (XINT (length) % BOOL_VECTOR_BITS_PER_CHAR)) - 1;
2198
2199 return val;
2200}
2201
2202
2203/* Make a string from NBYTES bytes at CONTENTS, and compute the number
2204 of characters from the contents. This string may be unibyte or
2205 multibyte, depending on the contents. */
2206
2207Lisp_Object
2208make_string (contents, nbytes)
2209 const char *contents;
2210 int nbytes;
2211{
2212 register Lisp_Object val;
2213 int nchars, multibyte_nbytes;
2214
2215 parse_str_as_multibyte (contents, nbytes, &nchars, &multibyte_nbytes);
2216 if (nbytes == nchars || nbytes != multibyte_nbytes)
2217 /* CONTENTS contains no multibyte sequences or contains an invalid
2218 multibyte sequence. We must make unibyte string. */
2219 val = make_unibyte_string (contents, nbytes);
2220 else
2221 val = make_multibyte_string (contents, nchars, nbytes);
2222 return val;
2223}
2224
2225
2226/* Make an unibyte string from LENGTH bytes at CONTENTS. */
2227
2228Lisp_Object
2229make_unibyte_string (contents, length)
2230 const char *contents;
2231 int length;
2232{
2233 register Lisp_Object val;
2234 val = make_uninit_string (length);
2235 bcopy (contents, SDATA (val), length);
2236 STRING_SET_UNIBYTE (val);
2237 return val;
2238}
2239
2240
2241/* Make a multibyte string from NCHARS characters occupying NBYTES
2242 bytes at CONTENTS. */
2243
2244Lisp_Object
2245make_multibyte_string (contents, nchars, nbytes)
2246 const char *contents;
2247 int nchars, nbytes;
2248{
2249 register Lisp_Object val;
2250 val = make_uninit_multibyte_string (nchars, nbytes);
2251 bcopy (contents, SDATA (val), nbytes);
2252 return val;
2253}
2254
2255
2256/* Make a string from NCHARS characters occupying NBYTES bytes at
2257 CONTENTS. It is a multibyte string if NBYTES != NCHARS. */
2258
2259Lisp_Object
2260make_string_from_bytes (contents, nchars, nbytes)
2261 const char *contents;
2262 int nchars, nbytes;
2263{
2264 register Lisp_Object val;
2265 val = make_uninit_multibyte_string (nchars, nbytes);
2266 bcopy (contents, SDATA (val), nbytes);
2267 if (SBYTES (val) == SCHARS (val))
2268 STRING_SET_UNIBYTE (val);
2269 return val;
2270}
2271
2272
2273/* Make a string from NCHARS characters occupying NBYTES bytes at
2274 CONTENTS. The argument MULTIBYTE controls whether to label the
2275 string as multibyte. If NCHARS is negative, it counts the number of
2276 characters by itself. */
2277
2278Lisp_Object
2279make_specified_string (contents, nchars, nbytes, multibyte)
2280 const char *contents;
2281 int nchars, nbytes;
2282 int multibyte;
2283{
2284 register Lisp_Object val;
2285
2286 if (nchars < 0)
2287 {
2288 if (multibyte)
2289 nchars = multibyte_chars_in_text (contents, nbytes);
2290 else
2291 nchars = nbytes;
2292 }
2293 val = make_uninit_multibyte_string (nchars, nbytes);
2294 bcopy (contents, SDATA (val), nbytes);
2295 if (!multibyte)
2296 STRING_SET_UNIBYTE (val);
2297 return val;
2298}
2299
2300
2301/* Make a string from the data at STR, treating it as multibyte if the
2302 data warrants. */
2303
2304Lisp_Object
2305build_string (str)
2306 const char *str;
2307{
2308 return make_string (str, strlen (str));
2309}
2310
2311
2312/* Return an unibyte Lisp_String set up to hold LENGTH characters
2313 occupying LENGTH bytes. */
2314
2315Lisp_Object
2316make_uninit_string (length)
2317 int length;
2318{
2319 Lisp_Object val;
2320 val = make_uninit_multibyte_string (length, length);
2321 STRING_SET_UNIBYTE (val);
2322 return val;
2323}
2324
2325
2326/* Return a multibyte Lisp_String set up to hold NCHARS characters
2327 which occupy NBYTES bytes. */
2328
2329Lisp_Object
2330make_uninit_multibyte_string (nchars, nbytes)
2331 int nchars, nbytes;
2332{
2333 Lisp_Object string;
2334 struct Lisp_String *s;
2335
2336 if (nchars < 0)
2337 abort ();
2338
2339 s = allocate_string ();
2340 allocate_string_data (s, nchars, nbytes);
2341 XSETSTRING (string, s);
2342 string_chars_consed += nbytes;
2343 return string;
2344}
2345
2346
2347\f
2348/***********************************************************************
2349 Float Allocation
2350 ***********************************************************************/
2351
2352/* We store float cells inside of float_blocks, allocating a new
2353 float_block with malloc whenever necessary. Float cells reclaimed
2354 by GC are put on a free list to be reallocated before allocating
2355 any new float cells from the latest float_block. */
2356
2357#define FLOAT_BLOCK_SIZE \
2358 (((BLOCK_BYTES - sizeof (struct float_block *) \
2359 /* The compiler might add padding at the end. */ \
2360 - (sizeof (struct Lisp_Float) - sizeof (int))) * CHAR_BIT) \
2361 / (sizeof (struct Lisp_Float) * CHAR_BIT + 1))
2362
2363#define GETMARKBIT(block,n) \
2364 (((block)->gcmarkbits[(n) / (sizeof(int) * CHAR_BIT)] \
2365 >> ((n) % (sizeof(int) * CHAR_BIT))) \
2366 & 1)
2367
2368#define SETMARKBIT(block,n) \
2369 (block)->gcmarkbits[(n) / (sizeof(int) * CHAR_BIT)] \
2370 |= 1 << ((n) % (sizeof(int) * CHAR_BIT))
2371
2372#define UNSETMARKBIT(block,n) \
2373 (block)->gcmarkbits[(n) / (sizeof(int) * CHAR_BIT)] \
2374 &= ~(1 << ((n) % (sizeof(int) * CHAR_BIT)))
2375
2376#define FLOAT_BLOCK(fptr) \
2377 ((struct float_block *)(((EMACS_UINT)(fptr)) & ~(BLOCK_ALIGN - 1)))
2378
2379#define FLOAT_INDEX(fptr) \
2380 ((((EMACS_UINT)(fptr)) & (BLOCK_ALIGN - 1)) / sizeof (struct Lisp_Float))
2381
2382struct float_block
2383{
2384 /* Place `floats' at the beginning, to ease up FLOAT_INDEX's job. */
2385 struct Lisp_Float floats[FLOAT_BLOCK_SIZE];
2386 int gcmarkbits[1 + FLOAT_BLOCK_SIZE / (sizeof(int) * CHAR_BIT)];
2387 struct float_block *next;
2388};
2389
2390#define FLOAT_MARKED_P(fptr) \
2391 GETMARKBIT (FLOAT_BLOCK (fptr), FLOAT_INDEX ((fptr)))
2392
2393#define FLOAT_MARK(fptr) \
2394 SETMARKBIT (FLOAT_BLOCK (fptr), FLOAT_INDEX ((fptr)))
2395
2396#define FLOAT_UNMARK(fptr) \
2397 UNSETMARKBIT (FLOAT_BLOCK (fptr), FLOAT_INDEX ((fptr)))
2398
2399/* Current float_block. */
2400
2401struct float_block *float_block;
2402
2403/* Index of first unused Lisp_Float in the current float_block. */
2404
2405int float_block_index;
2406
2407/* Total number of float blocks now in use. */
2408
2409int n_float_blocks;
2410
2411/* Free-list of Lisp_Floats. */
2412
2413struct Lisp_Float *float_free_list;
2414
2415
2416/* Initialize float allocation. */
2417
2418void
2419init_float ()
2420{
2421 float_block = NULL;
2422 float_block_index = FLOAT_BLOCK_SIZE; /* Force alloc of new float_block. */
2423 float_free_list = 0;
2424 n_float_blocks = 0;
2425}
2426
2427
2428/* Explicitly free a float cell by putting it on the free-list. */
2429
2430void
2431free_float (ptr)
2432 struct Lisp_Float *ptr;
2433{
2434 *(struct Lisp_Float **)&ptr->data = float_free_list;
2435 float_free_list = ptr;
2436}
2437
2438
2439/* Return a new float object with value FLOAT_VALUE. */
2440
2441Lisp_Object
2442make_float (float_value)
2443 double float_value;
2444{
2445 register Lisp_Object val;
2446
2447 if (float_free_list)
2448 {
2449 /* We use the data field for chaining the free list
2450 so that we won't use the same field that has the mark bit. */
2451 XSETFLOAT (val, float_free_list);
2452 float_free_list = *(struct Lisp_Float **)&float_free_list->data;
2453 }
2454 else
2455 {
2456 if (float_block_index == FLOAT_BLOCK_SIZE)
2457 {
2458 register struct float_block *new;
2459
2460 new = (struct float_block *) lisp_align_malloc (sizeof *new,
2461 MEM_TYPE_FLOAT);
2462 new->next = float_block;
2463 bzero ((char *) new->gcmarkbits, sizeof new->gcmarkbits);
2464 float_block = new;
2465 float_block_index = 0;
2466 n_float_blocks++;
2467 }
2468 XSETFLOAT (val, &float_block->floats[float_block_index]);
2469 float_block_index++;
2470 }
2471
2472 XFLOAT_DATA (val) = float_value;
2473 eassert (!FLOAT_MARKED_P (XFLOAT (val)));
2474 consing_since_gc += sizeof (struct Lisp_Float);
2475 floats_consed++;
2476 return val;
2477}
2478
2479
2480\f
2481/***********************************************************************
2482 Cons Allocation
2483 ***********************************************************************/
2484
2485/* We store cons cells inside of cons_blocks, allocating a new
2486 cons_block with malloc whenever necessary. Cons cells reclaimed by
2487 GC are put on a free list to be reallocated before allocating
2488 any new cons cells from the latest cons_block. */
2489
2490#define CONS_BLOCK_SIZE \
2491 (((BLOCK_BYTES - sizeof (struct cons_block *)) * CHAR_BIT) \
2492 / (sizeof (struct Lisp_Cons) * CHAR_BIT + 1))
2493
2494#define CONS_BLOCK(fptr) \
2495 ((struct cons_block *)(((EMACS_UINT)(fptr)) & ~(BLOCK_ALIGN - 1)))
2496
2497#define CONS_INDEX(fptr) \
2498 ((((EMACS_UINT)(fptr)) & (BLOCK_ALIGN - 1)) / sizeof (struct Lisp_Cons))
2499
2500struct cons_block
2501{
2502 /* Place `conses' at the beginning, to ease up CONS_INDEX's job. */
2503 struct Lisp_Cons conses[CONS_BLOCK_SIZE];
2504 int gcmarkbits[1 + CONS_BLOCK_SIZE / (sizeof(int) * CHAR_BIT)];
2505 struct cons_block *next;
2506};
2507
2508#define CONS_MARKED_P(fptr) \
2509 GETMARKBIT (CONS_BLOCK (fptr), CONS_INDEX ((fptr)))
2510
2511#define CONS_MARK(fptr) \
2512 SETMARKBIT (CONS_BLOCK (fptr), CONS_INDEX ((fptr)))
2513
2514#define CONS_UNMARK(fptr) \
2515 UNSETMARKBIT (CONS_BLOCK (fptr), CONS_INDEX ((fptr)))
2516
2517/* Current cons_block. */
2518
2519struct cons_block *cons_block;
2520
2521/* Index of first unused Lisp_Cons in the current block. */
2522
2523int cons_block_index;
2524
2525/* Free-list of Lisp_Cons structures. */
2526
2527struct Lisp_Cons *cons_free_list;
2528
2529/* Total number of cons blocks now in use. */
2530
2531int n_cons_blocks;
2532
2533
2534/* Initialize cons allocation. */
2535
2536void
2537init_cons ()
2538{
2539 cons_block = NULL;
2540 cons_block_index = CONS_BLOCK_SIZE; /* Force alloc of new cons_block. */
2541 cons_free_list = 0;
2542 n_cons_blocks = 0;
2543}
2544
2545
2546/* Explicitly free a cons cell by putting it on the free-list. */
2547
2548void
2549free_cons (ptr)
2550 struct Lisp_Cons *ptr;
2551{
2552 *(struct Lisp_Cons **)&ptr->cdr = cons_free_list;
2553#if GC_MARK_STACK
2554 ptr->car = Vdead;
2555#endif
2556 cons_free_list = ptr;
2557}
2558
2559DEFUN ("cons", Fcons, Scons, 2, 2, 0,
2560 doc: /* Create a new cons, give it CAR and CDR as components, and return it. */)
2561 (car, cdr)
2562 Lisp_Object car, cdr;
2563{
2564 register Lisp_Object val;
2565
2566 if (cons_free_list)
2567 {
2568 /* We use the cdr for chaining the free list
2569 so that we won't use the same field that has the mark bit. */
2570 XSETCONS (val, cons_free_list);
2571 cons_free_list = *(struct Lisp_Cons **)&cons_free_list->cdr;
2572 }
2573 else
2574 {
2575 if (cons_block_index == CONS_BLOCK_SIZE)
2576 {
2577 register struct cons_block *new;
2578 new = (struct cons_block *) lisp_align_malloc (sizeof *new,
2579 MEM_TYPE_CONS);
2580 bzero ((char *) new->gcmarkbits, sizeof new->gcmarkbits);
2581 new->next = cons_block;
2582 cons_block = new;
2583 cons_block_index = 0;
2584 n_cons_blocks++;
2585 }
2586 XSETCONS (val, &cons_block->conses[cons_block_index]);
2587 cons_block_index++;
2588 }
2589
2590 XSETCAR (val, car);
2591 XSETCDR (val, cdr);
2592 eassert (!CONS_MARKED_P (XCONS (val)));
2593 consing_since_gc += sizeof (struct Lisp_Cons);
2594 cons_cells_consed++;
2595 return val;
2596}
2597
2598/* Get an error now if there's any junk in the cons free list. */
2599void
2600check_cons_list ()
2601{
2602#ifdef GC_CHECK_CONS_LIST
2603 struct Lisp_Cons *tail = cons_free_list;
2604
2605 while (tail)
2606 tail = *(struct Lisp_Cons **)&tail->cdr;
2607#endif
2608}
2609
2610/* Make a list of 2, 3, 4 or 5 specified objects. */
2611
2612Lisp_Object
2613list2 (arg1, arg2)
2614 Lisp_Object arg1, arg2;
2615{
2616 return Fcons (arg1, Fcons (arg2, Qnil));
2617}
2618
2619
2620Lisp_Object
2621list3 (arg1, arg2, arg3)
2622 Lisp_Object arg1, arg2, arg3;
2623{
2624 return Fcons (arg1, Fcons (arg2, Fcons (arg3, Qnil)));
2625}
2626
2627
2628Lisp_Object
2629list4 (arg1, arg2, arg3, arg4)
2630 Lisp_Object arg1, arg2, arg3, arg4;
2631{
2632 return Fcons (arg1, Fcons (arg2, Fcons (arg3, Fcons (arg4, Qnil))));
2633}
2634
2635
2636Lisp_Object
2637list5 (arg1, arg2, arg3, arg4, arg5)
2638 Lisp_Object arg1, arg2, arg3, arg4, arg5;
2639{
2640 return Fcons (arg1, Fcons (arg2, Fcons (arg3, Fcons (arg4,
2641 Fcons (arg5, Qnil)))));
2642}
2643
2644
2645DEFUN ("list", Flist, Slist, 0, MANY, 0,
2646 doc: /* Return a newly created list with specified arguments as elements.
2647Any number of arguments, even zero arguments, are allowed.
2648usage: (list &rest OBJECTS) */)
2649 (nargs, args)
2650 int nargs;
2651 register Lisp_Object *args;
2652{
2653 register Lisp_Object val;
2654 val = Qnil;
2655
2656 while (nargs > 0)
2657 {
2658 nargs--;
2659 val = Fcons (args[nargs], val);
2660 }
2661 return val;
2662}
2663
2664
2665DEFUN ("make-list", Fmake_list, Smake_list, 2, 2, 0,
2666 doc: /* Return a newly created list of length LENGTH, with each element being INIT. */)
2667 (length, init)
2668 register Lisp_Object length, init;
2669{
2670 register Lisp_Object val;
2671 register int size;
2672
2673 CHECK_NATNUM (length);
2674 size = XFASTINT (length);
2675
2676 val = Qnil;
2677 while (size > 0)
2678 {
2679 val = Fcons (init, val);
2680 --size;
2681
2682 if (size > 0)
2683 {
2684 val = Fcons (init, val);
2685 --size;
2686
2687 if (size > 0)
2688 {
2689 val = Fcons (init, val);
2690 --size;
2691
2692 if (size > 0)
2693 {
2694 val = Fcons (init, val);
2695 --size;
2696
2697 if (size > 0)
2698 {
2699 val = Fcons (init, val);
2700 --size;
2701 }
2702 }
2703 }
2704 }
2705
2706 QUIT;
2707 }
2708
2709 return val;
2710}
2711
2712
2713\f
2714/***********************************************************************
2715 Vector Allocation
2716 ***********************************************************************/
2717
2718/* Singly-linked list of all vectors. */
2719
2720struct Lisp_Vector *all_vectors;
2721
2722/* Total number of vector-like objects now in use. */
2723
2724int n_vectors;
2725
2726
2727/* Value is a pointer to a newly allocated Lisp_Vector structure
2728 with room for LEN Lisp_Objects. */
2729
2730static struct Lisp_Vector *
2731allocate_vectorlike (len, type)
2732 EMACS_INT len;
2733 enum mem_type type;
2734{
2735 struct Lisp_Vector *p;
2736 size_t nbytes;
2737
2738#ifdef DOUG_LEA_MALLOC
2739 /* Prevent mmap'ing the chunk. Lisp data may not be mmap'ed
2740 because mapped region contents are not preserved in
2741 a dumped Emacs. */
2742 BLOCK_INPUT;
2743 mallopt (M_MMAP_MAX, 0);
2744 UNBLOCK_INPUT;
2745#endif
2746
2747 nbytes = sizeof *p + (len - 1) * sizeof p->contents[0];
2748 p = (struct Lisp_Vector *) lisp_malloc (nbytes, type);
2749
2750#ifdef DOUG_LEA_MALLOC
2751 /* Back to a reasonable maximum of mmap'ed areas. */
2752 BLOCK_INPUT;
2753 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS);
2754 UNBLOCK_INPUT;
2755#endif
2756
2757 consing_since_gc += nbytes;
2758 vector_cells_consed += len;
2759
2760 p->next = all_vectors;
2761 all_vectors = p;
2762 ++n_vectors;
2763 return p;
2764}
2765
2766
2767/* Allocate a vector with NSLOTS slots. */
2768
2769struct Lisp_Vector *
2770allocate_vector (nslots)
2771 EMACS_INT nslots;
2772{
2773 struct Lisp_Vector *v = allocate_vectorlike (nslots, MEM_TYPE_VECTOR);
2774 v->size = nslots;
2775 return v;
2776}
2777
2778
2779/* Allocate other vector-like structures. */
2780
2781struct Lisp_Hash_Table *
2782allocate_hash_table ()
2783{
2784 EMACS_INT len = VECSIZE (struct Lisp_Hash_Table);
2785 struct Lisp_Vector *v = allocate_vectorlike (len, MEM_TYPE_HASH_TABLE);
2786 EMACS_INT i;
2787
2788 v->size = len;
2789 for (i = 0; i < len; ++i)
2790 v->contents[i] = Qnil;
2791
2792 return (struct Lisp_Hash_Table *) v;
2793}
2794
2795
2796struct window *
2797allocate_window ()
2798{
2799 EMACS_INT len = VECSIZE (struct window);
2800 struct Lisp_Vector *v = allocate_vectorlike (len, MEM_TYPE_WINDOW);
2801 EMACS_INT i;
2802
2803 for (i = 0; i < len; ++i)
2804 v->contents[i] = Qnil;
2805 v->size = len;
2806
2807 return (struct window *) v;
2808}
2809
2810
2811struct frame *
2812allocate_frame ()
2813{
2814 EMACS_INT len = VECSIZE (struct frame);
2815 struct Lisp_Vector *v = allocate_vectorlike (len, MEM_TYPE_FRAME);
2816 EMACS_INT i;
2817
2818 for (i = 0; i < len; ++i)
2819 v->contents[i] = make_number (0);
2820 v->size = len;
2821 return (struct frame *) v;
2822}
2823
2824
2825struct Lisp_Process *
2826allocate_process ()
2827{
2828 EMACS_INT len = VECSIZE (struct Lisp_Process);
2829 struct Lisp_Vector *v = allocate_vectorlike (len, MEM_TYPE_PROCESS);
2830 EMACS_INT i;
2831
2832 for (i = 0; i < len; ++i)
2833 v->contents[i] = Qnil;
2834 v->size = len;
2835
2836 return (struct Lisp_Process *) v;
2837}
2838
2839
2840struct Lisp_Vector *
2841allocate_other_vector (len)
2842 EMACS_INT len;
2843{
2844 struct Lisp_Vector *v = allocate_vectorlike (len, MEM_TYPE_VECTOR);
2845 EMACS_INT i;
2846
2847 for (i = 0; i < len; ++i)
2848 v->contents[i] = Qnil;
2849 v->size = len;
2850
2851 return v;
2852}
2853
2854
2855DEFUN ("make-vector", Fmake_vector, Smake_vector, 2, 2, 0,
2856 doc: /* Return a newly created vector of length LENGTH, with each element being INIT.
2857See also the function `vector'. */)
2858 (length, init)
2859 register Lisp_Object length, init;
2860{
2861 Lisp_Object vector;
2862 register EMACS_INT sizei;
2863 register int index;
2864 register struct Lisp_Vector *p;
2865
2866 CHECK_NATNUM (length);
2867 sizei = XFASTINT (length);
2868
2869 p = allocate_vector (sizei);
2870 for (index = 0; index < sizei; index++)
2871 p->contents[index] = init;
2872
2873 XSETVECTOR (vector, p);
2874 return vector;
2875}
2876
2877
2878DEFUN ("make-char-table", Fmake_char_table, Smake_char_table, 1, 2, 0,
2879 doc: /* Return a newly created char-table, with purpose PURPOSE.
2880Each element is initialized to INIT, which defaults to nil.
2881PURPOSE should be a symbol which has a `char-table-extra-slots' property.
2882The property's value should be an integer between 0 and 10. */)
2883 (purpose, init)
2884 register Lisp_Object purpose, init;
2885{
2886 Lisp_Object vector;
2887 Lisp_Object n;
2888 CHECK_SYMBOL (purpose);
2889 n = Fget (purpose, Qchar_table_extra_slots);
2890 CHECK_NUMBER (n);
2891 if (XINT (n) < 0 || XINT (n) > 10)
2892 args_out_of_range (n, Qnil);
2893 /* Add 2 to the size for the defalt and parent slots. */
2894 vector = Fmake_vector (make_number (CHAR_TABLE_STANDARD_SLOTS + XINT (n)),
2895 init);
2896 XCHAR_TABLE (vector)->top = Qt;
2897 XCHAR_TABLE (vector)->parent = Qnil;
2898 XCHAR_TABLE (vector)->purpose = purpose;
2899 XSETCHAR_TABLE (vector, XCHAR_TABLE (vector));
2900 return vector;
2901}
2902
2903
2904/* Return a newly created sub char table with default value DEFALT.
2905 Since a sub char table does not appear as a top level Emacs Lisp
2906 object, we don't need a Lisp interface to make it. */
2907
2908Lisp_Object
2909make_sub_char_table (defalt)
2910 Lisp_Object defalt;
2911{
2912 Lisp_Object vector
2913 = Fmake_vector (make_number (SUB_CHAR_TABLE_STANDARD_SLOTS), Qnil);
2914 XCHAR_TABLE (vector)->top = Qnil;
2915 XCHAR_TABLE (vector)->defalt = defalt;
2916 XSETCHAR_TABLE (vector, XCHAR_TABLE (vector));
2917 return vector;
2918}
2919
2920
2921DEFUN ("vector", Fvector, Svector, 0, MANY, 0,
2922 doc: /* Return a newly created vector with specified arguments as elements.
2923Any number of arguments, even zero arguments, are allowed.
2924usage: (vector &rest OBJECTS) */)
2925 (nargs, args)
2926 register int nargs;
2927 Lisp_Object *args;
2928{
2929 register Lisp_Object len, val;
2930 register int index;
2931 register struct Lisp_Vector *p;
2932
2933 XSETFASTINT (len, nargs);
2934 val = Fmake_vector (len, Qnil);
2935 p = XVECTOR (val);
2936 for (index = 0; index < nargs; index++)
2937 p->contents[index] = args[index];
2938 return val;
2939}
2940
2941
2942DEFUN ("make-byte-code", Fmake_byte_code, Smake_byte_code, 4, MANY, 0,
2943 doc: /* Create a byte-code object with specified arguments as elements.
2944The arguments should be the arglist, bytecode-string, constant vector,
2945stack size, (optional) doc string, and (optional) interactive spec.
2946The first four arguments are required; at most six have any
2947significance.
2948usage: (make-byte-code ARGLIST BYTE-CODE CONSTANTS DEPTH &optional DOCSTRING INTERACTIVE-SPEC &rest ELEMENTS) */)
2949 (nargs, args)
2950 register int nargs;
2951 Lisp_Object *args;
2952{
2953 register Lisp_Object len, val;
2954 register int index;
2955 register struct Lisp_Vector *p;
2956
2957 XSETFASTINT (len, nargs);
2958 if (!NILP (Vpurify_flag))
2959 val = make_pure_vector ((EMACS_INT) nargs);
2960 else
2961 val = Fmake_vector (len, Qnil);
2962
2963 if (STRINGP (args[1]) && STRING_MULTIBYTE (args[1]))
2964 /* BYTECODE-STRING must have been produced by Emacs 20.2 or the
2965 earlier because they produced a raw 8-bit string for byte-code
2966 and now such a byte-code string is loaded as multibyte while
2967 raw 8-bit characters converted to multibyte form. Thus, now we
2968 must convert them back to the original unibyte form. */
2969 args[1] = Fstring_as_unibyte (args[1]);
2970
2971 p = XVECTOR (val);
2972 for (index = 0; index < nargs; index++)
2973 {
2974 if (!NILP (Vpurify_flag))
2975 args[index] = Fpurecopy (args[index]);
2976 p->contents[index] = args[index];
2977 }
2978 XSETCOMPILED (val, p);
2979 return val;
2980}
2981
2982
2983\f
2984/***********************************************************************
2985 Symbol Allocation
2986 ***********************************************************************/
2987
2988/* Each symbol_block is just under 1020 bytes long, since malloc
2989 really allocates in units of powers of two and uses 4 bytes for its
2990 own overhead. */
2991
2992#define SYMBOL_BLOCK_SIZE \
2993 ((1020 - sizeof (struct symbol_block *)) / sizeof (struct Lisp_Symbol))
2994
2995struct symbol_block
2996{
2997 /* Place `symbols' first, to preserve alignment. */
2998 struct Lisp_Symbol symbols[SYMBOL_BLOCK_SIZE];
2999 struct symbol_block *next;
3000};
3001
3002/* Current symbol block and index of first unused Lisp_Symbol
3003 structure in it. */
3004
3005struct symbol_block *symbol_block;
3006int symbol_block_index;
3007
3008/* List of free symbols. */
3009
3010struct Lisp_Symbol *symbol_free_list;
3011
3012/* Total number of symbol blocks now in use. */
3013
3014int n_symbol_blocks;
3015
3016
3017/* Initialize symbol allocation. */
3018
3019void
3020init_symbol ()
3021{
3022 symbol_block = NULL;
3023 symbol_block_index = SYMBOL_BLOCK_SIZE;
3024 symbol_free_list = 0;
3025 n_symbol_blocks = 0;
3026}
3027
3028
3029DEFUN ("make-symbol", Fmake_symbol, Smake_symbol, 1, 1, 0,
3030 doc: /* Return a newly allocated uninterned symbol whose name is NAME.
3031Its value and function definition are void, and its property list is nil. */)
3032 (name)
3033 Lisp_Object name;
3034{
3035 register Lisp_Object val;
3036 register struct Lisp_Symbol *p;
3037
3038 CHECK_STRING (name);
3039
3040 if (symbol_free_list)
3041 {
3042 XSETSYMBOL (val, symbol_free_list);
3043 symbol_free_list = *(struct Lisp_Symbol **)&symbol_free_list->value;
3044 }
3045 else
3046 {
3047 if (symbol_block_index == SYMBOL_BLOCK_SIZE)
3048 {
3049 struct symbol_block *new;
3050 new = (struct symbol_block *) lisp_malloc (sizeof *new,
3051 MEM_TYPE_SYMBOL);
3052 new->next = symbol_block;
3053 symbol_block = new;
3054 symbol_block_index = 0;
3055 n_symbol_blocks++;
3056 }
3057 XSETSYMBOL (val, &symbol_block->symbols[symbol_block_index]);
3058 symbol_block_index++;
3059 }
3060
3061 p = XSYMBOL (val);
3062 p->xname = name;
3063 p->plist = Qnil;
3064 p->value = Qunbound;
3065 p->function = Qunbound;
3066 p->next = NULL;
3067 p->gcmarkbit = 0;
3068 p->interned = SYMBOL_UNINTERNED;
3069 p->constant = 0;
3070 p->indirect_variable = 0;
3071 consing_since_gc += sizeof (struct Lisp_Symbol);
3072 symbols_consed++;
3073 return val;
3074}
3075
3076
3077\f
3078/***********************************************************************
3079 Marker (Misc) Allocation
3080 ***********************************************************************/
3081
3082/* Allocation of markers and other objects that share that structure.
3083 Works like allocation of conses. */
3084
3085#define MARKER_BLOCK_SIZE \
3086 ((1020 - sizeof (struct marker_block *)) / sizeof (union Lisp_Misc))
3087
3088struct marker_block
3089{
3090 /* Place `markers' first, to preserve alignment. */
3091 union Lisp_Misc markers[MARKER_BLOCK_SIZE];
3092 struct marker_block *next;
3093};
3094
3095struct marker_block *marker_block;
3096int marker_block_index;
3097
3098union Lisp_Misc *marker_free_list;
3099
3100/* Total number of marker blocks now in use. */
3101
3102int n_marker_blocks;
3103
3104void
3105init_marker ()
3106{
3107 marker_block = NULL;
3108 marker_block_index = MARKER_BLOCK_SIZE;
3109 marker_free_list = 0;
3110 n_marker_blocks = 0;
3111}
3112
3113/* Return a newly allocated Lisp_Misc object, with no substructure. */
3114
3115Lisp_Object
3116allocate_misc ()
3117{
3118 Lisp_Object val;
3119
3120 if (marker_free_list)
3121 {
3122 XSETMISC (val, marker_free_list);
3123 marker_free_list = marker_free_list->u_free.chain;
3124 }
3125 else
3126 {
3127 if (marker_block_index == MARKER_BLOCK_SIZE)
3128 {
3129 struct marker_block *new;
3130 new = (struct marker_block *) lisp_malloc (sizeof *new,
3131 MEM_TYPE_MISC);
3132 new->next = marker_block;
3133 marker_block = new;
3134 marker_block_index = 0;
3135 n_marker_blocks++;
3136 total_free_markers += MARKER_BLOCK_SIZE;
3137 }
3138 XSETMISC (val, &marker_block->markers[marker_block_index]);
3139 marker_block_index++;
3140 }
3141
3142 --total_free_markers;
3143 consing_since_gc += sizeof (union Lisp_Misc);
3144 misc_objects_consed++;
3145 XMARKER (val)->gcmarkbit = 0;
3146 return val;
3147}
3148
3149/* Free a Lisp_Misc object */
3150
3151void
3152free_misc (misc)
3153 Lisp_Object misc;
3154{
3155 XMISC (misc)->u_marker.type = Lisp_Misc_Free;
3156 XMISC (misc)->u_free.chain = marker_free_list;
3157 marker_free_list = XMISC (misc);
3158
3159 total_free_markers++;
3160}
3161
3162/* Return a Lisp_Misc_Save_Value object containing POINTER and
3163 INTEGER. This is used to package C values to call record_unwind_protect.
3164 The unwind function can get the C values back using XSAVE_VALUE. */
3165
3166Lisp_Object
3167make_save_value (pointer, integer)
3168 void *pointer;
3169 int integer;
3170{
3171 register Lisp_Object val;
3172 register struct Lisp_Save_Value *p;
3173
3174 val = allocate_misc ();
3175 XMISCTYPE (val) = Lisp_Misc_Save_Value;
3176 p = XSAVE_VALUE (val);
3177 p->pointer = pointer;
3178 p->integer = integer;
3179 p->dogc = 0;
3180 return val;
3181}
3182
3183DEFUN ("make-marker", Fmake_marker, Smake_marker, 0, 0, 0,
3184 doc: /* Return a newly allocated marker which does not point at any place. */)
3185 ()
3186{
3187 register Lisp_Object val;
3188 register struct Lisp_Marker *p;
3189
3190 val = allocate_misc ();
3191 XMISCTYPE (val) = Lisp_Misc_Marker;
3192 p = XMARKER (val);
3193 p->buffer = 0;
3194 p->bytepos = 0;
3195 p->charpos = 0;
3196 p->next = NULL;
3197 p->insertion_type = 0;
3198 return val;
3199}
3200
3201/* Put MARKER back on the free list after using it temporarily. */
3202
3203void
3204free_marker (marker)
3205 Lisp_Object marker;
3206{
3207 unchain_marker (XMARKER (marker));
3208 free_misc (marker);
3209}
3210
3211\f
3212/* Return a newly created vector or string with specified arguments as
3213 elements. If all the arguments are characters that can fit
3214 in a string of events, make a string; otherwise, make a vector.
3215
3216 Any number of arguments, even zero arguments, are allowed. */
3217
3218Lisp_Object
3219make_event_array (nargs, args)
3220 register int nargs;
3221 Lisp_Object *args;
3222{
3223 int i;
3224
3225 for (i = 0; i < nargs; i++)
3226 /* The things that fit in a string
3227 are characters that are in 0...127,
3228 after discarding the meta bit and all the bits above it. */
3229 if (!INTEGERP (args[i])
3230 || (XUINT (args[i]) & ~(-CHAR_META)) >= 0200)
3231 return Fvector (nargs, args);
3232
3233 /* Since the loop exited, we know that all the things in it are
3234 characters, so we can make a string. */
3235 {
3236 Lisp_Object result;
3237
3238 result = Fmake_string (make_number (nargs), make_number (0));
3239 for (i = 0; i < nargs; i++)
3240 {
3241 SSET (result, i, XINT (args[i]));
3242 /* Move the meta bit to the right place for a string char. */
3243 if (XINT (args[i]) & CHAR_META)
3244 SSET (result, i, SREF (result, i) | 0x80);
3245 }
3246
3247 return result;
3248 }
3249}
3250
3251
3252\f
3253/************************************************************************
3254 C Stack Marking
3255 ************************************************************************/
3256
3257#if GC_MARK_STACK || defined GC_MALLOC_CHECK
3258
3259/* Conservative C stack marking requires a method to identify possibly
3260 live Lisp objects given a pointer value. We do this by keeping
3261 track of blocks of Lisp data that are allocated in a red-black tree
3262 (see also the comment of mem_node which is the type of nodes in
3263 that tree). Function lisp_malloc adds information for an allocated
3264 block to the red-black tree with calls to mem_insert, and function
3265 lisp_free removes it with mem_delete. Functions live_string_p etc
3266 call mem_find to lookup information about a given pointer in the
3267 tree, and use that to determine if the pointer points to a Lisp
3268 object or not. */
3269
3270/* Initialize this part of alloc.c. */
3271
3272static void
3273mem_init ()
3274{
3275 mem_z.left = mem_z.right = MEM_NIL;
3276 mem_z.parent = NULL;
3277 mem_z.color = MEM_BLACK;
3278 mem_z.start = mem_z.end = NULL;
3279 mem_root = MEM_NIL;
3280}
3281
3282
3283/* Value is a pointer to the mem_node containing START. Value is
3284 MEM_NIL if there is no node in the tree containing START. */
3285
3286static INLINE struct mem_node *
3287mem_find (start)
3288 void *start;
3289{
3290 struct mem_node *p;
3291
3292 if (start < min_heap_address || start > max_heap_address)
3293 return MEM_NIL;
3294
3295 /* Make the search always successful to speed up the loop below. */
3296 mem_z.start = start;
3297 mem_z.end = (char *) start + 1;
3298
3299 p = mem_root;
3300 while (start < p->start || start >= p->end)
3301 p = start < p->start ? p->left : p->right;
3302 return p;
3303}
3304
3305
3306/* Insert a new node into the tree for a block of memory with start
3307 address START, end address END, and type TYPE. Value is a
3308 pointer to the node that was inserted. */
3309
3310static struct mem_node *
3311mem_insert (start, end, type)
3312 void *start, *end;
3313 enum mem_type type;
3314{
3315 struct mem_node *c, *parent, *x;
3316
3317 if (start < min_heap_address)
3318 min_heap_address = start;
3319 if (end > max_heap_address)
3320 max_heap_address = end;
3321
3322 /* See where in the tree a node for START belongs. In this
3323 particular application, it shouldn't happen that a node is already
3324 present. For debugging purposes, let's check that. */
3325 c = mem_root;
3326 parent = NULL;
3327
3328#if GC_MARK_STACK != GC_MAKE_GCPROS_NOOPS
3329
3330 while (c != MEM_NIL)
3331 {
3332 if (start >= c->start && start < c->end)
3333 abort ();
3334 parent = c;
3335 c = start < c->start ? c->left : c->right;
3336 }
3337
3338#else /* GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS */
3339
3340 while (c != MEM_NIL)
3341 {
3342 parent = c;
3343 c = start < c->start ? c->left : c->right;
3344 }
3345
3346#endif /* GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS */
3347
3348 /* Create a new node. */
3349#ifdef GC_MALLOC_CHECK
3350 x = (struct mem_node *) _malloc_internal (sizeof *x);
3351 if (x == NULL)
3352 abort ();
3353#else
3354 x = (struct mem_node *) xmalloc (sizeof *x);
3355#endif
3356 x->start = start;
3357 x->end = end;
3358 x->type = type;
3359 x->parent = parent;
3360 x->left = x->right = MEM_NIL;
3361 x->color = MEM_RED;
3362
3363 /* Insert it as child of PARENT or install it as root. */
3364 if (parent)
3365 {
3366 if (start < parent->start)
3367 parent->left = x;
3368 else
3369 parent->right = x;
3370 }
3371 else
3372 mem_root = x;
3373
3374 /* Re-establish red-black tree properties. */
3375 mem_insert_fixup (x);
3376
3377 return x;
3378}
3379
3380
3381/* Re-establish the red-black properties of the tree, and thereby
3382 balance the tree, after node X has been inserted; X is always red. */
3383
3384static void
3385mem_insert_fixup (x)
3386 struct mem_node *x;
3387{
3388 while (x != mem_root && x->parent->color == MEM_RED)
3389 {
3390 /* X is red and its parent is red. This is a violation of
3391 red-black tree property #3. */
3392
3393 if (x->parent == x->parent->parent->left)
3394 {
3395 /* We're on the left side of our grandparent, and Y is our
3396 "uncle". */
3397 struct mem_node *y = x->parent->parent->right;
3398
3399 if (y->color == MEM_RED)
3400 {
3401 /* Uncle and parent are red but should be black because
3402 X is red. Change the colors accordingly and proceed
3403 with the grandparent. */
3404 x->parent->color = MEM_BLACK;
3405 y->color = MEM_BLACK;
3406 x->parent->parent->color = MEM_RED;
3407 x = x->parent->parent;
3408 }
3409 else
3410 {
3411 /* Parent and uncle have different colors; parent is
3412 red, uncle is black. */
3413 if (x == x->parent->right)
3414 {
3415 x = x->parent;
3416 mem_rotate_left (x);
3417 }
3418
3419 x->parent->color = MEM_BLACK;
3420 x->parent->parent->color = MEM_RED;
3421 mem_rotate_right (x->parent->parent);
3422 }
3423 }
3424 else
3425 {
3426 /* This is the symmetrical case of above. */
3427 struct mem_node *y = x->parent->parent->left;
3428
3429 if (y->color == MEM_RED)
3430 {
3431 x->parent->color = MEM_BLACK;
3432 y->color = MEM_BLACK;
3433 x->parent->parent->color = MEM_RED;
3434 x = x->parent->parent;
3435 }
3436 else
3437 {
3438 if (x == x->parent->left)
3439 {
3440 x = x->parent;
3441 mem_rotate_right (x);
3442 }
3443
3444 x->parent->color = MEM_BLACK;
3445 x->parent->parent->color = MEM_RED;
3446 mem_rotate_left (x->parent->parent);
3447 }
3448 }
3449 }
3450
3451 /* The root may have been changed to red due to the algorithm. Set
3452 it to black so that property #5 is satisfied. */
3453 mem_root->color = MEM_BLACK;
3454}
3455
3456
3457/* (x) (y)
3458 / \ / \
3459 a (y) ===> (x) c
3460 / \ / \
3461 b c a b */
3462
3463static void
3464mem_rotate_left (x)
3465 struct mem_node *x;
3466{
3467 struct mem_node *y;
3468
3469 /* Turn y's left sub-tree into x's right sub-tree. */
3470 y = x->right;
3471 x->right = y->left;
3472 if (y->left != MEM_NIL)
3473 y->left->parent = x;
3474
3475 /* Y's parent was x's parent. */
3476 if (y != MEM_NIL)
3477 y->parent = x->parent;
3478
3479 /* Get the parent to point to y instead of x. */
3480 if (x->parent)
3481 {
3482 if (x == x->parent->left)
3483 x->parent->left = y;
3484 else
3485 x->parent->right = y;
3486 }
3487 else
3488 mem_root = y;
3489
3490 /* Put x on y's left. */
3491 y->left = x;
3492 if (x != MEM_NIL)
3493 x->parent = y;
3494}
3495
3496
3497/* (x) (Y)
3498 / \ / \
3499 (y) c ===> a (x)
3500 / \ / \
3501 a b b c */
3502
3503static void
3504mem_rotate_right (x)
3505 struct mem_node *x;
3506{
3507 struct mem_node *y = x->left;
3508
3509 x->left = y->right;
3510 if (y->right != MEM_NIL)
3511 y->right->parent = x;
3512
3513 if (y != MEM_NIL)
3514 y->parent = x->parent;
3515 if (x->parent)
3516 {
3517 if (x == x->parent->right)
3518 x->parent->right = y;
3519 else
3520 x->parent->left = y;
3521 }
3522 else
3523 mem_root = y;
3524
3525 y->right = x;
3526 if (x != MEM_NIL)
3527 x->parent = y;
3528}
3529
3530
3531/* Delete node Z from the tree. If Z is null or MEM_NIL, do nothing. */
3532
3533static void
3534mem_delete (z)
3535 struct mem_node *z;
3536{
3537 struct mem_node *x, *y;
3538
3539 if (!z || z == MEM_NIL)
3540 return;
3541
3542 if (z->left == MEM_NIL || z->right == MEM_NIL)
3543 y = z;
3544 else
3545 {
3546 y = z->right;
3547 while (y->left != MEM_NIL)
3548 y = y->left;
3549 }
3550
3551 if (y->left != MEM_NIL)
3552 x = y->left;
3553 else
3554 x = y->right;
3555
3556 x->parent = y->parent;
3557 if (y->parent)
3558 {
3559 if (y == y->parent->left)
3560 y->parent->left = x;
3561 else
3562 y->parent->right = x;
3563 }
3564 else
3565 mem_root = x;
3566
3567 if (y != z)
3568 {
3569 z->start = y->start;
3570 z->end = y->end;
3571 z->type = y->type;
3572 }
3573
3574 if (y->color == MEM_BLACK)
3575 mem_delete_fixup (x);
3576
3577#ifdef GC_MALLOC_CHECK
3578 _free_internal (y);
3579#else
3580 xfree (y);
3581#endif
3582}
3583
3584
3585/* Re-establish the red-black properties of the tree, after a
3586 deletion. */
3587
3588static void
3589mem_delete_fixup (x)
3590 struct mem_node *x;
3591{
3592 while (x != mem_root && x->color == MEM_BLACK)
3593 {
3594 if (x == x->parent->left)
3595 {
3596 struct mem_node *w = x->parent->right;
3597
3598 if (w->color == MEM_RED)
3599 {
3600 w->color = MEM_BLACK;
3601 x->parent->color = MEM_RED;
3602 mem_rotate_left (x->parent);
3603 w = x->parent->right;
3604 }
3605
3606 if (w->left->color == MEM_BLACK && w->right->color == MEM_BLACK)
3607 {
3608 w->color = MEM_RED;
3609 x = x->parent;
3610 }
3611 else
3612 {
3613 if (w->right->color == MEM_BLACK)
3614 {
3615 w->left->color = MEM_BLACK;
3616 w->color = MEM_RED;
3617 mem_rotate_right (w);
3618 w = x->parent->right;
3619 }
3620 w->color = x->parent->color;
3621 x->parent->color = MEM_BLACK;
3622 w->right->color = MEM_BLACK;
3623 mem_rotate_left (x->parent);
3624 x = mem_root;
3625 }
3626 }
3627 else
3628 {
3629 struct mem_node *w = x->parent->left;
3630
3631 if (w->color == MEM_RED)
3632 {
3633 w->color = MEM_BLACK;
3634 x->parent->color = MEM_RED;
3635 mem_rotate_right (x->parent);
3636 w = x->parent->left;
3637 }
3638
3639 if (w->right->color == MEM_BLACK && w->left->color == MEM_BLACK)
3640 {
3641 w->color = MEM_RED;
3642 x = x->parent;
3643 }
3644 else
3645 {
3646 if (w->left->color == MEM_BLACK)
3647 {
3648 w->right->color = MEM_BLACK;
3649 w->color = MEM_RED;
3650 mem_rotate_left (w);
3651 w = x->parent->left;
3652 }
3653
3654 w->color = x->parent->color;
3655 x->parent->color = MEM_BLACK;
3656 w->left->color = MEM_BLACK;
3657 mem_rotate_right (x->parent);
3658 x = mem_root;
3659 }
3660 }
3661 }
3662
3663 x->color = MEM_BLACK;
3664}
3665
3666
3667/* Value is non-zero if P is a pointer to a live Lisp string on
3668 the heap. M is a pointer to the mem_block for P. */
3669
3670static INLINE int
3671live_string_p (m, p)
3672 struct mem_node *m;
3673 void *p;
3674{
3675 if (m->type == MEM_TYPE_STRING)
3676 {
3677 struct string_block *b = (struct string_block *) m->start;
3678 int offset = (char *) p - (char *) &b->strings[0];
3679
3680 /* P must point to the start of a Lisp_String structure, and it
3681 must not be on the free-list. */
3682 return (offset >= 0
3683 && offset % sizeof b->strings[0] == 0
3684 && offset < (STRING_BLOCK_SIZE * sizeof b->strings[0])
3685 && ((struct Lisp_String *) p)->data != NULL);
3686 }
3687 else
3688 return 0;
3689}
3690
3691
3692/* Value is non-zero if P is a pointer to a live Lisp cons on
3693 the heap. M is a pointer to the mem_block for P. */
3694
3695static INLINE int
3696live_cons_p (m, p)
3697 struct mem_node *m;
3698 void *p;
3699{
3700 if (m->type == MEM_TYPE_CONS)
3701 {
3702 struct cons_block *b = (struct cons_block *) m->start;
3703 int offset = (char *) p - (char *) &b->conses[0];
3704
3705 /* P must point to the start of a Lisp_Cons, not be
3706 one of the unused cells in the current cons block,
3707 and not be on the free-list. */
3708 return (offset >= 0
3709 && offset % sizeof b->conses[0] == 0
3710 && offset < (CONS_BLOCK_SIZE * sizeof b->conses[0])
3711 && (b != cons_block
3712 || offset / sizeof b->conses[0] < cons_block_index)
3713 && !EQ (((struct Lisp_Cons *) p)->car, Vdead));
3714 }
3715 else
3716 return 0;
3717}
3718
3719
3720/* Value is non-zero if P is a pointer to a live Lisp symbol on
3721 the heap. M is a pointer to the mem_block for P. */
3722
3723static INLINE int
3724live_symbol_p (m, p)
3725 struct mem_node *m;
3726 void *p;
3727{
3728 if (m->type == MEM_TYPE_SYMBOL)
3729 {
3730 struct symbol_block *b = (struct symbol_block *) m->start;
3731 int offset = (char *) p - (char *) &b->symbols[0];
3732
3733 /* P must point to the start of a Lisp_Symbol, not be
3734 one of the unused cells in the current symbol block,
3735 and not be on the free-list. */
3736 return (offset >= 0
3737 && offset % sizeof b->symbols[0] == 0
3738 && offset < (SYMBOL_BLOCK_SIZE * sizeof b->symbols[0])
3739 && (b != symbol_block
3740 || offset / sizeof b->symbols[0] < symbol_block_index)
3741 && !EQ (((struct Lisp_Symbol *) p)->function, Vdead));
3742 }
3743 else
3744 return 0;
3745}
3746
3747
3748/* Value is non-zero if P is a pointer to a live Lisp float on
3749 the heap. M is a pointer to the mem_block for P. */
3750
3751static INLINE int
3752live_float_p (m, p)
3753 struct mem_node *m;
3754 void *p;
3755{
3756 if (m->type == MEM_TYPE_FLOAT)
3757 {
3758 struct float_block *b = (struct float_block *) m->start;
3759 int offset = (char *) p - (char *) &b->floats[0];
3760
3761 /* P must point to the start of a Lisp_Float and not be
3762 one of the unused cells in the current float block. */
3763 return (offset >= 0
3764 && offset % sizeof b->floats[0] == 0
3765 && offset < (FLOAT_BLOCK_SIZE * sizeof b->floats[0])
3766 && (b != float_block
3767 || offset / sizeof b->floats[0] < float_block_index));
3768 }
3769 else
3770 return 0;
3771}
3772
3773
3774/* Value is non-zero if P is a pointer to a live Lisp Misc on
3775 the heap. M is a pointer to the mem_block for P. */
3776
3777static INLINE int
3778live_misc_p (m, p)
3779 struct mem_node *m;
3780 void *p;
3781{
3782 if (m->type == MEM_TYPE_MISC)
3783 {
3784 struct marker_block *b = (struct marker_block *) m->start;
3785 int offset = (char *) p - (char *) &b->markers[0];
3786
3787 /* P must point to the start of a Lisp_Misc, not be
3788 one of the unused cells in the current misc block,
3789 and not be on the free-list. */
3790 return (offset >= 0
3791 && offset % sizeof b->markers[0] == 0
3792 && offset < (MARKER_BLOCK_SIZE * sizeof b->markers[0])
3793 && (b != marker_block
3794 || offset / sizeof b->markers[0] < marker_block_index)
3795 && ((union Lisp_Misc *) p)->u_marker.type != Lisp_Misc_Free);
3796 }
3797 else
3798 return 0;
3799}
3800
3801
3802/* Value is non-zero if P is a pointer to a live vector-like object.
3803 M is a pointer to the mem_block for P. */
3804
3805static INLINE int
3806live_vector_p (m, p)
3807 struct mem_node *m;
3808 void *p;
3809{
3810 return (p == m->start
3811 && m->type >= MEM_TYPE_VECTOR
3812 && m->type <= MEM_TYPE_WINDOW);
3813}
3814
3815
3816/* Value is non-zero if P is a pointer to a live buffer. M is a
3817 pointer to the mem_block for P. */
3818
3819static INLINE int
3820live_buffer_p (m, p)
3821 struct mem_node *m;
3822 void *p;
3823{
3824 /* P must point to the start of the block, and the buffer
3825 must not have been killed. */
3826 return (m->type == MEM_TYPE_BUFFER
3827 && p == m->start
3828 && !NILP (((struct buffer *) p)->name));
3829}
3830
3831#endif /* GC_MARK_STACK || defined GC_MALLOC_CHECK */
3832
3833#if GC_MARK_STACK
3834
3835#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3836
3837/* Array of objects that are kept alive because the C stack contains
3838 a pattern that looks like a reference to them . */
3839
3840#define MAX_ZOMBIES 10
3841static Lisp_Object zombies[MAX_ZOMBIES];
3842
3843/* Number of zombie objects. */
3844
3845static int nzombies;
3846
3847/* Number of garbage collections. */
3848
3849static int ngcs;
3850
3851/* Average percentage of zombies per collection. */
3852
3853static double avg_zombies;
3854
3855/* Max. number of live and zombie objects. */
3856
3857static int max_live, max_zombies;
3858
3859/* Average number of live objects per GC. */
3860
3861static double avg_live;
3862
3863DEFUN ("gc-status", Fgc_status, Sgc_status, 0, 0, "",
3864 doc: /* Show information about live and zombie objects. */)
3865 ()
3866{
3867 Lisp_Object args[8], zombie_list = Qnil;
3868 int i;
3869 for (i = 0; i < nzombies; i++)
3870 zombie_list = Fcons (zombies[i], zombie_list);
3871 args[0] = build_string ("%d GCs, avg live/zombies = %.2f/%.2f (%f%%), max %d/%d\nzombies: %S");
3872 args[1] = make_number (ngcs);
3873 args[2] = make_float (avg_live);
3874 args[3] = make_float (avg_zombies);
3875 args[4] = make_float (avg_zombies / avg_live / 100);
3876 args[5] = make_number (max_live);
3877 args[6] = make_number (max_zombies);
3878 args[7] = zombie_list;
3879 return Fmessage (8, args);
3880}
3881
3882#endif /* GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES */
3883
3884
3885/* Mark OBJ if we can prove it's a Lisp_Object. */
3886
3887static INLINE void
3888mark_maybe_object (obj)
3889 Lisp_Object obj;
3890{
3891 void *po = (void *) XPNTR (obj);
3892 struct mem_node *m = mem_find (po);
3893
3894 if (m != MEM_NIL)
3895 {
3896 int mark_p = 0;
3897
3898 switch (XGCTYPE (obj))
3899 {
3900 case Lisp_String:
3901 mark_p = (live_string_p (m, po)
3902 && !STRING_MARKED_P ((struct Lisp_String *) po));
3903 break;
3904
3905 case Lisp_Cons:
3906 mark_p = (live_cons_p (m, po) && !CONS_MARKED_P (XCONS (obj)));
3907 break;
3908
3909 case Lisp_Symbol:
3910 mark_p = (live_symbol_p (m, po) && !XSYMBOL (obj)->gcmarkbit);
3911 break;
3912
3913 case Lisp_Float:
3914 mark_p = (live_float_p (m, po) && !FLOAT_MARKED_P (XFLOAT (obj)));
3915 break;
3916
3917 case Lisp_Vectorlike:
3918 /* Note: can't check GC_BUFFERP before we know it's a
3919 buffer because checking that dereferences the pointer
3920 PO which might point anywhere. */
3921 if (live_vector_p (m, po))
3922 mark_p = !GC_SUBRP (obj) && !VECTOR_MARKED_P (XVECTOR (obj));
3923 else if (live_buffer_p (m, po))
3924 mark_p = GC_BUFFERP (obj) && !VECTOR_MARKED_P (XBUFFER (obj));
3925 break;
3926
3927 case Lisp_Misc:
3928 mark_p = (live_misc_p (m, po) && !XMARKER (obj)->gcmarkbit);
3929 break;
3930
3931 case Lisp_Int:
3932 case Lisp_Type_Limit:
3933 break;
3934 }
3935
3936 if (mark_p)
3937 {
3938#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
3939 if (nzombies < MAX_ZOMBIES)
3940 zombies[nzombies] = obj;
3941 ++nzombies;
3942#endif
3943 mark_object (obj);
3944 }
3945 }
3946}
3947
3948
3949/* If P points to Lisp data, mark that as live if it isn't already
3950 marked. */
3951
3952static INLINE void
3953mark_maybe_pointer (p)
3954 void *p;
3955{
3956 struct mem_node *m;
3957
3958 /* Quickly rule out some values which can't point to Lisp data. We
3959 assume that Lisp data is aligned on even addresses. */
3960 if ((EMACS_INT) p & 1)
3961 return;
3962
3963 m = mem_find (p);
3964 if (m != MEM_NIL)
3965 {
3966 Lisp_Object obj = Qnil;
3967
3968 switch (m->type)
3969 {
3970 case MEM_TYPE_NON_LISP:
3971 /* Nothing to do; not a pointer to Lisp memory. */
3972 break;
3973
3974 case MEM_TYPE_BUFFER:
3975 if (live_buffer_p (m, p) && !VECTOR_MARKED_P((struct buffer *)p))
3976 XSETVECTOR (obj, p);
3977 break;
3978
3979 case MEM_TYPE_CONS:
3980 if (live_cons_p (m, p) && !CONS_MARKED_P ((struct Lisp_Cons *) p))
3981 XSETCONS (obj, p);
3982 break;
3983
3984 case MEM_TYPE_STRING:
3985 if (live_string_p (m, p)
3986 && !STRING_MARKED_P ((struct Lisp_String *) p))
3987 XSETSTRING (obj, p);
3988 break;
3989
3990 case MEM_TYPE_MISC:
3991 if (live_misc_p (m, p) && !((struct Lisp_Free *) p)->gcmarkbit)
3992 XSETMISC (obj, p);
3993 break;
3994
3995 case MEM_TYPE_SYMBOL:
3996 if (live_symbol_p (m, p) && !((struct Lisp_Symbol *) p)->gcmarkbit)
3997 XSETSYMBOL (obj, p);
3998 break;
3999
4000 case MEM_TYPE_FLOAT:
4001 if (live_float_p (m, p) && !FLOAT_MARKED_P (p))
4002 XSETFLOAT (obj, p);
4003 break;
4004
4005 case MEM_TYPE_VECTOR:
4006 case MEM_TYPE_PROCESS:
4007 case MEM_TYPE_HASH_TABLE:
4008 case MEM_TYPE_FRAME:
4009 case MEM_TYPE_WINDOW:
4010 if (live_vector_p (m, p))
4011 {
4012 Lisp_Object tem;
4013 XSETVECTOR (tem, p);
4014 if (!GC_SUBRP (tem) && !VECTOR_MARKED_P (XVECTOR (tem)))
4015 obj = tem;
4016 }
4017 break;
4018
4019 default:
4020 abort ();
4021 }
4022
4023 if (!GC_NILP (obj))
4024 mark_object (obj);
4025 }
4026}
4027
4028
4029/* Mark Lisp objects referenced from the address range START..END. */
4030
4031static void
4032mark_memory (start, end)
4033 void *start, *end;
4034{
4035 Lisp_Object *p;
4036 void **pp;
4037
4038#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
4039 nzombies = 0;
4040#endif
4041
4042 /* Make START the pointer to the start of the memory region,
4043 if it isn't already. */
4044 if (end < start)
4045 {
4046 void *tem = start;
4047 start = end;
4048 end = tem;
4049 }
4050
4051 /* Mark Lisp_Objects. */
4052 for (p = (Lisp_Object *) start; (void *) p < end; ++p)
4053 mark_maybe_object (*p);
4054
4055 /* Mark Lisp data pointed to. This is necessary because, in some
4056 situations, the C compiler optimizes Lisp objects away, so that
4057 only a pointer to them remains. Example:
4058
4059 DEFUN ("testme", Ftestme, Stestme, 0, 0, 0, "")
4060 ()
4061 {
4062 Lisp_Object obj = build_string ("test");
4063 struct Lisp_String *s = XSTRING (obj);
4064 Fgarbage_collect ();
4065 fprintf (stderr, "test `%s'\n", s->data);
4066 return Qnil;
4067 }
4068
4069 Here, `obj' isn't really used, and the compiler optimizes it
4070 away. The only reference to the life string is through the
4071 pointer `s'. */
4072
4073 for (pp = (void **) start; (void *) pp < end; ++pp)
4074 mark_maybe_pointer (*pp);
4075}
4076
4077/* setjmp will work with GCC unless NON_SAVING_SETJMP is defined in
4078 the GCC system configuration. In gcc 3.2, the only systems for
4079 which this is so are i386-sco5 non-ELF, i386-sysv3 (maybe included
4080 by others?) and ns32k-pc532-min. */
4081
4082#if !defined GC_SAVE_REGISTERS_ON_STACK && !defined GC_SETJMP_WORKS
4083
4084static int setjmp_tested_p, longjmps_done;
4085
4086#define SETJMP_WILL_LIKELY_WORK "\
4087\n\
4088Emacs garbage collector has been changed to use conservative stack\n\
4089marking. Emacs has determined that the method it uses to do the\n\
4090marking will likely work on your system, but this isn't sure.\n\
4091\n\
4092If you are a system-programmer, or can get the help of a local wizard\n\
4093who is, please take a look at the function mark_stack in alloc.c, and\n\
4094verify that the methods used are appropriate for your system.\n\
4095\n\
4096Please mail the result to <emacs-devel@gnu.org>.\n\
4097"
4098
4099#define SETJMP_WILL_NOT_WORK "\
4100\n\
4101Emacs garbage collector has been changed to use conservative stack\n\
4102marking. Emacs has determined that the default method it uses to do the\n\
4103marking will not work on your system. We will need a system-dependent\n\
4104solution for your system.\n\
4105\n\
4106Please take a look at the function mark_stack in alloc.c, and\n\
4107try to find a way to make it work on your system.\n\
4108\n\
4109Note that you may get false negatives, depending on the compiler.\n\
4110In particular, you need to use -O with GCC for this test.\n\
4111\n\
4112Please mail the result to <emacs-devel@gnu.org>.\n\
4113"
4114
4115
4116/* Perform a quick check if it looks like setjmp saves registers in a
4117 jmp_buf. Print a message to stderr saying so. When this test
4118 succeeds, this is _not_ a proof that setjmp is sufficient for
4119 conservative stack marking. Only the sources or a disassembly
4120 can prove that. */
4121
4122static void
4123test_setjmp ()
4124{
4125 char buf[10];
4126 register int x;
4127 jmp_buf jbuf;
4128 int result = 0;
4129
4130 /* Arrange for X to be put in a register. */
4131 sprintf (buf, "1");
4132 x = strlen (buf);
4133 x = 2 * x - 1;
4134
4135 setjmp (jbuf);
4136 if (longjmps_done == 1)
4137 {
4138 /* Came here after the longjmp at the end of the function.
4139
4140 If x == 1, the longjmp has restored the register to its
4141 value before the setjmp, and we can hope that setjmp
4142 saves all such registers in the jmp_buf, although that
4143 isn't sure.
4144
4145 For other values of X, either something really strange is
4146 taking place, or the setjmp just didn't save the register. */
4147
4148 if (x == 1)
4149 fprintf (stderr, SETJMP_WILL_LIKELY_WORK);
4150 else
4151 {
4152 fprintf (stderr, SETJMP_WILL_NOT_WORK);
4153 exit (1);
4154 }
4155 }
4156
4157 ++longjmps_done;
4158 x = 2;
4159 if (longjmps_done == 1)
4160 longjmp (jbuf, 1);
4161}
4162
4163#endif /* not GC_SAVE_REGISTERS_ON_STACK && not GC_SETJMP_WORKS */
4164
4165
4166#if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
4167
4168/* Abort if anything GCPRO'd doesn't survive the GC. */
4169
4170static void
4171check_gcpros ()
4172{
4173 struct gcpro *p;
4174 int i;
4175
4176 for (p = gcprolist; p; p = p->next)
4177 for (i = 0; i < p->nvars; ++i)
4178 if (!survives_gc_p (p->var[i]))
4179 /* FIXME: It's not necessarily a bug. It might just be that the
4180 GCPRO is unnecessary or should release the object sooner. */
4181 abort ();
4182}
4183
4184#elif GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
4185
4186static void
4187dump_zombies ()
4188{
4189 int i;
4190
4191 fprintf (stderr, "\nZombies kept alive = %d:\n", nzombies);
4192 for (i = 0; i < min (MAX_ZOMBIES, nzombies); ++i)
4193 {
4194 fprintf (stderr, " %d = ", i);
4195 debug_print (zombies[i]);
4196 }
4197}
4198
4199#endif /* GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES */
4200
4201
4202/* Mark live Lisp objects on the C stack.
4203
4204 There are several system-dependent problems to consider when
4205 porting this to new architectures:
4206
4207 Processor Registers
4208
4209 We have to mark Lisp objects in CPU registers that can hold local
4210 variables or are used to pass parameters.
4211
4212 If GC_SAVE_REGISTERS_ON_STACK is defined, it should expand to
4213 something that either saves relevant registers on the stack, or
4214 calls mark_maybe_object passing it each register's contents.
4215
4216 If GC_SAVE_REGISTERS_ON_STACK is not defined, the current
4217 implementation assumes that calling setjmp saves registers we need
4218 to see in a jmp_buf which itself lies on the stack. This doesn't
4219 have to be true! It must be verified for each system, possibly
4220 by taking a look at the source code of setjmp.
4221
4222 Stack Layout
4223
4224 Architectures differ in the way their processor stack is organized.
4225 For example, the stack might look like this
4226
4227 +----------------+
4228 | Lisp_Object | size = 4
4229 +----------------+
4230 | something else | size = 2
4231 +----------------+
4232 | Lisp_Object | size = 4
4233 +----------------+
4234 | ... |
4235
4236 In such a case, not every Lisp_Object will be aligned equally. To
4237 find all Lisp_Object on the stack it won't be sufficient to walk
4238 the stack in steps of 4 bytes. Instead, two passes will be
4239 necessary, one starting at the start of the stack, and a second
4240 pass starting at the start of the stack + 2. Likewise, if the
4241 minimal alignment of Lisp_Objects on the stack is 1, four passes
4242 would be necessary, each one starting with one byte more offset
4243 from the stack start.
4244
4245 The current code assumes by default that Lisp_Objects are aligned
4246 equally on the stack. */
4247
4248static void
4249mark_stack ()
4250{
4251 int i;
4252 jmp_buf j;
4253 volatile int stack_grows_down_p = (char *) &j > (char *) stack_base;
4254 void *end;
4255
4256 /* This trick flushes the register windows so that all the state of
4257 the process is contained in the stack. */
4258 /* Fixme: Code in the Boehm GC suggests flushing (with `flushrs') is
4259 needed on ia64 too. See mach_dep.c, where it also says inline
4260 assembler doesn't work with relevant proprietary compilers. */
4261#ifdef sparc
4262 asm ("ta 3");
4263#endif
4264
4265 /* Save registers that we need to see on the stack. We need to see
4266 registers used to hold register variables and registers used to
4267 pass parameters. */
4268#ifdef GC_SAVE_REGISTERS_ON_STACK
4269 GC_SAVE_REGISTERS_ON_STACK (end);
4270#else /* not GC_SAVE_REGISTERS_ON_STACK */
4271
4272#ifndef GC_SETJMP_WORKS /* If it hasn't been checked yet that
4273 setjmp will definitely work, test it
4274 and print a message with the result
4275 of the test. */
4276 if (!setjmp_tested_p)
4277 {
4278 setjmp_tested_p = 1;
4279 test_setjmp ();
4280 }
4281#endif /* GC_SETJMP_WORKS */
4282
4283 setjmp (j);
4284 end = stack_grows_down_p ? (char *) &j + sizeof j : (char *) &j;
4285#endif /* not GC_SAVE_REGISTERS_ON_STACK */
4286
4287 /* This assumes that the stack is a contiguous region in memory. If
4288 that's not the case, something has to be done here to iterate
4289 over the stack segments. */
4290#ifndef GC_LISP_OBJECT_ALIGNMENT
4291#ifdef __GNUC__
4292#define GC_LISP_OBJECT_ALIGNMENT __alignof__ (Lisp_Object)
4293#else
4294#define GC_LISP_OBJECT_ALIGNMENT sizeof (Lisp_Object)
4295#endif
4296#endif
4297 for (i = 0; i < sizeof (Lisp_Object); i += GC_LISP_OBJECT_ALIGNMENT)
4298 mark_memory ((char *) stack_base + i, end);
4299 /* Allow for marking a secondary stack, like the register stack on the
4300 ia64. */
4301#ifdef GC_MARK_SECONDARY_STACK
4302 GC_MARK_SECONDARY_STACK ();
4303#endif
4304
4305#if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
4306 check_gcpros ();
4307#endif
4308}
4309
4310
4311#endif /* GC_MARK_STACK != 0 */
4312
4313
4314\f
4315/***********************************************************************
4316 Pure Storage Management
4317 ***********************************************************************/
4318
4319/* Allocate room for SIZE bytes from pure Lisp storage and return a
4320 pointer to it. TYPE is the Lisp type for which the memory is
4321 allocated. TYPE < 0 means it's not used for a Lisp object.
4322
4323 If store_pure_type_info is set and TYPE is >= 0, the type of
4324 the allocated object is recorded in pure_types. */
4325
4326static POINTER_TYPE *
4327pure_alloc (size, type)
4328 size_t size;
4329 int type;
4330{
4331 POINTER_TYPE *result;
4332#ifdef USE_LSB_TAG
4333 size_t alignment = (1 << GCTYPEBITS);
4334#else
4335 size_t alignment = sizeof (EMACS_INT);
4336
4337 /* Give Lisp_Floats an extra alignment. */
4338 if (type == Lisp_Float)
4339 {
4340#if defined __GNUC__ && __GNUC__ >= 2
4341 alignment = __alignof (struct Lisp_Float);
4342#else
4343 alignment = sizeof (struct Lisp_Float);
4344#endif
4345 }
4346#endif
4347
4348 again:
4349 result = ALIGN (purebeg + pure_bytes_used, alignment);
4350 pure_bytes_used = ((char *)result - (char *)purebeg) + size;
4351
4352 if (pure_bytes_used <= pure_size)
4353 return result;
4354
4355 /* Don't allocate a large amount here,
4356 because it might get mmap'd and then its address
4357 might not be usable. */
4358 purebeg = (char *) xmalloc (10000);
4359 pure_size = 10000;
4360 pure_bytes_used_before_overflow += pure_bytes_used - size;
4361 pure_bytes_used = 0;
4362 goto again;
4363}
4364
4365
4366/* Print a warning if PURESIZE is too small. */
4367
4368void
4369check_pure_size ()
4370{
4371 if (pure_bytes_used_before_overflow)
4372 message ("Pure Lisp storage overflow (approx. %d bytes needed)",
4373 (int) (pure_bytes_used + pure_bytes_used_before_overflow));
4374}
4375
4376
4377/* Return a string allocated in pure space. DATA is a buffer holding
4378 NCHARS characters, and NBYTES bytes of string data. MULTIBYTE
4379 non-zero means make the result string multibyte.
4380
4381 Must get an error if pure storage is full, since if it cannot hold
4382 a large string it may be able to hold conses that point to that
4383 string; then the string is not protected from gc. */
4384
4385Lisp_Object
4386make_pure_string (data, nchars, nbytes, multibyte)
4387 char *data;
4388 int nchars, nbytes;
4389 int multibyte;
4390{
4391 Lisp_Object string;
4392 struct Lisp_String *s;
4393
4394 s = (struct Lisp_String *) pure_alloc (sizeof *s, Lisp_String);
4395 s->data = (unsigned char *) pure_alloc (nbytes + 1, -1);
4396 s->size = nchars;
4397 s->size_byte = multibyte ? nbytes : -1;
4398 bcopy (data, s->data, nbytes);
4399 s->data[nbytes] = '\0';
4400 s->intervals = NULL_INTERVAL;
4401 XSETSTRING (string, s);
4402 return string;
4403}
4404
4405
4406/* Return a cons allocated from pure space. Give it pure copies
4407 of CAR as car and CDR as cdr. */
4408
4409Lisp_Object
4410pure_cons (car, cdr)
4411 Lisp_Object car, cdr;
4412{
4413 register Lisp_Object new;
4414 struct Lisp_Cons *p;
4415
4416 p = (struct Lisp_Cons *) pure_alloc (sizeof *p, Lisp_Cons);
4417 XSETCONS (new, p);
4418 XSETCAR (new, Fpurecopy (car));
4419 XSETCDR (new, Fpurecopy (cdr));
4420 return new;
4421}
4422
4423
4424/* Value is a float object with value NUM allocated from pure space. */
4425
4426Lisp_Object
4427make_pure_float (num)
4428 double num;
4429{
4430 register Lisp_Object new;
4431 struct Lisp_Float *p;
4432
4433 p = (struct Lisp_Float *) pure_alloc (sizeof *p, Lisp_Float);
4434 XSETFLOAT (new, p);
4435 XFLOAT_DATA (new) = num;
4436 return new;
4437}
4438
4439
4440/* Return a vector with room for LEN Lisp_Objects allocated from
4441 pure space. */
4442
4443Lisp_Object
4444make_pure_vector (len)
4445 EMACS_INT len;
4446{
4447 Lisp_Object new;
4448 struct Lisp_Vector *p;
4449 size_t size = sizeof *p + (len - 1) * sizeof (Lisp_Object);
4450
4451 p = (struct Lisp_Vector *) pure_alloc (size, Lisp_Vectorlike);
4452 XSETVECTOR (new, p);
4453 XVECTOR (new)->size = len;
4454 return new;
4455}
4456
4457
4458DEFUN ("purecopy", Fpurecopy, Spurecopy, 1, 1, 0,
4459 doc: /* Make a copy of OBJECT in pure storage.
4460Recursively copies contents of vectors and cons cells.
4461Does not copy symbols. Copies strings without text properties. */)
4462 (obj)
4463 register Lisp_Object obj;
4464{
4465 if (NILP (Vpurify_flag))
4466 return obj;
4467
4468 if (PURE_POINTER_P (XPNTR (obj)))
4469 return obj;
4470
4471 if (CONSP (obj))
4472 return pure_cons (XCAR (obj), XCDR (obj));
4473 else if (FLOATP (obj))
4474 return make_pure_float (XFLOAT_DATA (obj));
4475 else if (STRINGP (obj))
4476 return make_pure_string (SDATA (obj), SCHARS (obj),
4477 SBYTES (obj),
4478 STRING_MULTIBYTE (obj));
4479 else if (COMPILEDP (obj) || VECTORP (obj))
4480 {
4481 register struct Lisp_Vector *vec;
4482 register int i;
4483 EMACS_INT size;
4484
4485 size = XVECTOR (obj)->size;
4486 if (size & PSEUDOVECTOR_FLAG)
4487 size &= PSEUDOVECTOR_SIZE_MASK;
4488 vec = XVECTOR (make_pure_vector (size));
4489 for (i = 0; i < size; i++)
4490 vec->contents[i] = Fpurecopy (XVECTOR (obj)->contents[i]);
4491 if (COMPILEDP (obj))
4492 XSETCOMPILED (obj, vec);
4493 else
4494 XSETVECTOR (obj, vec);
4495 return obj;
4496 }
4497 else if (MARKERP (obj))
4498 error ("Attempt to copy a marker to pure storage");
4499
4500 return obj;
4501}
4502
4503
4504\f
4505/***********************************************************************
4506 Protection from GC
4507 ***********************************************************************/
4508
4509/* Put an entry in staticvec, pointing at the variable with address
4510 VARADDRESS. */
4511
4512void
4513staticpro (varaddress)
4514 Lisp_Object *varaddress;
4515{
4516 staticvec[staticidx++] = varaddress;
4517 if (staticidx >= NSTATICS)
4518 abort ();
4519}
4520
4521struct catchtag
4522{
4523 Lisp_Object tag;
4524 Lisp_Object val;
4525 struct catchtag *next;
4526};
4527
4528\f
4529/***********************************************************************
4530 Protection from GC
4531 ***********************************************************************/
4532
4533/* Temporarily prevent garbage collection. */
4534
4535int
4536inhibit_garbage_collection ()
4537{
4538 int count = SPECPDL_INDEX ();
4539 int nbits = min (VALBITS, BITS_PER_INT);
4540
4541 specbind (Qgc_cons_threshold, make_number (((EMACS_INT) 1 << (nbits - 1)) - 1));
4542 return count;
4543}
4544
4545
4546DEFUN ("garbage-collect", Fgarbage_collect, Sgarbage_collect, 0, 0, "",
4547 doc: /* Reclaim storage for Lisp objects no longer needed.
4548Garbage collection happens automatically if you cons more than
4549`gc-cons-threshold' bytes of Lisp data since previous garbage collection.
4550`garbage-collect' normally returns a list with info on amount of space in use:
4551 ((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS)
4552 (USED-MARKERS . FREE-MARKERS) USED-STRING-CHARS USED-VECTOR-SLOTS
4553 (USED-FLOATS . FREE-FLOATS) (USED-INTERVALS . FREE-INTERVALS)
4554 (USED-STRINGS . FREE-STRINGS))
4555However, if there was overflow in pure space, `garbage-collect'
4556returns nil, because real GC can't be done. */)
4557 ()
4558{
4559 register struct specbinding *bind;
4560 struct catchtag *catch;
4561 struct handler *handler;
4562 char stack_top_variable;
4563 register int i;
4564 int message_p;
4565 Lisp_Object total[8];
4566 int count = SPECPDL_INDEX ();
4567 EMACS_TIME t1, t2, t3;
4568
4569 if (abort_on_gc)
4570 abort ();
4571
4572 EMACS_GET_TIME (t1);
4573
4574 /* Can't GC if pure storage overflowed because we can't determine
4575 if something is a pure object or not. */
4576 if (pure_bytes_used_before_overflow)
4577 return Qnil;
4578
4579 /* In case user calls debug_print during GC,
4580 don't let that cause a recursive GC. */
4581 consing_since_gc = 0;
4582
4583 /* Save what's currently displayed in the echo area. */
4584 message_p = push_message ();
4585 record_unwind_protect (pop_message_unwind, Qnil);
4586
4587 /* Save a copy of the contents of the stack, for debugging. */
4588#if MAX_SAVE_STACK > 0
4589 if (NILP (Vpurify_flag))
4590 {
4591 i = &stack_top_variable - stack_bottom;
4592 if (i < 0) i = -i;
4593 if (i < MAX_SAVE_STACK)
4594 {
4595 if (stack_copy == 0)
4596 stack_copy = (char *) xmalloc (stack_copy_size = i);
4597 else if (stack_copy_size < i)
4598 stack_copy = (char *) xrealloc (stack_copy, (stack_copy_size = i));
4599 if (stack_copy)
4600 {
4601 if ((EMACS_INT) (&stack_top_variable - stack_bottom) > 0)
4602 bcopy (stack_bottom, stack_copy, i);
4603 else
4604 bcopy (&stack_top_variable, stack_copy, i);
4605 }
4606 }
4607 }
4608#endif /* MAX_SAVE_STACK > 0 */
4609
4610 if (garbage_collection_messages)
4611 message1_nolog ("Garbage collecting...");
4612
4613 BLOCK_INPUT;
4614
4615 shrink_regexp_cache ();
4616
4617 /* Don't keep undo information around forever. */
4618 {
4619 register struct buffer *nextb = all_buffers;
4620
4621 while (nextb)
4622 {
4623 /* If a buffer's undo list is Qt, that means that undo is
4624 turned off in that buffer. Calling truncate_undo_list on
4625 Qt tends to return NULL, which effectively turns undo back on.
4626 So don't call truncate_undo_list if undo_list is Qt. */
4627 if (! EQ (nextb->undo_list, Qt))
4628 nextb->undo_list
4629 = truncate_undo_list (nextb->undo_list, undo_limit,
4630 undo_strong_limit, undo_outer_limit);
4631
4632 /* Shrink buffer gaps, but skip indirect and dead buffers. */
4633 if (nextb->base_buffer == 0 && !NILP (nextb->name))
4634 {
4635 /* If a buffer's gap size is more than 10% of the buffer
4636 size, or larger than 2000 bytes, then shrink it
4637 accordingly. Keep a minimum size of 20 bytes. */
4638 int size = min (2000, max (20, (nextb->text->z_byte / 10)));
4639
4640 if (nextb->text->gap_size > size)
4641 {
4642 struct buffer *save_current = current_buffer;
4643 current_buffer = nextb;
4644 make_gap (-(nextb->text->gap_size - size));
4645 current_buffer = save_current;
4646 }
4647 }
4648
4649 nextb = nextb->next;
4650 }
4651 }
4652
4653 gc_in_progress = 1;
4654
4655 /* clear_marks (); */
4656
4657 /* Mark all the special slots that serve as the roots of accessibility. */
4658
4659 for (i = 0; i < staticidx; i++)
4660 mark_object (*staticvec[i]);
4661
4662 for (bind = specpdl; bind != specpdl_ptr; bind++)
4663 {
4664 mark_object (bind->symbol);
4665 mark_object (bind->old_value);
4666 }
4667 mark_kboards ();
4668
4669#ifdef USE_GTK
4670 {
4671 extern void xg_mark_data ();
4672 xg_mark_data ();
4673 }
4674#endif
4675
4676#if (GC_MARK_STACK == GC_MAKE_GCPROS_NOOPS \
4677 || GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS)
4678 mark_stack ();
4679#else
4680 {
4681 register struct gcpro *tail;
4682 for (tail = gcprolist; tail; tail = tail->next)
4683 for (i = 0; i < tail->nvars; i++)
4684 mark_object (tail->var[i]);
4685 }
4686#endif
4687
4688 mark_byte_stack ();
4689 for (catch = catchlist; catch; catch = catch->next)
4690 {
4691 mark_object (catch->tag);
4692 mark_object (catch->val);
4693 }
4694 for (handler = handlerlist; handler; handler = handler->next)
4695 {
4696 mark_object (handler->handler);
4697 mark_object (handler->var);
4698 }
4699 mark_backtrace ();
4700
4701#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
4702 mark_stack ();
4703#endif
4704
4705 /* Everything is now marked, except for the things that require special
4706 finalization, i.e. the undo_list.
4707 Look thru every buffer's undo list
4708 for elements that update markers that were not marked,
4709 and delete them. */
4710 {
4711 register struct buffer *nextb = all_buffers;
4712
4713 while (nextb)
4714 {
4715 /* If a buffer's undo list is Qt, that means that undo is
4716 turned off in that buffer. Calling truncate_undo_list on
4717 Qt tends to return NULL, which effectively turns undo back on.
4718 So don't call truncate_undo_list if undo_list is Qt. */
4719 if (! EQ (nextb->undo_list, Qt))
4720 {
4721 Lisp_Object tail, prev;
4722 tail = nextb->undo_list;
4723 prev = Qnil;
4724 while (CONSP (tail))
4725 {
4726 if (GC_CONSP (XCAR (tail))
4727 && GC_MARKERP (XCAR (XCAR (tail)))
4728 && !XMARKER (XCAR (XCAR (tail)))->gcmarkbit)
4729 {
4730 if (NILP (prev))
4731 nextb->undo_list = tail = XCDR (tail);
4732 else
4733 {
4734 tail = XCDR (tail);
4735 XSETCDR (prev, tail);
4736 }
4737 }
4738 else
4739 {
4740 prev = tail;
4741 tail = XCDR (tail);
4742 }
4743 }
4744 }
4745 /* Now that we have stripped the elements that need not be in the
4746 undo_list any more, we can finally mark the list. */
4747 mark_object (nextb->undo_list);
4748
4749 nextb = nextb->next;
4750 }
4751 }
4752
4753 gc_sweep ();
4754
4755 /* Clear the mark bits that we set in certain root slots. */
4756
4757 unmark_byte_stack ();
4758 VECTOR_UNMARK (&buffer_defaults);
4759 VECTOR_UNMARK (&buffer_local_symbols);
4760
4761#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES && 0
4762 dump_zombies ();
4763#endif
4764
4765 UNBLOCK_INPUT;
4766
4767 /* clear_marks (); */
4768 gc_in_progress = 0;
4769
4770 consing_since_gc = 0;
4771 if (gc_cons_threshold < 10000)
4772 gc_cons_threshold = 10000;
4773
4774 if (garbage_collection_messages)
4775 {
4776 if (message_p || minibuf_level > 0)
4777 restore_message ();
4778 else
4779 message1_nolog ("Garbage collecting...done");
4780 }
4781
4782 unbind_to (count, Qnil);
4783
4784 total[0] = Fcons (make_number (total_conses),
4785 make_number (total_free_conses));
4786 total[1] = Fcons (make_number (total_symbols),
4787 make_number (total_free_symbols));
4788 total[2] = Fcons (make_number (total_markers),
4789 make_number (total_free_markers));
4790 total[3] = make_number (total_string_size);
4791 total[4] = make_number (total_vector_size);
4792 total[5] = Fcons (make_number (total_floats),
4793 make_number (total_free_floats));
4794 total[6] = Fcons (make_number (total_intervals),
4795 make_number (total_free_intervals));
4796 total[7] = Fcons (make_number (total_strings),
4797 make_number (total_free_strings));
4798
4799#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
4800 {
4801 /* Compute average percentage of zombies. */
4802 double nlive = 0;
4803
4804 for (i = 0; i < 7; ++i)
4805 if (CONSP (total[i]))
4806 nlive += XFASTINT (XCAR (total[i]));
4807
4808 avg_live = (avg_live * ngcs + nlive) / (ngcs + 1);
4809 max_live = max (nlive, max_live);
4810 avg_zombies = (avg_zombies * ngcs + nzombies) / (ngcs + 1);
4811 max_zombies = max (nzombies, max_zombies);
4812 ++ngcs;
4813 }
4814#endif
4815
4816 if (!NILP (Vpost_gc_hook))
4817 {
4818 int count = inhibit_garbage_collection ();
4819 safe_run_hooks (Qpost_gc_hook);
4820 unbind_to (count, Qnil);
4821 }
4822
4823 /* Accumulate statistics. */
4824 EMACS_GET_TIME (t2);
4825 EMACS_SUB_TIME (t3, t2, t1);
4826 if (FLOATP (Vgc_elapsed))
4827 Vgc_elapsed = make_float (XFLOAT_DATA (Vgc_elapsed) +
4828 EMACS_SECS (t3) +
4829 EMACS_USECS (t3) * 1.0e-6);
4830 gcs_done++;
4831
4832 return Flist (sizeof total / sizeof *total, total);
4833}
4834
4835
4836/* Mark Lisp objects in glyph matrix MATRIX. Currently the
4837 only interesting objects referenced from glyphs are strings. */
4838
4839static void
4840mark_glyph_matrix (matrix)
4841 struct glyph_matrix *matrix;
4842{
4843 struct glyph_row *row = matrix->rows;
4844 struct glyph_row *end = row + matrix->nrows;
4845
4846 for (; row < end; ++row)
4847 if (row->enabled_p)
4848 {
4849 int area;
4850 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
4851 {
4852 struct glyph *glyph = row->glyphs[area];
4853 struct glyph *end_glyph = glyph + row->used[area];
4854
4855 for (; glyph < end_glyph; ++glyph)
4856 if (GC_STRINGP (glyph->object)
4857 && !STRING_MARKED_P (XSTRING (glyph->object)))
4858 mark_object (glyph->object);
4859 }
4860 }
4861}
4862
4863
4864/* Mark Lisp faces in the face cache C. */
4865
4866static void
4867mark_face_cache (c)
4868 struct face_cache *c;
4869{
4870 if (c)
4871 {
4872 int i, j;
4873 for (i = 0; i < c->used; ++i)
4874 {
4875 struct face *face = FACE_FROM_ID (c->f, i);
4876
4877 if (face)
4878 {
4879 for (j = 0; j < LFACE_VECTOR_SIZE; ++j)
4880 mark_object (face->lface[j]);
4881 }
4882 }
4883 }
4884}
4885
4886
4887#ifdef HAVE_WINDOW_SYSTEM
4888
4889/* Mark Lisp objects in image IMG. */
4890
4891static void
4892mark_image (img)
4893 struct image *img;
4894{
4895 mark_object (img->spec);
4896
4897 if (!NILP (img->data.lisp_val))
4898 mark_object (img->data.lisp_val);
4899}
4900
4901
4902/* Mark Lisp objects in image cache of frame F. It's done this way so
4903 that we don't have to include xterm.h here. */
4904
4905static void
4906mark_image_cache (f)
4907 struct frame *f;
4908{
4909 forall_images_in_image_cache (f, mark_image);
4910}
4911
4912#endif /* HAVE_X_WINDOWS */
4913
4914
4915\f
4916/* Mark reference to a Lisp_Object.
4917 If the object referred to has not been seen yet, recursively mark
4918 all the references contained in it. */
4919
4920#define LAST_MARKED_SIZE 500
4921Lisp_Object last_marked[LAST_MARKED_SIZE];
4922int last_marked_index;
4923
4924/* For debugging--call abort when we cdr down this many
4925 links of a list, in mark_object. In debugging,
4926 the call to abort will hit a breakpoint.
4927 Normally this is zero and the check never goes off. */
4928int mark_object_loop_halt;
4929
4930void
4931mark_object (arg)
4932 Lisp_Object arg;
4933{
4934 register Lisp_Object obj = arg;
4935#ifdef GC_CHECK_MARKED_OBJECTS
4936 void *po;
4937 struct mem_node *m;
4938#endif
4939 int cdr_count = 0;
4940
4941 loop:
4942
4943 if (PURE_POINTER_P (XPNTR (obj)))
4944 return;
4945
4946 last_marked[last_marked_index++] = obj;
4947 if (last_marked_index == LAST_MARKED_SIZE)
4948 last_marked_index = 0;
4949
4950 /* Perform some sanity checks on the objects marked here. Abort if
4951 we encounter an object we know is bogus. This increases GC time
4952 by ~80%, and requires compilation with GC_MARK_STACK != 0. */
4953#ifdef GC_CHECK_MARKED_OBJECTS
4954
4955 po = (void *) XPNTR (obj);
4956
4957 /* Check that the object pointed to by PO is known to be a Lisp
4958 structure allocated from the heap. */
4959#define CHECK_ALLOCATED() \
4960 do { \
4961 m = mem_find (po); \
4962 if (m == MEM_NIL) \
4963 abort (); \
4964 } while (0)
4965
4966 /* Check that the object pointed to by PO is live, using predicate
4967 function LIVEP. */
4968#define CHECK_LIVE(LIVEP) \
4969 do { \
4970 if (!LIVEP (m, po)) \
4971 abort (); \
4972 } while (0)
4973
4974 /* Check both of the above conditions. */
4975#define CHECK_ALLOCATED_AND_LIVE(LIVEP) \
4976 do { \
4977 CHECK_ALLOCATED (); \
4978 CHECK_LIVE (LIVEP); \
4979 } while (0) \
4980
4981#else /* not GC_CHECK_MARKED_OBJECTS */
4982
4983#define CHECK_ALLOCATED() (void) 0
4984#define CHECK_LIVE(LIVEP) (void) 0
4985#define CHECK_ALLOCATED_AND_LIVE(LIVEP) (void) 0
4986
4987#endif /* not GC_CHECK_MARKED_OBJECTS */
4988
4989 switch (SWITCH_ENUM_CAST (XGCTYPE (obj)))
4990 {
4991 case Lisp_String:
4992 {
4993 register struct Lisp_String *ptr = XSTRING (obj);
4994 CHECK_ALLOCATED_AND_LIVE (live_string_p);
4995 MARK_INTERVAL_TREE (ptr->intervals);
4996 MARK_STRING (ptr);
4997#ifdef GC_CHECK_STRING_BYTES
4998 /* Check that the string size recorded in the string is the
4999 same as the one recorded in the sdata structure. */
5000 CHECK_STRING_BYTES (ptr);
5001#endif /* GC_CHECK_STRING_BYTES */
5002 }
5003 break;
5004
5005 case Lisp_Vectorlike:
5006#ifdef GC_CHECK_MARKED_OBJECTS
5007 m = mem_find (po);
5008 if (m == MEM_NIL && !GC_SUBRP (obj)
5009 && po != &buffer_defaults
5010 && po != &buffer_local_symbols)
5011 abort ();
5012#endif /* GC_CHECK_MARKED_OBJECTS */
5013
5014 if (GC_BUFFERP (obj))
5015 {
5016 if (!VECTOR_MARKED_P (XBUFFER (obj)))
5017 {
5018#ifdef GC_CHECK_MARKED_OBJECTS
5019 if (po != &buffer_defaults && po != &buffer_local_symbols)
5020 {
5021 struct buffer *b;
5022 for (b = all_buffers; b && b != po; b = b->next)
5023 ;
5024 if (b == NULL)
5025 abort ();
5026 }
5027#endif /* GC_CHECK_MARKED_OBJECTS */
5028 mark_buffer (obj);
5029 }
5030 }
5031 else if (GC_SUBRP (obj))
5032 break;
5033 else if (GC_COMPILEDP (obj))
5034 /* We could treat this just like a vector, but it is better to
5035 save the COMPILED_CONSTANTS element for last and avoid
5036 recursion there. */
5037 {
5038 register struct Lisp_Vector *ptr = XVECTOR (obj);
5039 register EMACS_INT size = ptr->size;
5040 register int i;
5041
5042 if (VECTOR_MARKED_P (ptr))
5043 break; /* Already marked */
5044
5045 CHECK_LIVE (live_vector_p);
5046 VECTOR_MARK (ptr); /* Else mark it */
5047 size &= PSEUDOVECTOR_SIZE_MASK;
5048 for (i = 0; i < size; i++) /* and then mark its elements */
5049 {
5050 if (i != COMPILED_CONSTANTS)
5051 mark_object (ptr->contents[i]);
5052 }
5053 obj = ptr->contents[COMPILED_CONSTANTS];
5054 goto loop;
5055 }
5056 else if (GC_FRAMEP (obj))
5057 {
5058 register struct frame *ptr = XFRAME (obj);
5059
5060 if (VECTOR_MARKED_P (ptr)) break; /* Already marked */
5061 VECTOR_MARK (ptr); /* Else mark it */
5062
5063 CHECK_LIVE (live_vector_p);
5064 mark_object (ptr->name);
5065 mark_object (ptr->icon_name);
5066 mark_object (ptr->title);
5067 mark_object (ptr->focus_frame);
5068 mark_object (ptr->selected_window);
5069 mark_object (ptr->minibuffer_window);
5070 mark_object (ptr->param_alist);
5071 mark_object (ptr->scroll_bars);
5072 mark_object (ptr->condemned_scroll_bars);
5073 mark_object (ptr->menu_bar_items);
5074 mark_object (ptr->face_alist);
5075 mark_object (ptr->menu_bar_vector);
5076 mark_object (ptr->buffer_predicate);
5077 mark_object (ptr->buffer_list);
5078 mark_object (ptr->menu_bar_window);
5079 mark_object (ptr->tool_bar_window);
5080 mark_face_cache (ptr->face_cache);
5081#ifdef HAVE_WINDOW_SYSTEM
5082 mark_image_cache (ptr);
5083 mark_object (ptr->tool_bar_items);
5084 mark_object (ptr->desired_tool_bar_string);
5085 mark_object (ptr->current_tool_bar_string);
5086#endif /* HAVE_WINDOW_SYSTEM */
5087 }
5088 else if (GC_BOOL_VECTOR_P (obj))
5089 {
5090 register struct Lisp_Vector *ptr = XVECTOR (obj);
5091
5092 if (VECTOR_MARKED_P (ptr))
5093 break; /* Already marked */
5094 CHECK_LIVE (live_vector_p);
5095 VECTOR_MARK (ptr); /* Else mark it */
5096 }
5097 else if (GC_WINDOWP (obj))
5098 {
5099 register struct Lisp_Vector *ptr = XVECTOR (obj);
5100 struct window *w = XWINDOW (obj);
5101 register int i;
5102
5103 /* Stop if already marked. */
5104 if (VECTOR_MARKED_P (ptr))
5105 break;
5106
5107 /* Mark it. */
5108 CHECK_LIVE (live_vector_p);
5109 VECTOR_MARK (ptr);
5110
5111 /* There is no Lisp data above The member CURRENT_MATRIX in
5112 struct WINDOW. Stop marking when that slot is reached. */
5113 for (i = 0;
5114 (char *) &ptr->contents[i] < (char *) &w->current_matrix;
5115 i++)
5116 mark_object (ptr->contents[i]);
5117
5118 /* Mark glyphs for leaf windows. Marking window matrices is
5119 sufficient because frame matrices use the same glyph
5120 memory. */
5121 if (NILP (w->hchild)
5122 && NILP (w->vchild)
5123 && w->current_matrix)
5124 {
5125 mark_glyph_matrix (w->current_matrix);
5126 mark_glyph_matrix (w->desired_matrix);
5127 }
5128 }
5129 else if (GC_HASH_TABLE_P (obj))
5130 {
5131 struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
5132
5133 /* Stop if already marked. */
5134 if (VECTOR_MARKED_P (h))
5135 break;
5136
5137 /* Mark it. */
5138 CHECK_LIVE (live_vector_p);
5139 VECTOR_MARK (h);
5140
5141 /* Mark contents. */
5142 /* Do not mark next_free or next_weak.
5143 Being in the next_weak chain
5144 should not keep the hash table alive.
5145 No need to mark `count' since it is an integer. */
5146 mark_object (h->test);
5147 mark_object (h->weak);
5148 mark_object (h->rehash_size);
5149 mark_object (h->rehash_threshold);
5150 mark_object (h->hash);
5151 mark_object (h->next);
5152 mark_object (h->index);
5153 mark_object (h->user_hash_function);
5154 mark_object (h->user_cmp_function);
5155
5156 /* If hash table is not weak, mark all keys and values.
5157 For weak tables, mark only the vector. */
5158 if (GC_NILP (h->weak))
5159 mark_object (h->key_and_value);
5160 else
5161 VECTOR_MARK (XVECTOR (h->key_and_value));
5162 }
5163 else
5164 {
5165 register struct Lisp_Vector *ptr = XVECTOR (obj);
5166 register EMACS_INT size = ptr->size;
5167 register int i;
5168
5169 if (VECTOR_MARKED_P (ptr)) break; /* Already marked */
5170 CHECK_LIVE (live_vector_p);
5171 VECTOR_MARK (ptr); /* Else mark it */
5172 if (size & PSEUDOVECTOR_FLAG)
5173 size &= PSEUDOVECTOR_SIZE_MASK;
5174
5175 for (i = 0; i < size; i++) /* and then mark its elements */
5176 mark_object (ptr->contents[i]);
5177 }
5178 break;
5179
5180 case Lisp_Symbol:
5181 {
5182 register struct Lisp_Symbol *ptr = XSYMBOL (obj);
5183 struct Lisp_Symbol *ptrx;
5184
5185 if (ptr->gcmarkbit) break;
5186 CHECK_ALLOCATED_AND_LIVE (live_symbol_p);
5187 ptr->gcmarkbit = 1;
5188 mark_object (ptr->value);
5189 mark_object (ptr->function);
5190 mark_object (ptr->plist);
5191
5192 if (!PURE_POINTER_P (XSTRING (ptr->xname)))
5193 MARK_STRING (XSTRING (ptr->xname));
5194 MARK_INTERVAL_TREE (STRING_INTERVALS (ptr->xname));
5195
5196 /* Note that we do not mark the obarray of the symbol.
5197 It is safe not to do so because nothing accesses that
5198 slot except to check whether it is nil. */
5199 ptr = ptr->next;
5200 if (ptr)
5201 {
5202 ptrx = ptr; /* Use of ptrx avoids compiler bug on Sun */
5203 XSETSYMBOL (obj, ptrx);
5204 goto loop;
5205 }
5206 }
5207 break;
5208
5209 case Lisp_Misc:
5210 CHECK_ALLOCATED_AND_LIVE (live_misc_p);
5211 if (XMARKER (obj)->gcmarkbit)
5212 break;
5213 XMARKER (obj)->gcmarkbit = 1;
5214
5215 switch (XMISCTYPE (obj))
5216 {
5217 case Lisp_Misc_Buffer_Local_Value:
5218 case Lisp_Misc_Some_Buffer_Local_Value:
5219 {
5220 register struct Lisp_Buffer_Local_Value *ptr
5221 = XBUFFER_LOCAL_VALUE (obj);
5222 /* If the cdr is nil, avoid recursion for the car. */
5223 if (EQ (ptr->cdr, Qnil))
5224 {
5225 obj = ptr->realvalue;
5226 goto loop;
5227 }
5228 mark_object (ptr->realvalue);
5229 mark_object (ptr->buffer);
5230 mark_object (ptr->frame);
5231 obj = ptr->cdr;
5232 goto loop;
5233 }
5234
5235 case Lisp_Misc_Marker:
5236 /* DO NOT mark thru the marker's chain.
5237 The buffer's markers chain does not preserve markers from gc;
5238 instead, markers are removed from the chain when freed by gc. */
5239 break;
5240
5241 case Lisp_Misc_Intfwd:
5242 case Lisp_Misc_Boolfwd:
5243 case Lisp_Misc_Objfwd:
5244 case Lisp_Misc_Buffer_Objfwd:
5245 case Lisp_Misc_Kboard_Objfwd:
5246 /* Don't bother with Lisp_Buffer_Objfwd,
5247 since all markable slots in current buffer marked anyway. */
5248 /* Don't need to do Lisp_Objfwd, since the places they point
5249 are protected with staticpro. */
5250 break;
5251
5252 case Lisp_Misc_Save_Value:
5253#if GC_MARK_STACK
5254 {
5255 register struct Lisp_Save_Value *ptr = XSAVE_VALUE (obj);
5256 /* If DOGC is set, POINTER is the address of a memory
5257 area containing INTEGER potential Lisp_Objects. */
5258 if (ptr->dogc)
5259 {
5260 Lisp_Object *p = (Lisp_Object *) ptr->pointer;
5261 int nelt;
5262 for (nelt = ptr->integer; nelt > 0; nelt--, p++)
5263 mark_maybe_object (*p);
5264 }
5265 }
5266#endif
5267 break;
5268
5269 case Lisp_Misc_Overlay:
5270 {
5271 struct Lisp_Overlay *ptr = XOVERLAY (obj);
5272 mark_object (ptr->start);
5273 mark_object (ptr->end);
5274 mark_object (ptr->plist);
5275 if (ptr->next)
5276 {
5277 XSETMISC (obj, ptr->next);
5278 goto loop;
5279 }
5280 }
5281 break;
5282
5283 default:
5284 abort ();
5285 }
5286 break;
5287
5288 case Lisp_Cons:
5289 {
5290 register struct Lisp_Cons *ptr = XCONS (obj);
5291 if (CONS_MARKED_P (ptr)) break;
5292 CHECK_ALLOCATED_AND_LIVE (live_cons_p);
5293 CONS_MARK (ptr);
5294 /* If the cdr is nil, avoid recursion for the car. */
5295 if (EQ (ptr->cdr, Qnil))
5296 {
5297 obj = ptr->car;
5298 cdr_count = 0;
5299 goto loop;
5300 }
5301 mark_object (ptr->car);
5302 obj = ptr->cdr;
5303 cdr_count++;
5304 if (cdr_count == mark_object_loop_halt)
5305 abort ();
5306 goto loop;
5307 }
5308
5309 case Lisp_Float:
5310 CHECK_ALLOCATED_AND_LIVE (live_float_p);
5311 FLOAT_MARK (XFLOAT (obj));
5312 break;
5313
5314 case Lisp_Int:
5315 break;
5316
5317 default:
5318 abort ();
5319 }
5320
5321#undef CHECK_LIVE
5322#undef CHECK_ALLOCATED
5323#undef CHECK_ALLOCATED_AND_LIVE
5324}
5325
5326/* Mark the pointers in a buffer structure. */
5327
5328static void
5329mark_buffer (buf)
5330 Lisp_Object buf;
5331{
5332 register struct buffer *buffer = XBUFFER (buf);
5333 register Lisp_Object *ptr, tmp;
5334 Lisp_Object base_buffer;
5335
5336 VECTOR_MARK (buffer);
5337
5338 MARK_INTERVAL_TREE (BUF_INTERVALS (buffer));
5339
5340 /* For now, we just don't mark the undo_list. It's done later in
5341 a special way just before the sweep phase, and after stripping
5342 some of its elements that are not needed any more. */
5343
5344 if (buffer->overlays_before)
5345 {
5346 XSETMISC (tmp, buffer->overlays_before);
5347 mark_object (tmp);
5348 }
5349 if (buffer->overlays_after)
5350 {
5351 XSETMISC (tmp, buffer->overlays_after);
5352 mark_object (tmp);
5353 }
5354
5355 for (ptr = &buffer->name;
5356 (char *)ptr < (char *)buffer + sizeof (struct buffer);
5357 ptr++)
5358 mark_object (*ptr);
5359
5360 /* If this is an indirect buffer, mark its base buffer. */
5361 if (buffer->base_buffer && !VECTOR_MARKED_P (buffer->base_buffer))
5362 {
5363 XSETBUFFER (base_buffer, buffer->base_buffer);
5364 mark_buffer (base_buffer);
5365 }
5366}
5367
5368
5369/* Value is non-zero if OBJ will survive the current GC because it's
5370 either marked or does not need to be marked to survive. */
5371
5372int
5373survives_gc_p (obj)
5374 Lisp_Object obj;
5375{
5376 int survives_p;
5377
5378 switch (XGCTYPE (obj))
5379 {
5380 case Lisp_Int:
5381 survives_p = 1;
5382 break;
5383
5384 case Lisp_Symbol:
5385 survives_p = XSYMBOL (obj)->gcmarkbit;
5386 break;
5387
5388 case Lisp_Misc:
5389 survives_p = XMARKER (obj)->gcmarkbit;
5390 break;
5391
5392 case Lisp_String:
5393 survives_p = STRING_MARKED_P (XSTRING (obj));
5394 break;
5395
5396 case Lisp_Vectorlike:
5397 survives_p = GC_SUBRP (obj) || VECTOR_MARKED_P (XVECTOR (obj));
5398 break;
5399
5400 case Lisp_Cons:
5401 survives_p = CONS_MARKED_P (XCONS (obj));
5402 break;
5403
5404 case Lisp_Float:
5405 survives_p = FLOAT_MARKED_P (XFLOAT (obj));
5406 break;
5407
5408 default:
5409 abort ();
5410 }
5411
5412 return survives_p || PURE_POINTER_P ((void *) XPNTR (obj));
5413}
5414
5415
5416\f
5417/* Sweep: find all structures not marked, and free them. */
5418
5419static void
5420gc_sweep ()
5421{
5422 /* Remove or mark entries in weak hash tables.
5423 This must be done before any object is unmarked. */
5424 sweep_weak_hash_tables ();
5425
5426 sweep_strings ();
5427#ifdef GC_CHECK_STRING_BYTES
5428 if (!noninteractive)
5429 check_string_bytes (1);
5430#endif
5431
5432 /* Put all unmarked conses on free list */
5433 {
5434 register struct cons_block *cblk;
5435 struct cons_block **cprev = &cons_block;
5436 register int lim = cons_block_index;
5437 register int num_free = 0, num_used = 0;
5438
5439 cons_free_list = 0;
5440
5441 for (cblk = cons_block; cblk; cblk = *cprev)
5442 {
5443 register int i;
5444 int this_free = 0;
5445 for (i = 0; i < lim; i++)
5446 if (!CONS_MARKED_P (&cblk->conses[i]))
5447 {
5448 this_free++;
5449 *(struct Lisp_Cons **)&cblk->conses[i].cdr = cons_free_list;
5450 cons_free_list = &cblk->conses[i];
5451#if GC_MARK_STACK
5452 cons_free_list->car = Vdead;
5453#endif
5454 }
5455 else
5456 {
5457 num_used++;
5458 CONS_UNMARK (&cblk->conses[i]);
5459 }
5460 lim = CONS_BLOCK_SIZE;
5461 /* If this block contains only free conses and we have already
5462 seen more than two blocks worth of free conses then deallocate
5463 this block. */
5464 if (this_free == CONS_BLOCK_SIZE && num_free > CONS_BLOCK_SIZE)
5465 {
5466 *cprev = cblk->next;
5467 /* Unhook from the free list. */
5468 cons_free_list = *(struct Lisp_Cons **) &cblk->conses[0].cdr;
5469 lisp_align_free (cblk);
5470 n_cons_blocks--;
5471 }
5472 else
5473 {
5474 num_free += this_free;
5475 cprev = &cblk->next;
5476 }
5477 }
5478 total_conses = num_used;
5479 total_free_conses = num_free;
5480 }
5481
5482 /* Put all unmarked floats on free list */
5483 {
5484 register struct float_block *fblk;
5485 struct float_block **fprev = &float_block;
5486 register int lim = float_block_index;
5487 register int num_free = 0, num_used = 0;
5488
5489 float_free_list = 0;
5490
5491 for (fblk = float_block; fblk; fblk = *fprev)
5492 {
5493 register int i;
5494 int this_free = 0;
5495 for (i = 0; i < lim; i++)
5496 if (!FLOAT_MARKED_P (&fblk->floats[i]))
5497 {
5498 this_free++;
5499 *(struct Lisp_Float **)&fblk->floats[i].data = float_free_list;
5500 float_free_list = &fblk->floats[i];
5501 }
5502 else
5503 {
5504 num_used++;
5505 FLOAT_UNMARK (&fblk->floats[i]);
5506 }
5507 lim = FLOAT_BLOCK_SIZE;
5508 /* If this block contains only free floats and we have already
5509 seen more than two blocks worth of free floats then deallocate
5510 this block. */
5511 if (this_free == FLOAT_BLOCK_SIZE && num_free > FLOAT_BLOCK_SIZE)
5512 {
5513 *fprev = fblk->next;
5514 /* Unhook from the free list. */
5515 float_free_list = *(struct Lisp_Float **) &fblk->floats[0].data;
5516 lisp_align_free (fblk);
5517 n_float_blocks--;
5518 }
5519 else
5520 {
5521 num_free += this_free;
5522 fprev = &fblk->next;
5523 }
5524 }
5525 total_floats = num_used;
5526 total_free_floats = num_free;
5527 }
5528
5529 /* Put all unmarked intervals on free list */
5530 {
5531 register struct interval_block *iblk;
5532 struct interval_block **iprev = &interval_block;
5533 register int lim = interval_block_index;
5534 register int num_free = 0, num_used = 0;
5535
5536 interval_free_list = 0;
5537
5538 for (iblk = interval_block; iblk; iblk = *iprev)
5539 {
5540 register int i;
5541 int this_free = 0;
5542
5543 for (i = 0; i < lim; i++)
5544 {
5545 if (!iblk->intervals[i].gcmarkbit)
5546 {
5547 SET_INTERVAL_PARENT (&iblk->intervals[i], interval_free_list);
5548 interval_free_list = &iblk->intervals[i];
5549 this_free++;
5550 }
5551 else
5552 {
5553 num_used++;
5554 iblk->intervals[i].gcmarkbit = 0;
5555 }
5556 }
5557 lim = INTERVAL_BLOCK_SIZE;
5558 /* If this block contains only free intervals and we have already
5559 seen more than two blocks worth of free intervals then
5560 deallocate this block. */
5561 if (this_free == INTERVAL_BLOCK_SIZE && num_free > INTERVAL_BLOCK_SIZE)
5562 {
5563 *iprev = iblk->next;
5564 /* Unhook from the free list. */
5565 interval_free_list = INTERVAL_PARENT (&iblk->intervals[0]);
5566 lisp_free (iblk);
5567 n_interval_blocks--;
5568 }
5569 else
5570 {
5571 num_free += this_free;
5572 iprev = &iblk->next;
5573 }
5574 }
5575 total_intervals = num_used;
5576 total_free_intervals = num_free;
5577 }
5578
5579 /* Put all unmarked symbols on free list */
5580 {
5581 register struct symbol_block *sblk;
5582 struct symbol_block **sprev = &symbol_block;
5583 register int lim = symbol_block_index;
5584 register int num_free = 0, num_used = 0;
5585
5586 symbol_free_list = NULL;
5587
5588 for (sblk = symbol_block; sblk; sblk = *sprev)
5589 {
5590 int this_free = 0;
5591 struct Lisp_Symbol *sym = sblk->symbols;
5592 struct Lisp_Symbol *end = sym + lim;
5593
5594 for (; sym < end; ++sym)
5595 {
5596 /* Check if the symbol was created during loadup. In such a case
5597 it might be pointed to by pure bytecode which we don't trace,
5598 so we conservatively assume that it is live. */
5599 int pure_p = PURE_POINTER_P (XSTRING (sym->xname));
5600
5601 if (!sym->gcmarkbit && !pure_p)
5602 {
5603 *(struct Lisp_Symbol **) &sym->value = symbol_free_list;
5604 symbol_free_list = sym;
5605#if GC_MARK_STACK
5606 symbol_free_list->function = Vdead;
5607#endif
5608 ++this_free;
5609 }
5610 else
5611 {
5612 ++num_used;
5613 if (!pure_p)
5614 UNMARK_STRING (XSTRING (sym->xname));
5615 sym->gcmarkbit = 0;
5616 }
5617 }
5618
5619 lim = SYMBOL_BLOCK_SIZE;
5620 /* If this block contains only free symbols and we have already
5621 seen more than two blocks worth of free symbols then deallocate
5622 this block. */
5623 if (this_free == SYMBOL_BLOCK_SIZE && num_free > SYMBOL_BLOCK_SIZE)
5624 {
5625 *sprev = sblk->next;
5626 /* Unhook from the free list. */
5627 symbol_free_list = *(struct Lisp_Symbol **)&sblk->symbols[0].value;
5628 lisp_free (sblk);
5629 n_symbol_blocks--;
5630 }
5631 else
5632 {
5633 num_free += this_free;
5634 sprev = &sblk->next;
5635 }
5636 }
5637 total_symbols = num_used;
5638 total_free_symbols = num_free;
5639 }
5640
5641 /* Put all unmarked misc's on free list.
5642 For a marker, first unchain it from the buffer it points into. */
5643 {
5644 register struct marker_block *mblk;
5645 struct marker_block **mprev = &marker_block;
5646 register int lim = marker_block_index;
5647 register int num_free = 0, num_used = 0;
5648
5649 marker_free_list = 0;
5650
5651 for (mblk = marker_block; mblk; mblk = *mprev)
5652 {
5653 register int i;
5654 int this_free = 0;
5655
5656 for (i = 0; i < lim; i++)
5657 {
5658 if (!mblk->markers[i].u_marker.gcmarkbit)
5659 {
5660 if (mblk->markers[i].u_marker.type == Lisp_Misc_Marker)
5661 unchain_marker (&mblk->markers[i].u_marker);
5662 /* Set the type of the freed object to Lisp_Misc_Free.
5663 We could leave the type alone, since nobody checks it,
5664 but this might catch bugs faster. */
5665 mblk->markers[i].u_marker.type = Lisp_Misc_Free;
5666 mblk->markers[i].u_free.chain = marker_free_list;
5667 marker_free_list = &mblk->markers[i];
5668 this_free++;
5669 }
5670 else
5671 {
5672 num_used++;
5673 mblk->markers[i].u_marker.gcmarkbit = 0;
5674 }
5675 }
5676 lim = MARKER_BLOCK_SIZE;
5677 /* If this block contains only free markers and we have already
5678 seen more than two blocks worth of free markers then deallocate
5679 this block. */
5680 if (this_free == MARKER_BLOCK_SIZE && num_free > MARKER_BLOCK_SIZE)
5681 {
5682 *mprev = mblk->next;
5683 /* Unhook from the free list. */
5684 marker_free_list = mblk->markers[0].u_free.chain;
5685 lisp_free (mblk);
5686 n_marker_blocks--;
5687 }
5688 else
5689 {
5690 num_free += this_free;
5691 mprev = &mblk->next;
5692 }
5693 }
5694
5695 total_markers = num_used;
5696 total_free_markers = num_free;
5697 }
5698
5699 /* Free all unmarked buffers */
5700 {
5701 register struct buffer *buffer = all_buffers, *prev = 0, *next;
5702
5703 while (buffer)
5704 if (!VECTOR_MARKED_P (buffer))
5705 {
5706 if (prev)
5707 prev->next = buffer->next;
5708 else
5709 all_buffers = buffer->next;
5710 next = buffer->next;
5711 lisp_free (buffer);
5712 buffer = next;
5713 }
5714 else
5715 {
5716 VECTOR_UNMARK (buffer);
5717 UNMARK_BALANCE_INTERVALS (BUF_INTERVALS (buffer));
5718 prev = buffer, buffer = buffer->next;
5719 }
5720 }
5721
5722 /* Free all unmarked vectors */
5723 {
5724 register struct Lisp_Vector *vector = all_vectors, *prev = 0, *next;
5725 total_vector_size = 0;
5726
5727 while (vector)
5728 if (!VECTOR_MARKED_P (vector))
5729 {
5730 if (prev)
5731 prev->next = vector->next;
5732 else
5733 all_vectors = vector->next;
5734 next = vector->next;
5735 lisp_free (vector);
5736 n_vectors--;
5737 vector = next;
5738
5739 }
5740 else
5741 {
5742 VECTOR_UNMARK (vector);
5743 if (vector->size & PSEUDOVECTOR_FLAG)
5744 total_vector_size += (PSEUDOVECTOR_SIZE_MASK & vector->size);
5745 else
5746 total_vector_size += vector->size;
5747 prev = vector, vector = vector->next;
5748 }
5749 }
5750
5751#ifdef GC_CHECK_STRING_BYTES
5752 if (!noninteractive)
5753 check_string_bytes (1);
5754#endif
5755}
5756
5757
5758
5759\f
5760/* Debugging aids. */
5761
5762DEFUN ("memory-limit", Fmemory_limit, Smemory_limit, 0, 0, 0,
5763 doc: /* Return the address of the last byte Emacs has allocated, divided by 1024.
5764This may be helpful in debugging Emacs's memory usage.
5765We divide the value by 1024 to make sure it fits in a Lisp integer. */)
5766 ()
5767{
5768 Lisp_Object end;
5769
5770 XSETINT (end, (EMACS_INT) sbrk (0) / 1024);
5771
5772 return end;
5773}
5774
5775DEFUN ("memory-use-counts", Fmemory_use_counts, Smemory_use_counts, 0, 0, 0,
5776 doc: /* Return a list of counters that measure how much consing there has been.
5777Each of these counters increments for a certain kind of object.
5778The counters wrap around from the largest positive integer to zero.
5779Garbage collection does not decrease them.
5780The elements of the value are as follows:
5781 (CONSES FLOATS VECTOR-CELLS SYMBOLS STRING-CHARS MISCS INTERVALS STRINGS)
5782All are in units of 1 = one object consed
5783except for VECTOR-CELLS and STRING-CHARS, which count the total length of
5784objects consed.
5785MISCS include overlays, markers, and some internal types.
5786Frames, windows, buffers, and subprocesses count as vectors
5787 (but the contents of a buffer's text do not count here). */)
5788 ()
5789{
5790 Lisp_Object consed[8];
5791
5792 consed[0] = make_number (min (MOST_POSITIVE_FIXNUM, cons_cells_consed));
5793 consed[1] = make_number (min (MOST_POSITIVE_FIXNUM, floats_consed));
5794 consed[2] = make_number (min (MOST_POSITIVE_FIXNUM, vector_cells_consed));
5795 consed[3] = make_number (min (MOST_POSITIVE_FIXNUM, symbols_consed));
5796 consed[4] = make_number (min (MOST_POSITIVE_FIXNUM, string_chars_consed));
5797 consed[5] = make_number (min (MOST_POSITIVE_FIXNUM, misc_objects_consed));
5798 consed[6] = make_number (min (MOST_POSITIVE_FIXNUM, intervals_consed));
5799 consed[7] = make_number (min (MOST_POSITIVE_FIXNUM, strings_consed));
5800
5801 return Flist (8, consed);
5802}
5803
5804int suppress_checking;
5805void
5806die (msg, file, line)
5807 const char *msg;
5808 const char *file;
5809 int line;
5810{
5811 fprintf (stderr, "\r\nEmacs fatal error: %s:%d: %s\r\n",
5812 file, line, msg);
5813 abort ();
5814}
5815\f
5816/* Initialization */
5817
5818void
5819init_alloc_once ()
5820{
5821 /* Used to do Vpurify_flag = Qt here, but Qt isn't set up yet! */
5822 purebeg = PUREBEG;
5823 pure_size = PURESIZE;
5824 pure_bytes_used = 0;
5825 pure_bytes_used_before_overflow = 0;
5826
5827 /* Initialize the list of free aligned blocks. */
5828 free_ablock = NULL;
5829
5830#if GC_MARK_STACK || defined GC_MALLOC_CHECK
5831 mem_init ();
5832 Vdead = make_pure_string ("DEAD", 4, 4, 0);
5833#endif
5834
5835 all_vectors = 0;
5836 ignore_warnings = 1;
5837#ifdef DOUG_LEA_MALLOC
5838 mallopt (M_TRIM_THRESHOLD, 128*1024); /* trim threshold */
5839 mallopt (M_MMAP_THRESHOLD, 64*1024); /* mmap threshold */
5840 mallopt (M_MMAP_MAX, MMAP_MAX_AREAS); /* max. number of mmap'ed areas */
5841#endif
5842 init_strings ();
5843 init_cons ();
5844 init_symbol ();
5845 init_marker ();
5846 init_float ();
5847 init_intervals ();
5848
5849#ifdef REL_ALLOC
5850 malloc_hysteresis = 32;
5851#else
5852 malloc_hysteresis = 0;
5853#endif
5854
5855 spare_memory = (char *) malloc (SPARE_MEMORY);
5856
5857 ignore_warnings = 0;
5858 gcprolist = 0;
5859 byte_stack_list = 0;
5860 staticidx = 0;
5861 consing_since_gc = 0;
5862 gc_cons_threshold = 100000 * sizeof (Lisp_Object);
5863#ifdef VIRT_ADDR_VARIES
5864 malloc_sbrk_unused = 1<<22; /* A large number */
5865 malloc_sbrk_used = 100000; /* as reasonable as any number */
5866#endif /* VIRT_ADDR_VARIES */
5867}
5868
5869void
5870init_alloc ()
5871{
5872 gcprolist = 0;
5873 byte_stack_list = 0;
5874#if GC_MARK_STACK
5875#if !defined GC_SAVE_REGISTERS_ON_STACK && !defined GC_SETJMP_WORKS
5876 setjmp_tested_p = longjmps_done = 0;
5877#endif
5878#endif
5879 Vgc_elapsed = make_float (0.0);
5880 gcs_done = 0;
5881}
5882
5883void
5884syms_of_alloc ()
5885{
5886 DEFVAR_INT ("gc-cons-threshold", &gc_cons_threshold,
5887 doc: /* *Number of bytes of consing between garbage collections.
5888Garbage collection can happen automatically once this many bytes have been
5889allocated since the last garbage collection. All data types count.
5890
5891Garbage collection happens automatically only when `eval' is called.
5892
5893By binding this temporarily to a large number, you can effectively
5894prevent garbage collection during a part of the program. */);
5895
5896 DEFVAR_INT ("pure-bytes-used", &pure_bytes_used,
5897 doc: /* Number of bytes of sharable Lisp data allocated so far. */);
5898
5899 DEFVAR_INT ("cons-cells-consed", &cons_cells_consed,
5900 doc: /* Number of cons cells that have been consed so far. */);
5901
5902 DEFVAR_INT ("floats-consed", &floats_consed,
5903 doc: /* Number of floats that have been consed so far. */);
5904
5905 DEFVAR_INT ("vector-cells-consed", &vector_cells_consed,
5906 doc: /* Number of vector cells that have been consed so far. */);
5907
5908 DEFVAR_INT ("symbols-consed", &symbols_consed,
5909 doc: /* Number of symbols that have been consed so far. */);
5910
5911 DEFVAR_INT ("string-chars-consed", &string_chars_consed,
5912 doc: /* Number of string characters that have been consed so far. */);
5913
5914 DEFVAR_INT ("misc-objects-consed", &misc_objects_consed,
5915 doc: /* Number of miscellaneous objects that have been consed so far. */);
5916
5917 DEFVAR_INT ("intervals-consed", &intervals_consed,
5918 doc: /* Number of intervals that have been consed so far. */);
5919
5920 DEFVAR_INT ("strings-consed", &strings_consed,
5921 doc: /* Number of strings that have been consed so far. */);
5922
5923 DEFVAR_LISP ("purify-flag", &Vpurify_flag,
5924 doc: /* Non-nil means loading Lisp code in order to dump an executable.
5925This means that certain objects should be allocated in shared (pure) space. */);
5926
5927 DEFVAR_INT ("undo-limit", &undo_limit,
5928 doc: /* Keep no more undo information once it exceeds this size.
5929This limit is applied when garbage collection happens.
5930The size is counted as the number of bytes occupied,
5931which includes both saved text and other data. */);
5932 undo_limit = 20000;
5933
5934 DEFVAR_INT ("undo-strong-limit", &undo_strong_limit,
5935 doc: /* Don't keep more than this much size of undo information.
5936A previous command which pushes the undo list past this size
5937is entirely forgotten when GC happens.
5938The size is counted as the number of bytes occupied,
5939which includes both saved text and other data. */);
5940 undo_strong_limit = 30000;
5941
5942 DEFVAR_INT ("undo-outer-limit", &undo_outer_limit,
5943 doc: /* Don't keep more than this much size of undo information.
5944If the current command has produced more than this much undo information,
5945GC discards it. This is a last-ditch limit to prevent memory overflow.
5946The size is counted as the number of bytes occupied,
5947which includes both saved text and other data. */);
5948 undo_outer_limit = 300000;
5949
5950 DEFVAR_BOOL ("garbage-collection-messages", &garbage_collection_messages,
5951 doc: /* Non-nil means display messages at start and end of garbage collection. */);
5952 garbage_collection_messages = 0;
5953
5954 DEFVAR_LISP ("post-gc-hook", &Vpost_gc_hook,
5955 doc: /* Hook run after garbage collection has finished. */);
5956 Vpost_gc_hook = Qnil;
5957 Qpost_gc_hook = intern ("post-gc-hook");
5958 staticpro (&Qpost_gc_hook);
5959
5960 DEFVAR_LISP ("memory-signal-data", &Vmemory_signal_data,
5961 doc: /* Precomputed `signal' argument for memory-full error. */);
5962 /* We build this in advance because if we wait until we need it, we might
5963 not be able to allocate the memory to hold it. */
5964 Vmemory_signal_data
5965 = list2 (Qerror,
5966 build_string ("Memory exhausted--use M-x save-some-buffers then exit and restart Emacs"));
5967
5968 DEFVAR_LISP ("memory-full", &Vmemory_full,
5969 doc: /* Non-nil means we are handling a memory-full error. */);
5970 Vmemory_full = Qnil;
5971
5972 staticpro (&Qgc_cons_threshold);
5973 Qgc_cons_threshold = intern ("gc-cons-threshold");
5974
5975 staticpro (&Qchar_table_extra_slots);
5976 Qchar_table_extra_slots = intern ("char-table-extra-slots");
5977
5978 DEFVAR_LISP ("gc-elapsed", &Vgc_elapsed,
5979 doc: /* Accumulated time elapsed in garbage collections.
5980The time is in seconds as a floating point value. */);
5981 DEFVAR_INT ("gcs-done", &gcs_done,
5982 doc: /* Accumulated number of garbage collections done. */);
5983
5984 defsubr (&Scons);
5985 defsubr (&Slist);
5986 defsubr (&Svector);
5987 defsubr (&Smake_byte_code);
5988 defsubr (&Smake_list);
5989 defsubr (&Smake_vector);
5990 defsubr (&Smake_char_table);
5991 defsubr (&Smake_string);
5992 defsubr (&Smake_bool_vector);
5993 defsubr (&Smake_symbol);
5994 defsubr (&Smake_marker);
5995 defsubr (&Spurecopy);
5996 defsubr (&Sgarbage_collect);
5997 defsubr (&Smemory_limit);
5998 defsubr (&Smemory_use_counts);
5999
6000#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
6001 defsubr (&Sgc_status);
6002#endif
6003}
6004
6005/* arch-tag: 6695ca10-e3c5-4c2c-8bc3-ed26a7dda857
6006 (do not change this comment) */