OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkChunkAlloc.h" | 8 #include "SkChunkAlloc.h" |
9 #include "SkUtils.h" | 9 #include "SkUtils.h" |
10 #include "Test.h" | 10 #include "Test.h" |
11 | 11 |
| 12 static void check_alloc(skiatest::Reporter* reporter, const SkChunkAlloc& alloc, |
| 13 size_t capacity, size_t used, int numBlocks) { |
| 14 REPORTER_ASSERT(reporter, alloc.totalCapacity() >= capacity); |
| 15 REPORTER_ASSERT(reporter, alloc.totalUsed() == used); |
| 16 REPORTER_ASSERT(reporter, alloc.blockCount() == numBlocks); |
| 17 } |
| 18 |
| 19 static void* simple_alloc(skiatest::Reporter* reporter, SkChunkAlloc* alloc, siz
e_t size) { |
| 20 void* ptr = alloc->allocThrow(size); |
| 21 check_alloc(reporter, *alloc, size, size, 1); |
| 22 REPORTER_ASSERT(reporter, alloc->contains(ptr)); |
| 23 return ptr; |
| 24 } |
| 25 |
12 static void test_chunkalloc(skiatest::Reporter* reporter) { | 26 static void test_chunkalloc(skiatest::Reporter* reporter) { |
13 size_t min = 256; | 27 static const size_t kMin = 1024; |
14 SkChunkAlloc alloc(min); | 28 SkChunkAlloc alloc(kMin); |
15 | 29 |
16 REPORTER_ASSERT(reporter, 0 == alloc.totalCapacity()); | 30 //------------------------------------------------------------------------ |
17 REPORTER_ASSERT(reporter, 0 == alloc.totalUsed()); | 31 // check empty |
18 REPORTER_ASSERT(reporter, 0 == alloc.blockCount()); | 32 check_alloc(reporter, alloc, 0, 0, 0); |
19 REPORTER_ASSERT(reporter, !alloc.contains(NULL)); | 33 REPORTER_ASSERT(reporter, !alloc.contains(NULL)); |
20 REPORTER_ASSERT(reporter, !alloc.contains(reporter)); | 34 REPORTER_ASSERT(reporter, !alloc.contains(reporter)); |
21 | 35 |
| 36 // reset on empty allocator |
22 alloc.reset(); | 37 alloc.reset(); |
23 REPORTER_ASSERT(reporter, 0 == alloc.totalCapacity()); | 38 check_alloc(reporter, alloc, 0, 0, 0); |
24 REPORTER_ASSERT(reporter, 0 == alloc.totalUsed()); | |
25 REPORTER_ASSERT(reporter, 0 == alloc.blockCount()); | |
26 | 39 |
27 size_t size = min >> 1; | 40 // rewind on empty allocator |
28 void* ptr = alloc.allocThrow(size); | 41 alloc.rewind(); |
29 REPORTER_ASSERT(reporter, alloc.totalCapacity() >= size); | 42 check_alloc(reporter, alloc, 0, 0, 0); |
30 REPORTER_ASSERT(reporter, alloc.totalUsed() == size); | 43 |
31 REPORTER_ASSERT(reporter, alloc.blockCount() > 0); | 44 //------------------------------------------------------------------------ |
| 45 // test reset when something is allocated |
| 46 size_t size = kMin >> 1; |
| 47 void* ptr = simple_alloc(reporter, &alloc, size); |
| 48 |
| 49 alloc.reset(); |
| 50 check_alloc(reporter, alloc, 0, 0, 0); |
| 51 REPORTER_ASSERT(reporter, !alloc.contains(ptr)); |
| 52 |
| 53 //------------------------------------------------------------------------ |
| 54 // test rewind when something is allocated |
| 55 ptr = simple_alloc(reporter, &alloc, size); |
| 56 |
| 57 alloc.rewind(); |
| 58 check_alloc(reporter, alloc, size, 0, 1); |
| 59 REPORTER_ASSERT(reporter, !alloc.contains(ptr)); |
| 60 |
| 61 // use the available block |
| 62 ptr = simple_alloc(reporter, &alloc, size); |
| 63 alloc.reset(); |
| 64 |
| 65 //------------------------------------------------------------------------ |
| 66 // test out pre allocating a block |
| 67 alloc.preAlloc(size); |
| 68 check_alloc(reporter, alloc, size, 0, 1); |
| 69 |
| 70 // use the available block |
| 71 ptr = simple_alloc(reporter, &alloc, size); |
| 72 alloc.reset(); |
| 73 |
| 74 //------------------------------------------------------------------------ |
| 75 // test out allocating a second block |
| 76 ptr = simple_alloc(reporter, &alloc, size); |
| 77 |
| 78 ptr = alloc.allocThrow(kMin); |
| 79 check_alloc(reporter, alloc, 2*kMin, size+kMin, 2); |
32 REPORTER_ASSERT(reporter, alloc.contains(ptr)); | 80 REPORTER_ASSERT(reporter, alloc.contains(ptr)); |
33 | 81 |
34 alloc.reset(); | 82 //------------------------------------------------------------------------ |
| 83 // test out unalloc |
| 84 size_t freed = alloc.unalloc(ptr); |
| 85 REPORTER_ASSERT(reporter, freed == kMin); |
| 86 check_alloc(reporter, alloc, 2*kMin, size, 2); |
35 REPORTER_ASSERT(reporter, !alloc.contains(ptr)); | 87 REPORTER_ASSERT(reporter, !alloc.contains(ptr)); |
36 REPORTER_ASSERT(reporter, 0 == alloc.totalCapacity()); | |
37 REPORTER_ASSERT(reporter, 0 == alloc.totalUsed()); | |
38 } | 88 } |
39 | 89 |
40 /////////////////////////////////////////////////////////////////////////////// | 90 /////////////////////////////////////////////////////////////////////////////// |
41 | 91 |
42 static void set_zero(void* dst, size_t bytes) { | 92 static void set_zero(void* dst, size_t bytes) { |
43 char* ptr = (char*)dst; | 93 char* ptr = (char*)dst; |
44 for (size_t i = 0; i < bytes; ++i) { | 94 for (size_t i = 0; i < bytes; ++i) { |
45 ptr[i] = 0; | 95 ptr[i] = 0; |
46 } | 96 } |
47 } | 97 } |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 * Test sk_memset16 and sk_memset32. | 164 * Test sk_memset16 and sk_memset32. |
115 * For performance considerations, implementations may take different paths | 165 * For performance considerations, implementations may take different paths |
116 * depending on the alignment of the dst, and/or the size of the count. | 166 * depending on the alignment of the dst, and/or the size of the count. |
117 */ | 167 */ |
118 DEF_TEST(Memset, reporter) { | 168 DEF_TEST(Memset, reporter) { |
119 test_16(reporter); | 169 test_16(reporter); |
120 test_32(reporter); | 170 test_32(reporter); |
121 | 171 |
122 test_chunkalloc(reporter); | 172 test_chunkalloc(reporter); |
123 } | 173 } |
OLD | NEW |