From 241e1dacb04a73aa4c21d502f03a9c11225c803b Mon Sep 17 00:00:00 2001 From: "Justin C. Miller" Date: Thu, 7 Mar 2019 23:53:38 -0800 Subject: [PATCH] Consolidate testing memory setup --- src/libraries/kutil/frame_allocator.cpp | 1 - .../kutil/include/kutil/buddy_allocator.h | 3 ++ .../kutil/include/kutil/frame_allocator.h | 2 +- src/tests/address_manager.cpp | 27 +++----------- src/tests/frame_allocator.cpp | 30 ++++++--------- src/tests/main.cpp | 37 +++++++++++++++++++ 6 files changed, 59 insertions(+), 41 deletions(-) diff --git a/src/libraries/kutil/frame_allocator.cpp b/src/libraries/kutil/frame_allocator.cpp index 75870de..59d8b6c 100644 --- a/src/libraries/kutil/frame_allocator.cpp +++ b/src/libraries/kutil/frame_allocator.cpp @@ -65,7 +65,6 @@ frame_allocator::init( { m_free.append(free); m_used.append(used); - consolidate_blocks(); } void diff --git a/src/libraries/kutil/include/kutil/buddy_allocator.h b/src/libraries/kutil/include/kutil/buddy_allocator.h index 60df8e1..116ad91 100644 --- a/src/libraries/kutil/include/kutil/buddy_allocator.h +++ b/src/libraries/kutil/include/kutil/buddy_allocator.h @@ -22,6 +22,9 @@ public: using region_node = list_node; using region_list = linked_list; + static const size_t min_alloc = (1 << size_min); + static const size_t max_alloc = (1 << size_max); + /// Constructor. buddy_allocator() {} diff --git a/src/libraries/kutil/include/kutil/frame_allocator.h b/src/libraries/kutil/include/kutil/frame_allocator.h index 6ec6009..1bf752e 100644 --- a/src/libraries/kutil/include/kutil/frame_allocator.h +++ b/src/libraries/kutil/include/kutil/frame_allocator.h @@ -48,11 +48,11 @@ public: /// \arg count The number of frames to be freed void free(uintptr_t address, size_t count); -private: /// Consolidate the free and used block lists. Return freed blocks /// to the cache. void consolidate_blocks(); +private: frame_block_list m_free; ///< Free frames list frame_block_list m_used; ///< In-use frames list frame_block_slab m_block_slab; ///< frame_block slab allocator diff --git a/src/tests/address_manager.cpp b/src/tests/address_manager.cpp index 31d3c4e..96fadfd 100644 --- a/src/tests/address_manager.cpp +++ b/src/tests/address_manager.cpp @@ -6,37 +6,25 @@ #include #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(); } diff --git a/src/tests/frame_allocator.cpp b/src/tests/frame_allocator.cpp index a297db9..ece0c04 100644 --- a/src/tests/frame_allocator.cpp +++ b/src/tests/frame_allocator.cpp @@ -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; } diff --git a/src/tests/main.cpp b/src/tests/main.cpp index af78789..8d2aa7f 100644 --- a/src/tests/main.cpp +++ b/src/tests/main.cpp @@ -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 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 default_heap_listener::memory; +kutil::heap_manager *default_heap_listener::heap; + +CATCH_REGISTER_LISTENER( default_heap_listener );