Eneboo - Documentación para desarrolladores
src/hoard/src/heapmanager.h
Ir a la documentación de este archivo.
00001 // -*- C++ -*-
00002 
00003 #ifndef _HEAPMANAGER_H_
00004 #define _HEAPMANAGER_H_
00005 
00006 #include <cstdlib>
00007 
00008 #include "guard.h"
00009 #include "cpuinfo.h"
00010 
00011 namespace Hoard {
00012 
00013   template <typename LockType,
00014             typename HeapType>
00015   class HeapManager : public HeapType {
00016   public:
00017 
00018     HeapManager (void)
00019     {
00020       HL::Guard<LockType> g (heapLock);
00021       
00023       int i;
00024       for (i = 0; i < HeapType::MaxThreads; i++) {
00025         HeapType::setTidMap (i, 0);
00026       }
00027       for (i = 0; i < HeapType::MaxHeaps; i++) {
00028         HeapType::setInusemap (i, 0);
00029       }
00030     }
00031 
00033     void chooseZero (void) {
00034       HL::Guard<LockType> g (heapLock);
00035       HeapType::setTidMap ((int) HL::CPUInfo::getThreadId() % MaxThreads, 0);
00036     }
00037 
00038     int findUnusedHeap (void) {
00039 
00040       HL::Guard<LockType> g (heapLock);
00041       
00042       unsigned int tid_original = HL::CPUInfo::getThreadId();
00043       unsigned int tid = tid_original % HeapType::MaxThreads;
00044       
00045       int i = 0;
00046       while ((i < HeapType::MaxHeaps) && (HeapType::getInusemap(i)))
00047         i++;
00048       if (i >= HeapType::MaxHeaps) {
00049         // Every heap is in use: pick heap one.
00050         i = 0;
00051       }
00052 
00053       HeapType::setInusemap (i, 1);
00054       HeapType::setTidMap (tid, i);
00055       
00056       return i;
00057     }
00058 
00059     void releaseHeap (void) {
00060       // Decrement the ref-count on the current heap.
00061       
00062       HL::Guard<LockType> g (heapLock);
00063       
00064       // Statically ensure that the number of threads is a power of two.
00065       enum { VerifyPowerOfTwo = 1 / ((HeapType::MaxThreads & ~(HeapType::MaxThreads-1))) };
00066       
00067       int tid = HL::CPUInfo::getThreadId() & (HeapType::MaxThreads - 1);
00068       int heapIndex = HeapType::getTidMap (tid);
00069       
00070       HeapType::setInusemap (heapIndex, 0);
00071       
00072       // Prevent underruns (defensive programming).
00073       
00074       if (HeapType::getInusemap (heapIndex) < 0) {
00075         HeapType::setInusemap (heapIndex, 0);
00076       }
00077     }
00078     
00079     
00080   private:
00081     
00082     // Disable copying.
00083     
00084     HeapManager (const HeapManager&);
00085     HeapManager& operator= (const HeapManager&);
00086     
00088     LockType heapLock;
00089   };
00090 
00091 }
00092 
00093 #endif
 Todo Clases Namespaces Archivos Funciones Variables 'typedefs' Enumeraciones Valores de enumeraciones Propiedades Amigas 'defines'