Files
42_INT_10_cube3d/srcs/mem/memorybook.c
2022-03-30 13:48:58 +02:00

64 lines
1.2 KiB
C

#include "cube3d.h"
t_exit **mb_exit_func();
t_list **mb_lst();
int mb_comp_addr(void *to_find, void *to_compare);
void mb_init(void(*f)(void*), void *param)
{
t_exit **texit;
texit = mb_exit_func();
(*texit)->param = param;
(*texit)->f = f;
}
void *mb_alloc(size_t size)
{
void *tmp;
t_list **lst;
lst = mb_lst();
tmp = ft_memalloc(size);
if (!tmp)
mb_exit(B_RED"failed create new allocation"RESET"\n");
if (!ft_lstpush_back(lst, ft_lstcreate(tmp)))
mb_exit(B_RED"failed add new element to list"RESET"\n");
return (tmp);
}
void mb_add(void *addr)
{
t_list **lst;
lst = mb_lst();
if (!ft_lstpush_back(lst, ft_lstcreate(addr)))
mb_exit(B_RED"failed add new element to list"RESET"\n");
}
void mb_free(void *addr)
{
t_list **lst;
t_list *tmp;
lst = mb_lst();
tmp = ft_lstfind((*lst), addr, mb_comp_addr);
if (!tmp)
ft_putstr_fd(B_RED"element to free doesn't exist (maybe it was already freed)"RESET"\n", 2);
ft_lsterase(tmp, free);
}
void mb_exit(char *str)
{
t_list **lst;
t_exit **texit;
lst = mb_lst();
texit = mb_exit_func();
if ((*texit)->f)
(*texit)->f((*texit)->param);
ft_putstr_fd(str, 2);
ft_lstfree((*lst), free);
exit(0);
}