Put the custom allocator in its own lib, as to lessen the difficulty to switch between libc's allocator and a custom one (#7)

This commit is contained in:
Maix0 2024-05-14 18:56:53 +02:00 committed by GitHub
parent 713f0f0302
commit cb7f3c3fdf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
85 changed files with 1121 additions and 877 deletions

34
stdme/src/mem/allocator.c Normal file
View file

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* allocator.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/14 18:26:27 by maiboyer #+# #+# */
/* Updated: 2024/05/14 18:28:24 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "aq/allocator.h"
#include "aq/libc_wrapper.h"
t_allocator *global_allocator(void)
{
static t_allocator global_alloc = {};
static bool init = false;
if (!init)
{
init = true;
global_alloc = lc_init();
}
return (&global_alloc);
}
void uninit_global_allocator(void)
{
t_allocator *allocator;
allocator = global_allocator();
allocator->uninit(allocator);
}

View file

@ -6,14 +6,25 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/06 14:47:49 by maiboyer #+# #+# */
/* Updated: 2024/05/07 12:45:31 by maiboyer ### ########.fr */
/* Updated: 2024/05/14 18:32:31 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem_alloc.h"
#include "me/alloc/alloc.h"
#include "me/mem/_allocator.h"
void *mem_alloc(t_usize size)
void *mem_alloc(t_usize size)
{
return (me_malloc(size));
t_allocator *a;
a = global_allocator();
return (a->alloc(a, size));
}
void mem_free(void *ptr)
{
t_allocator *a;
a = global_allocator();
return (a->free(a, ptr));
}

View file

@ -6,15 +6,17 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 15:53:21 by maiboyer #+# #+# */
/* Updated: 2024/05/07 12:46:04 by maiboyer ### ########.fr */
/* Updated: 2024/05/14 18:30:27 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/alloc/alloc.h"
#include "me/mem/mem_alloc_array.h"
#include <stdlib.h>
#include "me/mem/_allocator.h"
void *mem_alloc_array(t_usize item_count, t_usize item_size)
void *mem_alloc_array(t_usize size, t_usize count)
{
return (me_calloc(item_count, item_size));
t_allocator *a;
a = global_allocator();
return (a->alloc_array(a, size, count));
}

View file

@ -6,14 +6,24 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 12:46:18 by maiboyer #+# #+# */
/* Updated: 2024/05/07 12:47:12 by maiboyer ### ########.fr */
/* Updated: 2024/05/14 18:32:06 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem_realloc.h"
#include "me/alloc/alloc.h"
#include "me/mem/_allocator.h"
void *mem_realloc(void *ptr, t_usize size)
{
return (me_realloc(ptr, size));
t_allocator *a;
a = global_allocator();
return (a->realloc(a, ptr, size));
}
void *mem_realloc_array(void *ptr, t_usize size, t_usize count)
{
t_allocator *a;
a = global_allocator();
return (a->realloc_array(a, ptr, size, count));
}