From 7de95ddd662b52c803d307b6028fd90a1aa71892 Mon Sep 17 00:00:00 2001 From: Thomas Vanbesien Date: Fri, 27 Feb 2026 10:31:39 +0100 Subject: Add zone allocation, internal data structures, and README Implement mmap-based zone allocation for TINY (<=128B) and SMALL (<=1024B) categories. malloc now creates zones on demand and returns a pointer to the first chunk. --- src/malloc.c | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) (limited to 'src/malloc.c') diff --git a/src/malloc.c b/src/malloc.c index 8982de0..268e0d1 100644 --- a/src/malloc.c +++ b/src/malloc.c @@ -1,15 +1,48 @@ /** * @file malloc.c - * @brief Stub implementation of malloc. + * @brief malloc implementation. */ #include "malloc.h" -#include "libft.h" +#include "malloc_internal.h" + +t_heap g_heap = { 0 }; void * malloc (size_t size) { - (void)size; - ft_putendl_fd ("MALLOC", 1); - return (NULL); + t_zone **zone_list; + t_zone *zone; + t_chunk *chunk; + size_t alloc_max; + + if (size == 0) + size = 1; + if (size <= TINY_MAX) + { + zone_list = &g_heap.tiny; + alloc_max = TINY_MAX; + } + else if (size <= SMALL_MAX) + { + zone_list = &g_heap.small; + alloc_max = SMALL_MAX; + } + else + { + zone_list = &g_heap.large; + alloc_max = size; + } + if (*zone_list == NULL) + { + zone = zone_new (alloc_max); + if (zone == NULL) + return (NULL); + *zone_list = zone; + } + else + zone = *zone_list; + chunk = (t_chunk *)((char *)zone + ALIGN (sizeof (t_zone))); + chunk->is_free = 0; + return ((char *)chunk + ALIGN (sizeof (t_chunk))); } -- cgit v1.2.3