From 039d29f0d74213ed82acce211906bdefcd8c1842 Mon Sep 17 00:00:00 2001 From: tomoron Date: Thu, 5 Dec 2024 18:59:07 +0100 Subject: [PATCH] fix realloc size detection when new size is larger than current chunk max size, add pthread mutex to make the functions thread safe --- main.c | 5 +++++ srcs/free.c | 13 ++++++++----- srcs/includes/malloc.h | 4 +++- srcs/malloc.c | 33 +++++++++++++++++++-------------- srcs/realloc.c | 23 ++++++++++++++--------- srcs/show_alloc_mem.c | 4 +++- 6 files changed, 52 insertions(+), 30 deletions(-) diff --git a/main.c b/main.c index 07126d4..a6fca3b 100644 --- a/main.c +++ b/main.c @@ -5,6 +5,11 @@ int main(void) { void *ptr; + printf("%zu\n", SMALL_CHUNK_SIZE); ptr = malloc(1203); + show_alloc_mem(); + ptr = realloc(ptr, 12045); + show_alloc_mem(); free(ptr); + show_alloc_mem(); } diff --git a/srcs/free.c b/srcs/free.c index e12aa29..b429a08 100644 --- a/srcs/free.c +++ b/srcs/free.c @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/30 18:46:07 by tomoron #+# #+# */ -/* Updated: 2024/12/04 18:52:39 by tomoron ### ########.fr */ +/* Updated: 2024/12/05 17:01:32 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -111,9 +111,12 @@ void free(void *ptr) if (!ptr) return ; alloc = (t_alloc *)ptr - 1; + pthread_mutex_lock(&g_mallock); if (free_prealloc(alloc, &g_allocs.tiny, 0)) - return ; - if (free_prealloc(alloc, &g_allocs.small, 1)) - return ; - free_large(alloc); + pthread_mutex_unlock(&g_mallock); + else if (free_prealloc(alloc, &g_allocs.small, 1)) + pthread_mutex_unlock(&g_mallock); + else + free_large(alloc); + pthread_mutex_unlock(&g_mallock); } diff --git a/srcs/includes/malloc.h b/srcs/includes/malloc.h index aff00f2..08d0862 100644 --- a/srcs/includes/malloc.h +++ b/srcs/includes/malloc.h @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/26 16:38:01 by tomoron #+# #+# */ -/* Updated: 2024/12/03 15:21:41 by tomoron ### ########.fr */ +/* Updated: 2024/12/05 16:39:23 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ # include # include # include +# include # include "libft.h" # define PAGE_SIZE sysconf(_SC_PAGESIZE) @@ -50,6 +51,7 @@ typedef struct s_allocations typedef unsigned long t_ul; extern t_allocations g_allocs; +extern pthread_mutex_t g_mallock; size_t align(size_t nb, size_t align_nb); diff --git a/srcs/malloc.c b/srcs/malloc.c index 35779bb..0f255da 100644 --- a/srcs/malloc.c +++ b/srcs/malloc.c @@ -6,16 +6,14 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/23 17:19:59 by tomoron #+# #+# */ -/* Updated: 2024/12/03 19:01:33 by tomoron ### ########.fr */ +/* Updated: 2024/12/05 17:01:08 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "includes/malloc.h" -#include -#include -#include t_allocations g_allocs; +pthread_mutex_t g_mallock = PTHREAD_MUTEX_INITIALIZER; t_alloc *get_suitable_addr_in_chunk(t_mem_chunk *chunk, size_t size); t_mem_chunk *create_new_chunk(int is_small, t_mem_chunk **chunk, \ @@ -84,17 +82,24 @@ static void *pre_allocated(size_t size, t_mem_chunk **chunk, int is_small) void *malloc(size_t size) { - t_alloc *new; + t_alloc *res; + pthread_mutex_lock(&g_mallock); if (size <= TINY_MALLOC) - return (pre_allocated(size, &(g_allocs.tiny), 0)); + res = pre_allocated(size, &(g_allocs.tiny), 0); else if (size <= SMALL_MALLOC) - return (pre_allocated(size, &(g_allocs.small), 1)); - new = get_memory(size + sizeof(t_alloc), 1); - if (!new) - return (0); - new->size = size; - new->next = g_allocs.large; - g_allocs.large = new; - return ((char *)new + sizeof(t_alloc)); + res = pre_allocated(size, &(g_allocs.small), 1); + else + { + res = get_memory(size + sizeof(t_alloc), 1); + if (res) + { + res->size = size; + res->next = g_allocs.large; + g_allocs.large = res; + res++; + } + } + pthread_mutex_unlock(&g_mallock); + return (res); } diff --git a/srcs/realloc.c b/srcs/realloc.c index a3d65de..e83f02b 100644 --- a/srcs/realloc.c +++ b/srcs/realloc.c @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/02 14:41:22 by tomoron #+# #+# */ -/* Updated: 2024/12/04 18:50:45 by tomoron ### ########.fr */ +/* Updated: 2024/12/05 17:32:25 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,11 +20,15 @@ static void *realloc_recreate(t_alloc *alloc, size_t size) { void *new; + pthread_mutex_unlock(&g_mallock); new = malloc(size); + pthread_mutex_lock(&g_mallock); if (alloc->size < size) size = alloc->size; + pthread_mutex_unlock(&g_mallock); ft_memcpy(new, alloc + 1, size); free(alloc + 1); + pthread_mutex_lock(&g_mallock); return (new); } @@ -44,8 +48,8 @@ static void *realloc_prealloc(t_alloc *alloc, t_mem_chunk *chunk, \ alloc->size = size; return (alloc + 1); } - if ((size > (size_t)TINY_CHUNK_SIZE && is_small == 0) - || (size > SMALL_CHUNK_SIZE && is_small == 1) + if ((size > (size_t)TINY_MALLOC && is_small == 0) + || (size > SMALL_MALLOC && is_small == 1) || (t_ul)(alloc->next) - (t_ul)(alloc + 1) < size) return (realloc_recreate(alloc, size)); chunk->space_left -= size - alloc->size; @@ -69,11 +73,12 @@ void *realloc(void *ptr, size_t size) if (!ptr) return (malloc(size)); alloc = (t_alloc *)ptr - 1; + pthread_mutex_lock(&g_mallock); ptr = realloc_prealloc(alloc, g_allocs.tiny, 0, size); - if (ptr) - return (ptr); - ptr = realloc_prealloc(alloc, g_allocs.small, 1, size); - if (ptr) - return (ptr); - return (realloc_large(alloc, size)); + if(!ptr) + ptr = realloc_prealloc(alloc, g_allocs.small, 1, size); + if(!ptr) + ptr = realloc_large(alloc, size); + pthread_mutex_unlock(&g_mallock); + return(ptr); } diff --git a/srcs/show_alloc_mem.c b/srcs/show_alloc_mem.c index f88c226..5d24d33 100644 --- a/srcs/show_alloc_mem.c +++ b/srcs/show_alloc_mem.c @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/30 12:19:34 by tomoron #+# #+# */ -/* Updated: 2024/12/03 18:50:25 by tomoron ### ########.fr */ +/* Updated: 2024/12/05 17:05:48 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -94,9 +94,11 @@ void show_alloc_mem(void) size_t total; total = 0; + pthread_mutex_lock(&g_mallock); total += show_pre_allocated("TINY", g_allocs.tiny); total += show_pre_allocated("SMALL", g_allocs.small); total += show_large(); + pthread_mutex_unlock(&g_mallock); write(1, "Total : ", 8); put_ulnbr_base(total, "0123456789"); write(1, " bytes\n", 7);