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 "SkRandom.h" |
9 #include "SkUtils.h" | 10 #include "SkUtils.h" |
10 #include "Test.h" | 11 #include "Test.h" |
11 | 12 |
12 static void check_alloc(skiatest::Reporter* reporter, const SkChunkAlloc& alloc, | 13 static void check_alloc(skiatest::Reporter* reporter, const SkChunkAlloc& alloc, |
13 size_t capacity, size_t used, int numBlocks) { | 14 size_t capacity, size_t used, int numBlocks) { |
14 REPORTER_ASSERT(reporter, alloc.totalCapacity() >= capacity); | 15 REPORTER_ASSERT(reporter, alloc.totalCapacity() >= capacity); |
15 REPORTER_ASSERT(reporter, alloc.totalUsed() == used); | 16 REPORTER_ASSERT(reporter, alloc.totalUsed() == used); |
16 SkDEBUGCODE(REPORTER_ASSERT(reporter, alloc.blockCount() == numBlocks);) | 17 SkDEBUGCODE(REPORTER_ASSERT(reporter, alloc.blockCount() == numBlocks);) |
17 } | 18 } |
18 | 19 |
19 static void* simple_alloc(skiatest::Reporter* reporter, SkChunkAlloc* alloc, siz
e_t size) { | 20 static void* simple_alloc(skiatest::Reporter* reporter, SkChunkAlloc* alloc, siz
e_t size) { |
20 void* ptr = alloc->allocThrow(size); | 21 void* ptr = alloc->allocThrow(size); |
21 check_alloc(reporter, *alloc, size, size, 1); | 22 check_alloc(reporter, *alloc, size, size, 1); |
22 REPORTER_ASSERT(reporter, alloc->contains(ptr)); | 23 REPORTER_ASSERT(reporter, alloc->contains(ptr)); |
23 return ptr; | 24 return ptr; |
24 } | 25 } |
25 | 26 |
| 27 static void check_alloc_alignment(skiatest::Reporter* reporter, |
| 28 SkChunkAlloc* alloc, size_t size) { |
| 29 const size_t kAlignment = 8; |
| 30 void* ptr = alloc->allocThrow(size); |
| 31 REPORTER_ASSERT(reporter, ptr != nullptr); |
| 32 REPORTER_ASSERT(reporter, (size_t)ptr % kAlignment == 0); |
| 33 } |
| 34 |
26 static void test_chunkalloc(skiatest::Reporter* reporter) { | 35 static void test_chunkalloc(skiatest::Reporter* reporter) { |
27 static const size_t kMin = 1024; | 36 static const size_t kMin = 1024; |
28 SkChunkAlloc alloc(kMin); | 37 SkChunkAlloc alloc(kMin); |
29 | 38 |
30 //------------------------------------------------------------------------ | 39 //------------------------------------------------------------------------ |
31 // check empty | 40 // check empty |
32 check_alloc(reporter, alloc, 0, 0, 0); | 41 check_alloc(reporter, alloc, 0, 0, 0); |
33 REPORTER_ASSERT(reporter, !alloc.contains(nullptr)); | 42 REPORTER_ASSERT(reporter, !alloc.contains(nullptr)); |
34 REPORTER_ASSERT(reporter, !alloc.contains(reporter)); | 43 REPORTER_ASSERT(reporter, !alloc.contains(reporter)); |
35 | 44 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 ptr = alloc.allocThrow(kMin); | 78 ptr = alloc.allocThrow(kMin); |
70 check_alloc(reporter, alloc, 2*kMin, size+kMin, 2); | 79 check_alloc(reporter, alloc, 2*kMin, size+kMin, 2); |
71 REPORTER_ASSERT(reporter, alloc.contains(ptr)); | 80 REPORTER_ASSERT(reporter, alloc.contains(ptr)); |
72 | 81 |
73 //------------------------------------------------------------------------ | 82 //------------------------------------------------------------------------ |
74 // test out unalloc | 83 // test out unalloc |
75 size_t freed = alloc.unalloc(ptr); | 84 size_t freed = alloc.unalloc(ptr); |
76 REPORTER_ASSERT(reporter, freed == kMin); | 85 REPORTER_ASSERT(reporter, freed == kMin); |
77 check_alloc(reporter, alloc, 2*kMin, size, 2); | 86 check_alloc(reporter, alloc, 2*kMin, size, 2); |
78 REPORTER_ASSERT(reporter, !alloc.contains(ptr)); | 87 REPORTER_ASSERT(reporter, !alloc.contains(ptr)); |
| 88 |
| 89 //------------------------------------------------------------------------ |
| 90 // test the alignment |
| 91 alloc.reset(); |
| 92 SkRandom rand; |
| 93 for (int i = 0; i < 1000; i++) { |
| 94 check_alloc_alignment(reporter, &alloc, rand.nextU16()); |
| 95 } |
79 } | 96 } |
80 | 97 |
81 /////////////////////////////////////////////////////////////////////////////// | 98 /////////////////////////////////////////////////////////////////////////////// |
82 | 99 |
83 static void set_zero(void* dst, size_t bytes) { | 100 static void set_zero(void* dst, size_t bytes) { |
84 char* ptr = (char*)dst; | 101 char* ptr = (char*)dst; |
85 for (size_t i = 0; i < bytes; ++i) { | 102 for (size_t i = 0; i < bytes; ++i) { |
86 ptr[i] = 0; | 103 ptr[i] = 0; |
87 } | 104 } |
88 } | 105 } |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 * Test sk_memset16 and sk_memset32. | 170 * Test sk_memset16 and sk_memset32. |
154 * For performance considerations, implementations may take different paths | 171 * For performance considerations, implementations may take different paths |
155 * depending on the alignment of the dst, and/or the size of the count. | 172 * depending on the alignment of the dst, and/or the size of the count. |
156 */ | 173 */ |
157 DEF_TEST(Memset, reporter) { | 174 DEF_TEST(Memset, reporter) { |
158 test_16(reporter); | 175 test_16(reporter); |
159 test_32(reporter); | 176 test_32(reporter); |
160 | 177 |
161 test_chunkalloc(reporter); | 178 test_chunkalloc(reporter); |
162 } | 179 } |
OLD | NEW |