2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkTObjectPool.h"
9 #include "SkTObjectPool.h"
15 SK_DECLARE_INTERNAL_SLIST_INTERFACE(PoolEntry);
18 static const int kNumItemsPerBlock = 3;
19 typedef SkTObjectPool<PoolEntry, kNumItemsPerBlock> ObjectPoolType;
21 static bool verifyPool(skiatest::Reporter* reporter,
22 const ObjectPoolType& pool,
24 int available, int blocks) {
25 if (available != pool.available()) {
26 ERRORF(reporter, "%s - Pool available is %d not %d",
27 stage, pool.available(), available);
30 if (blocks != pool.blocks()) {
31 ERRORF(reporter, "%s - Pool blocks is %d not %d",
32 stage, pool.blocks(), blocks);
38 static const int kNumToAcquire = kNumItemsPerBlock * 5;
39 static void testObjectPool(skiatest::Reporter* reporter) {
41 SkTInternalSList<PoolEntry> used;
42 verifyPool(reporter, pool, "empty", 0, 0);
43 for (int index = 0; index < kNumToAcquire; ++index) {
44 used.push(pool.acquire());
45 int blocks = (index / kNumItemsPerBlock) + 1;
46 int available = (blocks * kNumItemsPerBlock) - (index + 1);
47 if (!verifyPool(reporter, pool, "acquire", available, blocks)) {
51 int available = pool.available();
52 int blocks = pool.blocks();
53 for (int index = 0; index < kNumToAcquire / 2; ++index) {
54 pool.release(used.pop());
56 if (!verifyPool(reporter, pool, "release", available, blocks)) {
60 available += used.getCount();
61 pool.releaseAll(&used);
62 REPORTER_ASSERT(reporter, used.isEmpty());
63 verifyPool(reporter, pool, "releaseAll", available, blocks);
66 DEF_TEST(ObjectPool, reporter) {
67 testObjectPool(reporter);