diff options
| -rw-r--r-- | src/bump_alloc.h | 33 | ||||
| -rw-r--r-- | src/bumpptr_alloc_single_tsd.c | 2 |
2 files changed, 16 insertions, 19 deletions
diff --git a/src/bump_alloc.h b/src/bump_alloc.h index bc9cbac..08f5786 100644 --- a/src/bump_alloc.h +++ b/src/bump_alloc.h @@ -1,6 +1,7 @@ #include <assert.h> -#include <stddef.h> /* NULL, size_t */ -#include <stdint.h> /* uintptr_t */ +#include <stddef.h> /* NULL, size_t */ +#include <stdint.h> /* uintptr_t */ +#include <sys/mman.h> /* mmap */ #ifndef MEMSIZE #define MEMSIZE 1024*4*1024*1024l @@ -12,36 +13,32 @@ extern "C" { #endif -typedef struct { - uintptr_t end = 0; - uintptr_t ptr = 0; +typedef struct bumpptr { + uintptr_t end; + uintptr_t ptr; } bumpptr_t; __thread bumpptr_t* tsd = NULL; -inline void init_tsd() { - void* mem_start = mmap(NULL, MEMSIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); - if(mem_start == MAP_FAILED) { - perror("mmap"); - return NULL; - } - tsd = (bumpptr_t*)mem_start; - tsd->ptr = (uintptr_t)mem_start + sizeof(bumpptr_t); - tsd->end = (uintptr_t)mem_start + MEMSIZE; -} - inline void* bump_up(size_t size, size_t align) { assert(align % 2 == 0); if (unlikely(tsd == NULL)) { - init_tsd(); + void* mem_start = mmap(NULL, MEMSIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); + if(mem_start == MAP_FAILED) { + perror("mmap"); + return NULL; + } + tsd = (bumpptr_t*)mem_start; + tsd->ptr = (uintptr_t)mem_start + sizeof(bumpptr_t); + tsd->end = (uintptr_t)mem_start + MEMSIZE; } // align ptr; uintptr_t aligned = (tsd->ptr + align - 1) & ~(align - 1); uintptr_t new_ptr = aligned + size; - if (new_ptr > mem_end) + if (new_ptr > tsd->end) return NULL; else { tsd->ptr = new_ptr; diff --git a/src/bumpptr_alloc_single_tsd.c b/src/bumpptr_alloc_single_tsd.c index 7c51bae..7be5399 100644 --- a/src/bumpptr_alloc_single_tsd.c +++ b/src/bumpptr_alloc_single_tsd.c @@ -127,7 +127,7 @@ void* aligned_alloc(size_t alignment, size_t size) int malloc_stats() { fprintf(stderr, "Bump pointer allocator by muhq\n"); - fprintf(stderr, "Memsize: %zu, start address: %p, bump pointer %p\n", MEMSIZE, mem_start, ptr); + fprintf(stderr, "Memsize: %zu, start address: %p, bump pointer %p\n", MEMSIZE, tsd, tsd->ptr); return 0; } |
