diff --git a/allocator/include/aq/alloc.h b/allocator/include/aq/alloc.h index 68a0e7ae..1bd9ec28 100644 --- a/allocator/include/aq/alloc.h +++ b/allocator/include/aq/alloc.h @@ -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 */ diff --git a/allocator/include/aq/alloc_internal.h b/allocator/include/aq/alloc_internal.h index b7b89212..a096a1bc 100644 --- a/allocator/include/aq/alloc_internal.h +++ b/allocator/include/aq/alloc_internal.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 +# include "aq/alloc.h" +# include "me/types.h" +# include -#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 */ diff --git a/allocator/include/aq/allocator.h b/allocator/include/aq/allocator.h index 5c6f15b8..484a0e33 100644 --- a/allocator/include/aq/allocator.h +++ b/allocator/include/aq/allocator.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 */ diff --git a/allocator/include/aq/internal_vg_funcs.h b/allocator/include/aq/internal_vg_funcs.h index 6a4e2cbd..0e9e4513 100644 --- a/allocator/include/aq/internal_vg_funcs.h +++ b/allocator/include/aq/internal_vg_funcs.h @@ -6,59 +6,63 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 +# include # endif -# define VGFUNCS -# include -# include -#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 */ diff --git a/allocator/include/aq/libc_wrapper.h b/allocator/include/aq/libc_wrapper.h index cf3ba8dd..fa548647 100644 --- a/allocator/include/aq/libc_wrapper.h +++ b/allocator/include/aq/libc_wrapper.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 */ diff --git a/allocator/include/aq/melloc.h b/allocator/include/aq/melloc.h index 7a46757c..719d8248 100644 --- a/allocator/include/aq/melloc.h +++ b/allocator/include/aq/melloc.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 */ diff --git a/allocator/include/aq/melloc_interal.h b/allocator/include/aq/melloc_interal.h index 7a798496..6b108de3 100644 --- a/allocator/include/aq/melloc_interal.h +++ b/allocator/include/aq/melloc_interal.h @@ -6,59 +6,80 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ diff --git a/allocator/src.list b/allocator/src.list index 6be242f4..aa50e884 100644 --- a/allocator/src.list +++ b/allocator/src.list @@ -4,9 +4,12 @@ lc_alloc/functions1 lc_alloc/functions2 me_alloc/functions1 me_alloc/functions2 +me_alloc/internals +me_alloc/pages +me_alloc/realloc vg/dummy_block -vg/dummy_mempool vg/dummy_mem_status +vg/dummy_mempool vg/valgrind_block -vg/valgrind_mempool vg/valgrind_mem_status +vg/valgrind_mempool diff --git a/allocator/src/alloc.c b/allocator/src/alloc.c index dfeef9db..9e251021 100644 --- a/allocator/src/alloc.c +++ b/allocator/src/alloc.c @@ -15,21 +15,21 @@ typedef struct s_allocator_page { - void *data; - t_usize size; -} t_allocator_page; + void *data; + t_usize size; +} t_allocator_page; typedef struct s_page_list { t_usize allocated; t_allocator_page a[10]; - struct s_page_list *next; -} t_page_list; + struct s_page_list *next; +} t_page_list; /* -void *me_malloc(t_usize size) +void *me_malloc(t_usize size) { - t_mblock *block; + t_mblock *block; size = usize_round_up_to(size, 16); block = get_block_for_size(size); @@ -42,18 +42,18 @@ block->size); vg_mem_no_access(block, sizeof(*block)); return ((void *)(((t_usize)block) + sizeof(*block))); } -void *me_calloc(t_usize elem_size, t_usize elem_count) +void *me_calloc(t_usize elem_size, t_usize elem_count) { if (elem_size != 0 && elem_count > SIZE_MAX / elem_size) me_abort("calloc overflow !"); return (me_malloc(elem_size * elem_count)); } -void *me_realloc(void *ptr, t_usize new_size) +void *me_realloc(void *ptr, t_usize new_size) { - t_mblock *block; - void *ret; - t_usize old_size; + t_mblock *block; + void *ret; + t_usize old_size; if (ptr == NULL) return (me_malloc(new_size)); @@ -83,19 +83,19 @@ void *me_realloc(void *ptr, t_usize new_size) } } -void *me_realloc_array(void *ptr, t_usize elem_size, t_usize elem_count) +void *me_realloc_array(void *ptr, t_usize elem_size, t_usize elem_count) { if (elem_size != 0 && elem_count > SIZE_MAX / elem_size) me_abort("realloc_array overflow !"); return (me_realloc(ptr, elem_size * elem_count)); } -void mem_free(void *ptr) +void mem_free(void *ptr) { - t_mblock *cur; + t_mblock *cur; if (ptr == NULL) - return; + return ; cur = (void *)(((t_usize)ptr) - sizeof(t_mblock)); vg_mempool_free(POOL_ADDR, ptr); vg_mem_defined(cur, sizeof(*cur)); @@ -108,8 +108,8 @@ void mem_free(void *ptr) // { // t_ptr_table *table; // t_ptr_table *table_next; -// t_usize i; -// t_usize unfree_count; +// t_usize i; +// t_usize unfree_count; // // unfree_count = 0; // table = get_table(); @@ -140,7 +140,7 @@ void mem_free(void *ptr) // void *me_malloc(t_usize size) // { // t_mblock *block; -// void *ret; +// void *ret; // // size = usize_round_up_to(size, 16); // printf("Allocating %zu.\n", size); @@ -164,7 +164,7 @@ void mem_free(void *ptr) // void *me_realloc(void *ptr, t_usize new_size) // { // t_mblock *block; -// void *ret; +// void *ret; // // if (ptr == NULL) // return (me_malloc(new_size)); @@ -187,7 +187,7 @@ void mem_free(void *ptr) // t_mblock *cur; // // if (ptr == NULL) -// return; +// return ; // cur = get_block_from_ptr(ptr); // if (cur == NULL) // return (me_abort("Invalid free (not allocated with me_*alloc)!")); diff --git a/allocator/src/get_arena.c b/allocator/src/get_arena.c deleted file mode 100644 index 6465024c..00000000 --- a/allocator/src/get_arena.c +++ /dev/null @@ -1,362 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* get_arena.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: maiboyer +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/05/07 09:47:50 by maiboyer #+# #+# */ -/* Updated: 2024/05/14 16:52:37 by maiboyer ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "aq/alloc.h" -#include "aq/alloc_internal.h" -#include "aq/internal_vg_funcs.h" -#include "me/fs/putendl_fd.h" -#include "me/fs/putnbr_fd.h" -#include "me/fs/putstr_fd.h" -#include "me/mem/mem.h" -#include "me/mem/mem.h" -#include "me/mem/mem.h" -#include "me/num/usize.h" -#include -#include -/* - -void *__libc_malloc(size_t size); -void __libc_free(void *ptr); - -t_mpage *alloc_page(t_usize size) -{ - t_mpage *val; - - size = usize_round_up_to(size + sizeof(t_mpage), PAGE_SIZE_DEFAULT); - val = __libc_malloc(size); - if (val == NULL) - return (NULL); - mem_set_zero(val, size); - val->next = NULL; - val->page_size = size - sizeof(*val); - val->first = (t_mblock *)(((t_usize)val) + sizeof(t_mpage)); - val->first->page = val; - val->first->next = NULL; - val->first->used = false; - val->first->size = size - sizeof(t_mpage) - sizeof(t_mblock) * 2; - mem_copy(val->first->padding, BLOCK_PADDING, 7); - vg_mem_no_access(val, size); - return (val); -} - -t_mpage *get_head_arena(void) -{ - static t_mpage *val = NULL; - - if (val == NULL && - PAGE_SIZE_DEFAULT > sizeof(t_mpage) + sizeof(t_mblock) * 2 + 16) - { - vg_mempool_create(POOL_ADDR); - val = alloc_page(PAGE_SIZE_DEFAULT - sizeof(t_mpage)); - } - if (val == NULL) - (me_putstr_fd("Failed to alloc first page", 2), exit(1)); - return (val); -} - -t_mblock *split_block(t_mblock *self, t_usize min_size) -{ - t_usize remaining; - t_mblock *old_next; - - vg_mem_defined(self, sizeof(*self)); - min_size = usize_round_up_to(min_size, 16); - if (self->size > (min_size + sizeof(t_mblock) + 16)) - { - remaining = self->size - min_size - sizeof(t_mblock); - printf("splitting %zu into %zu and %zu\n", self->size, min_size, - remaining); - self->size = min_size; - old_next = self->next; - self->next = (t_mblock *)(((t_usize)self) + sizeof(*self) + self->size); - vg_mem_defined(self->next, sizeof(*self->next)); - self->next->page = self->page; - self->next->next = old_next; - self->next->used = false; - self->next->size = remaining; - mem_copy(self->next->padding, BLOCK_PADDING, 7); - vg_mem_no_access(self->next, sizeof(*self->next)); - } - vg_mem_no_access(self, sizeof(*self)); - return (self); -} - -t_mblock *get_block_for_size(t_usize size) -{ - t_mblock *last; - t_mblock *cur; - t_mpage *head; - - last = NULL; - head = get_head_arena(); - vg_mem_defined(head, sizeof(*head)); - cur = head->first; - vg_mem_no_access(head, sizeof(*head)); - while (cur) - { - vg_mem_defined(cur, sizeof(*cur)); - if (!cur->used && cur->size >= size) - return (split_block(cur, size)); - last = cur; - cur = cur->next; - vg_mem_no_access(last, sizeof(*last)); - } - if (last == NULL) - return (NULL); - vg_mem_defined(last, sizeof(*last)); - vg_mem_defined(last->page, sizeof(*last->page)); - last->page->next = alloc_page(size + sizeof(t_mblock)); - if (last->page->next == NULL) - me_abort("Failed to alloc page!"); - vg_mem_defined(last->page->next, sizeof(*last->page->next)); - last->next = last->page->next->first; - cur = last->page->next->first; - vg_mem_no_access(last->page->next, sizeof(*last->page->next)); - vg_mem_no_access(last->page, sizeof(*last->page)); - vg_mem_no_access(last, sizeof(*last)); - return (split_block(cur, size)); -} - -void print_pages_info(void) -{ - t_mpage *page; - t_mpage *old; - t_i32 page_nb; - - page = get_head_arena(); - page_nb = 0; - while (page != NULL) - { - vg_mem_defined(page, sizeof(*page)); - me_putstr_fd("Page ", 2); - me_putnbr_fd(page_nb++, 2); - me_putstr_fd(" with size ", 2); - me_putnbr_fd((t_i32)page->page_size, 2); - me_putstr_fd("\n", 2); - old = page; - page = page->next; - vg_mem_no_access(old, sizeof(*old)); - } -} - -bool merge_block(t_mblock *self, t_usize min_size) -{ - t_mblock *old_next; - - vg_mem_defined(self, sizeof(*self)); - vg_mem_defined(self->next, sizeof(*self->next)); - if (!(self->next && !self->next->used && self->next->page == self->page && - self->size + self->next->size + sizeof(t_mblock) >= min_size)) - return (vg_mem_no_access(self->next, sizeof(*self->next)), - vg_mem_no_access(self, sizeof(*self)), false); - old_next = self->next; - self->size += sizeof(*self) + self->next->size; - self->next = self->next->next; - vg_mem_no_access(self, sizeof(*self)); - vg_mem_no_access(old_next, sizeof(*old_next)); - return (true); -} - -void uninit_allocator(void) -{ - t_mpage *page; - void *tmp; - t_mblock *block; - t_usize count_block; - - page = get_head_arena(); - count_block = 0; - vg_mem_defined(page, sizeof(*page)); - block = page->first; - while (block) - { - vg_mem_defined(block, sizeof(*block)); - if (block->used) - count_block += 1; - block = block->next; - } - while (page) - { - vg_mem_defined(page, sizeof(*page)); - tmp = page->next; - __libc_free(page); - page = tmp; - } - if (count_block != 0) - (me_putnbr_fd(count_block, 2), - me_putendl_fd(" Blocks weren't freed when exiting !", 2)); -}*/ - -// -// void free_ifn(void *ptr) -// { -// if (ptr != NULL) -// __libc_free(ptr); -// } -// -// t_mpage *alloc_page(t_usize size) -// { -// t_mpage *page; -// void *data; -// t_mblock *block; -// -// size = usize_round_up_to(size, PAGE_SIZE_DEFAULT); -// page = __libc_malloc(sizeof(t_mpage)); -// block = __libc_malloc(sizeof(t_mblock)); -// data = __libc_malloc(size); -// if (page == NULL || data == NULL || block == NULL || -// PAGE_SIZE_DEFAULT <= (t_usize)0) -// return (free_ifn(page), free_ifn(data), free_ifn(block), NULL); -// page->data = data; -// page->next = NULL; -// page->page_size = size; -// page->first = block; -// block->offset = 0; -// block->page = page; -// block->next = NULL; -// block->used = false; -// block->size = size; -// mem_copy(block->padding, BLOCK_PADDING, 7); -// return (page); -// } -// -// t_mpage *get_head_arena(void) -// { -// static t_mpage *val = NULL; -// -// if (val == NULL) -// { -// val = alloc_page(PAGE_SIZE_DEFAULT); -// if (val == NULL) -// (me_putstr_fd("Failed to alloc first page", 2), exit(1)); -// } -// return (val); -// } -// -// t_mblock *split_block(t_mblock *self, t_usize min_size) -// { -// t_usize remaining; -// t_mblock *new_next; -// -// min_size = usize_round_up_to(min_size, 16); -// if (self->size > min_size) -// { -// remaining = self->size - min_size - 1; -// new_next = __libc_malloc(sizeof(t_mblock)); -// if (new_next == NULL) -// return (me_abort("Failed to alloc block"), NULL); -// printf("splitting %zu into %zu and %zu\n", self->size, min_size, -// remaining); -// self->size = min_size; -// new_next->page = self->page; -// new_next->next = self->next; -// new_next->offset = self->offset + self->size + 1; -// new_next->used = false; -// new_next->size = remaining; -// mem_copy(new_next->padding, BLOCK_PADDING, 7); -// self->next = new_next; -// } -// return (self); -// } -// -// t_mblock *get_block_for_size(t_usize size) -// { -// t_mblock *last; -// t_mblock *cur; -// -// last = NULL; -// cur = get_head_arena()->first; -// while (cur) -// { -// if (cur->page == NULL) -// me_abort("block doesn't have a page ?????"); -// if (!cur->used && cur->size >= size) -// return (split_block(cur, size)); -// last = cur; -// cur = cur->next; -// } -// if (last == NULL) -// return (NULL); -// last->page->next = alloc_page(size); -// if (last->page->next == NULL) -// me_abort("Failed to alloc page!"); -// last->next = last->page->next->first; -// return (split_block(last->page->next->first, size)); -// } -// -// t_mblock *get_block_from_ptr(void *ptr) -// { -// t_mpage *page; -// t_mblock *block; -// -// page = get_page_from_ptr(ptr); -// if (page == NULL) -// return (NULL); -// block = page->first; -// while (block && block->page == page) -// { -// if (((t_u8 *)page->data) + block->offset == (t_u8 *)ptr) -// return (block); -// block = block->next; -// } -// return (NULL); -// } -// -// t_mpage *get_page_from_ptr(void *ptr) -// { -// t_mpage *page; -// -// page = get_head_arena(); -// while (page) -// { -// if ((t_u8 *)page->data <= (t_u8 *)ptr && -// (t_u8 *)ptr < (((t_u8 *)page->data) + page->page_size)) -// return (page); -// page = page->next; -// } -// return (NULL); -// } -// -// void print_pages_info(void) -// { -// t_mpage *page; -// t_i32 page_nb; -// -// page = get_head_arena(); -// page_nb = 0; -// while (page != NULL) -// { -// me_putstr_fd("Page ", 2); -// me_putnbr_fd(page_nb++, 2); -// me_putstr_fd(" with size ", 2); -// me_putnbr_fd((t_i32)page->page_size, 2); -// me_putstr_fd("\n", 2); -// page = page->next; -// } -// } -// -// bool merge_next_block(t_mblock *cur, t_usize min_size) -// { -// t_mblock *next; -// -// if (cur->next != NULL && cur->page == cur->next->page && !cur->next->used && -// cur->size + cur->next->size >= min_size) -// { -// cur->size += cur->next->size; -// next = cur->next; -// cur->next = cur->next->next; -// printf("merging two blocks %p and %p\n", cur, cur->next); -// __libc_free(next); -// return (NO_ERROR); -// } -// return (ERROR); -// } diff --git a/allocator/src/lc_alloc/functions1.c b/allocator/src/lc_alloc/functions1.c index b23aaf05..20f0f2f7 100644 --- a/allocator/src/lc_alloc/functions1.c +++ b/allocator/src/lc_alloc/functions1.c @@ -14,39 +14,39 @@ #include "aq/libc_wrapper.h" #include "me/types.h" -void *__libc_malloc(t_usize size); -void *__libc_calloc(t_usize size, t_usize elem); -void *__libc_realloc(void *ptr, t_usize size); -void *__libc_reallocarray(void *ptr, t_usize size, t_usize elem); -void __libc_free(void *ptr); +void *__libc_malloc(t_usize size); +void *__libc_calloc(t_usize size, t_usize elem); +void *__libc_realloc(void *ptr, t_usize size); +void *__libc_reallocarray(void *ptr, t_usize size, t_usize elem); +void __libc_free(void *ptr); -void *lc_malloc(t_allocator *self, t_usize size) +void *lc_malloc(t_allocator *self, t_usize size) { (void)(self); return (__libc_malloc(size)); } -void *lc_calloc(t_allocator *self, t_usize size, t_usize elem) +void *lc_calloc(t_allocator *self, t_usize size, t_usize elem) { (void)(self); return (__libc_calloc(size, elem)); } -void *lc_realloc(t_allocator *self, void *ptr, t_usize size) +void *lc_realloc(t_allocator *self, void *ptr, t_usize size) { (void)(self); return (__libc_realloc(ptr, size)); } -void *lc_realloc_array(t_allocator *self, void *ptr, t_usize size, t_usize elem) +void *lc_realloc_array(t_allocator *self, void *ptr, t_usize size, + t_usize elem) { (void)(self); return (__libc_reallocarray(ptr, size, elem)); } -void lc_free(t_allocator *self, void *ptr) +void lc_free(t_allocator *self, void *ptr) { (void)(self); return (__libc_free(ptr)); } - diff --git a/allocator/src/lc_alloc/functions2.c b/allocator/src/lc_alloc/functions2.c index db95a561..ad4770f1 100644 --- a/allocator/src/lc_alloc/functions2.c +++ b/allocator/src/lc_alloc/functions2.c @@ -13,12 +13,12 @@ #include "aq/allocator.h" #include "aq/libc_wrapper.h" -void lc_uninit(t_allocator *self) +void lc_uninit(t_allocator *self) { (void)(self); } -t_allocator lc_init(void) +t_allocator lc_init(void) { return ((t_allocator){ .alloc = lc_malloc, diff --git a/allocator/src/me_alloc/realloc.c b/allocator/src/me_alloc/realloc.c new file mode 100644 index 00000000..039aec4f --- /dev/null +++ b/allocator/src/me_alloc/realloc.c @@ -0,0 +1,97 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* realloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/10 16:48:19 by maiboyer #+# #+# */ +/* Updated: 2024/07/10 17:12:05 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "aq/allocator.h" +#include "aq/internal_vg_funcs.h" +#include "aq/melloc_interal.h" +#include "me/mem/mem.h" +#include "me/types.h" +#include + +static void *_realloc_inner_copy(struct s_allocator_melloc *self, void *ptr, \ + t_usize size, t_chunk *next) +{ + t_chunk *chk; + + chk = ptr - sizeof(*chk); + vg_mem_no_access(next, sizeof(*next)); + next = m_realloc(self, NULL, size); + vg_mem_defined(chk, sizeof(*chk)); + mem_move(next, ptr, chk->size); + vg_mem_no_access(chk, sizeof(*chk)); + m_free(self, ptr); + return (next); +} + +static void *_realloc_inner(struct s_allocator_melloc *self, void *ptr, \ + t_usize size) +{ + t_usize old_size; + t_chunk *chk; + t_chunk *next; + + chk = ptr - sizeof(*chk); + vg_mem_defined(chk, sizeof(*chk)); + if (chk->size >= size) + return (vg_mem_no_access(chk, sizeof(*chk)), ptr); + next = get_next_block(chk, false); + (vg_mem_defined(next, sizeof(*next)), vg_mem_defined(chk, sizeof(*chk))); + if (next != NULL && !next->used && chk->size + next->size + + sizeof(*next) >= size) + { + old_size = chk->size; + chk->size += next->size + sizeof(*next); + return (vg_mem_undefined(next, next->size + sizeof(*next)), \ + vg_block_resize((void *)chk + sizeof(*chk), old_size, chk->size), \ + vg_mem_no_access(chk, sizeof(*chk)), ptr); + } + else + return (_realloc_inner_copy(self, ptr, size, next)); +} + +static void *_realloc_alloc(struct s_allocator_melloc *self, t_usize size) +{ + t_chunk *chunk; + + chunk = find_chunk_of_size(self, size); + if (chunk == NULL) + { + if (alloc_new_page(self, size + sizeof(t_chunk) * 2)) + return (m_alloc_error(self, "Unable to alloc page")); + chunk = find_chunk_of_size(self, size); + if (chunk == NULL) + return (m_alloc_error(self, "Unable to find block")); + } + vg_mem_defined((void *)chunk, sizeof(*chunk)); + chunk->used = true; + vg_mem_defined((void *)chunk + sizeof(*chunk), chunk->size); + mem_set_zero((void *)chunk + sizeof(*chunk), chunk->size); + vg_block_malloc((void *)chunk + sizeof(*chunk), chunk->size); + vg_mem_no_access((void *)chunk, sizeof(*chunk)); + return ((void *)chunk + sizeof(*chunk)); +} + +void *m_realloc(struct s_allocator_melloc *self, void *ptr, t_usize size) +{ + t_chunk *chunk; + t_chunk *next; + + if (size > INT32_MAX - sizeof(t_chunk) * 10) + return (errno = ENOMEM, NULL); + size = round_to_pow2(size, PAGE_ALIGN); + if (ptr != NULL && size == 0) + return (m_free(self, ptr), NULL); + if (ptr == NULL) + return (_realloc_alloc(self, size)); + else + return (_realloc_inner(self, ptr, size)); +} diff --git a/allocator/src/vg/dummy_block.c b/allocator/src/vg/dummy_block.c index e8769f1e..1b711be7 100644 --- a/allocator/src/vg/dummy_block.c +++ b/allocator/src/vg/dummy_block.c @@ -14,20 +14,20 @@ #ifndef VGFUNCS -void vg_block_malloc(void *ptr, t_usize size) +void vg_block_malloc(void *ptr, t_usize size) { (void)(ptr); (void)(size); } -void vg_block_resize(void *ptr, t_usize oldsize, t_usize newsize) +void vg_block_resize(void *ptr, t_usize oldsize, t_usize newsize) { (void)(ptr); (void)(oldsize); (void)(newsize); } -void vg_block_free(void *ptr) +void vg_block_free(void *ptr) { (void)(ptr); } diff --git a/allocator/src/vg/dummy_mem_status.c b/allocator/src/vg/dummy_mem_status.c index 123604c5..c9fe9539 100644 --- a/allocator/src/vg/dummy_mem_status.c +++ b/allocator/src/vg/dummy_mem_status.c @@ -14,19 +14,19 @@ #ifndef VGFUNCS -void vg_mem_no_access(void *ptr, t_usize size) +void vg_mem_no_access(void *ptr, t_usize size) { (void)(ptr); (void)(size); } -void vg_mem_undefined(void *ptr, t_usize size) +void vg_mem_undefined(void *ptr, t_usize size) { (void)(ptr); (void)(size); } -void vg_mem_defined(void *ptr, t_usize size) +void vg_mem_defined(void *ptr, t_usize size) { (void)(ptr); (void)(size); diff --git a/allocator/src/vg/dummy_mempool.c b/allocator/src/vg/dummy_mempool.c index 68d5f617..271e0499 100644 --- a/allocator/src/vg/dummy_mempool.c +++ b/allocator/src/vg/dummy_mempool.c @@ -14,37 +14,37 @@ #ifndef VGFUNCS -void vg_mempool_create_ext(void *pool, t_usize flags) +void vg_mempool_create_ext(void *pool, t_usize flags) { (void)(pool); (void)(flags); } -void vg_mempool_resize(void *pool, void *ptr, t_usize size) +void vg_mempool_resize(void *pool, void *ptr, t_usize size) { (void)(pool); (void)(ptr); (void)(size); } -void vg_mempool_create(void *pool) +void vg_mempool_create(void *pool) { (void)(pool); } -void vg_mempool_destroy(void *pool) +void vg_mempool_destroy(void *pool) { (void)(pool); } -void vg_mempool_alloc(void *pool, void *addr, t_usize size) +void vg_mempool_alloc(void *pool, void *addr, t_usize size) { (void)(pool); (void)(addr); (void)(size); } -void vg_mempool_free(void *pool, void *addr) +void vg_mempool_free(void *pool, void *addr) { (void)(pool); (void)(addr); diff --git a/allocator/src/vg/valgrind_block.c b/allocator/src/vg/valgrind_block.c index c4c19e13..6ddad468 100644 --- a/allocator/src/vg/valgrind_block.c +++ b/allocator/src/vg/valgrind_block.c @@ -15,17 +15,17 @@ #ifdef VGFUNCS -void vg_block_malloc(void *ptr, t_usize size) +void vg_block_malloc(void *ptr, t_usize size) { VALGRIND_MALLOCLIKE_BLOCK(ptr, size, redzone_size(), ZEROED_ALLOC); } -void vg_block_resize(void *ptr, t_usize oldsize, t_usize newsize) +void vg_block_resize(void *ptr, t_usize oldsize, t_usize newsize) { VALGRIND_RESIZEINPLACE_BLOCK(ptr, oldsize, newsize, redzone_size()); } -void vg_block_free(void *ptr) +void vg_block_free(void *ptr) { VALGRIND_FREELIKE_BLOCK(ptr, redzone_size()); } diff --git a/allocator/src/vg/valgrind_mem_status.c b/allocator/src/vg/valgrind_mem_status.c index bbe5ee0e..4e82482f 100644 --- a/allocator/src/vg/valgrind_mem_status.c +++ b/allocator/src/vg/valgrind_mem_status.c @@ -14,17 +14,17 @@ #ifdef VGFUNCS -void vg_mem_no_access(void *ptr, t_usize size) +void vg_mem_no_access(void *ptr, t_usize size) { VALGRIND_MAKE_MEM_NOACCESS(ptr, size); } -void vg_mem_undefined(void *ptr, t_usize size) +void vg_mem_undefined(void *ptr, t_usize size) { VALGRIND_MAKE_MEM_UNDEFINED(ptr, size); } -void vg_mem_defined(void *ptr, t_usize size) +void vg_mem_defined(void *ptr, t_usize size) { VALGRIND_MAKE_MEM_DEFINED(ptr, size); } diff --git a/allocator/src/vg/valgrind_mempool.c b/allocator/src/vg/valgrind_mempool.c index 80b46f8f..1e13f963 100644 --- a/allocator/src/vg/valgrind_mempool.c +++ b/allocator/src/vg/valgrind_mempool.c @@ -15,41 +15,39 @@ #ifdef VGFUNCS -void vg_mempool_create_ext(void *pool, t_usize flags) +void vg_mempool_create_ext(void *pool, t_usize flags) { - t_usize actual_flags; + t_usize actual_flags; actual_flags = 0; if (flags & MEMPOOL_FLAG_MALLOCLIKE) actual_flags |= VALGRIND_MEMPOOL_METAPOOL; if (flags & MEMPOOL_FLAG_AUTOFREE) actual_flags |= VALGRIND_MEMPOOL_AUTO_FREE; - VALGRIND_CREATE_MEMPOOL_EXT(pool, 0, ZEROED_POOL, actual_flags); } -void vg_mempool_resize(void *pool, void *ptr, t_usize size) +void vg_mempool_resize(void *pool, void *ptr, t_usize size) { VALGRIND_MEMPOOL_CHANGE(pool, ptr, ptr, size); } -void vg_mempool_create(void *pool) +void vg_mempool_create(void *pool) { VALGRIND_CREATE_MEMPOOL(pool, 0, ZEROED_POOL); - } -void vg_mempool_destroy(void *pool) +void vg_mempool_destroy(void *pool) { VALGRIND_DESTROY_MEMPOOL(pool); } -void vg_mempool_alloc(void *pool, void *addr, t_usize size) +void vg_mempool_alloc(void *pool, void *addr, t_usize size) { VALGRIND_MEMPOOL_ALLOC(pool, addr, size); } -void vg_mempool_free(void *pool, void *addr) +void vg_mempool_free(void *pool, void *addr) { VALGRIND_MEMPOOL_FREE(pool, addr); }