Implements a memory allocation algorithm that balances speed with fair memory use.
More...
#include <jem/util/StdAllocator.h>
Like the other allocator classes, The StdAllocator
class can be used to allocate equally-sized chunks of memory. It is faster than the MallocAllocator
class – that uses simply uses the standardC library function malloc()
– and manages memory more fairly than the FastAllocator
class – which does not free memory at all.
- See also
FastAllocator
, MallocAllocator
.
jem::util::StdAllocator::StdAllocator |
( |
size_t |
itemSize | ) |
|
|
explicit |
Creates a StdAllocator
object that allocates memory chunks of itemSize bytes.
- Parameters
-
itemSize | - the size in bytes of the memory chunks to be allocated. |
- Precondition
itemSize > 0
jem::util::StdAllocator::~StdAllocator |
( |
| ) |
|
Frees the memory that has been allocated by this StdAllocator
.
- Warning
- The lifetime of this allocator should exceed the lifetime of all the memory chunks that have been allocated by this allocator.
void* jem::util::StdAllocator::alloc |
( |
| ) |
|
Allocates a memory chunk of size equal to the argument passed to the constructor of this StdAllocator
object.
- Returns
- A pointer to a newly allocated memory chunk.
- Exceptions
-
void jem::util::StdAllocator::dealloc |
( |
void * |
p | ) |
|
Deallocates the memory chunk pointed to by the pointer p. This chunk must have been allocated by a call to the alloc()
member function.
- Parameters
-
p | - a pointer to the memory chunk to be deallocated. |
- Precondition
- The memory chunk pointed to by p has been allocated by the member function
alloc()
.
Interchanges the internal state of this allocator with the rhs allocator. After calling this function, you must invoke the dealloc()
function on the rhs allocator to deallocate all memory chunks that have been obtained by invoking the alloc()
function on this allocator, and the other way around.
Example:
void* a = tinyAllocator.alloc ();
void* b = hugeAllocator.alloc ();
tinyAllocator.swap ( hugeAllocator );
tinyAllocator.dealloc ( b );
hugeAllocator.dealloc ( a );
- Parameters
-
This function is equivalent with
lhs.swap( rhs )