[176] | 1 | /* -*- C++ -*- */ |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | The Hoard Multiprocessor Memory Allocator |
---|
| 5 | www.hoard.org |
---|
| 6 | |
---|
| 7 | Author: Emery Berger, http://www.cs.umass.edu/~emery |
---|
| 8 | |
---|
| 9 | Copyright (c) 1998-2006 Emery Berger, The University of Texas at Austin |
---|
| 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 | /* |
---|
| 28 | * @file libhoard.cpp |
---|
| 29 | * @brief This file replaces malloc etc. in your application. |
---|
| 30 | * @author Emery Berger <http://www.cs.umass.edu/~emery> |
---|
| 31 | */ |
---|
| 32 | |
---|
| 33 | #include <new> |
---|
| 34 | |
---|
| 35 | // The undef below ensures that any pthread_* calls get strong |
---|
| 36 | // linkage. Otherwise, our versions here won't replace them. It is |
---|
| 37 | // IMPERATIVE that this line appear before any files get included. |
---|
| 38 | |
---|
| 39 | #undef __GXX_WEAK__ |
---|
| 40 | |
---|
| 41 | #if defined(_WIN32) |
---|
| 42 | #define WIN32_LEAN_AND_MEAN |
---|
| 43 | |
---|
| 44 | // Maximize the degree of inlining. |
---|
| 45 | #pragma inline_depth(255) |
---|
| 46 | |
---|
| 47 | // Turn inlining hints into requirements. |
---|
| 48 | #define inline __forceinline |
---|
| 49 | #pragma warning(disable:4273) |
---|
| 50 | #endif |
---|
| 51 | |
---|
| 52 | #if HOARD_NO_LOCK_OPT |
---|
| 53 | // Disable lock optimization. |
---|
| 54 | volatile int anyThreadCreated = 1; |
---|
| 55 | #else |
---|
| 56 | // The normal case. See heaplayers/spinlock.h. |
---|
| 57 | volatile int anyThreadCreated = 0; |
---|
| 58 | #endif |
---|
| 59 | |
---|
| 60 | namespace Hoard { |
---|
| 61 | |
---|
| 62 | /// The maximum amount of memory that each TLAB may hold, in bytes. |
---|
| 63 | enum { MAX_MEMORY_PER_TLAB = 256 * 1024 }; |
---|
| 64 | |
---|
| 65 | /// The maximum number of threads supported (sort of). |
---|
| 66 | enum { MaxThreads = 1024 }; |
---|
| 67 | |
---|
| 68 | /// The maximum number of heaps supported. |
---|
| 69 | enum { NumHeaps = 128 }; |
---|
| 70 | |
---|
| 71 | /// Size, in bytes, of the largest object we will cache on a |
---|
| 72 | /// thread-local allocation buffer. |
---|
| 73 | enum { LargestSmallObject = 256 }; |
---|
| 74 | |
---|
| 75 | // HOARD_MMAP_PROTECTION_MASK defines the protection flags used for |
---|
| 76 | // freshly-allocated memory. The default case is that heap memory is |
---|
| 77 | // NOT executable, thus preventing the class of attacks that inject |
---|
| 78 | // executable code on the heap. |
---|
| 79 | // |
---|
| 80 | // While this is not recommended, you can define HL_EXECUTABLE_HEAP as |
---|
| 81 | // 1 in heaplayers/hldefines.h if you really need to (i.e., you're |
---|
| 82 | // doing dynamic code generation into malloc'd space). |
---|
| 83 | |
---|
| 84 | #if HL_EXECUTABLE_HEAP |
---|
| 85 | #define HOARD_MMAP_PROTECTION_MASK (PROT_READ | PROT_WRITE | PROT_EXEC) |
---|
| 86 | #else |
---|
| 87 | #define HOARD_MMAP_PROTECTION_MASK (PROT_READ | PROT_WRITE) |
---|
| 88 | #endif |
---|
| 89 | |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | #include "ansiwrapper.h" |
---|
| 93 | #include "cpuinfo.h" |
---|
| 94 | #include "hoard.h" |
---|
| 95 | #include "heapmanager.h" |
---|
| 96 | #include "tlab.h" |
---|
| 97 | |
---|
| 98 | // |
---|
| 99 | // The base Hoard heap. |
---|
| 100 | // |
---|
| 101 | |
---|
| 102 | namespace Hoard { |
---|
| 103 | |
---|
| 104 | class HoardHeapType : |
---|
| 105 | public HeapManager<TheLockType, HoardHeap<MaxThreads, NumHeaps> > { |
---|
| 106 | }; |
---|
| 107 | |
---|
| 108 | // Just an abbreviation. |
---|
| 109 | typedef HoardHeapType::SuperblockType::Header TheHeader; |
---|
| 110 | |
---|
| 111 | // |
---|
| 112 | // The thread-local 'allocation buffers' (TLABs), which is a bit of a |
---|
| 113 | // misnomer since these are actually separate heaps in their own |
---|
| 114 | // right. |
---|
| 115 | // |
---|
| 116 | |
---|
| 117 | typedef ThreadLocalAllocationBuffer<HL::bins<TheHeader, SUPERBLOCK_SIZE>::NUM_BINS, |
---|
| 118 | HL::bins<TheHeader, SUPERBLOCK_SIZE>::getSizeClass, |
---|
| 119 | HL::bins<TheHeader, SUPERBLOCK_SIZE>::getClassSize, |
---|
| 120 | LargestSmallObject, |
---|
| 121 | MAX_MEMORY_PER_TLAB, |
---|
| 122 | HoardHeapType::SuperblockType, |
---|
| 123 | SUPERBLOCK_SIZE, |
---|
| 124 | HoardHeapType> TLABBase; |
---|
| 125 | |
---|
| 126 | class TLAB : public TLABBase { |
---|
| 127 | public: |
---|
| 128 | TLAB (HoardHeapType * h) |
---|
| 129 | : TLABBase (h) |
---|
| 130 | {} |
---|
| 131 | |
---|
| 132 | }; |
---|
| 133 | |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | using namespace Hoard; |
---|
| 137 | |
---|
| 138 | typedef TLAB TheCustomHeapType; |
---|
| 139 | |
---|
| 140 | /// Maintain a single instance of the main Hoard heap. |
---|
| 141 | |
---|
| 142 | inline static HoardHeapType * getMainHoardHeap (void) { |
---|
| 143 | // This function is C++ magic that ensures that the heap is |
---|
| 144 | // initialized before its first use. First, allocate a static buffer |
---|
| 145 | // to hold the heap. |
---|
| 146 | static double thBuf[sizeof(HoardHeapType) / sizeof(double) + 1]; |
---|
| 147 | |
---|
| 148 | // Now initialize the heap into that buffer. |
---|
| 149 | static HoardHeapType * th = new (thBuf) HoardHeapType; |
---|
| 150 | return th; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | // Compute the version of gcc we're compiling with (if any). |
---|
| 154 | #define GCC_VERSION (__GNUC__ * 10000 \ |
---|
| 155 | + __GNUC_MINOR__ * 100 \ |
---|
| 156 | + __GNUC_PATCHLEVEL__) |
---|
| 157 | |
---|
| 158 | #include "userealtls.cpp" |
---|
| 159 | |
---|
| 160 | // |
---|
| 161 | // Finally, get the replacements for the rest of the malloc family. |
---|
| 162 | // |
---|
| 163 | |
---|
| 164 | #include "wrapper.cpp" |
---|