add MALLOC_DEBUG_LEVEL environment variable support

This commit is contained in:
2024-12-09 20:08:28 +01:00
parent 039d29f0d7
commit 9824c9d230
12 changed files with 205 additions and 19 deletions

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/23 17:19:59 by tomoron #+# #+# */
/* Updated: 2024/12/05 17:01:08 by tomoron ### ########.fr */
/* Updated: 2024/12/09 18:48:53 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -25,8 +25,14 @@ void *get_memory(size_t size, int no_write)
chunk = mmap(0, size, PROT_WRITE | PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE,
-1, 0);
log_str("mmap called with size ",3, 1, 0);
log_ul(size, 3, 0, 1);
if (chunk == MAP_FAILED)
{
log_str("mmap call failed", 1, 1, 1);
return (0);
}
log_str("mmap call successful", 3, 1 ,1);
if (no_write)
return (chunk);
chunk->space_left = size - sizeof(t_mem_chunk);
@ -63,6 +69,7 @@ static void *pre_allocated(size_t size, t_mem_chunk **chunk, int is_small)
tmp = *chunk;
prev = *chunk;
res = 0;
log_str("malloc size corresponds to preallocated alloc size", 3, 1, 1);
while (tmp)
{
if (tmp->space_left >= size)
@ -85,6 +92,8 @@ void *malloc(size_t size)
t_alloc *res;
pthread_mutex_lock(&g_mallock);
log_str("malloc called with size ",3, 1, 0);
log_ul(size, 3, 0, 1);
if (size <= TINY_MALLOC)
res = pre_allocated(size, &(g_allocs.tiny), 0);
else if (size <= SMALL_MALLOC)
@ -94,12 +103,15 @@ void *malloc(size_t size)
res = get_memory(size + sizeof(t_alloc), 1);
if (res)
{
res->size = size;
res->next = g_allocs.large;
*res = (t_alloc){size, g_allocs.large};
g_allocs.large = res;
res++;
}
}
if(res)
log_str("malloc call successful", 3, 1, 1);
else
log_str("malloc call failed", 1, 1, 1);
pthread_mutex_unlock(&g_mallock);
return (res);
}