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

View file

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* alloc.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 09:42:02 by maiboyer #+# #+# */
/* Updated: 2024/05/14 17:44:38 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ALLOC_H
#define ALLOC_H
#include "me/types.h"
void *me_malloc(t_usize size);
void *me_calloc(t_usize elem_count, t_usize elem_size);
void *me_realloc(void *ptr, t_usize size);
void *me_realloc_array(void *ptr, t_usize elem_size, t_usize elem_count);
void uninit_allocator(void);
#endif /* ALLOC_H */

View file

@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* alloc_internal.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 09:48:17 by maiboyer #+# #+# */
/* Updated: 2024/05/14 16:12:54 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ALLOC_INTERNAL_H
#define ALLOC_INTERNAL_H
#include "aq/alloc.h"
#include "me/types.h"
#include <stdalign.h>
#define PAGE_SIZE_DEFAULT 4096
#define BLOCK_PADDING "\xFE\xDC\xAB\xC0\xFE\xEE\x66"
#define POOL_ADDR (void *)0xDeadBeef
typedef struct s_mblock
{
struct s_mblock *next;
struct s_mpage *page;
t_usize size;
bool used;
t_u8 padding[7];
} t_mblock;
typedef struct s_mpage
{
t_usize page_size;
t_mblock *first;
struct s_mpage *next;
} t_mpage;
// Will never be null, as it will allocate a new arena if it needs to do so
t_mpage *get_head_arena(void);
// Will return ERROR if it couldn't malloc the page
t_error alloc_arena_page(t_usize min_size, t_mpage **out);
t_mblock *get_block_for_size(t_usize size);
void print_pages_info(void);
bool merge_block(t_mblock *self, t_usize min_size);
#endif /* ALLOC_INTERNAL_H */

View file

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* allocator.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/14 17:52:31 by maiboyer #+# #+# */
/* Updated: 2024/05/14 17:55:36 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ALLOCATOR_H
#define ALLOCATOR_H
#include "me/types.h"
typedef struct s_allocator t_allocator;
typedef void *(*t_allocator_alloc)(t_allocator *self, t_usize size);
typedef void *(*t_allocator_alloc_array)(t_allocator *self, t_usize size,
t_usize count);
typedef void *(*t_allocator_realloc)(t_allocator *self, void *ptr,
t_usize requested_size);
typedef void *(*t_allocator_realloc_array)(t_allocator *self, void *ptr,
t_usize requested_size,
t_usize requested_count);
typedef void (*t_allocator_free)(t_allocator *self, void *ptr);
typedef void (*t_allocator_uninit)(t_allocator *self);
struct s_allocator
{
t_allocator_alloc alloc;
t_allocator_alloc_array alloc_array;
t_allocator_realloc realloc;
t_allocator_realloc_array realloc_array;
t_allocator_free free;
t_allocator_uninit uninit;
void *alloc_data;
};
#endif /* ALLOCATOR_H */

View file

@ -0,0 +1,66 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* internal_vg_funcs.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/12 22:20:30 by maiboyer #+# #+# */
/* Updated: 2024/05/14 16:17:25 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef INTERNAL_VG_FUNCS_H
#define INTERNAL_VG_FUNCS_H
#include "aq/alloc_internal.h"
#include "me/types.h"
#if !defined(NVALGRIND) || defined(VGHEADER)
# ifdef NVALGRIND
# undef NVALGRIND
# endif
# define VGFUNCS
# include <valgrind/memcheck.h>
# include <valgrind/valgrind.h>
#endif
#define ZEROED_POOL 1
#define ZEROED_ALLOC 1
static inline t_usize redzone_size(void)
{
return (sizeof(t_mblock));
}
#ifdef VGFUNCS
static inline bool vg_running(void)
{
return (RUNNING_ON_VALGRIND != 0);
}
#else
static inline bool vg_running(void)
{
return (false);
}
#endif
#define MEMPOOL_FLAG_MALLOCLIKE 1
#define MEMPOOL_FLAG_AUTOFREE 2
void vg_block_malloc(void *ptr, t_usize size);
void vg_block_resize(void *ptr, t_usize oldsize, t_usize newsize);
void vg_block_free(void *ptr);
void vg_mem_no_access(void *ptr, t_usize size);
void vg_mem_undefined(void *ptr, t_usize size);
void vg_mem_defined(void *ptr, t_usize size);
void vg_mempool_create(void *pool);
void vg_mempool_create_ext(void *pool, t_usize flags);
void vg_mempool_destroy(void *pool);
void vg_mempool_alloc(void *pool, void *addr, t_usize size);
void vg_mempool_free(void *pool, void *addr);
void vg_mempool_resize(void *pool, void *psrc, t_usize size);
#endif /* INTERNAL_VG_FUNCS_H */

View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libc_wrapper.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/14 17:52:11 by maiboyer #+# #+# */
/* Updated: 2024/05/14 18:48:06 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBC_WRAPPER_H
#define LIBC_WRAPPER_H
#include "aq/allocator.h"
#include "me/types.h"
void *lc_malloc(t_allocator *self, t_usize size);
void *lc_calloc(t_allocator *self, t_usize size, t_usize count);
void *lc_realloc(t_allocator *self, void *ptr, t_usize min_size);
void *lc_realloc_array(t_allocator *self, void *ptr, t_usize size,
t_usize count);
void lc_free(t_allocator *self, void *ptr);
void lc_uninit(t_allocator *self);
t_allocator lc_init(void);
#endif /* LIBC_WRAPPER_H */

View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* melloc.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/14 17:54:03 by maiboyer #+# #+# */
/* Updated: 2024/05/14 18:08:16 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MELLOC_H
#define MELLOC_H
#include "aq/allocator.h"
void *m_malloc(t_allocator *self, t_usize size);
void *m_alloc_array(t_allocator *self, t_usize size, t_usize count);
void *m_realloc(t_allocator *self, void *ptr, t_usize min_size);
void *m_realloc_array(t_allocator *self, void *ptr, t_usize size,
t_usize count);
void m_free(t_allocator *self, void *ptr);
void m_uninit(t_allocator *self);
#endif /* MELLOC_H */