some work done on splitting, free() next
This commit is contained in:
parent
5c78200d8f
commit
053c0773de
6 changed files with 124 additions and 42 deletions
|
|
@ -1,14 +1,17 @@
|
||||||
#ifndef MALLOC_H
|
#ifndef MALLOC_H
|
||||||
# define MALLOC_H
|
# define MALLOC_H
|
||||||
|
|
||||||
#define malloc_n 20
|
# define malloc_n 64
|
||||||
#define malloc_m 100
|
# define malloc_m 512
|
||||||
#define malloc_N 100
|
# define malloc_N (1 * getpagesize())
|
||||||
#define malloc_M 500
|
# define malloc_M (2 * getpagesize())
|
||||||
#define malloc_magic 1234567
|
# define malloc_magic 1234567
|
||||||
#define TINY(x) (x < (malloc_n + 1))
|
# define malloc_realign(x) ((((x) + 1) / 8) * 8)
|
||||||
#define SMALL(x) (!TINY(x) && !LARGE(x))
|
# define malloc_bytes(x) (((x) + 1) / 8)
|
||||||
#define LARGE(x) (malloc_m < x)
|
# define HEADER_SIZE malloc_bytes(sizeof(t_node))
|
||||||
|
# define TINY(x) (x < (malloc_n + 1))
|
||||||
|
# define SMALL(x) (!TINY(x) && !LARGE(x))
|
||||||
|
# define LARGE(x) (malloc_m < x)
|
||||||
|
|
||||||
#include "../libft/includes/libft.h"
|
#include "../libft/includes/libft.h"
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
|
|
@ -23,12 +26,15 @@ typedef struct s_node {
|
||||||
struct s_node *next;
|
struct s_node *next;
|
||||||
} t_node;
|
} t_node;
|
||||||
|
|
||||||
extern t_node *tiny_head;
|
extern t_node *tiny_zone;
|
||||||
extern t_node *small_head;
|
extern t_node *small_zone;
|
||||||
|
extern t_node *tiny_alloc;
|
||||||
|
extern t_node *small_alloc;
|
||||||
|
|
||||||
void free(void *ptr);
|
void free(void *ptr);
|
||||||
void *malloc(size_t size);
|
void *malloc(size_t size);
|
||||||
void *realloc(void *ptr, size_t size);
|
void *realloc(void *ptr, size_t size);
|
||||||
void show_alloc_mem(void);
|
void show_alloc_mem(void);
|
||||||
|
void show_free_mem(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -4,10 +4,24 @@ int main(void)
|
||||||
{
|
{
|
||||||
void *ptr;
|
void *ptr;
|
||||||
|
|
||||||
ptr = malloc(10);
|
printf("pagesize=[%i]\n", getpagesize());
|
||||||
ptr = malloc(42);
|
printf("sizeof(long)=[%lu]\n", sizeof(long));
|
||||||
ptr = malloc(3);
|
printf("sizeof(t_node)=[%lu]\n", sizeof(t_node));
|
||||||
ptr = malloc(15);
|
ptr = malloc(1 * sizeof(long));
|
||||||
show_alloc_mem();
|
show_alloc_mem();
|
||||||
|
ft_putchar('\n');
|
||||||
|
|
||||||
|
ptr = malloc(84 * sizeof(long));
|
||||||
|
show_alloc_mem();
|
||||||
|
ft_putchar('\n');
|
||||||
|
|
||||||
|
ptr = malloc(65 * sizeof(long));
|
||||||
|
show_alloc_mem();
|
||||||
|
ft_putchar('\n');
|
||||||
|
|
||||||
|
ptr = malloc(64 * sizeof(long));
|
||||||
|
show_alloc_mem();
|
||||||
|
ft_putchar('\n');
|
||||||
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
BIN
malloc/myprogram
BIN
malloc/myprogram
Binary file not shown.
|
|
@ -7,7 +7,7 @@ t_node *small_alloc = NULL;
|
||||||
|
|
||||||
t_node **find_node(t_node **node, size_t size)
|
t_node **find_node(t_node **node, size_t size)
|
||||||
{
|
{
|
||||||
while (*node && (size_t)(*node)->size < sizeof(t_header) + size)
|
while (*node && (size_t)(*node)->size < sizeof(t_node) + size)
|
||||||
node = &(*node)->next;
|
node = &(*node)->next;
|
||||||
return (node);
|
return (node);
|
||||||
}
|
}
|
||||||
|
|
@ -18,34 +18,57 @@ void add_chunk(t_node **node_ref, size_t size)
|
||||||
node_ref = &(*node_ref)->next;
|
node_ref = &(*node_ref)->next;
|
||||||
*node_ref = mmap(NULL, malloc_N, PROT_READ|PROT_WRITE,
|
*node_ref = mmap(NULL, malloc_N, PROT_READ|PROT_WRITE,
|
||||||
MAP_ANON|MAP_PRIVATE, -1, 0);
|
MAP_ANON|MAP_PRIVATE, -1, 0);
|
||||||
(*node_ref)->size = (TINY(size) ? malloc_N : malloc_M) + sizeof(t_node);
|
(*node_ref)->size = TINY(size) ? malloc_N : malloc_M;
|
||||||
(*node_ref)->next = NULL;
|
(*node_ref)->next = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *split_node(t_node **node, size_t size)
|
void mem_insert(t_node **head, t_node *new)
|
||||||
{
|
{
|
||||||
t_header *hptr;
|
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;
|
||||||
int free_size;
|
int free_size;
|
||||||
|
|
||||||
free_size = (*node)->size;
|
free_size = (*free)->size;
|
||||||
hptr = *(t_header**)node;
|
/* printf("split now size=[%zu]\n", size); */
|
||||||
*node = *node + size + sizeof(*hptr);
|
/* printf("free @ [%p], size=[%i]\n", *free, (*free)->size); */
|
||||||
(*node)->size = free_size - size - sizeof(*hptr);
|
/* printf("size + sizeof = [%lu]\n", size + HEADER_SIZE); */
|
||||||
hptr->size = size;
|
/* printf("alloc @ [%p]\n", *alloc); */
|
||||||
hptr->magic = malloc_magic;
|
/* fflush(stdout); */
|
||||||
return (hptr + sizeof(*hptr));
|
new_alloc = *free;
|
||||||
|
*(void**)free += 8 * (size + HEADER_SIZE);
|
||||||
|
(*free)->size = free_size - (size + HEADER_SIZE);
|
||||||
|
new_alloc->size = size;
|
||||||
|
mem_insert(alloc, new_alloc);
|
||||||
|
return (*alloc + HEADER_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void *malloc(size_t size)
|
void *malloc(size_t size)
|
||||||
{
|
{
|
||||||
t_node **zone_ref;
|
t_node **zone_ref;
|
||||||
|
t_node **alloc_ref;
|
||||||
t_node **node_ref;
|
t_node **node_ref;
|
||||||
void *ptr;
|
void *ptr;
|
||||||
|
|
||||||
printf("malloc(%zu) was called\n", size);
|
printf("malloc(%zu) was called. [%lu] bytes\n", size, malloc_bytes(size));
|
||||||
|
size = malloc_bytes(size);
|
||||||
zone_ref = TINY(size) ? &tiny_zone : &small_zone;
|
zone_ref = TINY(size) ? &tiny_zone : &small_zone;
|
||||||
while (!(*(node_ref = find_node(zone_ref, size))))
|
alloc_ref = TINY(size) ? &tiny_alloc : &small_alloc;
|
||||||
|
while (!*(node_ref = find_node(zone_ref, size)))
|
||||||
add_chunk(node_ref, size);
|
add_chunk(node_ref, size);
|
||||||
ptr = split_node(node_ref, size);
|
ptr = split_node(node_ref, alloc_ref, size);
|
||||||
return (ptr);
|
return (ptr);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,16 +2,40 @@
|
||||||
|
|
||||||
t_node *tiny_zone;
|
t_node *tiny_zone;
|
||||||
t_node *small_zone;
|
t_node *small_zone;
|
||||||
|
t_node *tiny_alloc;
|
||||||
|
t_node *small_alloc;
|
||||||
|
|
||||||
int show_free_zone(t_node *node)
|
void print_free_mem(t_node *node)
|
||||||
|
{
|
||||||
|
size_t size;
|
||||||
|
void *addr;
|
||||||
|
|
||||||
|
size = node->size;
|
||||||
|
addr = (void*)node;
|
||||||
|
printf("\t%p - %p : %4zu byte%c\n",
|
||||||
|
addr, addr + 8 * 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, addr + 8 * size, size, HEADER_SIZE, size > 1 ? 's' : 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int show_zone(t_node *node, int is_free)
|
||||||
{
|
{
|
||||||
int total;
|
int total;
|
||||||
|
|
||||||
total = 0;
|
total = 0;
|
||||||
while (node)
|
while (node)
|
||||||
{
|
{
|
||||||
printf("%p - %p: %i bytes\n", node, node + node->size, node->size);
|
is_free ? print_free_mem(node) : print_alloc_mem(node);
|
||||||
total += node->size;
|
total += node->size + HEADER_SIZE;
|
||||||
node = node->next;
|
node = node->next;
|
||||||
}
|
}
|
||||||
return (total);
|
return (total);
|
||||||
|
|
@ -19,12 +43,27 @@ int show_free_zone(t_node *node)
|
||||||
|
|
||||||
void show_alloc_mem(void)
|
void show_alloc_mem(void)
|
||||||
{
|
{
|
||||||
int total;
|
size_t free_total;
|
||||||
|
size_t alloc_total;
|
||||||
|
|
||||||
total = 0;
|
free_total = 0;
|
||||||
ft_putstr("TINY:\n");
|
alloc_total = 0;
|
||||||
total += show_free_zone(tiny_zone);
|
if (tiny_alloc)
|
||||||
ft_putstr("SMALL:\n");
|
ft_putstr("TINY:");
|
||||||
total += show_free_zone(small_zone);
|
ft_putstr(FG_RED);
|
||||||
printf("Total: %i bytes\n", total);
|
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)
|
||||||
|
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);
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue