Eneboo - Documentación para desarrolladores
|
00001 // -*- C++ -*- 00002 00003 /* 00004 00005 Heap Layers: An Extensible Memory Allocation Infrastructure 00006 00007 Copyright (c) 1998-2006 Emery Berger, The University of Texas at Austin 00008 http://www.cs.umass.edu/~emery 00009 emery@cs.umass.edu 00010 00011 This program is free software; you can redistribute it and/or modify 00012 it under the terms of the GNU General Public License as published by 00013 the Free Software Foundation; either version 2 of the License, or 00014 (at your option) any later version. 00015 00016 This program is distributed in the hope that it will be useful, 00017 but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 GNU General Public License for more details. 00020 00021 You should have received a copy of the GNU General Public License 00022 along with this program; if not, write to the Free Software 00023 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00024 00025 */ 00026 00027 #ifndef _HOARD_H_ 00028 #define _HOARD_H_ 00029 00030 #include "hldefines.h" 00031 00032 // The minimum allocation grain for a given object - 00033 // that is, we carve objects out of chunks of this size. 00034 #define SUPERBLOCK_SIZE 65536 00035 00036 // The number of 'emptiness classes'; see the ASPLOS paper for details. 00037 #define EMPTINESS_CLASSES 8 00038 00039 00040 // Hoard-specific Heap Layers 00041 00042 #include "check.h" 00043 #include "fixedrequestheap.h" 00044 #include "hoardmanager.h" 00045 #include "addheaderheap.h" 00046 #include "threadpoolheap.h" 00047 #include "redirectfree.h" 00048 #include "ignoreinvalidfree.h" 00049 #include "conformantheap.h" 00050 #include "hoardsuperblock.h" 00051 #include "lockmallocheap.h" 00052 #include "alignedsuperblockheap.h" 00053 #include "alignedmmap.h" 00054 #include "globalheap.h" 00055 00056 // Generic Heap Layers 00057 00058 #include "ansiwrapper.h" 00059 #include "debugheap.h" 00060 #include "lockedheap.h" 00061 #include "winlock.h" 00062 #include "bins4k.h" 00063 #include "bins8k.h" 00064 #include "bins16k.h" 00065 #include "bins64k.h" 00066 #include "oneheap.h" 00067 #include "freelistheap.h" 00068 #include "threadheap.h" 00069 #include "hybridheap.h" 00070 #include "posixlock.h" 00071 #include "spinlock.h" 00072 00073 // Note: I plan to eventually eliminate the use of the spin lock, 00074 // since the right place to do locking is in an OS-supplied library, 00075 // and platforms have substantially improved the efficiency of these 00076 // primitives. 00077 00078 #if defined(_WIN32) 00079 typedef HL::WinLockType TheLockType; 00080 #elif defined(__APPLE__) || defined(__SVR4) 00081 // NOTE: On older versions of the Mac OS, Hoard CANNOT use Posix locks, 00082 // since they may call malloc themselves. However, as of Snow Leopard, 00083 // that problem seems to have gone away. 00084 // typedef HL::PosixLockType TheLockType; 00085 typedef HL::SpinLockType TheLockType; 00086 #else 00087 typedef HL::SpinLockType TheLockType; 00088 #endif 00089 00090 namespace Hoard { 00091 00092 // 00093 // There is just one "global" heap, shared by all of the per-process heaps. 00094 // 00095 00096 typedef GlobalHeap<SUPERBLOCK_SIZE, EMPTINESS_CLASSES, TheLockType> 00097 TheGlobalHeap; 00098 00099 // 00100 // When a thread frees memory and causes a per-process heap to fall 00101 // below the emptiness threshold given in the function below, it 00102 // moves a (nearly or completely empty) superblock to the global heap. 00103 // 00104 00105 class hoardThresholdFunctionClass { 00106 public: 00107 inline static bool function (int u, int a, size_t objSize) { 00108 /* 00109 Returns 1 iff we've crossed the emptiness threshold: 00110 00111 U < A - 2S && U < EMPTINESS_CLASSES-1/EMPTINESS_CLASSES * A 00112 00113 */ 00114 bool r = ((EMPTINESS_CLASSES * u) < ((EMPTINESS_CLASSES-1) * a)) && ((u < a - (2 * SUPERBLOCK_SIZE) / (int) objSize)); 00115 return r; 00116 } 00117 }; 00118 00119 00120 class SmallHeap; 00121 00122 typedef HoardSuperblock<TheLockType, SUPERBLOCK_SIZE, SmallHeap> SmallSuperblockType; 00123 00124 // 00125 // The heap that manages small objects. 00126 // 00127 class SmallHeap : 00128 public ConformantHeap< 00129 HoardManager<AlignedSuperblockHeap<TheLockType, SUPERBLOCK_SIZE>, 00130 TheGlobalHeap, 00131 SmallSuperblockType, 00132 EMPTINESS_CLASSES, 00133 TheLockType, 00134 hoardThresholdFunctionClass, 00135 SmallHeap> > {}; 00136 00137 class BigHeap; 00138 00139 typedef HoardSuperblock<TheLockType, SUPERBLOCK_SIZE, BigHeap> BigSuperblockType; 00140 00141 // The heap that manages large objects. 00142 class BigHeap : 00143 public ConformantHeap<HL::LockedHeap<TheLockType, 00144 AddHeaderHeap<BigSuperblockType, 00145 SUPERBLOCK_SIZE, 00146 AlignedMmap<SUPERBLOCK_SIZE, TheLockType> > > > 00147 {}; 00148 00149 00150 enum { BigObjectSize = 00151 HL::bins<SmallSuperblockType::Header, SUPERBLOCK_SIZE>::BIG_OBJECT }; 00152 00153 // 00154 // Each thread has its own heap for small objects. 00155 // 00156 class PerThreadHoardHeap : 00157 public RedirectFree<LockMallocHeap<SmallHeap>, 00158 SmallSuperblockType> {}; 00159 00160 template <int N, int NH> 00161 class HoardHeap : 00162 public HL::ANSIWrapper< 00163 IgnoreInvalidFree< 00164 HL::HybridHeap<Hoard::BigObjectSize, 00165 ThreadPoolHeap<N, NH, Hoard::PerThreadHoardHeap>, 00166 Hoard::BigHeap> > > 00167 { 00168 public: 00169 00170 enum { BIG_OBJECT = Hoard::BigObjectSize }; 00171 00172 HL::sassert<sizeof(Hoard::BigSuperblockType::Header) 00173 == sizeof(Hoard::SmallSuperblockType::Header)> ensureSameSizeHeaders; 00174 00175 }; 00176 00177 } 00178 00179 00180 #endif