Skip to content

Commit

Permalink
Added gc files, updated Makefile to reflect changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jteissie committed Aug 24, 2024
1 parent f144e04 commit 6fd2f92
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 1 deletion.
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
48 changes: 48 additions & 0 deletions includes/garbage_collector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* garbage_collector.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marvin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <pthread.h>
# include <stdlib.h>
# include <unistd.h>
# include <string.h>
# 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
59 changes: 59 additions & 0 deletions srcs/garbage_collector/add_del.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* garbage_collector.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marvin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}
43 changes: 43 additions & 0 deletions srcs/garbage_collector/gc_handler.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main_test.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marvin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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));
}
78 changes: 78 additions & 0 deletions srcs/garbage_collector/gc_routine.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* del.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marvin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

0 comments on commit 6fd2f92

Please sign in to comment.