diff --git a/malloc/Makefile b/malloc/Makefile index 73bd761a..2197234c 100644 --- a/malloc/Makefile +++ b/malloc/Makefile @@ -6,7 +6,7 @@ # By: wescande +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2016/08/29 21:32:58 by wescande #+# #+# # -# Updated: 2017/10/07 13:15:40 by jhalford ### ########.fr # +# Updated: 2017/10/22 13:19:38 by jhalford ### ########.fr # # # # **************************************************************************** # @@ -18,8 +18,8 @@ NAME_BIS = libft_malloc.so NAME = libft_malloc_$(HOSTTYPE).so CC = gcc -FLAGS = -Wall -Wextra -Werror -fPIC #-fsanitize=address -MAIN_FLAGS = -shared +FLAGS = -Wall -Wextra -Werror -fvisibility=hidden -fPIC +MAIN_FLAGS = -shared -fPIC OBJ_FLAGS = LEN_NAME = `printf "%s" $(NAME) |wc -c` @@ -30,16 +30,23 @@ INC_DIR = includes/ OBJ_DIR = objs/ SRC_BASE = \ +calloc.c\ error_lib.c\ free.c\ +ft_memcpy.c\ +ft_memset.c\ ft_putchar.c\ ft_putnbr.c\ ft_putstr.c\ hexdump.c\ +interface.c\ +interface_extended.c\ malloc.c\ node_lib.c\ realloc.c\ -show_alloc_mem.c +reallocf.c\ +show_alloc_mem.c\ +valloc.c SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o)) @@ -56,6 +63,7 @@ $(NAME): $(OBJ_DIR) $(OBJS) @$(CC) $(OBJS) -o $(NAME) \ -I $(INC_DIR) \ $(LIBS) $(MAIN_FLAGS) $(FLAGS) + @strip -x $@ @printf "\r\033[38;5;117m✓ MAKE $(NAME)\033[0m\033[K\n" $(NAME_BIS): $(NAME) @@ -102,6 +110,6 @@ fclean: clean fcleanlib re: fclean all -.PHONY : fclean clean re relib cleanlib fcleanlib +.PHONY : fclean clean re relib cleanlib fcleanlib tests -include $(OBJS:.o=.d) diff --git a/malloc/do_test.sh b/malloc/do_test.sh new file mode 100755 index 00000000..e57343a6 --- /dev/null +++ b/malloc/do_test.sh @@ -0,0 +1,11 @@ +if [ -z $1 ] +then + echo "Need an arg"; + exit; +fi +if [ -z $2 ] +then + make && gcc -w -L. $1 -o .bin -lft_malloc && /usr/bin/time -l ./.bin +else + make && gcc -w -L. $1 -o .bin -lft_malloc && ./tests/run.sh /usr/bin/time -l ./.bin +fi diff --git a/malloc/includes/malloc.h b/malloc/includes/malloc.h index 18b91873..9b12cb46 100644 --- a/malloc/includes/malloc.h +++ b/malloc/includes/malloc.h @@ -6,27 +6,25 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/17 23:00:06 by jhalford #+# #+# */ -/* Updated: 2017/10/07 16:25:06 by jhalford ### ########.fr */ +/* Updated: 2017/10/22 17:36:27 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef FT_MALLOC_H # define FT_MALLOC_H -#include "stdlib.h" +# include "stdlib.h" # pragma GCC visibility push(default) -void ft_free(void *ptr); -void *ft_malloc(size_t size); -void *ft_realloc(void *ptr, size_t size); +void free(void *ptr); +void *malloc(size_t size); +void *realloc(void *ptr, size_t size); +void *reallocf(void *ptr, size_t size); +void *calloc(size_t count, size_t size); void show_alloc_mem(void); void dump_alloc_mem(void); -/* void *calloc(size_t count, size_t size); */ -/* void *reallocf(void *ptr, size_t size); */ -/* void *valloc(size_t size); */ - # pragma GCC visibility pop #endif diff --git a/malloc/includes/malloc_internal.h b/malloc/includes/malloc_internal.h index a3805e3e..17fe3c59 100644 --- a/malloc/includes/malloc_internal.h +++ b/malloc/includes/malloc_internal.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/17 23:00:24 by jhalford #+# #+# */ -/* Updated: 2017/10/08 18:16:21 by jhalford ### ########.fr */ +/* Updated: 2017/10/22 17:17:13 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,23 +23,16 @@ # define M_ISTINY(x) (x < (M_NTINY + 1)) # define M_ISSMALL(x) (!M_ISTINY(x) && !M_ISLARGE(x)) # define M_ISLARGE(x) (M_NSMALL < x) - # define NEXT(node) (node->islast ? NULL : (void*)node + node->size) # define FT_ABS(x) (((x) < 0) ? -(x) : x) -/* - * DEBUG without malloc - */ - # define DP_N(n) ft_putnbr_fd(n, 2) # define DP_H(n) ft_putnbr_hex_fd(n, 2) # define DP_C(n) ft_putchar_fd(n, 2) # define DP_S(n) ft_putstr_fd(n, 2) - # define DGPID DP_S("===");DP_N(getpid());DP_S("==="); # define DGW(d) DGPID;d;DP_C('\n') - # define DGS(s) do { DGW(DP_S(" "s)); } while(0) # define DGSN(s, n) do { DGW(DP_S(" "s"=");DP_N(n)); } while(0) # define DGSH(s, n) do { DGW(DP_S(" "s"=");DP_H(n)); } while(0) @@ -63,12 +56,13 @@ # define BG_MAGENTA "\e[45m" # define BG_CYAN "\e[46m" # define BG_DEFAULT "\e[49m" - # define FBG_DEFAULT FG_DEFAULT BG_DEFAULT -#include -#include -#include +# include +# include +# include +# include +# include "malloc.h" typedef struct s_chunk { struct s_chunk *next; @@ -87,40 +81,49 @@ enum e_zones { M_ZONES_MAX, }; -extern t_chunk *g_zones[M_ZONES_MAX]; -extern volatile int g_malloc_debug; +extern t_chunk *g_zones[M_ZONES_MAX]; +extern pthread_mutex_t g_mutex; +extern int g_malloc_debug; /* - * malloc_debug levels: - * 1: show chunks after malloc() and free() - * 2: show chunks before malloc() and free(), notify when coalescing. - * 3: show mmap() returns - */ +** malloc_debug levels: +** 1: show malloc/free calls with arguments +** 2: show chunks before and after malloc/free, notify when coalescing. +** 3: show mmap values +*/ -#include "malloc.h" +int ft_free(void *ptr); +void *ft_malloc(size_t size); +void *ft_realloc(void *ptr, size_t size); +void *ft_reallocf(void *ptr, size_t size); +void *ft_calloc(size_t count, size_t size); +int ret_free(void *ptr); -int ret_free(void *ptr); -t_chunk **get_zone(size_t size); -t_node *find_node_firstfit(t_chunk *chunk, size_t size); -t_node *find_prev_node(t_chunk *zone, t_node *node); -int split_node(t_node *node, size_t size); +t_chunk **get_zone(size_t size); +t_node *find_node_firstfit(t_chunk *chunk, size_t size); +t_node *find_prev_node(t_chunk *zone, t_node *node); +int split_node(t_node *node, size_t size); -void *hexdump(void *addr, unsigned int offset, unsigned int size); -void print_node(char fg[7], t_node *node); +void *hexdump(void *addr, unsigned int offset, + unsigned int size); +void show_chunk(char *name, t_chunk *chunk, int dump); -void error_mmap(void); -void error_free_notalloc(void *ptr); +void error_mmap(void); +void error_free_notalloc(void *ptr); -int ft_putchar(char c); -int ft_putchar_fd(char c, int fd); -int ft_putendl(char const *s); -int ft_putendl_fd(char const *s, int fd); -int ft_putstr(char const *s); -int ft_putstr_fd(char const *s, int fd); +int ft_putchar(char c); +int ft_putchar_fd(char c, int fd); +int ft_putendl(char const *s); +int ft_putendl_fd(char const *s, int fd); +int ft_putstr(char const *s); +int ft_putstr_fd(char const *s, int fd); -int ft_putnbr(long n); -int ft_putnbr_fd(long n, int fd); -int ft_putnbr_hex(long n); -int ft_putnbr_hex_fd(long n, int fd); -int ft_putnbr_loop(long n, int base, int fd); +int ft_putnbr(long n); +int ft_putnbr_fd(long n, int fd); +int ft_putnbr_hex(long n); +int ft_putnbr_hex_fd(long n, int fd); +int ft_putnbr_loop(long n, int base, int fd); + +void *ft_memcpy(void *dst, const void *src, size_t n); +void *ft_memset(void *b, int c, size_t len); #endif diff --git a/malloc/run.sh b/malloc/run.sh old mode 100755 new mode 100644 index 54d246dd..19faa239 --- a/malloc/run.sh +++ b/malloc/run.sh @@ -1,2 +1,5 @@ -#!/bin/bash -DYLD_LIBRARY_PATH=. DYLD_INSERT_LIBRARIES="libft_malloc.so" DYLD_FORCE_FLAT_NAMESPACE=1 $@ +#!/bin/sh +export DYLD_LIBRARY_PATH=. +export DYLD_INSERT_LIBRARIES="libft_malloc.so" +export DYLD_FORCE_FLAT_NAMESPACE=1 +$@ diff --git a/malloc/srcs/calloc.c b/malloc/srcs/calloc.c new file mode 100644 index 00000000..164fca5f --- /dev/null +++ b/malloc/srcs/calloc.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* calloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/10/22 14:35:06 by jhalford #+# #+# */ +/* Updated: 2017/10/22 17:30:13 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "malloc_internal.h" + +int g_malloc_debug; + +void *ft_calloc(size_t count, size_t size) +{ + void *ptr; + unsigned long big_size; + + big_size = count * size; + if (!(ptr = ft_malloc(big_size))) + return (ptr); + ft_memset(ptr, 0, big_size); + return (ptr); +} diff --git a/malloc/srcs/error_lib.c b/malloc/srcs/error_lib.c index 2fbe160f..6cf825d6 100644 --- a/malloc/srcs/error_lib.c +++ b/malloc/srcs/error_lib.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/18 19:34:23 by jhalford #+# #+# */ -/* Updated: 2017/10/07 17:06:51 by jhalford ### ########.fr */ +/* Updated: 2017/10/22 11:29:39 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,6 @@ void error_free_notalloc(void *ptr) fd = 2; ft_putstr_fd(FG_RED"(", fd); ft_putnbr_fd(getpid(), fd); - ft_putstr_fd(", ??? ", fd); ft_putstr_fd(")", fd); ft_putstr_fd(" malloc: error for object ", fd); ft_putnbr_hex_fd((long)ptr, fd); @@ -33,7 +32,6 @@ void error_mmap(void) fd = 2; ft_putstr_fd(FG_RED"(", fd); ft_putnbr_fd(getpid(), fd); - ft_putstr_fd(", ??? ", fd); ft_putstr_fd(")", fd); ft_putendl_fd(" malloc: mmap failed", fd); } diff --git a/malloc/srcs/free.c b/malloc/srcs/free.c index e08e38ce..01a071f0 100644 --- a/malloc/srcs/free.c +++ b/malloc/srcs/free.c @@ -6,19 +6,20 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/17 12:28:03 by jhalford #+# #+# */ -/* Updated: 2017/10/07 16:29:44 by jhalford ### ########.fr */ +/* Updated: 2017/10/22 17:32:14 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "malloc_internal.h" +#include -volatile int g_malloc_debug; +int g_malloc_debug; +t_chunk *g_zones[M_ZONES_MAX]; int coalesce_nodes(t_node *node) { t_node *next; - show_alloc_mem(); if (node->islast) return (0); next = (void*)node + node->size; @@ -35,33 +36,27 @@ int coalesce_nodes(t_node *node) return (0); } -int ret_free(void *ptr) +int ft_free(void *ptr) { - t_chunk *zone; t_node *node; t_node *prev; if (!ptr) return (2); + if (g_malloc_debug >= 2) + show_alloc_mem(); node = ptr - M_NODEHEAD; - zone = *get_zone(node->size); - if (!(prev = find_prev_node(zone, node))) + if (!((prev = find_prev_node(g_zones[M_TINY], node)) + || (prev = find_prev_node(g_zones[M_SMALL], node)) + || (prev = find_prev_node(g_zones[M_LARGE], node)))) { + if (g_malloc_debug >= 2) + error_free_notalloc(ptr); return (1); } node->isfree = 1; coalesce_nodes(prev); - return (0); -} - -void ft_free(void *ptr) -{ - if (g_malloc_debug >= 1) - DGSH("free called with addr", (long)ptr); if (g_malloc_debug >= 2) show_alloc_mem(); - if (ret_free(ptr) == 1) - error_free_notalloc(ptr); - if (g_malloc_debug >= 1) - show_alloc_mem(); + return (0); } diff --git a/malloc/srcs/ft_memcpy.c b/malloc/srcs/ft_memcpy.c new file mode 100644 index 00000000..1754c420 --- /dev/null +++ b/malloc/srcs/ft_memcpy.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/03 14:57:31 by jhalford #+# #+# */ +/* Updated: 2017/10/22 12:46:59 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "malloc_internal.h" + +void *ft_memcpy(void *dst, const void *src, size_t n) +{ + char *c1; + char *c2; + + if (n == 0 || dst == src) + return (dst); + c1 = (char *)dst; + c2 = (char *)src; + while (--n) + *c1++ = *c2++; + *c1 = *c2; + return (dst); +} diff --git a/malloc/srcs/ft_memset.c b/malloc/srcs/ft_memset.c new file mode 100644 index 00000000..cf02288e --- /dev/null +++ b/malloc/srcs/ft_memset.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/10/22 14:37:31 by jhalford #+# #+# */ +/* Updated: 2017/10/22 15:00:47 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "malloc_internal.h" + +void *ft_memset(void *b, int c, size_t len) +{ + size_t i; + + i = -1; + while (++i < len) + ((unsigned char *)b)[i] = (unsigned char)c; + return (b); +} diff --git a/malloc/srcs/ft_putstr.c b/malloc/srcs/ft_putstr.c index 58acc346..f09352b4 100644 --- a/malloc/srcs/ft_putstr.c +++ b/malloc/srcs/ft_putstr.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/07 17:07:41 by jhalford #+# #+# */ -/* Updated: 2017/10/07 17:08:06 by jhalford ### ########.fr */ +/* Updated: 2017/10/22 15:30:37 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/malloc/srcs/interface.c b/malloc/srcs/interface.c new file mode 100644 index 00000000..9b8cea12 --- /dev/null +++ b/malloc/srcs/interface.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* interface.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/10/22 13:25:32 by jhalford #+# #+# */ +/* Updated: 2017/10/22 17:34:54 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "malloc_internal.h" + +t_chunk *g_zones[M_ZONES_MAX] = +{ + NULL, + NULL, + NULL, +}; +int g_malloc_debug = 0; +pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER; + +void *malloc(size_t size) +{ + void *new_ptr; + + pthread_mutex_lock(&g_mutex); + if (g_malloc_debug >= 1) + DGS("malloc called"); + new_ptr = ft_malloc(size); + pthread_mutex_unlock(&g_mutex); + return (new_ptr); +} + +void free(void *ptr) +{ + pthread_mutex_lock(&g_mutex); + if (g_malloc_debug >= 1) + DGS("free called"); + ft_free(ptr); + pthread_mutex_unlock(&g_mutex); + return ; +} + +void *realloc(void *ptr, size_t size) +{ + void *new_ptr; + + pthread_mutex_lock(&g_mutex); + if (g_malloc_debug >= 1) + DGS("realloc called"); + new_ptr = ft_realloc(ptr, size); + pthread_mutex_unlock(&g_mutex); + return (new_ptr); +} diff --git a/malloc/srcs/interface_extended.c b/malloc/srcs/interface_extended.c new file mode 100644 index 00000000..f7d72d1f --- /dev/null +++ b/malloc/srcs/interface_extended.c @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* interface_extended.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/10/22 17:32:51 by jhalford #+# #+# */ +/* Updated: 2017/10/22 17:36:16 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "malloc_internal.h" + +t_chunk *g_zones[M_ZONES_MAX]; +int g_malloc_debug; +pthread_mutex_t g_mutex; + +void *reallocf(void *ptr, size_t size) +{ + void *new_ptr; + + pthread_mutex_lock(&g_mutex); + if (g_malloc_debug >= 1) + DGS("reallocf called"); + new_ptr = ft_reallocf(ptr, size); + pthread_mutex_unlock(&g_mutex); + return (new_ptr); +} + +void *calloc(size_t count, size_t size) +{ + void *new_ptr; + + pthread_mutex_lock(&g_mutex); + if (g_malloc_debug >= 1) + DGS("calloc called"); + new_ptr = ft_calloc(count, size); + pthread_mutex_unlock(&g_mutex); + return (new_ptr); +} + +void show_alloc_mem(void) +{ + pthread_mutex_lock(&g_mutex); + show_chunk("TINY: ", g_zones[M_TINY], 0); + show_chunk("SMALL: ", g_zones[M_SMALL], 0); + show_chunk("LARGE: ", g_zones[M_LARGE], 0); + pthread_mutex_unlock(&g_mutex); +} + +void dump_alloc_mem(void) +{ + pthread_mutex_lock(&g_mutex); + show_chunk("TINY: ", g_zones[M_TINY], 1); + show_chunk("SMALL: ", g_zones[M_SMALL], 1); + show_chunk("LARGE: ", g_zones[M_LARGE], 1); + pthread_mutex_unlock(&g_mutex); +} diff --git a/malloc/srcs/malloc.c b/malloc/srcs/malloc.c index 777123b7..0e7da332 100644 --- a/malloc/srcs/malloc.c +++ b/malloc/srcs/malloc.c @@ -6,20 +6,15 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/17 12:28:02 by jhalford #+# #+# */ -/* Updated: 2017/10/08 17:29:52 by jhalford ### ########.fr */ +/* Updated: 2017/10/22 17:29:44 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "malloc_internal.h" -t_chunk *g_zones[M_ZONES_MAX] = -{ - NULL, - NULL, - NULL, -}; - -volatile int g_malloc_debug = 0; +t_chunk *g_zones[M_ZONES_MAX]; +int g_malloc_debug; +pthread_mutex_t g_mutex; t_chunk **get_zone(size_t size) { @@ -46,7 +41,7 @@ void add_chunk(t_chunk **chunk, size_t size) MAP_ANON | MAP_PRIVATE, -1, 0))) error_mmap(); if (g_malloc_debug >= 3) - DGSN("malloc_debug", g_malloc_debug); + DGSH("mmap returned", (long)new); new->next = *chunk; *chunk = new; node = (t_node*)(*chunk + 1); @@ -61,13 +56,9 @@ void *ft_malloc(size_t size) t_node *node; void *ret; - if (g_malloc_debug >= 1) - DGSN("malloc called with size", size); + g_malloc_debug = 0; if (g_malloc_debug >= 2) - { - DGSN("malloc", size); show_alloc_mem(); - } size += M_NODEHEAD; zone = get_zone(size); while (!(node = find_node_firstfit(*zone, size))) @@ -75,9 +66,8 @@ void *ft_malloc(size_t size) split_node(node, size); ret = (void*)(node + 1); if (g_malloc_debug >= 1) - { DGSH("user got ptr", (long)ret); + if (g_malloc_debug >= 2) show_alloc_mem(); - } return (ret); } diff --git a/malloc/srcs/node_lib.c b/malloc/srcs/node_lib.c index 5ebc7966..d0b43668 100644 --- a/malloc/srcs/node_lib.c +++ b/malloc/srcs/node_lib.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/17 12:28:15 by jhalford #+# #+# */ -/* Updated: 2017/10/07 17:09:11 by jhalford ### ########.fr */ +/* Updated: 2017/10/22 17:17:14 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/malloc/srcs/realloc.c b/malloc/srcs/realloc.c index dd4739fb..0137f7a9 100644 --- a/malloc/srcs/realloc.c +++ b/malloc/srcs/realloc.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/18 13:23:20 by jhalford #+# #+# */ -/* Updated: 2017/10/07 17:08:46 by jhalford ### ########.fr */ +/* Updated: 2017/10/22 17:28:05 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,16 @@ void *ft_realloc(void *ptr, size_t size) { - if (ret_free(ptr)) + void *new; + size_t old_size; + + if (!ptr) + return (ft_malloc(size)); + if (ft_free(ptr)) return (NULL); - return (ft_malloc(size)); + old_size = ((t_node*)(ptr - M_NODEHEAD))->size; + if (!(new = ft_malloc(size))) + return (NULL); + ft_memcpy(new, ptr, old_size); + return (new); } diff --git a/malloc/srcs/reallocf.c b/malloc/srcs/reallocf.c new file mode 100644 index 00000000..f17ae8f5 --- /dev/null +++ b/malloc/srcs/reallocf.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* reallocf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/10/22 13:33:57 by jhalford #+# #+# */ +/* Updated: 2017/10/22 17:28:02 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "malloc_internal.h" + +extern pthread_mutex_t g_mutex; + +void *ft_reallocf(void *ptr, size_t size) +{ + void *new_ptr; + + if (!(new_ptr = ft_realloc(ptr, size))) + ft_free(ptr); + return (new_ptr); +} diff --git a/malloc/srcs/show_alloc_mem.c b/malloc/srcs/show_alloc_mem.c index c068a1dd..f77c14f3 100644 --- a/malloc/srcs/show_alloc_mem.c +++ b/malloc/srcs/show_alloc_mem.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/17 12:28:20 by jhalford #+# #+# */ -/* Updated: 2017/10/07 17:06:14 by jhalford ### ########.fr */ +/* Updated: 2017/10/22 13:37:38 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -54,17 +54,3 @@ void show_chunk(char *name, t_chunk *chunk, int dump) chunk = chunk->next; } } - -void show_alloc_mem(void) -{ - show_chunk("TINY: ", g_zones[M_TINY], 0); - show_chunk("SMALL: ", g_zones[M_SMALL], 0); - show_chunk("LARGE: ", g_zones[M_LARGE], 0); -} - -void dump_alloc_mem(void) -{ - show_chunk("TINY: ", g_zones[M_TINY], 1); - show_chunk("SMALL: ", g_zones[M_SMALL], 1); - show_chunk("LARGE: ", g_zones[M_LARGE], 1); -} diff --git a/malloc/srcs/valloc.c b/malloc/srcs/valloc.c new file mode 100644 index 00000000..48f456da --- /dev/null +++ b/malloc/srcs/valloc.c @@ -0,0 +1,13 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* valloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/10/22 15:00:53 by jhalford #+# #+# */ +/* Updated: 2017/10/22 17:11:32 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "malloc_internal.h" diff --git a/malloc/tests/test0.c b/malloc/tests/test0.c index af8a0efe..9db9c897 100644 --- a/malloc/tests/test0.c +++ b/malloc/tests/test0.c @@ -1,4 +1,5 @@ #include "../includes/malloc.h" +#include int main(void) { @@ -6,6 +7,7 @@ int main(void) char *addr; i = 0; + printf("check"); while (i < 1024) { i++; diff --git a/malloc/tests/test1.c b/malloc/tests/test1.c index b2172b62..69d28f35 100644 --- a/malloc/tests/test1.c +++ b/malloc/tests/test1.c @@ -1,6 +1,6 @@ #include "../includes/malloc.h" -void *ft_malloc(size_t size); +void *malloc(size_t size); int main(void) { int i; @@ -9,7 +9,7 @@ int main(void) i = 0; while (i < 1024) { - addr = (char*)ft_malloc(1024); + addr = (char*)malloc(1024); addr[0] = 42; i++; } diff --git a/malloc/tests/test2.c b/malloc/tests/test2.c index a9418b84..c65bdc0e 100644 --- a/malloc/tests/test2.c +++ b/malloc/tests/test2.c @@ -8,9 +8,9 @@ int main(void) i = 0; while (i < 1024) { - addr = (char*)ft_malloc(1024); + addr = (char*)malloc(1024); addr[0] = 42; - ft_free(addr); + free(addr); i++; } return (0); diff --git a/malloc/tests/test3.5.c b/malloc/tests/test3.5.c deleted file mode 100644 index 8e045aa2..00000000 --- a/malloc/tests/test3.5.c +++ /dev/null @@ -1,26 +0,0 @@ -#include "../includes/malloc.h" -#include -#include - -#define M (1024 * 1024) - -void print(char *s) -{ - write(1, s, strlen(s)); -} - -int main(void) -{ - char *addr1; - char *addr2; - char *addr3; - - addr1 = (char*)ft_malloc(16*M); - strcpy(addr1, "Bonjours\n"); - print(addr1); - addr2 = (char*)ft_malloc(16*M); - addr3 = (char*)ft_realloc(addr1, 128*M); - addr3[127*M] = 42; - print(addr3); - return (0); -} diff --git a/malloc/tests/test3.c b/malloc/tests/test3.c index 2100874d..8381fa06 100644 --- a/malloc/tests/test3.c +++ b/malloc/tests/test3.c @@ -15,10 +15,10 @@ int main(void) char *addr1; char *addr3; - addr1 = (char*)ft_malloc(16*M); + addr1 = (char*)malloc(16*M); strcpy(addr1, "Bonjours\n"); print(addr1); - addr3 = (char*)ft_realloc(addr1, 128*M); + addr3 = (char*)realloc(addr1, 128*M); addr3[127*M] = 42; print(addr3); return (0); diff --git a/malloc/tests/test4.c b/malloc/tests/test4.c index 5b433edf..9bb9f3d1 100644 --- a/malloc/tests/test4.c +++ b/malloc/tests/test4.c @@ -11,10 +11,10 @@ int main(void) { char *addr; - addr = ft_malloc(16); - ft_free(NULL); - ft_free((void*)addr + 5); - if (ft_realloc((void*)addr + 5, 10) == NULL) + addr = malloc(16); + free(NULL); + free((void*)addr + 5); + if (realloc((void*)addr + 5, 10) == NULL) print("Bonjours\n"); return (0); } diff --git a/malloc/tests/test5.c b/malloc/tests/test5.c index e1a9647d..e01aadb9 100644 --- a/malloc/tests/test5.c +++ b/malloc/tests/test5.c @@ -2,11 +2,11 @@ int main(void) { - ft_malloc(1024); - ft_malloc(1024 * 32); - ft_malloc(1024 * 1024); - ft_malloc(1024 * 1024 * 16); - ft_malloc(1024 * 1024 * 128); + malloc(1024); + malloc(1024 * 32); + malloc(1024 * 1024); + malloc(1024 * 1024 * 16); + malloc(1024 * 1024 * 128); show_alloc_mem(); return (0); }