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