epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2010 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 11 | #ifndef GrAllocator_DEFINED |
| 12 | #define GrAllocator_DEFINED |
| 13 | |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 14 | #include "GrNoncopyable.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 15 | #include "GrConfig.h" |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 16 | #include "SkTArray.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 17 | |
bsalomon@google.com | 4b90c62 | 2011-09-28 17:52:15 +0000 | [diff] [blame] | 18 | class GrAllocator : GrNoncopyable { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 19 | public: |
bsalomon@google.com | 4b90c62 | 2011-09-28 17:52:15 +0000 | [diff] [blame] | 20 | ~GrAllocator() { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 21 | reset(); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Create an allocator |
| 26 | * |
| 27 | * @param itemSize the size of each item to allocate |
| 28 | * @param itemsPerBlock the number of items to allocate at once |
| 29 | * @param initialBlock optional memory to use for the first block. |
| 30 | * Must be at least itemSize*itemsPerBlock sized. |
| 31 | * Caller is responsible for freeing this memory. |
| 32 | */ |
bsalomon@google.com | 6a77cc5 | 2011-04-28 17:33:34 +0000 | [diff] [blame] | 33 | GrAllocator(size_t itemSize, int itemsPerBlock, void* initialBlock) : |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 34 | fItemSize(itemSize), |
| 35 | fItemsPerBlock(itemsPerBlock), |
| 36 | fOwnFirstBlock(NULL == initialBlock), |
| 37 | fCount(0) { |
bsalomon@google.com | 6a77cc5 | 2011-04-28 17:33:34 +0000 | [diff] [blame] | 38 | GrAssert(itemsPerBlock > 0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 39 | fBlockSize = fItemSize * fItemsPerBlock; |
| 40 | fBlocks.push_back() = initialBlock; |
| 41 | GR_DEBUGCODE(if (!fOwnFirstBlock) {*((char*)initialBlock+fBlockSize-1)='a';} ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Adds an item and returns pointer to it. |
| 46 | * |
| 47 | * @return pointer to the added item. |
| 48 | */ |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 49 | void* push_back() { |
bsalomon@google.com | 6a77cc5 | 2011-04-28 17:33:34 +0000 | [diff] [blame] | 50 | int indexInBlock = fCount % fItemsPerBlock; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 51 | // we always have at least one block |
| 52 | if (0 == indexInBlock) { |
| 53 | if (0 != fCount) { |
| 54 | fBlocks.push_back() = GrMalloc(fBlockSize); |
| 55 | } else if (fOwnFirstBlock) { |
| 56 | fBlocks[0] = GrMalloc(fBlockSize); |
| 57 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 58 | } |
| 59 | void* ret = (char*)fBlocks[fCount/fItemsPerBlock] + |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 60 | fItemSize * indexInBlock; |
| 61 | ++fCount; |
| 62 | return ret; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * removes all added items |
| 67 | */ |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 68 | void reset() { |
| 69 | int blockCount = GrMax((unsigned)1, |
bsalomon@google.com | 6a77cc5 | 2011-04-28 17:33:34 +0000 | [diff] [blame] | 70 | GrUIDivRoundUp(fCount, fItemsPerBlock)); |
| 71 | for (int i = 1; i < blockCount; ++i) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 72 | GrFree(fBlocks[i]); |
| 73 | } |
| 74 | if (fOwnFirstBlock) { |
| 75 | GrFree(fBlocks[0]); |
| 76 | fBlocks[0] = NULL; |
| 77 | } |
| 78 | fBlocks.pop_back_n(blockCount-1); |
| 79 | fCount = 0; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * count of items |
| 84 | */ |
bsalomon@google.com | 6a77cc5 | 2011-04-28 17:33:34 +0000 | [diff] [blame] | 85 | int count() const { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 86 | return fCount; |
| 87 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 88 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 89 | /** |
| 90 | * is the count 0 |
| 91 | */ |
| 92 | bool empty() const { return fCount == 0; } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 93 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 94 | /** |
| 95 | * access last item, only call if count() != 0 |
| 96 | */ |
| 97 | void* back() { |
| 98 | GrAssert(fCount); |
| 99 | return (*this)[fCount-1]; |
| 100 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 101 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 102 | /** |
| 103 | * access last item, only call if count() != 0 |
| 104 | */ |
| 105 | const void* back() const { |
| 106 | GrAssert(fCount); |
| 107 | return (*this)[fCount-1]; |
| 108 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 109 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 110 | /** |
| 111 | * access item by index. |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 112 | */ |
bsalomon@google.com | 6a77cc5 | 2011-04-28 17:33:34 +0000 | [diff] [blame] | 113 | void* operator[] (int i) { |
| 114 | GrAssert(i >= 0 && i < fCount); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 115 | return (char*)fBlocks[i / fItemsPerBlock] + |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 116 | fItemSize * (i % fItemsPerBlock); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * access item by index. |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 121 | */ |
bsalomon@google.com | 6a77cc5 | 2011-04-28 17:33:34 +0000 | [diff] [blame] | 122 | const void* operator[] (int i) const { |
| 123 | GrAssert(i >= 0 && i < fCount); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 124 | return (const char*)fBlocks[i / fItemsPerBlock] + |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 125 | fItemSize * (i % fItemsPerBlock); |
| 126 | } |
| 127 | |
| 128 | private: |
bsalomon@google.com | 6a77cc5 | 2011-04-28 17:33:34 +0000 | [diff] [blame] | 129 | static const int NUM_INIT_BLOCK_PTRS = 8; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 130 | |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 131 | SkSTArray<NUM_INIT_BLOCK_PTRS, void*> fBlocks; |
| 132 | size_t fBlockSize; |
| 133 | size_t fItemSize; |
| 134 | int fItemsPerBlock; |
| 135 | bool fOwnFirstBlock; |
| 136 | int fCount; |
bsalomon@google.com | 4b90c62 | 2011-09-28 17:52:15 +0000 | [diff] [blame] | 137 | |
| 138 | typedef GrNoncopyable INHERITED; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | template <typename T> |
bsalomon@google.com | 4b90c62 | 2011-09-28 17:52:15 +0000 | [diff] [blame] | 142 | class GrTAllocator : GrNoncopyable { |
| 143 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 144 | public: |
bsalomon@google.com | 13788bf | 2011-10-27 17:17:44 +0000 | [diff] [blame] | 145 | virtual ~GrTAllocator() { this->reset(); }; |
bsalomon@google.com | a55847b | 2011-04-20 15:47:04 +0000 | [diff] [blame] | 146 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 147 | /** |
| 148 | * Create an allocator |
| 149 | * |
| 150 | * @param itemsPerBlock the number of items to allocate at once |
| 151 | * @param initialBlock optional memory to use for the first block. |
| 152 | * Must be at least size(T)*itemsPerBlock sized. |
| 153 | * Caller is responsible for freeing this memory. |
| 154 | */ |
bsalomon@google.com | 4b90c62 | 2011-09-28 17:52:15 +0000 | [diff] [blame] | 155 | explicit GrTAllocator(int itemsPerBlock) |
| 156 | : fAllocator(sizeof(T), itemsPerBlock, NULL) {} |
bsalomon@google.com | a55847b | 2011-04-20 15:47:04 +0000 | [diff] [blame] | 157 | |
| 158 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 159 | * Adds an item and returns it. |
| 160 | * |
| 161 | * @return the added item. |
| 162 | */ |
| 163 | T& push_back() { |
| 164 | void* item = fAllocator.push_back(); |
| 165 | GrAssert(NULL != item); |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 166 | SkNEW_PLACEMENT(item, T); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 167 | return *(T*)item; |
| 168 | } |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 169 | |
| 170 | T& push_back(const T& t) { |
| 171 | void* item = fAllocator.push_back(); |
| 172 | GrAssert(NULL != item); |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 173 | SkNEW_PLACEMENT_ARGS(item, T, (t)); |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 174 | return *(T*)item; |
| 175 | } |
| 176 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 177 | /** |
| 178 | * removes all added items |
| 179 | */ |
| 180 | void reset() { |
bsalomon@google.com | 6a77cc5 | 2011-04-28 17:33:34 +0000 | [diff] [blame] | 181 | int c = fAllocator.count(); |
| 182 | for (int i = 0; i < c; ++i) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 183 | ((T*)fAllocator[i])->~T(); |
| 184 | } |
| 185 | fAllocator.reset(); |
| 186 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 187 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 188 | /** |
| 189 | * count of items |
| 190 | */ |
bsalomon@google.com | 6a77cc5 | 2011-04-28 17:33:34 +0000 | [diff] [blame] | 191 | int count() const { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 192 | return fAllocator.count(); |
| 193 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 194 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 195 | /** |
| 196 | * is the count 0 |
| 197 | */ |
| 198 | bool empty() const { return fAllocator.empty(); } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 199 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 200 | /** |
| 201 | * access last item, only call if count() != 0 |
| 202 | */ |
| 203 | T& back() { |
| 204 | return *(T*)fAllocator.back(); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * access last item, only call if count() != 0 |
| 209 | */ |
| 210 | const T& back() const { |
| 211 | return *(const T*)fAllocator.back(); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * access item by index. |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 216 | */ |
bsalomon@google.com | 6a77cc5 | 2011-04-28 17:33:34 +0000 | [diff] [blame] | 217 | T& operator[] (int i) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 218 | return *(T*)(fAllocator[i]); |
| 219 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 220 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 221 | /** |
| 222 | * access item by index. |
| 223 | */ |
bsalomon@google.com | 6a77cc5 | 2011-04-28 17:33:34 +0000 | [diff] [blame] | 224 | const T& operator[] (int i) const { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 225 | return *(const T*)(fAllocator[i]); |
bsalomon@google.com | 4b90c62 | 2011-09-28 17:52:15 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | protected: |
| 229 | GrTAllocator(int itemsPerBlock, void* initialBlock) |
| 230 | : fAllocator(sizeof(T), itemsPerBlock, initialBlock) { |
| 231 | } |
| 232 | |
| 233 | private: |
| 234 | GrAllocator fAllocator; |
| 235 | typedef GrNoncopyable INHERITED; |
| 236 | }; |
| 237 | |
| 238 | template <int N, typename T> class GrSTAllocator : public GrTAllocator<T> { |
| 239 | private: |
| 240 | typedef GrTAllocator<T> INHERITED; |
| 241 | |
| 242 | public: |
| 243 | GrSTAllocator() : INHERITED(N, fStorage.get()) { |
| 244 | } |
| 245 | |
| 246 | private: |
| 247 | SkAlignedSTStorage<N, T> fStorage; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | #endif |