Isle
Loading...
Searching...
No Matches
mxmemorypool.h
Go to the documentation of this file.
1#ifndef MXMEMORYPOOL_H
2#define MXMEMORYPOOL_H
3
4#include "decomp.h"
5#include "mxbitset.h"
6#include "mxdebug.h"
7#include "mxtypes.h"
8
9#include <assert.h>
10
20template <size_t BS, size_t NB>
22public:
27 MxMemoryPool() : m_pool(NULL), m_blockSize(BS) {}
28
32 ~MxMemoryPool() { delete[] m_pool; }
33
42
49
55 void Release(MxU8* p_buf);
56
62 MxU32 GetPoolSize() const { return m_blockRef.Size(); }
63
64private:
65 MxU8* m_pool;
66 MxU32 m_blockSize;
67 MxBitset<NB> m_blockRef;
68};
69
70template <size_t BS, size_t NB>
72{
73 assert(m_pool == NULL);
74 assert(m_blockSize);
75 assert(m_blockRef.Size());
76
77 m_pool = new MxU8[GetPoolSize() * m_blockSize * 1024];
78 assert(m_pool);
79
80 return m_pool ? SUCCESS : FAILURE;
81}
82
83template <size_t BS, size_t NB>
85{
86 assert(m_pool != NULL);
87 assert(m_blockSize);
88 assert(m_blockRef.Size());
89
90 for (MxU32 i = 0; i < GetPoolSize(); i++) {
91 if (!m_blockRef[i]) {
92 m_blockRef[i].Flip();
93
94 MxTrace("Get> %d pool: busy %d blocks\n", m_blockSize, m_blockRef.Count());
95
96 return &m_pool[i * m_blockSize * 1024];
97 }
98 }
99
100 return NULL;
101}
102
103template <size_t BS, size_t NB>
105{
106 assert(m_pool != NULL);
107 assert(m_blockSize);
108 assert(m_blockRef.Size());
109
110 MxU32 i = (MxU32) (p_buf - m_pool) / (m_blockSize * 1024);
111
112 assert(i >= 0 && i < GetPoolSize());
113 assert(m_blockRef[i]);
114
115 if (m_blockRef[i]) {
116 m_blockRef[i].Flip();
117 }
118
119 MxTrace("Release> %d pool: busy %d blocks\n", m_blockSize, m_blockRef.Count());
120}
121
122// TEMPLATE: BETA10 0x101464a0
123// MxMemoryPool<64,22>::MxMemoryPool<64,22>
124
125// TEMPLATE: LEGO1 0x100b9100
126// TEMPLATE: BETA10 0x10146590
127// MxMemoryPool<64,22>::~MxMemoryPool<64,22>
128
129// TEMPLATE: BETA10 0x101465c0
130// MxMemoryPool<128,2>::MxMemoryPool<128,2>
131
132// TEMPLATE: LEGO1 0x100b9110
133// TEMPLATE: BETA10 0x101466b0
134// MxMemoryPool<128,2>::~MxMemoryPool<128,2>
135
136// TEMPLATE: BETA10 0x10146780
137// MxMemoryPool<64,22>::Allocate
138
139// TEMPLATE: BETA10 0x101468a0
140// MxMemoryPool<64,22>::GetPoolSize
141
142// TEMPLATE: BETA10 0x101468d0
143// MxMemoryPool<128,2>::Allocate
144
145// TEMPLATE: BETA10 0x101469f0
146// MxMemoryPool<128,2>::GetPoolSize
147
148// TEMPLATE: BETA10 0x10158610
149// MxMemoryPool<64,22>::Release
150
151// TEMPLATE: BETA10 0x101589e0
152// MxMemoryPool<128,2>::Release
153
154// TEMPLATE: BETA10 0x10158e50
155// MxMemoryPool<64,22>::Get
156
157// TEMPLATE: BETA10 0x10158f90
158// MxMemoryPool<128,2>::Get
159
160#endif // MXMEMORYPOOL_H
Templated fixed-size bitset for bit manipulation.
Definition: mxbitset.h:18
MxU32 Size() const
Returns the number of bits (N) this bitset manages.
Definition: mxbitset.h:120
[AI] Fixed-size memory pool template for fast allocation and deallocation.
Definition: mxmemorypool.h:21
MxMemoryPool()
[AI] Constructor.
Definition: mxmemorypool.h:27
MxResult Allocate()
[AI] Allocates the memory pool according to template parameters.
Definition: mxmemorypool.h:71
void Release(MxU8 *p_buf)
[AI] Releases the block at the given pointer back into the pool.
Definition: mxmemorypool.h:104
MxU8 * Get()
[AI] Gets a pointer to the next available block in the memory pool.
Definition: mxmemorypool.h:84
~MxMemoryPool()
[AI] Destructor.
Definition: mxmemorypool.h:32
MxU32 GetPoolSize() const
[AI] Returns the number of blocks in the pool.
Definition: mxmemorypool.h:62
#define NULL
[AI] Null pointer value (C/C++ semantics).
Definition: legotypes.h:26
#define FAILURE
[AI] Used to indicate a failed operation in result codes.
Definition: legotypes.h:34
#define SUCCESS
[AI] Used to indicate a successful operation in result codes.
Definition: legotypes.h:30
#define MxTrace(args)
[AI] Macro for trace logging (non-variadic version, MSVC compatibility), expands to nothing.
Definition: mxdebug.h:55
MxLong MxResult
[AI]
Definition: mxtypes.h:106
unsigned char MxU8
[AI]
Definition: mxtypes.h:8
unsigned int MxU32
[AI]
Definition: mxtypes.h:32