blob: 854b3c56eb1dba1a5e2b47a2e7895e3a2b665c9f [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
halcanary7d571242016-02-24 17:59:16 -080098static void compare16(skiatest::Reporter* r, const uint16_t base[],
99 uint16_t value, int count) {
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +0000100 for (int i = 0; i < count; ++i) {
101 if (base[i] != value) {
halcanary7d571242016-02-24 17:59:16 -0800102 ERRORF(r, "[%d] expected %x found %x\n", i, value, base[i]);
103 return;
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +0000104 }
105 }
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +0000106}
107
halcanary7d571242016-02-24 17:59:16 -0800108static void compare32(skiatest::Reporter* r, const uint32_t base[],
109 uint32_t value, int count) {
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +0000110 for (int i = 0; i < count; ++i) {
111 if (base[i] != value) {
halcanary7d571242016-02-24 17:59:16 -0800112 ERRORF(r, "[%d] expected %x found %x\n", i, value, base[i]);
113 return;
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +0000114 }
115 }
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +0000116}
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
halcanary7d571242016-02-24 17:59:16 -0800128 compare16(reporter, buffer, 0, PAD + alignment);
129 compare16(reporter, base, VALUE16, count);
130 compare16(reporter, base + count, 0, TOTAL - count - PAD - alignment);
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +0000131 }
132 }
133}
134
135static void test_32(skiatest::Reporter* reporter) {
136 uint32_t buffer[TOTAL];
rmistry@google.comd6176b02012-08-23 18:14:13 +0000137
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +0000138 for (int count = 0; count < MAX_COUNT; ++count) {
139 for (int alignment = 0; alignment < MAX_ALIGNMENT; ++alignment) {
140 set_zero(buffer, sizeof(buffer));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000141
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +0000142 uint32_t* base = &buffer[PAD + alignment];
143 sk_memset32(base, VALUE32, count);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000144
halcanary7d571242016-02-24 17:59:16 -0800145 compare32(reporter, buffer, 0, PAD + alignment);
146 compare32(reporter, base, VALUE32, count);
147 compare32(reporter, base + count, 0, TOTAL - count - PAD - alignment);
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +0000148 }
149 }
150}
151
152/**
153 * Test sk_memset16 and sk_memset32.
154 * For performance considerations, implementations may take different paths
155 * depending on the alignment of the dst, and/or the size of the count.
156 */
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000157DEF_TEST(Memset, reporter) {
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +0000158 test_16(reporter);
159 test_32(reporter);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000160
reed@google.comebd24962012-05-17 14:28:11 +0000161 test_chunkalloc(reporter);
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000162}