diff options
Diffstat (limited to 'src/alloc.c')
| -rw-r--r-- | src/alloc.c | 85 |
1 files changed, 43 insertions, 42 deletions
diff --git a/src/alloc.c b/src/alloc.c index c07d5c929f9..05d2db274da 100644 --- a/src/alloc.c +++ b/src/alloc.c | |||
| @@ -49,16 +49,24 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ | |||
| 49 | #include <setjmp.h> | 49 | #include <setjmp.h> |
| 50 | #include <verify.h> | 50 | #include <verify.h> |
| 51 | 51 | ||
| 52 | /* GC_CHECK_MARKED_OBJECTS means do sanity checks on allocated objects. | ||
| 53 | Doable only if GC_MARK_STACK. */ | ||
| 54 | #if ! GC_MARK_STACK | ||
| 55 | # undef GC_CHECK_MARKED_OBJECTS | ||
| 56 | #endif | ||
| 57 | |||
| 52 | /* GC_MALLOC_CHECK defined means perform validity checks of malloc'd | 58 | /* GC_MALLOC_CHECK defined means perform validity checks of malloc'd |
| 53 | memory. Can do this only if using gmalloc.c. */ | 59 | memory. Can do this only if using gmalloc.c and if not checking |
| 60 | marked objects. */ | ||
| 54 | 61 | ||
| 55 | #if defined SYSTEM_MALLOC || defined DOUG_LEA_MALLOC | 62 | #if (defined SYSTEM_MALLOC || defined DOUG_LEA_MALLOC \ |
| 63 | || defined GC_CHECK_MARKED_OBJECTS) | ||
| 56 | #undef GC_MALLOC_CHECK | 64 | #undef GC_MALLOC_CHECK |
| 57 | #endif | 65 | #endif |
| 58 | 66 | ||
| 59 | #include <unistd.h> | 67 | #include <unistd.h> |
| 60 | #ifndef HAVE_UNISTD_H | 68 | #ifndef HAVE_UNISTD_H |
| 61 | extern POINTER_TYPE *sbrk (); | 69 | extern void *sbrk (); |
| 62 | #endif | 70 | #endif |
| 63 | 71 | ||
| 64 | #include <fcntl.h> | 72 | #include <fcntl.h> |
| @@ -298,7 +306,7 @@ enum mem_type | |||
| 298 | MEM_TYPE_VECTORLIKE | 306 | MEM_TYPE_VECTORLIKE |
| 299 | }; | 307 | }; |
| 300 | 308 | ||
| 301 | static POINTER_TYPE *lisp_malloc (size_t, enum mem_type); | 309 | static void *lisp_malloc (size_t, enum mem_type); |
| 302 | 310 | ||
| 303 | 311 | ||
| 304 | #if GC_MARK_STACK || defined GC_MALLOC_CHECK | 312 | #if GC_MARK_STACK || defined GC_MALLOC_CHECK |
| @@ -380,7 +388,7 @@ static struct mem_node mem_z; | |||
| 380 | #define MEM_NIL &mem_z | 388 | #define MEM_NIL &mem_z |
| 381 | 389 | ||
| 382 | static struct Lisp_Vector *allocate_vectorlike (ptrdiff_t); | 390 | static struct Lisp_Vector *allocate_vectorlike (ptrdiff_t); |
| 383 | static void lisp_free (POINTER_TYPE *); | 391 | static void lisp_free (void *); |
| 384 | static void mark_stack (void); | 392 | static void mark_stack (void); |
| 385 | static int live_vector_p (struct mem_node *, void *); | 393 | static int live_vector_p (struct mem_node *, void *); |
| 386 | static int live_buffer_p (struct mem_node *, void *); | 394 | static int live_buffer_p (struct mem_node *, void *); |
| @@ -391,11 +399,8 @@ static int live_float_p (struct mem_node *, void *); | |||
| 391 | static int live_misc_p (struct mem_node *, void *); | 399 | static int live_misc_p (struct mem_node *, void *); |
| 392 | static void mark_maybe_object (Lisp_Object); | 400 | static void mark_maybe_object (Lisp_Object); |
| 393 | static void mark_memory (void *, void *); | 401 | static void mark_memory (void *, void *); |
| 402 | #if GC_MARK_STACK || defined GC_MALLOC_CHECK | ||
| 394 | static void mem_init (void); | 403 | static void mem_init (void); |
| 395 | #if (defined GC_MALLOC_CHECK \ | ||
| 396 | ? !defined SYSTEM_MALLOC && !defined SYNC_INPUT \ | ||
| 397 | : GC_MARK_STACK) | ||
| 398 | # define NEED_MEM_INSERT | ||
| 399 | static struct mem_node *mem_insert (void *, void *, enum mem_type); | 404 | static struct mem_node *mem_insert (void *, void *, enum mem_type); |
| 400 | static void mem_insert_fixup (struct mem_node *); | 405 | static void mem_insert_fixup (struct mem_node *); |
| 401 | #endif | 406 | #endif |
| @@ -430,15 +435,15 @@ static Lisp_Object *staticvec[NSTATICS] = {&Vpurify_flag}; | |||
| 430 | 435 | ||
| 431 | static int staticidx = 0; | 436 | static int staticidx = 0; |
| 432 | 437 | ||
| 433 | static POINTER_TYPE *pure_alloc (size_t, int); | 438 | static void *pure_alloc (size_t, int); |
| 434 | 439 | ||
| 435 | 440 | ||
| 436 | /* Value is SZ rounded up to the next multiple of ALIGNMENT. | 441 | /* Value is SZ rounded up to the next multiple of ALIGNMENT. |
| 437 | ALIGNMENT must be a power of 2. */ | 442 | ALIGNMENT must be a power of 2. */ |
| 438 | 443 | ||
| 439 | #define ALIGN(ptr, ALIGNMENT) \ | 444 | #define ALIGN(ptr, ALIGNMENT) \ |
| 440 | ((POINTER_TYPE *) ((((uintptr_t) (ptr)) + (ALIGNMENT) - 1) \ | 445 | ((void *) (((uintptr_t) (ptr) + (ALIGNMENT) - 1) \ |
| 441 | & ~((ALIGNMENT) - 1))) | 446 | & ~ ((ALIGNMENT) - 1))) |
| 442 | 447 | ||
| 443 | 448 | ||
| 444 | 449 | ||
| @@ -599,7 +604,7 @@ static ptrdiff_t check_depth; | |||
| 599 | 604 | ||
| 600 | /* Like malloc, but wraps allocated block with header and trailer. */ | 605 | /* Like malloc, but wraps allocated block with header and trailer. */ |
| 601 | 606 | ||
| 602 | static POINTER_TYPE * | 607 | static void * |
| 603 | overrun_check_malloc (size_t size) | 608 | overrun_check_malloc (size_t size) |
| 604 | { | 609 | { |
| 605 | register unsigned char *val; | 610 | register unsigned char *val; |
| @@ -617,15 +622,15 @@ overrun_check_malloc (size_t size) | |||
| 617 | XMALLOC_OVERRUN_CHECK_SIZE); | 622 | XMALLOC_OVERRUN_CHECK_SIZE); |
| 618 | } | 623 | } |
| 619 | --check_depth; | 624 | --check_depth; |
| 620 | return (POINTER_TYPE *)val; | 625 | return val; |
| 621 | } | 626 | } |
| 622 | 627 | ||
| 623 | 628 | ||
| 624 | /* Like realloc, but checks old block for overrun, and wraps new block | 629 | /* Like realloc, but checks old block for overrun, and wraps new block |
| 625 | with header and trailer. */ | 630 | with header and trailer. */ |
| 626 | 631 | ||
| 627 | static POINTER_TYPE * | 632 | static void * |
| 628 | overrun_check_realloc (POINTER_TYPE *block, size_t size) | 633 | overrun_check_realloc (void *block, size_t size) |
| 629 | { | 634 | { |
| 630 | register unsigned char *val = (unsigned char *) block; | 635 | register unsigned char *val = (unsigned char *) block; |
| 631 | int overhead = ++check_depth == 1 ? XMALLOC_OVERRUN_CHECK_OVERHEAD : 0; | 636 | int overhead = ++check_depth == 1 ? XMALLOC_OVERRUN_CHECK_OVERHEAD : 0; |
| @@ -647,7 +652,7 @@ overrun_check_realloc (POINTER_TYPE *block, size_t size) | |||
| 647 | memset (val, 0, XMALLOC_OVERRUN_CHECK_SIZE + XMALLOC_OVERRUN_SIZE_SIZE); | 652 | memset (val, 0, XMALLOC_OVERRUN_CHECK_SIZE + XMALLOC_OVERRUN_SIZE_SIZE); |
| 648 | } | 653 | } |
| 649 | 654 | ||
| 650 | val = (unsigned char *) realloc ((POINTER_TYPE *)val, size + overhead); | 655 | val = realloc (val, size + overhead); |
| 651 | 656 | ||
| 652 | if (val && check_depth == 1) | 657 | if (val && check_depth == 1) |
| 653 | { | 658 | { |
| @@ -658,13 +663,13 @@ overrun_check_realloc (POINTER_TYPE *block, size_t size) | |||
| 658 | XMALLOC_OVERRUN_CHECK_SIZE); | 663 | XMALLOC_OVERRUN_CHECK_SIZE); |
| 659 | } | 664 | } |
| 660 | --check_depth; | 665 | --check_depth; |
| 661 | return (POINTER_TYPE *)val; | 666 | return val; |
| 662 | } | 667 | } |
| 663 | 668 | ||
| 664 | /* Like free, but checks block for overrun. */ | 669 | /* Like free, but checks block for overrun. */ |
| 665 | 670 | ||
| 666 | static void | 671 | static void |
| 667 | overrun_check_free (POINTER_TYPE *block) | 672 | overrun_check_free (void *block) |
| 668 | { | 673 | { |
| 669 | unsigned char *val = (unsigned char *) block; | 674 | unsigned char *val = (unsigned char *) block; |
| 670 | 675 | ||
| @@ -713,13 +718,13 @@ overrun_check_free (POINTER_TYPE *block) | |||
| 713 | 718 | ||
| 714 | /* Like malloc but check for no memory and block interrupt input.. */ | 719 | /* Like malloc but check for no memory and block interrupt input.. */ |
| 715 | 720 | ||
| 716 | POINTER_TYPE * | 721 | void * |
| 717 | xmalloc (size_t size) | 722 | xmalloc (size_t size) |
| 718 | { | 723 | { |
| 719 | register POINTER_TYPE *val; | 724 | void *val; |
| 720 | 725 | ||
| 721 | MALLOC_BLOCK_INPUT; | 726 | MALLOC_BLOCK_INPUT; |
| 722 | val = (POINTER_TYPE *) malloc (size); | 727 | val = malloc (size); |
| 723 | MALLOC_UNBLOCK_INPUT; | 728 | MALLOC_UNBLOCK_INPUT; |
| 724 | 729 | ||
| 725 | if (!val && size) | 730 | if (!val && size) |
| @@ -730,18 +735,18 @@ xmalloc (size_t size) | |||
| 730 | 735 | ||
| 731 | /* Like realloc but check for no memory and block interrupt input.. */ | 736 | /* Like realloc but check for no memory and block interrupt input.. */ |
| 732 | 737 | ||
| 733 | POINTER_TYPE * | 738 | void * |
| 734 | xrealloc (POINTER_TYPE *block, size_t size) | 739 | xrealloc (void *block, size_t size) |
| 735 | { | 740 | { |
| 736 | register POINTER_TYPE *val; | 741 | void *val; |
| 737 | 742 | ||
| 738 | MALLOC_BLOCK_INPUT; | 743 | MALLOC_BLOCK_INPUT; |
| 739 | /* We must call malloc explicitly when BLOCK is 0, since some | 744 | /* We must call malloc explicitly when BLOCK is 0, since some |
| 740 | reallocs don't do this. */ | 745 | reallocs don't do this. */ |
| 741 | if (! block) | 746 | if (! block) |
| 742 | val = (POINTER_TYPE *) malloc (size); | 747 | val = malloc (size); |
| 743 | else | 748 | else |
| 744 | val = (POINTER_TYPE *) realloc (block, size); | 749 | val = realloc (block, size); |
| 745 | MALLOC_UNBLOCK_INPUT; | 750 | MALLOC_UNBLOCK_INPUT; |
| 746 | 751 | ||
| 747 | if (!val && size) | 752 | if (!val && size) |
| @@ -753,7 +758,7 @@ xrealloc (POINTER_TYPE *block, size_t size) | |||
| 753 | /* Like free but block interrupt input. */ | 758 | /* Like free but block interrupt input. */ |
| 754 | 759 | ||
| 755 | void | 760 | void |
| 756 | xfree (POINTER_TYPE *block) | 761 | xfree (void *block) |
| 757 | { | 762 | { |
| 758 | if (!block) | 763 | if (!block) |
| 759 | return; | 764 | return; |
| @@ -888,7 +893,7 @@ safe_alloca_unwind (Lisp_Object arg) | |||
| 888 | static void *lisp_malloc_loser; | 893 | static void *lisp_malloc_loser; |
| 889 | #endif | 894 | #endif |
| 890 | 895 | ||
| 891 | static POINTER_TYPE * | 896 | static void * |
| 892 | lisp_malloc (size_t nbytes, enum mem_type type) | 897 | lisp_malloc (size_t nbytes, enum mem_type type) |
| 893 | { | 898 | { |
| 894 | register void *val; | 899 | register void *val; |
| @@ -933,7 +938,7 @@ lisp_malloc (size_t nbytes, enum mem_type type) | |||
| 933 | call to lisp_malloc. */ | 938 | call to lisp_malloc. */ |
| 934 | 939 | ||
| 935 | static void | 940 | static void |
| 936 | lisp_free (POINTER_TYPE *block) | 941 | lisp_free (void *block) |
| 937 | { | 942 | { |
| 938 | MALLOC_BLOCK_INPUT; | 943 | MALLOC_BLOCK_INPUT; |
| 939 | free (block); | 944 | free (block); |
| @@ -1029,7 +1034,7 @@ static struct ablock *free_ablock; | |||
| 1029 | /* Allocate an aligned block of nbytes. | 1034 | /* Allocate an aligned block of nbytes. |
| 1030 | Alignment is on a multiple of BLOCK_ALIGN and `nbytes' has to be | 1035 | Alignment is on a multiple of BLOCK_ALIGN and `nbytes' has to be |
| 1031 | smaller or equal to BLOCK_BYTES. */ | 1036 | smaller or equal to BLOCK_BYTES. */ |
| 1032 | static POINTER_TYPE * | 1037 | static void * |
| 1033 | lisp_align_malloc (size_t nbytes, enum mem_type type) | 1038 | lisp_align_malloc (size_t nbytes, enum mem_type type) |
| 1034 | { | 1039 | { |
| 1035 | void *base, *val; | 1040 | void *base, *val; |
| @@ -1136,7 +1141,7 @@ lisp_align_malloc (size_t nbytes, enum mem_type type) | |||
| 1136 | } | 1141 | } |
| 1137 | 1142 | ||
| 1138 | static void | 1143 | static void |
| 1139 | lisp_align_free (POINTER_TYPE *block) | 1144 | lisp_align_free (void *block) |
| 1140 | { | 1145 | { |
| 1141 | struct ablock *ablock = block; | 1146 | struct ablock *ablock = block; |
| 1142 | struct ablocks *abase = ABLOCK_ABASE (ablock); | 1147 | struct ablocks *abase = ABLOCK_ABASE (ablock); |
| @@ -1309,7 +1314,7 @@ emacs_blocked_malloc (size_t size, const void *ptr) | |||
| 1309 | { | 1314 | { |
| 1310 | fprintf (stderr, "Malloc returned %p which is already in use\n", | 1315 | fprintf (stderr, "Malloc returned %p which is already in use\n", |
| 1311 | value); | 1316 | value); |
| 1312 | fprintf (stderr, "Region in use is %p...%p, %u bytes, type %d\n", | 1317 | fprintf (stderr, "Region in use is %p...%p, %td bytes, type %d\n", |
| 1313 | m->start, m->end, (char *) m->end - (char *) m->start, | 1318 | m->start, m->end, (char *) m->end - (char *) m->start, |
| 1314 | m->type); | 1319 | m->type); |
| 1315 | abort (); | 1320 | abort (); |
| @@ -3579,8 +3584,6 @@ mem_find (void *start) | |||
| 3579 | } | 3584 | } |
| 3580 | 3585 | ||
| 3581 | 3586 | ||
| 3582 | #ifdef NEED_MEM_INSERT | ||
| 3583 | |||
| 3584 | /* Insert a new node into the tree for a block of memory with start | 3587 | /* Insert a new node into the tree for a block of memory with start |
| 3585 | address START, end address END, and type TYPE. Value is a | 3588 | address START, end address END, and type TYPE. Value is a |
| 3586 | pointer to the node that was inserted. */ | 3589 | pointer to the node that was inserted. */ |
| @@ -3728,8 +3731,6 @@ mem_insert_fixup (struct mem_node *x) | |||
| 3728 | mem_root->color = MEM_BLACK; | 3731 | mem_root->color = MEM_BLACK; |
| 3729 | } | 3732 | } |
| 3730 | 3733 | ||
| 3731 | #endif /* NEED_MEM_INSERT */ | ||
| 3732 | |||
| 3733 | 3734 | ||
| 3734 | /* (x) (y) | 3735 | /* (x) (y) |
| 3735 | / \ / \ | 3736 | / \ / \ |
| @@ -4297,8 +4298,8 @@ mark_maybe_pointer (void *p) | |||
| 4297 | wider than a pointer might allocate a Lisp_Object in non-adjacent halves. | 4298 | wider than a pointer might allocate a Lisp_Object in non-adjacent halves. |
| 4298 | If USE_LSB_TAG, the bottom half is not a valid pointer, but it should | 4299 | If USE_LSB_TAG, the bottom half is not a valid pointer, but it should |
| 4299 | suffice to widen it to to a Lisp_Object and check it that way. */ | 4300 | suffice to widen it to to a Lisp_Object and check it that way. */ |
| 4300 | #if defined USE_LSB_TAG || UINTPTR_MAX >> VALBITS != 0 | 4301 | #if defined USE_LSB_TAG || VAL_MAX < UINTPTR_MAX |
| 4301 | # if !defined USE_LSB_TAG && UINTPTR_MAX >> VALBITS >> GCTYPEBITS != 0 | 4302 | # if !defined USE_LSB_TAG && VAL_MAX < UINTPTR_MAX >> GCTYPEBITS |
| 4302 | /* If tag bits straddle pointer-word boundaries, neither mark_maybe_pointer | 4303 | /* If tag bits straddle pointer-word boundaries, neither mark_maybe_pointer |
| 4303 | nor mark_maybe_object can follow the pointers. This should not occur on | 4304 | nor mark_maybe_object can follow the pointers. This should not occur on |
| 4304 | any practical porting target. */ | 4305 | any practical porting target. */ |
| @@ -4722,10 +4723,10 @@ valid_lisp_object_p (Lisp_Object obj) | |||
| 4722 | pointer to it. TYPE is the Lisp type for which the memory is | 4723 | pointer to it. TYPE is the Lisp type for which the memory is |
| 4723 | allocated. TYPE < 0 means it's not used for a Lisp object. */ | 4724 | allocated. TYPE < 0 means it's not used for a Lisp object. */ |
| 4724 | 4725 | ||
| 4725 | static POINTER_TYPE * | 4726 | static void * |
| 4726 | pure_alloc (size_t size, int type) | 4727 | pure_alloc (size_t size, int type) |
| 4727 | { | 4728 | { |
| 4728 | POINTER_TYPE *result; | 4729 | void *result; |
| 4729 | #ifdef USE_LSB_TAG | 4730 | #ifdef USE_LSB_TAG |
| 4730 | size_t alignment = (1 << GCTYPEBITS); | 4731 | size_t alignment = (1 << GCTYPEBITS); |
| 4731 | #else | 4732 | #else |
| @@ -5838,7 +5839,7 @@ mark_buffer (Lisp_Object buf) | |||
| 5838 | } | 5839 | } |
| 5839 | 5840 | ||
| 5840 | /* Mark the Lisp pointers in the terminal objects. | 5841 | /* Mark the Lisp pointers in the terminal objects. |
| 5841 | Called by the Fgarbage_collector. */ | 5842 | Called by Fgarbage_collect. */ |
| 5842 | 5843 | ||
| 5843 | static void | 5844 | static void |
| 5844 | mark_terminals (void) | 5845 | mark_terminals (void) |