00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00020 
00021 #include <iostream>
00022 #include <limits.h>
00023 #include <string.h>
00024 
00025 #include "config.h"
00026 
00027 #include "hoardheap.h"
00028 #include "threadheap.h"
00029 #include "persistentheap.h"
00030 
00031 
00032 threadHeap::threadHeap (void)
00033   : _pHeap (0), _persistentHeap(0)
00034 {}
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00042 
00043 void * threadHeap::malloc (const size_t size)
00044 {
00045         void      *allocRegionPtr; 
00046         const int sizeclass = sizeClass (size);
00047         block     *b = NULL;
00048 
00049         lock();
00050 
00051         
00052         
00053         
00054 
00055         superblock * sb = findAvailableSuperblock (sizeclass, b, _persistentHeap);
00056         std::cout << "threadHeap::malloc[1]: size = " << size << ", sb =  " << sb;
00057         if (sb) {
00058                 std::cout << ", owner = " << sb->getOwner();
00059         }       
00060         std::cout << ", this = " << this  << std::endl;
00061 
00062         if (sb == NULL) {
00063                 
00064                 
00065     
00066                 assert (_persistentHeap);
00067                 sb = _persistentHeap->acquire ((int) sizeclass, this);
00068                 std::cout << "threadHeap::malloc[2]: sb =  " << sb << ", owner = ";
00069                 if (sb) {
00070                         std::cout << ", owner = " << sb->getOwner(); 
00071                 }
00072                 std::cout << ", this = " << this << std::endl;
00073 
00074                 
00075                 
00076                 if (sb == NULL) {
00077                         std::cout << "_pHeap = " << _pHeap << ", _persistentHeap = " << _persistentHeap << std::endl;
00078                         sb = superblock::makeSuperblock (sizeclass, _pHeap, _persistentHeap);
00079                         if (sb == NULL) {
00080                                 
00081                                 unlock ();
00082                                 return NULL;
00083                         }
00084 #if HEAP_LOG
00085                         
00086                         MemoryRequest m;
00087                         m.allocate ((int) sb->getNumBlocks() * (int) sizeFromClass(sb->getBlockSizeClass()));
00088                         _persistentHeap->getLog(getIndex()).append(m);
00089 #endif
00090 #if HEAP_FRAG_STATS
00091                         _persistentHeap->setAllocated (0, sb->getNumBlocks() * sizeFromClass(sb->getBlockSizeClass()));
00092 #endif
00093                 }
00094     
00095                 
00096                 b = sb->acquireBlock ();
00097                 assert (b != NULL);
00098         
00099                 insertSuperblock (sizeclass, sb, _persistentHeap);
00100         }
00101 
00102         assert (b != NULL);
00103         assert (b->isValid());
00104         assert (sb->isValid());
00105 
00106         b->markAllocated();
00107 
00108         allocRegionPtr = (void *) (b->getSuperblock()->getBlockRegion(b->getId()));
00109 #if HEAP_LOG
00110         MemoryRequest m;
00111         m.malloc (allocRegionPtr, align(size));
00112         _pHeap->getLog(getIndex()).append(m);
00113 #endif
00114 #if HEAP_FRAG_STATS
00115         b->setRequestedSize (align(size));
00116         _pHeap->setAllocated (align(size), 0);
00117 #endif
00118 
00119         unlock();
00120   
00121         
00122         return allocRegionPtr;
00123 }