Made a memory allocator (crude)

This commit is contained in:
Maieul BOYER 2024-05-07 15:21:41 +02:00
parent b5c7344851
commit 941bac31b6
No known key found for this signature in database
53 changed files with 469 additions and 146 deletions

89
stdme/src/alloc/alloc.c Normal file
View file

@ -0,0 +1,89 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* alloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 10:13:06 by maiboyer #+# #+# */
/* Updated: 2024/05/07 14:54:56 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/alloc/alloc.h"
#include "me/alloc/alloc_internal.h"
#include "me/fs/putstr_fd.h"
#include "me/mem/mem_copy.h"
#include "me/mem/mem_set_zero.h"
#include "me/num/usize.h"
#include <stdalign.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
// the `+ 16` is twice the size of size_t, only to stay aligned on a 16 byte
// alignement
void *me_malloc(t_usize size)
{
t_arena_page *arena;
void *ret;
size = usize_round_up_to(size, 16);
if (size + 16 > ARENA_SIZE)
me_abort();
arena = get_head_arena();
while (arena->next != NULL && arena->current_index + 16 + size > ARENA_SIZE)
arena = arena->next;
if (arena->current_index + 16 + size > ARENA_SIZE)
{
if (alloc_arena(&arena->next))
me_abort();
arena = arena->next;
}
*(t_usize *)&arena->bytes[arena->current_index] = size;
ret = (void *)&arena->bytes[arena->current_index + 16];
arena->current_index += 16 + size;
return (ret);
}
void *me_calloc(t_usize elem_size, t_usize elem_count)
{
if (elem_size != 0 && elem_count > SIZE_MAX / elem_size)
return (NULL);
return (me_malloc(elem_size * elem_count));
}
void *me_realloc(void *ptr, t_usize new_size)
{
t_arena_page *arena;
t_usize size;
void *ret;
arena = get_head_arena();
while (arena != NULL && !((void *)&arena->bytes <= ptr &&
ptr <= (void *)(&arena->bytes) + ARENA_SIZE))
arena = arena->next;
if (arena == NULL)
return (NULL);
size = *(t_usize *)((t_u8 *)(ptr)-16);
if (size <= new_size)
return (ptr);
ret = me_malloc(new_size);
mem_copy(ret, ptr, size);
return (ret);
}
void me_free(void *ptr)
{
t_arena_page *arena;
arena = get_head_arena();
while (arena != NULL && !((void *)&arena->bytes <= ptr &&
ptr <= (void *)(&arena->bytes) + ARENA_SIZE))
arena = arena->next;
if (arena == NULL)
{
me_putstr_fd("Tried to free with me_free !\n", 2);
free(ptr);
}
}

View file

@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_arena.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 09:47:50 by maiboyer #+# #+# */
/* Updated: 2024/05/07 14:20:20 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/alloc/alloc.h"
#include "me/alloc/alloc_internal.h"
#include "me/fs/putstr_fd.h"
#include "me/mem/mem_set_zero.h"
#include <stdlib.h>
void *__libc_malloc(size_t size);
t_arena_page *get_head_arena(void)
{
static t_arena_page *val = NULL;
if (val == NULL)
{
if (alloc_arena(&val))
{
me_putstr_fd("Error: malloc failed\n", 2);
exit(1);
}
}
return (val);
}
t_error alloc_arena(t_arena_page **out)
{
if (out == NULL)
return (ERROR);
*out = __libc_malloc(sizeof(**out));
if (*out == NULL)
return (ERROR);
mem_set_zero(*out, sizeof(**out));
return (NO_ERROR);
}