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
|
||||
# define MALLOC_H
|
||||
|
||||
#define malloc_n 20
|
||||
#define malloc_m 100
|
||||
#define malloc_N 100
|
||||
#define malloc_M 500
|
||||
#define malloc_magic 1234567
|
||||
#define TINY(x) (x < (malloc_n + 1))
|
||||
#define SMALL(x) (!TINY(x) && !LARGE(x))
|
||||
#define LARGE(x) (malloc_m < x)
|
||||
# define malloc_n 64
|
||||
# define malloc_m 512
|
||||
# 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 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 <sys/mman.h>
|
||||
|
|
@ -23,12 +26,15 @@ typedef struct s_node {
|
|||
struct s_node *next;
|
||||
} t_node;
|
||||
|
||||
extern t_node *tiny_head;
|
||||
extern t_node *small_head;
|
||||
extern t_node *tiny_zone;
|
||||
extern t_node *small_zone;
|
||||
extern t_node *tiny_alloc;
|
||||
extern t_node *small_alloc;
|
||||
|
||||
void free(void *ptr);
|
||||
void *malloc(size_t size);
|
||||
void *realloc(void *ptr, size_t size);
|
||||
void show_alloc_mem(void);
|
||||
void show_free_mem(void);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -4,10 +4,24 @@ int main(void)
|
|||
{
|
||||
void *ptr;
|
||||
|
||||
ptr = malloc(10);
|
||||
ptr = malloc(42);
|
||||
ptr = malloc(3);
|
||||
ptr = malloc(15);
|
||||
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));
|
||||
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);
|
||||
}
|
||||
|
|
|
|||
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)
|
||||
{
|
||||
while (*node && (size_t)(*node)->size < sizeof(t_header) + size)
|
||||
while (*node && (size_t)(*node)->size < sizeof(t_node) + size)
|
||||
node = &(*node)->next;
|
||||
return (node);
|
||||
}
|
||||
|
|
@ -18,34 +18,57 @@ void add_chunk(t_node **node_ref, size_t size)
|
|||
node_ref = &(*node_ref)->next;
|
||||
*node_ref = mmap(NULL, malloc_N, PROT_READ|PROT_WRITE,
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
free_size = (*node)->size;
|
||||
hptr = *(t_header**)node;
|
||||
*node = *node + size + sizeof(*hptr);
|
||||
(*node)->size = free_size - size - sizeof(*hptr);
|
||||
hptr->size = size;
|
||||
hptr->magic = malloc_magic;
|
||||
return (hptr + sizeof(*hptr));
|
||||
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("alloc @ [%p]\n", *alloc); */
|
||||
/* fflush(stdout); */
|
||||
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)
|
||||
{
|
||||
t_node **zone_ref;
|
||||
t_node **alloc_ref;
|
||||
t_node **node_ref;
|
||||
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;
|
||||
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);
|
||||
ptr = split_node(node_ref, size);
|
||||
ptr = split_node(node_ref, alloc_ref, size);
|
||||
return (ptr);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,16 +2,40 @@
|
|||
|
||||
t_node *tiny_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;
|
||||
|
||||
total = 0;
|
||||
while (node)
|
||||
{
|
||||
printf("%p - %p: %i bytes\n", node, node + node->size, node->size);
|
||||
total += node->size;
|
||||
is_free ? print_free_mem(node) : print_alloc_mem(node);
|
||||
total += node->size + HEADER_SIZE;
|
||||
node = node->next;
|
||||
}
|
||||
return (total);
|
||||
|
|
@ -19,12 +43,27 @@ int show_free_zone(t_node *node)
|
|||
|
||||
void show_alloc_mem(void)
|
||||
{
|
||||
int total;
|
||||
size_t free_total;
|
||||
size_t alloc_total;
|
||||
|
||||
total = 0;
|
||||
ft_putstr("TINY:\n");
|
||||
total += show_free_zone(tiny_zone);
|
||||
ft_putstr("SMALL:\n");
|
||||
total += show_free_zone(small_zone);
|
||||
printf("Total: %i bytes\n", total);
|
||||
free_total = 0;
|
||||
alloc_total = 0;
|
||||
if (tiny_alloc)
|
||||
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)
|
||||
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