A class for quickly allocating many small data structures of the same size. More...
#include <mi32/mempool.h>
Public Member Functions | |
| MEMPOOL (INT32 SizeHint) | |
| MEMPOOL () | |
| ~MEMPOOL () | |
| template<class _CT > | |
| ERRVALUE | Alloc (_CT *&ptr, UINT32 size, bool bClear=false) |
| template<class _CT > | |
| ERRVALUE | Realloc (_CT *&ptr, UINT32 size, bool bClear=false) |
| void | SetSizeHint (UINT32 size) |
Static Public Member Functions | |
| template<class _CT > | |
| static void | Free (_CT *&ptr) |
A class for quickly allocating many small data structures of the same size.
This class is optimized for allocating a lot of small (< 1K each) data structures. It allocates larger block keeps track of free-lists itself.
The class that MEMPOOL is derived from is exported from misystem.dll MEMPOOL itself is not exported and consists mostly of template methods (which can't be exported from a DLL)
| MEMPOOL::MEMPOOL | ( | ) | [inline] |
Constructor.
| MEMPOOL::MEMPOOL | ( | INT32 | SizeHint | ) | [inline] |
Constructor This version allows us to tell the pool the most common size we will use.
| MEMPOOL::~MEMPOOL | ( | ) | [inline] |
Destructor.
Note: Destroying a MEMPOOL automatically frees anything allocated through it.
| ERRVALUE MEMPOOL::Alloc | ( | _CT *& | ptr, | |
| UINT32 | size, | |||
| bool | bClear = false | |||
| ) | [inline] |
Allocate memory from the mempool.
IMPORTANT! Any memory allocated this way must be freed by calling MEMPOOL::Free(), not MmFree(), free() or delete.
Also note: When calling MmAlloc(), you had to do this... MmAlloc((void**)&ptr, size); Now you must leave off the (void**)&, leaving... MemPool.Alloc(ptr, size); Be very careful to remove the & too, or you'll end up with someting that compiles but doesn't do what you want.
| bClear | Clear to 0 if true |
| static void MEMPOOL::Free | ( | _CT *& | ptr | ) | [inline, static] |
Free a chunk of memory allocated from a MEMPOOL.
Since the allocated chunk knows which pool it came from, this method can be static. This means you can free the memory from a destructor which doesn't know anything about the MEMPOOL.
Note, the pointer will be set to NULL after freeing, and freeing a NULL pointer is harmless.
| ERRVALUE MEMPOOL::Realloc | ( | _CT *& | ptr, | |
| UINT32 | size, | |||
| bool | bClear = false | |||
| ) | [inline] |
Resize a memory block in the mempool.
IMPORTANT! Any memory allocated this way must be freed by calling MEMPOOL::Free(), not MmFree(), free() or delete. Also, you can only reallocate a pointer which was allocated from the same pool.
Since the memory to be reallocated knows how big it is, you don't have to pass the old size, even if clearing the new parts.
Also note: When calling MmAlloc(), you had to do this... MmAlloc((void**)&ptr, size); Now you must leave off the (void**)&, leaving... MemPool.Alloc(ptr, size); Be very careful to remove the & too, or you'll end up with someting that compiles but doesn't do what you want.
| bClear | Clear the new part to 0 if true |
| void MEMPOOL::SetSizeHint | ( | UINT32 | size | ) | [inline] |
Set a default allocation size.
By default, a MEMPOOL will round any allocation size up to the nearest power of 2. You can be more space effeciant by giving it a size hint. This will create a bucket for the specified size so that it will waste less RAM. NOTE! You can only have one size hint! If you try to set multiple hints, it will free anything allocated at the old size.
1.6.1