first commit
This commit is contained in:
commit
5c78200d8f
13 changed files with 246 additions and 0 deletions
3
malloc/.gitmodules
vendored
Normal file
3
malloc/.gitmodules
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
[submodule "libft"]
|
||||||
|
path = libft
|
||||||
|
url = ../libft
|
||||||
86
malloc/Makefile
Normal file
86
malloc/Makefile
Normal file
|
|
@ -0,0 +1,86 @@
|
||||||
|
ifeq ($(HOSTTYPE),)
|
||||||
|
HOSTTYPE := $(shell uname -m)_$(shell uname -s)
|
||||||
|
endif
|
||||||
|
|
||||||
|
NAME = libft_malloc.so
|
||||||
|
ARCH_NAME = libft_malloc_$(HOSTTYPE).so
|
||||||
|
|
||||||
|
CC = gcc
|
||||||
|
FLAGS = -Wall -Wextra -Werror
|
||||||
|
D_FLAGS = -g
|
||||||
|
|
||||||
|
DELTA = $$(echo "$$(tput cols)-47"|bc)
|
||||||
|
|
||||||
|
LIBFT_DIR = libft/
|
||||||
|
LIBFT_LIB = $(LIBFT_DIR)libft.a
|
||||||
|
LIBFT_INC = $(LIBFT_DIR)includes/
|
||||||
|
|
||||||
|
SRC_DIR = src/
|
||||||
|
INC_DIR = includes/
|
||||||
|
OBJ_DIR = objs/
|
||||||
|
|
||||||
|
SRC_BASE = \
|
||||||
|
free.c\
|
||||||
|
malloc.c\
|
||||||
|
show_alloc_mem.c
|
||||||
|
|
||||||
|
SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE))
|
||||||
|
OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o))
|
||||||
|
NB = $(words $(SRC_BASE))
|
||||||
|
INDEX = 0
|
||||||
|
|
||||||
|
all :
|
||||||
|
@make -j $(NAME)
|
||||||
|
|
||||||
|
$(NAME): $(LIBFT_LIB) $(OBJ_DIR) $(OBJS)
|
||||||
|
@$(CC) --shared $(FLAGS) $(D_FLAGS) \
|
||||||
|
-I $(INC_DIR) \
|
||||||
|
-I $(LIBFT_INC) \
|
||||||
|
$(LIBS) \
|
||||||
|
$(LIBFT_LIB) $(OBJS) \
|
||||||
|
-o $(ARCH_NAME)
|
||||||
|
@ln -fs $(ARCH_NAME) $(NAME)
|
||||||
|
@printf "\r\e[48;5;15;38;5;25m✅ MAKE $(ARCH_NAME)\e[0m\e[K\n"
|
||||||
|
|
||||||
|
$(LIBFT_LIB):
|
||||||
|
@make -j -C $(LIBFT_DIR)
|
||||||
|
|
||||||
|
$(OBJ_DIR):
|
||||||
|
@mkdir -p $(OBJ_DIR)
|
||||||
|
@mkdir -p $(dir $(OBJS))
|
||||||
|
|
||||||
|
$(OBJ_DIR)%.o : $(SRC_DIR)%.c | $(OBJ_DIR)
|
||||||
|
@$(eval DONE=$(shell echo $$(($(INDEX)*20/$(NB)))))
|
||||||
|
@$(eval PERCENT=$(shell echo $$(($(INDEX)*100/$(NB)))))
|
||||||
|
@$(eval COLOR=$(shell echo $$(($(PERCENT)%35+196))))
|
||||||
|
@$(eval TO_DO=$(shell echo $$((20-$(INDEX)*20/$(NB)))))
|
||||||
|
@printf "\r\e[38;5;11m⌛ MAKE %10.10s : %2d%% \e[48;5;%dm%*s\e[0m%*s\e[48;5;255m \e[0m \e[38;5;11m %*s\e[0m\e[K" $(NAME) $(PERCENT) $(COLOR) $(DONE) "" $(TO_DO) "" $(DELTA) "$@"
|
||||||
|
@$(CC) $(FLAGS) $(D_FLAGS) -MMD -c $< -o $@\
|
||||||
|
-I $(INC_DIR)\
|
||||||
|
-I $(LIBFT_INC)
|
||||||
|
@$(eval INDEX=$(shell echo $$(($(INDEX)+1))))
|
||||||
|
|
||||||
|
clean: cleanlib
|
||||||
|
@rm -rf $(OBJ_DIR)
|
||||||
|
@printf "\r\e[38;5;202m✖ clean $(NAME).\e[0m\e[K\n"
|
||||||
|
|
||||||
|
cleanlib:
|
||||||
|
@make -C $(LIBFT_DIR) clean
|
||||||
|
|
||||||
|
fclean: clean fcleanlib
|
||||||
|
@rm -f $(NAME)
|
||||||
|
@printf "\r\e[38;5;196m❌ fclean $(NAME).\e[0m\e[K\n"
|
||||||
|
|
||||||
|
fcleanlib: cleanlib
|
||||||
|
@make -C $(LIBFT_DIR) fclean
|
||||||
|
|
||||||
|
re: fclean all
|
||||||
|
|
||||||
|
relib: fcleanlib $(LIBFT_LIB)
|
||||||
|
|
||||||
|
test:
|
||||||
|
gcc -lft_malloc -L. -Iincludes -o myprogram main.c $(FLAGS)
|
||||||
|
|
||||||
|
.PHONY : fclean clean re relib cleanlib fcleanlib
|
||||||
|
|
||||||
|
-include $(OBJS:.o=.d)
|
||||||
1
malloc/auteur
Normal file
1
malloc/auteur
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
jhalford
|
||||||
34
malloc/includes/malloc.h
Normal file
34
malloc/includes/malloc.h
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
#ifndef MALLOC_H
|
||||||
|
# define MALLOC_H
|
||||||
|
|
||||||
|
#define malloc_n 20
|
||||||
|
#define malloc_m 100
|
||||||
|
#define malloc_N 100
|
||||||
|
#define malloc_M 500
|
||||||
|
#define malloc_magic 1234567
|
||||||
|
#define TINY(x) (x < (malloc_n + 1))
|
||||||
|
#define SMALL(x) (!TINY(x) && !LARGE(x))
|
||||||
|
#define LARGE(x) (malloc_m < x)
|
||||||
|
|
||||||
|
#include "../libft/includes/libft.h"
|
||||||
|
#include <sys/mman.h>
|
||||||
|
|
||||||
|
typedef struct s_header {
|
||||||
|
int size;
|
||||||
|
int magic;
|
||||||
|
} t_header;
|
||||||
|
|
||||||
|
typedef struct s_node {
|
||||||
|
int size;
|
||||||
|
struct s_node *next;
|
||||||
|
} t_node;
|
||||||
|
|
||||||
|
extern t_node *tiny_head;
|
||||||
|
extern t_node *small_head;
|
||||||
|
|
||||||
|
void free(void *ptr);
|
||||||
|
void *malloc(size_t size);
|
||||||
|
void *realloc(void *ptr, size_t size);
|
||||||
|
void show_alloc_mem(void);
|
||||||
|
|
||||||
|
#endif
|
||||||
1
malloc/libft
Submodule
1
malloc/libft
Submodule
|
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit b9c0c4cbb1adf15f11e681951500f23ad1f34c60
|
||||||
1
malloc/libft_malloc.so
Symbolic link
1
malloc/libft_malloc.so
Symbolic link
|
|
@ -0,0 +1 @@
|
||||||
|
libft_malloc_x86_64_Darwin.so
|
||||||
BIN
malloc/libft_malloc_x86_64_Darwin.so
Executable file
BIN
malloc/libft_malloc_x86_64_Darwin.so
Executable file
Binary file not shown.
13
malloc/main.c
Normal file
13
malloc/main.c
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
#include "malloc.h"
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
void *ptr;
|
||||||
|
|
||||||
|
ptr = malloc(10);
|
||||||
|
ptr = malloc(42);
|
||||||
|
ptr = malloc(3);
|
||||||
|
ptr = malloc(15);
|
||||||
|
show_alloc_mem();
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
BIN
malloc/myprogram
Executable file
BIN
malloc/myprogram
Executable file
Binary file not shown.
9
malloc/src/free.c
Normal file
9
malloc/src/free.c
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
#include "malloc.h"
|
||||||
|
|
||||||
|
void free(void *ptr)
|
||||||
|
{
|
||||||
|
t_header *hptr;
|
||||||
|
|
||||||
|
hptr = ptr - sizeof(t_header);
|
||||||
|
ft_printf("hptr->magic = [%i]\n", hptr->magic);
|
||||||
|
}
|
||||||
51
malloc/src/malloc.c
Normal file
51
malloc/src/malloc.c
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
#include "malloc.h"
|
||||||
|
|
||||||
|
t_node *tiny_zone = NULL;
|
||||||
|
t_node *small_zone = NULL;
|
||||||
|
t_node *tiny_alloc = NULL;
|
||||||
|
t_node *small_alloc = NULL;
|
||||||
|
|
||||||
|
t_node **find_node(t_node **node, size_t size)
|
||||||
|
{
|
||||||
|
while (*node && (size_t)(*node)->size < sizeof(t_header) + size)
|
||||||
|
node = &(*node)->next;
|
||||||
|
return (node);
|
||||||
|
}
|
||||||
|
|
||||||
|
void add_chunk(t_node **node_ref, size_t size)
|
||||||
|
{
|
||||||
|
while (*node_ref)
|
||||||
|
node_ref = &(*node_ref)->next;
|
||||||
|
*node_ref = mmap(NULL, malloc_N, PROT_READ|PROT_WRITE,
|
||||||
|
MAP_ANON|MAP_PRIVATE, -1, 0);
|
||||||
|
(*node_ref)->size = (TINY(size) ? malloc_N : malloc_M) + sizeof(t_node);
|
||||||
|
(*node_ref)->next = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *split_node(t_node **node, size_t size)
|
||||||
|
{
|
||||||
|
t_header *hptr;
|
||||||
|
int free_size;
|
||||||
|
|
||||||
|
free_size = (*node)->size;
|
||||||
|
hptr = *(t_header**)node;
|
||||||
|
*node = *node + size + sizeof(*hptr);
|
||||||
|
(*node)->size = free_size - size - sizeof(*hptr);
|
||||||
|
hptr->size = size;
|
||||||
|
hptr->magic = malloc_magic;
|
||||||
|
return (hptr + sizeof(*hptr));
|
||||||
|
}
|
||||||
|
|
||||||
|
void *malloc(size_t size)
|
||||||
|
{
|
||||||
|
t_node **zone_ref;
|
||||||
|
t_node **node_ref;
|
||||||
|
void *ptr;
|
||||||
|
|
||||||
|
printf("malloc(%zu) was called\n", size);
|
||||||
|
zone_ref = TINY(size) ? &tiny_zone : &small_zone;
|
||||||
|
while (!(*(node_ref = find_node(zone_ref, size))))
|
||||||
|
add_chunk(node_ref, size);
|
||||||
|
ptr = split_node(node_ref, size);
|
||||||
|
return (ptr);
|
||||||
|
}
|
||||||
30
malloc/src/show_alloc_mem.c
Normal file
30
malloc/src/show_alloc_mem.c
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
#include "malloc.h"
|
||||||
|
|
||||||
|
t_node *tiny_zone;
|
||||||
|
t_node *small_zone;
|
||||||
|
|
||||||
|
int show_free_zone(t_node *node)
|
||||||
|
{
|
||||||
|
int total;
|
||||||
|
|
||||||
|
total = 0;
|
||||||
|
while (node)
|
||||||
|
{
|
||||||
|
printf("%p - %p: %i bytes\n", node, node + node->size, node->size);
|
||||||
|
total += node->size;
|
||||||
|
node = node->next;
|
||||||
|
}
|
||||||
|
return (total);
|
||||||
|
}
|
||||||
|
|
||||||
|
void show_alloc_mem(void)
|
||||||
|
{
|
||||||
|
int total;
|
||||||
|
|
||||||
|
total = 0;
|
||||||
|
ft_putstr("TINY:\n");
|
||||||
|
total += show_free_zone(tiny_zone);
|
||||||
|
ft_putstr("SMALL:\n");
|
||||||
|
total += show_free_zone(small_zone);
|
||||||
|
printf("Total: %i bytes\n", total);
|
||||||
|
}
|
||||||
17
malloc/update_makefile.sh
Executable file
17
malloc/update_makefile.sh
Executable file
|
|
@ -0,0 +1,17 @@
|
||||||
|
MYPATH=$(pwd)
|
||||||
|
CUR_MAKEFILE=$MYPATH/Makefile
|
||||||
|
if [ -e $CUR_MAKEFILE ]
|
||||||
|
then
|
||||||
|
echo "regenerate Makefile"
|
||||||
|
sed "`grep -n 'SRC_BASE =' $CUR_MAKEFILE | sed 's/:.*//'`, \$d" $CUR_MAKEFILE > NEWMAKEFILE
|
||||||
|
grep 'SRC_BASE =' $CUR_MAKEFILE >> NEWMAKEFILE
|
||||||
|
expr "$(find ./src | grep "\.c" | sed -e 's/src\///' -e 's/\.\///' -e 's/$/\\/')" : "\(.*\).$" >> NEWMAKEFILE
|
||||||
|
echo "" >> NEWMAKEFILE
|
||||||
|
grep 'SRCS =' $CUR_MAKEFILE >> NEWMAKEFILE
|
||||||
|
sed "1, `grep -n 'SRCS =' $CUR_MAKEFILE | sed 's/:.*//'`d" $CUR_MAKEFILE >> NEWMAKEFILE
|
||||||
|
mv $CUR_MAKEFILE ~/Documents/.OLDMakefile
|
||||||
|
mv NEWMAKEFILE $CUR_MAKEFILE
|
||||||
|
echo "Makefile done (copy still alive in ~/Documents/.OLDMakefile)"
|
||||||
|
else
|
||||||
|
echo "Makefile not found."
|
||||||
|
fi
|
||||||
Loading…
Reference in a new issue