norminette pass
This commit is contained in:
parent
d59364c35e
commit
fc969750e4
19 changed files with 323 additions and 560 deletions
|
|
@ -11,14 +11,14 @@
|
|||
/* ************************************************************************** */
|
||||
|
||||
#ifndef ALLOC_H
|
||||
#define ALLOC_H
|
||||
# define ALLOC_H
|
||||
|
||||
#include "me/types.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);
|
||||
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 */
|
||||
|
|
|
|||
|
|
@ -11,40 +11,40 @@
|
|||
/* ************************************************************************** */
|
||||
|
||||
#ifndef ALLOC_INTERNAL_H
|
||||
#define ALLOC_INTERNAL_H
|
||||
# define ALLOC_INTERNAL_H
|
||||
|
||||
#include "aq/alloc.h"
|
||||
#include "me/types.h"
|
||||
#include <stdalign.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
|
||||
# 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_mblock *next;
|
||||
struct s_mpage *page;
|
||||
t_usize size;
|
||||
bool used;
|
||||
t_u8 padding[7];
|
||||
} t_mblock;
|
||||
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;
|
||||
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);
|
||||
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_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);
|
||||
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 */
|
||||
|
|
|
|||
|
|
@ -11,32 +11,34 @@
|
|||
/* ************************************************************************** */
|
||||
|
||||
#ifndef ALLOCATOR_H
|
||||
#define ALLOCATOR_H
|
||||
# define ALLOCATOR_H
|
||||
|
||||
#include "me/types.h"
|
||||
# include "me/types.h"
|
||||
|
||||
typedef struct s_allocator t_allocator;
|
||||
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);
|
||||
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
|
||||
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;
|
||||
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 */
|
||||
|
|
|
|||
|
|
@ -6,59 +6,63 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/12 22:20:30 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/05/17 15:34:26 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/07/10 16:40:57 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef INTERNAL_VG_FUNCS_H
|
||||
#define INTERNAL_VG_FUNCS_H
|
||||
# define INTERNAL_VG_FUNCS_H
|
||||
|
||||
#include "me/types.h"
|
||||
#if !defined(NVALGRIND) || defined(VGHEADER)
|
||||
# ifdef NVALGRIND
|
||||
# undef NVALGRIND
|
||||
# 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 VGFUNCS
|
||||
# include <valgrind/memcheck.h>
|
||||
# include <valgrind/valgrind.h>
|
||||
#endif
|
||||
|
||||
#define ZEROED_POOL 1
|
||||
#define ZEROED_ALLOC 1
|
||||
# define ZEROED_POOL 1
|
||||
# define ZEROED_ALLOC 1
|
||||
|
||||
static inline t_usize redzone_size(void)
|
||||
static inline t_usize redzone_size(void)
|
||||
{
|
||||
return (8);
|
||||
}
|
||||
|
||||
#ifdef VGFUNCS
|
||||
static inline bool vg_running(void)
|
||||
# ifdef VGFUNCS
|
||||
|
||||
static inline bool vg_running(void)
|
||||
{
|
||||
return (RUNNING_ON_VALGRIND != 0);
|
||||
}
|
||||
#else
|
||||
static inline bool vg_running(void)
|
||||
# else
|
||||
|
||||
static inline bool vg_running(void)
|
||||
{
|
||||
return (false);
|
||||
}
|
||||
#endif
|
||||
# endif
|
||||
|
||||
#define MEMPOOL_FLAG_MALLOCLIKE 1
|
||||
#define MEMPOOL_FLAG_AUTOFREE 2
|
||||
# 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_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_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);
|
||||
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 */
|
||||
|
|
|
|||
|
|
@ -11,19 +11,19 @@
|
|||
/* ************************************************************************** */
|
||||
|
||||
#ifndef LIBC_WRAPPER_H
|
||||
#define LIBC_WRAPPER_H
|
||||
# define LIBC_WRAPPER_H
|
||||
|
||||
#include "aq/allocator.h"
|
||||
#include "me/types.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);
|
||||
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);
|
||||
t_allocator lc_init(void);
|
||||
|
||||
#endif /* LIBC_WRAPPER_H */
|
||||
|
|
|
|||
|
|
@ -11,18 +11,18 @@
|
|||
/* ************************************************************************** */
|
||||
|
||||
#ifndef MELLOC_H
|
||||
#define MELLOC_H
|
||||
# define MELLOC_H
|
||||
|
||||
#include "aq/allocator.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);
|
||||
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);
|
||||
|
||||
t_allocator m_init(void);
|
||||
t_allocator m_init(void);
|
||||
|
||||
#endif /* MELLOC_H */
|
||||
|
|
|
|||
|
|
@ -6,59 +6,80 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/15 15:27:46 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/05/16 14:58:27 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/07/10 16:55:59 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef MELLOC_INTERAL_H
|
||||
#define MELLOC_INTERAL_H
|
||||
# define MELLOC_INTERAL_H
|
||||
|
||||
#include "aq/allocator.h"
|
||||
#include "me/types.h"
|
||||
# include "aq/allocator.h"
|
||||
# include "me/types.h"
|
||||
|
||||
#define PAGE_LIST_MAX 255
|
||||
#define PAGE_POW_2 12
|
||||
#define PAGE_ALIGN 3
|
||||
# define PAGE_LIST_MAX 255
|
||||
# define PAGE_POW_2 12
|
||||
# define PAGE_ALIGN 3
|
||||
|
||||
typedef struct s_chunk
|
||||
{
|
||||
bool used : 1;
|
||||
t_u64 size : 63;
|
||||
} t_chunk;
|
||||
bool used : 1;
|
||||
t_u64 size : 63;
|
||||
} t_chunk;
|
||||
|
||||
typedef struct s_page
|
||||
{
|
||||
t_usize size;
|
||||
void *data;
|
||||
} t_page;
|
||||
t_usize size;
|
||||
void *data;
|
||||
} t_page;
|
||||
|
||||
typedef struct s_page_list
|
||||
{
|
||||
t_usize len;
|
||||
t_page pages[PAGE_LIST_MAX];
|
||||
struct s_page_list *next;
|
||||
} t_page_list;
|
||||
t_usize len;
|
||||
t_page pages[PAGE_LIST_MAX];
|
||||
struct s_page_list *next;
|
||||
} t_page_list;
|
||||
|
||||
struct s_allocator_melloc
|
||||
struct s_allocator_melloc
|
||||
{
|
||||
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;
|
||||
t_page_list *list;
|
||||
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;
|
||||
t_page_list *list;
|
||||
};
|
||||
|
||||
t_error alloc_page_list(t_page_list **out);
|
||||
void *m_malloc(struct s_allocator_melloc *self, t_usize size);
|
||||
void *m_alloc_array(struct s_allocator_melloc *self, \
|
||||
t_usize size, t_usize count);
|
||||
void *m_realloc(struct s_allocator_melloc *self, \
|
||||
void *ptr, t_usize min_size);
|
||||
void *m_realloc_array(struct s_allocator_melloc *self, \
|
||||
void *ptr, t_usize size, t_usize count);
|
||||
void m_free(struct s_allocator_melloc *self, void *ptr);
|
||||
void m_uninit(struct s_allocator_melloc *self);
|
||||
|
||||
void *m_malloc(struct s_allocator_melloc *self, t_usize size);
|
||||
void *m_alloc_array(struct s_allocator_melloc *self, t_usize size,
|
||||
t_usize count);
|
||||
void *m_realloc(struct s_allocator_melloc *self, void *ptr, t_usize min_size);
|
||||
void *m_realloc_array(struct s_allocator_melloc *self, void *ptr, t_usize size,
|
||||
t_usize count);
|
||||
void m_free(struct s_allocator_melloc *self, void *ptr);
|
||||
void m_uninit(struct s_allocator_melloc *self);
|
||||
t_chunk *find_chunk_of_size(struct s_allocator_melloc *alloc, t_usize size);
|
||||
t_chunk *get_first_block(t_page *page);
|
||||
t_chunk *get_next_block(t_chunk *chunk, bool find_zero);
|
||||
t_chunk *split_block(t_chunk *chunk, t_usize size);
|
||||
void merge_blocks(t_page *page);
|
||||
|
||||
t_error alloc_page_list(t_page_list **out);
|
||||
t_error alloc_new_page(struct s_allocator_melloc *alloc, t_usize page_size);
|
||||
|
||||
static inline t_usize round_to_pow2(t_usize val, t_usize pow)
|
||||
{
|
||||
pow = (1 << pow) - 1;
|
||||
return ((val + pow) & (~pow));
|
||||
}
|
||||
|
||||
static inline void *m_alloc_error(struct s_allocator_melloc *self, t_str msg)
|
||||
{
|
||||
self->uninit((void *)self);
|
||||
me_abort(msg);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
#endif /* MELLOC_INTERAL_H */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue