├── Makefile ├── ft_alloc.h ├── ft_alloc_util.c ├── README.md └── ft_alloc.c /Makefile: -------------------------------------------------------------------------------- 1 | SRS = ft_alloc.c ft_alloc_util.c 2 | 3 | OBJS = $(SRS:.c=.o) 4 | 5 | CC = gcc 6 | 7 | CFLAGS = -Wall -Wextra -Werror 8 | 9 | NAME = ft_alloc.a 10 | 11 | all: $(NAME) 12 | 13 | $(NAME): $(OBJS) 14 | ar rc $(NAME) $(OBJS) 15 | ranlib $(NAME) 16 | 17 | %.o: %.c ft_alloc.h 18 | $(CC) $(CFLAGS) -c $< -o $@ 19 | 20 | clean: 21 | rm -f $(OBJS) 22 | 23 | fclean: clean 24 | rm -f $(NAME) 25 | 26 | re: fclean all 27 | 28 | .PHONY: all clean fclean re 29 | -------------------------------------------------------------------------------- /ft_alloc.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_alloc.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: nbenyahy +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/07/28 07:28:54 by nbenyahy #+# #+# */ 9 | /* Updated: 2024/07/28 12:01:01 by nbenyahy ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef FT_ALLOC_H 14 | # define FT_ALLOC_H 15 | 16 | # include 17 | # include 18 | # include 19 | 20 | enum e_alloc_mode 21 | { 22 | MALLOC, 23 | CALLOC, 24 | REALLOC, 25 | FREE_ALL, 26 | FREE_PTR, 27 | }; 28 | 29 | typedef struct s_address 30 | { 31 | void *address; 32 | size_t size; 33 | struct s_address *next; 34 | struct s_address *prev; 35 | } t_address; 36 | 37 | void ft_alloc_add_back(t_address **lst, t_address *new_node); 38 | t_address *ft_alloc_new_node(void *ptr, size_t size); 39 | void *ft_alloc(size_t size, void *ptr, char c); 40 | 41 | #endif -------------------------------------------------------------------------------- /ft_alloc_util.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_alloc_util.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: nbenyahy +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/07/28 11:12:19 by nbenyahy #+# #+# */ 9 | /* Updated: 2024/07/28 11:54:46 by nbenyahy ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_alloc.h" 14 | 15 | void ft_alloc_add_back(t_address **lst, t_address *new_node) 16 | { 17 | t_address *tmp; 18 | 19 | tmp = (*lst); 20 | if (!lst) 21 | return ; 22 | if (!(*lst)) 23 | *lst = new_node; 24 | else 25 | { 26 | tmp = *lst; 27 | while (tmp->next) 28 | tmp = tmp->next; 29 | tmp->next = new_node; 30 | new_node->prev = tmp; 31 | } 32 | } 33 | 34 | t_address *ft_alloc_new_node(void *ptr, size_t size) 35 | { 36 | t_address *node; 37 | 38 | node = malloc(sizeof(t_address)); 39 | if (!node) 40 | return (NULL); 41 | node->address = ptr; 42 | node->size = size; 43 | node->next = NULL; 44 | node->prev = NULL; 45 | return (node); 46 | } 47 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 |

NO LEAKS NO REEKS

3 |
4 | Image 5 |
6 |
7 | 8 | # Memory Management Library 9 | 10 | This library provides a simple mechanism to manage dynamic memory allocations in C and to ensure that all allocated memory is freed at the end of the program, preventing memory leaks. It includes functions for allocating and freeing memory, and maintains a list of all allocated addresses. 11 | 12 | ## Functions 13 | 14 | ### `void *ft_alloc(size_t size, void *ptr, char mode)` 15 | 16 | Allocates, reallocates, or frees memory based on the specified operation. 17 | 18 | - **Parameters:** 19 | - `size`: The size of the memory to allocate. 20 | - `ptr`: The pointer to the memory to reallocate or free. Set to `NULL` for new allocations. 21 | - `mode`: The operation to perform. This can be: 22 | - `MALLOC` for `malloc` 23 | - `CALLOC` for `calloc` 24 | - `FREE_PTR` to free a specific pointer 25 | - `FREE_ALL` to free all allocated memory 26 | 27 | - **Returns:** A pointer to the allocated memory, or `NULL` if an error occurs. 28 | 29 | ## instalation 30 | - clone the repo inside your main project: 31 | ``` 32 | git clone git@github.com:nourddine-benyahya/ft_alloc.git 33 | ``` 34 | 35 | - append this inside your makefile in building target rule 36 | ``` 37 | make -C ft_alloc all 38 | ``` 39 | - append this inside your makefile in librarys includes 40 | ``` 41 | alloc/ft_alloc.a 42 | ``` 43 | - include `#include "ft_alloc.h"` in your header file 44 | ``` 45 | #include "ft_alloc.h" 46 | ``` 47 | 48 | #### example : 49 | before : 50 | ``` 51 | LIBS = libft/libft.a 52 | $(NAME) : $(OBJ) 53 | $(CC) $(CFLAGS) $(OBJ) $(LIBS) -o $(NAME) 54 | ``` 55 | after : 56 | ``` 57 | LIBS = libft/libft.a alloc/ft_alloc.a 58 | $(NAME) : $(OBJ) 59 | make -C ft_alloc all 60 | $(CC) $(CFLAGS) $(OBJ) $(LIBS) -o $(NAME) 61 | ``` 62 | 63 | 64 | 65 | make the libarary 66 | `cd ft_alloc` 67 | 68 | 69 | ## Usage 70 | 71 | ### simple testing Code Example 72 | 73 | ```c 74 | #include "ft_alloc.h" 75 | 76 | void leaks(void) 77 | { 78 | system("leaks a.out"); 79 | } 80 | 81 | int ft_testing(void) 82 | { 83 | //check memory leaks at the exit of the programm 84 | atexit(leaks); 85 | 86 | // Allocate memory using malloc 87 | void *ptr1 = ft_alloc(sizeof(char *), NULL, MALLOC); 88 | 89 | // Allocate memory using calloc 90 | void *ptr2 = ft_alloc(sizeof(int *), NULL, CALLOC); 91 | 92 | // Free a specific pointer 93 | ft_alloc(0, ptr1, FREE_PTR); 94 | 95 | // Free all allocated memory 96 | ft_alloc(0, NULL, FREE_ALL); 97 | 98 | return (0); 99 | } 100 | int main() 101 | { 102 | ft_testing(); 103 | } 104 | ``` 105 | 106 | -------------------------------------------------------------------------------- /ft_alloc.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_alloc.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: nbenyahy +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/07/28 07:28:38 by nbenyahy #+# #+# */ 9 | /* Updated: 2024/07/28 12:02:06 by nbenyahy ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_alloc.h" 14 | 15 | static void *clear_address(t_address **lst) 16 | { 17 | t_address *tmp; 18 | 19 | if (!lst || !*lst) 20 | return (NULL); 21 | while (*lst) 22 | { 23 | tmp = *lst; 24 | *lst = (*lst)->next; 25 | free(tmp->address); 26 | free(tmp); 27 | } 28 | return (NULL); 29 | } 30 | 31 | static void *ft_malloc(size_t size, t_address **lst) 32 | { 33 | void *ptr; 34 | t_address *newnode; 35 | 36 | ptr = malloc(size); 37 | if (!ptr) 38 | return (NULL); 39 | newnode = ft_alloc_new_node(ptr, size); 40 | if (!newnode) 41 | { 42 | free(ptr); 43 | return (NULL); 44 | } 45 | ft_alloc_add_back(lst, newnode); 46 | return (ptr); 47 | } 48 | 49 | static void *ft_calloc(size_t size, t_address **lst) 50 | { 51 | void *ptr; 52 | t_address *newnode; 53 | size_t i; 54 | 55 | i = 0; 56 | if (size > SIZE_MAX) 57 | return (NULL); 58 | ptr = malloc(size); 59 | if (!ptr) 60 | return (NULL); 61 | while (i < size) 62 | { 63 | ((unsigned char *)ptr)[i] = 0; 64 | i++; 65 | } 66 | newnode = ft_alloc_new_node(ptr, size); 67 | if (!newnode) 68 | { 69 | free(ptr); 70 | return (NULL); 71 | } 72 | ft_alloc_add_back(lst, newnode); 73 | return (ptr); 74 | } 75 | 76 | static void *ft_free(void *ptr, t_address **lst) 77 | { 78 | t_address *current; 79 | 80 | if (!ptr || !lst || !*lst) 81 | return (NULL); 82 | current = *lst; 83 | while (current) 84 | { 85 | if (ptr == current->address) 86 | { 87 | if (current->prev) 88 | current->prev->next = current->next; 89 | else 90 | *lst = current->next; 91 | if (current->next) 92 | current->next->prev = current->prev; 93 | free(current->address); 94 | free(current); 95 | return (NULL); 96 | } 97 | current = current->next; 98 | } 99 | return (NULL); 100 | } 101 | 102 | void *ft_alloc(size_t size, void *ptr, char c) 103 | { 104 | static t_address *address_list = NULL; 105 | 106 | if (c == MALLOC) 107 | return (ft_malloc(size, &address_list)); 108 | else if (c == CALLOC) 109 | return (ft_calloc(size, &address_list)); 110 | else if (c == FREE_PTR) 111 | return (ft_free(ptr, &address_list)); 112 | else if (c == FREE_ALL) 113 | return (clear_address(&address_list)); 114 | return (NULL); 115 | } 116 | --------------------------------------------------------------------------------