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 {
34  Arch::MemoryMap map;
35  Memory::Range heapRange = map.range(MemoryMap::UserHeap);
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 
74 {
75  return InvalidAddress;
76 }
PageAllocator::PageAllocator
PageAllocator(const Range range)
Class constructor.
Definition: PageAllocator.cpp:21
Memory::Range
Memory range.
Definition: Memory.h:55
API::Result
Result
Enumeration of generic kernel API result codes.
Definition: API.h:68
MapSparse
@ MapSparse
Definition: VMCtl.h:38
Allocator
Memory Allocator.
Definition: Allocator.h:46
PageAllocator::release
virtual Result release(const Address addr)
Release memory.
Definition: PageAllocator.cpp:73
PageAllocator::allocate
virtual Result allocate(Range &args)
Allocate memory.
Definition: PageAllocator.cpp:32
Memory::Writable
@ Writable
Definition: Memory.h:42
Memory::User
@ User
Definition: Memory.h:44
PAGESIZE
#define PAGESIZE
ARM uses 4K pages.
Definition: ARMConstant.h:97
PageAllocator::MinimumAllocationSize
static const Size MinimumAllocationSize
Minimum size required for allocations.
Definition: PageAllocator.h:41
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
VMCtl
API::Result VMCtl(const ProcessID procID, const MemoryOperation op, Memory::Range *range=ZERO)
Prototype for user applications.
Definition: VMCtl.h:61
Memory::Readable
@ Readable
Definition: Memory.h:41
Allocator::OutOfMemory
@ OutOfMemory
Definition: Allocator.h:59
Allocator::Range::size
Size size
Amount of memory in bytes.
Definition: Allocator.h:68
Allocator::InvalidAddress
@ InvalidAddress
Definition: Allocator.h:56
ARMMap
Memory mapping for the kernel and user processes on the ARM architecture.
Definition: ARMMap.h:37
SELF
#define SELF
Definition: ProcessID.h:35
PageAllocator.h
Allocator::Success
@ Success
Definition: Allocator.h:55
Memory::Range::phys
Address phys
Physical address.
Definition: Memory.h:58
MemoryMap::range
Memory::Range range(Region region) const
Get memory range for the given region.
Definition: MemoryMap.cpp:36
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::base
Address base() const
Get memory base address for allocations.
Definition: Allocator.cpp:69
Allocator::Range
Describes a range of memory.
Definition: Allocator.h:65
PageAllocator::m_allocated
Size m_allocated
Total number of bytes allocated.
Definition: PageAllocator.h:87
ERROR
#define ERROR(msg)
Output an error message.
Definition: Log.h:61
API::Success
@ Success
Definition: API.h:70
Memory::Range::virt
Address virt
Virtual address.
Definition: Memory.h:57
MemoryMap::UserHeap
@ UserHeap
< User heap
Definition: MemoryMap.h:57
Memory::Range::size
Size size
Size in number of bytes.
Definition: Memory.h:59
Memory::Range::access
Access access
Page access flags.
Definition: Memory.h:60
ZERO
#define ZERO
Zero value.
Definition: Macros.h:43
PageAllocator::available
virtual Size available() const
Get memory available.
Definition: PageAllocator.cpp:27
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