/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_malloc.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: thrieg +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/11/17 15:02:55 by thrieg #+# #+# */ /* Updated: 2025/12/13 06:31:39 by thrieg ### ########.fr */ /* */ /* ************************************************************************** */ #include "../includes/ft_malloc.h" void *malloc(size_t size) { pthread_mutex_lock(&g_mut); if (!g_state.is_init) init_env_variables(); if (!size || size % ALLIGN_BYTES) size = ((size / ALLIGN_BYTES) + 1) * ALLIGN_BYTES; void *ret = NULL; if (size > SMALL_SIZE_MAX) { ret = add_large(size); } else if (size > TINY_SIZE_MAX) { ret = add_small(size); } else { ret = add_tiny(size); } pthread_mutex_unlock(&g_mut); if (ret && g_state.patern) { size = (((t_header *)ret) - 1)->size; ft_memset(ret, g_state.patern, size); // doesn't need to lock because we only modify the user pointer and the user doesn't have accces to it yet } return (ret); }