Pool.h
上传用户:royluo
上传日期:2007-01-05
资源大小:1584k
文件大小:5k
源码类别:

游戏

开发平台:

Visual C++

  1. /*****************************************************************************
  2. *                                                                             
  3. *   Pool.h
  4. *                                                                             
  5. *   Electrical Engineering Faculty - Software Lab                             
  6. *   Spring semester 1998                                                      
  7. *                                                                             
  8. *   Tanks game                                                                
  9. *                                                                             
  10. *   Module description: CPool provides a memory pool template for fast 
  11. *                       allocation/deallocation of T objects; Used for game
  12. *                       message passed in process.
  13. *                       
  14. *                                                                             
  15. *   Authors: Eran Yariv - 28484475                                           
  16. *            Moshe Zur  - 24070856                                           
  17. *                                                                            
  18. *                                                                            
  19. *   Date: 23/09/98                                                           
  20. *                                                                            
  21. ******************************************************************************/
  22. #ifndef _POOL_H_
  23. #define _POOL_H_
  24. //
  25. //  Purpose: CPool provides a memory pool for fast allocation/deallocation of T objects;
  26. //  Usage:   Class T should declare CPool<T> object as a static member (preferably private),
  27. //           and override it's default new and delete operators to call CPool::Alloc and 
  28. //           CPool::Free instead.
  29. //
  30. #define ELEMENTS_PER_BLOCK  512
  31. template <class T>      // class T must provide access to a T* pNext member.
  32. class CPool
  33. {
  34. public:
  35.     CPool () {}     
  36.     ~CPool () {
  37.         // TODO: delete ALL allocated blocks
  38.         ::operator delete (m_pHeadOfBlocksList);
  39.     }
  40.     static void *Alloc (size_t uElementSize);
  41.     static void  Free (void *pDeadElement, size_t uElementSize);
  42. private:
  43.     static T *m_pHeadOfFreeList;
  44.     static T *m_pHeadOfBlocksList;
  45. };
  46. template <class T>
  47. T *CPool<T>::m_pHeadOfFreeList = NULL;
  48. template <class T>
  49. T *CPool<T>::m_pHeadOfBlocksList = NULL;
  50. template <class T>
  51. void *CPool<T>::Alloc (size_t uElementSize)
  52. {
  53.     if (uElementSize != sizeof (T)) 
  54.     {   // send request of the wrong size to ::operator new()
  55.         ASSERT(FALSE);
  56.         //return ::operator new (uElementSize);
  57.         return NULL;
  58.     }
  59.     T *p = m_pHeadOfFreeList;      // try to take an element from the head of list
  60.     if (p)                          // if p is valid just move head of list to next free element
  61.         m_pHeadOfFreeList = p->pNext;
  62.     else {
  63.         // The free list is empty. Allocate a block of memory for ELEMENTS_PER_BLOCK elements:
  64.         
  65.         T *pNewBlock = static_cast<T*>(::operator new (ELEMENTS_PER_BLOCK * sizeof (T)));
  66.         
  67.         // for now - just store a pointer to this single allocated block
  68.         // TODO: add block to list of blocks; meanwhile make sure we don't start another block
  69.         ASSERT(m_pHeadOfBlocksList==NULL);
  70.         if (m_pHeadOfBlocksList)
  71.         {
  72.             return NULL;
  73.         }
  74.         m_pHeadOfBlocksList = pNewBlock;
  75.         // form a new free list by linking the memory chunks together; skip the zeroth element
  76.         // because you'll return that to the caller of operator new
  77.         for (int i = 1; i < ELEMENTS_PER_BLOCK-1; i++)
  78.             pNewBlock[i].pNext = &pNewBlock[i+1];
  79.         // terminate the linked list with a null pointer
  80.         pNewBlock[ELEMENTS_PER_BLOCK-1].pNext = NULL;
  81.         // set p to front of list, m_pHeadOfFreeList to chunk immediately following
  82.         p = pNewBlock;
  83.         m_pHeadOfFreeList = &pNewBlock[1];
  84.     }
  85.     return p;
  86. }
  87. template <class T>
  88. void CPool<T>::Free (void *pDeadElement, size_t uElementSize)
  89. {
  90.     if (pDeadElement == NULL)           // default behavior of ::operator delete()
  91.         return;
  92.     if (uElementSize != sizeof (T)) 
  93.     {   // send request of the wrong size to ::operator delete()
  94.         ASSERT(FALSE);
  95.         //::operator delete(pDeadElement);
  96.         return;
  97.     }
  98.     // cast void pointer to element pointer
  99.     T *pCarcass = static_cast<T*>(pDeadElement);
  100.     // add element to head free list
  101.     pCarcass->pNext = m_pHeadOfFreeList;
  102.     m_pHeadOfFreeList = pCarcass;
  103. }
  104. #endif