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