From 6fd2f928a04afd1f636b378b968982e1b148146f Mon Sep 17 00:00:00 2001 From: jteissie Date: Sat, 24 Aug 2024 17:37:17 +0200 Subject: [PATCH] Added gc files, updated Makefile to reflect changes --- Makefile | 8 ++- includes/garbage_collector.h | 48 ++++++++++++++++++ srcs/garbage_collector/add_del.c | 59 ++++++++++++++++++++++ srcs/garbage_collector/gc_handler.c | 43 ++++++++++++++++ srcs/garbage_collector/gc_routine.c | 78 +++++++++++++++++++++++++++++ 5 files changed, 235 insertions(+), 1 deletion(-) create mode 100644 includes/garbage_collector.h create mode 100644 srcs/garbage_collector/add_del.c create mode 100644 srcs/garbage_collector/gc_handler.c create mode 100644 srcs/garbage_collector/gc_routine.c diff --git a/Makefile b/Makefile index 0c5d9ff..a952879 100644 --- a/Makefile +++ b/Makefile @@ -20,15 +20,21 @@ SRCS_MAP = map.c SRCS_RC = raycasting.c +SRCS_GC = gc_handler.c \ + add_del.c \ + gc_routine.c + PATH_M = srcs/ PATH_PS = srcs/parser/ PATH_MAP = srcs/map/ PATH_RC = srcs/raycasting/ +PATH_GC = srcs/garbage_collector/ SRCS = $(addprefix $(PATH_M), $(SRCS_M)) \ $(addprefix $(PATH_PS), $(SRCS_PS)) \ $(addprefix $(PATH_MAP), $(SRCS_MAP)) \ - $(addprefix $(PATH_RC), $(SRCS_RC)) + $(addprefix $(PATH_RC), $(SRCS_RC)) \ + $(addprefix $(PATH_GC), $(SRCS_GC)) HEADERS = cub3d.h diff --git a/includes/garbage_collector.h b/includes/garbage_collector.h new file mode 100644 index 0000000..cf7750a --- /dev/null +++ b/includes/garbage_collector.h @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* garbage_collector.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: marvin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/24 13:29:50 by marvin #+# #+# */ +/* Updated: 2024/08/24 17:25:59 by marvin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef GARBAGE_COLLECTOR_H +# define GARBAGE_COLLECTOR_H +# include +# include +# include +# include +# define MAX_GARBAGE 100 +# define STOP 0 +# define RUN 1 +# define SECOND 1000000 + +typedef enum e_type +{ + NULL_TYPE, + POINTER, + ARRAY, + IMAGE, + WINDOW, + MLX, +} t_type; + +typedef struct s_gc_container +{ + void *garbage[MAX_GARBAGE]; + t_type garbage_type[MAX_GARBAGE]; + size_t garbage_size[MAX_GARBAGE]; + size_t last_pos; + int run_status; + pthread_mutex_t garbage_lock; +} t_gc_container; + +int gc_init(t_gc_container *gc, pthread_t *gc_thread); +void gc_destroy(t_gc_container *gc, pthread_t *gc_thread); +int add_del(void *ptr, t_type type, size_t size, t_gc_container *container); +void *gc_routine(void *arg); +#endif diff --git a/srcs/garbage_collector/add_del.c b/srcs/garbage_collector/add_del.c new file mode 100644 index 0000000..8f17d7c --- /dev/null +++ b/srcs/garbage_collector/add_del.c @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* garbage_collector.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: marvin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/24 13:29:45 by marvin #+# #+# */ +/* Updated: 2024/08/24 13:29:45 by marvin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "garbage_collector.h" + +static int wait_for_free_slot(t_gc_container *container) +{ + size_t cycles; + + cycles = 0; + while (cycles < 15) + { + usleep(1 * SECOND); + pthread_mutex_lock(&container->garbage_lock); + if (container->last_pos != MAX_GARBAGE - 1) + { + pthread_mutex_unlock(&container->garbage_lock); + return (0); + } + else + { + cycles++; + pthread_mutex_unlock(&container->garbage_lock); + } + } + write(STDERR_FILENO, "Error: GC: Wait for free slot timeout\n", 39); + return (1); +} + +int add_del(void *ptr, t_type type, size_t size, t_gc_container *container) +{ + size_t insert_pos; + + pthread_mutex_lock(&container->garbage_lock); + insert_pos = container->last_pos; + if (insert_pos == MAX_GARBAGE - 1) + { + pthread_mutex_unlock(&container->garbage_lock); + if (wait_for_free_slot(container) == 0) + return (1); + pthread_mutex_lock(&container->garbage_lock); + insert_pos = container->last_pos; + } + container->garbage[insert_pos] = ptr; + container->garbage_type[insert_pos] = type; + container->garbage_size[insert_pos] = size; + container->last_pos++; + pthread_mutex_unlock(&container->garbage_lock); + return (0); +} diff --git a/srcs/garbage_collector/gc_handler.c b/srcs/garbage_collector/gc_handler.c new file mode 100644 index 0000000..e65eb09 --- /dev/null +++ b/srcs/garbage_collector/gc_handler.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main_test.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: marvin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/24 17:23:22 by marvin #+# #+# */ +/* Updated: 2024/08/24 17:23:22 by marvin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "garbage_collector.h" + +int gc_init(t_gc_container *gc, pthread_t *gc_thread) +{ + memset(gc->garbage, 0, MAX_GARBAGE * sizeof(void *)); + memset(gc->garbage_type, 0, MAX_GARBAGE * sizeof(t_type)); + memset(gc->garbage_size, 0, MAX_GARBAGE * sizeof(size_t)); + gc->last_pos = 0; + gc->run_status = RUN; + if (pthread_mutex_init(&gc->garbage_lock, NULL) != 0) + return (1); + if (pthread_create(gc_thread, NULL, gc_routine, gc) != 0) + { + pthread_mutex_destroy(&gc->garbage_lock); + return (1); + } + return (0); +} + +void gc_destroy(t_gc_container *gc, pthread_t *gc_thread) +{ + pthread_mutex_lock(&gc->garbage_lock); + gc->run_status = STOP; + pthread_mutex_unlock(&gc->garbage_lock); + pthread_join(*gc_thread, NULL); + gc_thread = NULL; + pthread_mutex_destroy(&gc->garbage_lock); + memset(gc->garbage, 0, MAX_GARBAGE * sizeof(void *)); + memset(gc->garbage_type, 0, MAX_GARBAGE * sizeof(t_type)); + memset(gc->garbage_size, 0, MAX_GARBAGE * sizeof(size_t)); +} diff --git a/srcs/garbage_collector/gc_routine.c b/srcs/garbage_collector/gc_routine.c new file mode 100644 index 0000000..08483f3 --- /dev/null +++ b/srcs/garbage_collector/gc_routine.c @@ -0,0 +1,78 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* del.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: marvin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/24 15:18:38 by marvin #+# #+# */ +/* Updated: 2024/08/24 15:18:38 by marvin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "garbage_collector.h" + +static void free_array(void **ptr, size_t size) +{ + size_t index; + + index = 0; + while (index < size) + { + free(ptr[index]); + index++; + } + free(ptr); +} + +static void del(void *ptr, size_t size, t_type type) +{ + if (type == POINTER) + free(ptr); + else if (type == ARRAY) + free_array((void **)ptr, size); +} + +static void sweep(t_gc_container *container) +{ + void *ptr; + size_t size; + size_t position; + t_type type; + + position = container->last_pos; + if (position == 0) + return ; + while (container->last_pos > 0) + { + position = container->last_pos - 1; + ptr = container->garbage[position]; + size = container->garbage_size[position]; + type = container->garbage_type[position]; + del(ptr, size, type); + container->garbage[position] = NULL; + container->garbage_size[position] = 0; + container->garbage_type[position] = NULL_TYPE; + container->last_pos--; + } +} + +void *gc_routine(void *arg) +{ + t_gc_container *container; + + container = (t_gc_container *)arg; + while (1) + { + pthread_mutex_lock(&container->garbage_lock); + sweep(container); + if (container->run_status == STOP) + { + pthread_mutex_unlock(&container->garbage_lock); + break ; + } + pthread_mutex_unlock(&container->garbage_lock); + usleep(5 * SECOND); + } + return (NULL); +}