reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2010 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 8 | #ifndef GrAllocator_DEFINED |
| 9 | #define GrAllocator_DEFINED |
| 10 | |
| 11 | #include "GrConfig.h" |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 12 | #include "GrTypes.h" |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 13 | #include "SkTArray.h" |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 14 | #include "SkTypes.h" |
Mike Klein | 79aea6a | 2018-06-11 10:45:26 -0400 | [diff] [blame] | 15 | #include <new> |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 16 | |
commit-bot@chromium.org | e3beb6b | 2014-04-07 19:34:38 +0000 | [diff] [blame] | 17 | class GrAllocator : SkNoncopyable { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 18 | public: |
bsalomon | bce3d6d | 2014-07-02 07:54:42 -0700 | [diff] [blame] | 19 | ~GrAllocator() { this->reset(); } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 20 | |
| 21 | /** |
| 22 | * Create an allocator |
| 23 | * |
| 24 | * @param itemSize the size of each item to allocate |
| 25 | * @param itemsPerBlock the number of items to allocate at once |
| 26 | * @param initialBlock optional memory to use for the first block. |
| 27 | * Must be at least itemSize*itemsPerBlock sized. |
| 28 | * Caller is responsible for freeing this memory. |
| 29 | */ |
bsalomon | bce3d6d | 2014-07-02 07:54:42 -0700 | [diff] [blame] | 30 | GrAllocator(size_t itemSize, int itemsPerBlock, void* initialBlock) |
| 31 | : fItemSize(itemSize) |
| 32 | , fItemsPerBlock(itemsPerBlock) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 33 | , fOwnFirstBlock(nullptr == initialBlock) |
bsalomon | bce3d6d | 2014-07-02 07:54:42 -0700 | [diff] [blame] | 34 | , fCount(0) |
| 35 | , fInsertionIndexInBlock(0) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 36 | SkASSERT(itemsPerBlock > 0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 37 | fBlockSize = fItemSize * fItemsPerBlock; |
bsalomon | bce3d6d | 2014-07-02 07:54:42 -0700 | [diff] [blame] | 38 | if (fOwnFirstBlock) { |
| 39 | // This force us to allocate a new block on push_back(). |
| 40 | fInsertionIndexInBlock = fItemsPerBlock; |
| 41 | } else { |
| 42 | fBlocks.push_back() = initialBlock; |
| 43 | fInsertionIndexInBlock = 0; |
| 44 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 45 | } |
| 46 | |
bsalomon | bce3d6d | 2014-07-02 07:54:42 -0700 | [diff] [blame] | 47 | /** |
| 48 | * Adds an item and returns pointer to it. |
| 49 | * |
| 50 | * @return pointer to the added item. |
| 51 | */ |
| 52 | void* push_back() { |
| 53 | // we always have at least one block |
| 54 | if (fItemsPerBlock == fInsertionIndexInBlock) { |
| 55 | fBlocks.push_back() = sk_malloc_throw(fBlockSize); |
| 56 | fInsertionIndexInBlock = 0; |
| 57 | } |
| 58 | void* ret = (char*)fBlocks.back() + fItemSize * fInsertionIndexInBlock; |
| 59 | ++fCount; |
| 60 | ++fInsertionIndexInBlock; |
| 61 | return ret; |
| 62 | } |
| 63 | |
| 64 | /** |
bsalomon | a1ae66d | 2014-09-05 06:13:43 -0700 | [diff] [blame] | 65 | * Remove the last item, only call if count() != 0 |
| 66 | */ |
| 67 | void pop_back() { |
| 68 | SkASSERT(fCount); |
| 69 | SkASSERT(fInsertionIndexInBlock > 0); |
| 70 | --fInsertionIndexInBlock; |
| 71 | --fCount; |
| 72 | if (0 == fInsertionIndexInBlock) { |
| 73 | // Never delete the first block |
| 74 | if (fBlocks.count() > 1) { |
| 75 | sk_free(fBlocks.back()); |
| 76 | fBlocks.pop_back(); |
| 77 | fInsertionIndexInBlock = fItemsPerBlock; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
bsalomon | bce3d6d | 2014-07-02 07:54:42 -0700 | [diff] [blame] | 83 | * Removes all added items. |
| 84 | */ |
| 85 | void reset() { |
| 86 | int firstBlockToFree = fOwnFirstBlock ? 0 : 1; |
| 87 | for (int i = firstBlockToFree; i < fBlocks.count(); ++i) { |
| 88 | sk_free(fBlocks[i]); |
| 89 | } |
| 90 | if (fOwnFirstBlock) { |
| 91 | fBlocks.reset(); |
| 92 | // This force us to allocate a new block on push_back(). |
| 93 | fInsertionIndexInBlock = fItemsPerBlock; |
| 94 | } else { |
| 95 | fBlocks.pop_back_n(fBlocks.count() - 1); |
| 96 | fInsertionIndexInBlock = 0; |
| 97 | } |
| 98 | fCount = 0; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Returns the item count. |
| 103 | */ |
| 104 | int count() const { |
| 105 | return fCount; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Is the count 0? |
| 110 | */ |
| 111 | bool empty() const { return 0 == fCount; } |
| 112 | |
| 113 | /** |
Chris Dalton | d0d409d | 2018-06-08 11:51:39 -0600 | [diff] [blame] | 114 | * Access first item, only call if count() != 0 |
| 115 | */ |
| 116 | void* front() { |
| 117 | SkASSERT(fCount); |
| 118 | SkASSERT(fInsertionIndexInBlock > 0); |
| 119 | return (char*)(fBlocks.front()); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Access first item, only call if count() != 0 |
| 124 | */ |
| 125 | const void* front() const { |
| 126 | SkASSERT(fCount); |
| 127 | SkASSERT(fInsertionIndexInBlock > 0); |
| 128 | return (const char*)(fBlocks.front()); |
| 129 | } |
| 130 | |
| 131 | /** |
bsalomon | bce3d6d | 2014-07-02 07:54:42 -0700 | [diff] [blame] | 132 | * Access last item, only call if count() != 0 |
| 133 | */ |
| 134 | void* back() { |
| 135 | SkASSERT(fCount); |
| 136 | SkASSERT(fInsertionIndexInBlock > 0); |
| 137 | return (char*)(fBlocks.back()) + (fInsertionIndexInBlock - 1) * fItemSize; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Access last item, only call if count() != 0 |
| 142 | */ |
| 143 | const void* back() const { |
| 144 | SkASSERT(fCount); |
| 145 | SkASSERT(fInsertionIndexInBlock > 0); |
| 146 | return (const char*)(fBlocks.back()) + (fInsertionIndexInBlock - 1) * fItemSize; |
| 147 | } |
| 148 | |
bsalomon | bce3d6d | 2014-07-02 07:54:42 -0700 | [diff] [blame] | 149 | /** |
| 150 | * Iterates through the allocator. This is faster than using operator[] when walking linearly |
| 151 | * through the allocator. |
| 152 | */ |
| 153 | class Iter { |
| 154 | public: |
| 155 | /** |
| 156 | * Initializes the iterator. next() must be called before get(). |
| 157 | */ |
| 158 | Iter(const GrAllocator* allocator) |
| 159 | : fAllocator(allocator) |
| 160 | , fBlockIndex(-1) |
| 161 | , fIndexInBlock(allocator->fItemsPerBlock - 1) |
| 162 | , fItemIndex(-1) {} |
| 163 | |
| 164 | /** |
| 165 | * Advances the iterator. Iteration is finished when next() returns false. |
| 166 | */ |
| 167 | bool next() { |
| 168 | ++fIndexInBlock; |
| 169 | ++fItemIndex; |
| 170 | if (fIndexInBlock == fAllocator->fItemsPerBlock) { |
| 171 | ++fBlockIndex; |
| 172 | fIndexInBlock = 0; |
| 173 | } |
| 174 | return fItemIndex < fAllocator->fCount; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Gets the current iterator value. Call next() at least once before calling. Don't call |
| 179 | * after next() returns false. |
| 180 | */ |
bsalomon | e1bc1c6 | 2014-07-02 13:44:57 -0700 | [diff] [blame] | 181 | void* get() const { |
bsalomon | bce3d6d | 2014-07-02 07:54:42 -0700 | [diff] [blame] | 182 | SkASSERT(fItemIndex >= 0 && fItemIndex < fAllocator->fCount); |
| 183 | return (char*) fAllocator->fBlocks[fBlockIndex] + fIndexInBlock * fAllocator->fItemSize; |
| 184 | } |
| 185 | |
| 186 | private: |
| 187 | const GrAllocator* fAllocator; |
| 188 | int fBlockIndex; |
| 189 | int fIndexInBlock; |
| 190 | int fItemIndex; |
| 191 | }; |
| 192 | |
| 193 | /** |
| 194 | * Access item by index. |
| 195 | */ |
| 196 | void* operator[] (int i) { |
| 197 | SkASSERT(i >= 0 && i < fCount); |
| 198 | return (char*)fBlocks[i / fItemsPerBlock] + |
| 199 | fItemSize * (i % fItemsPerBlock); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Access item by index. |
| 204 | */ |
| 205 | const void* operator[] (int i) const { |
| 206 | SkASSERT(i >= 0 && i < fCount); |
| 207 | return (const char*)fBlocks[i / fItemsPerBlock] + |
| 208 | fItemSize * (i % fItemsPerBlock); |
| 209 | } |
| 210 | |
| 211 | protected: |
| 212 | /** |
commit-bot@chromium.org | 845da77 | 2013-12-02 22:32:58 +0000 | [diff] [blame] | 213 | * Set first block of memory to write into. Must be called before any other methods. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 214 | * This requires that you have passed nullptr in the constructor. |
commit-bot@chromium.org | 845da77 | 2013-12-02 22:32:58 +0000 | [diff] [blame] | 215 | * |
| 216 | * @param initialBlock optional memory to use for the first block. |
| 217 | * Must be at least itemSize*itemsPerBlock sized. |
| 218 | * Caller is responsible for freeing this memory. |
| 219 | */ |
| 220 | void setInitialBlock(void* initialBlock) { |
| 221 | SkASSERT(0 == fCount); |
bsalomon | bce3d6d | 2014-07-02 07:54:42 -0700 | [diff] [blame] | 222 | SkASSERT(0 == fBlocks.count()); |
| 223 | SkASSERT(fItemsPerBlock == fInsertionIndexInBlock); |
commit-bot@chromium.org | 845da77 | 2013-12-02 22:32:58 +0000 | [diff] [blame] | 224 | fOwnFirstBlock = false; |
bsalomon | bce3d6d | 2014-07-02 07:54:42 -0700 | [diff] [blame] | 225 | fBlocks.push_back() = initialBlock; |
| 226 | fInsertionIndexInBlock = 0; |
commit-bot@chromium.org | 845da77 | 2013-12-02 22:32:58 +0000 | [diff] [blame] | 227 | } |
| 228 | |
bsalomon | bce3d6d | 2014-07-02 07:54:42 -0700 | [diff] [blame] | 229 | // For access to above function. |
| 230 | template <typename T> friend class GrTAllocator; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 231 | |
| 232 | private: |
bsalomon@google.com | 6a77cc5 | 2011-04-28 17:33:34 +0000 | [diff] [blame] | 233 | static const int NUM_INIT_BLOCK_PTRS = 8; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 234 | |
bsalomon | bce3d6d | 2014-07-02 07:54:42 -0700 | [diff] [blame] | 235 | SkSTArray<NUM_INIT_BLOCK_PTRS, void*, true> fBlocks; |
| 236 | size_t fBlockSize; |
| 237 | size_t fItemSize; |
| 238 | int fItemsPerBlock; |
| 239 | bool fOwnFirstBlock; |
| 240 | int fCount; |
| 241 | int fInsertionIndexInBlock; |
bsalomon@google.com | 4b90c62 | 2011-09-28 17:52:15 +0000 | [diff] [blame] | 242 | |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 243 | typedef SkNoncopyable INHERITED; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 244 | }; |
| 245 | |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 246 | template <typename T> class GrTAllocator; |
| 247 | template <typename T> void* operator new(size_t, GrTAllocator<T>*); |
| 248 | |
| 249 | template <typename T> class GrTAllocator : SkNoncopyable { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 250 | public: |
Mike Klein | fc6c37b | 2016-09-27 09:34:10 -0400 | [diff] [blame] | 251 | virtual ~GrTAllocator() { this->reset(); } |
bsalomon@google.com | a55847b | 2011-04-20 15:47:04 +0000 | [diff] [blame] | 252 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 253 | /** |
| 254 | * Create an allocator |
| 255 | * |
| 256 | * @param itemsPerBlock the number of items to allocate at once |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 257 | */ |
bsalomon@google.com | 4b90c62 | 2011-09-28 17:52:15 +0000 | [diff] [blame] | 258 | explicit GrTAllocator(int itemsPerBlock) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 259 | : fAllocator(sizeof(T), itemsPerBlock, nullptr) {} |
bsalomon@google.com | a55847b | 2011-04-20 15:47:04 +0000 | [diff] [blame] | 260 | |
| 261 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 262 | * Adds an item and returns it. |
| 263 | * |
| 264 | * @return the added item. |
| 265 | */ |
| 266 | T& push_back() { |
| 267 | void* item = fAllocator.push_back(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 268 | SkASSERT(item); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 269 | new (item) T; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 270 | return *(T*)item; |
| 271 | } |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 272 | |
| 273 | T& push_back(const T& t) { |
| 274 | void* item = fAllocator.push_back(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 275 | SkASSERT(item); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 276 | new (item) T(t); |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 277 | return *(T*)item; |
| 278 | } |
| 279 | |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 280 | template <typename... Args> T& emplace_back(Args&&... args) { |
| 281 | void* item = fAllocator.push_back(); |
| 282 | SkASSERT(item); |
| 283 | new (item) T(std::forward<Args>(args)...); |
| 284 | return *(T*)item; |
| 285 | } |
| 286 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 287 | /** |
bsalomon | a1ae66d | 2014-09-05 06:13:43 -0700 | [diff] [blame] | 288 | * Remove the last item, only call if count() != 0 |
| 289 | */ |
| 290 | void pop_back() { |
| 291 | this->back().~T(); |
| 292 | fAllocator.pop_back(); |
| 293 | } |
| 294 | |
| 295 | /** |
bsalomon | bce3d6d | 2014-07-02 07:54:42 -0700 | [diff] [blame] | 296 | * Removes all added items. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 297 | */ |
| 298 | void reset() { |
bsalomon@google.com | 6a77cc5 | 2011-04-28 17:33:34 +0000 | [diff] [blame] | 299 | int c = fAllocator.count(); |
| 300 | for (int i = 0; i < c; ++i) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 301 | ((T*)fAllocator[i])->~T(); |
| 302 | } |
| 303 | fAllocator.reset(); |
| 304 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 305 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 306 | /** |
bsalomon | bce3d6d | 2014-07-02 07:54:42 -0700 | [diff] [blame] | 307 | * Returns the item count. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 308 | */ |
bsalomon@google.com | 6a77cc5 | 2011-04-28 17:33:34 +0000 | [diff] [blame] | 309 | int count() const { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 310 | return fAllocator.count(); |
| 311 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 312 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 313 | /** |
bsalomon | bce3d6d | 2014-07-02 07:54:42 -0700 | [diff] [blame] | 314 | * Is the count 0? |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 315 | */ |
| 316 | bool empty() const { return fAllocator.empty(); } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 317 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 318 | /** |
Chris Dalton | d0d409d | 2018-06-08 11:51:39 -0600 | [diff] [blame] | 319 | * Access first item, only call if count() != 0 |
| 320 | */ |
| 321 | T& front() { |
| 322 | return *(T*)fAllocator.front(); |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Access first item, only call if count() != 0 |
| 327 | */ |
| 328 | const T& front() const { |
| 329 | return *(T*)fAllocator.front(); |
| 330 | } |
| 331 | |
| 332 | /** |
bsalomon | bce3d6d | 2014-07-02 07:54:42 -0700 | [diff] [blame] | 333 | * Access last item, only call if count() != 0 |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 334 | */ |
| 335 | T& back() { |
| 336 | return *(T*)fAllocator.back(); |
| 337 | } |
| 338 | |
| 339 | /** |
bsalomon | bce3d6d | 2014-07-02 07:54:42 -0700 | [diff] [blame] | 340 | * Access last item, only call if count() != 0 |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 341 | */ |
| 342 | const T& back() const { |
| 343 | return *(const T*)fAllocator.back(); |
| 344 | } |
| 345 | |
| 346 | /** |
bsalomon | bce3d6d | 2014-07-02 07:54:42 -0700 | [diff] [blame] | 347 | * Iterates through the allocator. This is faster than using operator[] when walking linearly |
| 348 | * through the allocator. |
| 349 | */ |
| 350 | class Iter { |
| 351 | public: |
| 352 | /** |
| 353 | * Initializes the iterator. next() must be called before get() or ops * and ->. |
| 354 | */ |
| 355 | Iter(const GrTAllocator* allocator) : fImpl(&allocator->fAllocator) {} |
| 356 | |
| 357 | /** |
| 358 | * Advances the iterator. Iteration is finished when next() returns false. |
| 359 | */ |
| 360 | bool next() { return fImpl.next(); } |
| 361 | |
| 362 | /** |
| 363 | * Gets the current iterator value. Call next() at least once before calling. Don't call |
| 364 | * after next() returns false. |
| 365 | */ |
bsalomon | e1bc1c6 | 2014-07-02 13:44:57 -0700 | [diff] [blame] | 366 | T* get() const { return (T*) fImpl.get(); } |
bsalomon | bce3d6d | 2014-07-02 07:54:42 -0700 | [diff] [blame] | 367 | |
| 368 | /** |
| 369 | * Convenience operators. Same rules for calling apply as get(). |
| 370 | */ |
bsalomon | e1bc1c6 | 2014-07-02 13:44:57 -0700 | [diff] [blame] | 371 | T& operator*() const { return *this->get(); } |
| 372 | T* operator->() const { return this->get(); } |
bsalomon | bce3d6d | 2014-07-02 07:54:42 -0700 | [diff] [blame] | 373 | |
| 374 | private: |
| 375 | GrAllocator::Iter fImpl; |
| 376 | }; |
| 377 | |
| 378 | /** |
| 379 | * Access item by index. |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 380 | */ |
bsalomon@google.com | 6a77cc5 | 2011-04-28 17:33:34 +0000 | [diff] [blame] | 381 | T& operator[] (int i) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 382 | return *(T*)(fAllocator[i]); |
| 383 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 384 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 385 | /** |
bsalomon | bce3d6d | 2014-07-02 07:54:42 -0700 | [diff] [blame] | 386 | * Access item by index. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 387 | */ |
bsalomon@google.com | 6a77cc5 | 2011-04-28 17:33:34 +0000 | [diff] [blame] | 388 | const T& operator[] (int i) const { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 389 | return *(const T*)(fAllocator[i]); |
bsalomon@google.com | 4b90c62 | 2011-09-28 17:52:15 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | protected: |
commit-bot@chromium.org | 845da77 | 2013-12-02 22:32:58 +0000 | [diff] [blame] | 393 | /* |
| 394 | * Set first block of memory to write into. Must be called before any other methods. |
| 395 | * |
| 396 | * @param initialBlock optional memory to use for the first block. |
| 397 | * Must be at least size(T)*itemsPerBlock sized. |
| 398 | * Caller is responsible for freeing this memory. |
| 399 | */ |
| 400 | void setInitialBlock(void* initialBlock) { |
| 401 | fAllocator.setInitialBlock(initialBlock); |
bsalomon@google.com | 4b90c62 | 2011-09-28 17:52:15 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | private: |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 405 | friend void* operator new<T>(size_t, GrTAllocator*); |
| 406 | |
bsalomon@google.com | 4b90c62 | 2011-09-28 17:52:15 +0000 | [diff] [blame] | 407 | GrAllocator fAllocator; |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 408 | typedef SkNoncopyable INHERITED; |
bsalomon@google.com | 4b90c62 | 2011-09-28 17:52:15 +0000 | [diff] [blame] | 409 | }; |
| 410 | |
| 411 | template <int N, typename T> class GrSTAllocator : public GrTAllocator<T> { |
| 412 | private: |
| 413 | typedef GrTAllocator<T> INHERITED; |
| 414 | |
| 415 | public: |
commit-bot@chromium.org | 845da77 | 2013-12-02 22:32:58 +0000 | [diff] [blame] | 416 | GrSTAllocator() : INHERITED(N) { |
| 417 | this->setInitialBlock(fStorage.get()); |
bsalomon@google.com | 4b90c62 | 2011-09-28 17:52:15 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | private: |
| 421 | SkAlignedSTStorage<N, T> fStorage; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 422 | }; |
| 423 | |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 424 | template <typename T> void* operator new(size_t size, GrTAllocator<T>* allocator) { |
| 425 | return allocator->fAllocator.push_back(); |
| 426 | } |
| 427 | |
| 428 | // Skia doesn't use C++ exceptions but it may be compiled with them enabled. Having an op delete |
| 429 | // to match the op new silences warnings about missing op delete when a constructor throws an |
| 430 | // exception. |
| 431 | template <typename T> void operator delete(void*, GrTAllocator<T>*) { |
djsollen | f2b340f | 2016-01-29 08:51:04 -0800 | [diff] [blame] | 432 | SK_ABORT("Invalid Operation"); |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | #define GrNEW_APPEND_TO_ALLOCATOR(allocator_ptr, type_name, args) \ |
| 436 | new (allocator_ptr) type_name args |
| 437 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 438 | #endif |