diff --git a/malloc.c b/malloc.c index 2629e8c..7ba8c28 100644 --- a/malloc.c +++ b/malloc.c @@ -176,7 +176,7 @@ static struct slab_metadata *alloc_metadata(struct size_class *c, size_t slab_si errno = ENOMEM; return NULL; } - size_t allocate = c->metadata_allocated * 2; + size_t allocate = max(c->metadata_allocated * 2, PAGE_SIZE / sizeof(struct slab_metadata)); if (allocate > metadata_max) { allocate = metadata_max; } @@ -825,10 +825,6 @@ COLD static void init_slow_path(void) { if (c->slab_info == NULL) { fatal_error("failed to allocate slab metadata"); } - c->metadata_allocated = PAGE_SIZE / sizeof(struct slab_metadata); - if (memory_protect_rw(c->slab_info, c->metadata_allocated * sizeof(struct slab_metadata))) { - fatal_error("failed to allocate initial slab info"); - } } deallocate_pages(rng, sizeof(struct random_state), PAGE_SIZE); diff --git a/util.h b/util.h index c6951d6..8698326 100644 --- a/util.h +++ b/util.h @@ -7,6 +7,18 @@ #define likely(x) __builtin_expect(!!(x), 1) #define unlikely(x) __builtin_expect(!!(x), 0) +#define min(x, y) ({ \ + __typeof__(x) _x = (x); \ + __typeof__(y) _y = (y); \ + (void) (&_x == &_y); \ + _x < _y ? _x : _y; }) + +#define max(x, y) ({ \ + __typeof__(x) _x = (x); \ + __typeof__(y) _y = (y); \ + (void) (&_x == &_y); \ + _x > _y ? _x : _y; }) + #define COLD __attribute__((cold)) #define UNUSED __attribute__((unused)) #define EXPORT __attribute__((visibility("default")))