centralized malloc system
This commit is contained in:
9
Makefile
9
Makefile
@@ -14,7 +14,7 @@ D_SRCS = srcs \
|
||||
srcs/init \
|
||||
srcs/hook \
|
||||
srcs/draw \
|
||||
srcs/free
|
||||
srcs/mem
|
||||
|
||||
SRCS = cube3d.c \
|
||||
\
|
||||
@@ -23,14 +23,17 @@ SRCS = cube3d.c \
|
||||
\
|
||||
keyhook.c \
|
||||
key_do_action.c \
|
||||
key_is_action_1.c
|
||||
key_is_action_1.c \
|
||||
\
|
||||
free_exit.c
|
||||
|
||||
# headers
|
||||
D_HEADERS = headers
|
||||
HEADERS = cube3d.h \
|
||||
cube3d_proto.h \
|
||||
cube3d_macro.h \
|
||||
cube3d_struct.h
|
||||
cube3d_struct.h \
|
||||
colors.h
|
||||
|
||||
INCLUDES = -I$(D_HEADERS) -I$(D_LFT) -I$(D_LMLX)
|
||||
|
||||
|
||||
25
headers/colors.h
Normal file
25
headers/colors.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef COLORS_H
|
||||
# define COLORS_H
|
||||
|
||||
# define GRAY "\e[0;30m"
|
||||
# define RED "\e[0;31m"
|
||||
# define GREEN "\e[0;32m"
|
||||
# define YELLOW "\e[0;33m"
|
||||
# define BLUE "\e[0;34m"
|
||||
# define PURPLE "\e[0;35m"
|
||||
# define CYAN "\e[0;36m"
|
||||
# define WHITE "\e[0;37m"
|
||||
|
||||
# define B_GRAY "\e[1;30m"
|
||||
# define B_RED "\e[1;31m"
|
||||
# define B_GREEN "\e[1;32m"
|
||||
# define B_YELLOW "\e[1;33m"
|
||||
# define B_BLUE "\e[1;34m"
|
||||
# define B_PURPLE "\e[1;35m"
|
||||
# define B_CYAN "\e[1;36m"
|
||||
# define B_WHITE "\e[1;37m"
|
||||
|
||||
# define RESET "\e[0m"
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
#ifndef CUBE3D_H
|
||||
# define CUBE3D_H
|
||||
|
||||
#include "../libs/libft/includes/libft.h"
|
||||
#include <mlx.h>
|
||||
#include <unistd.h> // for sleep()
|
||||
#include <stdlib.h> // for atoi()
|
||||
#include <stdio.h> // for printf()
|
||||
# include "../libs/libft/includes/libft.h"
|
||||
# include <mlx.h>
|
||||
# include <unistd.h> // for sleep()
|
||||
# include <stdlib.h> // for atoi()
|
||||
# include <stdio.h> // for printf()
|
||||
|
||||
#include "cube3d_macro.h"
|
||||
#include "cube3d_struct.h"
|
||||
#include "cube3d_proto.h"
|
||||
# include "colors.h"
|
||||
|
||||
# include "cube3d_macro.h"
|
||||
# include "cube3d_struct.h"
|
||||
# include "cube3d_proto.h"
|
||||
|
||||
#endif
|
||||
|
||||
@@ -44,6 +44,9 @@ int is_go_backward(int *k_hook);
|
||||
|
||||
// -------------------------------
|
||||
// SRC/FREE
|
||||
void *gmalloc(size_t size);
|
||||
void gfree(void *addr);
|
||||
void gexit(char *str);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
Submodule libs/libft updated: 39b3f2bc4e...2767ff165f
@@ -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);
|
||||
}
|
||||
162
test_lst.c
Normal file
162
test_lst.c
Normal file
@@ -0,0 +1,162 @@
|
||||
#include "libs/libft/includes/libft.h"
|
||||
#include <stdio.h>
|
||||
|
||||
void lst_print(void *element)
|
||||
{
|
||||
printf("lst->element : %s - %p\n",(char *)element, element);
|
||||
}
|
||||
|
||||
void lst_delete(void *element)
|
||||
{
|
||||
free(element);
|
||||
}
|
||||
|
||||
int lst_comp_ptr(void *to_find, void *to_compare)
|
||||
{
|
||||
return (to_find == to_compare);
|
||||
}
|
||||
|
||||
int lst_comp(void *to_find, void *to_compare)
|
||||
{
|
||||
return (!ft_strcmp((char *)to_find, (char *)to_compare));
|
||||
}
|
||||
|
||||
void *lst_copy(void *to_copy)
|
||||
{
|
||||
char *copy;
|
||||
|
||||
copy = ft_memalloc(sizeof(char) * (ft_strlen(to_copy) + 1));
|
||||
copy = ft_strcpy(copy, to_copy);
|
||||
return (copy);
|
||||
}
|
||||
|
||||
//
|
||||
// ft_lstnew
|
||||
// ft_lstadd_back
|
||||
// ft_lstadd_front
|
||||
// ft_lstclear
|
||||
// ft_lstdelone
|
||||
// ft_lstiter
|
||||
// ft_lstlast
|
||||
// ft_lstmap
|
||||
// ft_lstsize
|
||||
//
|
||||
// ft_lstfind
|
||||
// ft_lstremove
|
||||
// ft_lstremove_next
|
||||
//
|
||||
// ft_lstcreate <--
|
||||
// ft_lstpush_back <--
|
||||
// ft_lstpush_front <--
|
||||
// ft_lstloop <--
|
||||
// ft_lstloop_back <--
|
||||
// ft_lstbegin <--
|
||||
// ft_lstend <--
|
||||
// ft_lstfind <--
|
||||
// ft_lstinsert <--
|
||||
// ft_lsterase <--
|
||||
// ft_lstfree <--
|
||||
// ft_lstlen <--
|
||||
// ft_lstcopy <--
|
||||
//
|
||||
|
||||
int main(void)
|
||||
{
|
||||
t_list *toto = NULL;
|
||||
t_list *tmp;
|
||||
char *str;
|
||||
char *str2;
|
||||
int c = 'f';
|
||||
|
||||
///////////////////////////////////////////////////////////
|
||||
printf("\npush back ------------\n");
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
str = ft_memalloc(2 * sizeof(char));
|
||||
str[1] = '\0';
|
||||
str[0] = c++;
|
||||
ft_lstpush_back(&toto, ft_lstcreate(str));
|
||||
}
|
||||
printf("print forward --------\n");
|
||||
ft_lstloop(toto, lst_print);
|
||||
printf("print backward -------\n");
|
||||
ft_lstloop_back(ft_lstend(toto), lst_print);
|
||||
printf("len ------------------\n");
|
||||
printf("%i\n", ft_lstlen(toto));
|
||||
|
||||
///////////////////////////////////////////////////////////
|
||||
printf("\npush front -----------\n");
|
||||
c = 'e';
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
str = ft_memalloc(2 * sizeof(char));
|
||||
str[1] = '\0';
|
||||
str[0] = c--;
|
||||
ft_lstpush_front(&toto, ft_lstcreate(str));
|
||||
}
|
||||
printf("print forward --------\n");
|
||||
ft_lstloop(toto, lst_print);
|
||||
printf("print backward -------\n");
|
||||
ft_lstloop_back(ft_lstend(toto), lst_print);
|
||||
printf("len ------------------\n");
|
||||
printf("%i\n", ft_lstlen(toto));
|
||||
|
||||
///////////////////////////////////////////////////////////
|
||||
printf("\nfind -----------------\n");
|
||||
tmp = ft_lstfind(toto, "e", lst_comp);
|
||||
if (tmp)
|
||||
lst_print(tmp->content);
|
||||
|
||||
///////////////////////////////////////////////////////////
|
||||
printf("\ninsert ---------------\n");
|
||||
str = ft_memalloc(2 * sizeof(char));
|
||||
str[1] = '\0';
|
||||
str[0] = 'E';
|
||||
ft_lstinsert(tmp, ft_lstcreate(str));
|
||||
printf("print from insert ----\n");
|
||||
ft_lstloop(tmp, lst_print);
|
||||
printf("print from begin -----\n");
|
||||
ft_lstloop(toto, lst_print);
|
||||
printf("print backward -------\n");
|
||||
ft_lstloop_back(ft_lstend(toto), lst_print);
|
||||
printf("len ------------------\n");
|
||||
printf("%i\n", ft_lstlen(toto));
|
||||
|
||||
///////////////////////////////////////////////////////////
|
||||
printf("\nerase ----------------\n");
|
||||
ft_lsterase(ft_lstfind(toto, "E", lst_comp), lst_delete);
|
||||
printf("print forward --------\n");
|
||||
ft_lstloop(toto, lst_print);
|
||||
printf("print backward -------\n");
|
||||
ft_lstloop_back(ft_lstend(toto), lst_print);
|
||||
printf("len ------------------\n");
|
||||
printf("%i\n", ft_lstlen(toto));
|
||||
|
||||
///////////////////////////////////////////////////////////
|
||||
printf("\nfree -----------------\n");
|
||||
tmp = ft_lstfind(toto, "e", lst_comp);
|
||||
ft_lstfree(tmp, lst_delete);
|
||||
printf("print forward --------\n");
|
||||
ft_lstloop(toto, lst_print);
|
||||
printf("print backward -------\n");
|
||||
ft_lstloop_back(ft_lstend(toto), lst_print);
|
||||
printf("len ------------------\n");
|
||||
printf("%i\n", ft_lstlen(toto));
|
||||
|
||||
///////////////////////////////////////////////////////////
|
||||
printf("\ncopy -----------------\n");
|
||||
tmp = ft_lstcopy(toto, lst_copy);
|
||||
printf("print forward toto ---\n");
|
||||
ft_lstloop(toto, lst_print);
|
||||
printf("print forward tmp ----\n");
|
||||
ft_lstloop(tmp, lst_print);
|
||||
printf("len tmp --------------\n");
|
||||
printf("%i\n", ft_lstlen(tmp));
|
||||
ft_lstfree(tmp, lst_delete);
|
||||
|
||||
///////////////////////////////////////////////////////////
|
||||
ft_lstfree(toto, lst_delete);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user