fix realloc size detection when new size is larger than current chunk max size, add pthread mutex to make the functions thread safe

This commit is contained in:
2024-12-05 18:59:07 +01:00
parent a9ad4492cc
commit 039d29f0d7
6 changed files with 52 additions and 30 deletions

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}