FreeNOS
BubbleAllocator.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 <Types.h>
19 #include "BubbleAllocator.h"
20 
22  : Allocator(range)
23  , m_allocated(0)
24 {
25 }
26 
28 {
29  return size() - m_allocated;
30 }
31 
33 {
34  Size needed = aligned(args.size, alignment());
35 
36  // Do we still have enough room?
37  if (m_allocated + needed <= size())
38  {
39  args.address = base() + m_allocated;
40  m_allocated += needed;
41  return Success;
42  }
43  // No more memory available
44  return OutOfMemory;
45 }
46 
48 {
49  // BubbleAllocator never releases memory
50  return InvalidAddress;
51 }
BubbleAllocator::BubbleAllocator
BubbleAllocator(const Range range)
Class constructor.
Definition: BubbleAllocator.cpp:21
Types.h
BubbleAllocator::release
virtual Result release(const Address addr)
Release memory.
Definition: BubbleAllocator.cpp:47
Allocator
Memory Allocator.
Definition: Allocator.h:46
Address
unsigned long Address
A memory address.
Definition: Types.h:131
BubbleAllocator::allocate
virtual Result allocate(Range &args)
Allocate memory.
Definition: BubbleAllocator.cpp:32
Allocator::Range::address
Address address
Starting address of the memory range.
Definition: Allocator.h:67
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
BubbleAllocator::available
virtual Size available() const
Get memory available.
Definition: BubbleAllocator.cpp:27
Allocator::Success
@ Success
Definition: Allocator.h:55
BubbleAllocator.h
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
BubbleAllocator::m_allocated
Size m_allocated
Number of bytes allocated.
Definition: BubbleAllocator.h:80
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::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
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