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 
36 {
37  return m_alloc.allocate(args);
38 }
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 
85 {
86  return m_alloc.allocateAt(addr);
87 }
88 
90 {
91  return m_alloc.release(addr);
92 }
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 
106 bool SplitAllocator::isAllocated(const Address page) const
107 {
108  return m_alloc.isAllocated(page);
109 }
SplitAllocator::allocate
virtual Result allocate(Range &args)
Allocate physical memory.
Definition: SplitAllocator.cpp:35
SplitAllocator::m_alloc
BitAllocator m_alloc
Physical memory allocator.
Definition: SplitAllocator.h:141
SplitAllocator.h
SplitAllocator::m_pageSize
const Size m_pageSize
Size of a memory page.
Definition: SplitAllocator.h:147
CallbackFunction::execute
virtual void execute(void *parameter)=0
Execute the callback.
Allocator
Memory Allocator.
Definition: Allocator.h:46
CallbackFunction
Represents a callback function.
Definition: Callback.h:34
BitAllocator::isAllocated
bool isAllocated(const Address page) const
Check if a chunk is allocated.
Definition: BitAllocator.cpp:88
SplitAllocator::available
virtual Size available() const
Get memory available.
Definition: SplitAllocator.cpp:30
BitAllocator::allocateAt
Result allocateAt(const Address addr)
Allocate a specific address.
Definition: BitAllocator.cpp:80
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
Allocator::Range::alignment
Size alignment
Alignment in bytes or ZERO for default alignment.
Definition: Allocator.h:69
Allocator::OutOfMemory
@ OutOfMemory
Definition: Allocator.h:59
BitAllocator::available
virtual Size available() const
Get available memory.
Definition: BitAllocator.cpp:36
Allocator::Range::size
Size size
Amount of memory in bytes.
Definition: Allocator.h:68
Allocator::Success
@ Success
Definition: Allocator.h:55
BitAllocator::release
virtual Result release(const Address chunk)
Release memory chunk.
Definition: BitAllocator.cpp:97
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
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
SplitAllocator::m_virtRange
const Range m_virtRange
Virtual memory range to manage.
Definition: SplitAllocator.h:144
SplitAllocator::release
virtual Result release(const Address addr)
Release memory page.
Definition: SplitAllocator.cpp:89
SplitAllocator::toPhysical
Address toPhysical(const Address virt) const
Convert Address to physical pointer.
Definition: SplitAllocator.cpp:100
ZERO
#define ZERO
Zero value.
Definition: Macros.h:43
BitAllocator::allocate
virtual Result allocate(Range &args)
Allocate memory.
Definition: BitAllocator.cpp:41
SplitAllocator::SplitAllocator
SplitAllocator(const Range physRange, const Range virtRange, const Size pageSize)
Class constructor.
Definition: SplitAllocator.cpp:20
SplitAllocator::isAllocated
bool isAllocated(const Address page) const
Check if a physical page is allocated.
Definition: SplitAllocator.cpp:106
Allocator::InvalidSize
@ InvalidSize
Definition: Allocator.h:57
SplitAllocator::toVirtual
Address toVirtual(const Address phys) const
Convert Address to virtual pointer.
Definition: SplitAllocator.cpp:94
Allocator::Result
Result
Allocation results.
Definition: Allocator.h:53
SplitAllocator::allocateSparse
Result allocateSparse(const Range &range, CallbackFunction *function)
Allocate sparse (non-contiguous) physical memory.
Definition: SplitAllocator.cpp:40