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