added support for MALLOC_PERTURB_ environment variable

This commit is contained in:
Thomas Rieg 2025-12-08 15:48:24 +01:00
parent 0fd85cf568
commit 6d84b2f72a
17 changed files with 453 additions and 404 deletions

View file

@ -6,7 +6,7 @@
/* By: thrieg < thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/25 11:47:23 by thrieg #+# #+# */
/* Updated: 2025/11/28 16:37:22 by thrieg ### ########.fr */
/* Updated: 2025/12/08 15:37:18 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */
@ -39,6 +39,11 @@ void *realloc(void *ptr, size_t size)
size = ((size / ALLIGN_BYTES) + 1) * ALLIGN_BYTES;
void *ret = NULL;
pthread_mutex_lock(&g_mut);
if (!g_state.is_init)
{
init_env_variables(); // realloc shouldn't ever be called first with a non-NULL ptr
return (NULL); // should I just let it segfault here like the real realloc?
}
t_header *header = ((t_header *)ptr) - 1;
if (header->zone->type == E_LARGE)
{
@ -80,6 +85,7 @@ void *realloc(void *ptr, size_t size)
if (next_header && !next_header->occupied && (header->size + next_header->size + sizeof(t_header)) >= size)
{
const size_t available_space = (header->size + next_header->size + sizeof(t_header));
const size_t original_space = header->size;
if (available_space - size > sizeof(t_header))
{
// Split the block: create a new header in the remaining space
@ -96,7 +102,10 @@ void *realloc(void *ptr, size_t size)
{
header->size = available_space;
}
ret = ptr;
pthread_mutex_unlock(&g_mut);
if (g_state.patern)
ft_memset((char *)ptr + original_space, g_state.patern, header->size - original_space); // should I use original_size to only perturb the actually requested bytes instead of the bytes I allocate?
return (ptr);
}
else
{