FreeNOS
Allocator.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 Niek Linnenbank
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef __LIBALLOC_ALLOCATOR_H
19#define __LIBALLOC_ALLOCATOR_H
20#ifndef __ASSEMBLER__
21
22#include <Macros.h>
23#include <Types.h>
24
25#pragma GCC diagnostic push
26#pragma GCC diagnostic ignored "-Wnew-returns-null"
27
47{
48 public:
49
61
65 typedef struct Range
66 {
70 } Range;
71
72 public:
73
77 Allocator();
78
84 Allocator(const Range range);
85
89 virtual ~Allocator();
90
96 static void setDefault(Allocator *alloc);
97
103 static Allocator *getDefault();
104
111
117 Allocator * parent();
118
124 Address base() const;
125
131 Size alignment() const;
132
138 virtual Size size() const;
139
145 virtual Size available() const;
146
155 virtual Result allocate(Range & range);
156
166 virtual Result release(const Address addr);
167
168 protected:
169
181 Address aligned(const Address addr, const Size boundary) const;
182
183 private:
184
187
190
193};
194
200#ifndef __HOST__
201
207inline void * operator new(__SIZE_TYPE__ sz)
208{
209 Allocator::Range alloc_args;
210
211 alloc_args.size = sz;
212 alloc_args.alignment = 0;
213
215 return (void *) alloc_args.address;
216 else
217 return (void *) NULL;
218}
219
225inline void * operator new[](__SIZE_TYPE__ sz)
226{
227 Allocator::Range alloc_args;
228
229 alloc_args.size = sz;
230 alloc_args.alignment = 0;
231
233 return (void *) alloc_args.address;
234 else
235 return (void *) NULL;
236}
237
243inline void operator delete (void *mem)
244{
246}
247
253inline void operator delete[] (void *mem)
254{
256}
257
258#endif /* __HOST__ */
259
266inline void * operator new(__SIZE_TYPE__ sz, Address addr)
267{
268 return (void *) addr;
269}
270
280#endif /* __ASSEMBLER__ */
281#endif /* __LIBALLOC_ALLOCATOR_H */
Memory Allocator.
Definition Allocator.h:47
Allocator * m_parent
Our parent Allocator, if any.
Definition Allocator.h:189
virtual Result release(const Address addr)
Release memory.
Definition Allocator.cpp:89
Range m_range
Range of memory that this Allocator manages.
Definition Allocator.h:192
Allocator * parent()
Get parent Allocator.
Definition Allocator.cpp:49
static void setDefault(Allocator *alloc)
Makes the given Allocator the default.
Definition Allocator.cpp:59
virtual Size available() const
Get memory available.
Definition Allocator.cpp:79
Address base() const
Get memory base address for allocations.
Definition Allocator.cpp:69
virtual Size size() const
Get memory size.
Definition Allocator.cpp:64
Size alignment() const
Get memory alignment in bytes for allocations.
Definition Allocator.cpp:74
Allocator()
Default class constructor.
Definition Allocator.cpp:25
virtual Result allocate(Range &range)
Allocate memory.
Definition Allocator.cpp:84
void setParent(Allocator *parent)
Set parent allocator.
Definition Allocator.cpp:44
static Allocator * m_default
Points to the default Allocator for new()/delete().
Definition Allocator.h:186
virtual ~Allocator()
Class destructor.
Definition Allocator.cpp:40
static Allocator * getDefault()
Retrieve the currently default Allocator.
Definition Allocator.cpp:54
Result
Allocation results.
Definition Allocator.h:54
@ InvalidAddress
Definition Allocator.h:56
@ InvalidSize
Definition Allocator.h:57
@ InvalidAlignment
Definition Allocator.h:58
@ OutOfMemory
Definition Allocator.h:59
Address aligned(const Address addr, const Size boundary) const
Align memory address.
Definition Allocator.cpp:94
#define NULL
NULL means zero.
Definition Macros.h:39
unsigned long Address
A memory address.
Definition Types.h:131
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128
Describes a range of memory.
Definition Allocator.h:66
Size alignment
Alignment in bytes or ZERO for default alignment.
Definition Allocator.h:69
Address address
Starting address of the memory range.
Definition Allocator.h:67
Size size
Amount of memory in bytes.
Definition Allocator.h:68