-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f22e1e5
Showing
6 changed files
with
233 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.vscode/ | ||
.DS_Store |
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,32 @@ | ||
# Garbage Collector - README | ||
|
||
## Overview | ||
This project provides a simple garbage collection system in C using a linked list to manage dynamically allocated memory. The system ensures that all allocated memory is tracked and freed, preventing memory leaks. Additionally, a cleanup function (`main_cleanup`) is provided to free all memory and reset the garbage collector when the program terminates. | ||
|
||
## Features | ||
- **Linked List-Based Garbage Collection:** | ||
Memory blocks are tracked in a linked list, enabling efficient memory management. | ||
|
||
- **Automatic Memory Management:** | ||
Custom memory allocation function (`ft_malloc`) automatically registers memory blocks with the garbage collector. | ||
|
||
- **Batch Memory Deallocation:** | ||
All memory blocks tracked by the garbage collector are freed at once using `gc_free_all()`. | ||
|
||
- **Program Cleanup:** | ||
The `main_cleanup()` function frees all allocated memory, resets the garbage collector, and exits the program. | ||
|
||
## Files | ||
- **gb_garbage_collector.c**: Implements the core garbage collection system, including adding memory blocks to the collection list, creating nodes, and freeing all memory. | ||
- **gb_utils.c**: Provides utility functions for memory allocation, linked list operations, and printing the state of the garbage collector. | ||
- **main_cleanup()**: A cleanup function that frees all memory, resets the garbage collector, and terminates the program. | ||
|
||
## Usage | ||
|
||
### Garbage Collector Operations | ||
|
||
1. **Initialize Garbage Collector** | ||
Sets up an empty garbage collector to track dynamically allocated memory. | ||
|
||
```c | ||
t_garbage_collector *gc_init_garbage_collector(void); |
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,22 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* at_exit.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: iziane <iziane@student.42.fr> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/10/17 23:44:11 by iziane #+# #+# */ | ||
/* Updated: 2024/10/17 23:46:35 by iziane ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include <includes/garbage_collector/garbage_collector.h> | ||
|
||
noreturn void main_cleanup(uint8_t exit_stat) | ||
{ | ||
gc_free_all(); | ||
ft_bzero(get_gc(), sizeof(t_garbage_collector)); | ||
exit(exit_stat); | ||
} | ||
|
||
|
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,88 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* gb_garbage_collector.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: iziane <iziane@student.42.fr> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/10/17 23:49:05 by iziane #+# #+# */ | ||
/* Updated: 2024/10/17 23:49:08 by iziane ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include <garbage_collector.h> | ||
#include <main.h> | ||
|
||
void gc_free_all(void) | ||
{ | ||
int len; | ||
int i; | ||
t_gc_node *temp; | ||
t_gc_node *current; | ||
t_garbage_collector *gc; | ||
|
||
gc = get_gc(); | ||
current = gc->head; | ||
len = gc->size; | ||
i = -1; | ||
while (++i < len && current) | ||
{ | ||
temp = current->next; | ||
free(current->pointer); | ||
current->pointer = NULL; | ||
free(current); | ||
current = NULL; | ||
current = temp; | ||
} | ||
gc = NULL; | ||
} | ||
|
||
t_gc_node *gc_create_node(void *pointer2mem) | ||
{ | ||
t_gc_node *new_node; | ||
|
||
new_node = (t_gc_node *)malloc(sizeof(t_gc_node) * 1); | ||
if (!new_node) | ||
return (NULL); | ||
new_node->pointer = pointer2mem; | ||
return (new_node); | ||
} | ||
|
||
/* | ||
Function that will be used after Malloc | ||
*/ | ||
void gc_add_begin(void *pointer) | ||
{ | ||
t_gc_node *new_node; | ||
t_garbage_collector *gc; | ||
|
||
if (!pointer) | ||
return ; | ||
gc = get_gc(); | ||
new_node = gc_create_node(pointer); | ||
if (!new_node) | ||
ft_error("malloc fail", __FILE__, __LINE__, 1); | ||
if (gc->head == NULL) | ||
{ | ||
new_node->next = NULL; | ||
gc->head = new_node; | ||
gc->tail = new_node; | ||
} | ||
else | ||
{ | ||
new_node->next = (gc->head); | ||
gc->head = new_node; | ||
} | ||
gc->size++; | ||
} | ||
|
||
t_garbage_collector *gc_init_garbage_collector(void) | ||
{ | ||
t_garbage_collector *gc; | ||
|
||
gc = get_gc(); | ||
gc->head = NULL; | ||
gc->tail = NULL; | ||
gc->size = 0; | ||
return (gc); | ||
} |
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,42 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* gb_utils.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: iziane <iziane@student.42.fr> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/10/17 23:49:19 by iziane #+# #+# */ | ||
/* Updated: 2024/10/17 23:49:21 by iziane ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include <garbage_collector.h> | ||
|
||
// TODO: gc_overwrite(); | ||
|
||
t_garbage_collector *get_gc(void) | ||
{ | ||
static t_garbage_collector gc = {0}; | ||
|
||
return (&gc); | ||
} | ||
|
||
void gc_print_linked_list(t_garbage_collector *gc) | ||
{ | ||
if (!gc) | ||
return ; | ||
printf("Len of Linked List: %zu\n", gc->size); | ||
return ; | ||
} | ||
|
||
//TODO: | ||
void *ft_malloc(size_t len) | ||
{ | ||
void *ptr; | ||
|
||
ptr = malloc(len); | ||
if (!ptr) | ||
return (NULL); | ||
gc_add_begin(ptr); | ||
return (ptr); | ||
} |
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,47 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* garbage_collector.h :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: iziane <iziane@student.42.fr> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/10/09 22:34:21 by iziane #+# #+# */ | ||
/* Updated: 2024/10/17 23:50:38 by iziane ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#ifndef GARBAGE_COLLECTOR_H | ||
# define GARBAGE_COLLECTOR_H | ||
|
||
//Libs | ||
# include <stdlib.h> | ||
# include <stdio.h> | ||
# include <stdint.h> | ||
|
||
//BEGIN: Structs | ||
typedef struct s_gc_node | ||
{ | ||
void *pointer; | ||
struct s_gc_node *next; | ||
} t_gc_node; | ||
|
||
typedef struct s_garbage_collector | ||
{ | ||
t_gc_node *head; | ||
t_gc_node *tail; | ||
size_t size; | ||
} t_garbage_collector; | ||
//END: Structs | ||
|
||
//BEGIN: FNC-Prototyps | ||
t_gc_node *gc_create_node(void *pointer2mem); | ||
void gc_add_begin(void *pointer); | ||
t_garbage_collector *gc_init_garbage_collector(void); | ||
void gc_print_linked_list(t_garbage_collector *gc); | ||
void gc_free_all(void); | ||
t_garbage_collector *get_gc(void); | ||
// void *ft_malloc(size_t size); | ||
void *ft_malloc(size_t len); | ||
//END: FNC-Prototyps | ||
|
||
#endif |