diff --git a/malloc/Makefile b/malloc/Makefile index 2b404140..0304ea74 100644 --- a/malloc/Makefile +++ b/malloc/Makefile @@ -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 diff --git a/malloc/includes/malloc.h b/malloc/includes/malloc.h index 13526d2e..b9725805 100644 --- a/malloc/includes/malloc.h +++ b/malloc/includes/malloc.h @@ -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 diff --git a/malloc/libft_malloc_x86_64_Darwin.so b/malloc/libft_malloc_x86_64_Darwin.so index 541b8378..4ef82e3d 100755 Binary files a/malloc/libft_malloc_x86_64_Darwin.so and b/malloc/libft_malloc_x86_64_Darwin.so differ diff --git a/malloc/main.c b/malloc/main.c index 3b962dbd..28cbc7bc 100644 --- a/malloc/main.c +++ b/malloc/main.c @@ -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); } diff --git a/malloc/myprogram b/malloc/myprogram index e3d378bf..23304706 100755 Binary files a/malloc/myprogram and b/malloc/myprogram differ diff --git a/malloc/src/free.c b/malloc/src/free.c index e90c41d2..112865af 100644 --- a/malloc/src/free.c +++ b/malloc/src/free.c @@ -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); } diff --git a/malloc/src/insert_node.c b/malloc/src/insert_node.c new file mode 100644 index 00000000..ff9adf4a --- /dev/null +++ b/malloc/src/insert_node.c @@ -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; +} diff --git a/malloc/src/malloc.c b/malloc/src/malloc.c index 759c9b21..58311c28 100644 --- a/malloc/src/malloc.c +++ b/malloc/src/malloc.c @@ -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) diff --git a/malloc/src/show_alloc_mem.c b/malloc/src/show_alloc_mem.c index c160db0f..d2dc2526 100644 --- a/malloc/src/show_alloc_mem.c +++ b/malloc/src/show_alloc_mem.c @@ -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);