Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members

ObjectPool.h

00001 // Copyright (C) 1999 Jean-Marc Valin
00002 
00003 #ifndef OBJECT_POOL_H
00004 #define OBJECT_POOL_H
00005 
00006 #include <vector>
00007 #include "multithread.h"
00008 
00009 namespace FD {
00010 
00011 #define MAX_STORE 100
00012 
00013 template <class T>
00014 class ObjectPool {
00015   protected:
00016    static std::vector <T *> stack;
00017    static FastMutex mutex;
00018   public:
00019    ObjectPool() 
00020    {
00021       //Do not put anything there... everything is static!
00022    }
00023 
00024    ~ObjectPool() 
00025    {
00026       //Do not put anything there... everything is static!
00027    }
00028 
00029    static T *alloc()
00030    {
00031       mutex.lock();
00032       if (stack.size())
00033       {
00034          T *ret = stack.back();
00035          stack.pop_back();
00036          ret->ref();
00037          mutex.unlock();
00038          return ret;
00039       } else {
00040          mutex.unlock();
00041          return new T;
00042       }
00043    }
00044 
00045    static void release(T *obj)
00046    {
00047       mutex.lock();
00048       if (stack.size() > MAX_STORE)
00049       {
00050          delete obj;
00051       } else {
00052          stack.push_back(obj);
00053       }
00054       mutex.unlock();
00055    }
00056 
00057 };
00058 }//namespace FD
00059 #endif

Generated on Wed Oct 5 14:28:55 2005 for FlowDesigner by  doxygen 1.4.4