aboutsummaryrefslogtreecommitdiff
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
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.
-rw-r--r--src/Makefile4
-rw-r--r--src/bump_alloc.h51
-rw-r--r--src/bumpptr_alloc.c53
3 files changed, 63 insertions, 45 deletions
diff --git a/src/Makefile b/src/Makefile
index 6a46af9..6d25bf9 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -21,9 +21,9 @@ TARGETS = $(addprefix $(OBJDIR)/allocators/,$(ALLOCS)) $(addprefix $(OBJDIR)/,$(
all: $(TARGETS)
-$(OBJDIR)/allocators/bumpptr_alloc.so: bumpptr_alloc.c Makefile
+$(OBJDIR)/allocators/bumpptr_alloc.so: bump_alloc.h bumpptr_alloc.c Makefile
@if test \( ! \( -d $(@D) \) \) ;then mkdir -p $(@D);fi
- $(CC) $(LDFLAGS) -shared -DMEMSIZE=$(MEMSIZE) $(CFLAGS) -o $@ $<
+ $(CC) $(LDFLAGS) -shared -DMEMSIZE=$(MEMSIZE) $(CFLAGS) -o $@ bumpptr_alloc.c
$(OBJDIR)/allocators/speedymalloc.so: speedymalloc.c Makefile
@if test \( ! \( -d $(@D) \) \) ;then mkdir -p $(@D);fi
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
diff --git a/src/bumpptr_alloc.c b/src/bumpptr_alloc.c
index 3985904..7be5399 100644
--- a/src/bumpptr_alloc.c
+++ b/src/bumpptr_alloc.c
@@ -1,47 +1,20 @@
-#include <errno.h>
+#include <errno.h> /* TODO: set errno */
#include <stddef.h> /* NULL, size_t */
#include <stdint.h> /* uintptr_t */
-#include <stdio.h> /* uintptr_t */
+#include <stdio.h> /* fprintf */
#include <unistd.h> /* sysconf(_SC_PAGESIZE) */
#include <string.h> /* memset */
-#include <sys/mman.h> /* memset */
-#define MIN_ALIGNMENT 16
-
-#ifndef MEMSIZE
-#define MEMSIZE 1024*4*1024*1024l
-#endif
+#include "bump_alloc.h"
-#define unlikely(x) __builtin_expect((x),0)
+#define MIN_ALIGNMENT 16
#ifdef __cplusplus
extern "C" {
#endif
-__thread void* mem_start = NULL;
-__thread void* mem_end = NULL;
-
-__thread uintptr_t ptr = 0;
-
void* malloc(size_t size) {
- if (unlikely(mem_start == NULL)) {
- mem_start = mmap(NULL, MEMSIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
- if(mem_start == MAP_FAILED) {
- perror("mmap");
- return NULL;
- }
- ptr = (uintptr_t)mem_start;
- }
-
- // align ptr;
- if (ptr % MIN_ALIGNMENT != 0) {
- size_t mask = MIN_ALIGNMENT -1;
- ptr = (ptr + mask) & ~mask;
- }
-
- void* ret = (void*)ptr;
- ptr += size;
- return ret;
+ return bump_up(size, MIN_ALIGNMENT);
}
void free(__attribute__ ((unused)) void* ptr) {
@@ -51,20 +24,14 @@ void* realloc(void* ptr, size_t size) {
if(ptr == NULL)
return malloc(size);
- if(size == 0)
- return NULL;
-
- void* new_ptr = malloc(size);
+ void* new_ptr = bump_up(size, MIN_ALIGNMENT);
// this may copies to much
memcpy(new_ptr, ptr, size);
return new_ptr;
}
void* memalign(size_t alignment, size_t size) {
- // align ptr to alignment and malloc
- size_t mask = alignment - 1;
- ptr = (ptr + mask) & ~mask;
- return malloc(size);
+ return bump_up(size, alignment);
}
int posix_memalign(void **memptr, size_t alignment, size_t size)
@@ -89,7 +56,7 @@ int posix_memalign(void **memptr, size_t alignment, size_t size)
return 0;
}
- out = memalign(alignment, size);
+ out = bump_up(size, alignment);
if(out == NULL) {
return 12;
}
@@ -107,7 +74,7 @@ void* calloc(size_t nmemb, size_t size)
return NULL;
}
- out = malloc(fullsize);
+ out = bump_up(fullsize, MIN_ALIGNMENT);
if(out == NULL) {
return NULL;
}
@@ -160,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;
}