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 
46 class Allocator
47 {
48  public:
49 
53  enum Result
54  {
55  Success = 0,
60  };
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 
110  void setParent(Allocator *parent);
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 
207 inline void * operator new(__SIZE_TYPE__ sz)
208 {
209  Allocator::Range alloc_args;
210 
211  alloc_args.size = sz;
212  alloc_args.alignment = 0;
213 
214  if (Allocator::getDefault()->allocate(alloc_args) == Allocator::Success)
215  return (void *) alloc_args.address;
216  else
217  return (void *) NULL;
218 }
219 
225 inline void * operator new[](__SIZE_TYPE__ sz)
226 {
227  Allocator::Range alloc_args;
228 
229  alloc_args.size = sz;
230  alloc_args.alignment = 0;
231 
232  if (Allocator::getDefault()->allocate(alloc_args) == Allocator::Success)
233  return (void *) alloc_args.address;
234  else
235  return (void *) NULL;
236 }
237 
243 inline void operator delete (void *mem)
244 {
246 }
247 
253 inline void operator delete[] (void *mem)
254 {
256 }
257 
258 #endif /* __HOST__ */
259 
266 inline void * operator new(__SIZE_TYPE__ sz, Address addr)
267 {
268  return (void *) addr;
269 }
270 
280 #endif /* __ASSEMBLER__ */
281 #endif /* __LIBALLOC_ALLOCATOR_H */
Macros.h
Types.h
Allocator
Memory Allocator.
Definition: Allocator.h:46
Allocator::m_parent
Allocator * m_parent
Our parent Allocator, if any.
Definition: Allocator.h:189
Address
unsigned long Address
A memory address.
Definition: Types.h:131
Allocator::Range::address
Address address
Starting address of the memory range.
Definition: Allocator.h:67
Allocator::Range::alignment
Size alignment
Alignment in bytes or ZERO for default alignment.
Definition: Allocator.h:69
Allocator::Range
struct Allocator::Range Range
Describes a range of memory.
Allocator::OutOfMemory
@ OutOfMemory
Definition: Allocator.h:59
Allocator::~Allocator
virtual ~Allocator()
Class destructor.
Definition: Allocator.cpp:40
Allocator::InvalidAlignment
@ InvalidAlignment
Definition: Allocator.h:58
Allocator::Range::size
Size size
Amount of memory in bytes.
Definition: Allocator.h:68
Allocator::InvalidAddress
@ InvalidAddress
Definition: Allocator.h:56
Allocator::parent
Allocator * parent()
Get parent Allocator.
Definition: Allocator.cpp:49
Allocator::Success
@ Success
Definition: Allocator.h:55
NULL
#define NULL
NULL means zero.
Definition: Macros.h:39
Allocator::allocate
virtual Result allocate(Range &range)
Allocate memory.
Definition: Allocator.cpp:84
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
Allocator::size
virtual Size size() const
Get memory size.
Definition: Allocator.cpp:64
Allocator::available
virtual Size available() const
Get memory available.
Definition: Allocator.cpp:79
Allocator::getDefault
static Allocator * getDefault()
Retrieve the currently default Allocator.
Definition: Allocator.cpp:54
Allocator::base
Address base() const
Get memory base address for allocations.
Definition: Allocator.cpp:69
Allocator::alignment
Size alignment() const
Get memory alignment in bytes for allocations.
Definition: Allocator.cpp:74
Allocator::Range
Describes a range of memory.
Definition: Allocator.h:65
Allocator::setDefault
static void setDefault(Allocator *alloc)
Makes the given Allocator the default.
Definition: Allocator.cpp:59
Allocator::release
virtual Result release(const Address addr)
Release memory.
Definition: Allocator.cpp:89
Allocator::Allocator
Allocator()
Default class constructor.
Definition: Allocator.cpp:25
Allocator::m_range
Range m_range
Range of memory that this Allocator manages.
Definition: Allocator.h:192
Allocator::setParent
void setParent(Allocator *parent)
Set parent allocator.
Definition: Allocator.cpp:44
Allocator::InvalidSize
@ InvalidSize
Definition: Allocator.h:57
Allocator::m_default
static Allocator * m_default
Points to the default Allocator for new()/delete().
Definition: Allocator.h:186
Allocator::Result
Result
Allocation results.
Definition: Allocator.h:53
Allocator::aligned
Address aligned(const Address addr, const Size boundary) const
Align memory address.
Definition: Allocator.cpp:94