memorybook functionnal, with add
This commit is contained in:
64
srcs/mem/memorybook.c
Normal file
64
srcs/mem/memorybook.c
Normal file
@@ -0,0 +1,64 @@
|
||||
#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"failed free element"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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user