core
This commit is contained in:
parent
02f2caf9e1
commit
99814d4f52
@ -2,6 +2,8 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#define DEBUG
|
||||
|
||||
void ft_free(void *ptr);
|
||||
void *ft_malloc(size_t size);
|
||||
void *ft_realloc(void *ptr, size_t size);
|
||||
|
42
src/block.c
42
src/block.c
@ -7,7 +7,8 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#include "align.h"
|
||||
#include "./chunk.h"
|
||||
#include "chunk.h"
|
||||
#include "chunk_manager.h"
|
||||
|
||||
int add_new_block(chunk_t* chunk, size_t size, void *prev, void *next, bool is_used)
|
||||
{
|
||||
@ -19,7 +20,7 @@ int add_new_block(chunk_t* chunk, size_t size, void *prev, void *next, bool is_u
|
||||
|
||||
new_block = mmap(NULL, size + ALIGN_MARGING + CHUNK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
||||
|
||||
init_chunk(chunk, size + ALIGN_MARGING - get_align_increment(new_block + CHUNK_SIZE), block_id, prev, new_block, next, is_used);
|
||||
chunk_init(chunk, size + ALIGN_MARGING - get_align_increment(new_block + CHUNK_SIZE), block_id, prev, new_block, next, is_used);
|
||||
|
||||
if (prev != NULL)
|
||||
{
|
||||
@ -31,4 +32,41 @@ int add_new_block(chunk_t* chunk, size_t size, void *prev, void *next, bool is_u
|
||||
block_id++;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void destroy_block(chunk_t *chunk)
|
||||
{
|
||||
chunk_t chunk_prev, chunk_next, *first_chunk = chunk, *last_chunk = chunk;
|
||||
|
||||
if (chunk->prev != NULL)
|
||||
{
|
||||
chunk_read(chunk->prev, &chunk_prev);
|
||||
if (chunk_prev.block_id == chunk->block_id && chunk_prev.is_used == false)
|
||||
chunk_merge(&chunk_prev, chunk);
|
||||
first_chunk = &chunk_prev;
|
||||
}
|
||||
if (chunk->next != NULL)
|
||||
{
|
||||
chunk_read(chunk->next, &chunk_next);
|
||||
if (chunk_next.block_id == chunk->block_id && chunk_next.is_used == false)
|
||||
chunk_merge(chunk, &chunk_next);
|
||||
last_chunk = &chunk_next;
|
||||
}
|
||||
|
||||
if (last_chunk != chunk)
|
||||
{
|
||||
last_chunk->prev = chunk->prev;
|
||||
chunk_write(last_chunk);
|
||||
}
|
||||
|
||||
chunk->is_used = false;
|
||||
chunk_write(chunk);
|
||||
|
||||
if (first_chunk != chunk)
|
||||
{
|
||||
first_chunk->next = chunk->next;
|
||||
chunk_write(first_chunk);
|
||||
}
|
||||
|
||||
munmap(chunk->current, get_physical_size(chunk));
|
||||
}
|
@ -4,4 +4,5 @@
|
||||
|
||||
#include "chunk.h"
|
||||
|
||||
int add_new_block(chunk_t* chunk, size_t size, void *prev, void *next, bool is_used);
|
||||
int add_new_block(chunk_t* chunk, size_t size, void *prev, void *next, bool is_used);
|
||||
void destroy_block(chunk_t *chunk);
|
@ -30,7 +30,7 @@ void chunk_read(void * const *block, chunk_t *chunk)
|
||||
chunk->data_start = block[CHUNK_DATA_START_POS];
|
||||
}
|
||||
|
||||
void init_chunk(chunk_t *chunk, size_t size, long block_id, void *prev, void *current, void *next, bool is_used)
|
||||
void chunk_init(chunk_t *chunk, size_t size, long block_id, void *prev, void *current, void *next, bool is_used)
|
||||
{
|
||||
void *data_start = current + CHUNK_SIZE;
|
||||
|
||||
|
@ -33,6 +33,6 @@ enum {
|
||||
|
||||
void chunk_read(void * const *block, chunk_t *chunk);
|
||||
void chunk_write(const chunk_t *chunk);
|
||||
void init_chunk(chunk_t *chunk, size_t size, long block_id, void *prev, void *current, void *next, bool is_used);
|
||||
void chunk_init(chunk_t *chunk, size_t size, long block_id, void *prev, void *current, void *next, bool is_used);
|
||||
size_t get_physical_size(const chunk_t *chunk);
|
||||
void chunk_print(const chunk_t *chunk);
|
@ -1,5 +1,6 @@
|
||||
#include "chunk.h"
|
||||
#include "align.h"
|
||||
#include "print.h"
|
||||
|
||||
int get_unused_chunk(chunk_t *root, size_t size, chunk_t *result)
|
||||
{
|
||||
@ -21,8 +22,10 @@ int get_unused_chunk(chunk_t *root, size_t size, chunk_t *result)
|
||||
|
||||
void chunk_merge(chunk_t *first, chunk_t *second)
|
||||
{
|
||||
//if (first->block_id != second->block_id)
|
||||
|
||||
#ifndef DEBUG
|
||||
if (first->block_id != second->block_id)
|
||||
putstr("Attempt merge two chunk on different block\n");
|
||||
#endif
|
||||
first->size = first->size + second->size + CHUNK_SIZE + ALIGN_MARGING - get_align_increment(first->next);
|
||||
first->next = second->next;
|
||||
}
|
||||
@ -35,8 +38,8 @@ int chunk_split(chunk_t *chunk, chunk_t *new_chunk, size_t new_size)
|
||||
|
||||
void *new_chunk_pos = chunk->data_start + new_size;
|
||||
|
||||
init_chunk(new_chunk, chunk->size - new_size - CHUNK_SIZE - get_align_increment(new_chunk_pos + CHUNK_SIZE), chunk->block_id, chunk->current, new_chunk_pos, chunk->next, true);
|
||||
init_chunk(chunk, new_size, chunk->block_id, chunk->prev, chunk->current, new_chunk->current, false);
|
||||
chunk_init(new_chunk, chunk->size - new_size - CHUNK_SIZE - get_align_increment(new_chunk_pos + CHUNK_SIZE), chunk->block_id, chunk->current, new_chunk_pos, chunk->next, true);
|
||||
chunk_init(chunk, new_size, chunk->block_id, chunk->prev, chunk->current, new_chunk->current, false);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -52,39 +55,4 @@ void chunk_iter(chunk_t *root, void (*f)(chunk_t*))
|
||||
break;
|
||||
chunk_read(current->next, current);
|
||||
}
|
||||
}
|
||||
|
||||
void destroy_chunk(chunk_t *chunk)
|
||||
{
|
||||
chunk_t chunk_prev, chunk_next, *first_chunk = chunk, *last_chunk = chunk;
|
||||
|
||||
if (chunk->prev != NULL)
|
||||
{
|
||||
chunk_read(chunk->prev, &chunk_prev);
|
||||
if (chunk_prev.block_id == chunk->block_id && chunk_prev.is_used == false)
|
||||
chunk_merge(&chunk_prev, chunk);
|
||||
first_chunk = &chunk_prev;
|
||||
}
|
||||
if (chunk->next != NULL)
|
||||
{
|
||||
chunk_read(chunk->next, &chunk_next);
|
||||
if (chunk_next.block_id == chunk->block_id && chunk_next.is_used == false)
|
||||
chunk_merge(chunk, &chunk_next);
|
||||
last_chunk = &chunk_next;
|
||||
}
|
||||
|
||||
if (last_chunk != chunk)
|
||||
{
|
||||
last_chunk->prev = chunk->prev;
|
||||
chunk_write(last_chunk);
|
||||
}
|
||||
|
||||
chunk->is_used = false;
|
||||
chunk_write(chunk);
|
||||
|
||||
if (first_chunk != chunk)
|
||||
{
|
||||
first_chunk->next = chunk->next;
|
||||
chunk_write(first_chunk);
|
||||
}
|
||||
}
|
@ -2,5 +2,5 @@
|
||||
|
||||
#include "chunk.h"
|
||||
|
||||
void destroy_chunk(chunk_t *chunk);
|
||||
void chunk_iter(chunk_t *root, void (*f)(chunk_t*));
|
||||
void chunk_iter(chunk_t *root, void (*f)(chunk_t*));
|
||||
void chunk_merge(chunk_t *first, chunk_t *second);
|
@ -52,7 +52,6 @@ void *ft_malloc(size_t size)
|
||||
if (size < m)
|
||||
return alloc_small(size);
|
||||
return alloc_large(size);
|
||||
//mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
||||
}
|
||||
|
||||
void free_tiny(chunk_t *chunk)
|
||||
@ -69,8 +68,7 @@ void free_large(chunk_t *chunk)
|
||||
{
|
||||
if (chunk->current == allocs_tree[LARGE])
|
||||
allocs_tree[LARGE] = chunk->next;
|
||||
destroy_chunk(chunk);
|
||||
munmap(chunk->current, get_physical_size(chunk));
|
||||
destroy_block(chunk);
|
||||
}
|
||||
|
||||
static void (*free_funcs[3])(chunk_t*) = {free_tiny, free_small, free_large};
|
||||
|
Loading…
Reference in New Issue
Block a user