FreeNOS
SplitAllocator.cpp
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#include "SplitAllocator.h"
19
21 const Allocator::Range virtRange,
22 const Size pageSize)
23 : Allocator(physRange)
24 , m_alloc(physRange, pageSize)
25 , m_virtRange(virtRange)
26 , m_pageSize(pageSize)
27{
28}
29
31{
32 return m_alloc.available();
33}
34
39
41 CallbackFunction *callback)
42{
43 const Size allocSize = m_pageSize * 8U;
44
45 if (args.size > m_alloc.available())
46 {
47 return OutOfMemory;
48 } else if (args.size % (allocSize)) {
49 return InvalidSize;
50 }
51
52 for (Size i = 0; i < args.size; i += allocSize)
53 {
54 Range alloc_args;
55 alloc_args.address = 0;
56 alloc_args.size = allocSize;
57 alloc_args.alignment = ZERO;
58
59 const Result result = static_cast<Allocator *>(&m_alloc)->allocate(alloc_args);
60 if (result != Success)
61 return result;
62
63 callback->execute(&alloc_args.address);
64 }
65
66 return Success;
67}
68
70 Allocator::Range & virt)
71{
72 Result r = m_alloc.allocate(phys);
73
74 if (r == Success)
75 {
76 virt.address = toVirtual(phys.address);
77 virt.size = phys.size;
78 virt.alignment = phys.alignment;
79 }
80
81 return r;
82}
83
88
93
95{
96 const Size mappingDiff = base() - m_virtRange.address;
97 return phys - mappingDiff;
98}
99
101{
102 const Size mappingDiff = base() - m_virtRange.address;
103 return virt + mappingDiff;
104}
105
107{
108 return m_alloc.isAllocated(page);
109}
Memory Allocator.
Definition Allocator.h:47
Address base() const
Get memory base address for allocations.
Definition Allocator.cpp:69
Result
Allocation results.
Definition Allocator.h:54
@ InvalidSize
Definition Allocator.h:57
@ OutOfMemory
Definition Allocator.h:59
virtual Result release(const Address chunk)
Release memory chunk.
bool isAllocated(const Address page) const
Check if a chunk is allocated.
virtual Size available() const
Get available memory.
Result allocateAt(const Address addr)
Allocate a specific address.
virtual Result allocate(Range &args)
Allocate memory.
Represents a callback function.
Definition Callback.h:35
virtual void execute(void *parameter)=0
Execute the callback.
Address toPhysical(const Address virt) const
Convert Address to physical pointer.
virtual Result allocate(Range &args)
Allocate physical memory.
Result allocateSparse(const Range &range, CallbackFunction *function)
Allocate sparse (non-contiguous) physical memory.
bool isAllocated(const Address page) const
Check if a physical page is allocated.
Address toVirtual(const Address phys) const
Convert Address to virtual pointer.
virtual Result release(const Address addr)
Release memory page.
SplitAllocator(const Range physRange, const Range virtRange, const Size pageSize)
Class constructor.
const Size m_pageSize
Size of a memory page.
virtual Size available() const
Get memory available.
const Range m_virtRange
Virtual memory range to manage.
BitAllocator m_alloc
Physical memory allocator.
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