Initial commit

This commit is contained in:
2023-01-09 01:54:03 -06:00
commit d347b81e72
14 changed files with 856 additions and 0 deletions

6
tests/Makefile.am Normal file
View File

@@ -0,0 +1,6 @@
if BUILD_TESTS
noinst_PROGRAMS = test_buddyalloc
test_buddyalloc_SOURCES = test_buddyalloc.c
test_buddyalloc_LDADD = ../src/libmalloc.a
endif

51
tests/test_buddyalloc.c Normal file
View File

@@ -0,0 +1,51 @@
#include "libmalloc/bitmap_alloc.h"
#include "util.h"
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
int main(int argc, char **args)
{
const int memory_map_capacity = 32;
const int heap_size = 1 << 20;
const int cache_capacity = 20;
memory_region_t arr[memory_map_capacity];
unsigned long heap_cache[cache_capacity];
memory_map_t memory_map = {
.array = arr,
.capacity = memory_map_capacity,
.size = 0
};
bitmap_heap_descriptor_t heap = {
.bitmap = malloc(heap_size / 4),
.block_size = 1,
.cache = heap_cache,
.cache_capacity = cache_capacity
};
memmap_insert_region(&memory_map, 0, heap_size, M_AVAILABLE);
initialize_physical_heap(&heap, &memory_map);
unsigned long *reserved_blocks = malloc(sizeof(unsigned long) * heap_size);
for(int i = 0; i < heap_size; i++)
{
reserved_blocks[i] = reserve_region(&heap, 1);
}
/*for(int i = 0; i < heap_size; i++)
{
for(int j = i + 1; j < heap_size; j++)
{
assert(reserved_blocks[i] != reserved_blocks[j]);
}
}*/
for(int i = 0; i < heap_size; i++)
{
free_region(&heap, reserved_blocks[i], 1);
}
assert(heap.free_block_count == heap_size);
}