diff options
| author | Florian Fischer <florian.fl.fischer@fau.de> | 2020-02-26 18:32:14 +0100 |
|---|---|---|
| committer | Florian Fischer <florian.fl.fischer@fau.de> | 2020-02-26 18:32:14 +0100 |
| commit | a5f8ffa2a6cc2961c3762f43a17b8631949a2b68 (patch) | |
| tree | 5bc0334172c1b909beaeb2f250ce0dfc42ab84a9 /src | |
| parent | 37ae1544e68cbec5605218e7ddb1d6f9a3b702fd (diff) | |
| download | allocbench-a5f8ffa2a6cc2961c3762f43a17b8631949a2b68.tar.gz allocbench-a5f8ffa2a6cc2961c3762f43a17b8631949a2b68.zip | |
inlcude bump_alloc.h into bumpptr_alloc.c
Diffstat (limited to 'src')
| -rw-r--r-- | src/Makefile | 2 | ||||
| -rw-r--r-- | src/bump_alloc.h | 53 | ||||
| -rw-r--r-- | src/bumpptr_alloc.c | 50 |
3 files changed, 42 insertions, 63 deletions
diff --git a/src/Makefile b/src/Makefile index 3e085e2..84179e2 100644 --- a/src/Makefile +++ b/src/Makefile @@ -21,7 +21,7 @@ TARGETS = $(addprefix $(OBJDIR)/allocators/,$(ALLOCS)) $(addprefix $(OBJDIR)/,$( all: $(TARGETS) -$(OBJDIR)/allocators/bumpptr_alloc.so: bump_alloc.h bumpptr_alloc.c malloc.c Makefile +$(OBJDIR)/allocators/bumpptr_alloc.so: bumpptr_alloc.c malloc.c Makefile @if test \( ! \( -d $(@D) \) \) ;then mkdir -p $(@D);fi $(CC) $(LDFLAGS) -shared -DMEMSIZE=$(MEMSIZE) $(CFLAGS) -o $@ bumpptr_alloc.c malloc.c diff --git a/src/bump_alloc.h b/src/bump_alloc.h deleted file mode 100644 index 6a651a1..0000000 --- a/src/bump_alloc.h +++ /dev/null @@ -1,53 +0,0 @@ -#include <assert.h> -#include <errno.h> -#include <stddef.h> /* NULL, size_t */ -#include <stdint.h> /* uintptr_t */ -#include <sys/mman.h> /* mmap */ - -#ifndef MEMSIZE -#define MEMSIZE 1024*4*1024*1024l -#endif - -#define unlikely(x) __builtin_expect((x),0) - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct bumpptr { - uintptr_t end; - uintptr_t ptr; -} bumpptr_t; - -__thread bumpptr_t* tsd = NULL; - -static inline void* bump_up(size_t size, size_t align) { - assert(align % 2 == 0); - - if (unlikely(tsd == NULL)) { - void* mem_start = mmap(NULL, MEMSIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_HUGETLB, -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 > tsd->end) { - errno = ENOMEM; - return NULL; - } else { - tsd->ptr = new_ptr; - return (void*)aligned; - } -} - -#ifdef __cplusplus -} -#endif diff --git a/src/bumpptr_alloc.c b/src/bumpptr_alloc.c index 77a11d9..ee2928a 100644 --- a/src/bumpptr_alloc.c +++ b/src/bumpptr_alloc.c @@ -1,16 +1,52 @@ -#include <errno.h> /* TODO: set errno */ +#include <assert.h> +#include <errno.h> #include <stddef.h> /* NULL, size_t */ #include <stdint.h> /* uintptr_t */ #include <stdio.h> /* fprintf */ - -#include "bump_alloc.h" +#include <sys/mman.h> /* mmap */ #define MIN_ALIGNMENT 16 -#ifdef __cplusplus -extern "C" { +#ifndef MEMSIZE +#define MEMSIZE 1024*4*1024*1024l #endif +#define unlikely(x) __builtin_expect((x),0) + +typedef struct bumpptr { + uintptr_t end; + uintptr_t ptr; +} bumpptr_t; + +__thread bumpptr_t* tsd = NULL; + +static inline void* bump_up(size_t size, size_t align) { + assert(align % 2 == 0); + + if (unlikely(tsd == NULL)) { + void* mem_start = mmap(NULL, MEMSIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_HUGETLB, -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 > tsd->end) { + errno = ENOMEM; + return NULL; + } else { + tsd->ptr = new_ptr; + return (void*)aligned; + } +} + void* malloc(size_t size) { return bump_up(size, MIN_ALIGNMENT); } @@ -26,7 +62,3 @@ void malloc_stats() { fprintf(stderr, "Bump pointer allocator by muhq\n"); fprintf(stderr, "Memsize: %zu, start address: %p, bump pointer %p\n", MEMSIZE, tsd, tsd->ptr); } - -#ifdef __cplusplus -} -#endif |
