FreeNOS
PageAllocator.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 <FreeNOS/System.h>
19#include "PageAllocator.h"
20
22 : Allocator(range)
23 , m_allocated(PAGESIZE)
24{
25}
26
28{
29 return size() - m_allocated;
30}
31
33{
36 Memory::Range range;
37 Size bytes = args.size > MinimumAllocationSize ?
39
40 // Check for heap overflow
41 if (m_allocated + bytes >= heapRange.size)
42 {
43 ERROR("cannot allocate beyond maximum heap size " << heapRange.size);
45 }
46
47 // Set return address
48 args.address = base() + m_allocated;
49
50 // Align to pagesize
51 bytes = aligned(bytes, PAGESIZE * 32U);
52
53 // Fill in the message
54 range.size = bytes;
56 range.virt = base() + m_allocated;
57 range.phys = ZERO;
58 const API::Result r = VMCtl(SELF, MapSparse, &range);
59 if (r != API::Success)
60 {
61 ERROR("failed to allocate memory using VMCtl(): " << (int)r);
63 }
64
65 // Update count
66 m_allocated += range.size;
67
68 // Success
69 args.size = range.size;
70 return Success;
71}
72
Result
Enumeration of generic kernel API result codes.
Definition API.h:69
@ Success
Definition API.h:70
Memory mapping for the kernel and user processes on the ARM architecture.
Definition ARMMap.h:38
Memory Allocator.
Definition Allocator.h:47
Address base() const
Get memory base address for allocations.
Definition Allocator.cpp:69
virtual Size size() const
Get memory size.
Definition Allocator.cpp:64
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
@ UserHeap
< User heap
Definition MemoryMap.h:57
Memory::Range range(Region region) const
Get memory range for the given region.
Definition MemoryMap.cpp:36
static const Size MinimumAllocationSize
Minimum size required for allocations.
virtual Result release(const Address addr)
Release memory.
PageAllocator(const Range range)
Class constructor.
Size m_allocated
Total number of bytes allocated.
virtual Size available() const
Get memory available.
virtual Result allocate(Range &args)
Allocate memory.
#define SELF
Definition ProcessID.h:35
API::Result VMCtl(const ProcessID procID, const MemoryOperation op, Memory::Range *range=ZERO)
Prototype for user applications.
Definition VMCtl.h:61
@ MapSparse
Definition VMCtl.h:38
#define PAGESIZE
ARM uses 4K pages.
Definition ARMConstant.h:97
unsigned long Address
A memory address.
Definition Types.h:131
#define ERROR(msg)
Output an error message.
Definition Log.h:61
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128
#define ZERO
Zero value.
Definition Macros.h:43
@ User
Definition Memory.h:44
@ Readable
Definition Memory.h:41
@ Writable
Definition Memory.h:42
Describes a range of memory.
Definition Allocator.h:66
Address address
Starting address of the memory range.
Definition Allocator.h:67
Size size
Amount of memory in bytes.
Definition Allocator.h:68
Memory range.
Definition Memory.h:56
Size size
Size in number of bytes.
Definition Memory.h:59
Address phys
Physical address.
Definition Memory.h:58
Address virt
Virtual address.
Definition Memory.h:57
Access access
Page access flags.
Definition Memory.h:60