diff --git a/malloc/Makefile b/malloc/Makefile index 0304ea74..90b545b5 100644 --- a/malloc/Makefile +++ b/malloc/Makefile @@ -6,8 +6,10 @@ NAME = libft_malloc.so ARCH_NAME = libft_malloc_$(HOSTTYPE).so CC = gcc -FLAGS = -Wall -Wextra -Werror -D_FLAGS = -g +W_FLAGS = -Wall -Wextra -Werror +V_FLAGS = -fvisibility=hidden +D_FLAGS = +FLAGS = $(W_FLAGS) $(V_FLAGS) $(D_FLAGS) DELTA = $$(echo "$$(tput cols)-47"|bc) @@ -34,13 +36,14 @@ all : @make -j $(NAME) $(NAME): $(LIBFT_LIB) $(OBJ_DIR) $(OBJS) - @$(CC) --shared $(FLAGS) $(D_FLAGS) \ + @$(CC) --shared $(FLAGS)\ -I $(INC_DIR) \ -I $(LIBFT_INC) \ $(LIBS) \ $(LIBFT_LIB) $(OBJS) \ -o $(ARCH_NAME) @ln -fs $(ARCH_NAME) $(NAME) + @strip -x $(NAME) @printf "\r\e[48;5;15;38;5;25m✅ MAKE $(ARCH_NAME)\e[0m\e[K\n" $(LIBFT_LIB): @@ -56,7 +59,7 @@ $(OBJ_DIR)%.o : $(SRC_DIR)%.c | $(OBJ_DIR) @$(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 $@\ + @$(CC) $(FLAGS) -MMD -c $< -o $@\ -I $(INC_DIR)\ -I $(LIBFT_INC) @$(eval INDEX=$(shell echo $$(($(INDEX)+1)))) @@ -80,7 +83,7 @@ re: fclean all relib: fcleanlib $(LIBFT_LIB) test: - gcc -lft_malloc -L. -Iincludes -o myprogram main.c + gcc -lft_malloc -L. -Iincludes -I$(LIBFT_INC) -o myprogram main.c .PHONY : fclean clean re relib cleanlib fcleanlib diff --git a/malloc/includes/malloc.h b/malloc/includes/malloc.h index 07a3c41f..482cabbc 100644 --- a/malloc/includes/malloc.h +++ b/malloc/includes/malloc.h @@ -5,47 +5,19 @@ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2017/02/17 12:28:05 by jhalford #+# #+# */ -/* Updated: 2017/02/17 13:17:57 by jhalford ### ########.fr */ +/* Created: 2017/02/17 23:00:06 by jhalford #+# #+# */ +/* Updated: 2017/02/17 23:00:10 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef MALLOC_H # define MALLOC_H -# define malloc_n 64 -# define malloc_m 1024 -# define malloc_N (1 * getpagesize()) -# define malloc_M (2 * getpagesize()) -# define malloc_magic 1234567 -# define HEADER_SIZE (sizeof(t_node)) -# 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_zone; -extern t_node *small_zone; -extern t_node *tiny_alloc; -extern t_node *small_alloc; +#include "stdlib.h" void free(void *ptr); void *malloc(size_t size); void *realloc(void *ptr, size_t size); void show_alloc_mem(void); -void show_free_mem(void); -void insert_node(t_node **head, t_node *new); #endif diff --git a/malloc/includes/malloc_internal.h b/malloc/includes/malloc_internal.h new file mode 100644 index 00000000..6120b12d --- /dev/null +++ b/malloc/includes/malloc_internal.h @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* malloc_internal.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/17 23:00:24 by jhalford #+# #+# */ +/* Updated: 2017/02/17 23:25:09 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MALLOC_INTERNAL_H +# define MALLOC_INTERNAL_H + +# define malloc_n 64 +# define malloc_m 1024 +# define malloc_N (1 * getpagesize()) +# define malloc_M (2 * getpagesize()) +# define malloc_magic 1234567 +# define HEADER_SIZE (sizeof(t_node)) +# define TINY(x) (x < (malloc_n + 1)) +# define SMALL(x) (!TINY(x) && !LARGE(x)) +# define LARGE(x) (malloc_m < x) + +#include "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_zone; +extern t_node *small_zone; +extern t_node *tiny_alloc; +extern t_node *small_alloc; + +# pragma GCC visibility push(default) + +void free(void *ptr); +void *malloc(size_t size); +void *realloc(void *ptr, size_t size); +void show_alloc_mem(void); + +# pragma GCC visibility pop + +void show_free_mem(void); +void insert_node(t_node **head, t_node *new); + +#endif diff --git a/malloc/libft b/malloc/libft index b9c0c4cb..a82ea94e 160000 --- a/malloc/libft +++ b/malloc/libft @@ -1 +1 @@ -Subproject commit b9c0c4cbb1adf15f11e681951500f23ad1f34c60 +Subproject commit a82ea94ef7f50f8396d7bf6f9c08ab4a7faa994d diff --git a/malloc/libft_malloc_x86_64_Darwin.so b/malloc/libft_malloc_x86_64_Darwin.so index 71181154..9c5adf87 100755 Binary files a/malloc/libft_malloc_x86_64_Darwin.so and b/malloc/libft_malloc_x86_64_Darwin.so differ diff --git a/malloc/main.c b/malloc/main.c index a4601ef8..9933afc1 100644 --- a/malloc/main.c +++ b/malloc/main.c @@ -1,10 +1,8 @@ -#include "malloc.h" +#include "includes/malloc.h" +#include int main(void) { - printf("pagesize=[%i]\n", getpagesize()); - printf("sizeof(long)=[%lu]\n", sizeof(long)); - void *ptr0 = malloc(8150); show_alloc_mem(); printf("\n"); diff --git a/malloc/run.sh b/malloc/run.sh new file mode 100755 index 00000000..19faa239 --- /dev/null +++ b/malloc/run.sh @@ -0,0 +1,5 @@ +#!/bin/sh +export DYLD_LIBRARY_PATH=. +export DYLD_INSERT_LIBRARIES="libft_malloc.so" +export DYLD_FORCE_FLAT_NAMESPACE=1 +$@ diff --git a/malloc/src/free.c b/malloc/src/free.c index 01483f5c..77f01321 100644 --- a/malloc/src/free.c +++ b/malloc/src/free.c @@ -6,11 +6,11 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/17 12:28:03 by jhalford #+# #+# */ -/* Updated: 2017/02/17 13:18:39 by jhalford ### ########.fr */ +/* Updated: 2017/02/17 23:02:10 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ -#include "malloc.h" +#include "malloc_internal.h" int remove_node(t_node **head, t_node *node) { diff --git a/malloc/src/insert_node.c b/malloc/src/insert_node.c index 7b16e788..ddbb1104 100644 --- a/malloc/src/insert_node.c +++ b/malloc/src/insert_node.c @@ -6,11 +6,11 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/17 12:28:15 by jhalford #+# #+# */ -/* Updated: 2017/02/17 12:28:15 by jhalford ### ########.fr */ +/* Updated: 2017/02/17 23:02:17 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ -#include "malloc.h" +#include "malloc_internal.h" void insert_node(t_node **head, t_node *new) { diff --git a/malloc/src/malloc.c b/malloc/src/malloc.c index 64baa2bf..bdde52c4 100644 --- a/malloc/src/malloc.c +++ b/malloc/src/malloc.c @@ -6,11 +6,11 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/17 12:28:02 by jhalford #+# #+# */ -/* Updated: 2017/02/17 13:18:34 by jhalford ### ########.fr */ +/* Updated: 2017/02/17 23:02:14 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ -#include "malloc.h" +#include "malloc_internal.h" t_node *tiny_zone = NULL; t_node *small_zone = NULL; @@ -63,7 +63,7 @@ void *malloc(size_t size) t_node **node_ref; void *ptr; - printf("malloc(%zu) was called\n", size); + printf("malloc(%zu) called\n", size); if (LARGE(size)) { zone_ref = &large_zone; diff --git a/malloc/src/show_alloc_mem.c b/malloc/src/show_alloc_mem.c index 16d1fd06..2a21ce08 100644 --- a/malloc/src/show_alloc_mem.c +++ b/malloc/src/show_alloc_mem.c @@ -6,11 +6,11 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/17 12:28:20 by jhalford #+# #+# */ -/* Updated: 2017/02/17 13:18:10 by jhalford ### ########.fr */ +/* Updated: 2017/02/17 23:13:52 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ -#include "malloc.h" +#include "malloc_internal.h" t_node *tiny_zone; t_node *small_zone; @@ -41,28 +41,27 @@ void print_alloc_mem(t_node *node) addr, (void*)addr + size, size, HEADER_SIZE, size > 1 ? 's' : 0); } -int show_zone(t_node *node, int is_free, size_t (*total)[3]) -{ - while (node) - { - is_free ? print_free_mem(node) : print_alloc_mem(node); - (*total)[is_free ? 0 : 1] += node->size; - (*total)[2] += is_free ? 0 : HEADER_SIZE; - node = node->next; - } - return (0); -} - void show_alloc_zone(char *name, t_node *alloc, t_node *zone, size_t (*total)[3]) { - if (!alloc && !zone) - return ; - ft_putstr(name); + if (alloc || zone) + printf("%s", name); + /* printf("%s", FG_RED); */ ft_putstr(FG_RED); - show_zone(alloc, 0, total); - ft_putstr(FG_GREEN); - show_zone(zone, 1, total); - ft_putstr(FG_DEFAULT); + while (alloc) + { + print_alloc_mem(alloc); + (*total)[1] += alloc->size; + (*total)[2] += HEADER_SIZE; + alloc = alloc->next; + } + printf("%s", FG_GREEN); + while (zone) + { + print_free_mem(zone); + (*total)[0] += zone->size; + zone = zone->next; + } + printf("%s", FG_DEFAULT); } void show_alloc_mem(void)