Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

DynMemAlloc Class Reference

A fast allocator of variable-size memory chunks. More...

#include <DynMemAlloc.h>

Inheritance diagram for DynMemAlloc

Inheritance graph
[legend]
Collaboration diagram for DynMemAlloc:

Collaboration graph
[legend]
List of all members.

Public Methods

 DynMemAlloc (ppSizeT initialCapacity = 0, ppSizeT capacityGrowth = 16)
 This constructor does nothing but call Reset(initialCapacity, capacityGrowth), where the initialCapacity and capacityGrowth parameters passed to Reset() is the initialCapacity and capacityGrowth parameters passed to this constructor. Notice that if Reset() throws an exception, DynMemAlloc 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 parameters initialCapacity and capacityGrowth does. If you do not want this constructor to throw an exception under any circumstances, then let the capacity parameter have its default value, 0. You can then call Reset() at a later point to set the initial capacity, or just leave it at that and let DynMemAlloc grow itself automaticly.

 ~DynMemAlloc ()
 The destructor deallocates all memory allocated by DynMemAlloc.

void* Allocate (ppSizeT size)
 Allocate() returns the address of a chunk of memory that is as many bytes long as the parameter size. If the size of memory chunk requested is larger than the available capacity, Allocate() will allocate an extra block of memory from the free store using new. Because of the call to new, Allocate() might result in an exception of type bad_alloc in this case. The size of the extra block of memory will be equal to the capacity growth or the size of the requested memory chunk, whichever is larger. Add 4 or 8 bytes, depending on the set alignment behavior, to the size of the memory block. This is for the head of the memory block, which is used to keep track of all allocated blocks. Also, notice that if Allocate() has to allocate an extra block of memory, any excess capacity remaining in the block that was used prior to the allocation of the new block is wasted.

void Reset (ppSizeT initialCapacity = 0, ppSizeT capacityGrowth = 16)
 Reset() deallocates any previously allocated memory. If the argument initialCapacity 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 DynMemAlloc has been set so that it does not care about memory alignment at all. Reset() uses new to accomplish this, so it migth result in an exception of type bad_alloc in this case. If the memory alignment mode for DynMemAlloc has been set so that it aligns all memory chunks along a 32-bit border, then DynMemAlloc 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 DynMemAlloc has been set so that it aligns all memory chunks along a 64-bit border, then DynMemAlloc 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. the capacityGrowth parameter is the mininum size that allocated blocks will have, outside of the first (which has size initialCapacity). Notice that the default value of 16 for the parameter capacityGrowth will in many cases be too small to be effecient.


Protected Methods

void* RawAllocate (ppSizeT size)
 RawAllocate() allocates a chunk of memory exactly as many bytes long as the parameter size (ie, without caring about alignment). It migth allocate an extra block of memory if needed. This is accomplished by means of new, so RawAllocate() migth result in an exception of type bad_alloc in this case.

ppByte* AllocateBlock (ppSizeT size)
 AllocateBlock() allocates a block that is exactly as long as the parameter size, plus the head, which is 4 or 8 bytes long, depending on the set memory alignment behavior. This is accomplished by means of new, so AllocateBlock() migth result in an exception of type bad_alloc. AllocateBlock also updates the list of blocks and the position in this.

void* RawIncrement (void* pAdr, ppSizeT incrementBy)
 This is a utility method used several places internally in DynMemAlloc to increment a pointer without using the special pointer arithmetic rules of C++.


Protected Attributes

Nodem_pBlockListHead
 m_pBlockListHead is a pointer to the first node in a linked lists that contains all allocated blocks. It is nessecary to maintain this list to delete the blocks.

Nodem_pBlockListPos
 m_pBlockListPos is a pointer to the node that is in the head of the block that is currently used.

ppByte* m_pMemPos
 m_pMemPos points to a memory address that is somewhere inside the block currently used. All memory before this address that is still inside the currently used block is in use. The memory pointed to by m_pMemPos, and all memory after it that is still inside the currently used block, is free.

ppByte* m_pUpperBound
 m_pUpperBound is a pointer that contains the address of the byte right after the last byte in the currently used block. So the span of free memory in the currently used block is the interval of byte addresses from (inclusive) m_pMemPos to (exclusive) m_pUpperBound.

ppSizeT m_capacityGrowth
 The mininum size that allocated blocks of memory can have, and the size they will have if a request for memory does not exceed this size.


Detailed Description

A fast allocator of variable-size memory chunks.

The DynMemAlloc class is an allocator that is several times faster than the standard memory allocator both at allocation and deallocation.

This is accomplished by having some restrictions on how it can be used. Though DynMemAlloc can allocate memory chunks of any size, at any time, it cannot deallocate memory chunks one at a time and reuse them as MemAlloc can. Rather, all chunks of memory allocated by DynMemAlloc has to be deallocated all at once. This only happens at destruction and/or reinitialisation.

However, notice that the restriction that StatMemAlloc has that the total amount of memory that can be allocated has to be set at construction and/or reinitialisation, does not apply to DynMemAlloc. DynMemAlloc can allocate more memory as needed.

Use DynMemAlloc when you have only a very vague feeling of how much memory you are going to need, but this memory can be deallocated all at once.


The documentation for this class was generated from the following file:
Generated at Mon Nov 27 01:46:46 2000 for LibPenguinPlay by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000