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/12/02 14:41:22 by tomoron #+# #+# */
/* Updated: 2024/12/05 17:32:25 by tomoron ### ########.fr */
/* Updated: 2024/12/09 20:07:24 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -44,6 +44,7 @@ static void *realloc_prealloc(t_alloc *alloc, t_mem_chunk *chunk, \
return (0);
if (alloc->size >= size)
{
log_str("new realloc size is smaller, reducing", 3, 1, 1);
chunk->space_left += alloc->size - size;
alloc->size = size;
return (alloc + 1);
@ -51,7 +52,11 @@ static void *realloc_prealloc(t_alloc *alloc, t_mem_chunk *chunk, \
if ((size > (size_t)TINY_MALLOC && is_small == 0)
|| (size > SMALL_MALLOC && is_small == 1)
|| (t_ul)(alloc->next) - (t_ul)(alloc + 1) < size)
{
log_str("new realloc size doesn't fit in the current position, reallocating", 3, 1, 1);
return (realloc_recreate(alloc, size));
}
log_str("new realloc size fits in the current position, making it bigger",3, 1, 1);
chunk->space_left -= size - alloc->size;
alloc->size = size;
return (alloc + 1);
@ -62,7 +67,10 @@ static void *realloc_large(t_alloc *alloc, size_t size)
t_alloc *prev;
if (!get_prev_alloc(&alloc, &prev, g_allocs.large, "realloc"))
{
log_str("unknown pointer given to realloc", 1, 1 ,1);
return (0);
}
return (realloc_recreate(alloc, size));
}
@ -70,6 +78,7 @@ void *realloc(void *ptr, size_t size)
{
t_alloc *alloc;
log_str("realloc function called", 3, 1, 1);
if (!ptr)
return (malloc(size));
alloc = (t_alloc *)ptr - 1;
@ -79,6 +88,10 @@ void *realloc(void *ptr, size_t size)
ptr = realloc_prealloc(alloc, g_allocs.small, 1, size);
if(!ptr)
ptr = realloc_large(alloc, size);
if(ptr)
log_str("realloc sucessful", 3, 1, 1);
else
log_str("realloc failed", 1, 1, 1);
pthread_mutex_unlock(&g_mallock);
return(ptr);
}