blob: e9bae281d5a447c834fcd2f2044b1cb40dfd65c6 [file] [log] [blame]
Mike Klein58b13062016-11-11 10:38:49 -05001/*
2 * Copyright 2016 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
8#include "Test.h"
9#include "SkFixedAlloc.h"
10
11namespace {
12
Mike Klein1ee70352016-11-15 10:26:44 -050013 static int created;
Mike Klein58b13062016-11-11 10:38:49 -050014
15 struct Foo {
16 Foo(int X, float Y) : x(X), y(Y) { created++; }
Mike Klein58b13062016-11-11 10:38:49 -050017
18 int x;
19 float y;
20 };
21
22 struct Big {
23 Big() {}
24 uint32_t array[128];
25 };
26
27}
28
29DEF_TEST(FixedAlloc, r) {
30 // Basic mechanics.
31 {
32 uint8_t buf[128];
33 SkFixedAlloc fa(buf, sizeof(buf));
34
35 Foo* foo = fa.make<Foo>(3, 4.0f);
36 REPORTER_ASSERT(r, foo);
37 REPORTER_ASSERT(r, foo->x == 3);
38 REPORTER_ASSERT(r, foo->y == 4.0f);
39 REPORTER_ASSERT(r, created == 1);
Mike Klein58b13062016-11-11 10:38:49 -050040
41 Foo* bar = fa.make<Foo>(8, 1.0f);
42 REPORTER_ASSERT(r, bar);
43 REPORTER_ASSERT(r, bar->x == 8);
44 REPORTER_ASSERT(r, bar->y == 1.0f);
45 REPORTER_ASSERT(r, created == 2);
Mike Klein58b13062016-11-11 10:38:49 -050046
47 fa.undo();
Mike Klein58b13062016-11-11 10:38:49 -050048 }
Mike Klein58b13062016-11-11 10:38:49 -050049
50 {
51 // Test alignment gurantees.
52 uint8_t buf[64];
53 SkFixedAlloc fa(buf+3, sizeof(buf)-3);
54
55 Foo* foo = fa.make<Foo>(3, 4.0f);
56 REPORTER_ASSERT(r, SkIsAlign4((uintptr_t)foo));
57 REPORTER_ASSERT(r, created == 3);
Mike Klein58b13062016-11-11 10:38:49 -050058
59 // Might as well test reset() while we're at it.
60 fa.reset();
Mike Klein58b13062016-11-11 10:38:49 -050061 }
Mike Klein58b13062016-11-11 10:38:49 -050062}
63
64DEF_TEST(FallbackAlloc, r) {
65 // SkFixedAlloc will eventually fail when it runs out of space in its buffer.
66 int buf[32];
67 SkFixedAlloc fixed(buf, sizeof(buf));
68 bool fixed_failed = false;
69 for (int i = 0; i < 32; i++) {
Mike Klein1ee70352016-11-15 10:26:44 -050070 // (Remember, there is some overhead to each copy() call.)
71 fixed_failed = fixed_failed || (fixed.copy(i) == nullptr);
Mike Klein58b13062016-11-11 10:38:49 -050072 }
73 REPORTER_ASSERT(r, fixed_failed);
74
75
76 // SkFallbackAlloc will always succeed, using the heap as required.
77 fixed.reset();
78 SkFallbackAlloc fallback(&fixed);
79
80 bool fallback_failed = false;
81 for (int i = 0; i < 32; i++) {
Mike Klein1ee70352016-11-15 10:26:44 -050082 fallback_failed = fallback_failed || (fallback.copy(i) == nullptr);
Mike Klein58b13062016-11-11 10:38:49 -050083 }
84 REPORTER_ASSERT(r, !fallback_failed);
85
86
87 // Test small, big, small allocations to make sure once we go to the heap we stay there.
88 fallback.reset();
89 auto smallA = fallback.make<int>(2);
90 auto big = fallback.make<Big>();
91 auto smallB = fallback.make<int>(3);
92
93 auto in_buf = [&](void* ptr) {
94 return (uintptr_t)(buf+0 ) <= (uintptr_t)ptr
95 && (uintptr_t)(buf+32) > (uintptr_t)ptr;
96 };
97
98 REPORTER_ASSERT(r, in_buf(smallA));
99 REPORTER_ASSERT(r, !in_buf(big));
100 REPORTER_ASSERT(r, !in_buf(smallB));
101}