├── Makefile ├── misc_functions2.c ├── ft_malloc.h ├── misc_functions.c ├── README.md └── ft_malloc.c /Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: hmeftah +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2023/05/26 11:37:19 by hmeftah #+# #+# # 9 | # Updated: 2023/05/26 12:57:42 by hmeftah ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | NAME = ft_malloc.a 14 | 15 | FLAGS = -Wall -Werror -Wextra 16 | 17 | CC = cc 18 | 19 | SOURCES = $(addprefix ./, ft_malloc.c misc_functions.c misc_functions2.c) 20 | 21 | INCLUDE = ft_malloc.h 22 | 23 | all: $(NAME) $(SOURCES) 24 | 25 | $(NAME): $(SOURCES:.c=.o) $(INCLUDE) 26 | ar -r $(NAME) $(SOURCES:.c=.o) 27 | 28 | %.o: %.c 29 | $(CC) $(FLAGS) -I $(INCLUDE) -c $< 30 | 31 | clean: 32 | rm -rf $(SOURCES:.c=.o) 33 | 34 | fclean: clean 35 | rm -rf $(NAME) 36 | 37 | re: fclean all 38 | 39 | .PHONY: all clean fclean re -------------------------------------------------------------------------------- /misc_functions2.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* misc_functions2.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hmeftah +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/25 20:12:47 by hmeftah #+# #+# */ 9 | /* Updated: 2023/05/26 14:24:39 by hmeftah ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_malloc.h" 14 | 15 | void print_str_with_int(char *string, unsigned int n) 16 | { 17 | print_string(string); 18 | print_number(n); 19 | write(1, "\n", 1); 20 | } 21 | 22 | void print_string(char *string) 23 | { 24 | int i; 25 | 26 | i = 0; 27 | while (string[i]) 28 | { 29 | write(1, &string[i], 1); 30 | i++; 31 | } 32 | } 33 | 34 | void print_number(unsigned int number) 35 | { 36 | char n; 37 | 38 | if (number >= 0 && number <= 9) 39 | { 40 | n = number + 48; 41 | write(1, &n, 1); 42 | } 43 | else 44 | { 45 | print_number(number / 10); 46 | print_number(number % 10); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /ft_malloc.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_malloc.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hmeftah +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/25 16:36:53 by hmeftah #+# #+# */ 9 | /* Updated: 2023/05/26 14:24:32 by hmeftah ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef FT_MALLOC_H 14 | # define FT_MALLOC_H 15 | 16 | # include 17 | # include 18 | # include 19 | 20 | enum e_a_types { 21 | ALLOC, 22 | FREE, 23 | FREE_ALL, 24 | T_SIZE, 25 | }; 26 | 27 | struct s_address 28 | { 29 | void *ptr; 30 | unsigned int size; 31 | bool dealloc; 32 | struct s_address *next; 33 | struct s_address *prev; 34 | }; 35 | typedef struct s_address t_address; 36 | 37 | void *ft_malloc(unsigned int size, void *free_ptr, 38 | int type, uint32_t *t_size); 39 | t_address *create_node(void *ptr, unsigned int size); 40 | void add_node_back(t_address **head, t_address *node); 41 | void destroy_node(t_address *node); 42 | void print_string(char *string); 43 | void print_number(unsigned int number); 44 | void print_str_with_int(char *string, unsigned int n); 45 | #endif -------------------------------------------------------------------------------- /misc_functions.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* misc_functions.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hmeftah +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/25 16:54:30 by hmeftah #+# #+# */ 9 | /* Updated: 2023/05/26 14:48:08 by hmeftah ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_malloc.h" 14 | 15 | void destroy_node(t_address *node) 16 | { 17 | if (node == NULL) 18 | return ; 19 | if (node->ptr) 20 | { 21 | free (node->ptr); 22 | node->dealloc = 1; 23 | node->ptr = NULL; 24 | node->size = 0; 25 | } 26 | free(node); 27 | node = NULL; 28 | } 29 | 30 | t_address *create_node(void *ptr, unsigned int size) 31 | { 32 | t_address *node; 33 | 34 | node = malloc(sizeof(t_address)); 35 | if (node == NULL) 36 | return (NULL); 37 | node->size = size; 38 | node->dealloc = 0; 39 | node->prev = NULL; 40 | node->next = NULL; 41 | node->ptr = ptr; 42 | return (node); 43 | } 44 | 45 | void add_node_back(t_address **head, t_address *node) 46 | { 47 | t_address *temp; 48 | 49 | temp = *head; 50 | if (!*head) 51 | { 52 | *head = node; 53 | return ; 54 | } 55 | else if (temp) 56 | { 57 | while (temp->next) 58 | temp = temp->next; 59 | } 60 | temp->next = node; 61 | node->prev = temp; 62 | } 63 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FT_MALLOC 2 | ## _All in One!_ 3 | [![C Language](https://img.shields.io/badge/C-00599C?style=for-the-badge&logo=c&logoColor=white)]() 4 | 5 | > ft_malloc provides flexible memory allocation and management 6 | > with the ability to store all allocated pointers to be destroyed at will. 7 | 8 | ## Features 9 | 10 | - Proper allocation and disposal of memory when needed. 11 | - Calculation and display of memory reserved. 12 | - Implemented with proper memory management etiquette. 13 | - Ability to destroy a single or all pointers allocated by ft_malloc. 14 | - Coded with 42's norminette in mind for it to be used by students. 15 | 16 | 17 | ## Function Prototype 18 | ```c 19 | void *ft_malloc(unsigned int size, void *free_ptr, int type, uint32_t *t_size); 20 | ``` 21 | 22 | ## Guide 23 | ***Warning:*** 24 | Make sure to change the name of the author and email in 42's Header. 25 | 26 | ft_malloc relies on a list of types to achieve different goals. 27 | 28 | - [ALLOC] - This type allows ft_malloc to allocate memory for a single pointer returns it 29 | and stores it in a linked list. 30 | ```c 31 | USAGE: char *ptr = ft_malloc(42, NULL, ALLOC, NULL); 32 | ``` 33 | - [FREE] - Frees the free_ptr pointer and destroys the node associated with it. 34 | ```c 35 | USAGE: ft_malloc(0, ptr, FREE, NULL); 36 | ``` 37 | - [FREE_ALL] - Frees and nullifies all pointers allocated by ft_malloc. 38 | ```c 39 | USAGE: ft_malloc(0, NULL, FREE_ALL, NULL); 40 | ``` 41 | - [T_SIZE] - Displays the current size of all bytes allocated by ft_malloc. 42 | ``` 43 | USAGE: 44 | unsigned int size = 0; 45 | ft_malloc(0, NULL, T_SIZE, &size); 46 | ``` 47 | 48 | Please note that ft_malloc will not count any pointers allocated by other functions (malloc(), calloc() or any memory allocation functions). 49 | Also it will malfunction if you use free(), instead of the builtin [FREE] type that this library has. 50 | 51 | ## Mechanism of Action 52 | 53 | * ft_malloc relies heavily on the static linked list inside it to function, each time you allocate using ft_malloc a new node is created and points 54 | to the newly allocated pointer for it to be later freed, Once you use the [FREE_ALL] type, all pointers held by ft_malloc will be destoryed. 55 | 56 | ## Bugs 57 | * Since this is the first version of this library it is prone to bugs and issues. 58 | while the initial testing was successful, there might be issues that lie within. 59 | I'll appreciate any kind of feedback at my discord account ***SHM#5112*** 60 | 61 | 62 | [//]: # (Comments) 63 | 64 | [T_SIZE]: 65 | [FREE_ALL]: 66 | [ALLOC]: 67 | [FREE]: 68 | -------------------------------------------------------------------------------- /ft_malloc.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_malloc.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hmeftah +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/05/25 16:47:23 by hmeftah #+# #+# */ 9 | /* Updated: 2023/05/26 14:48:21 by hmeftah ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_malloc.h" 14 | 15 | static void *allocate_memory(unsigned int size, t_address **addresses) 16 | { 17 | void *ptr; 18 | t_address *node; 19 | 20 | ptr = malloc(size); 21 | if (ptr == NULL) 22 | return (NULL); 23 | node = create_node(ptr, size); 24 | if (node == NULL) 25 | { 26 | free(ptr); 27 | return (NULL); 28 | } 29 | add_node_back(addresses, node); 30 | return (ptr); 31 | } 32 | 33 | static void *free_memory(t_address **addresses) 34 | { 35 | t_address *t_node; 36 | 37 | t_node = *addresses; 38 | if (!*addresses) 39 | return (NULL); 40 | while (t_node->next) 41 | { 42 | t_node = t_node->next; 43 | destroy_node(t_node->prev); 44 | } 45 | destroy_node(t_node); 46 | *addresses = NULL; 47 | return (NULL); 48 | } 49 | 50 | static void *free_pointer(void *ptr, t_address *addresses) 51 | { 52 | t_address *temp; 53 | 54 | temp = addresses; 55 | while (temp) 56 | { 57 | if (temp->ptr == ptr) 58 | { 59 | temp->prev->next = temp->next; 60 | free(temp->ptr); 61 | temp->dealloc = 1; 62 | temp->size = 0; 63 | temp->ptr = NULL; 64 | free(temp); 65 | return (NULL); 66 | } 67 | temp = temp->next; 68 | } 69 | return (NULL); 70 | } 71 | 72 | static void *get_t_size(t_address *addresses, unsigned int *t_size) 73 | { 74 | t_address *temp; 75 | uint32_t size_max; 76 | uint32_t size_ptr; 77 | 78 | size_max = 0; 79 | size_ptr = 0; 80 | temp = addresses; 81 | if (!addresses) 82 | return (NULL); 83 | while (temp) 84 | { 85 | size_ptr += temp->size; 86 | size_max += size_ptr + sizeof(t_address); 87 | temp = temp->next; 88 | } 89 | if (t_size) 90 | *t_size = size_ptr; 91 | return (NULL); 92 | } 93 | 94 | /* 95 | - Allocates Memory using malloc(), Returns a pointer on success. 96 | - Has three types ALLOC, FREE, FREE_ALL, T_SIZE. 97 | - ALLOC to allocate memory for a pointer. 98 | - FREE to free a pointer. 99 | - FREE_ALL to free all pointers allocated since the beginning of the program. 100 | - T_SIZE prints the total size of the allocated memory. 101 | */ 102 | void *ft_malloc(unsigned int size, void *free_ptr, 103 | int type, uint32_t *t_size) 104 | { 105 | static t_address *addresses = NULL; 106 | 107 | if (type == ALLOC) 108 | return (allocate_memory(size, &addresses)); 109 | else if (type == FREE) 110 | return (free_pointer(free_ptr, addresses)); 111 | else if (type == FREE_ALL) 112 | return (free_memory(&addresses)); 113 | else if (type == T_SIZE) 114 | return (get_t_size(addresses, t_size)); 115 | return (NULL); 116 | } 117 | --------------------------------------------------------------------------------