centralized malloc system
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
#include "cube3d.h"
|
||||
|
||||
int shut_down(t_game *game)
|
||||
int shut_down(t_game *game)
|
||||
{
|
||||
mlx_destroy_window(game->mlx_ptr, game->win_ptr);
|
||||
gexit(B_RED"close windows"RESET"\n");
|
||||
exit(0);
|
||||
free(game);
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
// temp, to map all the keys on linux and mac
|
||||
static int print_keycode(int keycode)
|
||||
{
|
||||
ft_putnbrendl(keycode);
|
||||
ft_putnbr_fd(keycode, 1);
|
||||
ft_putchar_fd('\n', 1);
|
||||
return(0);
|
||||
}
|
||||
// temp end
|
||||
|
||||
@@ -4,7 +4,7 @@ t_game *init_game(void)
|
||||
{
|
||||
t_game *game;
|
||||
|
||||
game = malloc(sizeof(t_game));
|
||||
game = gmalloc(sizeof(t_game));
|
||||
// player first position
|
||||
game->plr_x = 16;
|
||||
game->plr_y = 16;
|
||||
|
||||
49
srcs/mem/free_exit.c
Normal file
49
srcs/mem/free_exit.c
Normal file
@@ -0,0 +1,49 @@
|
||||
#include "cube3d.h"
|
||||
|
||||
static int comp_addr(void *to_find, void *to_compare)
|
||||
{
|
||||
return (to_find == to_compare);
|
||||
}
|
||||
|
||||
t_list **alloc_lst()
|
||||
{
|
||||
static t_list *lst = NULL;
|
||||
|
||||
return (&lst);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
lst = alloc_lst();
|
||||
ft_putstr_fd(str, 2);
|
||||
ft_lstfree((*lst), free);
|
||||
exit(0);
|
||||
}
|
||||
Reference in New Issue
Block a user