FreeNOS
PoolAllocator.h
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 #ifndef __LIBALLOC_POOLALLOCATOR_H
19 #define __LIBALLOC_POOLALLOCATOR_H
20 
21 #include <Types.h>
22 #include <Macros.h>
23 #include "Allocator.h"
24 #include "BitAllocator.h"
25 
45 class PoolAllocator : public Allocator
46 {
47  private:
48 
50  static const Size MinimumPoolSize = 2;
51 
53  static const Size MaximumPoolSize = 27;
54 
56  static const u32 ObjectSignature = 0xF7312A56;
57 
61  typedef struct Pool : public BitAllocator
62  {
63  Pool(const Range & range,
64  const Size objectSize,
65  const Size bitmapSize,
66  u8 *bitmap)
67  : BitAllocator(range, objectSize, bitmap)
68  , prev(ZERO)
69  , next(ZERO)
70  , index(0)
72  {
73  }
74 
78  const Size bitmapSize;
79  } Pool;
80 
84  typedef struct ObjectPrefix
85  {
88  } ObjectPrefix;
89 
93  typedef struct ObjectPostfix
94  {
96  } ObjectPostfix;
97 
98  public:
99 
106 
112  virtual Size size() const;
113 
119  virtual Size available() const;
120 
129  virtual Result allocate(Range & args);
130 
140  virtual Result release(const Address addr);
141 
142  private:
143 
151  Size calculateObjectSize(const Size index) const;
152 
160  Size calculateObjectCount(const Size objectSize) const;
161 
168  void calculateUsage(Size & totalSize, Size & totalUsed) const;
169 
177  Pool * retrievePool(const Size inputSize);
178 
187  Pool * allocatePool(const uint index, const Size objectCount);
188 
196  Result releasePool(Pool *pool);
197 
198  private:
199 
202 };
203 
209 #endif /* __LIBALLOC_POOLALLOCATOR_H */
PoolAllocator::Pool::index
Size index
Index number in the m_pools array where this Pool is stored.
Definition: PoolAllocator.h:77
PoolAllocator::m_pools
Pool * m_pools[MaximumPoolSize+1]
Array of memory pools.
Definition: PoolAllocator.h:201
PoolAllocator::size
virtual Size size() const
Get memory size.
Definition: PoolAllocator.cpp:30
PoolAllocator::Pool::bitmapSize
const Size bitmapSize
Size in bytes of the bitmap array.
Definition: PoolAllocator.h:78
Macros.h
PoolAllocator::allocatePool
Pool * allocatePool(const uint index, const Size objectCount)
Creates a new Pool instance.
Definition: PoolAllocator.cpp:212
Types.h
Allocator.h
PoolAllocator::ObjectPostfix::signature
u32 signature
Filled with a fixed value to detect corruption/overflows.
Definition: PoolAllocator.h:95
Allocator
Memory Allocator.
Definition: Allocator.h:46
PoolAllocator::ObjectPrefix::signature
u32 signature
Filled with a fixed value to detect corruption/overflows.
Definition: PoolAllocator.h:86
PoolAllocator::ObjectSignature
static const u32 ObjectSignature
Signature value is used to detect object corruption/overflows.
Definition: PoolAllocator.h:56
PoolAllocator::ObjectPrefix
This data structure is prepended in memory before each object.
Definition: PoolAllocator.h:84
PoolAllocator::Pool::prev
Pool * prev
Points to the previous pool of this size (if any).
Definition: PoolAllocator.h:75
Address
unsigned long Address
A memory address.
Definition: Types.h:131
PoolAllocator::Pool
Allocates same-sized objects from a contiguous block of memory.
Definition: PoolAllocator.h:61
PoolAllocator::calculateObjectCount
Size calculateObjectCount(const Size objectSize) const
Calculate minimum object count for a Pool.
Definition: PoolAllocator.cpp:56
PoolAllocator::ObjectPostfix
Appended in memory after each object.
Definition: PoolAllocator.h:93
PoolAllocator::MinimumPoolSize
static const Size MinimumPoolSize
Minimum power of two for a pool size.
Definition: PoolAllocator.h:50
PoolAllocator::Pool::next
Pool * next
Points to the next pool of this size (if any).
Definition: PoolAllocator.h:76
uint
unsigned int uint
Unsigned integer number.
Definition: Types.h:44
PoolAllocator::Pool
PoolAllocator::Pool Pool
Allocates same-sized objects from a contiguous block of memory.
PoolAllocator::MaximumPoolSize
static const Size MaximumPoolSize
Maximum power of two size a pool can be (128MiB).
Definition: PoolAllocator.h:53
PoolAllocator::ObjectPrefix
struct PoolAllocator::ObjectPrefix ObjectPrefix
This data structure is prepended in memory before each object.
PoolAllocator::calculateObjectSize
Size calculateObjectSize(const Size index) const
Calculate object size given the Pool index number.
Definition: PoolAllocator.cpp:48
PoolAllocator::available
virtual Size available() const
Get memory available.
Definition: PoolAllocator.cpp:38
Allocator::parent
Allocator * parent()
Get parent Allocator.
Definition: Allocator.cpp:49
u32
unsigned int u32
Unsigned 32-bit number.
Definition: Types.h:53
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
PoolAllocator::releasePool
Result releasePool(Pool *pool)
Release Pool instance memory.
Definition: PoolAllocator.cpp:272
PoolAllocator::ObjectPrefix::pool
Pool * pool
Points to the Pool instance where this object belongs to.
Definition: PoolAllocator.h:87
PoolAllocator::retrievePool
Pool * retrievePool(const Size inputSize)
Find a Pool of sufficient size.
Definition: PoolAllocator.cpp:165
PoolAllocator::Pool::Pool
Pool(const Range &range, const Size objectSize, const Size bitmapSize, u8 *bitmap)
Definition: PoolAllocator.h:63
PoolAllocator::PoolAllocator
PoolAllocator(Allocator *parent)
Constructor.
Definition: PoolAllocator.cpp:23
Allocator::Range
Describes a range of memory.
Definition: Allocator.h:65
PoolAllocator::calculateUsage
void calculateUsage(Size &totalSize, Size &totalUsed) const
Determine total memory usage.
Definition: PoolAllocator.cpp:67
PoolAllocator
Memory allocator which uses pools that each manage same-sized objects.
Definition: PoolAllocator.h:45
u8
unsigned char u8
Unsigned 8-bit number.
Definition: Types.h:59
PoolAllocator::release
virtual Result release(const Address addr)
Release memory.
Definition: PoolAllocator.cpp:130
PoolAllocator::allocate
virtual Result allocate(Range &args)
Allocate memory.
Definition: PoolAllocator.cpp:87
BitAllocator
Bit memory allocator.
Definition: BitAllocator.h:40
BitAllocator.h
ZERO
#define ZERO
Zero value.
Definition: Macros.h:43
Allocator::Result
Result
Allocation results.
Definition: Allocator.h:53
PoolAllocator::ObjectPostfix
struct PoolAllocator::ObjectPostfix ObjectPostfix
Appended in memory after each object.