Put the custom allocator in its own lib, as to lessen the difficulty to switch between libc's allocator and a custom one (#7)

This commit is contained in:
Maix0 2024-05-14 18:56:53 +02:00 committed by GitHub
parent 713f0f0302
commit cb7f3c3fdf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
85 changed files with 1121 additions and 877 deletions

View file

@ -6,86 +6,16 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 13:08:52 by maiboyer #+# #+# */
/* Updated: 2024/05/12 17:05:27 by maiboyer ### ########.fr */
/* Updated: 2024/05/14 18:42:41 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/mem/_allocator.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)
{
uninit_allocator();
uninit_global_allocator();
exit(exit_code);
}