-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added gc files, updated Makefile to reflect changes
- Loading branch information
jteissie
committed
Aug 24, 2024
1 parent
f144e04
commit 6fd2f92
Showing
5 changed files
with
235 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |