diff --git a/memory-allocator/README.md b/memory-allocator/README.md deleted file mode 100644 index 757a2df..0000000 --- a/memory-allocator/README.md +++ /dev/null @@ -1,58 +0,0 @@ -## Memory Allocator - -Purpose of that repository is helping developers to manage their memories. It tracks your malloc functions and free your all allocated memory blocks automatically if one malloc fails. Use safe_malloc and safe_free to handle memory easily. - -> 42 students should feel free to use the library because it obey the Norminette rules. - -### Usage -```c -/* anywhere of your code */ -char *string = safe_malloc(sizeof(char) * 10); -if (!string) - return; -/* ... */ -/* another corner of your code */ -char *new_string = safe_malloc(sizeof(char) * 10); -if (!new_string) - return; /* there is no need to free old pointers. */ -``` - -### Handle aborting -Library allows you to register 2 handler function. Just give pointer -of your handler function. - -And of course, you don't have to register a function. It is optional. -```c -void abort_start_func() { - printf("I know my memory started to being aborted.\n"); -} - -void abort_stop_func() { - printf("I know all my malloc pointers freed now.\n"); - exit(1); -} - -int main() { - register_pre_abort_func(abort_start_func); - register_post_abort_func(abort_stop_func); - /* ... */ -} -``` - -If you exit in abort_stop function, you can assume -that your safe_malloc functions are non-null functions. -```c -/* anywhere of your code */ -char *string = safe_malloc(sizeof(char) * 10); -string[0] = 'a'; // it is safe! -``` - -### Testing - -#### Automatic Bruteforce Testing -1. Clone this repository. `git clone git@github.com:hamza-cskn/memory-allocator.git` -2. Run tester: `sh tester.sh` - -#### Manual Compiling for testing -1. Clone this repository. `git clone git@github.com:hamza-cskn/memory-allocator.git` -2. Set TESTER_MODE macro to 1 and compile: `gcc -DTESTER_MODE=1 allocator.c tester.c -o allocator` diff --git a/memory-allocator/aborter.c b/memory-allocator/aborter.c index ed1f109..6403415 100644 --- a/memory-allocator/aborter.c +++ b/memory-allocator/aborter.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* aborter.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: hcoskun +#+ +:+ +#+ */ +/* By: hamza +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/16 15:14:08 by hamza #+# #+# */ -/* Updated: 2023/12/17 13:46:15 by hcoskun ### ########.fr */ +/* Updated: 2024/03/25 15:35:05 by hamza ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ * @brief frees all memory blocks which allocated by using safe_malloc * function but does not call abort functions. */ -void free_memory(void) +void free_memory(void) { t_memory_block *memory_blocks; t_memory_block *next; @@ -26,8 +26,8 @@ void free_memory(void) while (memory_blocks != NULL) { next = memory_blocks->next; - FREE_MEMORY(memory_blocks->ptr); - FREE_MEMORY(memory_blocks); + free(memory_blocks->ptr); + free(memory_blocks); memory_blocks = next; } get_memory_blocks()->next = NULL; diff --git a/memory-allocator/allocator.c b/memory-allocator/allocator.c index d3c0d76..c370acb 100644 --- a/memory-allocator/allocator.c +++ b/memory-allocator/allocator.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* allocator.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: hcoskun +#+ +:+ +#+ */ +/* By: hamza +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/16 15:13:21 by hamza #+# #+# */ -/* Updated: 2023/12/17 15:22:52 by hcoskun ### ########.fr */ +/* Updated: 2024/03/25 15:35:20 by hamza ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,7 +44,7 @@ int append_memory_block(void *ptr) if (!ptr) return (BAD_EXIT); - new = ALLOCATE_MEMORY(sizeof(t_memory_block)); + new = malloc(sizeof(t_memory_block)); if (!new) return (BAD_EXIT); *new = (t_memory_block){.ptr = ptr, .next = NULL}; @@ -76,7 +76,7 @@ int remove_memory_block(void *ptr) if (cur->ptr == ptr) { prev->next = cur->next; - FREE_MEMORY(cur); + free(cur); return (GOOD_EXIT); } prev = cur; @@ -100,7 +100,7 @@ void *safe_malloc(int size) { void *ptr; - ptr = ALLOCATE_MEMORY(size); + ptr = malloc(size); if (!ptr) { abort_memory(); @@ -108,7 +108,7 @@ void *safe_malloc(int size) } if (!append_memory_block(ptr)) { - FREE_MEMORY(ptr); + free(ptr); abort_memory(); return (NULL); } @@ -122,7 +122,7 @@ void *safe_malloc(int size) void safe_free(void *ptr) { if (!ptr) - return; + return ; remove_memory_block(ptr); - FREE_MEMORY(ptr); + free(ptr); } diff --git a/memory-allocator/allocator.h b/memory-allocator/allocator.h index 9cd43f5..2f4f97a 100644 --- a/memory-allocator/allocator.h +++ b/memory-allocator/allocator.h @@ -1,36 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* allocator.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hamza +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/25 15:17:28 by hamza #+# #+# */ +/* Updated: 2024/03/25 15:20:02 by hamza ### ########.fr */ +/* */ +/* ************************************************************************** */ + #ifndef ALLOCATOR_H # define ALLOCATOR_H -# ifndef TESTER_MODE -# define TESTER_MODE 0 -# endif - -# if TESTER_MODE -# include "tester.h" -# define ALLOCATE_MEMORY fake_malloc -# define FREE_MEMORY fake_free -# else -# define ALLOCATE_MEMORY malloc -# define FREE_MEMORY free -# endif - /* A struct to store memory blocks which allocated by using malloc function. Yes, it is a linked list. */ -typedef struct s_memory_block { - void *ptr; - struct s_memory_block *next; -} t_memory_block; +typedef struct s_memory_block +{ + void *ptr; + struct s_memory_block *next; +} t_memory_block; void *safe_malloc(int size); void safe_free(void *ptr); t_memory_block *get_memory_blocks(void); void free_memory(void); void abort_memory(void); -void register_pre_abort_func(void (*abort_func)(void)); -void register_post_abort_func(void (*abort_func)(void)); +void register_pre_abort_func(void (*abort_func)(void)); +void register_post_abort_func(void (*abort_func)(void)); #endif diff --git a/memory-allocator/tester.c b/memory-allocator/tester.c deleted file mode 100644 index 0b948c5..0000000 --- a/memory-allocator/tester.c +++ /dev/null @@ -1,103 +0,0 @@ -#include "allocator.h" -#include -#include -#include -#include - int - gettimeofday(struct timeval *restrict tp, void *restrict tzp); - -void print_hex(unsigned long num) { - if (num >= 16) { - print_hex(num / 16); - } - write(1, &"0123456789abcdef"[num % 16], 1); -} - -void print_ptr(char *msg, void *ptr) { - while (*msg) { - if (msg[0] == '%' && msg[1] == 'p') { - print_hex((unsigned long) ptr); - msg += 2; - continue; - } - write(1, msg, 1); - msg++; - } -} - -void *fake_malloc(int size) { - static int seed = -1; - - if (seed < 0) { - struct timeval time; - gettimeofday(&time, NULL); - seed = (int) (time.tv_usec + time.tv_sec); - srand(seed); - } - - int chance = rand() % 10; - - if (chance < 1){ - return NULL; - } else { - void *ptr = malloc(size); - print_ptr("MALLOC %p\n", ptr); - return ptr; - } -} - -void fake_free(void *ptr) { - print_ptr("FREE %p\n", ptr); - free(ptr); -} - -void test() { - int **test = (int **) safe_malloc(50 * sizeof(int *)); - if (test == NULL) { - print_ptr("PROGRAM LEVEL malloc failed\n", 0); - return; - } - for (int i = 0; i < 50; i++) { - test[i] = safe_malloc(sizeof(int) * i); - if (test[i] == NULL) { - print_ptr("PROGRAM LEVEL malloc(2) failed\n", 0); - return; - } - } - - abort_memory(); -} - -void test2() { - if (!safe_malloc(10)) return; - if (!safe_malloc(10)) return; - void *ptr = safe_malloc(10); - if (!ptr) return; - safe_free(ptr); - if (!safe_malloc(10)) return; - - void *ptr2 = safe_malloc(10); - if (!ptr2) return; - safe_free(ptr2); - - abort_memory(); -} - -void abort_start_func() { - print_ptr("PROGRAM LEVEL i know that memory of my program started to being aborted\n", 0); -} - -void abort_stop_func() { - print_ptr("PROGRAM LEVEL i know that memory of my program completely aborted.\n", 0); - // exit() will fit here perfectly -} - - -int main() { - register_pre_abort_func(abort_start_func); - register_post_abort_func(abort_stop_func); - //test(); - write(1, "test2\n", 6); - test2(); - return 0; -} diff --git a/memory-allocator/tester.h b/memory-allocator/tester.h deleted file mode 100644 index 55c833f..0000000 --- a/memory-allocator/tester.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef TESTER_H -# define TESTER_H - -void *fake_malloc(int size); -void fake_free(void *ptr); - -#endif diff --git a/memory-allocator/tester.sh b/memory-allocator/tester.sh deleted file mode 100644 index a957bb3..0000000 --- a/memory-allocator/tester.sh +++ /dev/null @@ -1,30 +0,0 @@ -echo "Compiling for tester mode..." -gcc -DTESTER_MODE=1 aborter.c allocator.c tester.c -o allocator_test - -if [ $? -ne 0 ]; then - echo "Compilation failed." - exit 1 -fi - -echo "Compilation successful. Brute force testing starting..." -echo "----- MALLOC <--> FREE -----" -for ((i=1; i<=10000; i++)) -do - ./allocator_test > output.txt - - if [ $? -ne 0 ]; then - echo "Execution failed." - exit 1 - fi - - malloc_count=$(grep 'MALLOC' output.txt | grep -v '0x0' | wc -l | awk '{print $1}') - free_count=$(grep 'FREE' output.txt | wc -l | awk '{print $1}') - - echo "$i# test: $malloc_count <--> $free_count" - if [ $malloc_count -ne $free_count ]; then - echo "Counts of 'MALLOC' and 'FREE' are not equal." - exit 1 - fi -done - -echo "All tests passed successfully." \ No newline at end of file