usermode/library/pmalloc/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 <string.h>
00036 
00037 #include "arch-specific.h"
00038 #include "config.h"
00039 #include "hoardheap.h"
00040 #include "superblock.h"
00041 #include "persistentheap.h"
00042 #include "persistentsuperblock.h"
00043 
00044 #include <iostream>
00045 using namespace std;
00046 
00047 superblock::superblock (int numBlocks,  // The number of blocks in the sb.
00048                         int szclass,    // The size class of the blocks.
00049                         hoardHeap * o,  // The heap that "owns" this sb.
00050                         persistentSuperblock *psb) // The persistent sb backing this volatile sb
00051                        :
00052 #if HEAP_DEBUG
00053         _magic (SUPERBLOCK_MAGIC),
00054 #endif
00055         _sizeClass (szclass),
00056         _numBlocks (numBlocks),
00057         _numAvailable (0),
00058         _fullness (0),
00059         _freeList (NULL),
00060         _owner (o),
00061         _next (NULL),
00062         _prev (NULL),
00063         dirtyFullness (true)
00064 {
00065         assert (_numBlocks >= 1);
00066 
00067 
00068         // Determine the size of each block. This does not include the buffer 
00069         // since the block is actually a cached header for the 
00070         const int blksize =  hoardHeap::align (sizeof(block));
00071 
00072         // Make sure this size is in fact aligned.
00073         assert ((blksize & hoardHeap::ALIGNMENT_MASK) == 0);
00074 
00075         // Set the first block to just past this volatile superblock header.
00076         block * b = (block *) hoardHeap::align ((unsigned long) (this + 1));
00077 
00078         _psb = psb;
00079         // Initialize all the blocks,
00080         // and insert the block pointers into the linked list.
00081         for (int i = 0; i < _numBlocks; i++) {
00082                 // Make sure the block is on a double-word boundary.
00083                 assert (((uintptr_t) b & hoardHeap::ALIGNMENT_MASK) == 0);
00084                 new (b) block (this);
00085                 assert (b->getSuperblock() == this);
00086                 b->setId (i);
00087                 if (psb->isBlockFree(i)) {
00088                         b->setNext (_freeList);
00089                         _freeList = b;
00090                         _numAvailable++;
00091                 }
00092                 b = (block *) ((char *) b + blksize);
00093         }
00094         computeFullness();
00095         assert ((uintptr_t) b <= hoardHeap::align (sizeof(superblock) + blksize * _numBlocks) + (uintptr_t) this);
00096 
00097         hoardLockInit (_upLock);
00098 }
00099 
00100 
00101 superblock * superblock::makeSuperblock (int sizeclass,
00102                                          persistentSuperblock *pSuperblock)
00103 {
00104         char *buf;
00105         int  numBlocks = hoardHeap::numBlocks(sizeclass);
00106         int  blksize = hoardHeap::sizeFromClass(sizeclass);
00107   
00108         // Compute how much memory we need.
00109         unsigned long moreMemory;
00110 
00111         moreMemory = hoardHeap::align(sizeof(superblock) + (hoardHeap::align (sizeof(block)) * numBlocks));
00112         // Get some memory from the process heap.
00113         buf = (char *) hoardGetMemory (moreMemory);
00114  
00115         // Make sure that we actually got the memory.
00116         if (buf == NULL) {
00117                 return 0;
00118         }
00119         buf = (char *) hoardHeap::align ((unsigned long) buf);
00120 
00121         // Make sure this buffer is double-word aligned.
00122         assert (buf == (char *) hoardHeap::align ((unsigned long) buf));
00123         assert ((((unsigned long) buf) & hoardHeap::ALIGNMENT_MASK) == 0);
00124 
00125         // If sizeclass is different from the pSuperblock's sizeclass then make
00126         // sure that the pSuperblock is free
00127         if (pSuperblock->getBlockSize() != blksize) {
00128                 assert(pSuperblock->isFree() == true);
00129                 pSuperblock->setBlockSize(blksize);
00130         }
00131 
00132         // Instantiate the new superblock in the buffer.
00133 
00134         superblock * sb = new (buf) superblock (numBlocks, sizeclass, NULL, pSuperblock);
00135         pSuperblock->setSuperblock(sb);
00136 
00137         return sb;
00138 }
00139 
00140 
00141 superblock * superblock::makeSuperblock (persistentSuperblock *pSuperblock)
00142 {
00143         // We need to get more memory.
00144 
00145         int  blksize = pSuperblock->getBlockSize();
00146         int  sizeclass = hoardHeap::sizeClass(blksize);
00147 
00148         return makeSuperblock(sizeclass, pSuperblock);
00149 }
00150 
00151 
00152 block *superblock::getBlock (int id)
00153 {
00154         block * b = (block *) hoardHeap::align ((unsigned long) (this + 1));
00155         const int blksize = hoardHeap::align (sizeof(block));
00156   
00157         b = (block *) ((char *) b + id*blksize);
00158         return b;
00159 }

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