├── Makefile ├── README.md ├── safeAlloc.h └── safeAlloc.c /Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | CFLAGS = -Wall -Wextra -Werror -I. 3 | 4 | SRCS = $(shell find . -name "*.c") 5 | OBJS = $(SRCS:.c=.o) 6 | NAME = safeAlloc.a 7 | 8 | all: 9 | $(CC) $(CFLAGS) -c $(SRCS) 10 | ar rcs $(NAME) $(OBJS) 11 | 12 | re: fclean all 13 | 14 | clean: 15 | rm -rf $(OBJS) 16 | 17 | fclean: 18 | rm -rf $(OBJS) $(NAME) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # safeAlloc 2 | This library enhances the development process of projects by making the `malloc` function safer. The `safeAlloc()` function allocates memory and keeps track of the allocated memory blocks in a struct list, while also recording information such as the line number and size in the `safeAlloc` file. The `safeFree()` function removes the freed memory blocks from both the struct and the `safeAlloc` file, making it easier to identify unfreed memory. When exiting the program using the `safeExit()` function, all memory addresses in the struct are traversed and freed, allowing you to test your program safely.
3 | How To Use
4 | When you compile using the make command, you will see the output safeAlloc.a. After adding safeAlloc.h to your C library, compile your program with safeAlloc.a.
5 | Linkedin: https://www.linkedin.com/in/hiqermod/ 6 | -------------------------------------------------------------------------------- /safeAlloc.h: -------------------------------------------------------------------------------- 1 | /* 2 | safeAlloc.h 3 | Safe Memory Allocation 4 | Created By Omer Yumusak 5 | 06.25.2023 6 | https://www.linkedin.com/in/hiqermod/ 7 | */ 8 | #ifndef SAFEALLOC_H 9 | # define SAFEALLOC_H 10 | 11 | #define safeAlloc(nbr) OMERYUMUSAK_safeAlloc(nbr, __FILE__, __LINE__, 0) 12 | #define safeFree(ptr) OMERYUMUSAK_free(ptr, __FILE__, __LINE__) 13 | #define safeExit(nb) OMERYUMUSAK_exit(nb) 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | typedef struct s_safeList 22 | { 23 | char *fileDir; 24 | int line; 25 | void *alloc; 26 | size_t size; 27 | struct s_safeList *next; 28 | } t_safeList; 29 | 30 | void OMERYUMUSAK_free(void *dest, char *file, int line); 31 | void *OMERYUMUSAK_safeAlloc(int size, char *file, int line, int mode); 32 | void OMERYUMUSAK_exit(int nb); 33 | 34 | 35 | #endif -------------------------------------------------------------------------------- /safeAlloc.c: -------------------------------------------------------------------------------- 1 | /* 2 | safeAlloc.c 3 | Safe Memory Allocation 4 | Created By Omer Yumusak 5 | 06.25.2023 6 | https://www.linkedin.com/in/hiqermod/ 7 | */ 8 | 9 | #include "safeAlloc.h" 10 | 11 | static size_t my_strlen(char *str) 12 | { 13 | size_t counter; 14 | 15 | counter = 0; 16 | while (str[counter]) 17 | { 18 | counter++; 19 | } 20 | return (counter); 21 | } 22 | 23 | static char *my_strdup(char *str) 24 | { 25 | char *retVal; 26 | size_t counter; 27 | 28 | retVal = (char *)malloc(sizeof(char) * my_strlen(str) + 1); 29 | if (!retVal) 30 | { 31 | fprintf(stderr ,"SafeAlloc: Strdup Alloc Error!\n"); 32 | safeExit(-1); 33 | } 34 | 35 | counter = 0; 36 | while (*(str + counter)) 37 | { 38 | *(retVal + counter) = *(str + counter); 39 | counter++; 40 | } 41 | *(retVal + counter) = 0; 42 | 43 | return (retVal); 44 | } 45 | 46 | t_safeList *createList(char *fileDir, int line, void *alloc, int size) 47 | { 48 | t_safeList *retVal; 49 | 50 | retVal = (t_safeList *)malloc(sizeof(t_safeList)); 51 | if (!retVal) 52 | { 53 | fprintf(stderr ,"SafeAlloc: CreateList Malloc Err!\n"); 54 | safeExit(-1); 55 | } 56 | 57 | retVal->fileDir = my_strdup(fileDir); 58 | retVal->alloc = alloc; 59 | retVal->next = 0; 60 | retVal->line = line; 61 | retVal->size = size; 62 | 63 | return (retVal); 64 | } 65 | 66 | t_safeList *addList(t_safeList *lst, char *fileDir, int line, void *alloc, int size) 67 | { 68 | t_safeList *tmp; 69 | 70 | tmp = lst; 71 | while (tmp->next) 72 | tmp = tmp->next; 73 | 74 | tmp->next = (t_safeList *)malloc(sizeof(t_safeList)); 75 | if (!tmp->next) 76 | { 77 | fprintf(stderr, "SafeAlloc: AddList Alloc Err\n"); 78 | safeExit(-1); 79 | } 80 | 81 | tmp->next->next = 0; 82 | tmp->next->alloc = alloc; 83 | tmp->next->fileDir = my_strdup(fileDir); 84 | tmp->next->line = line; 85 | tmp->next->size = size; 86 | 87 | return (lst); 88 | } 89 | 90 | t_safeList *pushList(t_safeList *lst, char *fileDir, int line, void *alloc, int size) 91 | { 92 | if (lst == 0) 93 | lst = createList(fileDir, line, alloc, size); 94 | else 95 | lst = addList(lst, fileDir, line, alloc, size); 96 | return (lst); 97 | } 98 | 99 | 100 | void OMERYUMUSAK_exit(int nb) 101 | { 102 | t_safeList *lst; 103 | t_safeList *before; 104 | 105 | lst = OMERYUMUSAK_safeAlloc(0, "garbage!", 0, 1); 106 | before = 0; 107 | while (lst) 108 | { 109 | before = lst; 110 | lst = lst->next; 111 | free(before->alloc); 112 | free(before->fileDir); 113 | free(before); 114 | } 115 | exit(nb); 116 | } 117 | 118 | void *OMERYUMUSAK_safeAlloc(int size, char *file, int line, int mode) 119 | { 120 | void *ret_val; 121 | FILE *filee; 122 | static t_safeList *lst = 0; 123 | static int check = 0; 124 | 125 | // get Ptr mode 126 | if (mode == 1) 127 | return (lst); 128 | // set Ptr mode 129 | else if (mode == 2) 130 | { 131 | lst = 0; 132 | return (NULL); 133 | } 134 | 135 | if (size <= 0) 136 | { 137 | fprintf(stderr ,"SafeAlloc: Negative Size! Folder: %s, Line: %d\n", file, line); 138 | safeExit (-1); 139 | } 140 | 141 | 142 | ret_val = malloc(size); 143 | if (!ret_val) 144 | { 145 | fprintf(stderr, "SafeAlloc: Allocate Err! Folder: %s, Line: %d\n", file, line); 146 | safeExit(-1); 147 | } 148 | 149 | 150 | if (access("safeAlloc", F_OK)) 151 | { 152 | filee = fopen("./safeAlloc", "w"); 153 | } 154 | else 155 | filee = fopen("./safeAlloc", "a"); 156 | if (filee == NULL) 157 | { 158 | fprintf(stderr, "SafeAlloc: Open Err! Folder: %s, Line: %d\n", file, line); 159 | free(ret_val); 160 | safeExit(-1); 161 | } 162 | 163 | if (check == 0) 164 | { 165 | fprintf(filee, "SafeAlloc By Omer Yumusak\n"); 166 | check = 1; 167 | } 168 | 169 | 170 | lst = pushList(lst, file, line, ret_val, size); 171 | 172 | fprintf(filee, "File: %s, Line: %d, Size: %d\n", file, line, size); 173 | 174 | fclose(filee); 175 | return (ret_val); 176 | } 177 | 178 | void OMERYUMUSAK_free(void *dest, char *file, int line) 179 | { 180 | t_safeList *lst; 181 | t_safeList *tmp; 182 | int check; 183 | FILE *filee; 184 | 185 | check = 0; 186 | lst = OMERYUMUSAK_safeAlloc(0, "garbage!", 0, 1); 187 | 188 | tmp = lst; 189 | while (tmp) 190 | { 191 | if (tmp->alloc == dest) 192 | { 193 | check = 1; 194 | break; 195 | } 196 | tmp = tmp->next; 197 | } 198 | 199 | if (check == 0) 200 | { 201 | fprintf(stderr, "Not Allocated Free!\n"); 202 | return ; 203 | } 204 | 205 | filee = fopen("./safeAlloc", "w"); 206 | if (filee == NULL) 207 | { 208 | fprintf(stderr, "SafeAlloc: Open Err! Folder: %s, Line: %d\n", file, line); 209 | safeExit(-1); 210 | } 211 | 212 | fprintf(filee, "SafeAlloc By OMER YUMUSAK\n"); 213 | 214 | tmp = lst; 215 | lst = 0; 216 | 217 | while (tmp) 218 | { 219 | if (tmp->alloc != dest) 220 | fprintf(filee, "File: %s, Line: %d, Size: %ld\n", tmp->fileDir, tmp->line, tmp->size); 221 | else 222 | { 223 | if (lst && tmp->next) 224 | { 225 | lst->next = tmp->next; 226 | 227 | free(tmp->alloc); 228 | free(tmp->fileDir); 229 | free(tmp); 230 | tmp = lst; 231 | } 232 | else if (lst && !tmp->next) 233 | { 234 | free(tmp->alloc); 235 | free(tmp->fileDir); 236 | free(tmp); 237 | lst->next = 0; 238 | break; 239 | } 240 | else if (!lst && tmp->next) 241 | { 242 | lst = tmp; 243 | 244 | tmp = tmp->next; 245 | 246 | free(lst->alloc); 247 | free(lst->fileDir); 248 | free(lst); 249 | lst = 0; 250 | continue; 251 | } 252 | else if (!lst && !tmp->next) 253 | { 254 | free(tmp->alloc); 255 | free(tmp->fileDir); 256 | free(tmp); 257 | OMERYUMUSAK_safeAlloc(0, "garbage!", 0, 2); 258 | break; 259 | } 260 | } 261 | lst = tmp; 262 | tmp = tmp->next; 263 | } 264 | fclose(filee); 265 | } --------------------------------------------------------------------------------