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);
75  assert(isAllocated(args.address));
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 
88 bool 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 }
BitArray::set
void set(const Size bit, const bool value=true)
Sets the given bit to the given value.
Definition: BitArray.cpp:50
BitAllocator::allocateFrom
Result allocateFrom(Range &args, const Size startBit)
Allocate memory from defined starting address.
Definition: BitAllocator.cpp:52
BitArray::isSet
bool isSet(const Size bit) const
Verify if a given bit is set.
Definition: BitArray.cpp:82
BitAllocator::chunkSize
Size chunkSize() const
Get chunk size.
Definition: BitAllocator.cpp:31
Allocator
Memory Allocator.
Definition: Allocator.h:46
BitAllocator::isAllocated
bool isAllocated(const Address page) const
Check if a chunk is allocated.
Definition: BitAllocator.cpp:88
BitAllocator::allocateAt
Result allocateAt(const Address addr)
Allocate a specific address.
Definition: BitAllocator.cpp:80
Assert.h
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::InvalidAlignment
@ InvalidAlignment
Definition: Allocator.h:58
Allocator::Range::size
Size size
Amount of memory in bytes.
Definition: Allocator.h:68
BitAllocator::m_array
BitArray m_array
Marks which chunks are (un)used.
Definition: BitAllocator.h:120
BitArray::Result
Result
Result codes.
Definition: BitArray.h:43
Allocator::Success
@ Success
Definition: Allocator.h:55
BitAllocator::release
virtual Result release(const Address chunk)
Release memory chunk.
Definition: BitAllocator.cpp:97
BitArray::Success
@ Success
Definition: BitArray.h:45
BitAllocator::m_lastBit
Size m_lastBit
Last bit that was set.
Definition: BitAllocator.h:126
BitArray::setNext
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
BitArray::unset
void unset(const Size bit)
Sets the given bit to zero.
Definition: BitArray.cpp:77
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
BitArray::count
Size count(const bool on) const
Get the number of bits in the map which have the given value.
Definition: BitArray.cpp:45
Allocator::base
Address base() const
Get memory base address for allocations.
Definition: Allocator.cpp:69
Allocator::alignment
Size alignment() const
Get memory alignment in bytes for allocations.
Definition: Allocator.cpp:74
Allocator::Range
Describes a range of memory.
Definition: Allocator.h:65
BitAllocator::BitAllocator
BitAllocator(const Range range, const Size chunkSize, u8 *bitmap=ZERO)
Constructor function.
Definition: BitAllocator.cpp:21
assert
#define assert(exp)
Insert program diagnostics.
Definition: assert.h:60
u8
unsigned char u8
Unsigned 8-bit number.
Definition: Types.h:59
BitAllocator.h
BitAllocator::allocate
virtual Result allocate(Range &args)
Allocate memory.
Definition: BitAllocator.cpp:41
BitAllocator::m_chunkSize
const Size m_chunkSize
Size of each chunk.
Definition: BitAllocator.h:123
Allocator::Result
Result
Allocation results.
Definition: Allocator.h:53