norme ok, still need to fix without ft_

This commit is contained in:
Jack Halford 2017-10-07 17:10:59 +02:00
parent a24acfd0a1
commit ac5300023b
21 changed files with 407 additions and 170 deletions

View file

@ -6,7 +6,7 @@
# By: wescande <wescande@student.42.fr> +#+ +:+ +#+ # # By: wescande <wescande@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2016/08/29 21:32:58 by wescande #+# #+# # # Created: 2016/08/29 21:32:58 by wescande #+# #+# #
# Updated: 2017/03/31 18:08:35 by jhalford ### ########.fr # # Updated: 2017/10/07 13:15:40 by jhalford ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -25,12 +25,6 @@ OBJ_FLAGS =
LEN_NAME = `printf "%s" $(NAME) |wc -c` LEN_NAME = `printf "%s" $(NAME) |wc -c`
DELTA = $$(echo "$$(tput cols)-31-$(LEN_NAME)"|bc) DELTA = $$(echo "$$(tput cols)-31-$(LEN_NAME)"|bc)
LIBFT_DIR = libft/
LIBFT_LIB = $(LIBFT_DIR)libft.a
LIBFT_INC = $(LIBFT_DIR)includes/
LIBS =
SRC_DIR = srcs/ SRC_DIR = srcs/
INC_DIR = includes/ INC_DIR = includes/
OBJ_DIR = objs/ OBJ_DIR = objs/
@ -38,7 +32,11 @@ OBJ_DIR = objs/
SRC_BASE = \ SRC_BASE = \
error_lib.c\ error_lib.c\
free.c\ free.c\
ft_putchar.c\
ft_putnbr.c\
ft_putstr.c\
get_zone.c\ get_zone.c\
hexdump.c\
malloc.c\ malloc.c\
node_lib.c\ node_lib.c\
realloc.c\ realloc.c\
@ -52,15 +50,13 @@ INDEX = 0
SHELL := /bin/bash SHELL := /bin/bash
all : all :
@make -C $(LIBFT_DIR)
@make -j $(NAME) @make -j $(NAME)
@make $(NAME_BIS) @make $(NAME_BIS)
$(NAME): $(LIBFT_LIB) $(OBJ_DIR) $(OBJS) $(NAME): $(OBJ_DIR) $(OBJS)
@$(CC) $(OBJS) -o $(NAME) \ @$(CC) $(OBJS) -o $(NAME) \
-I $(INC_DIR) \ -I $(INC_DIR) \
-I $(LIBFT_INC) \ $(LIBS) $(MAIN_FLAGS) $(FLAGS)
$(LIBS) $(LIBFT_LIB) $(MAIN_FLAGS) $(FLAGS)
@printf "\r\033[38;5;117m✓ MAKE $(NAME)\033[0m\033[K\n" @printf "\r\033[38;5;117m✓ MAKE $(NAME)\033[0m\033[K\n"
$(NAME_BIS): $(NAME) $(NAME_BIS): $(NAME)
@ -71,9 +67,6 @@ $(NAME_BIS): $(NAME)
@ln -s $(NAME) $(NAME_BIS) @ln -s $(NAME) $(NAME_BIS)
@printf "\r\033[38;5;117m✓ LINK $(NAME_BIS)\033[0m\033[K\n" @printf "\r\033[38;5;117m✓ LINK $(NAME_BIS)\033[0m\033[K\n"
$(LIBFT_LIB):
@make -C $(LIBFT_DIR)
$(OBJ_DIR) : $(OBJ_DIR) :
@mkdir -p $(OBJ_DIR) @mkdir -p $(OBJ_DIR)
@mkdir -p $(dir $(OBJS)) @mkdir -p $(dir $(OBJS))
@ -85,8 +78,7 @@ $(OBJ_DIR)%.o : $(SRC_DIR)%.c | $(OBJ_DIR)
@$(eval COLOR=$(shell list=(160 196 202 208 215 221 226 227 190 154 118 82 46); index=$$(($(PERCENT) * $${#list[@]} / 100)); echo "$${list[$$index]}")) @$(eval COLOR=$(shell list=(160 196 202 208 215 221 226 227 190 154 118 82 46); index=$$(($(PERCENT) * $${#list[@]} / 100)); echo "$${list[$$index]}"))
@printf "\r\033[38;5;%dm⌛ [%s]: %2d%% `printf '█%.0s' {0..$(DONE)}`%*s❙%*.*s\033[0m\033[K" $(COLOR) $(NAME) $(PERCENT) $(TO_DO) "" $(DELTA) $(DELTA) "$(shell echo "$@" | sed 's/^.*\///')" @printf "\r\033[38;5;%dm⌛ [%s]: %2d%% `printf '█%.0s' {0..$(DONE)}`%*s❙%*.*s\033[0m\033[K" $(COLOR) $(NAME) $(PERCENT) $(TO_DO) "" $(DELTA) $(DELTA) "$(shell echo "$@" | sed 's/^.*\///')"
@$(CC) $(FLAGS) $(OBJ_FLAG) -MMD -c $< -o $@\ @$(CC) $(FLAGS) $(OBJ_FLAG) -MMD -c $< -o $@\
-I $(INC_DIR)\ -I $(INC_DIR)
-I $(LIBFT_INC)
@$(eval INDEX=$(shell echo $$(($(INDEX)+1)))) @$(eval INDEX=$(shell echo $$(($(INDEX)+1))))
clean: cleanlib clean: cleanlib
@ -96,9 +88,6 @@ clean: cleanlib
printf "\r\033[38;5;202m✗ clean $(NAME).\033[0m\033[K\n"; \ printf "\r\033[38;5;202m✗ clean $(NAME).\033[0m\033[K\n"; \
fi; fi;
cleanlib:
@make -C $(LIBFT_DIR) clean
fclean: clean fcleanlib fclean: clean fcleanlib
@if [ -e $(NAME) ]; \ @if [ -e $(NAME) ]; \
then \ then \
@ -112,13 +101,8 @@ fclean: clean fcleanlib
fi; fi;
@$(foreach n, $(shell echo libft_malloc_*.so), if [ -f $n ]; then rm -rf $n; printf "\r\033[38;5;196m✗ delete old lib $n.\033[0m\033[K\n"; fi;) @$(foreach n, $(shell echo libft_malloc_*.so), if [ -f $n ]; then rm -rf $n; printf "\r\033[38;5;196m✗ delete old lib $n.\033[0m\033[K\n"; fi;)
fcleanlib: cleanlib
@make -C $(LIBFT_DIR) fclean
re: fclean all re: fclean all
relib: fcleanlib $(LIBFT_LIB)
.PHONY : fclean clean re relib cleanlib fcleanlib .PHONY : fclean clean re relib cleanlib fcleanlib
-include $(OBJS:.o=.d) -include $(OBJS:.o=.d)

View file

@ -6,24 +6,26 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 23:00:06 by jhalford #+# #+# */ /* Created: 2017/02/17 23:00:06 by jhalford #+# #+# */
/* Updated: 2017/03/06 17:43:23 by jhalford ### ########.fr */ /* Updated: 2017/10/07 16:25:06 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef MALLOC_H #ifndef FT_MALLOC_H
# define MALLOC_H # define FT_MALLOC_H
#include "stdlib.h" #include "stdlib.h"
# pragma GCC visibility push(default) # pragma GCC visibility push(default)
void *calloc(size_t count, size_t size); void ft_free(void *ptr);
void free(void *ptr); void *ft_malloc(size_t size);
void *malloc(size_t size); void *ft_realloc(void *ptr, size_t size);
/* void *realloc(void *ptr, 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 *reallocf(void *ptr, size_t size); */
/* void *valloc(size_t size); */ /* void *valloc(size_t size); */
void show_alloc_mem(void);
# pragma GCC visibility pop # pragma GCC visibility pop

View file

@ -6,37 +6,78 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 23:00:24 by jhalford #+# #+# */ /* Created: 2017/02/17 23:00:24 by jhalford #+# #+# */
/* Updated: 2017/03/01 12:03:35 by jhalford ### ########.fr */ /* Updated: 2017/10/07 17:08:37 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef MALLOC_INTERNAL_H #ifndef MALLOC_INTERNAL_H
# define MALLOC_INTERNAL_H # define MALLOC_INTERNAL_H
# define M_NTINY (64) # define M_NTINY ((size_t)64)
# define M_NSMALL (1024) # define M_NSMALL ((size_t)1024)
# define M_PAGEALIGN(x) ((x / getpagesize() + 1) * getpagesize()) # define M_PAGEALIGN(x) ((x / getpagesize() + 1) * getpagesize()) * 10
# define M_TINYCHUNK (M_PAGEALIGN(101 * M_NTINY)) # define M_TINYCHUNK (M_PAGEALIGN(101 * M_NTINY))
# define M_SMALLCHUNK (M_PAGEALIGN(101 * M_NSMALL)) # define M_SMALLCHUNK (M_PAGEALIGN(101 * M_NSMALL))
# define M_MAGIC 1234567 # define M_CHUNKHEAD (sizeof(t_chunk))
# define M_CHUNKHEAD (sizeof(t_chunk) - alignof(t_chunk)) # define M_NODEHEAD (sizeof(t_node))
# define M_NODEHEAD (sizeof(t_node) - alignof(t_node))
# define M_ISTINY(x) (x < (M_NTINY + 1)) # define M_ISTINY(x) (x < (M_NTINY + 1))
# define M_ISSMALL(x) (!M_TINY(x) && !M_LARGE(x)) # define M_ISSMALL(x) (!M_ISTINY(x) && !M_ISLARGE(x))
# define M_ISLARGE(x) (M_SMALL < 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)
# define FG_DEFAULT "\e[0m"
# define ON_BOLD "\e[1m"
# define ON_UNDERLINED "\e[4m"
# define ON_INVERTED "\e[7m"
# define FG_BLACK "\e[30m"
# define FG_RED "\e[31m"
# define FG_GREEN "\e[32m"
# define FG_YELLOW "\e[33m"
# define FG_BLUE "\e[34m"
# define FG_MAGENTA "\e[35m"
# define FG_CYAN "\e[36m"
# define BG_BLACK "\e[40m"
# define BG_RED "\e[41m"
# define BG_GREEN "\e[42m"
# define BG_YELLOW "\e[43m"
# define BG_BLUE "\e[44m"
# define BG_MAGENTA "\e[45m"
# define BG_CYAN "\e[46m"
# define BG_DEFAULT "\e[49m"
# define FBG_DEFAULT FG_DEFAULT BG_DEFAULT
#include "libft.h"
#include <sys/mman.h> #include <sys/mman.h>
#include <stdalign.h> #include <stdalign.h>
#include <unistd.h>
typedef struct s_chunk { typedef struct s_chunk {
struct s_chunk *next; struct s_chunk *next;
} t_chunk; } t_chunk;
typedef struct s_node { typedef struct s_node {
struct s_node *next;
size_t size; size_t size;
int isfree:1; unsigned int isfree:1;
unsigned int islast:1;
} t_node; } t_node;
enum e_zones { enum e_zones {
@ -47,17 +88,38 @@ enum e_zones {
}; };
extern t_chunk *g_zones[M_ZONES_MAX]; extern t_chunk *g_zones[M_ZONES_MAX];
extern volatile 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
*/
#include "malloc.h" #include "malloc.h"
t_chunk *get_zone(size_t size); 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_node_firstfit(t_chunk *chunk, size_t size);
t_node *find_prev_node(t_chunk *zone, t_node *node); t_node *find_prev_node(t_chunk *zone, t_node *node);
int split_node(t_node *node, size_t size); int split_node(t_node *node, size_t size);
void show_free_mem(void); void *hexdump(void *addr, unsigned int offset, unsigned int size);
void print_node(char fg[7], t_node *node); void print_node(char fg[7], t_node *node);
void error_mmap(void); void error_mmap(void);
void error_free_notalloc(void *ptr); 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_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);
#endif #endif

@ -1 +0,0 @@
Subproject commit 8c0961c50468d42c3527c208d7f4ae3c98646882

View file

@ -1,7 +1,2 @@
#!/bin/bash #!/bin/bash
if [[ `uname` == 'Darwin' ]]; then DYLD_LIBRARY_PATH=. DYLD_INSERT_LIBRARIES="libft_malloc.so" DYLD_FORCE_FLAT_NAMESPACE=1 $@
DYLD_LIBRARY_PATH=. DYLD_INSERT_LIBRARIES="libft_malloc.so" DYLD_FORCE_FLAT_NAMESPACE=1 $@
else
echo $PWD
LD_LIBRARY_PATH=$PWD/. $@
fi

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/18 19:34:23 by jhalford #+# #+# */ /* Created: 2017/02/18 19:34:23 by jhalford #+# #+# */
/* Updated: 2017/02/18 20:08:32 by jhalford ### ########.fr */ /* Updated: 2017/10/07 17:06:51 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,7 +24,6 @@ void error_free_notalloc(void *ptr)
ft_putstr_fd(" malloc: error for object ", fd); ft_putstr_fd(" malloc: error for object ", fd);
ft_putnbr_hex_fd((long)ptr, fd); ft_putnbr_hex_fd((long)ptr, fd);
ft_putendl_fd(": pointer being freed was not allocated"FG_DEFAULT, fd); ft_putendl_fd(": pointer being freed was not allocated"FG_DEFAULT, fd);
/* exit(134); */
} }
void error_mmap(void) void error_mmap(void)
@ -37,5 +36,4 @@ void error_mmap(void)
ft_putstr_fd(", ??? ", fd); ft_putstr_fd(", ??? ", fd);
ft_putstr_fd(")", fd); ft_putstr_fd(")", fd);
ft_putendl_fd(" malloc: mmap failed", fd); ft_putendl_fd(" malloc: mmap failed", fd);
/* exit(134); */
} }

View file

@ -6,48 +6,62 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 12:28:03 by jhalford #+# #+# */ /* Created: 2017/02/17 12:28:03 by jhalford #+# #+# */
/* Updated: 2017/03/01 12:04:36 by jhalford ### ########.fr */ /* Updated: 2017/10/07 16:29:44 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "malloc_internal.h" #include "malloc_internal.h"
volatile int g_malloc_debug;
int coalesce_nodes(t_node *node) int coalesce_nodes(t_node *node)
{ {
t_node *next; t_node *next;
if ((next = node->next) && next->isfree) show_alloc_mem();
if (node->islast)
return (0);
next = (void*)node + node->size;
if (node->isfree && next->isfree)
{ {
if (g_malloc_debug >= 2)
{
DGSH("coalescing 2 nodes at addr", (long)node);
DGSH("and addr", (long)next);
}
node->islast = next->islast;
node->size += next->size; node->size += next->size;
node->next = next->next;
} }
return (0); return (0);
} }
void free(void *ptr) int ret_free(void *ptr)
{ {
t_chunk *zone; t_chunk *zone;
t_node *node; t_node *node;
t_node *prev; t_node *prev;
/* DGSH("free i", (int)ptr); */
/* DGSH("free ui", (unsigned int)ptr); */
/* DGSH("free ll", (long long)ptr); */
DGSH("free ull", (unsigned long long)ptr);
if (!ptr) if (!ptr)
return ; return (2);
node = ptr - M_NODEHEAD; node = ptr - M_NODEHEAD;
DGSN("node->size", node->size); zone = *get_zone(node->size);
zone = get_zone(node->size);
DGSH("free", (long)ptr);
if (!(prev = find_prev_node(zone, node))) if (!(prev = find_prev_node(zone, node)))
{ {
error_free_notalloc(ptr); return (1);
return ;
} }
DGSH("free", (long)ptr); node->isfree = 1;
coalesce_nodes(node);
DGSH("free", (long)ptr);
coalesce_nodes(prev); coalesce_nodes(prev);
DGS("successful free"); 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();
} }

View file

@ -1,28 +1,23 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* get_zones.c :+: :+: :+: */ /* ft_putchar.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/18 17:53:41 by jhalford #+# #+# */ /* Created: 2017/10/07 17:07:04 by jhalford #+# #+# */
/* Updated: 2017/02/18 20:06:53 by jhalford ### ########.fr */ /* Updated: 2017/10/07 17:07:06 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "malloc_internal.h" #include "malloc_internal.h"
t_chunk *g_zones[M_ZONES_MAX] = int ft_putchar_fd(char c, int fd)
{ {
NULL, return (write(fd, &c, 1));
NULL, }
NULL,
}; int ft_putchar(char c)
{
t_chunk *get_zone(size_t size) return (write(1, &c, 1));
{
if (M_ISLARGE(size))
return (g_zones[M_LARGE]);
else
return (g_zones[M_ISTINY(size) ? M_TINY : M_SMALL]);
} }

44
malloc/srcs/ft_putnbr.c Normal file
View file

@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/07 17:07:00 by jhalford #+# #+# */
/* Updated: 2017/10/07 17:08:25 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "malloc_internal.h"
int ft_putnbr_loop(long n, int base, int fd)
{
if (n >= base)
ft_putnbr_loop(n / base, base, fd);
return (ft_putchar_fd("0123456789abcdef"[n % base], fd));
}
int ft_putnbr_hex_fd(long n, int fd)
{
ft_putstr_fd("0x", fd);
return (ft_putnbr_loop(n, 16, fd));
}
int ft_putnbr_fd(long n, int fd)
{
if (n < 0)
ft_putchar_fd('-', fd);
n = FT_ABS(n);
return (ft_putnbr_loop(n, 10, fd));
}
int ft_putnbr_hex(long n)
{
return (ft_putnbr_hex_fd(n, 1));
}
int ft_putnbr(long n)
{
return (ft_putnbr_fd(n, 1));
}

49
malloc/srcs/ft_putstr.c Normal file
View file

@ -0,0 +1,49 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/07 17:07:41 by jhalford #+# #+# */
/* Updated: 2017/10/07 17:08:06 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "malloc_internal.h"
size_t ft_strlen(const char *s)
{
int i;
i = 0;
if (!s)
return (0);
while (s[i])
i++;
return (i);
}
int ft_putendl_fd(char const *s, int fd)
{
char nl;
nl = '\n';
write(fd, s, ft_strlen(s));
return (write(fd, &nl, 1));
}
int ft_putendl(char const *s)
{
return (ft_putendl_fd(s, 1));
}
int ft_putstr_fd(char const *s, int fd)
{
return (write(fd, s, ft_strlen(s)));
}
int ft_putstr(char const *s)
{
return (write(1, s, ft_strlen(s)));
}

53
malloc/srcs/hexdump.c Normal file
View file

@ -0,0 +1,53 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hexdump.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/07 15:44:13 by jhalford #+# #+# */
/* Updated: 2017/10/07 17:09:29 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "malloc_internal.h"
int ft_dumpnbr_hex(char n)
{
if (n < 0xa)
ft_putchar('0');
return (ft_putnbr_loop(n, 16, 1));
}
static void print_hex_contents(void *addr, unsigned int size)
{
void *a;
a = addr;
while (a - addr < 16)
{
if ((a - addr) >= size)
break ;
else
ft_dumpnbr_hex(*(unsigned char*)a);
ft_putchar(' ');
a++;
}
}
void *hexdump(void *addr, unsigned int offset, unsigned int size)
{
void *a;
addr += offset;
a = addr;
if (addr == NULL)
return (addr);
while ((a - addr) < size)
{
print_hex_contents(a, (size - (a - addr)));
ft_putchar('\n');
a += 16;
}
return (addr);
}

View file

@ -6,12 +6,31 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 12:28:02 by jhalford #+# #+# */ /* Created: 2017/02/17 12:28:02 by jhalford #+# #+# */
/* Updated: 2017/03/01 12:15:24 by jhalford ### ########.fr */ /* Updated: 2017/10/07 17:10:38 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "malloc_internal.h" #include "malloc_internal.h"
t_chunk *g_zones[M_ZONES_MAX] =
{
NULL,
NULL,
NULL,
};
volatile int g_malloc_debug = 0;
t_chunk **get_zone(size_t size)
{
if (M_ISLARGE(size))
return (&g_zones[M_LARGE]);
else if (M_ISSMALL(size))
return (&g_zones[M_SMALL]);
else
return (&g_zones[M_TINY]);
}
void add_chunk(t_chunk **chunk, size_t size) void add_chunk(t_chunk **chunk, size_t size)
{ {
int chunk_size; int chunk_size;
@ -22,31 +41,44 @@ void add_chunk(t_chunk **chunk, size_t size)
chunk_size = M_PAGEALIGN(size); chunk_size = M_PAGEALIGN(size);
else else
chunk_size = M_ISTINY(size) ? M_TINYCHUNK : M_SMALLCHUNK; chunk_size = M_ISTINY(size) ? M_TINYCHUNK : M_SMALLCHUNK;
if (!(new = mmap(NULL, chunk_size, PROT_READ|PROT_WRITE, chunk_size += M_CHUNKHEAD;
MAP_ANON|MAP_PRIVATE, -1, 0))) if (!(new = mmap(NULL, chunk_size, PROT_READ | PROT_WRITE,
MAP_ANON | MAP_PRIVATE, -1, 0)))
error_mmap(); error_mmap();
if (g_malloc_debug >= 3)
DGSN("malloc_debug", g_malloc_debug);
new->next = *chunk; new->next = *chunk;
*chunk = new; *chunk = new;
node = (t_node*)(*chunk + 1); node = (t_node*)(*chunk + 1);
node->size = chunk_size - M_CHUNKHEAD; node->size = chunk_size - M_CHUNKHEAD;
node->next = 0;
node->isfree = 1; node->isfree = 1;
node->islast = 1;
} }
void *malloc(size_t size) void *ft_malloc(size_t size)
{ {
t_chunk *zone; t_chunk **zone;
t_node *node; t_node *node;
void *ret; void *ret;
g_malloc_debug = 1;
if (g_malloc_debug >= 1)
DGSN("malloc called with size", size);
if (g_malloc_debug >= 2)
{
DGSN("malloc", size); DGSN("malloc", size);
show_alloc_mem();
}
size += M_NODEHEAD; size += M_NODEHEAD;
zone = get_zone(size); zone = get_zone(size);
DGSH("zone", (long)zone); while (!(node = find_node_firstfit(*zone, size)))
while (!(node = find_node_firstfit(zone, size))) add_chunk(zone, size);
add_chunk(&zone, size);
split_node(node, size); split_node(node, size);
ret = (void*)node + M_NODEHEAD; ret = (void*)(node + 1);
DGSH("to user", (long)ret); if (g_malloc_debug >= 1)
{
DGSH("user got ptr", (long)ret);
show_alloc_mem();
}
return (ret); return (ret);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 12:28:15 by jhalford #+# #+# */ /* Created: 2017/02/17 12:28:15 by jhalford #+# #+# */
/* Updated: 2017/03/01 12:04:08 by jhalford ### ########.fr */ /* Updated: 2017/10/07 17:09:11 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,9 +19,11 @@ t_node *find_node_firstfit(t_chunk *chunk, size_t size)
if (!chunk) if (!chunk)
return (NULL); return (NULL);
node = (t_node*)(chunk + 1); node = (t_node*)(chunk + 1);
while (node && !(node->isfree && node->size >= size)) while (node)
{ {
node = node->next; if (node->isfree && node->size >= size)
break ;
node = NEXT(node);
} }
return (node); return (node);
} }
@ -30,13 +32,15 @@ int split_node(t_node *node, size_t size)
{ {
t_node *new_node; t_node *new_node;
new_node = (void*)node + size;
new_node->next = node->next;
new_node->size = node->next - new_node;
new_node->isfree = 1;
node->next = new_node;
node->size -= new_node->size;
node->isfree = 0; node->isfree = 0;
if (node->size - size <= M_NODEHEAD)
return (0);
new_node = (void*)node + size;
new_node->size = node->size - size;
new_node->isfree = 1;
new_node->islast = node->islast;
node->islast = 0;
node->size = size;
return (0); return (0);
} }
@ -54,7 +58,7 @@ t_node *find_prev_node(t_chunk *zone, t_node *node)
if (n == node) if (n == node)
return (prev); return (prev);
prev = n; prev = n;
n = n->next; n = NEXT(n);
} }
zone = zone->next; zone = zone->next;
} }

View file

@ -6,15 +6,15 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/18 13:23:20 by jhalford #+# #+# */ /* Created: 2017/02/18 13:23:20 by jhalford #+# #+# */
/* Updated: 2017/03/01 12:15:31 by jhalford ### ########.fr */ /* Updated: 2017/10/07 17:08:46 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "malloc_internal.h" #include "malloc_internal.h"
void *realloc(void *ptr, size_t size) void *ft_realloc(void *ptr, size_t size)
{ {
ft_putendl("realloc called"); if (ret_free(ptr))
free(ptr); return (NULL);
return (malloc(size)); return (ft_malloc(size));
} }

View file

@ -6,60 +6,65 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 12:28:20 by jhalford #+# #+# */ /* Created: 2017/02/17 12:28:20 by jhalford #+# #+# */
/* Updated: 2017/02/21 16:37:55 by jhalford ### ########.fr */ /* Updated: 2017/10/07 17:06:14 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "malloc_internal.h" #include "malloc_internal.h"
t_node *tiny_zone; t_chunk *g_zones[M_ZONES_MAX];
t_node *small_zone;
t_node *large_zone;
t_node *tiny_alloc;
t_node *small_alloc;
t_node *large_alloc;
void print_node(char color[7], t_node *node) void print_node(char color[7], t_node *node)
{ {
ft_putstr("\t"); ft_putstr("\t");
ft_putstr(color); ft_putstr(color);
/* ft_putnbr_hex((long)node->data); */ ft_putnbr_hex((long)(node));
ft_putstr(" - "); ft_putstr(" - ");
/* ft_putnbr_hex((long)node->data + node->size); */ ft_putnbr_hex((long)(node + 1));
ft_putstr(" - ");
ft_putnbr_hex((long)((void*)node + node->size));
ft_putstr(FBG_DEFAULT" : "); ft_putstr(FBG_DEFAULT" : ");
ft_putnbr(node->size); ft_putnbr(node->size);
ft_putendl(" bytes"); ft_putstr(" bytes");
ft_putchar('\n');
} }
void show_alloc_zone(char *name, t_node *alloc, t_node *zone, size_t (*total)[3]) void show_chunk(char *name, t_chunk *chunk, int dump)
{ {
if (alloc || zone) t_node *node;
ft_putstr(name); ft_putstr(name);
while (alloc) if (!chunk)
{ {
print_node(FG_RED, alloc); ft_putstr("empty");
(*total)[1] += alloc->size; ft_putchar('\n');
alloc = alloc->next; return ;
} }
while (zone) while (chunk)
{ {
print_node(FG_GREEN, zone); ft_putchar('\n');
(*total)[0] += zone->size; node = (t_node*)(chunk + 1);
zone = zone->next; while (node)
{
print_node(node->isfree ? FG_GREEN : FG_RED, node);
if (dump && !node->isfree)
hexdump(node, M_NODEHEAD, node->size - M_NODEHEAD);
node = NEXT(node);
}
chunk = chunk->next;
} }
} }
void show_alloc_mem(void) void show_alloc_mem(void)
{ {
size_t total[3]; show_chunk("TINY: ", g_zones[M_TINY], 0);
show_chunk("SMALL: ", g_zones[M_SMALL], 0);
total[0] = 0; show_chunk("LARGE: ", g_zones[M_LARGE], 0);
total[1] = 0; }
show_alloc_zone("TINY:", tiny_alloc, tiny_zone, &total);
show_alloc_zone("SMALL:", small_alloc, small_zone, &total); void dump_alloc_mem(void)
show_alloc_zone("LARGE:", large_alloc, large_zone, &total); {
printf("Total:"); show_chunk("TINY: ", g_zones[M_TINY], 1);
printf("\t%7zu bytes free\n", total[0]); show_chunk("SMALL: ", g_zones[M_SMALL], 1);
printf("\t%7zu bytes allocated\n", total[1]); show_chunk("LARGE: ", g_zones[M_LARGE], 1);
printf("\t%7zu bytes mmap'd\n", total[0] + total[1]);
} }

View file

@ -1,5 +1,6 @@
#include "../includes/malloc.h" #include "../includes/malloc.h"
void *ft_malloc(size_t size);
int main(void) int main(void)
{ {
int i; int i;
@ -8,7 +9,7 @@ int main(void)
i = 0; i = 0;
while (i < 1024) while (i < 1024)
{ {
addr = (char*)malloc(1024); addr = (char*)ft_malloc(1024);
addr[0] = 42; addr[0] = 42;
i++; i++;
} }

View file

@ -8,9 +8,9 @@ int main(void)
i = 0; i = 0;
while (i < 1024) while (i < 1024)
{ {
addr = (char*)malloc(1024); addr = (char*)ft_malloc(1024);
addr[0] = 42; addr[0] = 42;
free(addr); ft_free(addr);
i++; i++;
} }
return (0); return (0);

View file

@ -15,11 +15,11 @@ int main(void)
char *addr2; char *addr2;
char *addr3; char *addr3;
addr1 = (char*)malloc(16*M); addr1 = (char*)ft_malloc(16*M);
strcpy(addr1, "Bonjours\n"); strcpy(addr1, "Bonjours\n");
print(addr1); print(addr1);
addr2 = (char*)malloc(16*M); addr2 = (char*)ft_malloc(16*M);
addr3 = (char*)realloc(addr1, 128*M); addr3 = (char*)ft_realloc(addr1, 128*M);
addr3[127*M] = 42; addr3[127*M] = 42;
print(addr3); print(addr3);
return (0); return (0);

View file

@ -15,10 +15,10 @@ int main(void)
char *addr1; char *addr1;
char *addr3; char *addr3;
addr1 = (char*)malloc(16*M); addr1 = (char*)ft_malloc(16*M);
strcpy(addr1, "Bonjours\n"); strcpy(addr1, "Bonjours\n");
print(addr1); print(addr1);
addr3 = (char*)realloc(addr1, 128*M); addr3 = (char*)ft_realloc(addr1, 128*M);
addr3[127*M] = 42; addr3[127*M] = 42;
print(addr3); print(addr3);
return (0); return (0);

View file

@ -11,10 +11,10 @@ int main(void)
{ {
char *addr; char *addr;
addr = malloc(16); addr = ft_malloc(16);
free(NULL); ft_free(NULL);
free((void*)addr + 5); ft_free((void*)addr + 5);
if (realloc((void*)addr + 5, 10) == NULL) if (ft_realloc((void*)addr + 5, 10) == NULL)
print("Bonjours\n"); print("Bonjours\n");
return (0); return (0);
} }

View file

@ -2,11 +2,11 @@
int main(void) int main(void)
{ {
malloc(1024); ft_malloc(1024);
malloc(1024 * 32); ft_malloc(1024 * 32);
malloc(1024 * 1024); ft_malloc(1024 * 1024);
malloc(1024 * 1024 * 16); ft_malloc(1024 * 1024 * 16);
malloc(1024 * 1024 * 128); ft_malloc(1024 * 1024 * 128);
show_alloc_mem(); show_alloc_mem();
return (0); return (0);
} }