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

ObjAlloc Class Template Reference

A fast (de)allocator of objects. More...

#include <MemAlloc.h>

Inheritance diagram for ObjAlloc

Inheritance graph
[legend]
Collaboration diagram for ObjAlloc:

Collaboration graph
[legend]
List of all members.

Public Methods

 ObjAlloc (ppu32 blockSize = 16)
 blockSize is the amount of objects of type OBJ ObjAlloc allocates memory for in each block.

 ~ObjAlloc ()
 Refer to the destructor of MemAlloc.

OBJ* ConstructAllocate ()
 This method allocates memory for an object of type OBJ via the MemAlloc method Allocate(). More...

void DestructDeallocate (OBJ* pObj)
 ConstructAllocate() destructs the object of type OBJ pointed to by the pObj argument. More...


Detailed Description

template<class OBJ> template class ObjAlloc

A fast (de)allocator of objects.

ObjAlloc behaves very much like its parent class, MemAlloc. Actually, it behaves exactly like MemAlloc, except for the fact that it implements two additional methods, ConstructAllocate() and DestructDeallocate().

As the names migth suggest, these two classes not only (de)allocates memory for an object of type OBJ, but also de-/constructs this memory using the destructor or the default constructor.

The unit size of ObjAlloc is equal to sizeof(OBJ), and cannot be set at runtime.

The basic concept is exactly the same for both ObjAlloc and MemAlloc. Refer to MemAlloc to get a more detailed description of how both of these classes work.


Member Function Documentation

template<classOBJ>
OBJ * ObjAlloc<OBJ>::ConstructAllocate ( ) [inline]
 

This method allocates memory for an object of type OBJ via the MemAlloc method Allocate().

This call to Allocate() migth result in an exception of type bad_alloc that ConstructAllocate() does not catch.

ConstructAllocate() also constructs the allocated memory using the default constructor. If this constructor throws an exception, ConstructAllocate() catches it, deallocates the allocated memory, and rethrows the exception.

Finally, ConstructAllocate() returns the address of the newly allocated and constructed object.

template<classOBJ>
void ObjAlloc<OBJ>::DestructDeallocate ( OBJ * pObj ) [inline]
 

ConstructAllocate() destructs the object of type OBJ pointed to by the pObj argument.

After that, the memory pointed to by the pObj argument is deallocated via the MemAlloc method Deallocate().

If an exception is thrown from the called constructor, the memory pointed to by pObj is still deallocated. The exception is then rethrown.

Throwing exceptions from destructors is bad programming practice, so even though ObjAlloc can handle it, do your best to avoid it.


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