still working on memory stuff

This commit is contained in:
Maieul BOYER 2024-05-08 19:35:13 +02:00
parent 8e90a858ad
commit 326c9318e4
No known key found for this signature in database
18 changed files with 206 additions and 77 deletions

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 10:13:06 by maiboyer #+# #+# */
/* Updated: 2024/05/08 16:12:19 by maiboyer ### ########.fr */
/* Updated: 2024/05/08 19:20:59 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,12 +19,15 @@
#include <stdalign.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
t_arena_page *find_page_for(t_usize data_size, t_arena_page *page)
{
t_arena_block *block;
t_arena_page *last_page;
last_page = NULL;
while (page != NULL)
{
if (page->page_size <= data_size + sizeof(t_arena_block))
@ -34,6 +37,7 @@ t_arena_page *find_page_for(t_usize data_size, t_arena_page *page)
ARENA_SIZE_DEFAULT),
&page->next))
return (me_abort("Failed Malloc"), NULL);
last_page = page;
page = page->next;
}
block = (t_arena_block *)(&page[1]);
@ -41,12 +45,21 @@ t_arena_page *find_page_for(t_usize data_size, t_arena_page *page)
{
if ((t_u8 *)block >= (t_u8 *)page + sizeof(*page) + page->page_size)
break;
if (block->free && block->size >= data_size)
if (!block->used && block->size >= data_size)
return (page);
block = (void *)((t_u8 *)block + block->size + sizeof(*block));
}
last_page = page;
page = page->next;
}
if (last_page != NULL)
{
if (alloc_arena_page(usize_round_up_to(data_size + sizeof(*block),
ARENA_SIZE_DEFAULT),
&last_page->next))
return (me_abort("Failed Malloc"), NULL);
return (last_page->next);
}
return ((me_abort("Found no pages for memory"), NULL));
}
@ -61,7 +74,7 @@ void *me_malloc(t_usize size)
arena = find_page_for(size, get_head_arena());
if (get_block_for_page(size, arena, &block))
return (me_abort("Found no page for me_malloc"), NULL);
block->free = false;
block->used = true;
return ((t_u8 *)block + sizeof(*block));
}
@ -74,27 +87,38 @@ void *me_calloc(t_usize elem_size, t_usize elem_count)
void *me_realloc(void *ptr, t_usize new_size)
{
t_arena_page *arena;
t_arena_block *block;
void *ret;
arena = get_head_arena();
if (arena == NULL)
return (NULL);
if (ptr == NULL)
return (me_malloc(new_size));
block = (void *)((t_u8 *)(ptr) - sizeof(t_arena_block));
while (arena && !((t_u8 *)arena + arena->page_size + sizeof(*arena) > (t_u8 *)block && (t_u8 *)block > (t_u8 *)arena))
arena = arena->next;
if (arena == NULL)
return (NULL);
if (block->size <= new_size)
return (ptr);
ret = me_malloc(new_size);
mem_copy(ret, ptr, block->size);
return (ret);
}
void me_free(void *ptr)
{
t_arena_page *page;
t_arena_block *cur;
t_arena_block *next;
(void)(ptr);
// t_arena_page *arena;
page = get_page_from_ptr(ptr);
if (page == NULL)
me_abort("Tried to me_free with me_free something that isn't allocated "
"with me_alloc");
cur = (void *)(((t_usize)ptr) - sizeof(*cur));
cur->used = false;
next = (void *)(((t_usize)cur) + sizeof(*cur) + cur->size);
if (((t_usize)page) <= ((t_usize)next) &&
((t_usize)next) <= ((t_usize)page) + page->page_size + sizeof(*page))
{
if (!next->used)
cur->size += next->size + sizeof(*cur);
}
}

View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* alloc_get_page_from_bloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/08 19:11:20 by maiboyer #+# #+# */
/* Updated: 2024/05/08 19:14:57 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/alloc/alloc.h"
#include "me/alloc/alloc_internal.h"
t_arena_page *get_page_from_ptr(void *ptr)
{
t_arena_page *page;
page = get_head_arena();
while (page)
{
if (((t_usize)page) <= ((t_usize)ptr) &&
((t_usize)ptr) <= ((t_usize)page) + page->page_size + sizeof(*page))
return (page);
page = page->next;
}
return (NULL);
}

View file

@ -6,15 +6,18 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 09:47:50 by maiboyer #+# #+# */
/* Updated: 2024/05/07 22:04:20 by maiboyer ### ########.fr */
/* Updated: 2024/05/08 19:24:47 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/alloc/alloc.h"
#include "me/alloc/alloc_internal.h"
#include "me/fs/putendl_fd.h"
#include "me/fs/putnbr_fd.h"
#include "me/fs/putstr_fd.h"
#include "me/mem/mem_set_zero.h"
#include "me/num/usize.h"
#include <stdio.h>
#include <stdlib.h>
void *__libc_malloc(size_t size);
@ -37,6 +40,8 @@ t_arena_page *get_head_arena(void)
t_error alloc_arena_page(t_usize min_size, t_arena_page **out)
{
t_arena_block *block;
printf("Allocating page with size %zu", min_size);
min_size = usize_round_up_to(min_size, ARENA_SIZE_DEFAULT);
if (out == NULL)
return (ERROR);
@ -48,7 +53,7 @@ t_error alloc_arena_page(t_usize min_size, t_arena_page **out)
(*out)->next = NULL;
block = (t_arena_block *)((t_u8 *)*out + sizeof(**out));
block->end = true;
block->free = true;
block->used = false;
block->size = min_size - sizeof(*block);
return (NO_ERROR);
}
@ -65,17 +70,19 @@ t_error get_block_for_page(t_usize size, t_arena_page *page,
if (page->page_size - sizeof(t_arena_block) <= size)
return (ERROR);
cur = (void *)((t_u8 *)page + sizeof(*page));
while ((t_u8 *)cur < (t_u8 *)page + page->page_size + sizeof(*page))
while ((t_usize)((t_u8 *)cur - (t_u8 *)page) <
page->page_size - sizeof(*page))
{
if (cur->free && cur->size >= size)
if (!cur->used && cur->size >= size)
{
if (cur->size > size + sizeof(*cur))
if (cur->size > size + sizeof(*cur) * 2)
{
leftover = cur->size - size - sizeof(*cur);
cur->size = size;
next = (void *)((t_u8 *)cur + cur->size + sizeof(*cur));
next = (void *)(((t_usize)cur) + size);
next->size = leftover;
next->end = cur->end;
next->used = false;
cur->end = false;
}
*out = cur;
@ -85,3 +92,21 @@ t_error get_block_for_page(t_usize size, t_arena_page *page,
}
return (ERROR);
}
void print_pages_info(void)
{
t_arena_page *page;
t_i32 page_nb;
page = get_head_arena();
page_nb = 0;
while (page != NULL)
{
me_putstr_fd("Page ", 2);
me_putnbr_fd(page_nb++, 2);
me_putstr_fd(" with size ", 2);
me_putnbr_fd((t_i32)page->page_size, 2);
me_putstr_fd("\n", 2);
page = page->next;
}
}