improve gexit with init function and add basic mlx image gestion
This commit is contained in:
94
srcs/mem/g_alloc_free_exit.c
Normal file
94
srcs/mem/g_alloc_free_exit.c
Normal file
@@ -0,0 +1,94 @@
|
||||
#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);
|
||||
}
|
||||
Reference in New Issue
Block a user