commit 5c78200d8f82d02a4b1a68912694b2b22d4763bd Author: Jack Halford Date: Thu Feb 16 01:26:55 2017 +0100 first commit diff --git a/malloc/.gitmodules b/malloc/.gitmodules new file mode 100644 index 00000000..49141b44 --- /dev/null +++ b/malloc/.gitmodules @@ -0,0 +1,3 @@ +[submodule "libft"] + path = libft + url = ../libft diff --git a/malloc/Makefile b/malloc/Makefile new file mode 100644 index 00000000..2b404140 --- /dev/null +++ b/malloc/Makefile @@ -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) diff --git a/malloc/auteur b/malloc/auteur new file mode 100644 index 00000000..b0e9923f --- /dev/null +++ b/malloc/auteur @@ -0,0 +1 @@ +jhalford diff --git a/malloc/includes/malloc.h b/malloc/includes/malloc.h new file mode 100644 index 00000000..8598e563 --- /dev/null +++ b/malloc/includes/malloc.h @@ -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 + +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 diff --git a/malloc/libft b/malloc/libft new file mode 160000 index 00000000..b9c0c4cb --- /dev/null +++ b/malloc/libft @@ -0,0 +1 @@ +Subproject commit b9c0c4cbb1adf15f11e681951500f23ad1f34c60 diff --git a/malloc/libft_malloc.so b/malloc/libft_malloc.so new file mode 120000 index 00000000..d6d22878 --- /dev/null +++ b/malloc/libft_malloc.so @@ -0,0 +1 @@ +libft_malloc_x86_64_Darwin.so \ No newline at end of file diff --git a/malloc/libft_malloc_x86_64_Darwin.so b/malloc/libft_malloc_x86_64_Darwin.so new file mode 100755 index 00000000..a9743133 Binary files /dev/null and b/malloc/libft_malloc_x86_64_Darwin.so differ diff --git a/malloc/main.c b/malloc/main.c new file mode 100644 index 00000000..d2b8fb26 --- /dev/null +++ b/malloc/main.c @@ -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); +} diff --git a/malloc/myprogram b/malloc/myprogram new file mode 100755 index 00000000..f11d0a53 Binary files /dev/null and b/malloc/myprogram differ diff --git a/malloc/src/free.c b/malloc/src/free.c new file mode 100644 index 00000000..e90c41d2 --- /dev/null +++ b/malloc/src/free.c @@ -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); +} diff --git a/malloc/src/malloc.c b/malloc/src/malloc.c new file mode 100644 index 00000000..21a2b294 --- /dev/null +++ b/malloc/src/malloc.c @@ -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); +} diff --git a/malloc/src/show_alloc_mem.c b/malloc/src/show_alloc_mem.c new file mode 100644 index 00000000..94cbd062 --- /dev/null +++ b/malloc/src/show_alloc_mem.c @@ -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); +} diff --git a/malloc/update_makefile.sh b/malloc/update_makefile.sh new file mode 100755 index 00000000..a1b3228d --- /dev/null +++ b/malloc/update_makefile.sh @@ -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