usermode/library/pmalloc/src/threadheap.cpp

00001 
00002 //
00003 // The Hoard Multiprocessor Memory Allocator
00004 // www.hoard.org
00005 //
00006 // Author: Emery Berger, http://www.cs.utexas.edu/users/emery
00007 //
00008 // Copyright (c) 1998-2001, The University of Texas at Austin.
00009 //
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Library General Public License as
00012 // published by the Free Software Foundation, http://www.fsf.org.
00013 //
00014 // This library is distributed in the hope that it will be useful, but
00015 // WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 // Library General Public License for more details.
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 // malloc (sz):
00038 //   inputs: the size of the object to be allocated.
00039 //   returns: a pointer to an object of the appropriate size.
00040 //   side effects: allocates a block from a superblock;
00041 
00042 void * threadHeap::malloc (const size_t size)
00043 {
00044         void      *allocRegionPtr; 
00045         const int sizeclass = sizeClass (size);
00046         block     *b = NULL;
00047 
00048         lock();
00049 
00050         // Look for a free block.
00051         // We usually have memory locally so we first look for space in the
00052         // superblock list.
00053 
00054         superblock * sb = findAvailableSuperblock (sizeclass, b, _persistentHeap);
00055         //std::cout << "threadHeap::malloc[1]: size = " << size << ", sb =  " << sb;
00056         //if (sb) { std::cout << ", owner = " << sb->getOwner();        }       
00057         //std::cout << ", this = " << this  << std::endl;
00058 
00059         if (sb == NULL) {
00060                 // We don't have memory locally.
00061                 // Try to get more from the persistent heap.
00062     
00063                 assert (_persistentHeap);
00064                 sb = _persistentHeap->acquire ((int) sizeclass, this);
00065                 //std::cout << "threadHeap::malloc[2]: sb =  " << sb << ", owner = ";
00066                 //if (sb) { std::cout << ", owner = " << sb->getOwner(); }
00067                 //std::cout << ", this = " << this << std::endl;
00068 
00069                 if (sb == NULL) {
00070                         // We're out of memory!
00071                         unlock ();
00072                         assert(0 && "We're out of memory!\n");
00073                         return NULL;
00074                 }
00075     
00076                 // Get a block from the superblock.
00077                 b = sb->acquireBlock ();
00078                 assert (b != NULL);
00079         // Insert the superblock into our list.
00080                 insertSuperblock (sizeclass, sb, _persistentHeap);
00081         }
00082 
00083         assert (b != NULL);
00084         assert (b->isValid());
00085         assert (sb->isValid());
00086 
00087         b->markAllocated();
00088 
00089         allocRegionPtr = (void *) (b->getSuperblock()->getBlockRegion(b->getId()));
00090 #if HEAP_LOG
00091         MemoryRequest m;
00092         m.malloc (allocRegionPtr, align(size));
00093         _pHeap->getLog(getIndex()).append(m);
00094 #endif
00095 #if HEAP_FRAG_STATS
00096         b->setRequestedSize (align(size));
00097         _pHeap->setAllocated (align(size), 0);
00098 #endif
00099 
00100         unlock();
00101   
00102         // Skip past the block header and return the pointer.
00103         return allocRegionPtr;
00104 }

Generated on Sat Apr 23 11:43:35 2011 for Mnemosyne by  doxygen 1.4.7