colorful debug framework

This commit is contained in:
Jack Halford 2017-02-18 20:11:41 +01:00
parent 28f582ad7f
commit c395e96cd5
15 changed files with 282 additions and 144 deletions

View file

@ -7,7 +7,7 @@ ARCH_NAME = libft_malloc_$(HOSTTYPE).so
CC = gcc
W_FLAGS = -Wall -Wextra -Werror
V_FLAGS = -fvisibility=hidden
V_FLAGS =
D_FLAGS =
FLAGS = $(W_FLAGS) $(V_FLAGS) $(D_FLAGS)
@ -22,9 +22,12 @@ INC_DIR = includes/
OBJ_DIR = objs/
SRC_BASE = \
error_lib.c\
free.c\
insert_node.c\
get_zones.c\
malloc.c\
node_lib.c\
realloc.c\
show_alloc_mem.c
SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE))
@ -85,6 +88,6 @@ relib: fcleanlib $(LIBFT_LIB)
test:
gcc -lft_malloc -L. -Iincludes -I$(LIBFT_INC) -o myprogram main.c
.PHONY : fclean clean re relib cleanlib fcleanlib
.PHONY : fclean clean re relib cleanlib fcleanlib $(LIBFT_LIB)
-include $(OBJS:.o=.d)

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 23:00:06 by jhalford #+# #+# */
/* Updated: 2017/02/17 23:00:10 by jhalford ### ########.fr */
/* Updated: 2017/02/18 18:53:22 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 23:00:24 by jhalford #+# #+# */
/* Updated: 2017/02/17 23:25:09 by jhalford ### ########.fr */
/* Updated: 2017/02/18 20:06:55 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,13 +18,14 @@
# define malloc_N (1 * getpagesize())
# define malloc_M (2 * getpagesize())
# define malloc_magic 1234567
# define HEADER_SIZE (sizeof(t_node))
# define HEADER_SIZE (sizeof(t_node) - alignof(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 <sys/mman.h>
#include <stdalign.h>
typedef struct s_header {
int size;
@ -34,6 +35,7 @@ typedef struct s_header {
typedef struct s_node {
int size;
struct s_node *next;
char data[1];
} t_node;
extern t_node *tiny_zone;
@ -50,7 +52,16 @@ void show_alloc_mem(void);
# pragma GCC visibility pop
void show_free_mem(void);
void insert_node(t_node **head, t_node *new);
void get_zones(t_node ***zone_ref, t_node ***alloc_ref, size_t size);
void insert_node(t_node **head, t_node *node);
int remove_node(t_node **head, t_node *node);
t_node *split_node(t_node **node, t_node **alloc, t_node **zone, size_t size);
t_node **find_node_firstfit(t_node **node, size_t size);
void show_free_mem(void);
void print_node(char fg[7], t_node *node);
void error_mmap(void);
void error_free_notalloc(void *ptr);
#endif

@ -1 +1 @@
Subproject commit a82ea94ef7f50f8396d7bf6f9c08ab4a7faa994d
Subproject commit 0ca8ca817f32fc0345ef93ef74a3abe2583bd89c

Binary file not shown.

View file

@ -1,23 +1,20 @@
#include <stdlib.h>
#include "includes/malloc.h"
#include <stdio.h>
int main(void)
{
void *ptr0 = malloc(8150);
void *ptr0 = malloc(4096);
show_alloc_mem();
printf("\n");
void *ptr1 = malloc(300);
void *ptr1 = malloc(16);
show_alloc_mem();
printf("\n");
/* free(ptr0); */
/* show_alloc_mem(); */
/* printf("\n"); */
free(ptr1);
free(ptr0);
show_alloc_mem();
printf("\n");
return (0);
void *ptr2 = malloc(16);
show_alloc_mem();
void *ptr3 = malloc(32);
show_alloc_mem();
}

View file

@ -1,5 +1,5 @@
#!/bin/sh
export DYLD_LIBRARY_PATH=.
export DYLD_LIBRARY_PATH="."
export DYLD_INSERT_LIBRARIES="libft_malloc.so"
export DYLD_FORCE_FLAT_NAMESPACE=1
$@

41
malloc/src/error_lib.c Normal file
View file

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* error_lib.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/18 19:34:23 by jhalford #+# #+# */
/* Updated: 2017/02/18 20:08:32 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "malloc_internal.h"
void error_free_notalloc(void *ptr)
{
int fd;
fd = 1;
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_putaddr_fd(ptr, fd);
ft_putendl_fd(": pointer being freed was not allocated"FG_DEFAULT, fd);
/* exit(134); */
}
void error_mmap(void)
{
int fd;
fd = 1;
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);
/* exit(134); */
}

View file

@ -6,25 +6,18 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 12:28:03 by jhalford #+# #+# */
/* Updated: 2017/02/17 23:02:10 by jhalford ### ########.fr */
/* Updated: 2017/02/18 20:10:24 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "malloc_internal.h"
int remove_node(t_node **head, t_node *node)
{
while (*head)
{
if (*head == node)
{
*head = (*head)->next;
return (0);
}
head = &(*head)->next;
}
return (1);
}
t_node *tiny_zone;
t_node *small_zone;
t_node *large_zone;
t_node *tiny_alloc;
t_node *small_alloc;
t_node *large_alloc;
int coalesce_nodes(t_node **head)
{
@ -45,14 +38,21 @@ void free(void *ptr)
{
t_node **zone_ref;
t_node **alloc_ref;
t_node *hptr;
t_node *node;
hptr = ptr - HEADER_SIZE;
zone_ref = TINY(hptr->size) ? &tiny_zone : &small_zone;
alloc_ref = TINY(hptr->size) ? &tiny_alloc : &small_alloc;
if (remove_node(alloc_ref, hptr))
printf("trying to free bad address");
insert_node(zone_ref, hptr);
hptr->size += HEADER_SIZE;
coalesce_nodes(zone_ref);
ft_putstr(FG_YELLOW"free(");
ft_putaddr(ptr);
ft_putendl(")"FG_DEFAULT);
if (!ptr)
return ;
node = ptr - HEADER_SIZE;
get_zones(&zone_ref, &alloc_ref, node->size);
if (remove_node(alloc_ref, node))
{
error_free_notalloc(ptr);
return ;
}
insert_node(zone_ref, node);
coalesce_nodes(zone_ref);
ft_putendl(BG_GREEN"SUCCESSFUL FREE"BG_DEFAULT);
}

36
malloc/src/get_zones.c Normal file
View file

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_zones.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/18 17:53:41 by jhalford #+# #+# */
/* Updated: 2017/02/18 20:06:53 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "malloc_internal.h"
t_node *tiny_zone = NULL;
t_node *small_zone = NULL;
t_node *large_zone = NULL;
t_node *tiny_alloc = NULL;
t_node *small_alloc = NULL;
t_node *large_alloc = NULL;
void get_zones(t_node ***zone_ref, t_node ***alloc_ref, size_t size)
{
if (LARGE(size))
{
*zone_ref = &large_zone;
*alloc_ref = &large_alloc;
}
else
{
*zone_ref = TINY(size) ? &tiny_zone : &small_zone;
*alloc_ref = TINY(size) ? &tiny_alloc : &small_alloc;
}
ft_putstr("zone @");
**zone_ref ? print_node(BG_MAGENTA, **zone_ref) : ft_putendl(" NULL");
}

View file

@ -6,54 +6,37 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 12:28:02 by jhalford #+# #+# */
/* Updated: 2017/02/17 23:02:14 by jhalford ### ########.fr */
/* Updated: 2017/02/18 20:08:49 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "malloc_internal.h"
t_node *tiny_zone = NULL;
t_node *small_zone = NULL;
t_node *large_zone = NULL;
t_node *tiny_alloc = NULL;
t_node *small_alloc = NULL;
t_node *large_alloc = NULL;
t_node *tiny_zone;
t_node *small_zone;
t_node *large_zone;
t_node *tiny_alloc;
t_node *small_alloc;
t_node *large_alloc;
t_node **find_node(t_node **node, size_t size)
{
while (*node && (size_t)(*node)->size < size + HEADER_SIZE)
node = &(*node)->next;
return (node);
}
void add_chunk(t_node **node_ref, size_t size)
void add_chunk(t_node **zone_ref, size_t size)
{
size_t chunk_size;
t_node *node;
while (*node_ref)
node_ref = &(*node_ref)->next;
/* while (*node_ref) */
/* node_ref = &(*node_ref)->next; */
if (LARGE(size))
chunk_size = size + HEADER_SIZE;
else
chunk_size = TINY(size) ? malloc_N : malloc_M;
*node_ref = mmap(NULL, chunk_size, PROT_READ|PROT_WRITE,
MAP_ANON|MAP_PRIVATE, -1, 0);
(*node_ref)->size = chunk_size;
(*node_ref)->next = NULL;
}
void *split_node(t_node **free, t_node **alloc, size_t size)
{
t_node *new_alloc;
int free_size;
free_size = (*free)->size;
new_alloc = *free;
*(void**)free += (size + HEADER_SIZE);
(*free)->size = free_size - (size + HEADER_SIZE);
new_alloc->size = size;
insert_node(alloc, new_alloc);
return ((void*)new_alloc + HEADER_SIZE);
if (!(node = mmap(NULL, chunk_size, PROT_READ|PROT_WRITE,
MAP_ANON|MAP_PRIVATE, -1, 0)))
error_mmap();
ft_putstr("nchunk@");
node->size = chunk_size;
insert_node(zone_ref, node);
print_node(BG_GREEN, node);
}
void *malloc(size_t size)
@ -61,21 +44,18 @@ void *malloc(size_t size)
t_node **zone_ref;
t_node **alloc_ref;
t_node **node_ref;
void *ptr;
t_node *node;
printf("malloc(%zu) called\n", size);
if (LARGE(size))
{
zone_ref = &large_zone;
alloc_ref = &large_alloc;
}
else
{
zone_ref = TINY(size) ? &tiny_zone : &small_zone;
alloc_ref = TINY(size) ? &tiny_alloc : &small_alloc;
}
while (!*(node_ref = find_node(zone_ref, size)))
add_chunk(node_ref, size);
ptr = split_node(node_ref, alloc_ref, size);
return (ptr);
ft_putstr(FG_YELLOW"malloc(");
ft_putnbr(size);
ft_putendl(")"FG_DEFAULT);
get_zones(&zone_ref, &alloc_ref, size);
while (!*(node_ref = find_node_firstfit(zone_ref, size)))
add_chunk(zone_ref, size);
ft_putstr("found @");
print_node(FG_GREEN, *node_ref);
node = split_node(node_ref, alloc_ref, zone_ref, size);
ft_putstr("touser@");
print_node(FG_RED, node);
return (node->data);
}

83
malloc/src/node_lib.c Normal file
View file

@ -0,0 +1,83 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* insert_node.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 12:28:15 by jhalford #+# #+# */
/* Updated: 2017/02/18 20:08:46 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "malloc_internal.h"
t_node **find_node_firstfit(t_node **node, size_t size)
{
/* if (*node) */
/* { */
/* ft_putstr("startf@"); */
/* print_node(BG_CYAN, *node); */
/* } */
while (*node && (size_t)(*node)->size < size + HEADER_SIZE)
{
node = &(*node)->next;
/* ft_putstr("firstf@"); */
/* print_node(FG_GREEN, *node); */
}
return (node);
}
t_node *split_node(t_node **node, t_node **alloc, t_node **zone, size_t size)
{
t_node *new_alloc;
int free_size;
free_size = (*node)->size;
new_alloc = *node;
/* ft_putstr("node->data @ ["); */
/* ft_putaddr((*node)->data); */
/* ft_putendl("]"); */
*node = (t_node*)((*node)->data + size);
/* ft_putstr("node @ ["); */
/* ft_putaddr(*node); */
/* ft_putendl("]"); */
(*node)->size = free_size - (size + HEADER_SIZE);
if ((*node)->size == 0)
remove_node(zone, *node);
new_alloc->size = size;
insert_node(alloc, new_alloc);
return (new_alloc);
}
int remove_node(t_node **head, t_node *node)
{
while (*head)
{
ft_putstr("looking for node; diff=[");
ft_putaddr(node);
ft_putstr(",");
ft_putaddr(*head);
ft_putendl("]");
if (*head == node)
{
*head = (*head)->next;
return (0);
}
head = &(*head)->next;
}
return (1);
}
void insert_node(t_node **head, t_node *new)
{
while (*head)
{
if (new < *head)
break ;
head = &(*head)->next;
}
new->next = *head;
*head = new;
}

View file

@ -1,28 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* insert_node.c :+: :+: :+: */
/* realloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 12:28:15 by jhalford #+# #+# */
/* Updated: 2017/02/17 23:02:17 by jhalford ### ########.fr */
/* Created: 2017/02/18 13:23:20 by jhalford #+# #+# */
/* Updated: 2017/02/18 18:08:11 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "malloc_internal.h"
void insert_node(t_node **head, t_node *new)
void *realloc(void *ptr, size_t size)
{
while (*head)
{
if (new < *head)
{
new->next = *head;
*head = new;
return ;
}
head = &(*head)->next;
}
*head = new;
ft_putendl("realloc called");
free(ptr);
return (malloc(size));
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 12:28:20 by jhalford #+# #+# */
/* Updated: 2017/02/17 23:13:52 by jhalford ### ########.fr */
/* Updated: 2017/02/18 19:58:20 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,49 +19,34 @@ t_node *tiny_alloc;
t_node *small_alloc;
t_node *large_alloc;
void print_free_mem(t_node *node)
void print_node(char color[7], t_node *node)
{
size_t size;
void *addr;
size = node->size;
addr = (void*)node;
printf("\t%p - %p : %4zu byte%c\n",
addr, (void*)addr + size, size, size > 1 ? 's' : 0);
}
void print_alloc_mem(t_node *node)
{
size_t size;
void *addr;
size = node->size;
addr = (void*)node + HEADER_SIZE;
printf("\t%p - %p : %4zu(+%zu) byte%c\n",
addr, (void*)addr + size, size, HEADER_SIZE, size > 1 ? 's' : 0);
ft_putstr("\t");
ft_putstr(color);
ft_putaddr(node->data);
ft_putstr(" - ");
ft_putaddr(node->data + node->size);
ft_putstr(FBG_DEFAULT" : ");
ft_putnbr(node->size);
ft_putendl(" bytes");
}
void show_alloc_zone(char *name, t_node *alloc, t_node *zone, size_t (*total)[3])
{
if (alloc || zone)
printf("%s", name);
/* printf("%s", FG_RED); */
ft_putstr(FG_RED);
ft_putstr(name);
while (alloc)
{
print_alloc_mem(alloc);
print_node(FG_RED, alloc);
(*total)[1] += alloc->size;
(*total)[2] += HEADER_SIZE;
alloc = alloc->next;
}
printf("%s", FG_GREEN);
while (zone)
{
print_free_mem(zone);
print_node(FG_GREEN, zone);
(*total)[0] += zone->size;
zone = zone->next;
}
printf("%s", FG_DEFAULT);
}
void show_alloc_mem(void)
@ -70,13 +55,11 @@ void show_alloc_mem(void)
total[0] = 0;
total[1] = 0;
total[2] = 0;
show_alloc_zone("TINY:", tiny_alloc, tiny_zone, &total);
show_alloc_zone("SMALL:", small_alloc, small_zone, &total);
show_alloc_zone("LARGE:", large_alloc, large_zone, &total);
printf("Total:");
printf("\t%7zu bytes free\n", total[0]);
printf("\t%7zu bytes allocated\n", total[1]);
printf("\t%7zu header bytes\n", total[2]);
printf("\t%7zu bytes mmap'd\n", total[0] + total[1] + total[2]);
printf("\t%7zu bytes mmap'd\n", total[0] + total[1]);
}

12
malloc/test0.c Normal file
View file

@ -0,0 +1,12 @@
#include "includes/malloc.h"
int main(void)
{
int i;
char *addr;
i = 0;
while (i < 1024)
i++;
return (0);
}