aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Makefile8
-rw-r--r--src/bumpptr_alloc.c106
-rw-r--r--src/malloc.c115
-rw-r--r--src/speedymalloc.c111
4 files changed, 123 insertions, 217 deletions
diff --git a/src/Makefile b/src/Makefile
index 53f8f97..5458801 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -21,13 +21,13 @@ TARGETS = $(addprefix $(OBJDIR)/allocators/,$(ALLOCS)) $(addprefix $(OBJDIR)/,$(
all: $(TARGETS)
-$(OBJDIR)/allocators/bumpptr_alloc.so: bump_alloc.h bumpptr_alloc.c Makefile
+$(OBJDIR)/allocators/bumpptr_alloc.so: bump_alloc.h 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
+ $(CC) $(LDFLAGS) -shared -DMEMSIZE=$(MEMSIZE) $(CFLAGS) -o $@ bumpptr_alloc.c malloc.c
-$(OBJDIR)/allocators/speedymalloc.so: speedymalloc.c Makefile
+$(OBJDIR)/allocators/speedymalloc.so: speedymalloc.c malloc.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 $@ speedymalloc.c malloc.c
$(OBJDIR)/allocators/align_to_cl.so: align_to_cl.c Makefile
@if test \( ! \( -d $(@D) \) \) ;then mkdir -p $(@D);fi
diff --git a/src/bumpptr_alloc.c b/src/bumpptr_alloc.c
index 7be5399..77a11d9 100644
--- a/src/bumpptr_alloc.c
+++ b/src/bumpptr_alloc.c
@@ -2,8 +2,6 @@
#include <stddef.h> /* NULL, size_t */
#include <stdint.h> /* uintptr_t */
#include <stdio.h> /* fprintf */
-#include <unistd.h> /* sysconf(_SC_PAGESIZE) */
-#include <string.h> /* memset */
#include "bump_alloc.h"
@@ -20,115 +18,13 @@ void* malloc(size_t size) {
void free(__attribute__ ((unused)) void* ptr) {
}
-void* realloc(void* ptr, size_t size) {
- if(ptr == NULL)
- return 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) {
return bump_up(size, alignment);
}
-int posix_memalign(void **memptr, size_t alignment, size_t size)
-{
- void *out;
-
- if(memptr == NULL) {
- return 22;
- }
-
- if((alignment % sizeof(void*)) != 0) {
- return 22;
- }
-
- /* if not power of two */
- if(!((alignment != 0) && !(alignment & (alignment - 1)))) {
- return 22;
- }
-
- if(size == 0) {
- *memptr = NULL;
- return 0;
- }
-
- out = bump_up(size, alignment);
- if(out == NULL) {
- return 12;
- }
-
- *memptr = out;
- return 0;
-}
-
-void* calloc(size_t nmemb, size_t size)
-{
- void *out;
- size_t fullsize = nmemb * size;
-
- if((size != 0) && ((fullsize / size) != nmemb)) {
- return NULL;
- }
-
- out = bump_up(fullsize, MIN_ALIGNMENT);
- if(out == NULL) {
- return NULL;
- }
-
- memset(out, 0, fullsize);
- return out;
-}
-
-void* valloc(size_t size)
-{
- long ret = sysconf(_SC_PAGESIZE);
- if(ret == -1) {
- return NULL;
- }
-
- return memalign(ret, size);
-}
-
-void* pvalloc(size_t size)
-{
- size_t ps, rem, allocsize;
-
- long ret = sysconf(_SC_PAGESIZE);
- if(ret == -1) {
- return NULL;
- }
-
- ps = ret;
- rem = size % ps;
- allocsize = size;
- if(rem != 0) {
- allocsize = ps + (size - rem);
- }
-
- return memalign(ps, allocsize);
-}
-
-void* aligned_alloc(size_t alignment, size_t size)
-{
- if(alignment > size) {
- return NULL;
- }
-
- if((size % alignment) != 0) {
- return NULL;
- }
-
- return memalign(alignment, size);
-}
-
-int malloc_stats() {
+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);
- return 0;
}
#ifdef __cplusplus
diff --git a/src/malloc.c b/src/malloc.c
new file mode 100644
index 0000000..4653aa8
--- /dev/null
+++ b/src/malloc.c
@@ -0,0 +1,115 @@
+#include <malloc.h> /* memalign */
+#include <stdlib.h> /* malloc, free */
+#include <stddef.h> /* NULL, size_t */
+#include <stdint.h> /* uintptr_t */
+#include <string.h> /* memcpy */
+#include <unistd.h> /* sysconf */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void* realloc(void* ptr, size_t size) {
+ if(ptr == NULL)
+ return malloc(size);
+
+ void* new_ptr = malloc(size);
+ // this may copies to much
+ memcpy(new_ptr, ptr, size);
+ return new_ptr;
+}
+
+int posix_memalign(void **memptr, size_t alignment, size_t size)
+{
+ void *out;
+
+ if(memptr == NULL) {
+ return 22;
+ }
+
+ if((alignment % sizeof(void*)) != 0) {
+ return 22;
+ }
+
+ /* if not power of two */
+ if(!((alignment != 0) && !(alignment & (alignment - 1)))) {
+ return 22;
+ }
+
+ if(size == 0) {
+ *memptr = NULL;
+ return 0;
+ }
+
+ out = malloc(size);
+ if(out == NULL) {
+ return 12;
+ }
+
+ *memptr = out;
+ return 0;
+}
+
+void* calloc(size_t nmemb, size_t size)
+{
+ void *out;
+ size_t fullsize = nmemb * size;
+
+ if((size != 0) && ((fullsize / size) != nmemb)) {
+ return NULL;
+ }
+
+ out = malloc(fullsize);
+ if(out == NULL) {
+ return NULL;
+ }
+
+ memset(out, 0, fullsize);
+ return out;
+}
+
+void* valloc(size_t size)
+{
+ long ret = sysconf(_SC_PAGESIZE);
+ if(ret == -1) {
+ return NULL;
+ }
+
+ return memalign(ret, size);
+}
+
+void* pvalloc(size_t size)
+{
+ size_t ps, rem, allocsize;
+
+ long ret = sysconf(_SC_PAGESIZE);
+ if(ret == -1) {
+ return NULL;
+ }
+
+ ps = ret;
+ rem = size % ps;
+ allocsize = size;
+ if(rem != 0) {
+ allocsize = ps + (size - rem);
+ }
+
+ return memalign(ps, allocsize);
+}
+
+void* aligned_alloc(size_t alignment, size_t size)
+{
+ if(alignment > size) {
+ return NULL;
+ }
+
+ if((size % alignment) != 0) {
+ return NULL;
+ }
+
+ return memalign(alignment, size);
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/src/speedymalloc.c b/src/speedymalloc.c
index 89e9548..d1edbf3 100644
--- a/src/speedymalloc.c
+++ b/src/speedymalloc.c
@@ -120,21 +120,6 @@ void free(void* ptr) {
}
}
-void* realloc(void* ptr, size_t size) {
- if (ptr == NULL) {
- return malloc(size);
- }
-
- if (size == 0) {
- return NULL;
- }
-
- void* new_ptr = malloc(size);
- size_t to_copy = ptr2chunk(ptr)->size;
- memcpy(new_ptr, ptr, to_copy);
- return new_ptr;
-}
-
void* memalign(size_t alignment, size_t size) {
/* if not power of two */
if (!((alignment != 0) && !(alignment & (alignment - 1)))) {
@@ -159,99 +144,9 @@ void* memalign(size_t alignment, size_t size) {
return ptr;
}
-int posix_memalign(void **memptr, size_t alignment, size_t size)
-{
- void *out;
-
- if (memptr == NULL) {
- return 22;
- }
-
- if ((alignment % sizeof(void*)) != 0) {
- return 22;
- }
-
- /* if not power of two */
- if (!((alignment != 0) && !(alignment & (alignment - 1)))) {
- return 22;
- }
-
- if (size == 0) {
- *memptr = NULL;
- return 0;
- }
-
- out = memalign(alignment, size);
- if (out == NULL) {
- return 12;
- }
-
- *memptr = out;
- return 0;
-}
-
-void* calloc(size_t nmemb, size_t size)
-{
- void *out;
- size_t fullsize = nmemb * size;
-
- if ((size != 0) && ((fullsize / size) != nmemb)) {
- return NULL;
- }
-
- out = malloc(fullsize);
- if (out != NULL)
- memset(out, 0, fullsize);
-
- return out;
-}
-
-void* valloc(size_t size)
-{
- long ret = sysconf(_SC_PAGESIZE);
- if (ret == -1) {
- return NULL;
- }
-
- return memalign(ret, size);
-}
-
-void* pvalloc(size_t size)
-{
- size_t ps, rem, allocsize;
-
- long ret = sysconf(_SC_PAGESIZE);
- if (ret == -1) {
- return NULL;
- }
-
- ps = ret;
- rem = size % ps;
- allocsize = size;
- if (rem != 0) {
- allocsize = ps + (size - rem);
- }
-
- return memalign(ps, allocsize);
-}
-
-void* aligned_alloc(size_t alignment, size_t size)
-{
- if (alignment > size) {
- return NULL;
- }
-
- if ((size % alignment) != 0) {
- return NULL;
- }
-
- return memalign(alignment, size);
-}
-
-int malloc_stats() {
- fprintf(stderr, "Bump pointer allocator by muhq\n");
- fprintf(stderr, "Memsize: %zu, start address: %p, bump pointer %p\n", MEMSIZE, &tls, tls->ptr);
- return 0;
+void malloc_stats() {
+ fprintf(stderr, "speedymalloc allocator by muhq\n");
+ fprintf(stderr, "Memsize: %zu, start address: %p, bump pointer %p\n", MEMSIZE, tls, tls->ptr);
}
#ifdef __cplusplus