blob: 76412eec7763dab629dcea475bc461341187d5f3 [file] [log] [blame]
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +00001/*
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 */
reed@google.comebd24962012-05-17 14:28:11 +00007
reed@google.comebd24962012-05-17 14:28:11 +00008#include "SkChunkAlloc.h"
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +00009#include "SkUtils.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000010#include "Test.h"
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000011
robertphillipsa9061de2015-02-27 08:31:57 -080012static 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 SkDEBUGCODE(REPORTER_ASSERT(reporter, alloc.blockCount() == numBlocks);)
17}
rmistry@google.comd6176b02012-08-23 18:14:13 +000018
robertphillipsa9061de2015-02-27 08:31:57 -080019static void* simple_alloc(skiatest::Reporter* reporter, SkChunkAlloc* alloc, size_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
26static void test_chunkalloc(skiatest::Reporter* reporter) {
27 static const size_t kMin = 1024;
28 SkChunkAlloc alloc(kMin);
29
30 //------------------------------------------------------------------------
31 // check empty
32 check_alloc(reporter, alloc, 0, 0, 0);
halcanary96fcdcc2015-08-27 07:41:13 -070033 REPORTER_ASSERT(reporter, !alloc.contains(nullptr));
reed@google.comebd24962012-05-17 14:28:11 +000034 REPORTER_ASSERT(reporter, !alloc.contains(reporter));
35
robertphillipsa9061de2015-02-27 08:31:57 -080036 // reset on empty allocator
reed@google.comebd24962012-05-17 14:28:11 +000037 alloc.reset();
robertphillipsa9061de2015-02-27 08:31:57 -080038 check_alloc(reporter, alloc, 0, 0, 0);
reed@google.comebd24962012-05-17 14:28:11 +000039
robertphillipsa9061de2015-02-27 08:31:57 -080040 // rewind on empty allocator
41 alloc.rewind();
42 check_alloc(reporter, alloc, 0, 0, 0);
43
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 allocating a second block
67 ptr = simple_alloc(reporter, &alloc, size);
68
69 ptr = alloc.allocThrow(kMin);
70 check_alloc(reporter, alloc, 2*kMin, size+kMin, 2);
reed@google.comebd24962012-05-17 14:28:11 +000071 REPORTER_ASSERT(reporter, alloc.contains(ptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +000072
robertphillipsa9061de2015-02-27 08:31:57 -080073 //------------------------------------------------------------------------
74 // test out unalloc
75 size_t freed = alloc.unalloc(ptr);
76 REPORTER_ASSERT(reporter, freed == kMin);
77 check_alloc(reporter, alloc, 2*kMin, size, 2);
reed@google.comebd24962012-05-17 14:28:11 +000078 REPORTER_ASSERT(reporter, !alloc.contains(ptr));
79}
80
81///////////////////////////////////////////////////////////////////////////////
82
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000083static void set_zero(void* dst, size_t bytes) {
84 char* ptr = (char*)dst;
85 for (size_t i = 0; i < bytes; ++i) {
86 ptr[i] = 0;
87 }
88}
89
90#define MAX_ALIGNMENT 64
91#define MAX_COUNT ((MAX_ALIGNMENT) * 32)
92#define PAD 32
93#define TOTAL (PAD + MAX_ALIGNMENT + MAX_COUNT + PAD)
94
95#define VALUE16 0x1234
96#define VALUE32 0x12345678
97
98static bool compare16(const uint16_t base[], uint16_t value, int count) {
99 for (int i = 0; i < count; ++i) {
100 if (base[i] != value) {
101 SkDebugf("[%d] expected %x found %x\n", i, value, base[i]);
102 return false;
103 }
104 }
105 return true;
106}
107
108static bool compare32(const uint32_t base[], uint32_t value, int count) {
109 for (int i = 0; i < count; ++i) {
110 if (base[i] != value) {
111 SkDebugf("[%d] expected %x found %x\n", i, value, base[i]);
112 return false;
113 }
114 }
115 return true;
116}
117
118static void test_16(skiatest::Reporter* reporter) {
119 uint16_t buffer[TOTAL];
rmistry@google.comd6176b02012-08-23 18:14:13 +0000120
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +0000121 for (int count = 0; count < MAX_COUNT; ++count) {
122 for (int alignment = 0; alignment < MAX_ALIGNMENT; ++alignment) {
123 set_zero(buffer, sizeof(buffer));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000124
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +0000125 uint16_t* base = &buffer[PAD + alignment];
126 sk_memset16(base, VALUE16, count);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000127
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000128 REPORTER_ASSERT(reporter,
129 compare16(buffer, 0, PAD + alignment) &&
130 compare16(base, VALUE16, count) &&
131 compare16(base + count, 0, TOTAL - count - PAD - alignment));
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +0000132 }
133 }
134}
135
136static void test_32(skiatest::Reporter* reporter) {
137 uint32_t buffer[TOTAL];
rmistry@google.comd6176b02012-08-23 18:14:13 +0000138
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +0000139 for (int count = 0; count < MAX_COUNT; ++count) {
140 for (int alignment = 0; alignment < MAX_ALIGNMENT; ++alignment) {
141 set_zero(buffer, sizeof(buffer));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000142
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +0000143 uint32_t* base = &buffer[PAD + alignment];
144 sk_memset32(base, VALUE32, count);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000145
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000146 REPORTER_ASSERT(reporter,
147 compare32(buffer, 0, PAD + alignment) &&
148 compare32(base, VALUE32, count) &&
149 compare32(base + count, 0, TOTAL - count - PAD - alignment));
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +0000150 }
151 }
152}
153
154/**
155 * Test sk_memset16 and sk_memset32.
156 * For performance considerations, implementations may take different paths
157 * depending on the alignment of the dst, and/or the size of the count.
158 */
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000159DEF_TEST(Memset, reporter) {
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +0000160 test_16(reporter);
161 test_32(reporter);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000162
reed@google.comebd24962012-05-17 14:28:11 +0000163 test_chunkalloc(reporter);
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000164}