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
43
45{
47}
48
50{
51 return m_parent;
52}
53
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
88
93
94Address 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}
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
@ OutOfMemory
Definition Allocator.h:59
Address aligned(const Address addr, const Size boundary) const
Align memory address.
Definition Allocator.cpp:94
static void * set(void *dest, int ch, unsigned count)
Fill memory with a constant byte.
#define assert(exp)
Insert program diagnostics.
Definition assert.h:60
unsigned int u32
Unsigned 32-bit number.
Definition Types.h:53
unsigned long Address
A memory address.
Definition Types.h:131
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128
#define ZERO
Zero value.
Definition Macros.h:43
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