Nebula
Loading...
Searching...
No Matches
win32memorypool.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
18#include "core/types.h"
20
21//------------------------------------------------------------------------------
22namespace Win32
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
68 SLIST_HEADER listHead;
69 ubyte* poolStart; // first valid block header is at poolStart + (align - sizeof(SLIST_ENTRY))
71};
72
73//------------------------------------------------------------------------------
76#if NEBULA_MEMORY_STATS
77inline uint
78Win32MemoryPool::GetAllocCount() const
79{
80 return this->allocCount;
81}
82#endif
83
84//------------------------------------------------------------------------------
87inline uint
89{
90 uint blockSizeWithHeader = blockSize + sizeof(PSLIST_ENTRY);
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
118{
119 return this->alignedBlockSize;
120}
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 Win32
141//------------------------------------------------------------------------------
142
A simple thread-safe memory pool.
Definition win32memorypool.h:25
ubyte * poolStart
Definition win32memorypool.h:69
uint poolSize
Definition win32memorypool.h:62
static const uint NewBlockPattern
Definition win32memorypool.h:56
uint alignedBlockSize
Definition win32memorypool.h:61
~Win32MemoryPool()
destructor
Definition win32memorypool.cc:35
ubyte * poolEnd
Definition win32memorypool.h:70
uint blockSize
Definition win32memorypool.h:60
void Free(void *ptr)
deallocate a block from the pool
Definition win32memorypool.cc:119
static uint ComputeAlignedBlockSize(uint blockSize)
compute the actual block size including alignment and management data
Definition win32memorypool.h:88
uint numBlocks
Definition win32memorypool.h:63
uint GetBlockSize() const
get block size
Definition win32memorypool.h:108
uint GetAlignedBlockSize() const
get aligned block size
Definition win32memorypool.h:117
void * Alloc()
allocate a block from the pool (NOTE: returns 0 if pool exhausted!)
Definition win32memorypool.cc:94
Memory::HeapType heapType
Definition win32memorypool.h:59
static const int BlockAlign
Definition win32memorypool.h:57
SLIST_HEADER listHead
Definition win32memorypool.h:68
Win32MemoryPool()
constructor
Definition win32memorypool.cc:17
void Setup(Memory::HeapType heapType, uint blockSize, uint numBlocks)
setup the memory pool
Definition win32memorypool.cc:57
bool IsPoolBlock(void *ptr) const
return true if block is owned by this pool
Definition win32memorypool.h:135
static const uint FreeBlockPattern
get current allocation count
Definition win32memorypool.h:55
uint GetNumBlocks() const
get number of allocated blocks in pool
Definition win32memorypool.h:99
uint GetPoolSize() const
get pool size
Definition win32memorypool.h:126
HeapType
Heap types are defined here.
Definition osxmemoryconfig.h:25
[TODO: Describe Win32 subsystem]
unsigned char ubyte
Definition types.h:34
unsigned int uint
Definition types.h:31