From f645da78853af8349d98721e6de20547b1dbbfd3 Mon Sep 17 00:00:00 2001 From: Hugo LAMY Date: Fri, 25 Mar 2022 21:27:24 +0100 Subject: [PATCH] memorybook functionnal, with add --- Makefile | 4 +- headers/cube3d.h | 1 + headers/cube3d_proto.h | 10 ---- headers/memorybook.h | 18 +++++++ srcs/cube3d.c | 6 +-- srcs/init/init_struct.c | 3 +- srcs/mem/g_alloc_free_exit.c | 94 ------------------------------------ srcs/mem/memorybook.c | 64 ++++++++++++++++++++++++ srcs/mem/memorybook_utils.c | 23 +++++++++ 9 files changed, 113 insertions(+), 110 deletions(-) create mode 100644 headers/memorybook.h delete mode 100644 srcs/mem/g_alloc_free_exit.c create mode 100644 srcs/mem/memorybook.c create mode 100644 srcs/mem/memorybook_utils.c diff --git a/Makefile b/Makefile index 6d22401..51b7e6f 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,8 @@ D_SRCS = srcs \ SRCS = cube3d.c \ \ - g_alloc_free_exit.c \ + memorybook.c \ + memorybook_utils.c \ \ init_struct.c \ init_parsing.c \ @@ -36,6 +37,7 @@ HEADERS = cube3d.h \ cube3d_proto.h \ cube3d_macro.h \ cube3d_struct.h \ + memorybook.h \ colors.h INCLUDES = -I$(D_HEADERS) -I$(D_LFT) -I$(D_LMLX) diff --git a/headers/cube3d.h b/headers/cube3d.h index a321709..330dd20 100644 --- a/headers/cube3d.h +++ b/headers/cube3d.h @@ -8,6 +8,7 @@ # include // for printf() # include "colors.h" +# include "memorybook.h" # include "cube3d_macro.h" # include "cube3d_struct.h" diff --git a/headers/cube3d_proto.h b/headers/cube3d_proto.h index 62a8257..8cc6fd2 100644 --- a/headers/cube3d_proto.h +++ b/headers/cube3d_proto.h @@ -45,14 +45,4 @@ int is_go_backward(int *k_hook); // draw.c void draw(t_game *game); -// ------------------------------- -// SRC/MEM -// ------------------------------- -// g_alloc_free_exit.c -void gexit_init(void(*f)(void*), void *param); -void *gmalloc(size_t size); -void gfree(void *addr); -void gexit(char *str); - - #endif diff --git a/headers/memorybook.h b/headers/memorybook.h new file mode 100644 index 0000000..19c4c73 --- /dev/null +++ b/headers/memorybook.h @@ -0,0 +1,18 @@ +#ifndef MEMORYBOOK_H +# define MEMORYBOOK_H + +typedef void(*t_mb_func_exit)(void*); + +typedef struct s_exit +{ + t_mb_func_exit f; + void *param; +} t_exit; + +void mb_init(void(*f)(void*), void *param); +void *mb_alloc(size_t size); +void mb_add(void *addr); +void mb_free(void *addr); +void mb_exit(char *str); + +#endif diff --git a/srcs/cube3d.c b/srcs/cube3d.c index 63baebd..8940f24 100644 --- a/srcs/cube3d.c +++ b/srcs/cube3d.c @@ -8,12 +8,11 @@ void destroy_mlx(void *param) mlx_destroy_image(game->mlx_ptr, game->img_ptr); mlx_destroy_window(game->mlx_ptr, game->win_ptr); mlx_destroy_display(game->mlx_ptr); - free(game->mlx_ptr); } int shut_down() { - gexit(B_RED"close windows"RESET"\n"); + mb_exit(B_RED"close windows"RESET"\n"); return (0); } @@ -23,7 +22,7 @@ int main(int ac, char **av) game = init_game(); init_parsing(ac, av, game); - gexit_init(destroy_mlx, game); + mb_init(destroy_mlx, game); // receive a keypress event mlx_hook(game->win_ptr, 2, 1L << 0, keypress, game); @@ -35,4 +34,3 @@ int main(int ac, char **av) mlx_loop(game->mlx_ptr); return (0); } - diff --git a/srcs/init/init_struct.c b/srcs/init/init_struct.c index 2dc994e..726f781 100644 --- a/srcs/init/init_struct.c +++ b/srcs/init/init_struct.c @@ -4,7 +4,7 @@ t_game *init_game(void) { t_game *game; - game = gmalloc(sizeof(t_game)); + game = mb_alloc(sizeof(t_game)); // player first position game->plr_x = 16; game->plr_y = 16; @@ -13,6 +13,7 @@ t_game *init_game(void) game->win_size_y = 500; // init connexion to server game->mlx_ptr = mlx_init(); + mb_add(game->mlx_ptr); // create the window game->win_ptr = mlx_new_window(game->mlx_ptr, game->win_size_x, game->win_size_y, "test"); diff --git a/srcs/mem/g_alloc_free_exit.c b/srcs/mem/g_alloc_free_exit.c deleted file mode 100644 index 4be64ea..0000000 --- a/srcs/mem/g_alloc_free_exit.c +++ /dev/null @@ -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); -} diff --git a/srcs/mem/memorybook.c b/srcs/mem/memorybook.c new file mode 100644 index 0000000..31c8b48 --- /dev/null +++ b/srcs/mem/memorybook.c @@ -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); +} + diff --git a/srcs/mem/memorybook_utils.c b/srcs/mem/memorybook_utils.c new file mode 100644 index 0000000..3ebab5d --- /dev/null +++ b/srcs/mem/memorybook_utils.c @@ -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); +} +