Development repo for tnard project
This project is made to perform a clean management of all malloc.
Functions :
t_m_free *ft_free_init(void);
Return malloced struct of t_m_free to store all malloced ptr
void *ft_free_malloc(size_t size);
Malloc a new ptr and store it in m_free struct (ft_free_add not needed, already done)
int ft_free_add(void *ptr);
Add inside m_free the malloced ptr
void ft_free_remove(void *ptr);
Remove inside m_free the ptr malloced and free it
void ft_free(t_m_free *m_free);
Free all maloced ptr and free m_free
int ft_free_size(void);
Return the number of malloced ptr
If you try to add two (or much) time a ptr to ft_free_add(), that gonna do nothing (The program cancel the action if found in the list)
If you try to free a function with ft_free_remove two times (or more) that gonna do nothing.
But if you free it with free() that can crash if after you use ft_free_remove() / ft_free() - To patch that :
- Set the PTR to NULL (Or don't use free()..).
If you try to add a variable not malloced / not NULLED it will crash the program.
If you want to use it, be sure the variables is malloced or the variable = NULL
If a NULL variables is gived in ft_free_add(), the program will don't add it
Example of a crash :
int main(void)
{
t_m_free *m_free;
char *a;
char *b;
a = malloc(sizeof(char));
ft_free_add((void *)a);
ft_free_add((void *)b);
}
To patch it :
b = NULL; // Before adding it, or don't add it ;).
If a variables is manually freed and is inside the free list, that gonna cause a "double free error" if you use ft_free() / ft_free_remove(m_free_ptr, ptr)
The struct m_free returned by ft_free_init() is a malloced struct, so if you don't use ft_free(), he don't gonna be free. If you init him and you don't add any ptr, you can use ft_free or free(m_free)
int main(void)
{
t_m_free *m_free;
char *test;
char *test2;
// Now you can malloc a variable.
test = malloc(sizeof(char));
test2 = ft_free_malloc(sizeof(char));
ft_free_add((void *)test);
// If you want to remove it manually you just need to use :
ft_free_remove((void *)test);
// Else, if you want to remove all malloced variable (also needed to destroy *m_free):
}
int main(void)
{
t_m_free *m_free;
char *a;
int *b;
char **c;
char *d;
int x;
d = NULL;
x = 0;
while (x < 1000)
{
a = malloc(sizeof(char) * 100);
ft_free_add(a);
b = malloc(sizeof(int) * 100);
ft_free_add(b);
c = malloc(sizeof(char *) * 2);
c[0] = malloc(sizeof(char) * 100);
c[1] = malloc(sizeof(char) * 100);
ft_free_add(d); // Exemple of null pointer
ft_free_add(c[1]);
ft_free_add(c);
ft_free_add(c[0]);
x++;
}
printf("%d\n", ft_free_size());
return (0);
}
int main(void)
{
t_m_free *m_free;
char *test;
test = ft_free_malloc(sizeof(char) * 100);
test[0] = 'a';
test[1] = 'b';
test[2] = 'c';
test[3] = '\0';
printf("%s\n", test);
return (0);
}
int main(void)
{
t_m_free *m_free;
char *test;
printf("%d\n", ft_free_size());
test = ft_free_malloc(sizeof(char));
printf("%d\n", ft_free_size());
return (0);
}
██╗ ██╗██████╗ ██╗ ██╗ ██╗ ██████╗ ███╗ ██╗ ███████╗██████╗
██║ ██║╚════██╗██║ ╚██╗ ██╔╝██╔═══██╗████╗ ██║ ██╔════╝██╔══██╗
███████║ █████╔╝██║ ╚████╔╝ ██║ ██║██╔██╗ ██║ █████╗ ██████╔╝
╚════██║██╔═══╝ ██║ ╚██╔╝ ██║ ██║██║╚██╗██║ ██╔══╝ ██╔══██╗
██║███████╗███████╗██║ ╚██████╔╝██║ ╚████║██╗██║ ██║ ██║
╚═╝╚══════╝╚══════╝╚═╝ ╚═════╝ ╚═╝ ╚═══╝╚═╝╚═╝ ╚═╝ ╚═╝