Embedded Template Library 1.0
imemory_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_MEMORY_BLOCK_ALLOCATOR_INCLUDED
32#define ETL_MEMORY_BLOCK_ALLOCATOR_INCLUDED
33
34#include "platform.h"
35#include "nullptr.h"
36#include "successor.h"
37
38namespace etl
39{
40 //*****************************************************************************
42 //*****************************************************************************
43 class imemory_block_allocator : public successor<imemory_block_allocator>
44 {
45 public:
46
47 //*****************************************************************************
49 //*****************************************************************************
51 {
52 }
53
54 //*****************************************************************************
57 //*****************************************************************************
58 void* allocate(size_t required_size, size_t required_alignment)
59 {
60 // Call the derived implementation.
61 void* p = allocate_block(required_size, required_alignment);
62
63 // If that failed...
64 if (p == ETL_NULLPTR)
65 {
67 if (has_successor())
68 {
69 // ...try to allocate from the next one in the chain.
70 return get_successor().allocate(required_size, required_alignment);
71 }
72 }
73
74 return p;
75 }
76
77 //*****************************************************************************
80 //*****************************************************************************
81 bool release(const void* const p)
82 {
83 bool was_released = release_block(p);
84
85 // If that failed...
86 if (!was_released)
87 {
89 if (has_successor())
90 {
91 // ...try to release from the next one in the chain.
92 was_released = get_successor().release(p);
93 }
94 }
95
96 return was_released;
97 }
98
99 //*****************************************************************************
102 //*****************************************************************************
103 bool is_owner_of(const void* const p) const
104 {
105 bool is_owner = is_owner_of_block(p);
106
107 // If that failed...
108 if (!is_owner)
109 {
111 if (has_successor())
112 {
113 // ...check with the next one in the chain.
114 is_owner = get_successor().is_owner_of(p);
115 }
116 }
117
118 return is_owner;
119 }
120
121 protected:
122
123 virtual void* allocate_block(size_t required_size, size_t required_alignment) = 0;
124 virtual bool release_block(const void* const) = 0;
125 virtual bool is_owner_of_block(const void* const) const = 0;
126
127 private:
128
129 // No copying allowed.
131 imemory_block_allocator& operator =(const imemory_block_allocator&) ETL_DELETE;
132 };
133}
134
135#endif
The interface for a memory block pool.
Definition: imemory_block_allocator.h:44
bool release(const void *const p)
Definition: imemory_block_allocator.h:81
bool is_owner_of(const void *const p) const
Definition: imemory_block_allocator.h:103
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:58
Adds successor traits to a class.
Definition: successor.h:73
bool has_successor() const
Does this have a successor?
Definition: successor.h:184
successor_type & get_successor() const
Definition: successor.h:174
bitset_ext
Definition: absolute.h:38