usermode/library/malloc-hoard-old/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 //                 may call sbrk() (via makeSuperblock).
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         // Look for a free block.
00052         // We usually have memory locally so we first look for space in the
00053         // superblock list.
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                 // We don't have memory locally.
00064                 // Try to get more from the persistent heap.
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                 // If we didn't get any memory from the process heap,
00075                 // we'll have to allocate our own superblock.
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                                 // We're out of memory!
00081                                 unlock ();
00082                                 return NULL;
00083                         }
00084 #if HEAP_LOG
00085                         // Record the memory allocation.
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                 // Get a block from the superblock.
00096                 b = sb->acquireBlock ();
00097                 assert (b != NULL);
00098         // Insert the superblock into our list.
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         // Skip past the block header and return the pointer.
00122         return allocRegionPtr;
00123 }

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