Added allocate() and static assert test to etl::pool

This commit is contained in:
John Wellbelove 2017-08-09 09:25:09 +01:00
parent dbd642e781
commit 78c81ef929
2 changed files with 46 additions and 0 deletions

View File

@ -39,6 +39,7 @@ SOFTWARE.
#include "nullptr.h"
#include "alignment.h"
#include "error_handler.h"
#include "static_assert.h"
#include <iterator>
#include <algorithm>
@ -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 <typename U>
U* allocate()
{
STATIC_ASSERT(sizeof(U) <= ELEMENT_SIZE, "Type too large for pool");
return ipool::allocate<U>();
}
private:
// The pool element.

View File

@ -233,6 +233,37 @@ namespace
CHECK(pool.is_in_pool(p1));
CHECK(!pool.is_in_pool(&not_in_pool));
}
//*************************************************************************
TEST(test_generic_storage)
{
union Storage
{
uint64_t dummy; // For alignment purposes.
char buffer[1000];
};
etl::pool<Storage, 4> pool;
Test_Data* pdata = pool.allocate<Test_Data>();
new (pdata) Test_Data("ABC", 3);
etl::array<int, 10>* parray = pool.allocate<etl::array<int, 10>>();
new (parray) etl::array<int, 10>();
parray->fill(0x12345678);
etl::array<int, 10> 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());
}
};
}