Embedded Template Library 1.0
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"
36#include "generic_pool.h"
37#include "alignment.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 }
60
61 protected:
62
63 //*************************************************************************
65 //*************************************************************************
66 virtual void* allocate_block(size_t required_size, size_t required_alignment) ETL_OVERRIDE
67 {
68 if ((required_alignment <= Alignment) &&
69 (required_size <= Block_Size) &&
70 !pool.full())
71 {
72 return pool.template allocate<block>();
73 }
74 else
75 {
76 return ETL_NULLPTR;
77 }
78 }
79
80 //*************************************************************************
82 //*************************************************************************
83 virtual bool release_block(const void* const pblock) ETL_OVERRIDE
84 {
85 if (pool.is_in_pool(pblock))
86 {
87 pool.release(static_cast<const block* const>(pblock));
88 return true;
89 }
90 else
91 {
92 return false;
93 }
94 }
95
96 //*************************************************************************
98 //*************************************************************************
99 virtual bool is_owner_of_block(const void* const pblock) const ETL_OVERRIDE
100 {
101 return pool.is_in_pool(pblock);
102 }
103
104 private:
105
107 struct block
108 {
109 char data[Block_Size];
110 };
111
114 };
115
116 template <size_t VBlock_Size, size_t VAlignment, size_t VSize>
117 ETL_CONSTANT size_t fixed_sized_memory_block_allocator<VBlock_Size, VAlignment, VSize>::Block_Size;
118
119 template <size_t VBlock_Size, size_t VAlignment, size_t VSize>
120 ETL_CONSTANT size_t fixed_sized_memory_block_allocator<VBlock_Size, VAlignment, VSize>::Alignment;
121
122 template <size_t VBlock_Size, size_t VAlignment, size_t VSize>
123 ETL_CONSTANT size_t fixed_sized_memory_block_allocator<VBlock_Size, VAlignment, VSize>::Size;
124}
125
126#endif
Definition: fixed_sized_memory_block_allocator.h:47
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:66
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:99
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:83
The interface for a memory block pool.
Definition: imemory_block_allocator.h:44
bool full() const
Definition: ipool.h:311
bool is_in_pool(const void *const p_object) const
Definition: ipool.h:260
void release(const U *const p_object)
Definition: pool.h:157
Definition: pool.h:54
bitset_ext
Definition: absolute.h:38