all good
This commit is contained in:
parent
a8fe30763e
commit
8e70deabc3
26 changed files with 362 additions and 155 deletions
|
|
@ -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/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
|
NAME = libft_malloc_$(HOSTTYPE).so
|
||||||
|
|
||||||
CC = gcc
|
CC = gcc
|
||||||
FLAGS = -Wall -Wextra -Werror -fPIC #-fsanitize=address
|
FLAGS = -Wall -Wextra -Werror -fvisibility=hidden -fPIC
|
||||||
MAIN_FLAGS = -shared
|
MAIN_FLAGS = -shared -fPIC
|
||||||
OBJ_FLAGS =
|
OBJ_FLAGS =
|
||||||
|
|
||||||
LEN_NAME = `printf "%s" $(NAME) |wc -c`
|
LEN_NAME = `printf "%s" $(NAME) |wc -c`
|
||||||
|
|
@ -30,16 +30,23 @@ INC_DIR = includes/
|
||||||
OBJ_DIR = objs/
|
OBJ_DIR = objs/
|
||||||
|
|
||||||
SRC_BASE = \
|
SRC_BASE = \
|
||||||
|
calloc.c\
|
||||||
error_lib.c\
|
error_lib.c\
|
||||||
free.c\
|
free.c\
|
||||||
|
ft_memcpy.c\
|
||||||
|
ft_memset.c\
|
||||||
ft_putchar.c\
|
ft_putchar.c\
|
||||||
ft_putnbr.c\
|
ft_putnbr.c\
|
||||||
ft_putstr.c\
|
ft_putstr.c\
|
||||||
hexdump.c\
|
hexdump.c\
|
||||||
|
interface.c\
|
||||||
|
interface_extended.c\
|
||||||
malloc.c\
|
malloc.c\
|
||||||
node_lib.c\
|
node_lib.c\
|
||||||
realloc.c\
|
realloc.c\
|
||||||
show_alloc_mem.c
|
reallocf.c\
|
||||||
|
show_alloc_mem.c\
|
||||||
|
valloc.c
|
||||||
|
|
||||||
SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE))
|
SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE))
|
||||||
OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o))
|
OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o))
|
||||||
|
|
@ -56,6 +63,7 @@ $(NAME): $(OBJ_DIR) $(OBJS)
|
||||||
@$(CC) $(OBJS) -o $(NAME) \
|
@$(CC) $(OBJS) -o $(NAME) \
|
||||||
-I $(INC_DIR) \
|
-I $(INC_DIR) \
|
||||||
$(LIBS) $(MAIN_FLAGS) $(FLAGS)
|
$(LIBS) $(MAIN_FLAGS) $(FLAGS)
|
||||||
|
@strip -x $@
|
||||||
@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)
|
||||||
|
|
@ -102,6 +110,6 @@ fclean: clean fcleanlib
|
||||||
|
|
||||||
re: fclean all
|
re: fclean all
|
||||||
|
|
||||||
.PHONY : fclean clean re relib cleanlib fcleanlib
|
.PHONY : fclean clean re relib cleanlib fcleanlib tests
|
||||||
|
|
||||||
-include $(OBJS:.o=.d)
|
-include $(OBJS:.o=.d)
|
||||||
|
|
|
||||||
11
malloc/do_test.sh
Executable file
11
malloc/do_test.sh
Executable file
|
|
@ -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
|
||||||
|
|
@ -6,27 +6,25 @@
|
||||||
/* 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/10/07 16:25:06 by jhalford ### ########.fr */
|
/* Updated: 2017/10/22 17:36:27 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#ifndef FT_MALLOC_H
|
#ifndef FT_MALLOC_H
|
||||||
# define FT_MALLOC_H
|
# define FT_MALLOC_H
|
||||||
|
|
||||||
#include "stdlib.h"
|
# include "stdlib.h"
|
||||||
|
|
||||||
# pragma GCC visibility push(default)
|
# pragma GCC visibility push(default)
|
||||||
|
|
||||||
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 *reallocf(void *ptr, size_t size);
|
||||||
|
void *calloc(size_t count, size_t size);
|
||||||
void show_alloc_mem(void);
|
void show_alloc_mem(void);
|
||||||
void dump_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
|
# pragma GCC visibility pop
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* 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/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_ISTINY(x) (x < (M_NTINY + 1))
|
||||||
# define M_ISSMALL(x) (!M_ISTINY(x) && !M_ISLARGE(x))
|
# define M_ISSMALL(x) (!M_ISTINY(x) && !M_ISLARGE(x))
|
||||||
# define M_ISLARGE(x) (M_NSMALL < x)
|
# define M_ISLARGE(x) (M_NSMALL < x)
|
||||||
|
|
||||||
# define NEXT(node) (node->islast ? NULL : (void*)node + node->size)
|
# define NEXT(node) (node->islast ? NULL : (void*)node + node->size)
|
||||||
|
|
||||||
# define FT_ABS(x) (((x) < 0) ? -(x) : x)
|
# define FT_ABS(x) (((x) < 0) ? -(x) : x)
|
||||||
|
|
||||||
/*
|
|
||||||
* DEBUG without malloc
|
|
||||||
*/
|
|
||||||
|
|
||||||
# define DP_N(n) ft_putnbr_fd(n, 2)
|
# define DP_N(n) ft_putnbr_fd(n, 2)
|
||||||
# define DP_H(n) ft_putnbr_hex_fd(n, 2)
|
# define DP_H(n) ft_putnbr_hex_fd(n, 2)
|
||||||
# define DP_C(n) ft_putchar_fd(n, 2)
|
# define DP_C(n) ft_putchar_fd(n, 2)
|
||||||
# define DP_S(n) ft_putstr_fd(n, 2)
|
# define DP_S(n) ft_putstr_fd(n, 2)
|
||||||
|
|
||||||
# define DGPID DP_S("===");DP_N(getpid());DP_S("===");
|
# define DGPID DP_S("===");DP_N(getpid());DP_S("===");
|
||||||
# define DGW(d) DGPID;d;DP_C('\n')
|
# define DGW(d) DGPID;d;DP_C('\n')
|
||||||
|
|
||||||
# define DGS(s) do { DGW(DP_S(" "s)); } while(0)
|
# 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 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 DGSH(s, n) do { DGW(DP_S(" "s"=");DP_H(n)); } while(0)
|
||||||
|
|
@ -63,12 +56,13 @@
|
||||||
# define BG_MAGENTA "\e[45m"
|
# define BG_MAGENTA "\e[45m"
|
||||||
# define BG_CYAN "\e[46m"
|
# define BG_CYAN "\e[46m"
|
||||||
# define BG_DEFAULT "\e[49m"
|
# define BG_DEFAULT "\e[49m"
|
||||||
|
|
||||||
# define FBG_DEFAULT FG_DEFAULT BG_DEFAULT
|
# define FBG_DEFAULT FG_DEFAULT BG_DEFAULT
|
||||||
|
|
||||||
#include <sys/mman.h>
|
# include <sys/mman.h>
|
||||||
#include <stdalign.h>
|
# include <stdalign.h>
|
||||||
#include <unistd.h>
|
# include <unistd.h>
|
||||||
|
# include <pthread.h>
|
||||||
|
# include "malloc.h"
|
||||||
|
|
||||||
typedef struct s_chunk {
|
typedef struct s_chunk {
|
||||||
struct s_chunk *next;
|
struct s_chunk *next;
|
||||||
|
|
@ -88,24 +82,30 @@ 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;
|
extern pthread_mutex_t g_mutex;
|
||||||
|
extern int g_malloc_debug;
|
||||||
/*
|
/*
|
||||||
* malloc_debug levels:
|
** malloc_debug levels:
|
||||||
* 1: show chunks after malloc() and free()
|
** 1: show malloc/free calls with arguments
|
||||||
* 2: show chunks before malloc() and free(), notify when coalescing.
|
** 2: show chunks before and after malloc/free, notify when coalescing.
|
||||||
* 3: show mmap() returns
|
** 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_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 *hexdump(void *addr, unsigned int offset, unsigned int size);
|
void *hexdump(void *addr, unsigned int offset,
|
||||||
void print_node(char fg[7], t_node *node);
|
unsigned int size);
|
||||||
|
void show_chunk(char *name, t_chunk *chunk, int dump);
|
||||||
|
|
||||||
void error_mmap(void);
|
void error_mmap(void);
|
||||||
void error_free_notalloc(void *ptr);
|
void error_free_notalloc(void *ptr);
|
||||||
|
|
@ -123,4 +123,7 @@ int ft_putnbr_hex(long n);
|
||||||
int ft_putnbr_hex_fd(long n, int fd);
|
int ft_putnbr_hex_fd(long n, int fd);
|
||||||
int ft_putnbr_loop(long n, int base, 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
|
#endif
|
||||||
|
|
|
||||||
7
malloc/run.sh
Executable file → Normal file
7
malloc/run.sh
Executable file → Normal file
|
|
@ -1,2 +1,5 @@
|
||||||
#!/bin/bash
|
#!/bin/sh
|
||||||
DYLD_LIBRARY_PATH=. DYLD_INSERT_LIBRARIES="libft_malloc.so" DYLD_FORCE_FLAT_NAMESPACE=1 $@
|
export DYLD_LIBRARY_PATH=.
|
||||||
|
export DYLD_INSERT_LIBRARIES="libft_malloc.so"
|
||||||
|
export DYLD_FORCE_FLAT_NAMESPACE=1
|
||||||
|
$@
|
||||||
|
|
|
||||||
27
malloc/srcs/calloc.c
Normal file
27
malloc/srcs/calloc.c
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* calloc.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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);
|
||||||
|
}
|
||||||
|
|
@ -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/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;
|
fd = 2;
|
||||||
ft_putstr_fd(FG_RED"(", fd);
|
ft_putstr_fd(FG_RED"(", fd);
|
||||||
ft_putnbr_fd(getpid(), fd);
|
ft_putnbr_fd(getpid(), fd);
|
||||||
ft_putstr_fd(", ??? ", fd);
|
|
||||||
ft_putstr_fd(")", fd);
|
ft_putstr_fd(")", fd);
|
||||||
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);
|
||||||
|
|
@ -33,7 +32,6 @@ void error_mmap(void)
|
||||||
fd = 2;
|
fd = 2;
|
||||||
ft_putstr_fd(FG_RED"(", fd);
|
ft_putstr_fd(FG_RED"(", fd);
|
||||||
ft_putnbr_fd(getpid(), fd);
|
ft_putnbr_fd(getpid(), 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);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,19 +6,20 @@
|
||||||
/* 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/10/07 16:29:44 by jhalford ### ########.fr */
|
/* Updated: 2017/10/22 17:32:14 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "malloc_internal.h"
|
#include "malloc_internal.h"
|
||||||
|
#include <dlfcn.h>
|
||||||
|
|
||||||
volatile int g_malloc_debug;
|
int g_malloc_debug;
|
||||||
|
t_chunk *g_zones[M_ZONES_MAX];
|
||||||
|
|
||||||
int coalesce_nodes(t_node *node)
|
int coalesce_nodes(t_node *node)
|
||||||
{
|
{
|
||||||
t_node *next;
|
t_node *next;
|
||||||
|
|
||||||
show_alloc_mem();
|
|
||||||
if (node->islast)
|
if (node->islast)
|
||||||
return (0);
|
return (0);
|
||||||
next = (void*)node + node->size;
|
next = (void*)node + node->size;
|
||||||
|
|
@ -35,33 +36,27 @@ int coalesce_nodes(t_node *node)
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ret_free(void *ptr)
|
int ft_free(void *ptr)
|
||||||
{
|
{
|
||||||
t_chunk *zone;
|
|
||||||
t_node *node;
|
t_node *node;
|
||||||
t_node *prev;
|
t_node *prev;
|
||||||
|
|
||||||
if (!ptr)
|
if (!ptr)
|
||||||
return (2);
|
return (2);
|
||||||
|
if (g_malloc_debug >= 2)
|
||||||
|
show_alloc_mem();
|
||||||
node = ptr - M_NODEHEAD;
|
node = ptr - M_NODEHEAD;
|
||||||
zone = *get_zone(node->size);
|
if (!((prev = find_prev_node(g_zones[M_TINY], node))
|
||||||
if (!(prev = find_prev_node(zone, 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);
|
return (1);
|
||||||
}
|
}
|
||||||
node->isfree = 1;
|
node->isfree = 1;
|
||||||
coalesce_nodes(prev);
|
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)
|
if (g_malloc_debug >= 2)
|
||||||
show_alloc_mem();
|
show_alloc_mem();
|
||||||
if (ret_free(ptr) == 1)
|
return (0);
|
||||||
error_free_notalloc(ptr);
|
|
||||||
if (g_malloc_debug >= 1)
|
|
||||||
show_alloc_mem();
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
28
malloc/srcs/ft_memcpy.c
Normal file
28
malloc/srcs/ft_memcpy.c
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_memcpy.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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);
|
||||||
|
}
|
||||||
23
malloc/srcs/ft_memset.c
Normal file
23
malloc/srcs/ft_memset.c
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_memset.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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);
|
||||||
|
}
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/10/07 17:07:41 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
56
malloc/srcs/interface.c
Normal file
56
malloc/srcs/interface.c
Normal file
|
|
@ -0,0 +1,56 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* interface.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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);
|
||||||
|
}
|
||||||
59
malloc/srcs/interface_extended.c
Normal file
59
malloc/srcs/interface_extended.c
Normal file
|
|
@ -0,0 +1,59 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* interface_extended.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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);
|
||||||
|
}
|
||||||
|
|
@ -6,20 +6,15 @@
|
||||||
/* 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/10/08 17:29:52 by jhalford ### ########.fr */
|
/* Updated: 2017/10/22 17:29:44 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "malloc_internal.h"
|
#include "malloc_internal.h"
|
||||||
|
|
||||||
t_chunk *g_zones[M_ZONES_MAX] =
|
t_chunk *g_zones[M_ZONES_MAX];
|
||||||
{
|
int g_malloc_debug;
|
||||||
NULL,
|
pthread_mutex_t g_mutex;
|
||||||
NULL,
|
|
||||||
NULL,
|
|
||||||
};
|
|
||||||
|
|
||||||
volatile int g_malloc_debug = 0;
|
|
||||||
|
|
||||||
t_chunk **get_zone(size_t size)
|
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)))
|
MAP_ANON | MAP_PRIVATE, -1, 0)))
|
||||||
error_mmap();
|
error_mmap();
|
||||||
if (g_malloc_debug >= 3)
|
if (g_malloc_debug >= 3)
|
||||||
DGSN("malloc_debug", g_malloc_debug);
|
DGSH("mmap returned", (long)new);
|
||||||
new->next = *chunk;
|
new->next = *chunk;
|
||||||
*chunk = new;
|
*chunk = new;
|
||||||
node = (t_node*)(*chunk + 1);
|
node = (t_node*)(*chunk + 1);
|
||||||
|
|
@ -61,13 +56,9 @@ void *ft_malloc(size_t size)
|
||||||
t_node *node;
|
t_node *node;
|
||||||
void *ret;
|
void *ret;
|
||||||
|
|
||||||
if (g_malloc_debug >= 1)
|
g_malloc_debug = 0;
|
||||||
DGSN("malloc called with size", size);
|
|
||||||
if (g_malloc_debug >= 2)
|
if (g_malloc_debug >= 2)
|
||||||
{
|
|
||||||
DGSN("malloc", size);
|
|
||||||
show_alloc_mem();
|
show_alloc_mem();
|
||||||
}
|
|
||||||
size += M_NODEHEAD;
|
size += M_NODEHEAD;
|
||||||
zone = get_zone(size);
|
zone = get_zone(size);
|
||||||
while (!(node = find_node_firstfit(*zone, size)))
|
while (!(node = find_node_firstfit(*zone, size)))
|
||||||
|
|
@ -75,9 +66,8 @@ void *ft_malloc(size_t size)
|
||||||
split_node(node, size);
|
split_node(node, size);
|
||||||
ret = (void*)(node + 1);
|
ret = (void*)(node + 1);
|
||||||
if (g_malloc_debug >= 1)
|
if (g_malloc_debug >= 1)
|
||||||
{
|
|
||||||
DGSH("user got ptr", (long)ret);
|
DGSH("user got ptr", (long)ret);
|
||||||
|
if (g_malloc_debug >= 2)
|
||||||
show_alloc_mem();
|
show_alloc_mem();
|
||||||
}
|
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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/10/07 17:09:11 by jhalford ### ########.fr */
|
/* Updated: 2017/10/22 17:17:14 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* 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/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)
|
void *ft_realloc(void *ptr, size_t size)
|
||||||
{
|
{
|
||||||
if (ret_free(ptr))
|
void *new;
|
||||||
return (NULL);
|
size_t old_size;
|
||||||
|
|
||||||
|
if (!ptr)
|
||||||
return (ft_malloc(size));
|
return (ft_malloc(size));
|
||||||
|
if (ft_free(ptr))
|
||||||
|
return (NULL);
|
||||||
|
old_size = ((t_node*)(ptr - M_NODEHEAD))->size;
|
||||||
|
if (!(new = ft_malloc(size)))
|
||||||
|
return (NULL);
|
||||||
|
ft_memcpy(new, ptr, old_size);
|
||||||
|
return (new);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
24
malloc/srcs/reallocf.c
Normal file
24
malloc/srcs/reallocf.c
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* reallocf.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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);
|
||||||
|
}
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* 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/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;
|
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);
|
|
||||||
}
|
|
||||||
|
|
|
||||||
13
malloc/srcs/valloc.c
Normal file
13
malloc/srcs/valloc.c
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* valloc.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2017/10/22 15:00:53 by jhalford #+# #+# */
|
||||||
|
/* Updated: 2017/10/22 17:11:32 by jhalford ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "malloc_internal.h"
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
#include "../includes/malloc.h"
|
#include "../includes/malloc.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
|
|
@ -6,6 +7,7 @@ int main(void)
|
||||||
char *addr;
|
char *addr;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
|
printf("check");
|
||||||
while (i < 1024)
|
while (i < 1024)
|
||||||
{
|
{
|
||||||
i++;
|
i++;
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
#include "../includes/malloc.h"
|
#include "../includes/malloc.h"
|
||||||
|
|
||||||
void *ft_malloc(size_t size);
|
void *malloc(size_t size);
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
@ -9,7 +9,7 @@ int main(void)
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < 1024)
|
while (i < 1024)
|
||||||
{
|
{
|
||||||
addr = (char*)ft_malloc(1024);
|
addr = (char*)malloc(1024);
|
||||||
addr[0] = 42;
|
addr[0] = 42;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,9 +8,9 @@ int main(void)
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < 1024)
|
while (i < 1024)
|
||||||
{
|
{
|
||||||
addr = (char*)ft_malloc(1024);
|
addr = (char*)malloc(1024);
|
||||||
addr[0] = 42;
|
addr[0] = 42;
|
||||||
ft_free(addr);
|
free(addr);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return (0);
|
return (0);
|
||||||
|
|
|
||||||
|
|
@ -1,26 +0,0 @@
|
||||||
#include "../includes/malloc.h"
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#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);
|
|
||||||
}
|
|
||||||
|
|
@ -15,10 +15,10 @@ int main(void)
|
||||||
char *addr1;
|
char *addr1;
|
||||||
char *addr3;
|
char *addr3;
|
||||||
|
|
||||||
addr1 = (char*)ft_malloc(16*M);
|
addr1 = (char*)malloc(16*M);
|
||||||
strcpy(addr1, "Bonjours\n");
|
strcpy(addr1, "Bonjours\n");
|
||||||
print(addr1);
|
print(addr1);
|
||||||
addr3 = (char*)ft_realloc(addr1, 128*M);
|
addr3 = (char*)realloc(addr1, 128*M);
|
||||||
addr3[127*M] = 42;
|
addr3[127*M] = 42;
|
||||||
print(addr3);
|
print(addr3);
|
||||||
return (0);
|
return (0);
|
||||||
|
|
|
||||||
|
|
@ -11,10 +11,10 @@ int main(void)
|
||||||
{
|
{
|
||||||
char *addr;
|
char *addr;
|
||||||
|
|
||||||
addr = ft_malloc(16);
|
addr = malloc(16);
|
||||||
ft_free(NULL);
|
free(NULL);
|
||||||
ft_free((void*)addr + 5);
|
free((void*)addr + 5);
|
||||||
if (ft_realloc((void*)addr + 5, 10) == NULL)
|
if (realloc((void*)addr + 5, 10) == NULL)
|
||||||
print("Bonjours\n");
|
print("Bonjours\n");
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,11 +2,11 @@
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
ft_malloc(1024);
|
malloc(1024);
|
||||||
ft_malloc(1024 * 32);
|
malloc(1024 * 32);
|
||||||
ft_malloc(1024 * 1024);
|
malloc(1024 * 1024);
|
||||||
ft_malloc(1024 * 1024 * 16);
|
malloc(1024 * 1024 * 16);
|
||||||
ft_malloc(1024 * 1024 * 128);
|
malloc(1024 * 1024 * 128);
|
||||||
show_alloc_mem();
|
show_alloc_mem();
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue