aboutsummaryrefslogtreecommitdiff
path: root/src/bump_alloc.h
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fl.fischer@fau.de>2019-11-12 11:12:00 +0100
committerFlorian Fischer <florian.fl.fischer@fau.de>2019-11-12 11:12:00 +0100
commit639c7ca5885b8f47d252754694534685acaaaefb (patch)
treeb967c79e30bd914ffb47ebea4ac576202228bae4 /src/bump_alloc.h
parentd16cc247bbf4f3cb4f9c6ce31bfe1bec4bdb8dd9 (diff)
downloadallocbench-639c7ca5885b8f47d252754694534685acaaaefb.tar.gz
allocbench-639c7ca5885b8f47d252754694534685acaaaefb.zip
speedup bumpptr_alloc
Don't use three individual expensive TSD variables. Externalize and inline bump_up.
Diffstat (limited to 'src/bump_alloc.h')
-rw-r--r--src/bump_alloc.h51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/bump_alloc.h b/src/bump_alloc.h
new file mode 100644
index 0000000..300feef
--- /dev/null
+++ b/src/bump_alloc.h
@@ -0,0 +1,51 @@
+#include <assert.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, -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)
+ return NULL;
+ else {
+ tsd->ptr = new_ptr;
+ return (void*)aligned;
+ }
+}
+
+#ifdef __cplusplus
+}
+#endif