aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Makefile4
-rw-r--r--src/bump_alloc.h2
-rw-r--r--src/bumpptr_alloc.c40
-rw-r--r--src/bumpptr_alloc_always_align.c2
4 files changed, 23 insertions, 25 deletions
diff --git a/src/Makefile b/src/Makefile
index 9b22b28..40329bd 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -29,9 +29,9 @@ $(OBJDIR)/allocators/bumpptr_alloc_always_align.so: bumpptr_alloc_always_align.c
@if test \( ! \( -d $(@D) \) \) ;then mkdir -p $(@D);fi
$(CC) $(LDFLAGS) -shared -DMEMSIZE=$(MEMSIZE) $(CFLAGS) -o $@ $<
-$(OBJDIR)/allocators/bumpptr_alloc_single_tsd.so: bumpptr_alloc_single_tsd.c Makefile
+$(OBJDIR)/allocators/bumpptr_alloc_single_tsd.so: bump_alloc.h bumpptr_alloc_single_tsd.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_single_tsd.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
index 08f5786..300feef 100644
--- a/src/bump_alloc.h
+++ b/src/bump_alloc.h
@@ -20,7 +20,7 @@ typedef struct bumpptr {
__thread bumpptr_t* tsd = NULL;
-inline void* bump_up(size_t size, size_t align) {
+static inline void* bump_up(size_t size, size_t align) {
assert(align % 2 == 0);
if (unlikely(tsd == NULL)) {
diff --git a/src/bumpptr_alloc.c b/src/bumpptr_alloc.c
index 6cb82df..3985904 100644
--- a/src/bumpptr_alloc.c
+++ b/src/bumpptr_alloc.c
@@ -1,4 +1,3 @@
-#include <assert.h>
#include <errno.h>
#include <stddef.h> /* NULL, size_t */
#include <stdint.h> /* uintptr_t */
@@ -20,12 +19,11 @@ extern "C" {
#endif
__thread void* mem_start = NULL;
-__thread uintptr_t mem_end = 0;
-__thread uintptr_t ptr = 0;
+__thread void* mem_end = NULL;
-inline void* bump_up(size_t size, size_t align) {
- assert(align % 2 == 0);
+__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) {
@@ -33,23 +31,17 @@ inline void* bump_up(size_t size, size_t align) {
return NULL;
}
ptr = (uintptr_t)mem_start;
- mem_end = ptr + MEMSIZE;
}
// align ptr;
- uintptr_t aligned = (ptr + align - 1) & ~(align - 1);
-
- uintptr_t new_ptr = aligned + size;
- if (new_ptr > mem_end)
- return NULL;
- else {
- ptr = new_ptr;
- return (void*)aligned;
+ if (ptr % MIN_ALIGNMENT != 0) {
+ size_t mask = MIN_ALIGNMENT -1;
+ ptr = (ptr + mask) & ~mask;
}
-}
-void* malloc(size_t size) {
- return bump_up(size, MIN_ALIGNMENT);
+ void* ret = (void*)ptr;
+ ptr += size;
+ return ret;
}
void free(__attribute__ ((unused)) void* ptr) {
@@ -59,14 +51,20 @@ void* realloc(void* ptr, size_t size) {
if(ptr == NULL)
return malloc(size);
- void* new_ptr = bump_up(size, MIN_ALIGNMENT);
+ if(size == 0)
+ return NULL;
+
+ void* new_ptr = malloc(size);
// this may copies to much
memcpy(new_ptr, ptr, size);
return new_ptr;
}
void* memalign(size_t alignment, size_t size) {
- return bump_up(size, alignment);
+ // align ptr to alignment and malloc
+ size_t mask = alignment - 1;
+ ptr = (ptr + mask) & ~mask;
+ return malloc(size);
}
int posix_memalign(void **memptr, size_t alignment, size_t size)
@@ -91,7 +89,7 @@ int posix_memalign(void **memptr, size_t alignment, size_t size)
return 0;
}
- out = bump_up(size, alignment);
+ out = memalign(alignment, size);
if(out == NULL) {
return 12;
}
@@ -109,7 +107,7 @@ void* calloc(size_t nmemb, size_t size)
return NULL;
}
- out = bump_up(fullsize, MIN_ALIGNMENT);
+ out = malloc(fullsize);
if(out == NULL) {
return NULL;
}
diff --git a/src/bumpptr_alloc_always_align.c b/src/bumpptr_alloc_always_align.c
index 6cb82df..4f1ba5f 100644
--- a/src/bumpptr_alloc_always_align.c
+++ b/src/bumpptr_alloc_always_align.c
@@ -23,7 +23,7 @@ __thread void* mem_start = NULL;
__thread uintptr_t mem_end = 0;
__thread uintptr_t ptr = 0;
-inline void* bump_up(size_t size, size_t align) {
+static inline void* bump_up(size_t size, size_t align) {
assert(align % 2 == 0);
if (unlikely(mem_start == NULL)) {