The DejaVU Framework -- hush 3.1

include: professional-program-19-ObjectPoolTest-ObjectPool.h /cygdrive/d/www/media


- [up] [top] - index make include source logic grammar scripts html configure mx slides talks scenes reports projects
<body bgcolor="#FFFFFF" text="#000000">

include 
include 

using namespace std;

//
// template class ObjectPool
//
// Provides an object pool that can be used with any class that provides a
// default constructor.
//
// The object pool constructor creates a pool of objects, which it hands out
// to clients when requested via the acquireObject() method. When a client is
// finished with the object it calls releaseObject() to put the object back
// into the object pool.
//
// The constructor and destructor on each object in the pool will be called only
// once each for the lifetime of the program, not once per acquisition and release.
//
// The primary use of an object pool is to avoid creating and deleting objects
// over and over. The object pool is most suited to applications that use large 
// numbers of objects for short periods of time.
//
// For efficiency, the object pool doesn't perform sanity checks.
// Expects the user to release every acquired object exactly once.
// Expects the user to avoid using any objects that he or she
// has released.
//
// Expects the user not to delete the object pool until every object
// that was acquired has been released. Deleting the object pool invalidates
// any objects that the user had acquired, even if they had not yet been released.
//
template 
<h4 align=right text=red> ObjectPool</h4><hr>
  class ObjectPool
{
 public:
  //
  // Creates an object pool with chunkSize objects.
  // Whenever the object pool runs out of objects, chunkSize
  // more objects will be added to the pool. The pool only grows:
  // objects are never removed from the pool (freed), until
  // the pool is destroyed.
  //
  // chunkSize must be greater than 0.
  //
  ObjectPool(int chunkSize = kDefaultChunkSize)
    throw(invalid_argument, bad_alloc);

  //
  // Frees all the allocated objects. Invalidates any objects that have
  // been aquired for use.
  //
  ~ObjectPool();

  //
  // Reserve an object for use. The reference to the object is invalidated
  // if the object pool itself is freed.
  // 
  // Clients must not free the object!
  //
  T &acquireObject();

  //
  // Return the object to the pool. Clients must not use the object after
  // it has been returned to the pool.
  //
  void releaseObject(T &obj);

 protected:
  //
  // mFreeList stores the objects that are not currently in use
  // by clients.
  //
  queue mFreeList;
  //
  // mAllObjects stores pointers to all the objects, in use
  // or not. This vector is needed in order to ensure that all
  // objects are freed properly in the destructor.
  //
  vector mAllObjects;

  int mChunkSize;
  static const int kDefaultChunkSize;

  //
  // Allocate mChunkSize new objects and adds them
  // to the mFreeList.
  //
  void allocateChunk();
  static void arrayDeleteObject(T *obj);

 private:
  // prevent assignment and pass-by-value
  ObjectPool(const ObjectPool &src);
  ObjectPool &operator=(const ObjectPool &rhs);
};
<hr>

<hr> <style type="text/css"> div.mainnavigate { margin: 20px 2px; /* background-color: #ffffff; */ border: 1px solid black; } </style> <div class=xnavigate> [] <black>readme</black> course(s) preface <black>I</black> 1 2 <black>II</black> 3 4 <black>III</black> 5 6 7 <black>IV</black> 8 9 10 <black>V</black> 11 12 afterthought(s) <black>appendix</black> reference(s) example(s) <black>resource(s)</black> _ </div> <hr>

(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author. </div> <script src="http://www.google-analytics.com/urchin.js" type="text/javascript"> </script> <script type="text/javascript"> _uacct = "UA-2780434-1"; urchinTracker(); </script> </body> </html> <hr> <hr> <table cellpadding=10> <tr> <td> <address> Hush Online Technology </address> hush@cs.vu.nl <br>10/19/08 </td><td> </td> <td></td><td></td><td></td><td></td><td></td><td></td><td></td> <td> </td> </tr> </table>