usermode/library/malloc-original/src/superblock.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 /*
00022   superblock.cpp
00023   ------------------------------------------------------------------------
00024   The superblock class controls a number of blocks (which are
00025   allocatable units of memory).
00026   ------------------------------------------------------------------------
00027   @(#) $Id: superblock.cpp,v 1.45 2001/12/18 02:42:56 emery Exp $
00028   ------------------------------------------------------------------------
00029   Emery Berger                    | <http://www.cs.utexas.edu/users/emery>
00030   Department of Computer Sciences |             <http://www.cs.utexas.edu>
00031   University of Texas at Austin   |                <http://www.utexas.edu>
00032   ========================================================================
00033 */
00034 
00035 #include <new>
00036 #include <string.h>
00037 
00038 #include "arch-specific.h"
00039 #include "config.h"
00040 #include "hoardheap.h"
00041 #include "processheap.h"
00042 #include "superblock.h"
00043 
00044 
00045 superblock::superblock (int numBlocks,  // The number of blocks in the sb.
00046                         int szclass,    // The size class of the blocks.
00047                         hoardHeap * o)  // The heap that "owns" this sb.
00048   :
00049 #if HEAP_DEBUG
00050     _magic (SUPERBLOCK_MAGIC),
00051 #endif
00052     _sizeClass (szclass),
00053     _numBlocks (numBlocks),
00054     _numAvailable (0),
00055     _fullness (0),
00056     _freeList (NULL),
00057     _owner (o),
00058     _next (NULL),
00059     _prev (NULL),
00060     dirtyFullness (true)
00061 {
00062   assert (_numBlocks >= 1);
00063 
00064   // Determine the size of each block.
00065   const int blksize =
00066     hoardHeap::align (sizeof(block) + hoardHeap::sizeFromClass(_sizeClass));
00067 
00068   // Make sure this size is in fact aligned.
00069   assert ((blksize & hoardHeap::ALIGNMENT_MASK) == 0);
00070 
00071   // Set the first block to just past this superblock header.
00072   block * b
00073     = (block *) hoardHeap::align ((unsigned long) (this + 1));
00074 
00075   // Initialize all the blocks,
00076   // and insert the block pointers into the linked list.
00077   for (int i = 0; i < _numBlocks; i++) {
00078     // Make sure the block is on a double-word boundary.
00079     assert (((unsigned int) b & hoardHeap::ALIGNMENT_MASK) == 0);
00080     new (b) block (this);
00081     assert (b->getSuperblock() == this);
00082     b->setNext (_freeList);
00083     _freeList = b;
00084     b = (block *) ((char *) b + blksize);
00085   }
00086   _numAvailable = _numBlocks;
00087   //  computeFullness();
00088   assert ((unsigned long) b <= hoardHeap::align (sizeof(superblock) + blksize * _numBlocks) + (unsigned long) this);
00089 
00090   hoardLockInit (_upLock);
00091 }
00092 
00093 
00094 superblock * superblock::makeSuperblock (int sizeclass,
00095                                          processHeap * pHeap)
00096 {
00097   // We need to get more memory.
00098 
00099   char * buf;
00100   int numBlocks = hoardHeap::numBlocks(sizeclass);
00101 
00102   // Compute how much memory we need.
00103   unsigned long moreMemory;
00104   size_t sz = hoardHeap::sizeFromClass(sizeclass);
00105 
00106   if (numBlocks > 1) {
00107         // hoardHeap::align(sizeof(superblock) + (hoardHeap::align (sizeof(block) + sz)) * numBlocks) <= hoardHeap::SUPERBLOCK_SIZE) {
00108 
00109     moreMemory = hoardHeap::SUPERBLOCK_SIZE;
00110     assert (moreMemory >= hoardHeap::align(sizeof(superblock) + (hoardHeap::align (sizeof(block) + sz)) * numBlocks));
00111 
00112     // Get some memory from the process heap.
00113     buf = (char *) hoardGetMemory (moreMemory);
00114 
00115   } else {
00116     // One object.
00117     assert (numBlocks == 1);
00118 
00119     size_t blksize = hoardHeap::align (sizeof(block) + sz);
00120     moreMemory = hoardHeap::align (sizeof(superblock) + blksize);
00121 
00122     // Get space from the system.
00123     buf = (char *) hoardGetMemory (moreMemory);
00124   }
00125  
00126   // Make sure that we actually got the memory.
00127   if (buf == NULL) {
00128     return 0;
00129   }
00130   buf = (char *) hoardHeap::align ((unsigned long) buf);
00131 
00132   // Make sure this buffer is double-word aligned.
00133   assert (buf == (char *) hoardHeap::align ((unsigned long) buf));
00134   assert ((((unsigned long) buf) & hoardHeap::ALIGNMENT_MASK) == 0);
00135 
00136   // Instantiate the new superblock in the buffer.
00137   superblock * sb = new (buf) superblock (numBlocks, sizeclass, NULL);
00138 
00139   return sb;
00140 }

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