Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Examples  

Pool.h

Go to the documentation of this file.
00001 #ifndef POOL_H
00002 #define POOL_H
00003 
00004 #include "SundanceDefs.h"
00005 
00006 namespace Sundance
00007 {
00008 
00009   /**
00010    * \ingroup General
00011    * Support class for pool allocation
00012    */
00013 
00014   class Pool {
00015   private:
00016     struct Link { Link* next_; };
00017 
00018     struct Chunk {
00019       enum {csize = 8*(1024-16)};
00020       Chunk* next_;
00021       char mem_[csize];
00022     };
00023 
00024     Chunk* chunks_;
00025 
00026     const unsigned int esize_;
00027     Link* head_;
00028 
00029     /** copy ctor */
00030     Pool(const Pool& other);
00031     void operator=(const Pool& other);
00032     void grow();
00033 
00034   public:
00035     Pool(unsigned int n);
00036     ~Pool();
00037 
00038     void* alloc();
00039     void free(void* b);
00040   };
00041 
00042   inline void* Pool::alloc()
00043     {
00044       if (head_==0) grow();
00045       Link* p = head_;
00046       head_ = p->next_;
00047       return p;
00048     }
00049 
00050   inline void Pool::free(void* b)
00051     {
00052       Link* p = (Link*) b;
00053       p->next_ = head_;
00054       head_ = p;
00055     }
00056 
00057 
00058 
00059 
00060 }
00061 #endif

Contact:
Kevin Long (krlong@ca.sandia.gov)


Documentation generated by