Nebula
Loading...
Searching...
No Matches
genericmemorypool.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
18#include "core/types.h"
19#include "threading/safequeue.h"
20
21//------------------------------------------------------------------------------
22namespace Base
23{
25{
26public:
36 void* Alloc();
38 void Free(void* ptr);
40 bool IsPoolBlock(void* ptr) const;
42 uint GetNumBlocks() const;
44 uint GetBlockSize() const;
48 uint GetPoolSize() const;
50 #if NEBULA_MEMORY_STATS
51 uint GetAllocCount() const;
52 #endif
53
54private:
55 static const uint FreeBlockPattern = 0xFE;
56 static const uint NewBlockPattern = 0xFD;
57 static const int BlockAlign = 16;
58
64 #if NEBULA_MEMORY_STATS
65 volatile long allocCount;
66 #endif
67
71};
72
73//------------------------------------------------------------------------------
76#if NEBULA_MEMORY_STATS
77inline uint
78GenericMemoryPool::GetAllocCount() const
79{
80 return this->allocCount;
81}
82#endif
83
84//------------------------------------------------------------------------------
87inline uint
89{
90 uint blockSizeWithHeader = blockSize;
91 uint padding = (BlockAlign - (blockSizeWithHeader % BlockAlign)) % BlockAlign;
92 return (blockSizeWithHeader + padding);
93}
94
95//------------------------------------------------------------------------------
98inline uint
100{
101 return this->numBlocks;
102}
103
104//------------------------------------------------------------------------------
107inline uint
109{
110 return this->blockSize;
111}
112
113//------------------------------------------------------------------------------
116inline uint
121
122//------------------------------------------------------------------------------
125inline uint
127{
128 return this->poolSize;
129}
130
131//------------------------------------------------------------------------------
134inline bool
136{
137 return (ptr >= this->poolStart) && (ptr < this->poolEnd);
138}
139
140} // namespace Base
141//------------------------------------------------------------------------------
142
static const uint FreeBlockPattern
get current allocation count
Definition genericmemorypool.h:55
ubyte * poolEnd
Definition genericmemorypool.h:70
uint GetBlockSize() const
get block size
Definition genericmemorypool.h:108
void Free(void *ptr)
deallocate a block from the pool
Definition genericmemorypool.cc:115
Memory::HeapType heapType
Definition genericmemorypool.h:59
void * Alloc()
allocate a block from the pool (NOTE: returns 0 if pool exhausted!)
Definition genericmemorypool.cc:90
GenericMemoryPool()
constructor
Definition genericmemorypool.cc:16
static const int BlockAlign
Definition genericmemorypool.h:57
Threading::SafeQueue< ubyte * > freeList
Definition genericmemorypool.h:68
uint blockSize
Definition genericmemorypool.h:60
uint GetNumBlocks() const
get number of allocated blocks in pool
Definition genericmemorypool.h:99
static const uint NewBlockPattern
Definition genericmemorypool.h:56
~GenericMemoryPool()
destructor
Definition genericmemorypool.cc:33
uint GetAlignedBlockSize() const
get aligned block size
Definition genericmemorypool.h:117
uint numBlocks
Definition genericmemorypool.h:63
ubyte * poolStart
Definition genericmemorypool.h:69
uint GetPoolSize() const
get pool size
Definition genericmemorypool.h:126
bool IsPoolBlock(void *ptr) const
return true if block is owned by this pool
Definition genericmemorypool.h:135
void Setup(Memory::HeapType heapType, uint blockSize, uint numBlocks)
setup the memory pool
Definition genericmemorypool.cc:54
static uint ComputeAlignedBlockSize(uint blockSize)
compute the actual block size including alignment and management data
Definition genericmemorypool.h:88
uint poolSize
Definition genericmemorypool.h:62
uint alignedBlockSize
Definition genericmemorypool.h:61
Thread-safe version of Util::Queue.
Definition safequeue.h:27
Definition gamecontentserverbase.cc:10
HeapType
Heap types are defined here.
Definition osxmemoryconfig.h:25
unsigned char ubyte
Definition types.h:36
unsigned int uint
Definition types.h:33