diff --git a/.gitignore b/.gitignore index c3ba4cd..e549ed7 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,8 @@ libft_malloc* test gpttest +a.out +tester # Prerequisites *.d diff --git a/Makefile b/Makefile index 4139e25..361be4f 100644 --- a/Makefile +++ b/Makefile @@ -39,8 +39,8 @@ re: fclean all rere: ffclean all test: $(NAME) - $(CC) $(CFLAGS) -o test test.c -L. -lft_malloc -Wl,-rpath=. - $(CC) $(CFLAGS) -o gpttest gpttest.c -L. -lft_malloc -Wl,-rpath=. + $(CC) $(CFLAGS) -O0 -o test test.c -L. -lft_malloc -Wl,-rpath=. + $(CC) $(CFLAGS) -O0 -o gpttest gpttest.c -L. -lft_malloc -Wl,-rpath=. $(LIBFT): make -C libft diff --git a/include/malloc.h b/include/malloc.h index 638f594..8c859a8 100644 --- a/include/malloc.h +++ b/include/malloc.h @@ -12,6 +12,7 @@ #define MMAP_SHIFT(mmap) ((void *)mmap + sizeof(t_mmap)) #define ALLOC_SHIFT(alloc) ((void *)alloc + sizeof(t_alloc)) +#define ALIGNMENT 16 #define TINY_MMAP_SIZE (4 * getpagesize()) #define TINY_ALLOC_SIZE (128) #define SMALL_MMAP_SIZE (32 * getpagesize()) diff --git a/src/alloc.c b/src/alloc.c index 537c747..ca38c3e 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -1,11 +1,17 @@ #include "../include/malloc.h" +void *alignp(void *size) { + intptr_t s = (intptr_t)size; + while (s % ALIGNMENT != 0) + s++; + /* ft_printf("before %p, after %p\n", size, (void *)s); */ + return (void *)s; +} + t_alloc *new_alloc(t_mmap *mmap, void *ptr, size_t size) { t_alloc *alloc = ptr; alloc->size = size; alloc->parent = mmap; - /* ft_printf("creating new alloc at %p. should return %p\n", ptr, */ - /* ALLOC_SHIFT(ptr)); */ alloc->next = NULL; alloc->prev = NULL; @@ -34,24 +40,26 @@ t_alloc *new_alloc(t_mmap *mmap, void *ptr, size_t size) { } t_alloc *find_alloc(size_t size) { + size += ALIGNMENT; // add space to align the return ptr if (!g_mmap) return NULL; for (t_mmap *mmap = g_mmap; mmap; mmap = mmap->next) { - if (mmap->type != get_mmap_type(size)) + if (mmap->type != get_mmap_type(size - ALIGNMENT)) continue; // in case it's the first allocation of a mmap if (!mmap->alloc) - return new_alloc(mmap, MMAP_SHIFT(mmap), size); + return new_alloc(mmap, alignp(MMAP_SHIFT(mmap)), size); + /* return new_alloc(mmap, MMAP_SHIFT(mmap), size); */ t_alloc *alloc = mmap->alloc; - void *ptr = MMAP_SHIFT(mmap); + void *ptr = alignp(MMAP_SHIFT(mmap)); while (alloc) { // seach for space between allocated spaces size_t space = (void *)alloc - ptr; if (ptr < (void *)alloc && space >= (size_t)ALLOC_SHIFT(size)) return new_alloc(mmap, ptr, size); - ptr = ALLOC_SHIFT(alloc) + alloc->size; + ptr = alignp(ALLOC_SHIFT(alloc) + alloc->size); alloc = alloc->next; } // allocated space after the last allocated space diff --git a/src/calloc.c b/src/calloc.c index 2acc623..4c7706b 100644 --- a/src/calloc.c +++ b/src/calloc.c @@ -2,22 +2,19 @@ void *calloc(size_t nmemb, size_t size) { ft_printf("calloc(%d)", size); - /* size_t overflow = nmemb * size; */ - /* if (size && overflow / size != nmemb) { */ - /* // TODO: rsyslog() detected overflow */ - /* return NULL; */ - /* } */ + size_t overflow = nmemb * size; + if (size && overflow / size != nmemb) { + ft_printf("OVERFLOW\n"); + // TODO: rsyslog() detected overflow + return NULL; + } - /* if (nmemb * size == 0) */ - /* return NULL; */ + if (nmemb * size == 0) + return NULL; - lock_mutex(); // TODO: rsyslog() - void *res; - if (nmemb * size == 0) - res = _malloc(1); - else - res = _malloc(nmemb * size); + lock_mutex(); + void *res = _malloc(nmemb * size); unlock_mutex(); ft_bzero(res, nmemb * size); ft_printf(" -> %p\n", res); diff --git a/src/free.c b/src/free.c index e52dc52..218e641 100644 --- a/src/free.c +++ b/src/free.c @@ -24,13 +24,12 @@ void _free(void *ptr) { return; if (!g_mmap) return; - /* t_alloc *alloc = ptr - sizeof(t_alloc); */ - t_alloc *alloc = find_alloc_ptr(ptr); + t_alloc *alloc = ptr - sizeof(t_alloc); + /* t_alloc *alloc = find_alloc_ptr(ptr); */ if (!alloc) { - ft_printf("tryed free(%p) but it's not an alloc\n", ptr); + ft_printf("tryed _free(%p) but it's not an alloc\n", ptr); return; } - ft_printf("free: (%p)->size %d\n", ptr, alloc->size); if (alloc->prev) alloc->prev->next = alloc->next; if (alloc->next) @@ -54,8 +53,8 @@ void free(void *ptr) { ft_printf("free(%p)\n", ptr); if (!ptr) return; - lock_mutex(); // TODO: rsyslog() + lock_mutex(); _free(ptr); unlock_mutex(); } diff --git a/src/global_init.c b/src/global_init.c index 7aa475c..82c2387 100644 --- a/src/global_init.c +++ b/src/global_init.c @@ -2,7 +2,10 @@ t_mmap *g_mmap = NULL; -pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER; +pthread_mutex_t *get_mutex() { + static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + return &mutex; +} -void lock_mutex() { pthread_mutex_lock(&g_mutex); } -void unlock_mutex() { pthread_mutex_unlock(&g_mutex); } +void lock_mutex() { pthread_mutex_lock(get_mutex()); } +void unlock_mutex() { pthread_mutex_unlock(get_mutex()); } diff --git a/src/malloc.c b/src/malloc.c index 02174bb..56f22c4 100644 --- a/src/malloc.c +++ b/src/malloc.c @@ -6,20 +6,19 @@ void *_malloc(size_t size) { return ALLOC_SHIFT(res); new_mmap(size); res = find_alloc(size); + if (!res) + ft_printf("NO SPACE FOUND ON NEW MMAP\n"); return ALLOC_SHIFT(res); } void *malloc(size_t size) { ft_printf("malloc(%d)", size); - /* if (!size) */ - /* size = 1; */ - if (size == 0) + if (!size) return NULL; - lock_mutex(); // TODO: rsyslog() + lock_mutex(); void *res = _malloc(size); unlock_mutex(); ft_printf(" -> %p\n", res); - show_alloc_mem(); return res; } diff --git a/src/mmap.c b/src/mmap.c index 68d1731..a9f1ea0 100644 --- a/src/mmap.c +++ b/src/mmap.c @@ -2,10 +2,10 @@ size_t get_mmap_size(size_t size) { if (size <= TINY_ALLOC_SIZE) - return TINY_MMAP_SIZE; + return TINY_MMAP_SIZE + ALIGNMENT; if (size <= SMALL_ALLOC_SIZE) - return SMALL_MMAP_SIZE; - return size + sizeof(t_alloc); + return SMALL_MMAP_SIZE + ALIGNMENT; + return size + sizeof(t_alloc) + ALIGNMENT; } t_type get_mmap_type(size_t size) { @@ -17,9 +17,8 @@ t_type get_mmap_type(size_t size) { } t_mmap *new_mmap(size_t size) { - t_mmap *map = - mmap(NULL, get_mmap_size(size) + sizeof(t_mmap), PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + t_mmap *map = mmap(NULL, get_mmap_size(size) + sizeof(t_mmap), + PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); if (!map) { errno = ENOMEM; return NULL; diff --git a/src/realloc.c b/src/realloc.c index f795919..809b244 100644 --- a/src/realloc.c +++ b/src/realloc.c @@ -31,13 +31,14 @@ void *_realloc(void *ptr, size_t size) { void *realloc(void *ptr, size_t size) { ft_printf("realloc(%d) on %p", size, ptr); - lock_mutex(); // TODO: rsyslog() + lock_mutex(); void *res = NULL; if (!ptr) res = _malloc(size); else if (!size && ptr) { _free(ptr); + // TODO: should remove this is useless res = NULL; } else res = _realloc(ptr, size); diff --git a/src/reallocarray.c b/src/reallocarray.c index 8e6cca5..f069435 100644 --- a/src/reallocarray.c +++ b/src/reallocarray.c @@ -11,9 +11,9 @@ void *reallocarray(void *ptr, size_t nmemb, size_t size) { } if (nmemb * size == 0) return NULL; - lock_mutex(); // TODO: rsyslog() void *res; + lock_mutex(); if (!ptr) res = _malloc(size * nmemb); else if ((size * nmemb) == 0 && ptr) { diff --git a/src/show_alloc_mem.c b/src/show_alloc_mem.c index 7f71d2d..4faedf3 100644 --- a/src/show_alloc_mem.c +++ b/src/show_alloc_mem.c @@ -10,13 +10,16 @@ char *get_type_string(t_type type) { void show_alloc_mem() { if (!g_mmap) { - ft_putstr("No memory allocated\n"); + ft_putstr("show_alloc_mem(): No memory allocated\n"); return; } size_t total = 0; for (t_mmap *mmap = g_mmap; mmap; mmap = mmap->next) { ft_printf("%s : %p\n", get_type_string(mmap->type), mmap); for (t_alloc *alloc = mmap->alloc; alloc; alloc = alloc->next) { + if (!alloc) { + ft_printf("alloc is NULL\n"); + } ft_printf("%p - %p : %d bytes\n", ALLOC_SHIFT(alloc), ALLOC_SHIFT(alloc) + alloc->size, alloc->size); if (alloc->next == alloc) { diff --git a/test.c b/test.c index 0a942c1..4f8f88a 100644 --- a/test.c +++ b/test.c @@ -1,38 +1,48 @@ #include "include/malloc.h" +#include "libft/libft.h" #include +#include #include #include int main(void) { - int len = 100; - char *a = malloc(len); - ft_printf("a(%p)\n", a); - ft_bzero(a, len); - char *a1 = malloc(len); - ft_bzero(a1, len); - ft_printf("a(%p)\n", a1); - a = malloc(len); - ft_printf("a(%p)\n", a); - ft_bzero(a, len); - free(a1); - a = malloc(len); - ft_printf("a(%p)\n", a); - ft_bzero(a, len); + /* int len = 100; */ + /* char *a = malloc(len); */ + /* ft_printf("a(%p)\n", a); */ + /* ft_bzero(a, len); */ + /* char *a1 = malloc(len); */ + /* ft_bzero(a1, len); */ + /* ft_printf("a(%p)\n", a1); */ + /* a = malloc(len); */ + /* ft_printf("a(%p)\n", a); */ + /* ft_bzero(a, len); */ + /* free(a1); */ + /* a = malloc(len); */ + /* ft_printf("a(%p)\n", a); */ + /* ft_bzero(a, len); */ + /**/ + /* len = 4000; */ + /* char *b = malloc(len); */ + /* ft_printf("b(%p)\n", b); */ + /* ft_bzero(b, len); */ + /**/ + /* len = 500; */ + /* char *c = malloc(len); */ + /* ft_printf("c(%p)\n", c); */ + /* ft_bzero(c, len); */ - len = 4000; - char *b = malloc(len); - ft_printf("b(%p)\n", b); - ft_bzero(b, len); - - len = 500; - char *c = malloc(len); - ft_printf("c(%p)\n", c); - ft_bzero(c, len); - - /* ft_printf("allocating 1000 times\n"); */ - /* for (int i = 0; i < 1000; i++) { */ - /* char *d = malloc(i); */ - /* ft_bzero(d, i); */ - /* } */ + ft_printf("allocating 5000 times\n"); + for (int i = 1; i < 50000; i += 3) { + char *d = malloc(i); + /* d = realloc(d, i); */ + /* d = realloc(d, i - 1); */ + /* d = realloc(d, i + 1); */ + ((void)d); + ft_bzero(d, i); + bzero(d, i); + ft_memset(d, 'a', i); + memset(d, 'b', i); + free(d); + } ft_putchar('\n'); show_alloc_mem(); diff --git a/testreal.c b/testreal.c new file mode 100644 index 0000000..435caa9 --- /dev/null +++ b/testreal.c @@ -0,0 +1,40 @@ +#include "libft/libft.h" +#include +#include +#include +#include +int main(void) { + int len = 100; + char *a = malloc(len); + ft_printf("a(%p)\n", a); + ft_bzero(a, len); + char *a1 = malloc(len); + ft_bzero(a1, len); + ft_printf("a(%p)\n", a1); + a = malloc(len); + ft_printf("a(%p)\n", a); + ft_bzero(a, len); + free(a1); + a = malloc(len); + ft_printf("a(%p)\n", a); + ft_bzero(a, len); + + len = 4000; + char *b = malloc(len); + ft_printf("b(%p)\n", b); + ft_bzero(b, len); + + len = 500; + char *c = malloc(len); + ft_printf("c(%p)\n", c); + ft_bzero(c, len); + + /* ft_printf("allocating 1000 times\n"); */ + /* for (int i = 0; i < 1000; i++) { */ + /* char *d = malloc(i); */ + /* ft_bzero(d, i); */ + /* } */ + + ft_putchar('\n'); + return EXIT_SUCCESS; +}