diff --git a/stdme/include/me/alloc/alloc_internal.h b/stdme/include/me/alloc/alloc_internal.h index 44bc9b5f..48c2e273 100644 --- a/stdme/include/me/alloc/alloc_internal.h +++ b/stdme/include/me/alloc/alloc_internal.h @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/07 09:48:17 by maiboyer #+# #+# */ -/* Updated: 2024/05/09 13:30:12 by maiboyer ### ########.fr */ +/* Updated: 2024/05/12 14:02:32 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,6 +25,7 @@ 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; @@ -32,6 +33,7 @@ typedef struct s_mblock typedef struct s_mpage { t_usize page_size; + void *data; t_mblock *first; struct s_mpage *next; } t_mpage; @@ -40,10 +42,11 @@ typedef struct s_mpage 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); 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 */ diff --git a/stdme/src/alloc/alloc.c b/stdme/src/alloc/alloc.c index f8bab81a..13812b68 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/10 21:50:09 by maiboyer ### ########.fr */ +/* Updated: 2024/05/12 14:05:48 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,17 +22,22 @@ #include #include +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; - mem_set_zero((t_u8 *)block + sizeof(*block), block->size); - return ((void *)(((t_usize)block) + sizeof(t_mblock))); + 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) @@ -49,17 +54,11 @@ void *me_realloc(void *ptr, t_usize new_size) if (ptr == NULL) return (me_malloc(new_size)); - block = (void *)((t_u8 *)(ptr) - sizeof(t_mblock)); - if (block->size <= new_size) + block = get_block_from_ptr(ptr); + if (block == NULL || block->size <= new_size) return (ptr); - if (block->next && block->next->page == block->page && !block->used && - block->next->size + block->size + sizeof(t_mblock) >= new_size) - - { - block->size = block->size + block->next->size + sizeof(t_mblock); - block->next = block->next->next; + if (!merge_next_block(block, new_size)) return (ptr); - } else { ret = me_malloc(new_size); @@ -75,12 +74,9 @@ void me_free(void *ptr) if (ptr == NULL) return; - cur = (void *)(((t_usize)ptr) - sizeof(t_mblock)); + cur = get_block_from_ptr(ptr); + if (cur == NULL) + return (me_abort("Invalid free (not allocated with me_*alloc)!")); cur->used = false; - if (cur->next != NULL && cur->page == cur->next->page && !cur->next->used) - { - cur->size += sizeof(t_mblock) + cur->next->size; - mem_set_zero(cur->next->padding, 7); - cur->next = cur->next->next; - } + merge_next_block(cur, ~0); } diff --git a/stdme/src/alloc/get_arena.c b/stdme/src/alloc/get_arena.c index b7b57e64..4219ac8c 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/11 15:05:07 by maiboyer ### ########.fr */ +/* Updated: 2024/05/12 14:03:55 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,34 +23,49 @@ #include 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 *val; + t_mpage *page; + void *data; + t_mblock *block; - size = usize_round_up_to(size + sizeof(t_mpage), PAGE_SIZE_DEFAULT); - val = __libc_malloc(size); - if (val == NULL || sizeof(t_mpage) + sizeof(t_mblock) >= PAGE_SIZE_DEFAULT) - return (NULL); - val->next = NULL; - val->page_size = size; - 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_mblock) - sizeof(t_mpage); - mem_copy(val->first->padding, BLOCK_PADDING, 7); - return (val); + 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 && PAGE_SIZE_DEFAULT > sizeof(t_mpage)) - val = alloc_page(PAGE_SIZE_DEFAULT - sizeof(t_mpage)); if (val == NULL) - (me_putstr_fd("Failed to alloc first page", 2), exit(1)); + { + val = alloc_page(PAGE_SIZE_DEFAULT); + if (val == NULL) + (me_putstr_fd("Failed to alloc first page", 2), exit(1)); + } return (val); } @@ -58,22 +73,26 @@ 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 + sizeof(t_mblock) + 16)) + if (self->size > min_size) { - remaining = self->size - min_size - sizeof(t_mblock); + 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; - self->next = - (t_mblock *)(((t_usize)self) + self->size + sizeof(t_mblock)); - 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); + 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); } @@ -85,6 +104,7 @@ t_mblock *get_block_for_size(t_usize size) last = NULL; cur = get_head_arena()->first; + printf("cur == %p\n", cur); while (cur) { if (cur->page == NULL) @@ -96,13 +116,46 @@ t_mblock *get_block_for_size(t_usize size) } if (last == NULL) return (NULL); - last->page->next = alloc_page(size + sizeof(t_mblock)); + 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; @@ -120,3 +173,20 @@ void print_pages_info(void) 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); +} diff --git a/stdme/src/os/exit.c b/stdme/src/os/exit.c index 8ede006e..73f34310 100644 --- a/stdme/src/os/exit.c +++ b/stdme/src/os/exit.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/07 13:08:52 by maiboyer #+# #+# */ -/* Updated: 2024/05/09 17:55:46 by maiboyer ### ########.fr */ +/* Updated: 2024/05/12 14:07:28 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,22 +21,25 @@ void __libc_free(void *ptr); void me_exit(t_i32 exit_code) { t_mpage *page; - t_mpage *tmp; + 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) { - block = page->first; - while (block) - { - if (block->used) - count_block += 1; - block = block->next; - } tmp = page->next; + __libc_free(page->data); __libc_free(page); page = tmp; }