Embedded Template Library 1.0
Loading...
Searching...
No Matches
fixed_sized_memory_block_allocator.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2021 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_FIXED_MEMORY_BLOCK_POOL_INCLUDED
32#define ETL_FIXED_MEMORY_BLOCK_POOL_INCLUDED
33
34#include "platform.h"
35#include "alignment.h"
36#include "generic_pool.h"
38
39namespace etl
40{
41 //*************************************************************************
44 //*************************************************************************
45 template <size_t VBlock_Size, size_t VAlignment, size_t VSize>
47 {
48 public:
49
50 static ETL_CONSTANT size_t Block_Size = VBlock_Size;
51 static ETL_CONSTANT size_t Alignment = VAlignment;
52 static ETL_CONSTANT size_t Size = VSize;
53
54 //*************************************************************************
56 //*************************************************************************
58
59 protected:
60
61 //*************************************************************************
63 //*************************************************************************
64 virtual void* allocate_block(size_t required_size, size_t required_alignment) ETL_OVERRIDE
65 {
66 if ((required_alignment <= Alignment) && (required_size <= Block_Size) && !pool.full())
67 {
68 return pool.template allocate<block>();
69 }
70 else
71 {
72 return ETL_NULLPTR;
73 }
74 }
75
76 //*************************************************************************
78 //*************************************************************************
79 virtual bool release_block(const void* const pblock) ETL_OVERRIDE
80 {
81 if (pool.is_in_pool(pblock))
82 {
83 pool.release(static_cast<const block* const>(pblock));
84 return true;
85 }
86 else
87 {
88 return false;
89 }
90 }
91
92 //*************************************************************************
94 //*************************************************************************
95 virtual bool is_owner_of_block(const void* const pblock) const ETL_OVERRIDE
96 {
97 return pool.is_in_pool(pblock);
98 }
99
100 private:
101
103 struct block
104 {
105 char data[Block_Size];
106 };
107
110 };
111
112 template <size_t VBlock_Size, size_t VAlignment, size_t VSize>
113 ETL_CONSTANT size_t fixed_sized_memory_block_allocator<VBlock_Size, VAlignment, VSize>::Block_Size;
114
115 template <size_t VBlock_Size, size_t VAlignment, size_t VSize>
116 ETL_CONSTANT size_t fixed_sized_memory_block_allocator<VBlock_Size, VAlignment, VSize>::Alignment;
117
118 template <size_t VBlock_Size, size_t VAlignment, size_t VSize>
119 ETL_CONSTANT size_t fixed_sized_memory_block_allocator<VBlock_Size, VAlignment, VSize>::Size;
120} // namespace etl
121
122#endif
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
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
fixed_sized_memory_block_allocator()
Default constructor.
Definition fixed_sized_memory_block_allocator.h:57
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
imemory_block_allocator()
Default constructor.
Definition imemory_block_allocator.h:50
void * allocate(size_t required_size, size_t required_alignment)
Definition imemory_block_allocator.h:57
Definition generic_pool.h:56
bitset_ext
Definition absolute.h:40
ETL_CONSTEXPR TContainer::pointer data(TContainer &container)
Definition iterator.h:1228