added the mandatory show_alloc_mem, and sorted the zones by increasing address in the linked lists

This commit is contained in:
thrieg 2025-12-13 06:07:24 +01:00
parent 65447f4a0a
commit db2b5f27bb
92 changed files with 176 additions and 20 deletions

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* bonus_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thrieg < thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/28 16:42:13 by thrieg #+# #+# */
/* Updated: 2025/11/28 18:25:49 by thrieg ### ########.fr */
/* Updated: 2025/12/13 04:42:15 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* init_state.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thrieg < thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/18 16:44:13 by thrieg #+# #+# */
/* Updated: 2025/12/08 15:32:55 by thrieg ### ########.fr */
/* Updated: 2025/12/13 05:50:48 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */
@ -28,6 +28,43 @@ void init_env_variables()
g_state.is_init = true;
}
//inserts a page in the linked list whilst keeping the zones sorted in ascending address
static void zone_insert_sorted(t_zone **head, t_zone *z)
{
t_zone *cur;
z->prev = NULL;
z->next = NULL;
if (*head == NULL)
{
*head = z;
return;
}
cur = *head;
// insert before head
if ((uintptr_t)z < (uintptr_t)cur)
{
z->next = cur;
cur->prev = z;
*head = z;
return;
}
// walk until the next node is after z (or end)
while (cur->next && (uintptr_t)cur->next < (uintptr_t)z)
cur = cur->next;
// insert after cur
z->next = cur->next;
z->prev = cur;
if (cur->next)
cur->next->prev = z;
cur->next = z;
}
// only call this for TINY or SMALL
void *add_page(t_type type)
{
@ -51,10 +88,7 @@ void *add_page(t_type type)
header->occupied = false;
header->size = size - sizeof(t_zone) - sizeof(t_header);
header->zone = ptr;
ptr->next = g_state.tiny_zone; // tiny_zone can be NULL but that's fine
if (g_state.tiny_zone)
g_state.tiny_zone->prev = ptr;
g_state.tiny_zone = ptr;
zone_insert_sorted(&g_state.tiny_zone, ptr);
}
else if (type == E_SMALL)
{
@ -64,10 +98,7 @@ void *add_page(t_type type)
header->occupied = false;
header->size = size - sizeof(t_zone) - sizeof(t_header);
header->zone = ptr;
ptr->next = g_state.small_zone; // small_zone can be NULL but that's fine
if (g_state.small_zone)
g_state.small_zone->prev = ptr;
g_state.small_zone = ptr;
zone_insert_sorted(&g_state.small_zone, ptr);
}
return (ptr);
}
@ -83,14 +114,11 @@ void *add_large(size_t size)
ptr->size = size;
ptr->type = E_LARGE;
ptr->prev = NULL;
ptr->next = g_state.large_zone; // large_zone can be NULL but that's fine
if (g_state.large_zone)
g_state.large_zone->prev = ptr;
g_state.large_zone = ptr;
t_header *header = (t_header *)(ptr + 1);
header->occupied = true;
header->size = size - sizeof(t_zone) - sizeof(t_header);
header->zone = ptr;
zone_insert_sorted(&g_state.large_zone, ptr);
return ((void *)(header + 1));
}

55
srcs/utils.c Normal file
View file

@ -0,0 +1,55 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/13 04:41:06 by thrieg #+# #+# */
/* Updated: 2025/12/13 06:00:35 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/ft_malloc.h"
#include "../libft/ft_printf/ft_printf.h"
void print_allocs_zone(t_zone *zone)
{
ft_printf("%s : %p\n", zone->type == E_TINY ? "TINY" : (zone->type == E_SMALL ? "SMALL" : "LARGE"), zone);
char *zone_end = (char *)zone + zone->size;
t_header *header = (t_header *)(zone + 1);
while ((char *)header + sizeof(t_header) <= zone_end)
{
if (header->occupied)
ft_printf("%p - %p : %u bytes\n", (char *)header + sizeof(header), (char *)header + sizeof(header) + header->size, (unsigned int)header->size);
header = (t_header *)((char *)(header + 1) + header->size);
}
}
static t_zone *min_zone_ptr(t_zone *a, t_zone *b, t_zone *c)
{
t_zone *m = NULL;
if (a && (!m || (uintptr_t)a < (uintptr_t)m)) m = a;
if (b && (!m || (uintptr_t)b < (uintptr_t)m)) m = b;
if (c && (!m || (uintptr_t)c < (uintptr_t)m)) m = c;
return m;
}
void show_alloc_mem(void)
{
t_zone *t = g_state.tiny_zone;
t_zone *s = g_state.small_zone;
t_zone *l = g_state.large_zone;
while (t || s || l)
{
t_zone *m = min_zone_ptr(t, s, l);
print_allocs_zone(m);
if (m == t) t = t->next;
else if (m == s) s = s->next;
else l = l->next;
}
}