ft_malloc/srcs/ft_calloc.c
2025-11-28 19:50:58 +01:00

29 lines
1.4 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thrieg < thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/25 11:47:16 by thrieg #+# #+# */
/* Updated: 2025/11/28 16:41:16 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/ft_malloc.h"
void *calloc(size_t nmemb, size_t size)
{
size_t real_size = nmemb * size;
void *malloc_ret;
if (real_size % ALLIGN_BYTES)
real_size = ((real_size / ALLIGN_BYTES) + 1) * ALLIGN_BYTES;
malloc_ret = malloc(real_size);
if (!malloc_ret)
return (NULL);
// pthread_mutex_lock(&g_mut);
ft_memset(malloc_ret, 0, real_size); // no need to lock because pointer is internal until we return it, user can't call any of our function on it
// pthread_mutex_unlock(&g_mut);
return (malloc_ret);
}