Consolidate testing memory setup

This commit is contained in:
Justin C. Miller
2019-03-07 23:53:38 -08:00
parent ac19d3f532
commit 241e1dacb0
6 changed files with 59 additions and 41 deletions

View File

@@ -65,7 +65,6 @@ frame_allocator::init(
{ {
m_free.append(free); m_free.append(free);
m_used.append(used); m_used.append(used);
consolidate_blocks();
} }
void void

View File

@@ -22,6 +22,9 @@ public:
using region_node = list_node<region_type>; using region_node = list_node<region_type>;
using region_list = linked_list<region_type>; using region_list = linked_list<region_type>;
static const size_t min_alloc = (1 << size_min);
static const size_t max_alloc = (1 << size_max);
/// Constructor. /// Constructor.
buddy_allocator() {} buddy_allocator() {}

View File

@@ -48,11 +48,11 @@ public:
/// \arg count The number of frames to be freed /// \arg count The number of frames to be freed
void free(uintptr_t address, size_t count); void free(uintptr_t address, size_t count);
private:
/// Consolidate the free and used block lists. Return freed blocks /// Consolidate the free and used block lists. Return freed blocks
/// to the cache. /// to the cache.
void consolidate_blocks(); void consolidate_blocks();
private:
frame_block_list m_free; ///< Free frames list frame_block_list m_free; ///< Free frames list
frame_block_list m_used; ///< In-use frames list frame_block_list m_used; ///< In-use frames list
frame_block_slab m_block_slab; ///< frame_block slab allocator frame_block_slab m_block_slab; ///< frame_block slab allocator

View File

@@ -6,37 +6,25 @@
#include <stdint.h> #include <stdint.h>
#include "kutil/address_manager.h" #include "kutil/address_manager.h"
#include "kutil/heap_manager.h"
#include "kutil/memory.h"
#include "catch.hpp" #include "catch.hpp"
using namespace kutil; using namespace kutil;
extern void * grow_callback(size_t); static const size_t max_block = 1ull << 36;
extern void free_memory(); static const size_t start = max_block;
static const size_t GB = 1ull << 30;
const size_t max_block = 1ull << 36;
const size_t start = max_block;
const size_t GB = 1ull << 30;
TEST_CASE( "Buddy addresses tests", "[address buddy]" ) 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; address_manager am;
am.add_regions(start, max_block * 2); am.add_regions(start, max_block * 2);
// Blocks should be: // Blocks should be:
// 36: 0-64G, 64-128G // 36: 0-64G, 64-128G
uintptr_t a = am.allocate(0x4000); // under 64K min uintptr_t a = am.allocate(0x400); // under min
uintptr_t b = am.allocate(0x4000); uintptr_t b = am.allocate(0x400);
CHECK( b == a + (1<<16)); CHECK( b == a + address_manager::min_alloc);
am.free(a); am.free(a);
am.free(b); am.free(b);
@@ -77,7 +65,4 @@ TEST_CASE( "Buddy addresses tests", "[address buddy]" )
b = am.allocate(max_block); b = am.allocate(max_block);
CHECK( b == start + max_block ); CHECK( b == start + max_block );
CHECK( a == start ); CHECK( a == start );
kutil::setup::set_heap(nullptr);
free_memory();
} }

View File

@@ -1,22 +1,10 @@
#include "kutil/frame_allocator.h" #include "kutil/frame_allocator.h"
#include "kutil/heap_manager.h"
#include "kutil/memory.h"
#include "catch.hpp" #include "catch.hpp"
using namespace kutil; 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]" ) TEST_CASE( "Frame allocator tests", "[memory frame]" )
{ {
heap_manager mm(grow_callback);
kutil::setup::set_heap(&mm);
frame_block_list free; frame_block_list free;
frame_block_list used; frame_block_list used;
frame_block_list cache; frame_block_list cache;
@@ -24,16 +12,20 @@ TEST_CASE( "Frame allocator tests", "[memory frame]" )
auto *f = new frame_block_list::item_type; auto *f = new frame_block_list::item_type;
f->address = 0x1000; f->address = 0x1000;
f->count = 1; f->count = 1;
f->flags = kutil::frame_block_flags::none;
free.sorted_insert(f); free.sorted_insert(f);
f = new frame_block_list::item_type; auto *g = new frame_block_list::item_type;
f->address = 0x2000; g->address = 0x2000;
f->count = 1; g->count = 1;
free.sorted_insert(f); g->flags = kutil::frame_block_flags::none;
free.sorted_insert(g);
frame_allocator fa(std::move(cache)); frame_allocator fa(std::move(cache));
fa.init(std::move(free), std::move(used)); fa.init(std::move(free), std::move(used));
fa.consolidate_blocks();
uintptr_t a = 0; uintptr_t a = 0;
size_t c = fa.allocate(2, &a); size_t c = fa.allocate(2, &a);
CHECK( a == 0x1000 ); CHECK( a == 0x1000 );
@@ -42,10 +34,12 @@ TEST_CASE( "Frame allocator tests", "[memory frame]" )
fa.free(a, 2); fa.free(a, 2);
a = 0; a = 0;
fa.consolidate_blocks();
c = fa.allocate(2, &a); c = fa.allocate(2, &a);
CHECK( a == 0x1000 ); CHECK( a == 0x1000 );
CHECK( c == 2 ); CHECK( c == 2 );
kutil::setup::set_heap(nullptr); delete f;
free_memory(); delete g;
} }

View File

@@ -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); }
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 );