From 78c81ef9296625c44e9e4177afc73954c0185347 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Wed, 9 Aug 2017 09:25:09 +0100 Subject: [PATCH] Added allocate() and static assert test to etl::pool --- src/pool.h | 15 +++++++++++++++ test/test_pool.cpp | 31 +++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/src/pool.h b/src/pool.h index 4186a889..c6f2545b 100644 --- a/src/pool.h +++ b/src/pool.h @@ -39,6 +39,7 @@ SOFTWARE. #include "nullptr.h" #include "alignment.h" #include "error_handler.h" +#include "static_assert.h" #include #include @@ -328,6 +329,20 @@ namespace etl } + //************************************************************************* + /// Allocate an object from the pool. + /// Uses the default constructor. + /// If asserts or exceptions are enabled and there are no more free items an + /// etl::pool_no_allocation if thrown, otherwise a nullptr is returned. + /// Static asserts if the specified type is too large for the pool. + //************************************************************************* + template + U* allocate() + { + STATIC_ASSERT(sizeof(U) <= ELEMENT_SIZE, "Type too large for pool"); + return ipool::allocate(); + } + private: // The pool element. diff --git a/test/test_pool.cpp b/test/test_pool.cpp index 28cc5f05..5aabf967 100644 --- a/test/test_pool.cpp +++ b/test/test_pool.cpp @@ -233,6 +233,37 @@ namespace CHECK(pool.is_in_pool(p1)); CHECK(!pool.is_in_pool(¬_in_pool)); } + + //************************************************************************* + TEST(test_generic_storage) + { + union Storage + { + uint64_t dummy; // For alignment purposes. + char buffer[1000]; + }; + + etl::pool pool; + + Test_Data* pdata = pool.allocate(); + new (pdata) Test_Data("ABC", 3); + + etl::array* parray = pool.allocate>(); + new (parray) etl::array(); + parray->fill(0x12345678); + + etl::array compare; + compare.fill(0x12345678); + + CHECK(pdata->value == "ABC"); + CHECK(pdata->index == 3); + CHECK(*parray == compare); + + pool.release(parray); + pool.release(pdata); + + CHECK_EQUAL(4U, pool.available()); + } }; }