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
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
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 */
Memory Allocator.
Definition Allocator.h:47
Allocator * parent()
Get parent Allocator.
Definition Allocator.cpp:49
Result
Allocation results.
Definition Allocator.h:54
Bit memory allocator.
Memory allocator which uses pools that each manage same-sized objects.
static const Size MaximumPoolSize
Maximum power of two size a pool can be (128MiB).
Size calculateObjectCount(const Size objectSize) const
Calculate minimum object count for a Pool.
static const Size MinimumPoolSize
Minimum power of two for a pool size.
Pool * m_pools[MaximumPoolSize+1]
Array of memory pools.
virtual Result allocate(Range &args)
Allocate memory.
Size calculateObjectSize(const Size index) const
Calculate object size given the Pool index number.
virtual Size size() const
Get memory size.
void calculateUsage(Size &totalSize, Size &totalUsed) const
Determine total memory usage.
static const u32 ObjectSignature
Signature value is used to detect object corruption/overflows.
virtual Size available() const
Get memory available.
Pool * retrievePool(const Size inputSize)
Find a Pool of sufficient size.
Result releasePool(Pool *pool)
Release Pool instance memory.
Pool * allocatePool(const uint index, const Size objectCount)
Creates a new Pool instance.
virtual Result release(const Address addr)
Release memory.
unsigned int u32
Unsigned 32-bit number.
Definition Types.h:53
unsigned long Address
A memory address.
Definition Types.h:131
unsigned int uint
Unsigned integer number.
Definition Types.h:44
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128
#define ZERO
Zero value.
Definition Macros.h:43
unsigned char u8
Unsigned 8-bit number.
Definition Types.h:59
Describes a range of memory.
Definition Allocator.h:66
Appended in memory after each object.
u32 signature
Filled with a fixed value to detect corruption/overflows.
This data structure is prepended in memory before each object.
Pool * pool
Points to the Pool instance where this object belongs to.
u32 signature
Filled with a fixed value to detect corruption/overflows.
Allocates same-sized objects from a contiguous block of memory.
Pool * next
Points to the next pool of this size (if any).
Pool * prev
Points to the previous pool of this size (if any).
Pool(const Range &range, const Size objectSize, const Size bitmapSize, u8 *bitmap)
Size index
Index number in the m_pools array where this Pool is stored.
const Size bitmapSize
Size in bytes of the bitmap array.