FreeNOS
Allocator.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009 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 #include <Assert.h>
19 #include <Macros.h>
20 #include <MemoryBlock.h>
21 #include "Allocator.h"
22 
24 
26  : m_parent(ZERO)
27 {
28  MemoryBlock::set(&m_range, 0, sizeof(m_range));
29 }
30 
32  : m_parent(ZERO)
33  , m_range(range)
34 {
35  assert(m_range.alignment >= sizeof(u32));
36  assert(m_range.size >= sizeof(u32));
38 }
39 
41 {
42 }
43 
45 {
46  m_parent = parent;
47 }
48 
50 {
51  return m_parent;
52 }
53 
55 {
56  return m_default;
57 }
58 
60 {
61  m_default = alloc;
62 }
63 
65 {
66  return m_range.size;
67 }
68 
70 {
71  return m_range.address;
72 }
73 
75 {
76  return m_range.alignment;
77 }
78 
80 {
81  return m_parent ? m_parent->available() : ZERO;
82 }
83 
85 {
86  return OutOfMemory;
87 }
88 
90 {
91  return InvalidAddress;
92 }
93 
94 Address Allocator::aligned(const Address addr, const Size boundary) const
95 {
96  Address corrected = addr;
97 
98  if (addr % boundary)
99  corrected += boundary - (addr % boundary);
100 
101  return corrected;
102 }
Macros.h
MemoryBlock::set
static void * set(void *dest, int ch, unsigned count)
Fill memory with a constant byte.
Definition: MemoryBlock.cpp:25
Allocator.h
Allocator
Memory Allocator.
Definition: Allocator.h:46
Allocator::m_parent
Allocator * m_parent
Our parent Allocator, if any.
Definition: Allocator.h:189
Assert.h
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
MemoryBlock.h
Allocator::Range::alignment
Size alignment
Alignment in bytes or ZERO for default alignment.
Definition: Allocator.h:69
Allocator::OutOfMemory
@ OutOfMemory
Definition: Allocator.h:59
Allocator::~Allocator
virtual ~Allocator()
Class destructor.
Definition: Allocator.cpp:40
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::allocate
virtual Result allocate(Range &range)
Allocate memory.
Definition: Allocator.cpp:84
u32
unsigned int u32
Unsigned 32-bit number.
Definition: Types.h:53
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
assert
#define assert(exp)
Insert program diagnostics.
Definition: assert.h:60
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
ZERO
#define ZERO
Zero value.
Definition: Macros.h:43
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