allocated that works ?

This commit is contained in:
Maieul BOYER 2024-05-12 14:09:22 +02:00
parent 334db1eace
commit 4f1768b9e8
No known key found for this signature in database
4 changed files with 131 additions and 59 deletions

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 09:48:17 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_mblock *next;
struct s_mpage *page; struct s_mpage *page;
t_usize size; t_usize size;
t_usize offset;
bool used; bool used;
t_u8 padding[7]; t_u8 padding[7];
} t_mblock; } t_mblock;
@ -32,6 +33,7 @@ typedef struct s_mblock
typedef struct s_mpage typedef struct s_mpage
{ {
t_usize page_size; t_usize page_size;
void *data;
t_mblock *first; t_mblock *first;
struct s_mpage *next; struct s_mpage *next;
} t_mpage; } t_mpage;
@ -40,10 +42,11 @@ typedef struct s_mpage
t_mpage *get_head_arena(void); t_mpage *get_head_arena(void);
// Will return ERROR if it couldn't malloc the page // 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); t_mblock *get_block_for_size(t_usize size);
void print_pages_info(void); void print_pages_info(void);
t_mpage *get_page_from_ptr(void *ptr); 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 */ #endif /* ALLOC_INTERNAL_H */

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 10:13:06 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 <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
void __libc_free(void *ptr);
void *me_malloc(t_usize size) void *me_malloc(t_usize size)
{ {
t_mblock *block; t_mblock *block;
void *ret;
size = usize_round_up_to(size, 16); size = usize_round_up_to(size, 16);
printf("Allocating %zu.\n", size);
block = get_block_for_size(size); block = get_block_for_size(size);
if (block == NULL) if (block == NULL)
return (me_abort("Found no page for me_malloc"), NULL); return (me_abort("Found no page for me_malloc"), NULL);
block->used = true; block->used = true;
mem_set_zero((t_u8 *)block + sizeof(*block), block->size); ret = ((t_u8 *)block->page->data) + block->offset;
return ((void *)(((t_usize)block) + sizeof(t_mblock))); mem_set_zero(ret, block->size);
return (ret);
} }
void *me_calloc(t_usize elem_size, t_usize elem_count) 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) if (ptr == NULL)
return (me_malloc(new_size)); return (me_malloc(new_size));
block = (void *)((t_u8 *)(ptr) - sizeof(t_mblock)); block = get_block_from_ptr(ptr);
if (block->size <= new_size) if (block == NULL || block->size <= new_size)
return (ptr); return (ptr);
if (block->next && block->next->page == block->page && !block->used && if (!merge_next_block(block, new_size))
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;
return (ptr); return (ptr);
}
else else
{ {
ret = me_malloc(new_size); ret = me_malloc(new_size);
@ -75,12 +74,9 @@ void me_free(void *ptr)
if (ptr == NULL) if (ptr == NULL)
return; 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; cur->used = false;
if (cur->next != NULL && cur->page == cur->next->page && !cur->next->used) merge_next_block(cur, ~0);
{
cur->size += sizeof(t_mblock) + cur->next->size;
mem_set_zero(cur->next->padding, 7);
cur->next = cur->next->next;
}
} }

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 09:47:50 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 <stdlib.h> #include <stdlib.h>
void *__libc_malloc(size_t size); 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 *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); size = usize_round_up_to(size, PAGE_SIZE_DEFAULT);
val = __libc_malloc(size); page = __libc_malloc(sizeof(t_mpage));
if (val == NULL || sizeof(t_mpage) + sizeof(t_mblock) >= PAGE_SIZE_DEFAULT) block = __libc_malloc(sizeof(t_mblock));
return (NULL); data = __libc_malloc(size);
val->next = NULL; if (page == NULL || data == NULL || block == NULL || PAGE_SIZE_DEFAULT <= 0)
val->page_size = size; return (free_ifn(page), free_ifn(data), free_ifn(block), NULL);
val->first = (t_mblock *)(((t_usize)val) + sizeof(t_mpage)); page->data = data;
val->first->page = val; page->next = NULL;
val->first->next = NULL; page->page_size = size;
val->first->used = false; page->first = block;
val->first->size = size - sizeof(t_mblock) - sizeof(t_mpage); block->offset = 0;
mem_copy(val->first->padding, BLOCK_PADDING, 7); block->page = page;
return (val); block->next = NULL;
block->used = false;
block->size = size;
mem_copy(block->padding, BLOCK_PADDING, 7);
return (page);
} }
t_mpage *get_head_arena(void) t_mpage *get_head_arena(void)
{ {
static t_mpage *val = NULL; 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) 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); return (val);
} }
@ -58,22 +73,26 @@ t_mblock *split_block(t_mblock *self, t_usize min_size)
{ {
t_usize remaining; t_usize remaining;
t_mblock *old_next; t_mblock *old_next;
t_mblock *new_next;
min_size = usize_round_up_to(min_size, 16); 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, printf("splitting %zu into %zu and %zu\n", self->size, min_size,
remaining); remaining);
self->size = min_size; self->size = min_size;
old_next = self->next; old_next = self->next;
self->next = new_next->page = self->page;
(t_mblock *)(((t_usize)self) + self->size + sizeof(t_mblock)); new_next->next = old_next;
self->next->page = self->page; new_next->offset = self->offset + self->size;
self->next->next = old_next; new_next->used = false;
self->next->used = false; new_next->size = remaining;
self->next->size = remaining; mem_copy(new_next->padding, BLOCK_PADDING, 7);
mem_copy(self->next->padding, BLOCK_PADDING, 7); self->next = new_next;
} }
return (self); return (self);
} }
@ -85,6 +104,7 @@ t_mblock *get_block_for_size(t_usize size)
last = NULL; last = NULL;
cur = get_head_arena()->first; cur = get_head_arena()->first;
printf("cur == %p\n", cur);
while (cur) while (cur)
{ {
if (cur->page == NULL) if (cur->page == NULL)
@ -96,13 +116,46 @@ t_mblock *get_block_for_size(t_usize size)
} }
if (last == NULL) if (last == NULL)
return (NULL); return (NULL);
last->page->next = alloc_page(size + sizeof(t_mblock)); last->page->next = alloc_page(size);
if (last->page->next == NULL) if (last->page->next == NULL)
me_abort("Failed to alloc page!"); me_abort("Failed to alloc page!");
last->next = last->page->next->first; last->next = last->page->next->first;
return (split_block(last->page->next->first, size)); 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) void print_pages_info(void)
{ {
t_mpage *page; t_mpage *page;
@ -120,3 +173,20 @@ void print_pages_info(void)
page = page->next; 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);
}

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 13:08:52 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) void me_exit(t_i32 exit_code)
{ {
t_mpage *page; t_mpage *page;
t_mpage *tmp; void *tmp;
t_mblock *block; t_mblock *block;
t_usize count_block; t_usize count_block;
page = get_head_arena(); page = get_head_arena();
count_block = 0; count_block = 0;
block = page->first;
while (block)
{
if (block->used)
count_block += 1;
tmp = block->next;
__libc_free(block);
block = tmp;
}
while (page) while (page)
{ {
block = page->first;
while (block)
{
if (block->used)
count_block += 1;
block = block->next;
}
tmp = page->next; tmp = page->next;
__libc_free(page->data);
__libc_free(page); __libc_free(page);
page = tmp; page = tmp;
} }