new chunk management
This commit is contained in:
parent
202e63f291
commit
631792b831
9 changed files with 124 additions and 175 deletions
|
|
@ -38,7 +38,7 @@ OBJ_DIR = objs/
|
|||
SRC_BASE = \
|
||||
error_lib.c\
|
||||
free.c\
|
||||
get_zones.c\
|
||||
get_zone.c\
|
||||
malloc.c\
|
||||
node_lib.c\
|
||||
realloc.c\
|
||||
|
|
|
|||
|
|
@ -13,44 +13,47 @@
|
|||
#ifndef MALLOC_INTERNAL_H
|
||||
# define MALLOC_INTERNAL_H
|
||||
|
||||
# define malloc_n 64
|
||||
# define malloc_m 1024
|
||||
# define malloc_N (1 * getpagesize())
|
||||
# define malloc_M (2 * getpagesize())
|
||||
# define malloc_magic 1234567
|
||||
# define HEADER_SIZE (sizeof(t_node) - alignof(t_node))
|
||||
# define TINY(x) (x < (malloc_n + 1))
|
||||
# define SMALL(x) (!TINY(x) && !LARGE(x))
|
||||
# define LARGE(x) (malloc_m < x)
|
||||
# define M_NTINY (64)
|
||||
# define M_NSMALL (1024)
|
||||
# define M_PAGEALIGN(x) ((x / getpagesize() + 1) * getpagesize())
|
||||
# define M_TINYCHUNK (M_PAGEALIGN(101 * M_NTINY))
|
||||
# define M_SMALLCHUNK (M_PAGEALIGN(101 * M_NSMALL))
|
||||
# define M_MAGIC 1234567
|
||||
# define M_CHUNKHEAD (sizeof(t_chunk) - alignof(t_chunk))
|
||||
# define M_NODEHEAD (sizeof(t_node) - alignof(t_node))
|
||||
# define M_ISTINY(x) (x < (M_NTINY + 1))
|
||||
# define M_ISSMALL(x) (!M_TINY(x) && !M_LARGE(x))
|
||||
# define M_ISLARGE(x) (M_SMALL < x)
|
||||
|
||||
#include "libft.h"
|
||||
#include <sys/mman.h>
|
||||
#include <stdalign.h>
|
||||
|
||||
typedef struct s_header {
|
||||
int size;
|
||||
int magic;
|
||||
} t_header;
|
||||
typedef struct s_chunk {
|
||||
struct s_chunk *next;
|
||||
} t_chunk;
|
||||
|
||||
typedef struct s_node {
|
||||
int size;
|
||||
struct s_node *next;
|
||||
char data[1];
|
||||
size_t size;
|
||||
int isfree:1;
|
||||
} t_node;
|
||||
|
||||
extern t_node *tiny_zone;
|
||||
extern t_node *small_zone;
|
||||
extern t_node *tiny_alloc;
|
||||
extern t_node *small_alloc;
|
||||
enum e_zones {
|
||||
M_TINY,
|
||||
M_SMALL,
|
||||
M_LARGE,
|
||||
M_ZONES_MAX,
|
||||
};
|
||||
|
||||
extern t_chunk *g_zones[M_ZONES_MAX];
|
||||
|
||||
#include "malloc.h"
|
||||
|
||||
void get_zones(t_node ***zone_ref, t_node ***alloc_ref, size_t size);
|
||||
|
||||
void insert_node(t_node **head, t_node *node);
|
||||
int remove_node(t_node **head, t_node *node);
|
||||
t_node *split_node(t_node **node, t_node **alloc, t_node **zone, size_t size);
|
||||
t_node **find_node_firstfit(t_node **node, size_t size);
|
||||
t_chunk *get_zone(size_t size);
|
||||
t_node *find_node_firstfit(t_chunk *chunk, size_t size);
|
||||
t_node *find_prev_node(t_chunk *zone, t_node *node);
|
||||
int split_node(t_node *node, size_t size);
|
||||
|
||||
void show_free_mem(void);
|
||||
void print_node(char fg[7], t_node *node);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,2 @@
|
|||
#!/bin/sh
|
||||
export DYLD_LIBRARY_PATH=.
|
||||
export DYLD_INSERT_LIBRARIES="libft_malloc.so"
|
||||
export DYLD_FORCE_FLAT_NAMESPACE=1
|
||||
$@
|
||||
DYLD_LIBRARY_PATH=. DYLD_INSERT_LIBRARIES="libft_malloc.so" DYLD_FORCE_FLAT_NAMESPACE=1 $@
|
||||
|
|
|
|||
|
|
@ -12,49 +12,42 @@
|
|||
|
||||
#include "malloc_internal.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;
|
||||
|
||||
int coalesce_nodes(t_node **head)
|
||||
int coalesce_nodes(t_node *node)
|
||||
{
|
||||
while (*head)
|
||||
t_node *next;
|
||||
|
||||
if ((next = node->next) && next->isfree)
|
||||
{
|
||||
if ((*head)->next == *(void**)head + (*head)->size)
|
||||
{
|
||||
(*head)->size += (*head)->next->size;
|
||||
(*head)->next = (*head)->next->next;
|
||||
}
|
||||
else
|
||||
head = &(*head)->next;
|
||||
node->size += next->size;
|
||||
node->next = next->next;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
void free(void *ptr)
|
||||
{
|
||||
t_node **zone_ref;
|
||||
t_node **alloc_ref;
|
||||
t_chunk *zone;
|
||||
t_node *node;
|
||||
t_node *prev;
|
||||
|
||||
/* ft_putstr(FG_YELLOW"free("); */
|
||||
/* ft_putnbr_hex((long)ptr); */
|
||||
/* ft_putendl(")"FG_DEFAULT); */
|
||||
/* DGSH("free i", (int)ptr); */
|
||||
/* DGSH("free ui", (unsigned int)ptr); */
|
||||
/* DGSH("free ll", (long long)ptr); */
|
||||
DGSH("free ull", (unsigned long long)ptr);
|
||||
if (!ptr)
|
||||
return ;
|
||||
node = ptr - HEADER_SIZE;
|
||||
get_zones(&zone_ref, &alloc_ref, node->size);
|
||||
/* ft_putstr("zone @"); */
|
||||
/* *zone_ref ? print_node(BG_MAGENTA, *zone_ref) : ft_putendl(" NULL"); */
|
||||
if (remove_node(alloc_ref, node))
|
||||
node = ptr - M_NODEHEAD;
|
||||
DGSN("node->size", node->size);
|
||||
zone = get_zone(node->size);
|
||||
DGSH("free", (long)ptr);
|
||||
if (!(prev = find_prev_node(zone, node)))
|
||||
{
|
||||
/* error_free_notalloc(ptr); */
|
||||
error_free_notalloc(ptr);
|
||||
return ;
|
||||
}
|
||||
insert_node(zone_ref, node);
|
||||
coalesce_nodes(zone_ref);
|
||||
/* ft_putendl(BG_GREEN"SUCCESSFUL FREE"BG_DEFAULT); */
|
||||
DGSH("free", (long)ptr);
|
||||
coalesce_nodes(node);
|
||||
DGSH("free", (long)ptr);
|
||||
coalesce_nodes(prev);
|
||||
DGS("successful free");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,23 +12,17 @@
|
|||
|
||||
#include "malloc_internal.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;
|
||||
|
||||
void get_zones(t_node ***zone_ref, t_node ***alloc_ref, size_t size)
|
||||
t_chunk *g_zones[M_ZONES_MAX] =
|
||||
{
|
||||
if (LARGE(size))
|
||||
{
|
||||
*zone_ref = &large_zone;
|
||||
*alloc_ref = &large_alloc;
|
||||
}
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
};
|
||||
|
||||
t_chunk *get_zone(size_t size)
|
||||
{
|
||||
if (M_ISLARGE(size))
|
||||
return (g_zones[M_LARGE]);
|
||||
else
|
||||
{
|
||||
*zone_ref = TINY(size) ? &tiny_zone : &small_zone;
|
||||
*alloc_ref = TINY(size) ? &tiny_alloc : &small_alloc;
|
||||
}
|
||||
return (g_zones[M_ISTINY(size) ? M_TINY : M_SMALL]);
|
||||
}
|
||||
|
|
@ -12,57 +12,41 @@
|
|||
|
||||
#include "malloc_internal.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 add_chunk(t_node **zone_ref, size_t size)
|
||||
void add_chunk(t_chunk **chunk, size_t size)
|
||||
{
|
||||
size_t chunk_size;
|
||||
int chunk_size;
|
||||
t_chunk *new;
|
||||
t_node *node;
|
||||
|
||||
/* while (*node_ref) */
|
||||
/* node_ref = &(*node_ref)->next; */
|
||||
if (LARGE(size))
|
||||
chunk_size = size + HEADER_SIZE;
|
||||
if (M_ISLARGE(size))
|
||||
chunk_size = M_PAGEALIGN(size);
|
||||
else
|
||||
chunk_size = TINY(size) ? malloc_N : malloc_M;
|
||||
if (!(node = mmap(NULL, chunk_size, PROT_READ|PROT_WRITE,
|
||||
MAP_ANON|MAP_PRIVATE, -1, 0)))
|
||||
chunk_size = M_ISTINY(size) ? M_TINYCHUNK : M_SMALLCHUNK;
|
||||
if (!(new = mmap(NULL, chunk_size, PROT_READ|PROT_WRITE,
|
||||
MAP_ANON|MAP_PRIVATE, -1, 0)))
|
||||
error_mmap();
|
||||
node->size = chunk_size;
|
||||
insert_node(zone_ref, node);
|
||||
/* ft_putstr("nchunk@"); */
|
||||
/* print_node(BG_GREEN, node); */
|
||||
new->next = *chunk;
|
||||
*chunk = new;
|
||||
node = (t_node*)(*chunk + 1);
|
||||
node->size = chunk_size - M_CHUNKHEAD;
|
||||
node->next = 0;
|
||||
node->isfree = 1;
|
||||
}
|
||||
|
||||
void *malloc(size_t size)
|
||||
{
|
||||
t_node **zone_ref;
|
||||
t_node **alloc_ref;
|
||||
t_node **node_ref;
|
||||
t_chunk *zone;
|
||||
t_node *node;
|
||||
void *ret;
|
||||
|
||||
/* ft_putstr(FG_YELLOW"malloc("); */
|
||||
/* ft_putnbr(size); */
|
||||
/* ft_putendl(")"FG_DEFAULT); */
|
||||
|
||||
get_zones(&zone_ref, &alloc_ref, size);
|
||||
while (!*(node_ref = find_node_firstfit(zone_ref, size)))
|
||||
add_chunk(zone_ref, size);
|
||||
|
||||
/* ft_putstr("found @"); */
|
||||
/* print_node(FG_GREEN, *node_ref); */
|
||||
|
||||
node = split_node(node_ref, alloc_ref, zone_ref, size);
|
||||
|
||||
/* ft_putstr("touser@"); */
|
||||
/* print_node(FG_RED, node); */
|
||||
/* ft_putstr("passing "FG_RED); */
|
||||
/* ft_putnbr_hex((long)node->data); */
|
||||
/* ft_putendl(FG_DEFAULT" to user"); */
|
||||
return (node->data);
|
||||
DGSN("malloc", size);
|
||||
size += M_NODEHEAD;
|
||||
zone = get_zone(size);
|
||||
DGSH("zone", (long)zone);
|
||||
while (!(node = find_node_firstfit(zone, size)))
|
||||
add_chunk(&zone, size);
|
||||
split_node(node, size);
|
||||
ret = (void*)node + M_NODEHEAD;
|
||||
DGSH("to user", (long)ret);
|
||||
return (ret);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,72 +12,51 @@
|
|||
|
||||
#include "malloc_internal.h"
|
||||
|
||||
t_node **find_node_firstfit(t_node **node, size_t size)
|
||||
t_node *find_node_firstfit(t_chunk *chunk, size_t size)
|
||||
{
|
||||
/* if (*node) */
|
||||
/* { */
|
||||
/* ft_putstr("startf@"); */
|
||||
/* print_node(BG_CYAN, *node); */
|
||||
/* } */
|
||||
while (*node && (size_t)(*node)->size < size + HEADER_SIZE)
|
||||
t_node *node;
|
||||
|
||||
if (!chunk)
|
||||
return (NULL);
|
||||
node = (t_node*)(chunk + 1);
|
||||
while (node && !(node->isfree && node->size >= size))
|
||||
{
|
||||
node = &(*node)->next;
|
||||
/* ft_putstr("firstf@"); */
|
||||
/* print_node(FG_GREEN, *node); */
|
||||
node = node->next;
|
||||
}
|
||||
return (node);
|
||||
}
|
||||
|
||||
t_node *split_node(t_node **node, t_node **alloc, t_node **zone, size_t size)
|
||||
int split_node(t_node *node, size_t size)
|
||||
{
|
||||
t_node *new_alloc;
|
||||
int free_size;
|
||||
t_node *new_node;
|
||||
|
||||
free_size = (*node)->size;
|
||||
new_alloc = *node;
|
||||
/* ft_putstr("node->data @ ["); */
|
||||
/* ft_putaddr((*node)->data); */
|
||||
/* ft_putendl("]"); */
|
||||
*node = (t_node*)((*node)->data + size);
|
||||
/* ft_putstr("node @ ["); */
|
||||
/* ft_putaddr(*node); */
|
||||
/* ft_putendl("]"); */
|
||||
(*node)->size = free_size - (size + HEADER_SIZE);
|
||||
if ((*node)->size == 0)
|
||||
remove_node(zone, *node);
|
||||
new_alloc->size = size;
|
||||
insert_node(alloc, new_alloc);
|
||||
return (new_alloc);
|
||||
new_node = (void*)node + size;
|
||||
new_node->next = node->next;
|
||||
new_node->size = node->next - new_node;
|
||||
new_node->isfree = 1;
|
||||
node->next = new_node;
|
||||
node->size -= new_node->size;
|
||||
node->isfree = 0;
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
||||
int remove_node(t_node **head, t_node *node)
|
||||
t_node *find_prev_node(t_chunk *zone, t_node *node)
|
||||
{
|
||||
while (*head)
|
||||
t_node *n;
|
||||
t_node *prev;
|
||||
|
||||
while (zone)
|
||||
{
|
||||
/* ft_putstr("looking for node -> ["); */
|
||||
/* ft_putnbr_hex((long)node); */
|
||||
/* ft_putstr(","); */
|
||||
/* ft_putnbr_hex((long)*head); */
|
||||
/* ft_putendl("]"); */
|
||||
if (*head == node)
|
||||
n = (t_node*)(zone + 1);
|
||||
prev = n;
|
||||
while (n)
|
||||
{
|
||||
*head = (*head)->next;
|
||||
return (0);
|
||||
if (n == node)
|
||||
return (prev);
|
||||
prev = n;
|
||||
n = n->next;
|
||||
}
|
||||
head = &(*head)->next;
|
||||
zone = zone->next;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
void insert_node(t_node **head, t_node *new)
|
||||
{
|
||||
while (*head)
|
||||
{
|
||||
if (new < *head)
|
||||
break ;
|
||||
head = &(*head)->next;
|
||||
}
|
||||
new->next = *head;
|
||||
*head = new;
|
||||
return (NULL);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,9 +23,9 @@ void print_node(char color[7], t_node *node)
|
|||
{
|
||||
ft_putstr("\t");
|
||||
ft_putstr(color);
|
||||
ft_putnbr_hex((long)node->data);
|
||||
/* ft_putnbr_hex((long)node->data); */
|
||||
ft_putstr(" - ");
|
||||
ft_putnbr_hex((long)node->data + node->size);
|
||||
/* ft_putnbr_hex((long)node->data + node->size); */
|
||||
ft_putstr(FBG_DEFAULT" : ");
|
||||
ft_putnbr(node->size);
|
||||
ft_putendl(" bytes");
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ int main(void)
|
|||
char *addr;
|
||||
|
||||
i = 0;
|
||||
valloc(42);
|
||||
while (i < 1024)
|
||||
{
|
||||
i++;
|
||||
|
|
|
|||
Loading…
Reference in a new issue