halcanary@google.com | bc55eec | 2013-12-10 18:33:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | |
halcanary | 51761d1 | 2016-09-08 08:58:37 -0700 | [diff] [blame] | 8 | #include "SkDiscardableMemoryPool.h" |
halcanary@google.com | bc55eec | 2013-12-10 18:33:07 +0000 | [diff] [blame] | 9 | |
| 10 | #include "Test.h" |
halcanary@google.com | bc55eec | 2013-12-10 18:33:07 +0000 | [diff] [blame] | 11 | |
halcanary | 51761d1 | 2016-09-08 08:58:37 -0700 | [diff] [blame] | 12 | namespace { |
| 13 | constexpr char kTestString[] = "HELLO, WORLD!"; |
| 14 | constexpr size_t kTestStringLength = sizeof(kTestString); |
| 15 | } |
| 16 | |
| 17 | static void test_dm(skiatest::Reporter* reporter, |
| 18 | SkDiscardableMemory* dm, |
| 19 | bool assertRelock) { |
| 20 | REPORTER_ASSERT(reporter, dm); |
| 21 | if (!dm) { |
halcanary@google.com | bc55eec | 2013-12-10 18:33:07 +0000 | [diff] [blame] | 22 | return; |
| 23 | } |
| 24 | void* ptr = dm->data(); |
halcanary | 51761d1 | 2016-09-08 08:58:37 -0700 | [diff] [blame] | 25 | REPORTER_ASSERT(reporter, ptr); |
| 26 | if (!ptr) { |
| 27 | return; |
| 28 | } |
| 29 | memcpy(ptr, kTestString, sizeof(kTestString)); |
halcanary@google.com | bc55eec | 2013-12-10 18:33:07 +0000 | [diff] [blame] | 30 | dm->unlock(); |
halcanary | 51761d1 | 2016-09-08 08:58:37 -0700 | [diff] [blame] | 31 | bool relockSuccess = dm->lock(); |
| 32 | if (assertRelock) { |
| 33 | REPORTER_ASSERT(reporter, relockSuccess); |
| 34 | } |
| 35 | if (!relockSuccess) { |
halcanary@google.com | bc55eec | 2013-12-10 18:33:07 +0000 | [diff] [blame] | 36 | return; |
| 37 | } |
| 38 | ptr = dm->data(); |
halcanary | 51761d1 | 2016-09-08 08:58:37 -0700 | [diff] [blame] | 39 | REPORTER_ASSERT(reporter, ptr); |
| 40 | if (!ptr) { |
| 41 | return; |
| 42 | } |
| 43 | REPORTER_ASSERT(reporter, 0 == memcmp(ptr, kTestString, kTestStringLength)); |
halcanary@google.com | bc55eec | 2013-12-10 18:33:07 +0000 | [diff] [blame] | 44 | dm->unlock(); |
| 45 | } |
halcanary | 51761d1 | 2016-09-08 08:58:37 -0700 | [diff] [blame] | 46 | |
| 47 | DEF_TEST(DiscardableMemory_global, reporter) { |
| 48 | std::unique_ptr<SkDiscardableMemory> dm(SkDiscardableMemory::Create(kTestStringLength)); |
| 49 | // lock() test is allowed to fail, since other threads could be |
| 50 | // using global pool. |
| 51 | test_dm(reporter, dm.get(), false); |
| 52 | } |
| 53 | |
| 54 | DEF_TEST(DiscardableMemory_nonglobal, reporter) { |
| 55 | std::unique_ptr<SkDiscardableMemoryPool> pool( |
| 56 | SkDiscardableMemoryPool::Create(1024, /* mutex = */ nullptr)); |
| 57 | std::unique_ptr<SkDiscardableMemory> dm(pool->create(kTestStringLength)); |
| 58 | test_dm(reporter, dm.get(), true); |
| 59 | } |
| 60 | |