1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #include "tensorflow/lite/simple_memory_arena.h"
16
17 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 #include "tensorflow/lite/testing/util.h"
20
21 namespace tflite {
22 namespace {
23
TEST(SimpleMemoryArenaTest,BasicArenaOperations)24 TEST(SimpleMemoryArenaTest, BasicArenaOperations) {
25 TfLiteContext context;
26 SimpleMemoryArena arena(64);
27 ArenaAlloc allocs[6];
28
29 arena.Allocate(&context, 32, 2047, &allocs[0]);
30 arena.Allocate(&context, 32, 2047, &allocs[1]);
31 arena.Allocate(&context, 32, 2047, &allocs[2]);
32 arena.Deallocate(&context, allocs[0]);
33 arena.Allocate(&context, 32, 1023, &allocs[3]);
34 arena.Allocate(&context, 32, 2047, &allocs[4]);
35 arena.Deallocate(&context, allocs[1]);
36 arena.Allocate(&context, 32, 1023, &allocs[5]);
37
38 EXPECT_EQ(allocs[0].offset, 0);
39 EXPECT_EQ(allocs[1].offset, 2048);
40 EXPECT_EQ(allocs[2].offset, 4096);
41 EXPECT_EQ(allocs[3].offset, 0);
42 EXPECT_EQ(allocs[4].offset, 6144);
43 EXPECT_EQ(allocs[5].offset, 1024);
44 }
45
TEST(SimpleMemoryArenaTest,BasicZeroAlloc)46 TEST(SimpleMemoryArenaTest, BasicZeroAlloc) {
47 TfLiteContext context;
48 SimpleMemoryArena arena(64);
49 ArenaAlloc alloc;
50
51 // Zero-sized allocs should have a 0 offset and size.
52 ASSERT_EQ(arena.Allocate(&context, 32, 0, &alloc), kTfLiteOk);
53 EXPECT_EQ(alloc.offset, 0);
54 EXPECT_EQ(alloc.size, 0);
55
56 // Deallocation of zero-sized allocs should always succeed (even redundantly).
57 ASSERT_EQ(arena.Deallocate(&context, alloc), kTfLiteOk);
58 ASSERT_EQ(arena.Deallocate(&context, alloc), kTfLiteOk);
59
60 // The zero-sized alloc should resolve to null.
61 char* resolved_ptr = nullptr;
62 ASSERT_EQ(arena.Commit(&context), kTfLiteOk);
63 ASSERT_EQ(arena.ResolveAlloc(&context, alloc, &resolved_ptr), kTfLiteOk);
64 EXPECT_EQ(resolved_ptr, nullptr);
65 }
66
TEST(SimpleMemoryArenaTest,InterleavedZeroAlloc)67 TEST(SimpleMemoryArenaTest, InterleavedZeroAlloc) {
68 TfLiteContext context;
69 SimpleMemoryArena arena(64);
70 ArenaAlloc allocs[4];
71
72 // Interleave some zero and non-zero-sized allocations and deallocations.
73 ASSERT_EQ(arena.Allocate(&context, 32, 2047, &allocs[0]), kTfLiteOk);
74 ASSERT_EQ(arena.Allocate(&context, 32, 0, &allocs[1]), kTfLiteOk);
75 ASSERT_EQ(arena.Allocate(&context, 32, 1023, &allocs[2]), kTfLiteOk);
76 ASSERT_EQ(arena.Deallocate(&context, allocs[1]), kTfLiteOk);
77 ASSERT_EQ(arena.Deallocate(&context, allocs[2]), kTfLiteOk);
78 ASSERT_EQ(arena.Allocate(&context, 32, 2047, &allocs[3]), kTfLiteOk);
79
80 // Deallocation of a zero-sized alloc should not impact the allocator offsets.
81 EXPECT_EQ(allocs[0].offset, 0);
82 EXPECT_EQ(allocs[1].offset, 0);
83 EXPECT_EQ(allocs[2].offset, 2048);
84 EXPECT_EQ(allocs[3].offset, 2048);
85 }
86
TEST(SimpleMemoryArenaTest,TestAfterClear)87 TEST(SimpleMemoryArenaTest, TestAfterClear) {
88 TfLiteContext context;
89 SimpleMemoryArena arena(64);
90 ArenaAlloc allocs[9];
91
92 arena.Allocate(&context, 32, 2047, &allocs[0]);
93 arena.Allocate(&context, 32, 2047, &allocs[1]);
94 arena.Allocate(&context, 32, 2047, &allocs[2]);
95 arena.Commit(&context);
96
97 EXPECT_EQ(allocs[0].offset, 0);
98 EXPECT_EQ(allocs[1].offset, 2048);
99 EXPECT_EQ(allocs[2].offset, 4096);
100
101 arena.Clear();
102
103 // Test with smaller allocs.
104 arena.Allocate(&context, 32, 1023, &allocs[3]);
105 arena.Allocate(&context, 32, 1023, &allocs[4]);
106 arena.Allocate(&context, 32, 1023, &allocs[5]);
107 arena.Commit(&context);
108
109 EXPECT_EQ(allocs[3].offset, 0);
110 EXPECT_EQ(allocs[4].offset, 1024);
111 EXPECT_EQ(allocs[5].offset, 2048);
112
113 arena.Clear();
114
115 // Test larger allocs which should require a reallocation.
116 arena.Allocate(&context, 32, 4095, &allocs[6]);
117 arena.Allocate(&context, 32, 4095, &allocs[7]);
118 arena.Allocate(&context, 32, 4095, &allocs[8]);
119 arena.Commit(&context);
120
121 EXPECT_EQ(allocs[6].offset, 0);
122 EXPECT_EQ(allocs[7].offset, 4096);
123 EXPECT_EQ(allocs[8].offset, 8192);
124 }
125
126 } // namespace
127 } // namespace tflite
128
main(int argc,char ** argv)129 int main(int argc, char** argv) {
130 ::tflite::LogToStderr();
131 ::testing::InitGoogleTest(&argc, argv);
132 return RUN_ALL_TESTS();
133 }
134