aboutsummaryrefslogtreecommitdiffstats
path: root/src/alloc.c
diff options
context:
space:
mode:
authorStefan Monnier2012-03-25 16:37:21 -0400
committerStefan Monnier2012-03-25 16:37:21 -0400
commit699c782b7668c44d0fa4446331b0590a6d5dac82 (patch)
tree5dcce364741d0761920a3d274b0fc8aba4103d45 /src/alloc.c
parent98fb480ee31bf74cf554044f60f21df16566dd7f (diff)
parente99a9b8bdccadded1f6fae88ee7a2a93dfd4eacf (diff)
downloademacs-699c782b7668c44d0fa4446331b0590a6d5dac82.tar.gz
emacs-699c782b7668c44d0fa4446331b0590a6d5dac82.zip
Merge from trunkpending
Diffstat (limited to 'src/alloc.c')
-rw-r--r--src/alloc.c491
1 files changed, 354 insertions, 137 deletions
diff --git a/src/alloc.c b/src/alloc.c
index f679787e95c..f85661415cd 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -1,6 +1,7 @@
1/* Storage allocation and gc for GNU Emacs Lisp interpreter. 1/* Storage allocation and gc for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985-1986, 1988, 1993-1995, 1997-2011 2
3 Free Software Foundation, Inc. 3Copyright (C) 1985-1986, 1988, 1993-1995, 1997-2012
4 Free Software Foundation, Inc.
4 5
5This file is part of GNU Emacs. 6This file is part of GNU Emacs.
6 7
@@ -24,7 +25,7 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
24 25
25#include <signal.h> 26#include <signal.h>
26 27
27#ifdef HAVE_GTK_AND_PTHREAD 28#ifdef HAVE_PTHREAD
28#include <pthread.h> 29#include <pthread.h>
29#endif 30#endif
30 31
@@ -46,6 +47,7 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
46#include "syssignal.h" 47#include "syssignal.h"
47#include "termhooks.h" /* For struct terminal. */ 48#include "termhooks.h" /* For struct terminal. */
48#include <setjmp.h> 49#include <setjmp.h>
50#include <verify.h>
49 51
50/* GC_MALLOC_CHECK defined means perform validity checks of malloc'd 52/* GC_MALLOC_CHECK defined means perform validity checks of malloc'd
51 memory. Can do this only if using gmalloc.c. */ 53 memory. Can do this only if using gmalloc.c. */
@@ -68,10 +70,6 @@ extern POINTER_TYPE *sbrk ();
68#ifdef DOUG_LEA_MALLOC 70#ifdef DOUG_LEA_MALLOC
69 71
70#include <malloc.h> 72#include <malloc.h>
71/* malloc.h #defines this as size_t, at least in glibc2. */
72#ifndef __malloc_size_t
73#define __malloc_size_t int
74#endif
75 73
76/* Specify maximum number of areas to mmap. It would be nice to use a 74/* Specify maximum number of areas to mmap. It would be nice to use a
77 value that explicitly means "no limit". */ 75 value that explicitly means "no limit". */
@@ -82,20 +80,21 @@ extern POINTER_TYPE *sbrk ();
82 80
83/* The following come from gmalloc.c. */ 81/* The following come from gmalloc.c. */
84 82
85#define __malloc_size_t size_t 83extern size_t _bytes_used;
86extern __malloc_size_t _bytes_used; 84extern size_t __malloc_extra_blocks;
87extern __malloc_size_t __malloc_extra_blocks;
88 85
89#endif /* not DOUG_LEA_MALLOC */ 86#endif /* not DOUG_LEA_MALLOC */
90 87
91#if ! defined SYSTEM_MALLOC && ! defined SYNC_INPUT 88#if ! defined SYSTEM_MALLOC && ! defined SYNC_INPUT
92#ifdef HAVE_GTK_AND_PTHREAD 89#ifdef HAVE_PTHREAD
93 90
94/* When GTK uses the file chooser dialog, different backends can be loaded 91/* When GTK uses the file chooser dialog, different backends can be loaded
95 dynamically. One such a backend is the Gnome VFS backend that gets loaded 92 dynamically. One such a backend is the Gnome VFS backend that gets loaded
96 if you run Gnome. That backend creates several threads and also allocates 93 if you run Gnome. That backend creates several threads and also allocates
97 memory with malloc. 94 memory with malloc.
98 95
96 Also, gconf and gsettings may create several threads.
97
99 If Emacs sets malloc hooks (! SYSTEM_MALLOC) and the emacs_blocked_* 98 If Emacs sets malloc hooks (! SYSTEM_MALLOC) and the emacs_blocked_*
100 functions below are called from malloc, there is a chance that one 99 functions below are called from malloc, there is a chance that one
101 of these threads preempts the Emacs main thread and the hook variables 100 of these threads preempts the Emacs main thread and the hook variables
@@ -127,12 +126,12 @@ static pthread_mutex_t alloc_mutex;
127 } \ 126 } \
128 while (0) 127 while (0)
129 128
130#else /* ! defined HAVE_GTK_AND_PTHREAD */ 129#else /* ! defined HAVE_PTHREAD */
131 130
132#define BLOCK_INPUT_ALLOC BLOCK_INPUT 131#define BLOCK_INPUT_ALLOC BLOCK_INPUT
133#define UNBLOCK_INPUT_ALLOC UNBLOCK_INPUT 132#define UNBLOCK_INPUT_ALLOC UNBLOCK_INPUT
134 133
135#endif /* ! defined HAVE_GTK_AND_PTHREAD */ 134#endif /* ! defined HAVE_PTHREAD */
136#endif /* ! defined SYSTEM_MALLOC && ! defined SYNC_INPUT */ 135#endif /* ! defined SYSTEM_MALLOC && ! defined SYNC_INPUT */
137 136
138/* Mark, unmark, query mark bit of a Lisp string. S must be a pointer 137/* Mark, unmark, query mark bit of a Lisp string. S must be a pointer
@@ -205,29 +204,23 @@ static int malloc_hysteresis;
205 remapping on more recent systems because this is less important 204 remapping on more recent systems because this is less important
206 nowadays than in the days of small memories and timesharing. */ 205 nowadays than in the days of small memories and timesharing. */
207 206
208#ifndef VIRT_ADDR_VARIES
209static
210#endif
211EMACS_INT pure[(PURESIZE + sizeof (EMACS_INT) - 1) / sizeof (EMACS_INT)] = {1,}; 207EMACS_INT pure[(PURESIZE + sizeof (EMACS_INT) - 1) / sizeof (EMACS_INT)] = {1,};
212#define PUREBEG (char *) pure 208#define PUREBEG (char *) pure
213 209
214/* Pointer to the pure area, and its size. */ 210/* Pointer to the pure area, and its size. */
215 211
216static char *purebeg; 212static char *purebeg;
217static size_t pure_size; 213static ptrdiff_t pure_size;
218 214
219/* Number of bytes of pure storage used before pure storage overflowed. 215/* Number of bytes of pure storage used before pure storage overflowed.
220 If this is non-zero, this implies that an overflow occurred. */ 216 If this is non-zero, this implies that an overflow occurred. */
221 217
222static size_t pure_bytes_used_before_overflow; 218static ptrdiff_t pure_bytes_used_before_overflow;
223 219
224/* Value is non-zero if P points into pure space. */ 220/* Value is non-zero if P points into pure space. */
225 221
226#define PURE_POINTER_P(P) \ 222#define PURE_POINTER_P(P) \
227 (((PNTR_COMPARISON_TYPE) (P) \ 223 ((uintptr_t) (P) - (uintptr_t) purebeg <= pure_size)
228 < (PNTR_COMPARISON_TYPE) ((char *) purebeg + pure_size)) \
229 && ((PNTR_COMPARISON_TYPE) (P) \
230 >= (PNTR_COMPARISON_TYPE) purebeg))
231 224
232/* Index in pure at which next pure Lisp object will be allocated.. */ 225/* Index in pure at which next pure Lisp object will be allocated.. */
233 226
@@ -252,7 +245,7 @@ const char *pending_malloc_warning;
252 245
253#if MAX_SAVE_STACK > 0 246#if MAX_SAVE_STACK > 0
254static char *stack_copy; 247static char *stack_copy;
255static size_t stack_copy_size; 248static ptrdiff_t stack_copy_size;
256#endif 249#endif
257 250
258/* Non-zero means ignore malloc warnings. Set during initialization. 251/* Non-zero means ignore malloc warnings. Set during initialization.
@@ -281,6 +274,7 @@ static void compact_small_strings (void);
281static void free_large_strings (void); 274static void free_large_strings (void);
282static void sweep_strings (void); 275static void sweep_strings (void);
283static void free_misc (Lisp_Object); 276static void free_misc (Lisp_Object);
277extern Lisp_Object which_symbols (Lisp_Object, EMACS_INT) EXTERNALLY_VISIBLE;
284 278
285/* When scanning the C stack for live Lisp objects, Emacs keeps track 279/* When scanning the C stack for live Lisp objects, Emacs keeps track
286 of what memory allocated via lisp_malloc is intended for what 280 of what memory allocated via lisp_malloc is intended for what
@@ -316,6 +310,7 @@ static POINTER_TYPE *lisp_malloc (size_t, enum mem_type);
316 on free lists recognizable in O(1). */ 310 on free lists recognizable in O(1). */
317 311
318static Lisp_Object Vdead; 312static Lisp_Object Vdead;
313#define DEADP(x) EQ (x, Vdead)
319 314
320#ifdef GC_MALLOC_CHECK 315#ifdef GC_MALLOC_CHECK
321 316
@@ -395,7 +390,7 @@ static int live_symbol_p (struct mem_node *, void *);
395static int live_float_p (struct mem_node *, void *); 390static int live_float_p (struct mem_node *, void *);
396static int live_misc_p (struct mem_node *, void *); 391static int live_misc_p (struct mem_node *, void *);
397static void mark_maybe_object (Lisp_Object); 392static void mark_maybe_object (Lisp_Object);
398static void mark_memory (void *, void *, int); 393static void mark_memory (void *, void *);
399static void mem_init (void); 394static void mem_init (void);
400static struct mem_node *mem_insert (void *, void *, enum mem_type); 395static struct mem_node *mem_insert (void *, void *, enum mem_type);
401static void mem_insert_fixup (struct mem_node *); 396static void mem_insert_fixup (struct mem_node *);
@@ -412,6 +407,10 @@ static void check_gcpros (void);
412 407
413#endif /* GC_MARK_STACK || GC_MALLOC_CHECK */ 408#endif /* GC_MARK_STACK || GC_MALLOC_CHECK */
414 409
410#ifndef DEADP
411# define DEADP(x) 0
412#endif
413
415/* Recording what needs to be marked for gc. */ 414/* Recording what needs to be marked for gc. */
416 415
417struct gcpro *gcprolist; 416struct gcpro *gcprolist;
@@ -486,47 +485,90 @@ buffer_memory_full (EMACS_INT nbytes)
486 485
487 486
488#ifndef XMALLOC_OVERRUN_CHECK 487#ifndef XMALLOC_OVERRUN_CHECK
489#define XMALLOC_OVERRUN_CHECK_SIZE 0 488#define XMALLOC_OVERRUN_CHECK_OVERHEAD 0
490#else 489#else
491 490
492/* Check for overrun in malloc'ed buffers by wrapping a 16 byte header 491/* Check for overrun in malloc'ed buffers by wrapping a header and trailer
493 and a 16 byte trailer around each block. 492 around each block.
494 493
495 The header consists of 12 fixed bytes + a 4 byte integer contaning the 494 The header consists of XMALLOC_OVERRUN_CHECK_SIZE fixed bytes
496 original block size, while the trailer consists of 16 fixed bytes. 495 followed by XMALLOC_OVERRUN_SIZE_SIZE bytes containing the original
496 block size in little-endian order. The trailer consists of
497 XMALLOC_OVERRUN_CHECK_SIZE fixed bytes.
497 498
498 The header is used to detect whether this block has been allocated 499 The header is used to detect whether this block has been allocated
499 through these functions -- as it seems that some low-level libc 500 through these functions, as some low-level libc functions may
500 functions may bypass the malloc hooks. 501 bypass the malloc hooks. */
501*/
502
503 502
504#define XMALLOC_OVERRUN_CHECK_SIZE 16 503#define XMALLOC_OVERRUN_CHECK_SIZE 16
504#define XMALLOC_OVERRUN_CHECK_OVERHEAD \
505 (2 * XMALLOC_OVERRUN_CHECK_SIZE + XMALLOC_OVERRUN_SIZE_SIZE)
506
507/* Define XMALLOC_OVERRUN_SIZE_SIZE so that (1) it's large enough to
508 hold a size_t value and (2) the header size is a multiple of the
509 alignment that Emacs needs for C types and for USE_LSB_TAG. */
510#define XMALLOC_BASE_ALIGNMENT \
511 offsetof ( \
512 struct { \
513 union { long double d; intmax_t i; void *p; } u; \
514 char c; \
515 }, \
516 c)
517#ifdef USE_LSB_TAG
518/* A common multiple of the positive integers A and B. Ideally this
519 would be the least common multiple, but there's no way to do that
520 as a constant expression in C, so do the best that we can easily do. */
521# define COMMON_MULTIPLE(a, b) \
522 ((a) % (b) == 0 ? (a) : (b) % (a) == 0 ? (b) : (a) * (b))
523# define XMALLOC_HEADER_ALIGNMENT \
524 COMMON_MULTIPLE (1 << GCTYPEBITS, XMALLOC_BASE_ALIGNMENT)
525#else
526# define XMALLOC_HEADER_ALIGNMENT XMALLOC_BASE_ALIGNMENT
527#endif
528#define XMALLOC_OVERRUN_SIZE_SIZE \
529 (((XMALLOC_OVERRUN_CHECK_SIZE + sizeof (size_t) \
530 + XMALLOC_HEADER_ALIGNMENT - 1) \
531 / XMALLOC_HEADER_ALIGNMENT * XMALLOC_HEADER_ALIGNMENT) \
532 - XMALLOC_OVERRUN_CHECK_SIZE)
533
534static char const xmalloc_overrun_check_header[XMALLOC_OVERRUN_CHECK_SIZE] =
535 { '\x9a', '\x9b', '\xae', '\xaf',
536 '\xbf', '\xbe', '\xce', '\xcf',
537 '\xea', '\xeb', '\xec', '\xed',
538 '\xdf', '\xde', '\x9c', '\x9d' };
539
540static char const xmalloc_overrun_check_trailer[XMALLOC_OVERRUN_CHECK_SIZE] =
541 { '\xaa', '\xab', '\xac', '\xad',
542 '\xba', '\xbb', '\xbc', '\xbd',
543 '\xca', '\xcb', '\xcc', '\xcd',
544 '\xda', '\xdb', '\xdc', '\xdd' };
545
546/* Insert and extract the block size in the header. */
505 547
506static char xmalloc_overrun_check_header[XMALLOC_OVERRUN_CHECK_SIZE-4] = 548static void
507 { 0x9a, 0x9b, 0xae, 0xaf, 549xmalloc_put_size (unsigned char *ptr, size_t size)
508 0xbf, 0xbe, 0xce, 0xcf, 550{
509 0xea, 0xeb, 0xec, 0xed }; 551 int i;
510 552 for (i = 0; i < XMALLOC_OVERRUN_SIZE_SIZE; i++)
511static char xmalloc_overrun_check_trailer[XMALLOC_OVERRUN_CHECK_SIZE] = 553 {
512 { 0xaa, 0xab, 0xac, 0xad, 554 *--ptr = size & ((1 << CHAR_BIT) - 1);
513 0xba, 0xbb, 0xbc, 0xbd, 555 size >>= CHAR_BIT;
514 0xca, 0xcb, 0xcc, 0xcd, 556 }
515 0xda, 0xdb, 0xdc, 0xdd }; 557}
516
517/* Macros to insert and extract the block size in the header. */
518
519#define XMALLOC_PUT_SIZE(ptr, size) \
520 (ptr[-1] = (size & 0xff), \
521 ptr[-2] = ((size >> 8) & 0xff), \
522 ptr[-3] = ((size >> 16) & 0xff), \
523 ptr[-4] = ((size >> 24) & 0xff))
524 558
525#define XMALLOC_GET_SIZE(ptr) \ 559static size_t
526 (size_t)((unsigned)(ptr[-1]) | \ 560xmalloc_get_size (unsigned char *ptr)
527 ((unsigned)(ptr[-2]) << 8) | \ 561{
528 ((unsigned)(ptr[-3]) << 16) | \ 562 size_t size = 0;
529 ((unsigned)(ptr[-4]) << 24)) 563 int i;
564 ptr -= XMALLOC_OVERRUN_SIZE_SIZE;
565 for (i = 0; i < XMALLOC_OVERRUN_SIZE_SIZE; i++)
566 {
567 size <<= CHAR_BIT;
568 size += *ptr++;
569 }
570 return size;
571}
530 572
531 573
532/* The call depth in overrun_check functions. For example, this might happen: 574/* The call depth in overrun_check functions. For example, this might happen:
@@ -545,10 +587,10 @@ static char xmalloc_overrun_check_trailer[XMALLOC_OVERRUN_CHECK_SIZE] =
545 587
546 xfree(10032) 588 xfree(10032)
547 overrun_check_free(10032) 589 overrun_check_free(10032)
548 decrease overhed 590 decrease overhead
549 free(10016) <- crash, because 10000 is the original pointer. */ 591 free(10016) <- crash, because 10000 is the original pointer. */
550 592
551static int check_depth; 593static ptrdiff_t check_depth;
552 594
553/* Like malloc, but wraps allocated block with header and trailer. */ 595/* Like malloc, but wraps allocated block with header and trailer. */
554 596
@@ -556,15 +598,16 @@ static POINTER_TYPE *
556overrun_check_malloc (size_t size) 598overrun_check_malloc (size_t size)
557{ 599{
558 register unsigned char *val; 600 register unsigned char *val;
559 size_t overhead = ++check_depth == 1 ? XMALLOC_OVERRUN_CHECK_SIZE*2 : 0; 601 int overhead = ++check_depth == 1 ? XMALLOC_OVERRUN_CHECK_OVERHEAD : 0;
602 if (SIZE_MAX - overhead < size)
603 abort ();
560 604
561 val = (unsigned char *) malloc (size + overhead); 605 val = (unsigned char *) malloc (size + overhead);
562 if (val && check_depth == 1) 606 if (val && check_depth == 1)
563 { 607 {
564 memcpy (val, xmalloc_overrun_check_header, 608 memcpy (val, xmalloc_overrun_check_header, XMALLOC_OVERRUN_CHECK_SIZE);
565 XMALLOC_OVERRUN_CHECK_SIZE - 4); 609 val += XMALLOC_OVERRUN_CHECK_SIZE + XMALLOC_OVERRUN_SIZE_SIZE;
566 val += XMALLOC_OVERRUN_CHECK_SIZE; 610 xmalloc_put_size (val, size);
567 XMALLOC_PUT_SIZE(val, size);
568 memcpy (val + size, xmalloc_overrun_check_trailer, 611 memcpy (val + size, xmalloc_overrun_check_trailer,
569 XMALLOC_OVERRUN_CHECK_SIZE); 612 XMALLOC_OVERRUN_CHECK_SIZE);
570 } 613 }
@@ -580,31 +623,32 @@ static POINTER_TYPE *
580overrun_check_realloc (POINTER_TYPE *block, size_t size) 623overrun_check_realloc (POINTER_TYPE *block, size_t size)
581{ 624{
582 register unsigned char *val = (unsigned char *) block; 625 register unsigned char *val = (unsigned char *) block;
583 size_t overhead = ++check_depth == 1 ? XMALLOC_OVERRUN_CHECK_SIZE*2 : 0; 626 int overhead = ++check_depth == 1 ? XMALLOC_OVERRUN_CHECK_OVERHEAD : 0;
627 if (SIZE_MAX - overhead < size)
628 abort ();
584 629
585 if (val 630 if (val
586 && check_depth == 1 631 && check_depth == 1
587 && memcmp (xmalloc_overrun_check_header, 632 && memcmp (xmalloc_overrun_check_header,
588 val - XMALLOC_OVERRUN_CHECK_SIZE, 633 val - XMALLOC_OVERRUN_CHECK_SIZE - XMALLOC_OVERRUN_SIZE_SIZE,
589 XMALLOC_OVERRUN_CHECK_SIZE - 4) == 0) 634 XMALLOC_OVERRUN_CHECK_SIZE) == 0)
590 { 635 {
591 size_t osize = XMALLOC_GET_SIZE (val); 636 size_t osize = xmalloc_get_size (val);
592 if (memcmp (xmalloc_overrun_check_trailer, val + osize, 637 if (memcmp (xmalloc_overrun_check_trailer, val + osize,
593 XMALLOC_OVERRUN_CHECK_SIZE)) 638 XMALLOC_OVERRUN_CHECK_SIZE))
594 abort (); 639 abort ();
595 memset (val + osize, 0, XMALLOC_OVERRUN_CHECK_SIZE); 640 memset (val + osize, 0, XMALLOC_OVERRUN_CHECK_SIZE);
596 val -= XMALLOC_OVERRUN_CHECK_SIZE; 641 val -= XMALLOC_OVERRUN_CHECK_SIZE + XMALLOC_OVERRUN_SIZE_SIZE;
597 memset (val, 0, XMALLOC_OVERRUN_CHECK_SIZE); 642 memset (val, 0, XMALLOC_OVERRUN_CHECK_SIZE + XMALLOC_OVERRUN_SIZE_SIZE);
598 } 643 }
599 644
600 val = (unsigned char *) realloc ((POINTER_TYPE *)val, size + overhead); 645 val = (unsigned char *) realloc ((POINTER_TYPE *)val, size + overhead);
601 646
602 if (val && check_depth == 1) 647 if (val && check_depth == 1)
603 { 648 {
604 memcpy (val, xmalloc_overrun_check_header, 649 memcpy (val, xmalloc_overrun_check_header, XMALLOC_OVERRUN_CHECK_SIZE);
605 XMALLOC_OVERRUN_CHECK_SIZE - 4); 650 val += XMALLOC_OVERRUN_CHECK_SIZE + XMALLOC_OVERRUN_SIZE_SIZE;
606 val += XMALLOC_OVERRUN_CHECK_SIZE; 651 xmalloc_put_size (val, size);
607 XMALLOC_PUT_SIZE(val, size);
608 memcpy (val + size, xmalloc_overrun_check_trailer, 652 memcpy (val + size, xmalloc_overrun_check_trailer,
609 XMALLOC_OVERRUN_CHECK_SIZE); 653 XMALLOC_OVERRUN_CHECK_SIZE);
610 } 654 }
@@ -623,20 +667,20 @@ overrun_check_free (POINTER_TYPE *block)
623 if (val 667 if (val
624 && check_depth == 1 668 && check_depth == 1
625 && memcmp (xmalloc_overrun_check_header, 669 && memcmp (xmalloc_overrun_check_header,
626 val - XMALLOC_OVERRUN_CHECK_SIZE, 670 val - XMALLOC_OVERRUN_CHECK_SIZE - XMALLOC_OVERRUN_SIZE_SIZE,
627 XMALLOC_OVERRUN_CHECK_SIZE - 4) == 0) 671 XMALLOC_OVERRUN_CHECK_SIZE) == 0)
628 { 672 {
629 size_t osize = XMALLOC_GET_SIZE (val); 673 size_t osize = xmalloc_get_size (val);
630 if (memcmp (xmalloc_overrun_check_trailer, val + osize, 674 if (memcmp (xmalloc_overrun_check_trailer, val + osize,
631 XMALLOC_OVERRUN_CHECK_SIZE)) 675 XMALLOC_OVERRUN_CHECK_SIZE))
632 abort (); 676 abort ();
633#ifdef XMALLOC_CLEAR_FREE_MEMORY 677#ifdef XMALLOC_CLEAR_FREE_MEMORY
634 val -= XMALLOC_OVERRUN_CHECK_SIZE; 678 val -= XMALLOC_OVERRUN_CHECK_SIZE + XMALLOC_OVERRUN_SIZE_SIZE;
635 memset (val, 0xff, osize + XMALLOC_OVERRUN_CHECK_SIZE*2); 679 memset (val, 0xff, osize + XMALLOC_OVERRUN_CHECK_OVERHEAD);
636#else 680#else
637 memset (val + osize, 0, XMALLOC_OVERRUN_CHECK_SIZE); 681 memset (val + osize, 0, XMALLOC_OVERRUN_CHECK_SIZE);
638 val -= XMALLOC_OVERRUN_CHECK_SIZE; 682 val -= XMALLOC_OVERRUN_CHECK_SIZE + XMALLOC_OVERRUN_SIZE_SIZE;
639 memset (val, 0, XMALLOC_OVERRUN_CHECK_SIZE); 683 memset (val, 0, XMALLOC_OVERRUN_CHECK_SIZE + XMALLOC_OVERRUN_SIZE_SIZE);
640#endif 684#endif
641 } 685 }
642 686
@@ -717,6 +761,93 @@ xfree (POINTER_TYPE *block)
717} 761}
718 762
719 763
764/* Other parts of Emacs pass large int values to allocator functions
765 expecting ptrdiff_t. This is portable in practice, but check it to
766 be safe. */
767verify (INT_MAX <= PTRDIFF_MAX);
768
769
770/* Allocate an array of NITEMS items, each of size ITEM_SIZE.
771 Signal an error on memory exhaustion, and block interrupt input. */
772
773void *
774xnmalloc (ptrdiff_t nitems, ptrdiff_t item_size)
775{
776 xassert (0 <= nitems && 0 < item_size);
777 if (min (PTRDIFF_MAX, SIZE_MAX) / item_size < nitems)
778 memory_full (SIZE_MAX);
779 return xmalloc (nitems * item_size);
780}
781
782
783/* Reallocate an array PA to make it of NITEMS items, each of size ITEM_SIZE.
784 Signal an error on memory exhaustion, and block interrupt input. */
785
786void *
787xnrealloc (void *pa, ptrdiff_t nitems, ptrdiff_t item_size)
788{
789 xassert (0 <= nitems && 0 < item_size);
790 if (min (PTRDIFF_MAX, SIZE_MAX) / item_size < nitems)
791 memory_full (SIZE_MAX);
792 return xrealloc (pa, nitems * item_size);
793}
794
795
796/* Grow PA, which points to an array of *NITEMS items, and return the
797 location of the reallocated array, updating *NITEMS to reflect its
798 new size. The new array will contain at least NITEMS_INCR_MIN more
799 items, but will not contain more than NITEMS_MAX items total.
800 ITEM_SIZE is the size of each item, in bytes.
801
802 ITEM_SIZE and NITEMS_INCR_MIN must be positive. *NITEMS must be
803 nonnegative. If NITEMS_MAX is -1, it is treated as if it were
804 infinity.
805
806 If PA is null, then allocate a new array instead of reallocating
807 the old one. Thus, to grow an array A without saving its old
808 contents, invoke xfree (A) immediately followed by xgrowalloc (0,
809 &NITEMS, ...).
810
811 Block interrupt input as needed. If memory exhaustion occurs, set
812 *NITEMS to zero if PA is null, and signal an error (i.e., do not
813 return). */
814
815void *
816xpalloc (void *pa, ptrdiff_t *nitems, ptrdiff_t nitems_incr_min,
817 ptrdiff_t nitems_max, ptrdiff_t item_size)
818{
819 /* The approximate size to use for initial small allocation
820 requests. This is the largest "small" request for the GNU C
821 library malloc. */
822 enum { DEFAULT_MXFAST = 64 * sizeof (size_t) / 4 };
823
824 /* If the array is tiny, grow it to about (but no greater than)
825 DEFAULT_MXFAST bytes. Otherwise, grow it by about 50%. */
826 ptrdiff_t n = *nitems;
827 ptrdiff_t tiny_max = DEFAULT_MXFAST / item_size - n;
828 ptrdiff_t half_again = n >> 1;
829 ptrdiff_t incr_estimate = max (tiny_max, half_again);
830
831 /* Adjust the increment according to three constraints: NITEMS_INCR_MIN,
832 NITEMS_MAX, and what the C language can represent safely. */
833 ptrdiff_t C_language_max = min (PTRDIFF_MAX, SIZE_MAX) / item_size;
834 ptrdiff_t n_max = (0 <= nitems_max && nitems_max < C_language_max
835 ? nitems_max : C_language_max);
836 ptrdiff_t nitems_incr_max = n_max - n;
837 ptrdiff_t incr = max (nitems_incr_min, min (incr_estimate, nitems_incr_max));
838
839 xassert (0 < item_size && 0 < nitems_incr_min && 0 <= n && -1 <= nitems_max);
840 if (! pa)
841 *nitems = 0;
842 if (nitems_incr_max < incr)
843 memory_full (SIZE_MAX);
844 n += incr;
845 pa = xrealloc (pa, n * item_size);
846 *nitems = n;
847 return pa;
848}
849
850
720/* Like strdup, but uses xmalloc. */ 851/* Like strdup, but uses xmalloc. */
721 852
722char * 853char *
@@ -746,7 +877,7 @@ safe_alloca_unwind (Lisp_Object arg)
746 877
747/* Like malloc but used for allocating Lisp data. NBYTES is the 878/* Like malloc but used for allocating Lisp data. NBYTES is the
748 number of bytes to allocate, TYPE describes the intended use of the 879 number of bytes to allocate, TYPE describes the intended use of the
749 allcated memory block (for strings, for conses, ...). */ 880 allocated memory block (for strings, for conses, ...). */
750 881
751#ifndef USE_LSB_TAG 882#ifndef USE_LSB_TAG
752static void *lisp_malloc_loser; 883static void *lisp_malloc_loser;
@@ -1092,11 +1223,11 @@ static void (*old_free_hook) (void*, const void*);
1092# define BYTES_USED _bytes_used 1223# define BYTES_USED _bytes_used
1093#endif 1224#endif
1094 1225
1095static __malloc_size_t bytes_used_when_reconsidered; 1226static size_t bytes_used_when_reconsidered;
1096 1227
1097/* Value of _bytes_used, when spare_memory was freed. */ 1228/* Value of _bytes_used, when spare_memory was freed. */
1098 1229
1099static __malloc_size_t bytes_used_when_full; 1230static size_t bytes_used_when_full;
1100 1231
1101/* This function is used as the hook for free to call. */ 1232/* This function is used as the hook for free to call. */
1102 1233
@@ -1251,20 +1382,20 @@ emacs_blocked_realloc (void *ptr, size_t size, const void *ptr2)
1251} 1382}
1252 1383
1253 1384
1254#ifdef HAVE_GTK_AND_PTHREAD 1385#ifdef HAVE_PTHREAD
1255/* Called from Fdump_emacs so that when the dumped Emacs starts, it has a 1386/* Called from Fdump_emacs so that when the dumped Emacs starts, it has a
1256 normal malloc. Some thread implementations need this as they call 1387 normal malloc. Some thread implementations need this as they call
1257 malloc before main. The pthread_self call in BLOCK_INPUT_ALLOC then 1388 malloc before main. The pthread_self call in BLOCK_INPUT_ALLOC then
1258 calls malloc because it is the first call, and we have an endless loop. */ 1389 calls malloc because it is the first call, and we have an endless loop. */
1259 1390
1260void 1391void
1261reset_malloc_hooks () 1392reset_malloc_hooks (void)
1262{ 1393{
1263 __free_hook = old_free_hook; 1394 __free_hook = old_free_hook;
1264 __malloc_hook = old_malloc_hook; 1395 __malloc_hook = old_malloc_hook;
1265 __realloc_hook = old_realloc_hook; 1396 __realloc_hook = old_realloc_hook;
1266} 1397}
1267#endif /* HAVE_GTK_AND_PTHREAD */ 1398#endif /* HAVE_PTHREAD */
1268 1399
1269 1400
1270/* Called from main to set up malloc to use our hooks. */ 1401/* Called from main to set up malloc to use our hooks. */
@@ -1272,11 +1403,11 @@ reset_malloc_hooks ()
1272void 1403void
1273uninterrupt_malloc (void) 1404uninterrupt_malloc (void)
1274{ 1405{
1275#ifdef HAVE_GTK_AND_PTHREAD 1406#ifdef HAVE_PTHREAD
1276#ifdef DOUG_LEA_MALLOC 1407#ifdef DOUG_LEA_MALLOC
1277 pthread_mutexattr_t attr; 1408 pthread_mutexattr_t attr;
1278 1409
1279 /* GLIBC has a faster way to do this, but lets keep it portable. 1410 /* GLIBC has a faster way to do this, but let's keep it portable.
1280 This is according to the Single UNIX Specification. */ 1411 This is according to the Single UNIX Specification. */
1281 pthread_mutexattr_init (&attr); 1412 pthread_mutexattr_init (&attr);
1282 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE); 1413 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
@@ -1286,7 +1417,7 @@ uninterrupt_malloc (void)
1286 and the bundled gmalloc.c doesn't require it. */ 1417 and the bundled gmalloc.c doesn't require it. */
1287 pthread_mutex_init (&alloc_mutex, NULL); 1418 pthread_mutex_init (&alloc_mutex, NULL);
1288#endif /* !DOUG_LEA_MALLOC */ 1419#endif /* !DOUG_LEA_MALLOC */
1289#endif /* HAVE_GTK_AND_PTHREAD */ 1420#endif /* HAVE_PTHREAD */
1290 1421
1291 if (__free_hook != emacs_blocked_free) 1422 if (__free_hook != emacs_blocked_free)
1292 old_free_hook = __free_hook; 1423 old_free_hook = __free_hook;
@@ -1452,6 +1583,21 @@ make_number (EMACS_INT n)
1452} 1583}
1453#endif 1584#endif
1454 1585
1586/* Convert the pointer-sized word P to EMACS_INT while preserving its
1587 type and ptr fields. */
1588static Lisp_Object
1589widen_to_Lisp_Object (void *p)
1590{
1591 intptr_t i = (intptr_t) p;
1592#ifdef USE_LISP_UNION_TYPE
1593 Lisp_Object obj;
1594 obj.i = i;
1595 return obj;
1596#else
1597 return i;
1598#endif
1599}
1600
1455/*********************************************************************** 1601/***********************************************************************
1456 String Allocation 1602 String Allocation
1457 ***********************************************************************/ 1603 ***********************************************************************/
@@ -1661,7 +1807,8 @@ static char const string_overrun_cookie[GC_STRING_OVERRUN_COOKIE_SIZE] =
1661 calculating a value to be passed to malloc. */ 1807 calculating a value to be passed to malloc. */
1662#define STRING_BYTES_MAX \ 1808#define STRING_BYTES_MAX \
1663 min (STRING_BYTES_BOUND, \ 1809 min (STRING_BYTES_BOUND, \
1664 ((SIZE_MAX - XMALLOC_OVERRUN_CHECK_SIZE - GC_STRING_EXTRA \ 1810 ((SIZE_MAX - XMALLOC_OVERRUN_CHECK_OVERHEAD \
1811 - GC_STRING_EXTRA \
1665 - offsetof (struct sblock, first_data) \ 1812 - offsetof (struct sblock, first_data) \
1666 - SDATA_DATA_OFFSET) \ 1813 - SDATA_DATA_OFFSET) \
1667 & ~(sizeof (EMACS_INT) - 1))) 1814 & ~(sizeof (EMACS_INT) - 1)))
@@ -1775,7 +1922,7 @@ check_string_free_list (void)
1775 while (s != NULL) 1922 while (s != NULL)
1776 { 1923 {
1777 if ((uintptr_t) s < 1024) 1924 if ((uintptr_t) s < 1024)
1778 abort(); 1925 abort ();
1779 s = NEXT_FREE_LISP_STRING (s); 1926 s = NEXT_FREE_LISP_STRING (s);
1780 } 1927 }
1781} 1928}
@@ -2426,17 +2573,17 @@ make_uninit_multibyte_string (EMACS_INT nchars, EMACS_INT nbytes)
2426 / (sizeof (struct Lisp_Float) * CHAR_BIT + 1)) 2573 / (sizeof (struct Lisp_Float) * CHAR_BIT + 1))
2427 2574
2428#define GETMARKBIT(block,n) \ 2575#define GETMARKBIT(block,n) \
2429 (((block)->gcmarkbits[(n) / (sizeof(int) * CHAR_BIT)] \ 2576 (((block)->gcmarkbits[(n) / (sizeof (int) * CHAR_BIT)] \
2430 >> ((n) % (sizeof(int) * CHAR_BIT))) \ 2577 >> ((n) % (sizeof (int) * CHAR_BIT))) \
2431 & 1) 2578 & 1)
2432 2579
2433#define SETMARKBIT(block,n) \ 2580#define SETMARKBIT(block,n) \
2434 (block)->gcmarkbits[(n) / (sizeof(int) * CHAR_BIT)] \ 2581 (block)->gcmarkbits[(n) / (sizeof (int) * CHAR_BIT)] \
2435 |= 1 << ((n) % (sizeof(int) * CHAR_BIT)) 2582 |= 1 << ((n) % (sizeof (int) * CHAR_BIT))
2436 2583
2437#define UNSETMARKBIT(block,n) \ 2584#define UNSETMARKBIT(block,n) \
2438 (block)->gcmarkbits[(n) / (sizeof(int) * CHAR_BIT)] \ 2585 (block)->gcmarkbits[(n) / (sizeof (int) * CHAR_BIT)] \
2439 &= ~(1 << ((n) % (sizeof(int) * CHAR_BIT))) 2586 &= ~(1 << ((n) % (sizeof (int) * CHAR_BIT)))
2440 2587
2441#define FLOAT_BLOCK(fptr) \ 2588#define FLOAT_BLOCK(fptr) \
2442 ((struct float_block *) (((uintptr_t) (fptr)) & ~(BLOCK_ALIGN - 1))) 2589 ((struct float_block *) (((uintptr_t) (fptr)) & ~(BLOCK_ALIGN - 1)))
@@ -2448,7 +2595,7 @@ struct float_block
2448{ 2595{
2449 /* Place `floats' at the beginning, to ease up FLOAT_INDEX's job. */ 2596 /* Place `floats' at the beginning, to ease up FLOAT_INDEX's job. */
2450 struct Lisp_Float floats[FLOAT_BLOCK_SIZE]; 2597 struct Lisp_Float floats[FLOAT_BLOCK_SIZE];
2451 int gcmarkbits[1 + FLOAT_BLOCK_SIZE / (sizeof(int) * CHAR_BIT)]; 2598 int gcmarkbits[1 + FLOAT_BLOCK_SIZE / (sizeof (int) * CHAR_BIT)];
2452 struct float_block *next; 2599 struct float_block *next;
2453}; 2600};
2454 2601
@@ -2554,7 +2701,7 @@ struct cons_block
2554{ 2701{
2555 /* Place `conses' at the beginning, to ease up CONS_INDEX's job. */ 2702 /* Place `conses' at the beginning, to ease up CONS_INDEX's job. */
2556 struct Lisp_Cons conses[CONS_BLOCK_SIZE]; 2703 struct Lisp_Cons conses[CONS_BLOCK_SIZE];
2557 int gcmarkbits[1 + CONS_BLOCK_SIZE / (sizeof(int) * CHAR_BIT)]; 2704 int gcmarkbits[1 + CONS_BLOCK_SIZE / (sizeof (int) * CHAR_BIT)];
2558 struct cons_block *next; 2705 struct cons_block *next;
2559}; 2706};
2560 2707
@@ -2859,7 +3006,7 @@ allocate_hash_table (void)
2859struct window * 3006struct window *
2860allocate_window (void) 3007allocate_window (void)
2861{ 3008{
2862 return ALLOCATE_PSEUDOVECTOR(struct window, current_matrix, PVEC_WINDOW); 3009 return ALLOCATE_PSEUDOVECTOR (struct window, current_matrix, PVEC_WINDOW);
2863} 3010}
2864 3011
2865 3012
@@ -3267,12 +3414,16 @@ memory_full (size_t nbytes)
3267 int enough_free_memory = 0; 3414 int enough_free_memory = 0;
3268 if (SPARE_MEMORY < nbytes) 3415 if (SPARE_MEMORY < nbytes)
3269 { 3416 {
3270 void *p = malloc (SPARE_MEMORY); 3417 void *p;
3418
3419 MALLOC_BLOCK_INPUT;
3420 p = malloc (SPARE_MEMORY);
3271 if (p) 3421 if (p)
3272 { 3422 {
3273 free (p); 3423 free (p);
3274 enough_free_memory = 1; 3424 enough_free_memory = 1;
3275 } 3425 }
3426 MALLOC_UNBLOCK_INPUT;
3276 } 3427 }
3277 3428
3278 if (! enough_free_memory) 3429 if (! enough_free_memory)
@@ -3320,7 +3471,7 @@ refill_memory_reserve (void)
3320{ 3471{
3321#ifndef SYSTEM_MALLOC 3472#ifndef SYSTEM_MALLOC
3322 if (spare_memory[0] == 0) 3473 if (spare_memory[0] == 0)
3323 spare_memory[0] = (char *) malloc ((size_t) SPARE_MEMORY); 3474 spare_memory[0] = (char *) malloc (SPARE_MEMORY);
3324 if (spare_memory[1] == 0) 3475 if (spare_memory[1] == 0)
3325 spare_memory[1] = (char *) lisp_align_malloc (sizeof (struct cons_block), 3476 spare_memory[1] = (char *) lisp_align_malloc (sizeof (struct cons_block),
3326 MEM_TYPE_CONS); 3477 MEM_TYPE_CONS);
@@ -3936,7 +4087,7 @@ DEFUN ("gc-status", Fgc_status, Sgc_status, 0, 0, "",
3936{ 4087{
3937 Lisp_Object args[8], zombie_list = Qnil; 4088 Lisp_Object args[8], zombie_list = Qnil;
3938 EMACS_INT i; 4089 EMACS_INT i;
3939 for (i = 0; i < nzombies; i++) 4090 for (i = 0; i < min (MAX_ZOMBIES, nzombies); i++)
3940 zombie_list = Fcons (zombies[i], zombie_list); 4091 zombie_list = Fcons (zombies[i], zombie_list);
3941 args[0] = build_string ("%d GCs, avg live/zombies = %.2f/%.2f (%f%%), max %d/%d\nzombies: %S"); 4092 args[0] = build_string ("%d GCs, avg live/zombies = %.2f/%.2f (%f%%), max %d/%d\nzombies: %S");
3942 args[1] = make_number (ngcs); 4093 args[1] = make_number (ngcs);
@@ -4050,7 +4201,7 @@ mark_maybe_pointer (void *p)
4050 break; 4201 break;
4051 4202
4052 case MEM_TYPE_BUFFER: 4203 case MEM_TYPE_BUFFER:
4053 if (live_buffer_p (m, p) && !VECTOR_MARKED_P((struct buffer *)p)) 4204 if (live_buffer_p (m, p) && !VECTOR_MARKED_P ((struct buffer *)p))
4054 XSETVECTOR (obj, p); 4205 XSETVECTOR (obj, p);
4055 break; 4206 break;
4056 4207
@@ -4100,14 +4251,25 @@ mark_maybe_pointer (void *p)
4100} 4251}
4101 4252
4102 4253
4254/* Alignment of Lisp_Object and pointer values. Use offsetof, as it
4255 sometimes returns a smaller alignment than GCC's __alignof__ and
4256 mark_memory might miss objects if __alignof__ were used. For
4257 example, on x86 with WIDE_EMACS_INT, __alignof__ (Lisp_Object) is 8
4258 but GC_LISP_OBJECT_ALIGNMENT should be 4. */
4259#ifndef GC_LISP_OBJECT_ALIGNMENT
4260# define GC_LISP_OBJECT_ALIGNMENT offsetof (struct {char a; Lisp_Object b;}, b)
4261#endif
4262#define GC_POINTER_ALIGNMENT offsetof (struct {char a; void *b;}, b)
4263
4103/* Mark Lisp objects referenced from the address range START+OFFSET..END 4264/* Mark Lisp objects referenced from the address range START+OFFSET..END
4104 or END+OFFSET..START. */ 4265 or END+OFFSET..START. */
4105 4266
4106static void 4267static void
4107mark_memory (void *start, void *end, int offset) 4268mark_memory (void *start, void *end)
4108{ 4269{
4109 Lisp_Object *p; 4270 Lisp_Object *p;
4110 void **pp; 4271 void **pp;
4272 int i;
4111 4273
4112#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES 4274#if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
4113 nzombies = 0; 4275 nzombies = 0;
@@ -4123,8 +4285,9 @@ mark_memory (void *start, void *end, int offset)
4123 } 4285 }
4124 4286
4125 /* Mark Lisp_Objects. */ 4287 /* Mark Lisp_Objects. */
4126 for (p = (Lisp_Object *) ((char *) start + offset); (void *) p < end; ++p) 4288 for (p = start; (void *) p < end; p++)
4127 mark_maybe_object (*p); 4289 for (i = 0; i < sizeof *p; i += GC_LISP_OBJECT_ALIGNMENT)
4290 mark_maybe_object (*(Lisp_Object *) ((char *) p + i));
4128 4291
4129 /* Mark Lisp data pointed to. This is necessary because, in some 4292 /* Mark Lisp data pointed to. This is necessary because, in some
4130 situations, the C compiler optimizes Lisp objects away, so that 4293 situations, the C compiler optimizes Lisp objects away, so that
@@ -4144,8 +4307,21 @@ mark_memory (void *start, void *end, int offset)
4144 away. The only reference to the life string is through the 4307 away. The only reference to the life string is through the
4145 pointer `s'. */ 4308 pointer `s'. */
4146 4309
4147 for (pp = (void **) ((char *) start + offset); (void *) pp < end; ++pp) 4310 for (pp = start; (void *) pp < end; pp++)
4148 mark_maybe_pointer (*pp); 4311 for (i = 0; i < sizeof *pp; i += GC_POINTER_ALIGNMENT)
4312 {
4313 void *w = *(void **) ((char *) pp + i);
4314 mark_maybe_pointer (w);
4315
4316#ifdef USE_LSB_TAG
4317 /* A host where a Lisp_Object is wider than a pointer might
4318 allocate a Lisp_Object in non-adjacent halves. If
4319 USE_LSB_TAG, the bottom half is not a valid pointer, so
4320 widen it to to a Lisp_Object and check it that way. */
4321 if (sizeof w < sizeof (Lisp_Object))
4322 mark_maybe_object (widen_to_Lisp_Object (w));
4323#endif
4324 }
4149} 4325}
4150 4326
4151/* setjmp will work with GCC unless NON_SAVING_SETJMP is defined in 4327/* setjmp will work with GCC unless NON_SAVING_SETJMP is defined in
@@ -4262,7 +4438,7 @@ dump_zombies (void)
4262{ 4438{
4263 int i; 4439 int i;
4264 4440
4265 fprintf (stderr, "\nZombies kept alive = %"pI":\n", nzombies); 4441 fprintf (stderr, "\nZombies kept alive = %"pI"d:\n", nzombies);
4266 for (i = 0; i < min (MAX_ZOMBIES, nzombies); ++i) 4442 for (i = 0; i < min (MAX_ZOMBIES, nzombies); ++i)
4267 { 4443 {
4268 fprintf (stderr, " %d = ", i); 4444 fprintf (stderr, " %d = ", i);
@@ -4319,15 +4495,11 @@ dump_zombies (void)
4319 pass starting at the start of the stack + 2. Likewise, if the 4495 pass starting at the start of the stack + 2. Likewise, if the
4320 minimal alignment of Lisp_Objects on the stack is 1, four passes 4496 minimal alignment of Lisp_Objects on the stack is 1, four passes
4321 would be necessary, each one starting with one byte more offset 4497 would be necessary, each one starting with one byte more offset
4322 from the stack start. 4498 from the stack start. */
4323
4324 The current code assumes by default that Lisp_Objects are aligned
4325 equally on the stack. */
4326 4499
4327static void 4500static void
4328mark_stack (void) 4501mark_stack (void)
4329{ 4502{
4330 int i;
4331 void *end; 4503 void *end;
4332 4504
4333#ifdef HAVE___BUILTIN_UNWIND_INIT 4505#ifdef HAVE___BUILTIN_UNWIND_INIT
@@ -4385,15 +4557,8 @@ mark_stack (void)
4385 /* This assumes that the stack is a contiguous region in memory. If 4557 /* This assumes that the stack is a contiguous region in memory. If
4386 that's not the case, something has to be done here to iterate 4558 that's not the case, something has to be done here to iterate
4387 over the stack segments. */ 4559 over the stack segments. */
4388#ifndef GC_LISP_OBJECT_ALIGNMENT 4560 mark_memory (stack_base, end);
4389#ifdef __GNUC__ 4561
4390#define GC_LISP_OBJECT_ALIGNMENT __alignof__ (Lisp_Object)
4391#else
4392#define GC_LISP_OBJECT_ALIGNMENT sizeof (Lisp_Object)
4393#endif
4394#endif
4395 for (i = 0; i < sizeof (Lisp_Object); i += GC_LISP_OBJECT_ALIGNMENT)
4396 mark_memory (stack_base, end, i);
4397 /* Allow for marking a secondary stack, like the register stack on the 4562 /* Allow for marking a secondary stack, like the register stack on the
4398 ia64. */ 4563 ia64. */
4399#ifdef GC_MARK_SECONDARY_STACK 4564#ifdef GC_MARK_SECONDARY_STACK
@@ -4415,18 +4580,18 @@ valid_pointer_p (void *p)
4415#ifdef WINDOWSNT 4580#ifdef WINDOWSNT
4416 return w32_valid_pointer_p (p, 16); 4581 return w32_valid_pointer_p (p, 16);
4417#else 4582#else
4418 int fd; 4583 int fd[2];
4419 4584
4420 /* Obviously, we cannot just access it (we would SEGV trying), so we 4585 /* Obviously, we cannot just access it (we would SEGV trying), so we
4421 trick the o/s to tell us whether p is a valid pointer. 4586 trick the o/s to tell us whether p is a valid pointer.
4422 Unfortunately, we cannot use NULL_DEVICE here, as emacs_write may 4587 Unfortunately, we cannot use NULL_DEVICE here, as emacs_write may
4423 not validate p in that case. */ 4588 not validate p in that case. */
4424 4589
4425 if ((fd = emacs_open ("__Valid__Lisp__Object__", O_CREAT | O_WRONLY | O_TRUNC, 0666)) >= 0) 4590 if (pipe (fd) == 0)
4426 { 4591 {
4427 int valid = (emacs_write (fd, (char *)p, 16) == 16); 4592 int valid = (emacs_write (fd[1], (char *) p, 16) == 16);
4428 emacs_close (fd); 4593 emacs_close (fd[1]);
4429 unlink ("__Valid__Lisp__Object__"); 4594 emacs_close (fd[0]);
4430 return valid; 4595 return valid;
4431 } 4596 }
4432 4597
@@ -4846,11 +5011,12 @@ Garbage collection happens automatically if you cons more than
4846`gc-cons-threshold' bytes of Lisp data since previous garbage collection. 5011`gc-cons-threshold' bytes of Lisp data since previous garbage collection.
4847`garbage-collect' normally returns a list with info on amount of space in use: 5012`garbage-collect' normally returns a list with info on amount of space in use:
4848 ((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS) 5013 ((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS)
4849 (USED-MARKERS . FREE-MARKERS) USED-STRING-CHARS USED-VECTOR-SLOTS 5014 (USED-MISCS . FREE-MISCS) USED-STRING-CHARS USED-VECTOR-SLOTS
4850 (USED-FLOATS . FREE-FLOATS) (USED-INTERVALS . FREE-INTERVALS) 5015 (USED-FLOATS . FREE-FLOATS) (USED-INTERVALS . FREE-INTERVALS)
4851 (USED-STRINGS . FREE-STRINGS)) 5016 (USED-STRINGS . FREE-STRINGS))
4852However, if there was overflow in pure space, `garbage-collect' 5017However, if there was overflow in pure space, `garbage-collect'
4853returns nil, because real GC can't be done. */) 5018returns nil, because real GC can't be done.
5019See Info node `(elisp)Garbage Collection'. */)
4854 (void) 5020 (void)
4855{ 5021{
4856 register struct specbinding *bind; 5022 register struct specbinding *bind;
@@ -4922,7 +5088,7 @@ returns nil, because real GC can't be done. */)
4922 if (NILP (Vpurify_flag)) 5088 if (NILP (Vpurify_flag))
4923 { 5089 {
4924 char *stack; 5090 char *stack;
4925 size_t stack_size; 5091 ptrdiff_t stack_size;
4926 if (&stack_top_variable < stack_bottom) 5092 if (&stack_top_variable < stack_bottom)
4927 { 5093 {
4928 stack = &stack_top_variable; 5094 stack = &stack_top_variable;
@@ -5233,7 +5399,7 @@ static int last_marked_index;
5233 links of a list, in mark_object. In debugging, 5399 links of a list, in mark_object. In debugging,
5234 the call to abort will hit a breakpoint. 5400 the call to abort will hit a breakpoint.
5235 Normally this is zero and the check never goes off. */ 5401 Normally this is zero and the check never goes off. */
5236static size_t mark_object_loop_halt; 5402ptrdiff_t mark_object_loop_halt EXTERNALLY_VISIBLE;
5237 5403
5238static void 5404static void
5239mark_vectorlike (struct Lisp_Vector *ptr) 5405mark_vectorlike (struct Lisp_Vector *ptr)
@@ -5290,7 +5456,7 @@ mark_object (Lisp_Object arg)
5290 void *po; 5456 void *po;
5291 struct mem_node *m; 5457 struct mem_node *m;
5292#endif 5458#endif
5293 size_t cdr_count = 0; 5459 ptrdiff_t cdr_count = 0;
5294 5460
5295 loop: 5461 loop:
5296 5462
@@ -5733,7 +5899,7 @@ gc_sweep (void)
5733 int ilim = (lim + BITS_PER_INT - 1) / BITS_PER_INT; 5899 int ilim = (lim + BITS_PER_INT - 1) / BITS_PER_INT;
5734 5900
5735 /* Scan the mark bits an int at a time. */ 5901 /* Scan the mark bits an int at a time. */
5736 for (i = 0; i <= ilim; i++) 5902 for (i = 0; i < ilim; i++)
5737 { 5903 {
5738 if (cblk->gcmarkbits[i] == -1) 5904 if (cblk->gcmarkbits[i] == -1)
5739 { 5905 {
@@ -6113,6 +6279,55 @@ Frames, windows, buffers, and subprocesses count as vectors
6113 return Flist (8, consed); 6279 return Flist (8, consed);
6114} 6280}
6115 6281
6282/* Find at most FIND_MAX symbols which have OBJ as their value or
6283 function. This is used in gdbinit's `xwhichsymbols' command. */
6284
6285Lisp_Object
6286which_symbols (Lisp_Object obj, EMACS_INT find_max)
6287{
6288 struct symbol_block *sblk;
6289 int gc_count = inhibit_garbage_collection ();
6290 Lisp_Object found = Qnil;
6291
6292 if (! DEADP (obj))
6293 {
6294 for (sblk = symbol_block; sblk; sblk = sblk->next)
6295 {
6296 struct Lisp_Symbol *sym = sblk->symbols;
6297 int bn;
6298
6299 for (bn = 0; bn < SYMBOL_BLOCK_SIZE; bn++, sym++)
6300 {
6301 Lisp_Object val;
6302 Lisp_Object tem;
6303
6304 if (sblk == symbol_block && bn >= symbol_block_index)
6305 break;
6306
6307 XSETSYMBOL (tem, sym);
6308 val = find_symbol_value (tem);
6309 if (EQ (val, obj)
6310 || EQ (sym->function, obj)
6311 || (!NILP (sym->function)
6312 && COMPILEDP (sym->function)
6313 && EQ (AREF (sym->function, COMPILED_BYTECODE), obj))
6314 || (!NILP (val)
6315 && COMPILEDP (val)
6316 && EQ (AREF (val, COMPILED_BYTECODE), obj)))
6317 {
6318 found = Fcons (tem, found);
6319 if (--find_max == 0)
6320 goto out;
6321 }
6322 }
6323 }
6324 }
6325
6326 out:
6327 unbind_to (gc_count, Qnil);
6328 return found;
6329}
6330
6116#ifdef ENABLE_CHECKING 6331#ifdef ENABLE_CHECKING
6117int suppress_checking; 6332int suppress_checking;
6118 6333
@@ -6213,7 +6428,7 @@ If this portion is smaller than `gc-cons-threshold', this is ignored. */);
6213 Vgc_cons_percentage = make_float (0.1); 6428 Vgc_cons_percentage = make_float (0.1);
6214 6429
6215 DEFVAR_INT ("pure-bytes-used", pure_bytes_used, 6430 DEFVAR_INT ("pure-bytes-used", pure_bytes_used,
6216 doc: /* Number of bytes of sharable Lisp data allocated so far. */); 6431 doc: /* Number of bytes of shareable Lisp data allocated so far. */);
6217 6432
6218 DEFVAR_INT ("cons-cells-consed", cons_cells_consed, 6433 DEFVAR_INT ("cons-cells-consed", cons_cells_consed,
6219 doc: /* Number of cons cells that have been consed so far. */); 6434 doc: /* Number of cons cells that have been consed so far. */);
@@ -6231,7 +6446,9 @@ If this portion is smaller than `gc-cons-threshold', this is ignored. */);
6231 doc: /* Number of string characters that have been consed so far. */); 6446 doc: /* Number of string characters that have been consed so far. */);
6232 6447
6233 DEFVAR_INT ("misc-objects-consed", misc_objects_consed, 6448 DEFVAR_INT ("misc-objects-consed", misc_objects_consed,
6234 doc: /* Number of miscellaneous objects that have been consed so far. */); 6449 doc: /* Number of miscellaneous objects that have been consed so far.
6450These include markers and overlays, plus certain objects not visible
6451to users. */);
6235 6452
6236 DEFVAR_INT ("intervals-consed", intervals_consed, 6453 DEFVAR_INT ("intervals-consed", intervals_consed,
6237 doc: /* Number of intervals that have been consed so far. */); 6454 doc: /* Number of intervals that have been consed so far. */);