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,13 +6,12 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 11:08:03 by maiboyer #+# #+# */
/* Updated: 2024/05/09 07:43:10 by maiboyer ### ########.fr */
/* Updated: 2024/05/14 18:42:15 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#define _GNU_SOURCE
#include "me/alloc/alloc_internal.h"
#include "me/fs/putendl_fd.h"
#include "me/fs/putstr_fd.h"
#include "me/types.h"
@ -91,7 +90,6 @@ void me_abort(t_str msg)
if (msg == NULL)
msg = "No message (msg was NULL)";
me_putendl_fd("Memory information:", 2);
print_pages_info();
me_putstr_fd("Abort: ", 2);
me_putendl_fd(msg, 2);
print_trace();

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);
}

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/03 16:22:41 by maiboyer #+# #+# */
/* Updated: 2024/05/09 16:55:16 by rparodi ### ########.fr */
/* Updated: 2024/05/14 18:42:59 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,6 +15,7 @@
#include "me/os/process.h"
#include "me/string/str_find_chr.h"
#include "me/string/str_n_compare.h"
#include "me/mem/mem.h"
#include "me/string/str_split.h"
#include "me/types.h"
#include "me/vec/vec_str.h"
@ -73,8 +74,8 @@ t_error in_path(t_spawn_info *info, t_process *process, t_const_str path,
}
sp_index = 0;
while (splitted_path[sp_index])
me_free(splitted_path[sp_index++]);
me_free(splitted_path);
mem_free(splitted_path[sp_index++]);
mem_free(splitted_path);
return (NO_ERROR);
}
@ -99,7 +100,7 @@ t_error find_binary(t_spawn_info *info, t_process *process)
}
if (access(s.buf, X_OK | R_OK) == 0)
{
me_free(info->binary_path);
mem_free(info->binary_path);
info->binary_path = s.buf;
return (NO_ERROR);
}
@ -119,7 +120,7 @@ static void cleanup(t_spawn_info info, t_process *process, bool cleanup_process)
close(info.stderr.vals.fd.value);
vec_str_free(info.arguments);
vec_str_free(info.environement);
me_free(info.binary_path);
mem_free(info.binary_path);
}
t_error spawn_process(t_spawn_info info, t_process *process)