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

View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* alloc.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 09:42:02 by maiboyer #+# #+# */
/* Updated: 2024/05/07 09:43:31 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);
#endif /* ALLOC_H */

View file

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* alloc_internal.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 09:48:17 by maiboyer #+# #+# */
/* Updated: 2024/05/07 10:14:16 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ALLOC_INTERNAL_H
#define ALLOC_INTERNAL_H
#include "me/alloc/alloc.h"
#include "me/types.h"
#define ARENA_SIZE 16384
typedef struct s_arena_page
{
t_u8 bytes[ARENA_SIZE];
t_usize current_index;
struct s_arena_page *next;
} t_arena_page;
// Will never be null, as it will allocate a new arena if it needs to do so
t_arena_page *get_head_arena(void);
// Will return ERROR if it couldn't malloc the page
t_error alloc_arena(t_arena_page **out);
#endif /* ALLOC_INTERNAL_H */

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 free(void *);
void me_free(void *);
free(buf.buf);
me_free(buf.buf);
}
static inline char str_pop(t_buffer_str *buf)

View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* mem_realloc.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 12:47:23 by maiboyer #+# #+# */
/* Updated: 2024/05/07 12:47:42 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MEM_REALLOC_H
#define MEM_REALLOC_H
#include "me/types.h"
void *mem_realloc(void *ptr, t_usize size);
#endif /* MEM_REALLOC_H */

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 14:10:03 by maiboyer #+# #+# */
/* Updated: 2023/12/11 14:17:32 by maiboyer ### ########.fr */
/* Updated: 2024/05/07 10:45:15 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,5 +17,6 @@
t_usize usize_rotate_left(t_usize n, t_usize by);
t_usize usize_rotate_right(t_usize n, t_usize by);
t_usize usize_round_up_to(t_usize self, t_usize mul);
#endif

20
stdme/include/me/quit.h Normal file
View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* quit.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 12:54:50 by maiboyer #+# #+# */
/* Updated: 2024/05/07 12:55:08 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef QUIT_H
#define QUIT_H
#include "me/types.h"
void quit(t_i32 exit_code);
#endif /* QUIT_H */

View file

@ -6,44 +6,51 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/03 14:31:12 by maiboyer #+# #+# */
/* Updated: 2024/01/05 00:08:41 by maiboyer ### ########.fr */
/* Updated: 2024/05/07 14:52:32 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef TYPES_H
# define TYPES_H
#define TYPES_H
# include <stdbool.h>
# include <stddef.h>
# include <unistd.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <unistd.h>
typedef char *t_str;
typedef const char *t_const_str;
typedef char *t_str;
typedef const char *t_const_str;
typedef unsigned char t_u8;
typedef char t_i8;
typedef unsigned short t_u16;
typedef short t_i16;
typedef int t_i32;
typedef unsigned int t_u32;
typedef unsigned long long t_u64;
typedef long long t_i64;
typedef ssize_t t_isize;
typedef size_t t_usize;
typedef uint8_t t_u8;
typedef int8_t t_i8;
typedef uint16_t t_u16;
typedef int16_t t_i16;
typedef uint32_t t_u32;
typedef int32_t t_i32;
typedef uint64_t t_u64;
typedef int64_t t_i64;
typedef ssize_t t_isize;
typedef size_t t_usize;
typedef float t_f32;
typedef double t_f64;
typedef float t_f32;
typedef double t_f64;
typedef int t_file;
typedef int t_file;
typedef struct s_list
{
void *content;
struct s_list *next;
} t_list;
void *content;
struct s_list *next;
} t_list;
typedef bool t_error;
typedef bool t_error;
# define ERROR 1
# define NO_ERROR 0
void me_abort(void);
void me_exit(t_i32 code);
void me_free(void *ptr);
#define ERROR 1
#define NO_ERROR 0
#define __NonNullable
#endif