#include <StatMemAlloc.h>
Inheritance diagram for StatMemAlloc
Public Methods | |
StatMemAlloc (ppSizeT capacity = 0) | |
This constructor does nothing but call Reset(capacity), where the capacity parameter passed to Reset() is the capacity parameter passed to the constructor. More... | |
~StatMemAlloc () | |
The destructor deallocates all memory allocated by StatMemAlloc. | |
void* | Allocate (ppSizeT size) |
Allocate() returns the address of a chunk of memory that is as many bytes long as the parameter size. More... | |
void | Reset (ppSizeT capacity = 0) |
Reset() deallocates any previously allocated memory. More... | |
Protected Methods | |
void* | RawAllocate (ppSizeT size) |
RawAllocate() allocates a chunk of memory exactly as many bytes long as the parameter size (without caring about alignment). | |
Protected Attributes | |
ppByte* | m_pMemory |
This pointer points to the first element of the array that MemStatAlloc allocates memory chunks from. More... | |
ppByte* | m_pMemPos |
m_pMemPos points to a memory address that is somewhere inside the array pointed to by m_pMemory. More... |
The StatMemAlloc class is an allocator that is several times faster than the standard memory allocator both at allocation and deallocation. It is the fastest allocator available in PenguinFile, and perhaps also the fastest possible allocator that can made in C++.
This is accomplished by having some restrictions on how it can be used. Though StatMemAlloc can allocate memory chunks of any size, at any time, the total amount of memory it can allocate is limited, and has to be set at allocation and/or reinitialisation.
Also, StatMemAlloc cannot deallocate memory chunks one at a time and reuse them as MemAlloc can. Rather, all chunks of memory allocated by StatMemAlloc has to be deallocated all at once. This only happens at destruction and/or reinitialisation.
Use StatMemAlloc when you know exactly how much memory are are going to allocate in the worst case, and this memory can be deallocated all at once.
If the amount of memory used in the average or best case is very much lower than the amount of memory used in the worst case, you might want to consider using another allocator. DynMemAlloc performs and behave very much like StatMemAlloc, the difference being that it can grow itself dynamicly, thus eliminating the need for specifying the exact amount of memory to be allocated.
|
This constructor does nothing but call Reset(capacity), where the capacity parameter passed to Reset() is the capacity parameter passed to the constructor. Notice that if Reset() throws an exception, StatMemAlloc does not catch it. So in any case where capacity is not 0, calling this constructor migth result in an exception of type bad_alloc. Refer to Reset() for what the parameter capacity does. If you do not want this constructor to result in an exception, then let the capacity parameter have its default value, 0, and then call Reset() at a later point. This will move the resulting exception from the constructor to an ordinary method, which in some cases is beneficial since handling exceptions thrown from constructors can be very tricky sometimes. |
|
Allocate() returns the address of a chunk of memory that is as many bytes long as the parameter size. In release builds, Allocate() does not check to see if it has enough free memory left to meet the request. If it does not, memory corruption is a very likely result. In debug builds (PP_DEBUGLEVEL >= 3), Allocate() asserts if it is asked to allocate more memory than it has. Notice that Allocate() cannot throw exceptions of any kind, including exceptions of type bad_alloc. |
|
Reset() deallocates any previously allocated memory. If the argument capacity is different from 0, its default value, Reset() also allocates enough memory so that it can allocate an amount of memory equal to the argument capacity, if the memory alignment mode for StatMemAlloc has been set so that it does not care about memory alignment at all. If the memory alignment mode for StatMemAlloc has been set so that it aligns all memory chunks along a 32-bit border, then StatMemAlloc will waste 3 bytes of memory per allocation in the worst case. The exact amount of wasted bytes of memory for an allocation is equal to this expression: size % 4, where size is the requested size of memory chunk, and % is the modulus operator. If the memory alignment mode for StatMemAlloc has been set so that it aligns all memory chunks along a 64-bit border, then StatMemAlloc will waste 7 bytes of memory per allocation in the worst case. The exact amount of wasted bytes of memory for an allocation is equal to this expression: size % 8, where size is the requested size of memory chunk, and % is the modulus operator. Reset() might result in an exception of type bad_alloc if the argument capacity is different from 0, as it calls new in that case. |
|
m_pMemPos points to a memory address that is somewhere inside the array pointed to by m_pMemory. All memory before this address that is still inside the array pointed to by m_pMemory is in use. The memory pointed to by m_pMemPos, and all memory after it that is still inside the array pointed to by m_pMemory, is free. If m_pMemory is equal to 0 (NULL), m_pMemPos is not in use, and can have any value. |
|
This pointer points to the first element of the array that MemStatAlloc allocates memory chunks from. It is nessecary to have a pointer that points to this address to delete the array at destruction and/or reinitialisation. If m_pMemory is equal to 0 (NULL), no array is allocated and Allocate() should not be called. |