I want to kill sheeps in minecraft with a sword

This commit is contained in:
Maieul BOYER 2024-05-12 17:14:25 +02:00
parent 4f1768b9e8
commit a779ccb768
No known key found for this signature in database
17 changed files with 488 additions and 268 deletions

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
// }

View file

@ -0,0 +1,140 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* alloc_dumb.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <stdlib.h>
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 <stdio.h>
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;
}
}

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
// }

View file

@ -6,45 +6,86 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <stdlib.h>
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);
}