From 3d7869132b0dbada361d75fe3da787a186e55aea Mon Sep 17 00:00:00 2001 From: mirsella Date: Fri, 29 Sep 2023 18:43:24 +0200 Subject: [PATCH] test --- Makefile | 4 ++-- include/malloc.h | 5 +++-- src/alloc.c | 29 +++++++++++++++-------------- src/calloc.c | 8 +++++--- src/malloc.c | 11 ++++++++--- src/mmap.c | 4 ++-- src/realloc.c | 11 ++++++----- src/show_alloc_mem.c | 6 +++--- test.c | 13 ++++++++----- 9 files changed, 52 insertions(+), 39 deletions(-) diff --git a/Makefile b/Makefile index 62cc5b8..b97cb12 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ OBJS = $(SRCS:.c=.o) DEPS = $(SRCS:.c=.d) CC = cc -CFLAGS = -Wall -Wextra -Werror -MMD -fPIC -pthread +CFLAGS = -Wall -Wextra -Werror -MMD -fPIC -pthread -pedantic -fsanitize=address HOSTNAME := $(shell hostname) @@ -38,7 +38,7 @@ re: fclean all rere: ffclean all test: $(NAME) - $(CC) -g3 -Werror -Wall -Wextra -MMD -O0 -o test test.c -L. -lft_malloc -Wl,-rpath=. + $(CC) -g3 -Werror -Wall -Wextra -MMD -O0 -o test test.c ./$(NAME) $(LIBFT) $(LIBFT): make -C libft diff --git a/include/malloc.h b/include/malloc.h index 22a1387..29c79c7 100644 --- a/include/malloc.h +++ b/include/malloc.h @@ -15,8 +15,8 @@ #define LOGGING false #define LOGFILE "/tmp/malloc.log" -#define MMAP_SHIFT(mmap) ((void *)mmap + sizeof(t_mmap)) -#define ALLOC_SHIFT(alloc) ((void *)alloc + sizeof(t_alloc)) +#define MMAP_SHIFT(mmap) ((size_t)mmap + sizeof(t_mmap)) +#define ALLOC_SHIFT(alloc) ((size_t)alloc + sizeof(t_alloc)) #define ALIGNMENT 16 // 8 on 32 bits, 16 on 64 bits #define TINY_MMAP_SIZE (4 * getpagesize()) @@ -72,6 +72,7 @@ void *calloc(size_t nmemb, size_t size); void *reallocarray(void *ptr, size_t nmemb, size_t size); // alloc.c +void *alignp(size_t size); t_alloc *find_alloc_ptr(void *ptr); size_t get_alloc_size(size_t size); t_alloc *find_alloc(size_t size); diff --git a/src/alloc.c b/src/alloc.c index 06c36bb..4415f02 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -4,14 +4,16 @@ // alloc t_alloc *find_alloc_ptr(void *ptr) { for (t_mmap *mmap = g_mmap; mmap; mmap = mmap->next) { - if (ptr < (void *)mmap || ptr > MMAP_SHIFT(mmap) + mmap->size) + if ((size_t)ptr < (size_t)mmap || + (size_t)ptr > MMAP_SHIFT(mmap) + mmap->size) continue; for (t_alloc *alloc = mmap->alloc; alloc; alloc = alloc->next) { - if (ptr > ALLOC_SHIFT(alloc) + alloc->size) + if ((size_t)ptr > ALLOC_SHIFT(alloc) + alloc->size) continue; - if (ptr < (void *)alloc) + if ((size_t)ptr < (size_t)alloc) return NULL; - if (ptr < ALLOC_SHIFT(alloc) + alloc->size && ptr >= ALLOC_SHIFT(alloc)) + if ((size_t)ptr < ALLOC_SHIFT(alloc) + alloc->size && + (size_t)ptr >= ALLOC_SHIFT(alloc)) return alloc; return NULL; } @@ -19,7 +21,7 @@ t_alloc *find_alloc_ptr(void *ptr) { return NULL; } -void *alignp(void *size) { +void *alignp(size_t size) { intptr_t s = (intptr_t)size; while (s % ALIGNMENT != 0) s++; @@ -61,27 +63,26 @@ t_alloc *find_alloc(size_t size) { 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, alignp(MMAP_SHIFT(mmap)), size); + return new_alloc(mmap, (void *)MMAP_SHIFT(mmap), size); t_alloc *alloc = mmap->alloc; - void *ptr = alignp(MMAP_SHIFT(mmap)); - // + ALIGNMENT because we need to have space to align the alloc - size_t needed_space = size + sizeof(t_alloc) + ALIGNMENT; + void *ptr = (void *)MMAP_SHIFT(mmap); + size_t needed_space = size + sizeof(t_alloc); while (alloc) { // seach for space between allocated spaces - size_t space = (void *)alloc - ptr; - if (ptr < (void *)alloc && space >= needed_space) + size_t space = (size_t)alloc - (size_t)ptr; + if ((size_t)ptr < (size_t)alloc && space >= needed_space) return new_alloc(mmap, ptr, size); - ptr = alignp(ALLOC_SHIFT(alloc) + alloc->size); + ptr = (void *)((size_t)ALLOC_SHIFT(alloc) + alloc->size); alloc = alloc->next; } // allocated space after the last allocated space - if ((size_t)(MMAP_SHIFT(mmap) + mmap->size - ptr) >= needed_space) + if ((size_t)(MMAP_SHIFT(mmap) + mmap->size - (size_t)ptr) >= needed_space) return new_alloc(mmap, ptr, size); } return NULL; diff --git a/src/calloc.c b/src/calloc.c index 04a7d99..536875b 100644 --- a/src/calloc.c +++ b/src/calloc.c @@ -10,7 +10,8 @@ void *calloc(size_t nmemb, size_t size) { return NULL; } - if (nmemb * size == 0) + size = nmemb * size; + if (size == 0) size = 1; while (size % ALIGNMENT != 0) size++; @@ -19,8 +20,9 @@ void *calloc(size_t nmemb, size_t size) { /* flog("calloc(): ", size * nmemb); */ } lock_mutex(); - void *res = _malloc(nmemb * size); + void *res = _malloc(size); unlock_mutex(); - ft_bzero(res, nmemb * size); + if (res) + ft_bzero(res, size); return res; } diff --git a/src/malloc.c b/src/malloc.c index 1015052..32d7aff 100644 --- a/src/malloc.c +++ b/src/malloc.c @@ -1,12 +1,15 @@ #include "../include/malloc.h" void *_malloc(size_t size) { + size += ALIGNMENT; t_alloc *res = find_alloc(size); if (res) - return ALLOC_SHIFT(res); - new_mmap(size); + return alignp(ALLOC_SHIFT(res)); + new_mmap(size - ALIGNMENT); res = find_alloc(size); - return ALLOC_SHIFT(res); + if (!res) + return NULL; + return alignp(ALLOC_SHIFT(res)); } void *malloc(size_t size) { @@ -21,5 +24,7 @@ void *malloc(size_t size) { lock_mutex(); void *res = _malloc(size); unlock_mutex(); + if (!res) + ft_printf("malloc returned null\n"); return res; } diff --git a/src/mmap.c b/src/mmap.c index 2e9137d..28a95dc 100644 --- a/src/mmap.c +++ b/src/mmap.c @@ -2,9 +2,9 @@ size_t get_mmap_size(size_t size) { if (size <= TINY_ALLOC_SIZE) - return TINY_MMAP_SIZE + ALIGNMENT; + return TINY_MMAP_SIZE; if (size <= SMALL_ALLOC_SIZE) - return SMALL_MMAP_SIZE + ALIGNMENT; + return SMALL_MMAP_SIZE; return size + sizeof(t_alloc) + ALIGNMENT; } diff --git a/src/realloc.c b/src/realloc.c index 760163b..dcecb69 100644 --- a/src/realloc.c +++ b/src/realloc.c @@ -3,6 +3,7 @@ void *_realloc(void *ptr, size_t size) { if (!ptr || !size) return NULL; + size += ALIGNMENT; /* t_alloc *alloc = ptr - sizeof(t_alloc); */ t_alloc *alloc = find_alloc_ptr(ptr); if (!alloc) { @@ -17,20 +18,20 @@ void *_realloc(void *ptr, size_t size) { // reduce space if (alloc->size >= size) { alloc->size = size; - return ALLOC_SHIFT(alloc); + return ptr; } // find bigger space if (alloc->next && (size_t)alloc->next - (size_t)ptr >= size) { alloc->size = size; - return ALLOC_SHIFT(alloc); + return alignp(ALLOC_SHIFT(alloc)); } else if (!alloc->next && (size_t)MMAP_SHIFT(map) + map->size - (size_t)ptr >= size) { alloc->size = size; - return ALLOC_SHIFT(alloc); + return alignp(ALLOC_SHIFT(alloc)); } else { - void *new = _malloc(size); - ft_memcpy(new, ALLOC_SHIFT(alloc), alloc->size); + void *new = _malloc(size - ALIGNMENT); + ft_memcpy(new, alignp(ALLOC_SHIFT(alloc)), alloc->size); _free(ptr); return new; } diff --git a/src/show_alloc_mem.c b/src/show_alloc_mem.c index d863200..0350be0 100644 --- a/src/show_alloc_mem.c +++ b/src/show_alloc_mem.c @@ -50,7 +50,7 @@ void asciidump(void *ptr, size_t size) { b = *((unsigned char *)ptr + i); if (ft_isprint(b)) ft_printf("%c", b); - else if (is_end(ptr + i + 1, size - i - 1)) { + else if (is_end((void *)((size_t)ptr + i + 1), size - i - 1)) { ft_printf("\\0"); return; } else @@ -83,7 +83,7 @@ void show_alloc_mem_asciidump() { continue; ft_printf("%p - %p : %d bytes\n", ALLOC_SHIFT(alloc), ALLOC_SHIFT(alloc) + alloc->size, alloc->size); - asciidump(ALLOC_SHIFT(alloc), alloc->size); + asciidump((void *)ALLOC_SHIFT(alloc), alloc->size); ft_putstr("\n\n"); if (alloc->next == alloc) { ft_printf("detected infinite loop on alloc at %p\n", @@ -112,7 +112,7 @@ void show_alloc_mem_hexdump() { continue; ft_printf("%p - %p : %d bytes\n", ALLOC_SHIFT(alloc), ALLOC_SHIFT(alloc) + alloc->size, alloc->size); - hexdump(ALLOC_SHIFT(alloc), alloc->size); + hexdump((void *)ALLOC_SHIFT(alloc), alloc->size); ft_putstr("\n\n"); if (alloc->next == alloc) { ft_printf("detected infinite loop on alloc at %p\n", diff --git a/test.c b/test.c index 3c6702d..3fdcce7 100644 --- a/test.c +++ b/test.c @@ -6,11 +6,11 @@ #include int main() { - // 1. Basic functionality tests char *str = (char *)malloc(20); assert(str != NULL); - strcpy(str, "42helloworld"); + ft_strlcpy(str, "42helloworld", 20); assert(strcmp(str, "42helloworld") == 0); + ft_bzero(str, 20); free(str); int *arr = (int *)calloc(5, sizeof(int)); @@ -18,20 +18,23 @@ int main() { for (int i = 0; i < 5; i++) { assert(arr[i] == 0); } + ft_bzero(arr, 5 * sizeof(int)); free(arr); char *dynstr = (char *)malloc(5); assert(dynstr != NULL); - strcpy(dynstr, "Test"); + ft_strlcpy(dynstr, "Test", 5); dynstr = realloc(dynstr, 10); - strcat(dynstr, "1234"); - assert(strcmp(dynstr, "Test1234") == 0); + ft_strlcat(dynstr, "1234", 10); + assert(ft_strcmp(dynstr, "Test1234") == 0); + ft_bzero(dynstr, 10); free(dynstr); void *edge1 = malloc(0); free(edge1); char *edge2 = (char *)malloc(5); + ft_bzero(edge2, 5); edge2 = realloc(edge2, 0); assert(edge2 == NULL);