diff --git a/gen.list b/gen.list index 6e4974e0..3018dcfc 100644 --- a/gen.list +++ b/gen.list @@ -10,3 +10,6 @@ src/vec/parser_range/parser_range_functions3.c src/vec/reduce_action/reduce_action.c src/vec/reduce_action/reduce_action_functions2.c src/vec/reduce_action/reduce_action_functions3.c +src/vec/str/str.c +src/vec/str/str_functions2.c +src/vec/str/str_functions3.c diff --git a/output/src/vec/parser_heredoc/parser_heredoc.c b/output/src/vec/parser_heredoc/parser_heredoc.c index a39ea661..ed9078d6 100644 --- a/output/src/vec/parser_heredoc/parser_heredoc.c +++ b/output/src/vec/parser_heredoc/parser_heredoc.c @@ -43,7 +43,7 @@ t_error vec_parser_heredoc_push(t_vec_parser_heredoc *vec, t_heredoc element) new_capacity = (vec->capacity * 3) / 2 + 1; while (vec->len + 1 > new_capacity) new_capacity = (new_capacity * 3) / 2 + 1; - vec->buffer = me_realloc(vec->buffer, new_capacity); + vec->buffer = me_realloc_array(vec->buffer, new_capacity, sizeof(t_heredoc)); vec->capacity = new_capacity; } vec->buffer[vec->len] = element; @@ -63,7 +63,7 @@ t_error vec_parser_heredoc_reserve(t_vec_parser_heredoc *vec, t_usize wanted_cap new_capacity = (vec->capacity * 3) / 2 + 1; while (wanted_capacity > new_capacity) new_capacity = (new_capacity * 3) / 2 + 1; - vec->buffer = me_realloc(vec->buffer, new_capacity); + vec->buffer = me_realloc_array(vec->buffer, new_capacity, sizeof(t_heredoc)); vec->capacity = new_capacity; } return (NO_ERROR); diff --git a/output/src/vec/parser_range/parser_range.c b/output/src/vec/parser_range/parser_range.c index f019d533..2d271790 100644 --- a/output/src/vec/parser_range/parser_range.c +++ b/output/src/vec/parser_range/parser_range.c @@ -43,7 +43,7 @@ t_error vec_parser_range_push(t_vec_parser_range *vec, t_parser_range element) new_capacity = (vec->capacity * 3) / 2 + 1; while (vec->len + 1 > new_capacity) new_capacity = (new_capacity * 3) / 2 + 1; - vec->buffer = me_realloc(vec->buffer, new_capacity); + vec->buffer = me_realloc_array(vec->buffer, new_capacity, sizeof(t_parser_range)); vec->capacity = new_capacity; } vec->buffer[vec->len] = element; @@ -63,7 +63,7 @@ t_error vec_parser_range_reserve(t_vec_parser_range *vec, t_usize wanted_capacit new_capacity = (vec->capacity * 3) / 2 + 1; while (wanted_capacity > new_capacity) new_capacity = (new_capacity * 3) / 2 + 1; - vec->buffer = me_realloc(vec->buffer, new_capacity); + vec->buffer = me_realloc_array(vec->buffer, new_capacity, sizeof(t_parser_range)); vec->capacity = new_capacity; } return (NO_ERROR); diff --git a/output/src/vec/reduce_action/reduce_action.c b/output/src/vec/reduce_action/reduce_action.c index a2b684ae..7e31a05f 100644 --- a/output/src/vec/reduce_action/reduce_action.c +++ b/output/src/vec/reduce_action/reduce_action.c @@ -43,7 +43,7 @@ t_error vec_reduce_action_push(t_vec_reduce_action *vec, t_reduce_action element new_capacity = (vec->capacity * 3) / 2 + 1; while (vec->len + 1 > new_capacity) new_capacity = (new_capacity * 3) / 2 + 1; - vec->buffer = me_realloc(vec->buffer, new_capacity); + vec->buffer = me_realloc_array(vec->buffer, new_capacity, sizeof(t_reduce_action)); vec->capacity = new_capacity; } vec->buffer[vec->len] = element; @@ -63,7 +63,7 @@ t_error vec_reduce_action_reserve(t_vec_reduce_action *vec, t_usize wanted_capac new_capacity = (vec->capacity * 3) / 2 + 1; while (wanted_capacity > new_capacity) new_capacity = (new_capacity * 3) / 2 + 1; - vec->buffer = me_realloc(vec->buffer, new_capacity); + vec->buffer = me_realloc_array(vec->buffer, new_capacity, sizeof(t_reduce_action)); vec->capacity = new_capacity; } return (NO_ERROR); diff --git a/output/src/vec/str/str.c b/output/src/vec/str/str.c index 6041d4ea..d833cc85 100644 --- a/output/src/vec/str/str.c +++ b/output/src/vec/str/str.c @@ -43,7 +43,7 @@ t_error vec_str_push(t_vec_str *vec, t_str element) new_capacity = (vec->capacity * 3) / 2 + 1; while (vec->len + 1 > new_capacity) new_capacity = (new_capacity * 3) / 2 + 1; - vec->buffer = me_realloc(vec->buffer, new_capacity); + vec->buffer = me_realloc_array(vec->buffer, new_capacity, sizeof(t_str)); vec->capacity = new_capacity; } vec->buffer[vec->len] = element; @@ -63,7 +63,7 @@ t_error vec_str_reserve(t_vec_str *vec, t_usize wanted_capacity) new_capacity = (vec->capacity * 3) / 2 + 1; while (wanted_capacity > new_capacity) new_capacity = (new_capacity * 3) / 2 + 1; - vec->buffer = me_realloc(vec->buffer, new_capacity); + vec->buffer = me_realloc_array(vec->buffer, new_capacity, sizeof(t_str)); vec->capacity = new_capacity; } return (NO_ERROR); diff --git a/parser/Makefile b/parser/Makefile index fa1b4281..76498fdf 100644 --- a/parser/Makefile +++ b/parser/Makefile @@ -6,7 +6,7 @@ # By: maiboyer +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/03 13:20:01 by maiboyer #+# #+# # -# Updated: 2024/05/09 21:27:15 by maiboyer ### ########.fr # +# Updated: 2024/05/12 17:13:38 by maiboyer ### ########.fr # # # # **************************************************************************** # diff --git a/stdme/generic_sources/src/vec/C__PREFIX__.c__TEMPLATE__ b/stdme/generic_sources/src/vec/C__PREFIX__.c__TEMPLATE__ index f9d9dcce..008e8e4c 100644 --- a/stdme/generic_sources/src/vec/C__PREFIX__.c__TEMPLATE__ +++ b/stdme/generic_sources/src/vec/C__PREFIX__.c__TEMPLATE__ @@ -43,7 +43,7 @@ t_error vec_C__PREFIX___push(t_vec_C__PREFIX__ *vec, C__TYPENAME__ element) new_capacity = (vec->capacity * 3) / 2 + 1; while (vec->len + 1 > new_capacity) new_capacity = (new_capacity * 3) / 2 + 1; - vec->buffer = me_realloc(vec->buffer, new_capacity); + vec->buffer = me_realloc_array(vec->buffer, new_capacity, sizeof(C__TYPENAME__)); vec->capacity = new_capacity; } vec->buffer[vec->len] = element; @@ -63,7 +63,7 @@ t_error vec_C__PREFIX___reserve(t_vec_C__PREFIX__ *vec, t_usize wanted_capacity) new_capacity = (vec->capacity * 3) / 2 + 1; while (wanted_capacity > new_capacity) new_capacity = (new_capacity * 3) / 2 + 1; - vec->buffer = me_realloc(vec->buffer, new_capacity); + vec->buffer = me_realloc_array(vec->buffer, new_capacity, sizeof(C__TYPENAME__)); vec->capacity = new_capacity; } return (NO_ERROR); diff --git a/stdme/include/me/alloc/alloc.h b/stdme/include/me/alloc/alloc.h index 1a1ccec4..826767d0 100644 --- a/stdme/include/me/alloc/alloc.h +++ b/stdme/include/me/alloc/alloc.h @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/07 09:42:02 by maiboyer #+# #+# */ -/* Updated: 2024/05/07 09:43:31 by maiboyer ### ########.fr */ +/* Updated: 2024/05/12 17:10:51 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,5 +18,6 @@ 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); #endif /* ALLOC_H */ diff --git a/stdme/include/me/alloc/alloc_dumb_internal.h b/stdme/include/me/alloc/alloc_dumb_internal.h new file mode 100644 index 00000000..093dbe44 --- /dev/null +++ b/stdme/include/me/alloc/alloc_dumb_internal.h @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* alloc_dumb_internal.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 */ diff --git a/stdme/output/src/vec/buf_str/buf_str.c b/stdme/output/src/vec/buf_str/buf_str.c index 739de0bc..ae3f26b0 100644 --- a/stdme/output/src/vec/buf_str/buf_str.c +++ b/stdme/output/src/vec/buf_str/buf_str.c @@ -43,7 +43,7 @@ t_error vec_buf_str_push(t_vec_buf_str *vec, t_buffer_str element) new_capacity = (vec->capacity * 3) / 2 + 1; while (vec->len + 1 > new_capacity) new_capacity = (new_capacity * 3) / 2 + 1; - vec->buffer = me_realloc(vec->buffer, new_capacity); + vec->buffer = me_realloc_array(vec->buffer, new_capacity, sizeof(t_buffer_str)); vec->capacity = new_capacity; } vec->buffer[vec->len] = element; @@ -63,7 +63,7 @@ t_error vec_buf_str_reserve(t_vec_buf_str *vec, t_usize wanted_capacity) new_capacity = (vec->capacity * 3) / 2 + 1; while (wanted_capacity > new_capacity) new_capacity = (new_capacity * 3) / 2 + 1; - vec->buffer = me_realloc(vec->buffer, new_capacity); + vec->buffer = me_realloc_array(vec->buffer, new_capacity, sizeof(t_buffer_str)); vec->capacity = new_capacity; } return (NO_ERROR); diff --git a/stdme/output/src/vec/str/str.c b/stdme/output/src/vec/str/str.c index 6041d4ea..d833cc85 100644 --- a/stdme/output/src/vec/str/str.c +++ b/stdme/output/src/vec/str/str.c @@ -43,7 +43,7 @@ t_error vec_str_push(t_vec_str *vec, t_str element) new_capacity = (vec->capacity * 3) / 2 + 1; while (vec->len + 1 > new_capacity) new_capacity = (new_capacity * 3) / 2 + 1; - vec->buffer = me_realloc(vec->buffer, new_capacity); + vec->buffer = me_realloc_array(vec->buffer, new_capacity, sizeof(t_str)); vec->capacity = new_capacity; } vec->buffer[vec->len] = element; @@ -63,7 +63,7 @@ t_error vec_str_reserve(t_vec_str *vec, t_usize wanted_capacity) new_capacity = (vec->capacity * 3) / 2 + 1; while (wanted_capacity > new_capacity) new_capacity = (new_capacity * 3) / 2 + 1; - vec->buffer = me_realloc(vec->buffer, new_capacity); + vec->buffer = me_realloc_array(vec->buffer, new_capacity, sizeof(t_str)); vec->capacity = new_capacity; } return (NO_ERROR); diff --git a/stdme/output/src/vec/u8/u8.c b/stdme/output/src/vec/u8/u8.c index 26912107..b7b4550e 100644 --- a/stdme/output/src/vec/u8/u8.c +++ b/stdme/output/src/vec/u8/u8.c @@ -43,7 +43,7 @@ t_error vec_u8_push(t_vec_u8 *vec, t_u8 element) new_capacity = (vec->capacity * 3) / 2 + 1; while (vec->len + 1 > new_capacity) new_capacity = (new_capacity * 3) / 2 + 1; - vec->buffer = me_realloc(vec->buffer, new_capacity); + vec->buffer = me_realloc_array(vec->buffer, new_capacity, sizeof(t_u8)); vec->capacity = new_capacity; } vec->buffer[vec->len] = element; @@ -63,7 +63,7 @@ t_error vec_u8_reserve(t_vec_u8 *vec, t_usize wanted_capacity) new_capacity = (vec->capacity * 3) / 2 + 1; while (wanted_capacity > new_capacity) new_capacity = (new_capacity * 3) / 2 + 1; - vec->buffer = me_realloc(vec->buffer, new_capacity); + vec->buffer = me_realloc_array(vec->buffer, new_capacity, sizeof(t_u8)); vec->capacity = new_capacity; } return (NO_ERROR); diff --git a/stdme/src.list b/stdme/src.list index f9b4794a..3e01e7e1 100644 --- a/stdme/src.list +++ b/stdme/src.list @@ -1,4 +1,5 @@ alloc/alloc +alloc/alloc_dumb alloc/alloc_get_page_from_bloc alloc/get_arena blx/blx diff --git a/stdme/src/alloc/alloc.c b/stdme/src/alloc/alloc.c index 13812b68..0830ef0e 100644 --- a/stdme/src/alloc/alloc.c +++ b/stdme/src/alloc/alloc.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/07 10:13:06 by maiboyer #+# #+# */ -/* Updated: 2024/05/12 14:05:48 by maiboyer ### ########.fr */ +/* Updated: 2024/05/12 15:02:27 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,59 +24,60 @@ void __libc_free(void *ptr); -void *me_malloc(t_usize size) -{ - t_mblock *block; - void *ret; - - size = usize_round_up_to(size, 16); - printf("Allocating %zu.\n", size); - block = get_block_for_size(size); - if (block == NULL) - return (me_abort("Found no page for me_malloc"), NULL); - block->used = true; - ret = ((t_u8 *)block->page->data) + block->offset; - mem_set_zero(ret, block->size); - return (ret); -} - -void *me_calloc(t_usize elem_size, t_usize elem_count) -{ - if (elem_size != 0 && elem_count > SIZE_MAX / elem_size) - return (NULL); - return (me_malloc(elem_size * elem_count)); -} - -void *me_realloc(void *ptr, t_usize new_size) -{ - t_mblock *block; - void *ret; - - if (ptr == NULL) - return (me_malloc(new_size)); - block = get_block_from_ptr(ptr); - if (block == NULL || block->size <= new_size) - return (ptr); - if (!merge_next_block(block, new_size)) - return (ptr); - else - { - ret = me_malloc(new_size); - mem_copy(ret, ptr, block->size); - me_free(ptr); - return (ret); - } -} - -void me_free(void *ptr) -{ - t_mblock *cur; - - if (ptr == NULL) - return; - cur = get_block_from_ptr(ptr); - if (cur == NULL) - return (me_abort("Invalid free (not allocated with me_*alloc)!")); - cur->used = false; - merge_next_block(cur, ~0); -} +// void *me_malloc(t_usize size) +// { +// t_mblock *block; +// void *ret; +// +// size = usize_round_up_to(size, 16); +// printf("Allocating %zu.\n", size); +// block = get_block_for_size(size); +// if (block +// == NULL) +// return (me_abort("Found no page for me_malloc"), NULL); +// block->used = true; +// ret = ((t_u8 *)block->page->data) + block->offset; +// mem_set_zero(ret, block->size); +// return (ret); +// } +// +// void *me_calloc(t_usize elem_size, t_usize elem_count) +// { +// if (elem_size != 0 && elem_count > SIZE_MAX / elem_size) +// return (NULL); +// return (me_malloc(elem_size * elem_count)); +// } +// +// void *me_realloc(void *ptr, t_usize new_size) +// { +// t_mblock *block; +// void *ret; +// +// if (ptr == NULL) +// return (me_malloc(new_size)); +// block = get_block_from_ptr(ptr); +// if (block == NULL || block->size <= new_size) +// return (ptr); +// if (!merge_next_block(block, new_size)) +// return (ptr); +// else +// { +// ret = me_malloc(new_size); +// mem_copy(ret, ptr, block->size); +// me_free(ptr); +// return (ret); +// } +// } +// +// void me_free(void *ptr) +// { +// t_mblock *cur; +// +// if (ptr == NULL) +// return; +// cur = get_block_from_ptr(ptr); +// if (cur == NULL) +// return (me_abort("Invalid free (not allocated with me_*alloc)!")); +// cur->used = false; +// merge_next_block(cur, ~(t_usize)0); +// } diff --git a/stdme/src/alloc/alloc_dumb.c b/stdme/src/alloc/alloc_dumb.c new file mode 100644 index 00000000..82377247 --- /dev/null +++ b/stdme/src/alloc/alloc_dumb.c @@ -0,0 +1,140 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* alloc_dumb.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/12 15:02:37 by maiboyer #+# #+# */ +/* Updated: 2024/05/12 17:09:41 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/alloc/alloc.h" +#include "me/alloc/alloc_dumb_internal.h" +#include "me/fs/putendl_fd.h" +#include "me/mem/mem_copy.h" +#include "me/mem/mem_set_zero.h" +#include + +void *__libc_malloc(size_t size); +void __libc_free(void *ptr); + +t_ptr_table *get_table(void) +{ + static t_ptr_table *table = NULL; + + if (table == NULL) + { + table = __libc_malloc(sizeof(*table)); + if (table == NULL) + (me_putendl_fd("Failed to alloc ptr array", 2), exit(1)); + mem_set_zero(table, sizeof(*table)); + } + return (table); +} + +t_ptr *find_ptr(void *ptr) +{ + t_usize i; + t_ptr_table *t; + + t = get_table(); + i = 0; + while (t) + { + while (i < PTR_LENS) + { + if (t->table[i].ptr == ptr) + return (&t->table[i]); + i++; + } + t = t->next; + } + return (NULL); +} + +void print_pages_info(void) +{ +} + +#include + +void *me_malloc(t_usize size) +{ + t_ptr_table *table; + t_ptr *ret; + + // printf("ALLOC %zu\n", size); + ret = find_ptr(NULL); + if (ret == NULL) + { + table = get_table(); + while (table->next != NULL) + table = table->next; + table->next = __libc_malloc(size); + if (table->next == NULL) + (me_abort("Failed to alloc ptr array")); + mem_set_zero(table->next, sizeof(*table)); + ret = &table->next->table[0]; + } + ret->ptr = __libc_malloc(size); + if (ret->ptr == NULL) + me_abort("Failed to malloc!"); + ret->size = size; + mem_set_zero(ret->ptr, size); + return (ret->ptr); +} + +void *me_calloc(t_usize elem_count, t_usize elem_size) +{ + // printf("CALLOC %zu * %zu\n", elem_count, elem_size); + if (elem_size != 0 && elem_count > SIZE_MAX / elem_size) + return (me_abort("calloc overflow!"), NULL); + return (me_malloc(elem_size * elem_count)); +} + +void *me_realloc(void *ptr, t_usize size) +{ + t_ptr *p; + t_ptr tmp; + + if (ptr == NULL) + return (me_malloc(size)); + // printf("REALLOC %p %zu\n", ptr, size); + p = find_ptr(ptr); + if (p == NULL) + return (me_abort("realloc with wrong ptr"), NULL); + if (p->size >= size) + return (p->ptr); + tmp.size = size; + tmp.ptr = __libc_malloc(size); + if (tmp.ptr == NULL) + me_abort("failed to malloc..."); + mem_copy(tmp.ptr, p->ptr, p->size); + __libc_free(p->ptr); + *p = tmp; + return (tmp.ptr); +} + +void *me_realloc_array(void *ptr, t_usize elem_size, t_usize elem_count) +{ + // printf("CALLOC %zu * %zu\n", elem_count, elem_size); + if (elem_size != 0 && elem_count > SIZE_MAX / elem_size) + return (me_abort("realloc_array overflow !"), NULL); + return (me_realloc(ptr, elem_size * elem_count)); +} + +void me_free(void *ptr) +{ + t_ptr *p; + + // printf("FREE\n"); + p = find_ptr(ptr); + if (p != NULL) + { + __libc_free(p->ptr); + p->ptr = NULL; + p->size = 0; + } +} diff --git a/stdme/src/alloc/get_arena.c b/stdme/src/alloc/get_arena.c index 4219ac8c..a6eaba20 100644 --- a/stdme/src/alloc/get_arena.c +++ b/stdme/src/alloc/get_arena.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/07 09:47:50 by maiboyer #+# #+# */ -/* Updated: 2024/05/12 14:03:55 by maiboyer ### ########.fr */ +/* Updated: 2024/05/12 15:02:02 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,168 +25,167 @@ void *__libc_malloc(size_t size); void __libc_free(void *ptr); -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 <= 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 *old_next; - t_mblock *new_next; - - min_size = usize_round_up_to(min_size, 16); - if (self->size > min_size) - { - remaining = self->size - min_size; - 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; - old_next = self->next; - new_next->page = self->page; - new_next->next = old_next; - new_next->offset = self->offset + self->size; - 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; - printf("cur == %p\n", cur); - 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; - } -} - -t_error 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); -} +// +// 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/stdme/src/os/exit.c b/stdme/src/os/exit.c index 73f34310..c1903d8f 100644 --- a/stdme/src/os/exit.c +++ b/stdme/src/os/exit.c @@ -6,45 +6,86 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/07 13:08:52 by maiboyer #+# #+# */ -/* Updated: 2024/05/12 14:07:28 by maiboyer ### ########.fr */ +/* Updated: 2024/05/12 17:05:27 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ +#include "me/alloc/alloc_dumb_internal.h" #include "me/alloc/alloc_internal.h" #include "me/fs/putendl_fd.h" #include "me/fs/putnbr_fd.h" +#include "me/fs/putstr_fd.h" #include "me/types.h" #include void __libc_free(void *ptr); +// void uninit_allocator(void) +// { +// t_mpage *page; +// void *tmp; +// t_mblock *block; +// t_usize count_block; +// +// page = get_head_arena(); +// count_block = 0; +// block = page->first; +// while (block) +// { +// if (block->used) +// count_block += 1; +// tmp = block->next; +// __libc_free(block); +// block = tmp; +// } +// while (page) +// { +// tmp = page->next; +// __libc_free(page->data); +// __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 uninit_allocator(void) +{ + t_ptr_table *table; + t_ptr_table *table_next; + t_usize i; + t_usize unfree_count; + + unfree_count = 0; + table = get_table(); + + while (table) + { + i = 0; + while (i < PTR_LENS) + { + if (table->table[i].ptr != NULL) + { + __libc_free(table->table[i].ptr); + unfree_count++; + } + i++; + } + table_next = table->next; + __libc_free(table); + table = table_next; + } + if (unfree_count != 0) + { + me_putstr_fd("A total of ", 2); + me_putnbr_fd(unfree_count, 2); + me_putendl_fd(" blocks weren't freed !", 2); + } +} + void me_exit(t_i32 exit_code) { - t_mpage *page; - void *tmp; - t_mblock *block; - t_usize count_block; - - page = get_head_arena(); - count_block = 0; - block = page->first; - while (block) - { - if (block->used) - count_block += 1; - tmp = block->next; - __libc_free(block); - block = tmp; - } - while (page) - { - tmp = page->next; - __libc_free(page->data); - __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)); + uninit_allocator(); exit(exit_code); }