[176] | 1 | // -*- C++ -*- |
---|
| 2 | |
---|
| 3 | /** |
---|
| 4 | * |
---|
| 5 | * @class ThreadLocalAllocationBuffer |
---|
| 6 | * @author Emery Berger <http://www.cs.umass.edu/~emery> |
---|
| 7 | * @brief An allocator, meant to be used for thread-local allocation. |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | #include "dllist.h" |
---|
| 11 | #include "array.h" |
---|
| 12 | |
---|
| 13 | namespace Hoard { |
---|
| 14 | |
---|
| 15 | template <int NumBins, |
---|
| 16 | int (*getSizeClass) (size_t), |
---|
| 17 | size_t (*getClassSize) (const int), |
---|
| 18 | int LargestObject, |
---|
| 19 | int LocalHeapThreshold, |
---|
| 20 | class SuperblockType, |
---|
| 21 | int SuperblockSize, |
---|
| 22 | class ParentHeap> |
---|
| 23 | |
---|
| 24 | class ThreadLocalAllocationBuffer { |
---|
| 25 | |
---|
| 26 | public: |
---|
| 27 | |
---|
| 28 | ThreadLocalAllocationBuffer (ParentHeap * parent) |
---|
| 29 | : _parentHeap (parent), |
---|
| 30 | _localHeapBytes (0) |
---|
| 31 | { |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | ~ThreadLocalAllocationBuffer (void) { |
---|
| 35 | clear(); |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | inline static size_t getSize (void * ptr) { |
---|
| 39 | return getSuperblock(ptr)->getSize (ptr); |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | inline void * malloc (size_t sz) { |
---|
| 43 | #if 1 |
---|
| 44 | if (sz < 2 * sizeof(size_t)) { |
---|
| 45 | sz = 2 * sizeof(size_t); |
---|
| 46 | } |
---|
| 47 | sz = align (sz); |
---|
| 48 | #endif |
---|
| 49 | // Get memory from the local heap, |
---|
| 50 | // and deduct that amount from the local heap bytes counter. |
---|
| 51 | if (sz <= LargestObject) { |
---|
| 52 | int c = getSizeClass (sz); |
---|
| 53 | void * ptr = _localHeap(c).get(); |
---|
| 54 | if (ptr) { |
---|
| 55 | _localHeapBytes -= sz; |
---|
| 56 | assert (getSize(ptr) >= sz); |
---|
| 57 | return ptr; |
---|
| 58 | } |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | // No more local memory (for this size, at least). |
---|
| 62 | // Now get the memory from our parent. |
---|
| 63 | void * ptr = _parentHeap->malloc (sz); |
---|
| 64 | return ptr; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | |
---|
| 68 | inline void free (void * ptr) { |
---|
| 69 | if (!ptr) { |
---|
| 70 | return; |
---|
| 71 | } |
---|
| 72 | const SuperblockType * s = getSuperblock (ptr); |
---|
| 73 | // If this isn't a valid superblock, just return. |
---|
| 74 | |
---|
| 75 | if (s->isValidSuperblock()) { |
---|
| 76 | |
---|
| 77 | ptr = s->normalize (ptr); |
---|
| 78 | const size_t sz = s->getObjectSize (); |
---|
| 79 | |
---|
| 80 | if (sz <= LargestObject) { |
---|
| 81 | // Free small objects locally. |
---|
| 82 | |
---|
| 83 | assert (getSize(ptr) >= sizeof(HL::DLList::Entry *)); |
---|
| 84 | int c = getSizeClass (sz); |
---|
| 85 | |
---|
| 86 | _localHeap(c).insert ((HL::DLList::Entry *) ptr); |
---|
| 87 | _localHeapBytes += sz; |
---|
| 88 | |
---|
| 89 | // If we have too much memory stored locally, |
---|
| 90 | // give it all back. |
---|
| 91 | |
---|
| 92 | if (_localHeapBytes > LocalHeapThreshold) { |
---|
| 93 | clear(); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | } else { |
---|
| 97 | // Free it to the parent. |
---|
| 98 | _parentHeap->free (ptr); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | } else { |
---|
| 102 | #if 0 |
---|
| 103 | char buf[255]; |
---|
| 104 | sprintf (buf, "illegal ptr = %x.\n", ptr); |
---|
| 105 | printf (buf); |
---|
| 106 | #endif |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | void clear (void) { |
---|
| 111 | // Free every object to the 'parent' heap. |
---|
| 112 | for (int i = NumBins-1; i >= 0; i--) { |
---|
| 113 | while (!_localHeap(i).isEmpty()) { |
---|
| 114 | HL::DLList::Entry * e = _localHeap(i).get(); |
---|
| 115 | _parentHeap->free (e); |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | _localHeapBytes = 0; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | static inline SuperblockType * getSuperblock (void * ptr) { |
---|
| 122 | return SuperblockType::getSuperblock (ptr); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | private: |
---|
| 126 | |
---|
| 127 | inline static size_t align (size_t sz) { |
---|
| 128 | return (sz + (sizeof(double) - 1)) & ~(sizeof(double) - 1); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | |
---|
| 132 | // Disable assignment and copying. |
---|
| 133 | |
---|
| 134 | ThreadLocalAllocationBuffer (const ThreadLocalAllocationBuffer&); |
---|
| 135 | ThreadLocalAllocationBuffer& operator=(const ThreadLocalAllocationBuffer&); |
---|
| 136 | |
---|
| 137 | /// This heap's 'parent' (where to go for more memory). |
---|
| 138 | ParentHeap * _parentHeap; |
---|
| 139 | |
---|
| 140 | /// The number of bytes we currently have on this thread. |
---|
| 141 | int _localHeapBytes; |
---|
| 142 | |
---|
| 143 | /// The local heap itself. |
---|
| 144 | Array<NumBins, HL::DLList> _localHeap; |
---|
| 145 | |
---|
| 146 | }; |
---|
| 147 | |
---|
| 148 | } |
---|