77 lines
1.8 KiB
C
77 lines
1.8 KiB
C
#include "heap4_common.h"
|
|
|
|
static uint8_t heap_area[192];
|
|
static HeapBlock start;
|
|
static HeapBlock *end_marker;
|
|
|
|
volatile int g_free_count;
|
|
volatile size_t g_merged_size;
|
|
volatile int g_merged_with_right;
|
|
|
|
static void insert_block_merge(HeapBlock *block)
|
|
{
|
|
HeapBlock *iterator;
|
|
uint8_t *block_end;
|
|
|
|
for (iterator = &start; iterator->next < block; iterator = iterator->next)
|
|
;
|
|
|
|
block_end = (uint8_t *)block + block->size;
|
|
if (block_end == (uint8_t *)iterator->next) {
|
|
block->size += iterator->next->size;
|
|
block->next = iterator->next->next;
|
|
} else {
|
|
block->next = iterator->next;
|
|
}
|
|
|
|
if (iterator != &start && heap4_blocks_touch(iterator, block)) {
|
|
iterator->size += block->size;
|
|
iterator->next = block->next;
|
|
} else {
|
|
iterator->next = block;
|
|
}
|
|
}
|
|
|
|
static int count_free(void)
|
|
{
|
|
HeapBlock *block;
|
|
int count;
|
|
|
|
count = 0;
|
|
for (block = start.next; block != end_marker; block = block->next)
|
|
count++;
|
|
return count;
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
HeapBlock *left;
|
|
HeapBlock *middle;
|
|
HeapBlock *right;
|
|
|
|
left = (HeapBlock *)&heap_area[0];
|
|
middle = (HeapBlock *)&heap_area[48];
|
|
right = (HeapBlock *)&heap_area[96];
|
|
end_marker = (HeapBlock *)&heap_area[160];
|
|
|
|
start.next = left;
|
|
left->next = right;
|
|
right->next = end_marker;
|
|
end_marker->next = 0;
|
|
|
|
left->size = 48U;
|
|
middle->size = 48U;
|
|
right->size = 64U;
|
|
end_marker->size = 0U;
|
|
|
|
insert_block_merge(middle);
|
|
|
|
g_free_count = count_free();
|
|
g_merged_size = start.next->size;
|
|
g_merged_with_right = start.next->next == end_marker;
|
|
|
|
TRACE_PRINTF("count=%d size=%u merged_right=%d\n",
|
|
g_free_count, (unsigned)g_merged_size, g_merged_with_right);
|
|
return 0;
|
|
}
|