diff options
author | otto <> | 2011-05-18 18:07:20 +0000 |
---|---|---|
committer | otto <> | 2011-05-18 18:07:20 +0000 |
commit | b97cdc2e94b7b8949f7db50f8437fe790dcd1a41 (patch) | |
tree | e4452ec1b652a3397db8d25f4ba5a3acdad794a0 /src/lib | |
parent | d45d754e827ce79028a6fbdc3cb81ace2deedc16 (diff) | |
download | openbsd-b97cdc2e94b7b8949f7db50f8437fe790dcd1a41.tar.gz openbsd-b97cdc2e94b7b8949f7db50f8437fe790dcd1a41.tar.bz2 openbsd-b97cdc2e94b7b8949f7db50f8437fe790dcd1a41.zip |
zap regions_bits and rework MALLOC_MAXSHIFT a bit; ok djm@
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/libc/stdlib/malloc.c | 22 |
1 files changed, 8 insertions, 14 deletions
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c index b803501d41..2a1ec5e21e 100644 --- a/src/lib/libc/stdlib/malloc.c +++ b/src/lib/libc/stdlib/malloc.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: malloc.c,v 1.134 2011/05/12 12:03:40 otto Exp $ */ | 1 | /* $OpenBSD: malloc.c,v 1.135 2011/05/18 18:07:20 otto Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net> | 3 | * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net> |
4 | * | 4 | * |
@@ -49,9 +49,6 @@ | |||
49 | 49 | ||
50 | #include "thread_private.h" | 50 | #include "thread_private.h" |
51 | 51 | ||
52 | #define MALLOC_MINSHIFT 4 | ||
53 | #define MALLOC_MAXSHIFT 16 | ||
54 | |||
55 | #if defined(__sparc__) && !defined(__sparcv9__) | 52 | #if defined(__sparc__) && !defined(__sparcv9__) |
56 | #define MALLOC_PAGESHIFT (13U) | 53 | #define MALLOC_PAGESHIFT (13U) |
57 | #elif defined(__mips64__) | 54 | #elif defined(__mips64__) |
@@ -60,12 +57,14 @@ | |||
60 | #define MALLOC_PAGESHIFT (PGSHIFT) | 57 | #define MALLOC_PAGESHIFT (PGSHIFT) |
61 | #endif | 58 | #endif |
62 | 59 | ||
60 | #define MALLOC_MINSHIFT 4 | ||
61 | #define MALLOC_MAXSHIFT (MALLOC_PAGESHIFT - 1) | ||
63 | #define MALLOC_PAGESIZE (1UL << MALLOC_PAGESHIFT) | 62 | #define MALLOC_PAGESIZE (1UL << MALLOC_PAGESHIFT) |
64 | #define MALLOC_MINSIZE (1UL << MALLOC_MINSHIFT) | 63 | #define MALLOC_MINSIZE (1UL << MALLOC_MINSHIFT) |
65 | #define MALLOC_PAGEMASK (MALLOC_PAGESIZE - 1) | 64 | #define MALLOC_PAGEMASK (MALLOC_PAGESIZE - 1) |
66 | #define MASK_POINTER(p) ((void *)(((uintptr_t)(p)) & ~MALLOC_PAGEMASK)) | 65 | #define MASK_POINTER(p) ((void *)(((uintptr_t)(p)) & ~MALLOC_PAGEMASK)) |
67 | 66 | ||
68 | #define MALLOC_MAXCHUNK (1 << (MALLOC_PAGESHIFT-1)) | 67 | #define MALLOC_MAXCHUNK (1 << MALLOC_MAXSHIFT) |
69 | #define MALLOC_MAXCACHE 256 | 68 | #define MALLOC_MAXCACHE 256 |
70 | #define MALLOC_DELAYED_CHUNKS 15 /* max of getrnibble() */ | 69 | #define MALLOC_DELAYED_CHUNKS 15 /* max of getrnibble() */ |
71 | /* | 70 | /* |
@@ -106,12 +105,11 @@ struct dir_info { | |||
106 | u_int32_t canary1; | 105 | u_int32_t canary1; |
107 | struct region_info *r; /* region slots */ | 106 | struct region_info *r; /* region slots */ |
108 | size_t regions_total; /* number of region slots */ | 107 | size_t regions_total; /* number of region slots */ |
109 | size_t regions_bits; /* log2 of total */ | ||
110 | size_t regions_free; /* number of free slots */ | 108 | size_t regions_free; /* number of free slots */ |
111 | /* list of free chunk info structs */ | 109 | /* list of free chunk info structs */ |
112 | struct chunk_head chunk_info_list; | 110 | struct chunk_head chunk_info_list; |
113 | /* lists of chunks with free slots */ | 111 | /* lists of chunks with free slots */ |
114 | struct chunk_head chunk_dir[MALLOC_MAXSHIFT]; | 112 | struct chunk_head chunk_dir[MALLOC_MAXSHIFT + 1]; |
115 | size_t free_regions_size; /* free pages cached */ | 113 | size_t free_regions_size; /* free pages cached */ |
116 | /* free pages cache */ | 114 | /* free pages cache */ |
117 | struct region_info free_regions[MALLOC_MAXCACHE]; | 115 | struct region_info free_regions[MALLOC_MAXCACHE]; |
@@ -605,8 +603,7 @@ omalloc_init(struct dir_info **dp) | |||
605 | d = (struct dir_info *)(p + MALLOC_PAGESIZE + | 603 | d = (struct dir_info *)(p + MALLOC_PAGESIZE + |
606 | (arc4random_uniform(d_avail) << MALLOC_MINSHIFT)); | 604 | (arc4random_uniform(d_avail) << MALLOC_MINSHIFT)); |
607 | 605 | ||
608 | d->regions_bits = 9; | 606 | d->regions_free = d->regions_total = 512; |
609 | d->regions_free = d->regions_total = 1 << d->regions_bits; | ||
610 | regioninfo_size = d->regions_total * sizeof(struct region_info); | 607 | regioninfo_size = d->regions_total * sizeof(struct region_info); |
611 | d->r = MMAP(regioninfo_size); | 608 | d->r = MMAP(regioninfo_size); |
612 | if (d->r == MAP_FAILED) { | 609 | if (d->r == MAP_FAILED) { |
@@ -615,7 +612,7 @@ omalloc_init(struct dir_info **dp) | |||
615 | return 1; | 612 | return 1; |
616 | } | 613 | } |
617 | LIST_INIT(&d->chunk_info_list); | 614 | LIST_INIT(&d->chunk_info_list); |
618 | for (i = 0; i < MALLOC_MAXSHIFT; i++) | 615 | for (i = 0; i <= MALLOC_MAXSHIFT; i++) |
619 | LIST_INIT(&d->chunk_dir[i]); | 616 | LIST_INIT(&d->chunk_dir[i]); |
620 | malloc_used += regioninfo_size; | 617 | malloc_used += regioninfo_size; |
621 | d->canary1 = mopts.malloc_canary ^ (u_int32_t)(uintptr_t)d; | 618 | d->canary1 = mopts.malloc_canary ^ (u_int32_t)(uintptr_t)d; |
@@ -636,7 +633,6 @@ omalloc_init(struct dir_info **dp) | |||
636 | static int | 633 | static int |
637 | omalloc_grow(struct dir_info *d) | 634 | omalloc_grow(struct dir_info *d) |
638 | { | 635 | { |
639 | size_t newbits; | ||
640 | size_t newtotal; | 636 | size_t newtotal; |
641 | size_t newsize; | 637 | size_t newsize; |
642 | size_t mask; | 638 | size_t mask; |
@@ -646,7 +642,6 @@ omalloc_grow(struct dir_info *d) | |||
646 | if (d->regions_total > SIZE_MAX / sizeof(struct region_info) / 2 ) | 642 | if (d->regions_total > SIZE_MAX / sizeof(struct region_info) / 2 ) |
647 | return 1; | 643 | return 1; |
648 | 644 | ||
649 | newbits = d->regions_bits + 1; | ||
650 | newtotal = d->regions_total * 2; | 645 | newtotal = d->regions_total * 2; |
651 | newsize = newtotal * sizeof(struct region_info); | 646 | newsize = newtotal * sizeof(struct region_info); |
652 | mask = newtotal - 1; | 647 | mask = newtotal - 1; |
@@ -678,7 +673,6 @@ omalloc_grow(struct dir_info *d) | |||
678 | malloc_used -= d->regions_total * sizeof(struct region_info); | 673 | malloc_used -= d->regions_total * sizeof(struct region_info); |
679 | d->regions_free = d->regions_free + d->regions_total; | 674 | d->regions_free = d->regions_free + d->regions_total; |
680 | d->regions_total = newtotal; | 675 | d->regions_total = newtotal; |
681 | d->regions_bits = newbits; | ||
682 | d->r = p; | 676 | d->r = p; |
683 | return 0; | 677 | return 0; |
684 | } | 678 | } |
@@ -1530,7 +1524,7 @@ dump_free_chunk_info(int fd, struct dir_info *d) | |||
1530 | 1524 | ||
1531 | snprintf(buf, sizeof(buf), "Free chunk structs:\n"); | 1525 | snprintf(buf, sizeof(buf), "Free chunk structs:\n"); |
1532 | write(fd, buf, strlen(buf)); | 1526 | write(fd, buf, strlen(buf)); |
1533 | for (i = 0; i < MALLOC_MAXSHIFT; i++) { | 1527 | for (i = 0; i <= MALLOC_MAXSHIFT; i++) { |
1534 | struct chunk_info *p = LIST_FIRST(&d->chunk_dir[i]); | 1528 | struct chunk_info *p = LIST_FIRST(&d->chunk_dir[i]); |
1535 | if (p != NULL) { | 1529 | if (p != NULL) { |
1536 | snprintf(buf, sizeof(buf), "%2d) ", i); | 1530 | snprintf(buf, sizeof(buf), "%2d) ", i); |