[176] | 1 | // -*- C++ -*- |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | |
---|
| 5 | Heap Layers: An Extensible Memory Allocation Infrastructure |
---|
| 6 | |
---|
| 7 | Copyright (c) 1998-2006 Emery Berger, The University of Texas at Austin |
---|
| 8 | http://www.cs.umass.edu/~emery |
---|
| 9 | emery@cs.umass.edu |
---|
| 10 | |
---|
| 11 | This program is free software; you can redistribute it and/or modify |
---|
| 12 | it under the terms of the GNU General Public License as published by |
---|
| 13 | the Free Software Foundation; either version 2 of the License, or |
---|
| 14 | (at your option) any later version. |
---|
| 15 | |
---|
| 16 | This program is distributed in the hope that it will be useful, |
---|
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 19 | GNU General Public License for more details. |
---|
| 20 | |
---|
| 21 | You should have received a copy of the GNU General Public License |
---|
| 22 | along with this program; if not, write to the Free Software |
---|
| 23 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 24 | |
---|
| 25 | */ |
---|
| 26 | |
---|
| 27 | #ifndef _HOARD_H_ |
---|
| 28 | #define _HOARD_H_ |
---|
| 29 | |
---|
| 30 | #include "hldefines.h" |
---|
| 31 | |
---|
| 32 | // The minimum allocation grain for a given object - |
---|
| 33 | // that is, we carve objects out of chunks of this size. |
---|
| 34 | #define SUPERBLOCK_SIZE 65536 |
---|
| 35 | |
---|
| 36 | // The number of 'emptiness classes'; see the ASPLOS paper for details. |
---|
| 37 | #define EMPTINESS_CLASSES 8 |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | // Hoard-specific Heap Layers |
---|
| 41 | |
---|
| 42 | #include "check.h" |
---|
| 43 | #include "fixedrequestheap.h" |
---|
| 44 | #include "hoardmanager.h" |
---|
| 45 | #include "addheaderheap.h" |
---|
| 46 | #include "threadpoolheap.h" |
---|
| 47 | #include "redirectfree.h" |
---|
| 48 | #include "ignoreinvalidfree.h" |
---|
| 49 | #include "conformantheap.h" |
---|
| 50 | #include "hoardsuperblock.h" |
---|
| 51 | #include "lockmallocheap.h" |
---|
| 52 | #include "alignedsuperblockheap.h" |
---|
| 53 | #include "alignedmmap.h" |
---|
| 54 | #include "globalheap.h" |
---|
| 55 | |
---|
| 56 | // Generic Heap Layers |
---|
| 57 | |
---|
| 58 | #include "ansiwrapper.h" |
---|
| 59 | #include "debugheap.h" |
---|
| 60 | #include "lockedheap.h" |
---|
| 61 | #include "winlock.h" |
---|
| 62 | #include "bins4k.h" |
---|
| 63 | #include "bins8k.h" |
---|
| 64 | #include "bins16k.h" |
---|
| 65 | #include "bins64k.h" |
---|
| 66 | #include "oneheap.h" |
---|
| 67 | #include "freelistheap.h" |
---|
| 68 | #include "threadheap.h" |
---|
| 69 | #include "hybridheap.h" |
---|
| 70 | #include "posixlock.h" |
---|
| 71 | #include "spinlock.h" |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | #if defined(_WIN32) |
---|
| 75 | typedef HL::WinLockType TheLockType; |
---|
| 76 | #elif defined(__APPLE__) |
---|
| 77 | // Spin lock not working on Mac yet. |
---|
| 78 | typedef HL::PosixLockType TheLockType; |
---|
| 79 | #else |
---|
| 80 | typedef HL::SpinLockType TheLockType; |
---|
| 81 | #endif |
---|
| 82 | |
---|
| 83 | namespace Hoard { |
---|
| 84 | |
---|
| 85 | typedef GlobalHeap<SUPERBLOCK_SIZE, EMPTINESS_CLASSES, TheLockType> TheGlobalHeap; |
---|
| 86 | |
---|
| 87 | class hoardThresholdFunctionClass { |
---|
| 88 | public: |
---|
| 89 | inline static bool function (int u, int a, size_t objSize) { |
---|
| 90 | /* |
---|
| 91 | Returns 1 iff we've crossed the emptiness threshold: |
---|
| 92 | |
---|
| 93 | U < A - 2S && U < EMPTINESS_CLASSES-1/EMPTINESS_CLASSES * A |
---|
| 94 | |
---|
| 95 | */ |
---|
| 96 | bool r = ((EMPTINESS_CLASSES * u) < ((EMPTINESS_CLASSES-1) * a)) && ((u < a - (2 * SUPERBLOCK_SIZE) / (int) objSize)); |
---|
| 97 | return r; |
---|
| 98 | } |
---|
| 99 | }; |
---|
| 100 | |
---|
| 101 | |
---|
| 102 | class SmallHeap; |
---|
| 103 | |
---|
| 104 | typedef HoardSuperblock<TheLockType, SUPERBLOCK_SIZE, SmallHeap> SmallSuperblockType; |
---|
| 105 | |
---|
| 106 | // The heap that manages small objects. |
---|
| 107 | class SmallHeap : |
---|
| 108 | public ConformantHeap< |
---|
| 109 | HoardManager<AlignedSuperblockHeap<TheLockType, SUPERBLOCK_SIZE>, |
---|
| 110 | TheGlobalHeap, |
---|
| 111 | SmallSuperblockType, |
---|
| 112 | EMPTINESS_CLASSES, |
---|
| 113 | TheLockType, |
---|
| 114 | hoardThresholdFunctionClass, |
---|
| 115 | SmallHeap> > {}; |
---|
| 116 | |
---|
| 117 | class BigHeap; |
---|
| 118 | |
---|
| 119 | typedef HoardSuperblock<TheLockType, SUPERBLOCK_SIZE, BigHeap> BigSuperblockType; |
---|
| 120 | |
---|
| 121 | // The heap that manages large objects. |
---|
| 122 | class BigHeap : |
---|
| 123 | public ConformantHeap<HL::LockedHeap<TheLockType, |
---|
| 124 | AddHeaderHeap<BigSuperblockType, |
---|
| 125 | SUPERBLOCK_SIZE, |
---|
| 126 | AlignedMmap<SUPERBLOCK_SIZE> > > > |
---|
| 127 | {}; |
---|
| 128 | |
---|
| 129 | |
---|
| 130 | enum { BigObjectSize = |
---|
| 131 | HL::bins<SmallSuperblockType::Header, SUPERBLOCK_SIZE>::BIG_OBJECT }; |
---|
| 132 | |
---|
| 133 | class PerThreadHoardHeap : |
---|
| 134 | public RedirectFree<LockMallocHeap<SmallHeap>, |
---|
| 135 | SmallSuperblockType> {}; |
---|
| 136 | |
---|
| 137 | template <int N, int NH> |
---|
| 138 | class HoardHeap : |
---|
| 139 | public HL::ANSIWrapper< |
---|
| 140 | IgnoreInvalidFree< |
---|
| 141 | HL::HybridHeap<Hoard::BigObjectSize, |
---|
| 142 | ThreadPoolHeap<N, NH, Hoard::PerThreadHoardHeap>, |
---|
| 143 | Hoard::BigHeap> > > |
---|
| 144 | { |
---|
| 145 | public: |
---|
| 146 | |
---|
| 147 | enum { BIG_OBJECT = Hoard::BigObjectSize }; |
---|
| 148 | |
---|
| 149 | HL::sassert<sizeof(Hoard::BigSuperblockType::Header) |
---|
| 150 | == sizeof(Hoard::SmallSuperblockType::Header)> ensureSameSizeHeaders; |
---|
| 151 | }; |
---|
| 152 | |
---|
| 153 | }; |
---|
| 154 | |
---|
| 155 | |
---|
| 156 | #endif |
---|