misc: many things
This commit is contained in:
parent
664b18eb2e
commit
8076487071
25 changed files with 554 additions and 73 deletions
|
|
@ -11,7 +11,7 @@ PRINTF_CODE = \
|
|||
ft_strjoin stream1 stream2 utils1 utils2
|
||||
PRINTF_SRCS = tf_printf/src/
|
||||
PRINTF_INCLUDES = ./ tf_printf/include/
|
||||
WHAT = LIBTF v4.2
|
||||
WHAT = LIBTF v7
|
||||
USED = malloc() free() write()
|
||||
|
||||
# It works and I probably won't change it for the rest of my life
|
||||
|
|
@ -21,18 +21,16 @@ PRINTF_C_FILES = $(addsuffix .c,$(addprefix $(PRINTF_SRCS),$(PRINTF_CODE)))
|
|||
PRINTF_O_FILES = $(addsuffix .o,$(addprefix $(PRINTF_SRCS),$(PRINTF_CODE)))
|
||||
ifdef DEBUG
|
||||
CC = cc -Wall -Wextra -Werror -g
|
||||
FINISH_MSG = "\e[7;33m $(WHAT) compiled! (debug) \e[0m"
|
||||
FINISH_MSG = "| $(WHAT) compiled! (debug)"
|
||||
else
|
||||
CC = cc -Wall -Wextra -Werror
|
||||
FINISH_MSG = "\e[7;36m $(WHAT) compiled! \e[0m"
|
||||
FINISH_MSG = "| $(WHAT) compiled!"
|
||||
endif
|
||||
AR = ar rcs
|
||||
|
||||
all : $(NAME)
|
||||
@echo ""
|
||||
@echo $(FINISH_MSG)
|
||||
@echo "\e[36mFunctions used: $(USED) \e[0m"
|
||||
@echo ""
|
||||
@echo "| Functions used: $(USED)"
|
||||
|
||||
debug :
|
||||
make DEBUG=yes
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/18 16:37:23 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/01/23 18:13:29 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/03/11 14:03:48 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -20,9 +20,12 @@
|
|||
typedef struct s_memclass_in *t_memclass_in;
|
||||
typedef struct s_element t_element;
|
||||
|
||||
typedef struct s_memclass_in *t_memclass_in;
|
||||
|
||||
typedef struct s_element
|
||||
{
|
||||
void *address;
|
||||
t_memclass_in subclass;
|
||||
t_element *previous;
|
||||
t_element *next;
|
||||
t_memclass_in mc;
|
||||
|
|
@ -31,6 +34,7 @@ typedef struct s_element
|
|||
typedef struct s_memclass_in
|
||||
{
|
||||
t_element *first;
|
||||
t_element *parent_element;
|
||||
} *t_memclass_in;
|
||||
|
||||
void *create_address_with_element(t_element *element, size_t size);
|
||||
|
|
@ -39,4 +43,6 @@ t_element *get_address_element(void *address);
|
|||
|
||||
void free_address_with_element(void *address);
|
||||
|
||||
void freeelement(t_element *el);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/18 11:07:59 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/02/23 14:54:14 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/03/11 12:12:03 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -73,6 +73,14 @@ typedef void *t_memclass;
|
|||
*/
|
||||
t_memclass mem_newclass(t_err *err);
|
||||
|
||||
/**
|
||||
* Create a t_memclass and add it to parent.
|
||||
* It means if we freeall(parent), this mc is freeall too.
|
||||
* Possible errors: "alloc error"
|
||||
* Return NULL on error.
|
||||
*/
|
||||
t_memclass mem_subclass(t_err *err, t_memclass parent);
|
||||
|
||||
/**
|
||||
* Alloc memory of the class 'mc' and of the size 'size'.
|
||||
* If mc is NULL, do nothing.
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/18 11:08:16 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/02/23 13:27:46 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/03/11 14:03:06 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -18,10 +18,11 @@ t_memclass mem_newclass(t_err *err)
|
|||
{
|
||||
t_memclass_in r;
|
||||
|
||||
r = malloc(sizeof(t_memclass_in));
|
||||
r = malloc(sizeof(struct s_memclass_in));
|
||||
if (!r)
|
||||
return ((*err)("alloc error"), NULL);
|
||||
r->first = NULL;
|
||||
r->parent_element = NULL;
|
||||
return (r);
|
||||
}
|
||||
|
||||
|
|
@ -37,8 +38,9 @@ void *mem_alloc(t_err *err, t_memclass mc, size_t size)
|
|||
return ((*err)("alloc error"), NULL);
|
||||
r = create_address_with_element(new_element, size);
|
||||
if (!r)
|
||||
return ((*err)("alloc error"), NULL);
|
||||
return (free(new_element), (*err)("alloc error"), NULL);
|
||||
new_element->address = r;
|
||||
new_element->subclass = NULL;
|
||||
new_element->next = mc_in->first;
|
||||
if (new_element->next)
|
||||
new_element->next->previous = new_element;
|
||||
|
|
@ -60,9 +62,14 @@ void mem_freeall(t_memclass mc)
|
|||
{
|
||||
next = el->next;
|
||||
free_address_with_element(el->address);
|
||||
free(el);
|
||||
if (el->subclass)
|
||||
mem_freeall(el->subclass);
|
||||
else
|
||||
free(el);
|
||||
el = next;
|
||||
}
|
||||
if (((t_memclass_in) mc)->parent_element)
|
||||
freeelement(((t_memclass_in) mc)->parent_element);
|
||||
free(mc);
|
||||
}
|
||||
|
||||
|
|
@ -74,11 +81,30 @@ void mem_free(void *address)
|
|||
return ;
|
||||
el = get_address_element(address);
|
||||
free_address_with_element(address);
|
||||
if (el->previous)
|
||||
el->previous->next = el->next;
|
||||
else
|
||||
el->mc->first = el->next;
|
||||
if (el->next)
|
||||
el->next->previous = el->previous;
|
||||
free(el);
|
||||
freeelement(el);
|
||||
}
|
||||
|
||||
t_memclass mem_subclass(t_err *err, t_memclass parent)
|
||||
{
|
||||
t_memclass_in r;
|
||||
t_memclass_in parent_in;
|
||||
t_element *new_element;
|
||||
|
||||
parent_in = (t_memclass_in) parent;
|
||||
r = mem_newclass(err);
|
||||
if (!r)
|
||||
return (err("alloc error"), NULL);
|
||||
new_element = malloc(sizeof(t_element));
|
||||
if (!new_element)
|
||||
return (err("alloc error"), mem_freeall(r), NULL);
|
||||
new_element->address = NULL;
|
||||
new_element->subclass = r;
|
||||
new_element->next = parent_in->first;
|
||||
if (new_element->next)
|
||||
new_element->next->previous = new_element;
|
||||
new_element->previous = NULL;
|
||||
new_element->mc = parent;
|
||||
parent_in->first = new_element;
|
||||
r->parent_element = new_element;
|
||||
return ((t_memclass) r);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,12 +6,23 @@
|
|||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/18 16:36:49 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/01/23 18:16:18 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/03/11 14:03:20 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "mem_utils.h"
|
||||
|
||||
void freeelement(t_element *el)
|
||||
{
|
||||
if (el->previous)
|
||||
el->previous->next = el->next;
|
||||
else
|
||||
el->mc->first = el->next;
|
||||
if (el->next)
|
||||
el->next->previous = el->previous;
|
||||
free(el);
|
||||
}
|
||||
|
||||
void *create_address_with_element(t_element *element, size_t size)
|
||||
{
|
||||
void *r;
|
||||
|
|
@ -30,5 +41,6 @@ t_element *get_address_element(void *address)
|
|||
|
||||
void free_address_with_element(void *address)
|
||||
{
|
||||
free((char *)address - sizeof(t_element *));
|
||||
if (address)
|
||||
free((char *)address - sizeof(t_element *));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue