free() done, coalescing done

This commit is contained in:
Jack Halford 2017-02-16 18:10:01 +01:00
parent 053c0773de
commit 1b8b22fbdf
9 changed files with 94 additions and 39 deletions

View file

@ -21,6 +21,7 @@ OBJ_DIR = objs/
SRC_BASE = \
free.c\
insert_node.c\
malloc.c\
show_alloc_mem.c
@ -79,7 +80,7 @@ re: fclean all
relib: fcleanlib $(LIBFT_LIB)
test:
gcc -lft_malloc -L. -Iincludes -o myprogram main.c $(FLAGS)
gcc -lft_malloc -L. -Iincludes -o myprogram main.c
.PHONY : fclean clean re relib cleanlib fcleanlib

View file

@ -36,5 +36,6 @@ void *malloc(size_t size);
void *realloc(void *ptr, size_t size);
void show_alloc_mem(void);
void show_free_mem(void);
void insert_node(t_node **head, t_node *new);
#endif

Binary file not shown.

View file

@ -2,26 +2,37 @@
int main(void)
{
void *ptr;
printf("pagesize=[%i]\n", getpagesize());
printf("sizeof(long)=[%lu]\n", sizeof(long));
printf("sizeof(t_node)=[%lu]\n", sizeof(t_node));
ptr = malloc(1 * sizeof(long));
show_alloc_mem();
ft_putchar('\n');
ptr = malloc(84 * sizeof(long));
void *ptr0 = malloc(16 * sizeof(long));
show_alloc_mem();
ft_putchar('\n');
printf("\n");
ptr = malloc(65 * sizeof(long));
void *ptr1 = malloc(32 * sizeof(long));
show_alloc_mem();
ft_putchar('\n');
printf("\n");
ptr = malloc(64 * sizeof(long));
/* void *ptr2 = malloc(64 * sizeof(long)); */
/* show_alloc_mem(); */
/* printf("\n"); */
/* void *ptr3 = malloc(20 * sizeof(long)); */
/* show_alloc_mem(); */
/* printf("\n"); */
free(ptr0);
show_alloc_mem();
ft_putchar('\n');
printf("\n");
free(ptr1);
show_alloc_mem();
printf("\n");
/* free(ptr2); */
/* show_alloc_mem(); */
/* printf("\n"); */
return (0);
}

Binary file not shown.

View file

@ -1,9 +1,49 @@
#include "malloc.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);
}
int coalesce_nodes(t_node **head)
{
while (*head)
{
if ((*head)->next == *(void**)head + 8 * (*head)->size)
{
(*head)->size += (*head)->next->size;
(*head)->next = (*head)->next->next;
}
else
head = &(*head)->next;
}
return (0);
}
void free(void *ptr)
{
t_header *hptr;
t_node **zone_ref;
t_node **alloc_ref;
t_node *hptr;
hptr = ptr - sizeof(t_header);
ft_printf("hptr->magic = [%i]\n", hptr->magic);
hptr = ptr - 8 * HEADER_SIZE;
zone_ref = TINY(hptr->size) ? &tiny_zone : &small_zone;
alloc_ref = TINY(hptr->size) ? &tiny_alloc : &small_alloc;
/* printf("ptr @ [%p]\n", ptr); */
/* printf("hptr @ [%p]\n", hptr); */
/* printf("len=[%i]\n", hptr->size); */
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);
}

16
malloc/src/insert_node.c Normal file
View file

@ -0,0 +1,16 @@
#include "malloc.h"
void insert_node(t_node **head, t_node *new)
{
while (*head)
{
if (new < *head)
{
new->next = *head;
*head = new;
return ;
}
head = &(*head)->next;
}
*head = new;
}

View file

@ -22,21 +22,6 @@ void add_chunk(t_node **node_ref, size_t size)
(*node_ref)->next = NULL;
}
void mem_insert(t_node **head, t_node *new)
{
while (*head)
{
if (new < *head)
{
new->next = *head;
*head = new;
return ;
}
head = &(*head)->next;
}
*head = new;
}
void *split_node(t_node **free, t_node **alloc, size_t size)
{
t_node *new_alloc;
@ -45,15 +30,16 @@ void *split_node(t_node **free, t_node **alloc, size_t size)
free_size = (*free)->size;
/* printf("split now size=[%zu]\n", size); */
/* printf("free @ [%p], size=[%i]\n", *free, (*free)->size); */
/* printf("size + sizeof = [%lu]\n", size + HEADER_SIZE); */
/* printf("size = [%lu]\n", size + HEADER_SIZE); */
/* printf("alloc @ [%p]\n", *alloc); */
/* fflush(stdout); */
new_alloc = *free;
*(void**)free += 8 * (size + HEADER_SIZE);
/* printf("free @ [%p], size=[%i]\n", *free, (*free)->size); */
(*free)->size = free_size - (size + HEADER_SIZE);
new_alloc->size = size;
mem_insert(alloc, new_alloc);
return (*alloc + HEADER_SIZE);
insert_node(alloc, new_alloc);
return ((void*)new_alloc + HEADER_SIZE * 8);
}
void *malloc(size_t size)

View file

@ -13,7 +13,7 @@ void print_free_mem(t_node *node)
size = node->size;
addr = (void*)node;
printf("\t%p - %p : %4zu byte%c\n",
addr, addr + 8 * size, size, size > 1 ? 's' : 0);
addr, (void*)addr + 8 * size, size, size > 1 ? 's' : 0);
}
void print_alloc_mem(t_node *node)
@ -22,9 +22,9 @@ void print_alloc_mem(t_node *node)
void *addr;
size = node->size;
addr = (void*)node + HEADER_SIZE;
addr = (void*)node + 8 * HEADER_SIZE;
printf("\t%p - %p : %4zu(+%zu) byte%c\n",
addr, addr + 8 * size, size, HEADER_SIZE, size > 1 ? 's' : 0);
addr, (void*)addr + 8 * size, size, HEADER_SIZE, size > 1 ? 's' : 0);
}
int show_zone(t_node *node, int is_free)
@ -35,7 +35,7 @@ int show_zone(t_node *node, int is_free)
while (node)
{
is_free ? print_free_mem(node) : print_alloc_mem(node);
total += node->size + HEADER_SIZE;
total += node->size + (is_free ? 0 : HEADER_SIZE);
node = node->next;
}
return (total);
@ -48,14 +48,14 @@ void show_alloc_mem(void)
free_total = 0;
alloc_total = 0;
if (tiny_alloc)
if (tiny_alloc || tiny_zone)
ft_putstr("TINY:");
ft_putstr(FG_RED);
alloc_total += show_zone(tiny_alloc, 0);
ft_putstr(FG_GREEN);
free_total += show_zone(tiny_zone, 1);
ft_putstr(FG_DEFAULT);
if (small_alloc)
if (small_alloc || small_zone)
ft_putstr("SMALL:");
ft_putstr(FG_RED);
alloc_total += show_zone(small_alloc, 0);