bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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. |
| 6 | */ |
| 7 | |
bsalomon@google.com | a68937c | 2012-08-03 15:00:52 +0000 | [diff] [blame] | 8 | #include "Test.h" |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 9 | #include "GrMemoryPool.h" |
| 10 | #include "SkRandom.h" |
dskiba | e4cd006 | 2016-11-29 06:50:35 -0800 | [diff] [blame] | 11 | #include "SkTArray.h" |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 12 | #include "SkTDArray.h" |
reed@google.com | 9d1cff1 | 2013-04-18 18:43:26 +0000 | [diff] [blame] | 13 | #include "SkTemplates.h" |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 14 | |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 15 | // A is the top of an inheritance tree of classes that overload op new and |
| 16 | // and delete to use a GrMemoryPool. The objects have values of different types |
| 17 | // that can be set and checked. |
| 18 | class A { |
| 19 | public: |
Mike Klein | fc6c37b | 2016-09-27 09:34:10 -0400 | [diff] [blame] | 20 | A() {} |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 21 | virtual void setValues(int v) { |
Brian Osman | 50ea3c0 | 2019-02-04 10:01:53 -0500 | [diff] [blame] | 22 | fChar = static_cast<char>(v & 0xFF); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 23 | } |
| 24 | virtual bool checkValues(int v) { |
Brian Osman | 50ea3c0 | 2019-02-04 10:01:53 -0500 | [diff] [blame] | 25 | return fChar == static_cast<char>(v & 0xFF); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 26 | } |
Mike Klein | fc6c37b | 2016-09-27 09:34:10 -0400 | [diff] [blame] | 27 | virtual ~A() {} |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 28 | |
| 29 | void* operator new(size_t size) { |
| 30 | if (!gPool.get()) { |
| 31 | return ::operator new(size); |
| 32 | } else { |
| 33 | return gPool->allocate(size); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | void operator delete(void* p) { |
| 38 | if (!gPool.get()) { |
| 39 | ::operator delete(p); |
| 40 | } else { |
| 41 | return gPool->release(p); |
| 42 | } |
| 43 | } |
| 44 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 45 | static A* Create(SkRandom* r); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 46 | |
| 47 | static void SetAllocator(size_t preallocSize, size_t minAllocSize) { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 48 | GrMemoryPool* pool = new GrMemoryPool(preallocSize, minAllocSize); |
| 49 | gPool.reset(pool); |
| 50 | } |
| 51 | |
| 52 | static void ResetAllocator() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 53 | gPool.reset(nullptr); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | private: |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 57 | static std::unique_ptr<GrMemoryPool> gPool; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 58 | char fChar; |
| 59 | }; |
commit-bot@chromium.org | ab1c138 | 2013-12-05 12:08:12 +0000 | [diff] [blame] | 60 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 61 | std::unique_ptr<GrMemoryPool> A::gPool; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 62 | |
| 63 | class B : public A { |
| 64 | public: |
Mike Klein | fc6c37b | 2016-09-27 09:34:10 -0400 | [diff] [blame] | 65 | B() {} |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 66 | virtual void setValues(int v) { |
| 67 | fDouble = static_cast<double>(v); |
| 68 | this->INHERITED::setValues(v); |
| 69 | } |
| 70 | virtual bool checkValues(int v) { |
| 71 | return fDouble == static_cast<double>(v) && |
| 72 | this->INHERITED::checkValues(v); |
| 73 | } |
Mike Klein | fc6c37b | 2016-09-27 09:34:10 -0400 | [diff] [blame] | 74 | virtual ~B() {} |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 75 | |
| 76 | private: |
| 77 | double fDouble; |
| 78 | |
| 79 | typedef A INHERITED; |
| 80 | }; |
| 81 | |
| 82 | class C : public A { |
| 83 | public: |
Mike Klein | fc6c37b | 2016-09-27 09:34:10 -0400 | [diff] [blame] | 84 | C() {} |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 85 | virtual void setValues(int v) { |
| 86 | fInt64 = static_cast<int64_t>(v); |
| 87 | this->INHERITED::setValues(v); |
| 88 | } |
| 89 | virtual bool checkValues(int v) { |
| 90 | return fInt64 == static_cast<int64_t>(v) && |
| 91 | this->INHERITED::checkValues(v); |
| 92 | } |
Mike Klein | fc6c37b | 2016-09-27 09:34:10 -0400 | [diff] [blame] | 93 | virtual ~C() {} |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 94 | |
| 95 | private: |
| 96 | int64_t fInt64; |
| 97 | |
| 98 | typedef A INHERITED; |
| 99 | }; |
| 100 | |
| 101 | // D derives from C and owns a dynamically created B |
| 102 | class D : public C { |
| 103 | public: |
| 104 | D() { |
| 105 | fB = new B(); |
| 106 | } |
| 107 | virtual void setValues(int v) { |
bsalomon | ebc1c10 | 2015-08-06 17:33:16 -0700 | [diff] [blame] | 108 | fVoidStar = reinterpret_cast<void*>(static_cast<intptr_t>(v)); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 109 | this->INHERITED::setValues(v); |
| 110 | fB->setValues(v); |
| 111 | } |
| 112 | virtual bool checkValues(int v) { |
bsalomon | ebc1c10 | 2015-08-06 17:33:16 -0700 | [diff] [blame] | 113 | return fVoidStar == reinterpret_cast<void*>(static_cast<intptr_t>(v)) && |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 114 | fB->checkValues(v) && |
| 115 | this->INHERITED::checkValues(v); |
| 116 | } |
| 117 | virtual ~D() { |
| 118 | delete fB; |
| 119 | } |
| 120 | private: |
| 121 | void* fVoidStar; |
| 122 | B* fB; |
| 123 | |
| 124 | typedef C INHERITED; |
| 125 | }; |
| 126 | |
| 127 | class E : public A { |
| 128 | public: |
| 129 | E() {} |
| 130 | virtual void setValues(int v) { |
| 131 | for (size_t i = 0; i < SK_ARRAY_COUNT(fIntArray); ++i) { |
| 132 | fIntArray[i] = v; |
| 133 | } |
| 134 | this->INHERITED::setValues(v); |
| 135 | } |
| 136 | virtual bool checkValues(int v) { |
| 137 | bool ok = true; |
| 138 | for (size_t i = 0; ok && i < SK_ARRAY_COUNT(fIntArray); ++i) { |
| 139 | if (fIntArray[i] != v) { |
| 140 | ok = false; |
| 141 | } |
| 142 | } |
| 143 | return ok && this->INHERITED::checkValues(v); |
| 144 | } |
| 145 | virtual ~E() {} |
| 146 | private: |
| 147 | int fIntArray[20]; |
| 148 | |
| 149 | typedef A INHERITED; |
| 150 | }; |
| 151 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 152 | A* A::Create(SkRandom* r) { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 153 | switch (r->nextRangeU(0, 4)) { |
| 154 | case 0: |
| 155 | return new A; |
| 156 | case 1: |
| 157 | return new B; |
| 158 | case 2: |
| 159 | return new C; |
| 160 | case 3: |
| 161 | return new D; |
| 162 | case 4: |
| 163 | return new E; |
| 164 | default: |
| 165 | // suppress warning |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 166 | return nullptr; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 169 | |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 170 | struct Rec { |
| 171 | A* fInstance; |
| 172 | int fValue; |
| 173 | }; |
| 174 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 175 | DEF_TEST(GrMemoryPool, reporter) { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 176 | // prealloc and min alloc sizes for the pool |
| 177 | static const size_t gSizes[][2] = { |
| 178 | {0, 0}, |
| 179 | {10 * sizeof(A), 20 * sizeof(A)}, |
| 180 | {100 * sizeof(A), 100 * sizeof(A)}, |
| 181 | {500 * sizeof(A), 500 * sizeof(A)}, |
| 182 | {10000 * sizeof(A), 0}, |
| 183 | {1, 100 * sizeof(A)}, |
| 184 | }; |
| 185 | // different percentages of creation vs deletion |
| 186 | static const float gCreateFraction[] = {1.f, .95f, 0.75f, .5f}; |
| 187 | // number of create/destroys per test |
| 188 | static const int kNumIters = 20000; |
| 189 | // check that all the values stored in A objects are correct after this |
| 190 | // number of iterations |
| 191 | static const int kCheckPeriod = 500; |
| 192 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 193 | SkRandom r; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 194 | for (size_t s = 0; s < SK_ARRAY_COUNT(gSizes); ++s) { |
| 195 | A::SetAllocator(gSizes[s][0], gSizes[s][1]); |
| 196 | for (size_t c = 0; c < SK_ARRAY_COUNT(gCreateFraction); ++c) { |
| 197 | SkTDArray<Rec> instanceRecs; |
| 198 | for (int i = 0; i < kNumIters; ++i) { |
| 199 | float createOrDestroy = r.nextUScalar1(); |
| 200 | if (createOrDestroy < gCreateFraction[c] || |
| 201 | 0 == instanceRecs.count()) { |
| 202 | Rec* rec = instanceRecs.append(); |
| 203 | rec->fInstance = A::Create(&r); |
| 204 | rec->fValue = static_cast<int>(r.nextU()); |
| 205 | rec->fInstance->setValues(rec->fValue); |
| 206 | } else { |
| 207 | int d = r.nextRangeU(0, instanceRecs.count() - 1); |
| 208 | Rec& rec = instanceRecs[d]; |
| 209 | REPORTER_ASSERT(reporter, rec.fInstance->checkValues(rec.fValue)); |
| 210 | delete rec.fInstance; |
| 211 | instanceRecs.removeShuffle(d); |
| 212 | } |
| 213 | if (0 == i % kCheckPeriod) { |
| 214 | for (int r = 0; r < instanceRecs.count(); ++r) { |
| 215 | Rec& rec = instanceRecs[r]; |
| 216 | REPORTER_ASSERT(reporter, rec.fInstance->checkValues(rec.fValue)); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | for (int i = 0; i < instanceRecs.count(); ++i) { |
| 221 | Rec& rec = instanceRecs[i]; |
| 222 | REPORTER_ASSERT(reporter, rec.fInstance->checkValues(rec.fValue)); |
| 223 | delete rec.fInstance; |
| 224 | } |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
dskiba | e4cd006 | 2016-11-29 06:50:35 -0800 | [diff] [blame] | 229 | // GrMemoryPool requires that it's empty at the point of destruction. This helps |
| 230 | // achieving that by releasing all added memory in the destructor. |
| 231 | class AutoPoolReleaser { |
| 232 | public: |
| 233 | AutoPoolReleaser(GrMemoryPool& pool): fPool(pool) { |
| 234 | } |
| 235 | ~AutoPoolReleaser() { |
| 236 | for (void* ptr: fAllocated) { |
| 237 | fPool.release(ptr); |
| 238 | } |
| 239 | } |
| 240 | void add(void* ptr) { |
| 241 | fAllocated.push_back(ptr); |
| 242 | } |
| 243 | private: |
| 244 | GrMemoryPool& fPool; |
| 245 | SkTArray<void*> fAllocated; |
| 246 | }; |
| 247 | |
| 248 | DEF_TEST(GrMemoryPoolAPI, reporter) { |
| 249 | constexpr size_t kSmallestMinAllocSize = GrMemoryPool::kSmallestMinAllocSize; |
| 250 | |
| 251 | // Allocates memory until pool adds a new block (pool.size() changes). |
| 252 | auto allocateMemory = [](GrMemoryPool& pool, AutoPoolReleaser& r) { |
| 253 | size_t origPoolSize = pool.size(); |
| 254 | while (pool.size() == origPoolSize) { |
| 255 | r.add(pool.allocate(31)); |
| 256 | } |
| 257 | }; |
| 258 | |
| 259 | // Effective prealloc space capacity is >= kSmallestMinAllocSize. |
| 260 | { |
| 261 | GrMemoryPool pool(0, 0); |
| 262 | REPORTER_ASSERT(reporter, pool.preallocSize() == kSmallestMinAllocSize); |
| 263 | } |
| 264 | |
| 265 | // Effective prealloc space capacity is >= minAllocSize. |
| 266 | { |
| 267 | constexpr size_t kMinAllocSize = kSmallestMinAllocSize * 2; |
| 268 | GrMemoryPool pool(kSmallestMinAllocSize, kMinAllocSize); |
| 269 | REPORTER_ASSERT(reporter, pool.preallocSize() == kMinAllocSize); |
| 270 | } |
| 271 | |
| 272 | // Effective block size capacity >= kSmallestMinAllocSize. |
| 273 | { |
| 274 | GrMemoryPool pool(kSmallestMinAllocSize, kSmallestMinAllocSize / 2); |
| 275 | AutoPoolReleaser r(pool); |
| 276 | |
| 277 | allocateMemory(pool, r); |
| 278 | REPORTER_ASSERT(reporter, pool.size() == kSmallestMinAllocSize); |
| 279 | } |
| 280 | |
| 281 | // Pool allocates exactly preallocSize on creation. |
| 282 | { |
| 283 | constexpr size_t kPreallocSize = kSmallestMinAllocSize * 5; |
| 284 | GrMemoryPool pool(kPreallocSize, 0); |
| 285 | REPORTER_ASSERT(reporter, pool.preallocSize() == kPreallocSize); |
| 286 | } |
| 287 | |
| 288 | // Pool allocates exactly minAllocSize when it expands. |
| 289 | { |
| 290 | constexpr size_t kMinAllocSize = kSmallestMinAllocSize * 7; |
| 291 | GrMemoryPool pool(0, kMinAllocSize); |
| 292 | AutoPoolReleaser r(pool); |
| 293 | |
| 294 | allocateMemory(pool, r); |
| 295 | REPORTER_ASSERT(reporter, pool.size() == kMinAllocSize); |
| 296 | |
| 297 | allocateMemory(pool, r); |
| 298 | REPORTER_ASSERT(reporter, pool.size() == 2 * kMinAllocSize); |
| 299 | } |
| 300 | |
| 301 | // When asked to allocate amount > minAllocSize, pool allocates larger block |
| 302 | // to accommodate all internal structures. |
| 303 | { |
| 304 | constexpr size_t kMinAllocSize = kSmallestMinAllocSize * 2; |
| 305 | GrMemoryPool pool(kSmallestMinAllocSize, kMinAllocSize); |
| 306 | AutoPoolReleaser r(pool); |
| 307 | |
| 308 | REPORTER_ASSERT(reporter, pool.size() == 0); |
| 309 | |
| 310 | constexpr size_t hugeSize = 10 * kMinAllocSize; |
| 311 | r.add(pool.allocate(hugeSize)); |
| 312 | REPORTER_ASSERT(reporter, pool.size() > hugeSize); |
| 313 | |
| 314 | // Block size allocated to accommodate huge request doesn't include any extra |
| 315 | // space, so next allocation request allocates a new block. |
| 316 | size_t hugeBlockSize = pool.size(); |
| 317 | r.add(pool.allocate(0)); |
| 318 | REPORTER_ASSERT(reporter, pool.size() == hugeBlockSize + kMinAllocSize); |
| 319 | } |
| 320 | } |