mirror of
https://github.com/ETLCPP/etl.git
synced 2026-11-09 14:49:53 +08:00
Added allocate() and static assert test to etl::pool
This commit is contained in:
parent
dbd642e781
commit
78c81ef929
15
src/pool.h
15
src/pool.h
@ -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.
|
||||
|
||||
@ -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<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());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user