Loading...
Searching...
No Matches
fixed_sized_memory_block_allocator.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) 2021 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_FIXED_MEMORY_BLOCK_POOL_INCLUDED
#define ETL_FIXED_MEMORY_BLOCK_POOL_INCLUDED
#include "platform.h"
#include "alignment.h"
#include "generic_pool.h"
#include "imemory_block_allocator.h"
namespace etl
{
//*************************************************************************
//*************************************************************************
template <size_t VBlock_Size, size_t VAlignment, size_t VSize>
class fixed_sized_memory_block_allocator : public imemory_block_allocator
{
public:
static ETL_CONSTANT size_t Block_Size = VBlock_Size;
static ETL_CONSTANT size_t Alignment = VAlignment;
static ETL_CONSTANT size_t Size = VSize;
//*************************************************************************
//*************************************************************************
fixed_sized_memory_block_allocator() {}
protected:
//*************************************************************************
//*************************************************************************
virtual void* allocate_block(size_t required_size, size_t required_alignment) ETL_OVERRIDE
{
if ((required_alignment <= Alignment) && (required_size <= Block_Size) && !pool.full())
{
return pool.template allocate<block>();
}
else
{
return ETL_NULLPTR;
}
}
//*************************************************************************
//*************************************************************************
virtual bool release_block(const void* const pblock) ETL_OVERRIDE
{
if (pool.is_in_pool(pblock))
{
pool.release(static_cast<const block* const>(pblock));
return true;
}
else
{
return false;
}
}
//*************************************************************************
//*************************************************************************
virtual bool is_owner_of_block(const void* const pblock) const ETL_OVERRIDE
{
return pool.is_in_pool(pblock);
}
private:
struct block
{
char data[Block_Size];
};
etl::generic_pool<Block_Size, Alignment, Size> pool;
};
template <size_t VBlock_Size, size_t VAlignment, size_t VSize>
ETL_CONSTANT size_t fixed_sized_memory_block_allocator<VBlock_Size, VAlignment, VSize>::Block_Size;
template <size_t VBlock_Size, size_t VAlignment, size_t VSize>
ETL_CONSTANT size_t fixed_sized_memory_block_allocator<VBlock_Size, VAlignment, VSize>::Alignment;
template <size_t VBlock_Size, size_t VAlignment, size_t VSize>
ETL_CONSTANT size_t fixed_sized_memory_block_allocator<VBlock_Size, VAlignment, VSize>::Size;
} // namespace etl
#endif
alignment.h
etl::fixed_sized_memory_block_allocator::allocate_block
virtual void * allocate_block(size_t required_size, size_t required_alignment) ETL_OVERRIDE
The overridden virtual function to allocate a block.
Definition fixed_sized_memory_block_allocator.h:64
etl::fixed_sized_memory_block_allocator::is_owner_of_block
virtual bool is_owner_of_block(const void *const pblock) const ETL_OVERRIDE
Returns true if the allocator is the owner of the block.
Definition fixed_sized_memory_block_allocator.h:95
etl::fixed_sized_memory_block_allocator::fixed_sized_memory_block_allocator
fixed_sized_memory_block_allocator()
Default constructor.
Definition fixed_sized_memory_block_allocator.h:57
etl::fixed_sized_memory_block_allocator::release_block
virtual bool release_block(const void *const pblock) ETL_OVERRIDE
The overridden virtual function to release a block.
Definition fixed_sized_memory_block_allocator.h:79
etl::imemory_block_allocator::imemory_block_allocator
imemory_block_allocator()
Default constructor.
Definition imemory_block_allocator.h:50
etl::imemory_block_allocator::allocate
void * allocate(size_t required_size, size_t required_alignment)
Definition imemory_block_allocator.h:57
generic_pool.h
etl::generic_pool
Definition generic_pool.h:56
imemory_block_allocator.h
etl
Definition absolute.h:40
etl::data
ETL_CONSTEXPR TContainer::pointer data(TContainer &container)
Definition iterator.h:1470
platform.h