ObjectPool< Object > Class Template Reference

A thread-safe templated object pooling class that helps reduce the number of dynamic allocations and deallocations in your app. More...

#include <ObjectPool.h>

Inheritance diagram for ObjectPool< Object >:

Inheritance graph
[legend]
List of all members.

Public Types

typedef void(*) ObjectCallback (Object *obj, void *userData)
 Callback function type for Init and Recycle callbacks.

Public Member Functions

 ObjectPool (uint32 maxPoolSize=100, ObjectCallback recycleCallback=NULL, void *recycleData=NULL, ObjectCallback initCallback=NULL, void *initCallbackData=NULL)
 Constructor.
virtual ~ObjectPool ()
 Destructor.
Object * ObtainObject ()
 Returns a new Object for use (or NULL if no memory available).
void ReleaseObject (Object *obj)
 Adds the given object to our "standby" object list to be recycled, or deletes it if the "standby" list is already at its maximum size.
virtual void * ObtainObjectGeneric ()
 AbstractObjectGenerator API: Useful for polymorphism.
virtual void RecycleObject (void *obj)
 AbstractObjectRecycler API: Useful for polymorphism.
status_t SetInitObjectCallback (ObjectCallback cb, void *userData)
 With this call you can specify a callback function that will be called whenever a new Object is created by this pool.
void SetRecycleObjectCallback (ObjectCallback cb, void *userData)
 With this call you can specify a callback function that will be called by ReleaseObject() whenever it is putting an Object into its standby list.
virtual uint32 FlushCachedObjects ()
 Implemented to call Drain() and return the number of objects drained.
status_t Drain (uint32 *optSetNumDrained=NULL)
 Removes all "spare" objects from the pool and deletes them.
uint32 GetMaxPoolSize () const
 Returns the maximum number of "spare" objects that will be kept in the pool, ready to be recycled.
void SetMaxPoolSize (uint32 mps)
 Sets a new maximum size for this pool.

Friends

class ObjectSlab

Classes

class  ObjectNode
class  ObjectSlab

Detailed Description

template<class Object>
class ObjectPool< Object >

A thread-safe templated object pooling class that helps reduce the number of dynamic allocations and deallocations in your app.

Instead of calling 'new Object', you call myObjectPool.ObtainObject(), and instead of calling 'delete Object', you call myObjectPool.ReleaseObject(). The advantage is that the ObjectPool will keep (up to) a certain number of "spare" Objects around, and recycle them back to you as needed.

Definition at line 92 of file ObjectPool.h.


Constructor & Destructor Documentation

template<class Object>
ObjectPool< Object >::ObjectPool ( uint32  maxPoolSize = 100,
ObjectCallback  recycleCallback = NULL,
void *  recycleData = NULL,
ObjectCallback  initCallback = NULL,
void *  initCallbackData = NULL 
) [inline]

Constructor.

Parameters:
maxPoolSize the maximum number of recycled objects that may be kept around for future reuse at any one time.
recycleCallback optional callback function to be called whenever an object is returned to the pool.
recycleData user value to be passed in to the recycleCallback function.
initCallback optional callback function to be called whenever an object is taken out of the pool.
initCallbackData user value to be passed in to the initCallback function.

Definition at line 106 of file ObjectPool.h.

template<class Object>
virtual ObjectPool< Object >::~ObjectPool (  )  [inline, virtual]

Destructor.

Deletes all objects in the pool.

Definition at line 119 of file ObjectPool.h.

References ObjectPool< Object >::ObjectSlab.


Member Function Documentation

template<class Object>
Object* ObjectPool< Object >::ObtainObject (  )  [inline]

Returns a new Object for use (or NULL if no memory available).

You are responsible for calling ReleaseObject() on this object when you are done with it. This method is thread-safe.

Returns:
a new Object, or NULL if out of memory.

Definition at line 140 of file ObjectPool.h.

References Mutex::Lock(), ObjectPool< Object >::ObjectSlab, and Mutex::Unlock().

Referenced by ObjectPool< Object >::ObtainObjectGeneric().

template<class Object>
void ObjectPool< Object >::ReleaseObject ( Object *  obj  )  [inline]

Adds the given object to our "standby" object list to be recycled, or deletes it if the "standby" list is already at its maximum size.

This method is thread-safe.

Parameters:
obj An Object to recycle or delete. May be NULL.

Definition at line 197 of file ObjectPool.h.

References Mutex::Lock(), ObjectPool< Object >::ObjectSlab, and Mutex::Unlock().

Referenced by ObjectPool< Object >::RecycleObject().

template<class Object>
status_t ObjectPool< Object >::SetInitObjectCallback ( ObjectCallback  cb,
void *  userData 
) [inline]

With this call you can specify a callback function that will be called whenever a new Object is created by this pool.

If you need to do any initialization on the new Object, you can do it here. This method is thread safe.

Parameters:
cb The callback function that ObtainObject() will call.
userData User value that will be passed through to the callback function.
Returns:
B_NO_ERROR on success, B_ERROR if it couldn't lock its lock for some reason.

Definition at line 252 of file ObjectPool.h.

References Mutex::Lock(), and Mutex::Unlock().

template<class Object>
void ObjectPool< Object >::SetRecycleObjectCallback ( ObjectCallback  cb,
void *  userData 
) [inline]

With this call you can specify a callback function that will be called by ReleaseObject() whenever it is putting an Object into its standby list.

If you wish to do any cleanup on the object, you can do it here.

Parameters:
cb The callback function that ReleaseObject() will call.
userData User value that will be passed through to the callback function.
Returns:
B_NO_ERROR on success, B_ERROR if it couldn't lock its lock for some reason.

Definition at line 273 of file ObjectPool.h.

References Mutex::Lock(), and Mutex::Unlock().

template<class Object>
status_t ObjectPool< Object >::Drain ( uint32 *  optSetNumDrained = NULL  )  [inline]

Removes all "spare" objects from the pool and deletes them.

This method is thread-safe.

Parameters:
optSetNumDrained If non-NULL, this value will be set to the number of objects destroyed.
Returns:
B_NO_ERROR on success, or B_ERROR if it couldn't lock the lock for some reason.

Definition at line 293 of file ObjectPool.h.

References Mutex::Lock(), ObjectPool< Object >::ObjectSlab, and Mutex::Unlock().

Referenced by ObjectPool< Object >::FlushCachedObjects().

template<class Object>
uint32 ObjectPool< Object >::GetMaxPoolSize (  )  const [inline]

Returns the maximum number of "spare" objects that will be kept in the pool, ready to be recycled.

This is the value that was previously set either in the constructor or by SetMaxPoolSize().

Definition at line 338 of file ObjectPool.h.

template<class Object>
void ObjectPool< Object >::SetMaxPoolSize ( uint32  mps  )  [inline]

Sets a new maximum size for this pool.

Note that changing this value will not cause any object to be added or removed to the pool immediately; rather the new size will be enforced only on future operations.

Definition at line 345 of file ObjectPool.h.


The documentation for this class was generated from the following file:
Generated on Mon Apr 28 13:59:21 2008 for MUSCLE by  doxygen 1.5.1