fixes bit/byte confusion, also large zones work now
This commit is contained in:
parent
4482024705
commit
982fc062f9
7 changed files with 117 additions and 68 deletions
|
|
@ -1,3 +1,15 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* malloc.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/02/17 12:28:05 by jhalford #+# #+# */
|
||||
/* Updated: 2017/02/17 13:17:57 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef MALLOC_H
|
||||
# define MALLOC_H
|
||||
|
||||
|
|
@ -6,9 +18,7 @@
|
|||
# define malloc_N (1 * getpagesize())
|
||||
# define malloc_M (2 * getpagesize())
|
||||
# define malloc_magic 1234567
|
||||
# define malloc_realign(x) ((((x) + 1) / 8) * 8)
|
||||
# define malloc_bytes(x) (((x) + 1) / 8)
|
||||
# define HEADER_SIZE malloc_bytes(sizeof(t_node))
|
||||
# define HEADER_SIZE (sizeof(t_node))
|
||||
# define TINY(x) (x < (malloc_n + 1))
|
||||
# define SMALL(x) (!TINY(x) && !LARGE(x))
|
||||
# define LARGE(x) (malloc_m < x)
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -4,24 +4,15 @@ int main(void)
|
|||
{
|
||||
printf("pagesize=[%i]\n", getpagesize());
|
||||
printf("sizeof(long)=[%lu]\n", sizeof(long));
|
||||
printf("sizeof(t_node)=[%lu]\n", sizeof(t_node));
|
||||
|
||||
void *ptr0 = malloc(8190 * sizeof(long));
|
||||
void *ptr0 = malloc(8150);
|
||||
show_alloc_mem();
|
||||
printf("\n");
|
||||
|
||||
void *ptr1 = malloc(300 * sizeof(long));
|
||||
void *ptr1 = malloc(300);
|
||||
show_alloc_mem();
|
||||
printf("\n");
|
||||
|
||||
/* 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(); */
|
||||
/* printf("\n"); */
|
||||
|
|
@ -30,9 +21,5 @@ int main(void)
|
|||
show_alloc_mem();
|
||||
printf("\n");
|
||||
|
||||
/* free(ptr2); */
|
||||
/* show_alloc_mem(); */
|
||||
/* printf("\n"); */
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,15 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* free.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/02/17 12:28:03 by jhalford #+# #+# */
|
||||
/* Updated: 2017/02/17 13:18:39 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "malloc.h"
|
||||
|
||||
int remove_node(t_node **head, t_node *node)
|
||||
|
|
@ -18,7 +30,7 @@ int coalesce_nodes(t_node **head)
|
|||
{
|
||||
while (*head)
|
||||
{
|
||||
if ((*head)->next == *(void**)head + 8 * (*head)->size)
|
||||
if ((*head)->next == *(void**)head + (*head)->size)
|
||||
{
|
||||
(*head)->size += (*head)->next->size;
|
||||
(*head)->next = (*head)->next->next;
|
||||
|
|
@ -35,12 +47,9 @@ void free(void *ptr)
|
|||
t_node **alloc_ref;
|
||||
t_node *hptr;
|
||||
|
||||
hptr = ptr - 8 * HEADER_SIZE;
|
||||
hptr = ptr - 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);
|
||||
|
|
|
|||
|
|
@ -1,3 +1,15 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* insert_node.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/02/17 12:28:15 by jhalford #+# #+# */
|
||||
/* Updated: 2017/02/17 12:28:15 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "malloc.h"
|
||||
|
||||
void insert_node(t_node **head, t_node *new)
|
||||
|
|
|
|||
|
|
@ -1,9 +1,23 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* malloc.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/02/17 12:28:02 by jhalford #+# #+# */
|
||||
/* Updated: 2017/02/17 13:18:34 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "malloc.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 **find_node(t_node **node, size_t size)
|
||||
{
|
||||
|
|
@ -18,9 +32,11 @@ void add_chunk(t_node **node_ref, size_t size)
|
|||
|
||||
while (*node_ref)
|
||||
node_ref = &(*node_ref)->next;
|
||||
chunk_size = TINY(size) ? malloc_N : malloc_M;
|
||||
printf("chunk_size=[%zu]\n", chunk_size);
|
||||
*node_ref = mmap(NULL, (chunk_size +1) * 8, PROT_READ|PROT_WRITE,
|
||||
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;
|
||||
|
|
@ -32,18 +48,12 @@ void *split_node(t_node **free, t_node **alloc, size_t size)
|
|||
int free_size;
|
||||
|
||||
free_size = (*free)->size;
|
||||
/* printf("split now size=[%zu]\n", size); */
|
||||
/* printf("free @ [%p], size=[%i]\n", *free, (*free)->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); */
|
||||
*(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 * 8);
|
||||
return ((void*)new_alloc + HEADER_SIZE);
|
||||
}
|
||||
|
||||
void *malloc(size_t size)
|
||||
|
|
@ -53,10 +63,17 @@ void *malloc(size_t size)
|
|||
t_node **node_ref;
|
||||
void *ptr;
|
||||
|
||||
printf("malloc(%zu) was called. [%lu] bytes\n", size, malloc_bytes(size));
|
||||
size = malloc_bytes(size);
|
||||
zone_ref = TINY(size) ? &tiny_zone : &small_zone;
|
||||
alloc_ref = TINY(size) ? &tiny_alloc : &small_alloc;
|
||||
printf("malloc(%zu) was 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);
|
||||
|
|
|
|||
|
|
@ -1,9 +1,23 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* show_alloc_mem.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/02/17 12:28:20 by jhalford #+# #+# */
|
||||
/* Updated: 2017/02/17 13:18:10 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "malloc.h"
|
||||
|
||||
t_node *tiny_zone;
|
||||
t_node *small_zone;
|
||||
t_node *large_zone;
|
||||
t_node *tiny_alloc;
|
||||
t_node *small_alloc;
|
||||
t_node *large_alloc;
|
||||
|
||||
void print_free_mem(t_node *node)
|
||||
{
|
||||
|
|
@ -13,7 +27,7 @@ void print_free_mem(t_node *node)
|
|||
size = node->size;
|
||||
addr = (void*)node;
|
||||
printf("\t%p - %p : %4zu byte%c\n",
|
||||
addr, (void*)addr + 8 * size, size, size > 1 ? 's' : 0);
|
||||
addr, (void*)addr + size, size, size > 1 ? 's' : 0);
|
||||
}
|
||||
|
||||
void print_alloc_mem(t_node *node)
|
||||
|
|
@ -22,48 +36,48 @@ void print_alloc_mem(t_node *node)
|
|||
void *addr;
|
||||
|
||||
size = node->size;
|
||||
addr = (void*)node + 8 * HEADER_SIZE;
|
||||
addr = (void*)node + HEADER_SIZE;
|
||||
printf("\t%p - %p : %4zu(+%zu) byte%c\n",
|
||||
addr, (void*)addr + 8 * size, size, HEADER_SIZE, size > 1 ? 's' : 0);
|
||||
addr, (void*)addr + size, size, HEADER_SIZE, size > 1 ? 's' : 0);
|
||||
}
|
||||
|
||||
int show_zone(t_node *node, int is_free)
|
||||
int show_zone(t_node *node, int is_free, size_t (*total)[3])
|
||||
{
|
||||
int total;
|
||||
|
||||
total = 0;
|
||||
while (node)
|
||||
{
|
||||
is_free ? print_free_mem(node) : print_alloc_mem(node);
|
||||
total += node->size + (is_free ? 0 : HEADER_SIZE);
|
||||
(*total)[is_free ? 0 : 1] += node->size;
|
||||
(*total)[2] += is_free ? 0 : HEADER_SIZE;
|
||||
node = node->next;
|
||||
}
|
||||
return (total);
|
||||
return (0);
|
||||
}
|
||||
|
||||
void show_alloc_zone(char *name, t_node *alloc, t_node *zone, size_t (*total)[3])
|
||||
{
|
||||
if (!alloc && !zone)
|
||||
return ;
|
||||
ft_putstr(name);
|
||||
ft_putstr(FG_RED);
|
||||
show_zone(alloc, 0, total);
|
||||
ft_putstr(FG_GREEN);
|
||||
show_zone(zone, 1, total);
|
||||
ft_putstr(FG_DEFAULT);
|
||||
}
|
||||
|
||||
void show_alloc_mem(void)
|
||||
{
|
||||
size_t free_total;
|
||||
size_t alloc_total;
|
||||
size_t total[3];
|
||||
|
||||
free_total = 0;
|
||||
alloc_total = 0;
|
||||
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 || small_zone)
|
||||
ft_putstr("SMALL:");
|
||||
ft_putstr(FG_RED);
|
||||
alloc_total += show_zone(small_alloc, 0);
|
||||
ft_putstr(FG_GREEN);
|
||||
free_total += show_zone(small_zone, 1);
|
||||
ft_putstr(FG_DEFAULT);
|
||||
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 allocated\n", alloc_total);
|
||||
printf("\t%7zu bytes free\n", free_total);
|
||||
printf("\t%7zu bytes mapped\n", alloc_total + free_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]);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue