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

@ -1,34 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* alloc_dumb_internal.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/12 15:03:59 by maiboyer #+# #+# */
/* Updated: 2024/05/12 16:42:39 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ALLOC_DUMB_INTERNAL_H
#define ALLOC_DUMB_INTERNAL_H
#include "me/types.h"
#define PTR_LENS 255
typedef struct s_ptr
{
void *ptr;
t_usize size;
} t_ptr;
typedef struct s_ptr_table
{
t_ptr table[PTR_LENS];
struct s_ptr_table *next;
} t_ptr_table;
t_ptr_table *get_table(void);
#endif /* ALLOC_DUMB_INTERNAL_H */

View file

@ -1,52 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* alloc_internal.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 09:48:17 by maiboyer #+# #+# */
/* Updated: 2024/05/12 14:02:32 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ALLOC_INTERNAL_H
#define ALLOC_INTERNAL_H
#include "me/alloc/alloc.h"
#include "me/types.h"
#include <stdalign.h>
#define PAGE_SIZE_DEFAULT 4096
#define BLOCK_PADDING "\xFE\xDC\xAB\xC0\xFE\xEE\x66"
typedef struct s_mblock
{
struct s_mblock *next;
struct s_mpage *page;
t_usize size;
t_usize offset;
bool used;
t_u8 padding[7];
} t_mblock;
typedef struct s_mpage
{
t_usize page_size;
void *data;
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);
t_mpage *get_page_from_ptr(void *ptr);
t_mblock *get_block_from_ptr(void *ptr);
t_error merge_next_block(t_mblock *self, t_usize min_size);
#endif /* ALLOC_INTERNAL_H */

View file

@ -56,7 +56,7 @@ typedef struct s_blx
void *win;
t_blx_input inputs;
t_run_function func;
t_free_function me_free;
t_free_function mem_free;
t_blx_app app;
t_blx_data _data;
} t_blx;

View file

@ -29,9 +29,9 @@ t_error str_reserve(t_buffer_str *buf, t_usize size);
static inline void str_free(t_buffer_str buf)
{
void me_free(void *);
void mem_free(void *);
me_free(buf.buf);
mem_free(buf.buf);
}
static inline char str_pop(t_buffer_str *buf)

View file

@ -64,7 +64,7 @@ See the function declaration below for the signature and more information.
If you don't want/need the qoi_read and qoi_write functions, you can define
QOI_NO_STDIO before including this library.
This library uses malloc() and me_free(). To supply your own malloc implementation
This library uses malloc() and mem_free(). To supply your own malloc implementation
you can define QOI_MALLOC and QOI_FREE before including this library.
This library uses memset() to zero-initialize the index. To supply your own
@ -273,7 +273,7 @@ The function either returns NULL on failure (invalid data, or malloc or fopen
failed) or a pot_i32er to the decoded pixels. On success, the t_qoi_desc struct
will be filled with the description from the file header.
The returned pixel data should be me_free()d after use. */
The returned pixel data should be mem_free()d after use. */
void *qoi_read(t_const_str filename, t_qoi_desc *desc,
t_i32 channels);
@ -286,7 +286,7 @@ The function either returns NULL on failure (invalid parameters or malloc
failed) or a pot_i32er to the encoded data on success. On success the out_len
is set to the size in bytes of the encoded data.
The returned qoi data should be me_free()d after use. */
The returned qoi data should be mem_free()d after use. */
void *qoi_encode(const void *data, const t_qoi_desc *desc,
t_i32 *out_len);
@ -297,7 +297,7 @@ The function either returns NULL on failure (invalid parameters or malloc
failed) or a pot_i32er to the decoded pixels. On success, the t_qoi_desc struct
is filled with the description from the file header.
The returned pixel data should be me_free()d after use. */
The returned pixel data should be mem_free()d after use. */
void *qoi_decode(const void *data, t_i32 size, t_qoi_desc *desc,
t_i32 channels);

View file

@ -1,20 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_add_back.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 20:38:45 by maiboyer #+# #+# */
/* Updated: 2023/12/09 16:27:56 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIST_ADD_BACK_H
# define LIST_ADD_BACK_H
# include "me/types.h"
void list_add_back(t_list **list, t_list *new);
#endif

View file

@ -1,19 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_add_front.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 20:15:23 by maiboyer #+# #+# */
/* Updated: 2023/12/09 16:28:15 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIST_ADD_FRONT_H
# define LIST_ADD_FRONT_H
# include "me/types.h"
void list_add_front(t_list **lst, t_list *new);
#endif

View file

@ -1,20 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_alloc_node.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 19:57:28 by maiboyer #+# #+# */
/* Updated: 2023/12/09 16:28:30 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIST_ALLOC_NODE_H
# define LIST_ALLOC_NODE_H
# include "me/types.h"
t_list *list_alloc_node(void *content);
#endif

View file

@ -1,20 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_free_one.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 21:30:20 by maiboyer #+# #+# */
/* Updated: 2023/12/09 16:29:15 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIST_FREE_ONE_H
# define LIST_FREE_ONE_H
# include "me/types.h"
void list_free_one(t_list *lst, void (*del)(void *));
#endif

View file

@ -1,20 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_get_last.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 20:37:08 by maiboyer #+# #+# */
/* Updated: 2023/12/09 16:29:37 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIST_GET_LAST_H
# define LIST_GET_LAST_H
# include "me/types.h"
t_list *list_get_last(t_list *list);
#endif

View file

@ -1,20 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_iter.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 21:39:05 by maiboyer #+# #+# */
/* Updated: 2023/12/09 16:29:51 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIST_ITER_H
# define LIST_ITER_H
# include "me/types.h"
void list_iter(t_list *list, void (*f)(void *));
#endif

View file

@ -1,20 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_map.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 21:40:24 by maiboyer #+# #+# */
/* Updated: 2023/12/09 16:30:08 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIST_MAP_H
# define LIST_MAP_H
# include "me/types.h"
t_list *list_map(t_list *lst, void *(*f)(void *), void (*del)(void *));
#endif

View file

@ -1,20 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_size.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 20:23:19 by maiboyer #+# #+# */
/* Updated: 2023/12/09 16:30:23 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIST_SIZE_H
# define LIST_SIZE_H
# include "me/types.h"
t_usize list_size(t_list *lst);
#endif

View file

@ -1,20 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_free_all.h :+: :+: :+: */
/* _allocator.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 21:35:20 by maiboyer #+# #+# */
/* Updated: 2023/12/09 16:28:47 by maiboyer ### ########.fr */
/* Created: 2024/05/14 18:27:07 by maiboyer #+# #+# */
/* Updated: 2024/05/14 18:27:40 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIST_FREE_ALL_H
# define LIST_FREE_ALL_H
#ifndef _ALLOCATOR_H
#define _ALLOCATOR_H
# include "me/types.h"
#include "aq/allocator.h"
void list_free_all(t_list **lst, void (*del)(void *));
t_allocator *global_allocator(void);
void uninit_global_allocator(void);
#endif
#endif /* _ALLOCATOR_H */

View file

@ -1,23 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* alloc.h :+: :+: :+: */
/* mem.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 09:42:02 by maiboyer #+# #+# */
/* Updated: 2024/05/12 17:10:51 by maiboyer ### ########.fr */
/* Created: 2024/05/14 18:32:57 by maiboyer #+# #+# */
/* Updated: 2024/05/14 18:39:13 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ALLOC_H
#define ALLOC_H
#ifndef MEM_H
#define MEM_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 *mem_alloc(t_usize size);
void *mem_alloc_array(t_usize size, t_usize count);
void *mem_realloc(void *ptr, t_usize size);
void *mem_realloc_array(void *ptr, t_usize size, t_usize count);
void mem_free(void *ptr);
#endif /* ALLOC_H */
#endif /* MEM_H */

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/03 14:31:12 by maiboyer #+# #+# */
/* Updated: 2024/05/08 19:27:44 by maiboyer ### ########.fr */
/* Updated: 2024/05/14 18:41:05 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -35,18 +35,11 @@ typedef size_t t_usize;
typedef float t_f32;
typedef double t_f64;
typedef int t_file;
typedef struct s_list
{
void *content;
struct s_list *next;
} t_list;
typedef int t_file;
typedef bool t_error;
void me_abort(t_str msg);
void me_exit(t_i32 code);
void me_free(void *ptr);
void print_trace(void);
#define ERROR 1