Consolidate testing memory setup
This commit is contained in:
@@ -6,37 +6,25 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#include "kutil/address_manager.h"
|
||||
#include "kutil/heap_manager.h"
|
||||
#include "kutil/memory.h"
|
||||
#include "catch.hpp"
|
||||
|
||||
using namespace kutil;
|
||||
|
||||
extern void * grow_callback(size_t);
|
||||
extern void free_memory();
|
||||
|
||||
const size_t max_block = 1ull << 36;
|
||||
const size_t start = max_block;
|
||||
const size_t GB = 1ull << 30;
|
||||
static const size_t max_block = 1ull << 36;
|
||||
static const size_t start = max_block;
|
||||
static const size_t GB = 1ull << 30;
|
||||
|
||||
TEST_CASE( "Buddy addresses tests", "[address buddy]" )
|
||||
{
|
||||
heap_manager mm(grow_callback);
|
||||
kutil::setup::set_heap(&mm);
|
||||
|
||||
using clock = std::chrono::system_clock;
|
||||
unsigned seed = clock::now().time_since_epoch().count();
|
||||
std::default_random_engine rng(seed);
|
||||
|
||||
address_manager am;
|
||||
am.add_regions(start, max_block * 2);
|
||||
|
||||
// Blocks should be:
|
||||
// 36: 0-64G, 64-128G
|
||||
|
||||
uintptr_t a = am.allocate(0x4000); // under 64K min
|
||||
uintptr_t b = am.allocate(0x4000);
|
||||
CHECK( b == a + (1<<16));
|
||||
uintptr_t a = am.allocate(0x400); // under min
|
||||
uintptr_t b = am.allocate(0x400);
|
||||
CHECK( b == a + address_manager::min_alloc);
|
||||
|
||||
am.free(a);
|
||||
am.free(b);
|
||||
@@ -77,7 +65,4 @@ TEST_CASE( "Buddy addresses tests", "[address buddy]" )
|
||||
b = am.allocate(max_block);
|
||||
CHECK( b == start + max_block );
|
||||
CHECK( a == start );
|
||||
|
||||
kutil::setup::set_heap(nullptr);
|
||||
free_memory();
|
||||
}
|
||||
|
||||
@@ -1,22 +1,10 @@
|
||||
#include "kutil/frame_allocator.h"
|
||||
#include "kutil/heap_manager.h"
|
||||
#include "kutil/memory.h"
|
||||
#include "catch.hpp"
|
||||
|
||||
using namespace kutil;
|
||||
|
||||
extern void * grow_callback(size_t);
|
||||
extern void free_memory();
|
||||
|
||||
const size_t max_block = 1ull << 36;
|
||||
const size_t start = max_block;
|
||||
const size_t GB = 1ull << 30;
|
||||
|
||||
TEST_CASE( "Frame allocator tests", "[memory frame]" )
|
||||
{
|
||||
heap_manager mm(grow_callback);
|
||||
kutil::setup::set_heap(&mm);
|
||||
|
||||
frame_block_list free;
|
||||
frame_block_list used;
|
||||
frame_block_list cache;
|
||||
@@ -24,16 +12,20 @@ TEST_CASE( "Frame allocator tests", "[memory frame]" )
|
||||
auto *f = new frame_block_list::item_type;
|
||||
f->address = 0x1000;
|
||||
f->count = 1;
|
||||
f->flags = kutil::frame_block_flags::none;
|
||||
free.sorted_insert(f);
|
||||
|
||||
f = new frame_block_list::item_type;
|
||||
f->address = 0x2000;
|
||||
f->count = 1;
|
||||
free.sorted_insert(f);
|
||||
auto *g = new frame_block_list::item_type;
|
||||
g->address = 0x2000;
|
||||
g->count = 1;
|
||||
g->flags = kutil::frame_block_flags::none;
|
||||
free.sorted_insert(g);
|
||||
|
||||
frame_allocator fa(std::move(cache));
|
||||
fa.init(std::move(free), std::move(used));
|
||||
|
||||
fa.consolidate_blocks();
|
||||
|
||||
uintptr_t a = 0;
|
||||
size_t c = fa.allocate(2, &a);
|
||||
CHECK( a == 0x1000 );
|
||||
@@ -42,10 +34,12 @@ TEST_CASE( "Frame allocator tests", "[memory frame]" )
|
||||
fa.free(a, 2);
|
||||
a = 0;
|
||||
|
||||
fa.consolidate_blocks();
|
||||
|
||||
c = fa.allocate(2, &a);
|
||||
CHECK( a == 0x1000 );
|
||||
CHECK( c == 2 );
|
||||
|
||||
kutil::setup::set_heap(nullptr);
|
||||
free_memory();
|
||||
delete f;
|
||||
delete g;
|
||||
}
|
||||
|
||||
@@ -7,3 +7,40 @@ void * operator new[] (size_t n) { return ::malloc(n); }
|
||||
void operator delete (void *p) noexcept { return ::free(p); }
|
||||
void operator delete[] (void *p) noexcept { return ::free(p); }
|
||||
|
||||
#include "kutil/heap_manager.h"
|
||||
#include "kutil/memory.h"
|
||||
|
||||
struct default_heap_listener :
|
||||
public Catch::TestEventListenerBase
|
||||
{
|
||||
using TestEventListenerBase::TestEventListenerBase;
|
||||
|
||||
virtual void testCaseStarting(Catch::TestCaseInfo const& info) override
|
||||
{
|
||||
heap = new kutil::heap_manager(heap_grow_callback);
|
||||
kutil::setup::set_heap(heap);
|
||||
}
|
||||
|
||||
virtual void testCaseEnded(Catch::TestCaseStats const& stats) override
|
||||
{
|
||||
kutil::setup::set_heap(nullptr);
|
||||
delete heap;
|
||||
|
||||
for (void *p : memory) ::free(p);
|
||||
memory.clear();
|
||||
}
|
||||
|
||||
static std::vector<void *> memory;
|
||||
static kutil::heap_manager *heap;
|
||||
|
||||
static void * heap_grow_callback(size_t length) {
|
||||
void *p = aligned_alloc(length, length);
|
||||
memory.push_back(p);
|
||||
return p;
|
||||
}
|
||||
};
|
||||
|
||||
std::vector<void *> default_heap_listener::memory;
|
||||
kutil::heap_manager *default_heap_listener::heap;
|
||||
|
||||
CATCH_REGISTER_LISTENER( default_heap_listener );
|
||||
|
||||
Reference in New Issue
Block a user