usermode/library/malloc-original/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 <limits.h>
00022 #include <string.h>
00023 
00024 #include "config.h"
00025 
00026 #include "hoardheap.h"
00027 #include "threadheap.h"
00028 #include "processheap.h"
00029 
00030 
00031 threadHeap::threadHeap (void)
00032   : _pHeap (0)
00033 {}
00034 
00035 
00036 // malloc (sz):
00037 //   inputs: the size of the object to be allocated.
00038 //   returns: a pointer to an object of the appropriate size.
00039 //   side effects: allocates a block from a superblock;
00040 //                 may call sbrk() (via makeSuperblock).
00041 
00042 void * threadHeap::malloc (const size_t size)
00043 {
00044   const int sizeclass = sizeClass (size);
00045   block * b = NULL;
00046 
00047   lock();
00048 
00049   // Look for a free block.
00050   // We usually have memory locally so we first look for space in the
00051   // superblock list.
00052 
00053   superblock * sb = findAvailableSuperblock (sizeclass, b, _pHeap);
00054 
00055   if (sb == NULL) {
00056     
00057     // We don't have memory locally.
00058     // Try to get more from the process heap.
00059     
00060     assert (_pHeap);
00061     sb = _pHeap->acquire ((int) sizeclass, this);
00062     
00063     // If we didn't get any memory from the process heap,
00064     // we'll have to allocate our own superblock.
00065     if (sb == NULL) {
00066       sb = superblock::makeSuperblock (sizeclass, _pHeap);
00067       if (sb == NULL) {
00068         // We're out of memory!
00069         unlock ();
00070         return NULL;
00071       }
00072 #if HEAP_LOG
00073       // Record the memory allocation.
00074       MemoryRequest m;
00075       m.allocate ((int) sb->getNumBlocks() * (int) sizeFromClass(sb->getBlockSizeClass()));
00076       _pHeap->getLog(getIndex()).append(m);
00077 #endif
00078 #if HEAP_FRAG_STATS
00079       _pHeap->setAllocated (0, sb->getNumBlocks() * sizeFromClass(sb->getBlockSizeClass()));
00080 #endif
00081     }
00082     
00083     // Get a block from the superblock.
00084     b = sb->getBlock ();
00085     assert (b != NULL);
00086 
00087     // Insert the superblock into our list.
00088     insertSuperblock (sizeclass, sb, _pHeap);
00089   }
00090 
00091   assert (b != NULL);
00092   assert (b->isValid());
00093   assert (sb->isValid());
00094 
00095   b->markAllocated();
00096 
00097 #if HEAP_LOG
00098   MemoryRequest m;
00099   m.malloc ((void *) (b + 1), align(size));
00100   _pHeap->getLog(getIndex()).append(m);
00101 #endif
00102 #if HEAP_FRAG_STATS
00103   b->setRequestedSize (align(size));
00104   _pHeap->setAllocated (align(size), 0);
00105 #endif
00106 
00107   unlock();
00108   
00109   // Skip past the block header and return the pointer.
00110   return (void *) (b + 1);
00111 }

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