memorybook functionnal, with add
This commit is contained in:
@@ -1,94 +0,0 @@
|
||||
#include "cube3d.h"
|
||||
|
||||
/*
|
||||
* gexit_init
|
||||
*
|
||||
*/
|
||||
|
||||
//-------------------------------
|
||||
// gexit function
|
||||
//-------------------------------
|
||||
typedef void(*func_to_gexit)(void*);
|
||||
|
||||
typedef struct s_exit
|
||||
{
|
||||
func_to_gexit f;
|
||||
void *param;
|
||||
} t_exit;
|
||||
|
||||
|
||||
t_exit **exit_func()
|
||||
{
|
||||
static t_exit *texit = NULL;
|
||||
|
||||
if (!texit)
|
||||
texit = gmalloc(sizeof(t_exit));
|
||||
return (&texit);
|
||||
}
|
||||
|
||||
void gexit_init(void(*f)(void*), void *param)
|
||||
{
|
||||
t_exit **texit;
|
||||
|
||||
texit = exit_func();
|
||||
(*texit)->param = param;
|
||||
(*texit)->f = f;
|
||||
}
|
||||
|
||||
//-------------------------------
|
||||
// allocation list
|
||||
//-------------------------------
|
||||
t_list **alloc_lst()
|
||||
{
|
||||
static t_list *lst = NULL;
|
||||
|
||||
return (&lst);
|
||||
}
|
||||
|
||||
//-------------------------------
|
||||
// alloc, free, exit functions
|
||||
//-------------------------------
|
||||
void *gmalloc(size_t size)
|
||||
{
|
||||
void *tmp;
|
||||
t_list **lst;
|
||||
|
||||
lst = alloc_lst();
|
||||
tmp = ft_memalloc(size);
|
||||
if (!tmp)
|
||||
gexit(B_RED"failed allocation element"RESET"\n");
|
||||
if (!ft_lstpush_back(lst, ft_lstcreate(tmp)))
|
||||
gexit(B_RED"failed allocation list of alocated address"RESET"\n");
|
||||
return (tmp);
|
||||
}
|
||||
|
||||
static int comp_addr(void *to_find, void *to_compare)
|
||||
{
|
||||
return (to_find == to_compare);
|
||||
}
|
||||
|
||||
void gfree(void *addr)
|
||||
{
|
||||
t_list **lst;
|
||||
t_list *tmp;
|
||||
|
||||
lst = alloc_lst();
|
||||
tmp = ft_lstfind((*lst), addr, comp_addr);
|
||||
if (!tmp)
|
||||
ft_putstr_fd(B_RED"failed free element"RESET"\n", 2);
|
||||
ft_lsterase(tmp, free);
|
||||
}
|
||||
|
||||
void gexit(char *str)
|
||||
{
|
||||
t_list **lst;
|
||||
t_exit **texit;
|
||||
|
||||
lst = alloc_lst();
|
||||
texit = exit_func();
|
||||
if ((*texit)->f)
|
||||
(*texit)->f((*texit)->param);
|
||||
ft_putstr_fd(str, 2);
|
||||
ft_lstfree((*lst), free);
|
||||
exit(0);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
23
srcs/mem/memorybook_utils.c
Normal file
23
srcs/mem/memorybook_utils.c
Normal file
@@ -0,0 +1,23 @@
|
||||
#include "cube3d.h"
|
||||
|
||||
t_exit **mb_exit_func()
|
||||
{
|
||||
static t_exit *texit = NULL;
|
||||
|
||||
if (!texit)
|
||||
texit = mb_alloc(sizeof(t_exit));
|
||||
return (&texit);
|
||||
}
|
||||
|
||||
t_list **mb_lst()
|
||||
{
|
||||
static t_list *lst = NULL;
|
||||
|
||||
return (&lst);
|
||||
}
|
||||
|
||||
int mb_comp_addr(void *to_find, void *to_compare)
|
||||
{
|
||||
return (to_find == to_compare);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user