Loading...
Searching...
No Matches
generic_pool.h
Go to the documentation of this file.
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2014 John Wellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#ifndef ETL_GENERIC_POOL_INCLUDED
#define ETL_GENERIC_POOL_INCLUDED
#include "platform.h"
#include "alignment.h"
#include "ipool.h"
#include "static_assert.h"
#include "type_traits.h"
#define ETL_POOL_CPP03_CODE 0
//*****************************************************************************
//*****************************************************************************
namespace etl
{
//*************************************************************************
//*************************************************************************
template <size_t VTypeSize, size_t VAlignment, size_t VSize>
class generic_pool : public etl::ipool
{
public:
static ETL_CONSTANT size_t SIZE = VSize;
static ETL_CONSTANT size_t ALIGNMENT = VAlignment;
static ETL_CONSTANT size_t TYPE_SIZE = VTypeSize;
//*************************************************************************
//*************************************************************************
generic_pool()
: etl::ipool(reinterpret_cast<char*>(&buffer[0]), Element_Size, VSize)
{
}
//*************************************************************************
//*************************************************************************
template <typename U>
U* allocate()
{
ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
return ipool::allocate<U>();
}
#if ETL_CPP11_NOT_SUPPORTED || ETL_POOL_CPP03_CODE || ETL_USING_STLPORT
//*************************************************************************
//*************************************************************************
template <typename U>
U* create()
{
ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
return ipool::create<U>();
}
//*************************************************************************
//*************************************************************************
template <typename U, typename T1>
U* create(const T1& value1)
{
ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
return ipool::create<U>(value1);
}
//*************************************************************************
//*************************************************************************
template <typename U, typename T1, typename T2>
U* create(const T1& value1, const T2& value2)
{
ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
return ipool::create<U>(value1, value2);
}
//*************************************************************************
//*************************************************************************
template <typename U, typename T1, typename T2, typename T3>
U* create(const T1& value1, const T2& value2, const T3& value3)
{
ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
return ipool::create<U>(value1, value2, value3);
}
//*************************************************************************
//*************************************************************************
template <typename U, typename T1, typename T2, typename T3, typename T4>
U* create(const T1& value1, const T2& value2, const T3& value3, const T4& value4)
{
ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
return ipool::create<U>(value1, value2, value3, value4);
}
#else
//*************************************************************************
//*************************************************************************
template <typename U, typename... Args>
U* create(Args&&... args)
{
ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
return ipool::create<U>(etl::forward<Args>(args)...);
}
#endif
//*************************************************************************
//*************************************************************************
template <typename U>
void destroy(const U* const p_object)
{
ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
ipool::destroy(p_object);
}
private:
// The pool element.
union Element
{
char* next;
char value[VTypeSize];
typename etl::type_with_alignment<VAlignment>::type dummy;
};
typename etl::aligned_storage< sizeof(Element), etl::alignment_of<Element>::value>::type buffer[VSize];
static ETL_CONSTANT uint32_t Element_Size = sizeof(Element);
// Should not be copied.
generic_pool(const generic_pool&) ETL_DELETE;
generic_pool& operator=(const generic_pool&) ETL_DELETE;
};
template <size_t VTypeSize, size_t VAlignment, size_t VSize>
ETL_CONSTANT size_t generic_pool<VTypeSize, VAlignment, VSize>::SIZE;
template <size_t VTypeSize, size_t VAlignment, size_t VSize>
ETL_CONSTANT size_t generic_pool<VTypeSize, VAlignment, VSize>::ALIGNMENT;
template <size_t VTypeSize, size_t VAlignment, size_t VSize>
ETL_CONSTANT size_t generic_pool<VTypeSize, VAlignment, VSize>::TYPE_SIZE;
//*************************************************************************
//*************************************************************************
template <size_t VTypeSize, size_t VAlignment>
class generic_pool_ext : public etl::ipool
{
private:
// The pool element.
union element_internal
{
char* next;
char value[VTypeSize];
typename etl::type_with_alignment<VAlignment>::type dummy;
};
static const size_t ELEMENT_INTERNAL_SIZE = sizeof(element_internal);
public:
static ETL_CONSTANT size_t ALIGNMENT = VAlignment;
static ETL_CONSTANT size_t TYPE_SIZE = VTypeSize;
typedef typename etl::aligned_storage< sizeof(element_internal), etl::alignment_of<element_internal>::value>::type element;
//*************************************************************************
//*************************************************************************
generic_pool_ext(element* buffer, size_t size)
: etl::ipool(reinterpret_cast<char*>(&buffer[0]), ELEMENT_INTERNAL_SIZE, size)
{
}
//*************************************************************************
//*************************************************************************
template <typename U>
U* allocate()
{
ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
return ipool::allocate<U>();
}
#if ETL_CPP11_NOT_SUPPORTED || ETL_POOL_CPP03_CODE || ETL_USING_STLPORT
//*************************************************************************
//*************************************************************************
template <typename U>
U* create()
{
ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
return ipool::create<U>();
}
//*************************************************************************
//*************************************************************************
template <typename U, typename T1>
U* create(const T1& value1)
{
ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
return ipool::create<U>(value1);
}
//*************************************************************************
//*************************************************************************
template <typename U, typename T1, typename T2>
U* create(const T1& value1, const T2& value2)
{
ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
return ipool::create<U>(value1, value2);
}
//*************************************************************************
//*************************************************************************
template <typename U, typename T1, typename T2, typename T3>
U* create(const T1& value1, const T2& value2, const T3& value3)
{
ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
return ipool::create<U>(value1, value2, value3);
}
//*************************************************************************
//*************************************************************************
template <typename U, typename T1, typename T2, typename T3, typename T4>
U* create(const T1& value1, const T2& value2, const T3& value3, const T4& value4)
{
ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
return ipool::create<U>(value1, value2, value3, value4);
}
#else
//*************************************************************************
//*************************************************************************
template <typename U, typename... Args>
U* create(Args&&... args)
{
ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
return ipool::create<U>(etl::forward<Args>(args)...);
}
#endif
//*************************************************************************
//*************************************************************************
template <typename U>
void destroy(const U* const p_object)
{
ETL_STATIC_ASSERT(etl::alignment_of<U>::value <= VAlignment, "Type has incompatible alignment");
ETL_STATIC_ASSERT(sizeof(U) <= VTypeSize, "Type too large for pool");
ipool::destroy(p_object);
}
private:
// Should not be copied.
generic_pool_ext(const generic_pool_ext&) ETL_DELETE;
generic_pool_ext& operator=(const generic_pool_ext&) ETL_DELETE;
};
template <size_t VTypeSize, size_t VAlignment>
ETL_CONSTANT size_t generic_pool_ext<VTypeSize, VAlignment>::ALIGNMENT;
template <size_t VTypeSize, size_t VAlignment>
ETL_CONSTANT size_t generic_pool_ext<VTypeSize, VAlignment>::TYPE_SIZE;
} // namespace etl
#endif
alignment.h
etl::aligned_storage
Definition alignment.h:251
type
etl::ipool::size
size_t size() const
Returns the number of allocated items in the pool.
Definition ipool.h:523
etl::generic_pool::create
U * create(const T1 &value1)
Definition generic_pool.h:106
etl::generic_pool::create
U * create(const T1 &value1, const T2 &value2, const T3 &value3, const T4 &value4)
Definition generic_pool.h:148
etl::generic_pool_ext::create
U * create(const T1 &value1)
Definition generic_pool.h:279
etl::generic_pool_ext::create
U * create(const T1 &value1, const T2 &value2, const T3 &value3)
Definition generic_pool.h:307
etl::generic_pool_ext::generic_pool_ext
generic_pool_ext(element *buffer, size_t size)
Constructor.
Definition generic_pool.h:239
etl::generic_pool::create
U * create(const T1 &value1, const T2 &value2, const T3 &value3)
Definition generic_pool.h:134
etl::generic_pool::create
U * create(const T1 &value1, const T2 &value2)
Definition generic_pool.h:120
etl::ipool::allocate
T * allocate()
Definition ipool.h:334
etl::generic_pool_ext::create
U * create(const T1 &value1, const T2 &value2, const T3 &value3, const T4 &value4)
Definition generic_pool.h:321
etl::generic_pool_ext::create
U * create()
Definition generic_pool.h:265
etl::ipool::ipool
ipool(char *p_buffer_, uint32_t item_size_, uint32_t max_size_)
Constructor.
Definition ipool.h:551
etl::generic_pool::allocate
U * allocate()
Definition generic_pool.h:78
etl::generic_pool::generic_pool
generic_pool()
Constructor.
Definition generic_pool.h:66
etl::generic_pool_ext::create
U * create(const T1 &value1, const T2 &value2)
Definition generic_pool.h:293
etl::generic_pool::destroy
void destroy(const U *const p_object)
Definition generic_pool.h:173
etl::generic_pool::create
U * create()
Definition generic_pool.h:92
etl::generic_pool_ext::allocate
U * allocate()
Definition generic_pool.h:251
etl::ipool::create
T * create()
Definition ipool.h:351
etl::ipool::destroy
void destroy(const T *const p_object)
Definition ipool.h:444
etl::generic_pool_ext::destroy
void destroy(const U *const p_object)
Definition generic_pool.h:346
etl::generic_pool_ext
Definition generic_pool.h:216
etl::ipool
Definition ipool.h:110
ipool.h
etl
Definition absolute.h:40
platform.h
etl::type
type_traits.h