FreeNOS
BitAllocator.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 <Assert.h>
19#include "BitAllocator.h"
20
22 const Size chunkSize,
23 u8 *bitmap)
24 : Allocator(range)
25 , m_array(range.size / chunkSize, bitmap)
26 , m_chunkSize(chunkSize)
27 , m_lastBit(0)
28{
29}
30
32{
33 return m_chunkSize;
34}
35
37{
38 return m_array.count(false) * m_chunkSize;
39}
40
42{
44 if (result == OutOfMemory)
45 {
46 return allocateFrom(args, 0);
47 } else {
48 return result;
49 }
50}
51
53 const Size startBit)
54{
55 Size num = (args.size) / m_chunkSize;
56 BitArray::Result result;
57 Size bit, alignment = 1;
58
59 if ((args.size) % m_chunkSize)
60 num++;
61
62 if (args.alignment)
63 {
64 if (args.alignment % m_chunkSize)
65 return InvalidAlignment;
66 else
68 }
69
70 result = m_array.setNext(&bit, num, startBit, alignment);
71 if (result != BitArray::Success)
72 return OutOfMemory;
73
74 args.address = base() + (bit * m_chunkSize);
76 m_lastBit = bit;
77 return Success;
78}
79
81{
82 assert(!isAllocated(addr));
83
84 m_array.set((addr - base()) / m_chunkSize);
85 return Success;
86}
87
88bool BitAllocator::isAllocated(const Address addr) const
89{
90 assert(addr >= base());
91 assert(addr < base() + size());
92 assert(((addr - base()) % m_chunkSize) == 0);
93
94 return m_array.isSet((addr - base()) / m_chunkSize);
95}
96
98{
99 assert(isAllocated(addr));
100
101 m_array.unset((addr - base()) / m_chunkSize);
102 return Success;
103}
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
Size alignment() const
Get memory alignment in bytes for allocations.
Definition Allocator.cpp:74
Result
Allocation results.
Definition Allocator.h:54
@ InvalidAlignment
Definition Allocator.h:58
@ OutOfMemory
Definition Allocator.h:59
Size chunkSize() const
Get chunk size.
Result allocateFrom(Range &args, const Size startBit)
Allocate memory from defined starting address.
virtual Result release(const Address chunk)
Release memory chunk.
Size m_lastBit
Last bit that was set.
BitArray m_array
Marks which chunks are (un)used.
bool isAllocated(const Address page) const
Check if a chunk is allocated.
const Size m_chunkSize
Size of each chunk.
virtual Size available() const
Get available memory.
BitAllocator(const Range range, const Size chunkSize, u8 *bitmap=ZERO)
Constructor function.
Result allocateAt(const Address addr)
Allocate a specific address.
virtual Result allocate(Range &args)
Allocate memory.
void set(const Size bit, const bool value=true)
Sets the given bit to the given value.
Definition BitArray.cpp:50
bool isSet(const Size bit) const
Verify if a given bit is set.
Definition BitArray.cpp:82
Size count(const bool on) const
Get the number of bits in the map which have the given value.
Definition BitArray.cpp:45
void unset(const Size bit)
Sets the given bit to zero.
Definition BitArray.cpp:77
Result setNext(Size *bit, const Size count=1, const Size offset=0, const Size boundary=1)
Sets the next unset bit(s).
Definition BitArray.cpp:97
Result
Result codes.
Definition BitArray.h:44
@ Success
Definition BitArray.h:45
#define assert(exp)
Insert program diagnostics.
Definition assert.h:60
unsigned long Address
A memory address.
Definition Types.h:131
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128
unsigned char u8
Unsigned 8-bit number.
Definition Types.h:59
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