2024-07-30 03:28:43 -04:00
|
|
|
#include "chunk.h"
|
|
|
|
#include "align.h"
|
|
|
|
|
|
|
|
int get_unused_chunk(chunk_t *root, size_t size, chunk_t *result)
|
|
|
|
{
|
|
|
|
void** current = root->current;
|
|
|
|
|
|
|
|
while (current != NULL)
|
|
|
|
{
|
|
|
|
if ((bool) current[CHUNK_IS_USED_POS] == true)
|
|
|
|
continue;
|
|
|
|
if ((size_t) current[CHUNK_SIZE_POS] >= size)
|
|
|
|
{
|
|
|
|
chunk_read(current, result);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
current = current[CHUNK_NEXT_POS];
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void chunk_merge(chunk_t *first, chunk_t *second)
|
|
|
|
{
|
|
|
|
//if (first->block_id != second->block_id)
|
|
|
|
|
|
|
|
first->size = first->size + second->size + CHUNK_SIZE + ALIGN_MARGING - get_align_increment(first->next);
|
|
|
|
first->next = second->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
int chunk_split(chunk_t *chunk, chunk_t *new_chunk, size_t new_size)
|
|
|
|
{
|
|
|
|
// NOT ENOUGH SIZE TO BE SPLITTED
|
|
|
|
if (chunk->size + 1 < new_size + CHUNK_SIZE + get_align_increment(chunk->current + CHUNK_SIZE))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-07-30 07:17:36 -04:00
|
|
|
void chunk_iter(chunk_t *root, void (*f)(chunk_t*))
|
|
|
|
{
|
|
|
|
chunk_t *current = root;
|
|
|
|
|
|
|
|
while (current)
|
|
|
|
{
|
|
|
|
f(current);
|
|
|
|
if (current->next == NULL)
|
|
|
|
break;
|
|
|
|
chunk_read(current->next, current);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-30 03:28:43 -04:00
|
|
|
void destroy_chunk(chunk_t *chunk)
|
|
|
|
{
|
2024-07-30 05:33:42 -04:00
|
|
|
chunk_t chunk_prev, chunk_next, *first_chunk = chunk, *last_chunk = chunk;
|
2024-07-30 03:28:43 -04:00
|
|
|
|
|
|
|
if (chunk->prev != NULL)
|
|
|
|
{
|
2024-07-30 05:33:42 -04:00
|
|
|
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;
|
2024-07-30 03:28:43 -04:00
|
|
|
}
|
|
|
|
if (chunk->next != NULL)
|
|
|
|
{
|
2024-07-30 05:33:42 -04:00
|
|
|
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;
|
2024-07-30 03:28:43 -04:00
|
|
|
}
|
2024-07-30 05:33:42 -04:00
|
|
|
|
|
|
|
if (last_chunk != chunk)
|
|
|
|
{
|
|
|
|
last_chunk->prev = chunk->prev;
|
|
|
|
chunk_write(last_chunk);
|
|
|
|
}
|
|
|
|
|
2024-07-30 03:28:43 -04:00
|
|
|
chunk->is_used = false;
|
|
|
|
chunk_write(chunk);
|
2024-07-30 05:33:42 -04:00
|
|
|
|
|
|
|
if (first_chunk != chunk)
|
|
|
|
{
|
|
|
|
first_chunk->next = chunk->next;
|
|
|
|
chunk_write(first_chunk);
|
|
|
|
}
|
2024-07-30 03:28:43 -04:00
|
|
|
}
|