Embedded Template Library
1.0
Toggle main menu visibility
Loading...
Searching...
No Matches
imemory_block_allocator.h
Go to the documentation of this file.
1
2
3
/******************************************************************************
4
The MIT License(MIT)
5
6
Embedded Template Library.
7
https://github.com/ETLCPP/etl
8
https://www.etlcpp.com
9
10
Copyright(c) 2021 John Wellbelove
11
12
Permission is hereby granted, free of charge, to any person obtaining a copy
13
of this software and associated documentation files(the "Software"), to deal
14
in the Software without restriction, including without limitation the rights
15
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16
copies of the Software, and to permit persons to whom the Software is
17
furnished to do so, subject to the following conditions :
18
19
The above copyright notice and this permission notice shall be included in all
20
copies or substantial portions of the Software.
21
22
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
SOFTWARE.
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
38
namespace
etl
39
{
40
//*****************************************************************************
42
//*****************************************************************************
43
class
imemory_block_allocator
:
public
successor
<imemory_block_allocator>
44
{
45
public
:
46
47
//*****************************************************************************
49
//*****************************************************************************
50
imemory_block_allocator
() {}
51
52
//*****************************************************************************
56
//*****************************************************************************
57
void
*
allocate
(
size_t
required_size,
size_t
required_alignment)
58
{
59
// Call the derived implementation.
60
void
* p = allocate_block(required_size, required_alignment);
61
62
// If that failed...
63
if
(p == ETL_NULLPTR)
64
{
66
if
(
has_successor
())
67
{
68
// ...try to allocate from the next one in the chain.
69
return
get_successor
().
allocate
(required_size, required_alignment);
70
}
71
}
72
73
return
p;
74
}
75
76
//*****************************************************************************
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
//*****************************************************************************
103
//*****************************************************************************
104
bool
is_owner_of
(
const
void
*
const
p)
const
105
{
106
bool
is_owner = is_owner_of_block(p);
107
108
// If that failed...
109
if
(!is_owner)
110
{
112
if
(
has_successor
())
113
{
114
// ...check with the next one in the chain.
115
is_owner =
get_successor
().
is_owner_of
(p);
116
}
117
}
118
119
return
is_owner;
120
}
121
122
protected
:
123
124
virtual
void
* allocate_block(
size_t
required_size,
size_t
required_alignment) = 0;
125
virtual
bool
release_block(
const
void
*
const
) = 0;
126
virtual
bool
is_owner_of_block(
const
void
*
const
)
const
= 0;
127
128
private
:
129
130
// No copying allowed.
131
imemory_block_allocator
(
const
etl::imemory_block_allocator
&) ETL_DELETE;
132
imemory_block_allocator
& operator=(
const
imemory_block_allocator
&) ETL_DELETE;
133
};
134
}
// namespace etl
135
136
#endif
etl::imemory_block_allocator
The interface for a memory block pool.
Definition
imemory_block_allocator.h:44
etl::imemory_block_allocator::release
bool release(const void *const p)
Definition
imemory_block_allocator.h:81
etl::imemory_block_allocator::is_owner_of
bool is_owner_of(const void *const p) const
Definition
imemory_block_allocator.h:104
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
etl::successor< imemory_block_allocator >::has_successor
bool has_successor() const
Definition
successor.h:184
etl::successor< imemory_block_allocator >::get_successor
successor_type & get_successor() const
Definition
successor.h:174
etl::successor< imemory_block_allocator >::successor
successor()
Definition
successor.h:81
etl
Definition
absolute.h:40
nullptr.h
platform.h
successor.h
include
etl
imemory_block_allocator.h
Generated on
for Embedded Template Library by
1.17.0