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 @@
/* ::: :::::::: */
/* 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));
}