1 /* Copyright 2015 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 16 #include "tensorflow/core/lib/core/arena.h" 17 18 #include "tensorflow/core/platform/macros.h" 19 #include "tensorflow/core/platform/test.h" 20 21 namespace tensorflow { 22 namespace core { 23 namespace { 24 25 // Write random data to allocated memory 26 static void TestMemory(void* mem, int size) { 27 // Check that we can memset the entire memory 28 memset(mem, 0xaa, size); 29 30 // Do some memory allocation to check that the arena doesn't mess up 31 // the internal memory allocator 32 char* tmp[100]; 33 for (size_t i = 0; i < TF_ARRAYSIZE(tmp); i++) { 34 tmp[i] = new char[i * i + 1]; 35 } 36 37 memset(mem, 0xcc, size); 38 39 // Free up the allocated memory; 40 for (size_t i = 0; i < TF_ARRAYSIZE(tmp); i++) { 41 delete[] tmp[i]; 42 } 43 44 // Check that we can memset the entire memory 45 memset(mem, 0xee, size); 46 } 47 48 TEST(ArenaTest, TestBasicArena) { 49 Arena a(1024); 50 char* memory = a.Alloc(100); 51 ASSERT_NE(memory, nullptr); 52 TestMemory(memory, 100); 53 54 // Allocate again 55 memory = a.Alloc(100); 56 ASSERT_NE(memory, nullptr); 57 TestMemory(memory, 100); 58 } 59 60 TEST(ArenaTest, TestAlignment) { 61 Arena a(1024); 62 char* byte0 = a.Alloc(1); 63 char* alloc_aligned8 = a.AllocAligned(17, 8); 64 EXPECT_EQ(alloc_aligned8 - byte0, 8); 65 char* alloc_aligned8_b = a.AllocAligned(8, 8); 66 EXPECT_EQ(alloc_aligned8_b - alloc_aligned8, 24); 67 char* alloc_aligned8_c = a.AllocAligned(16, 8); 68 EXPECT_EQ(alloc_aligned8_c - alloc_aligned8_b, 8); 69 char* alloc_aligned8_d = a.AllocAligned(8, 1); 70 EXPECT_EQ(alloc_aligned8_d - alloc_aligned8_c, 16); 71 } 72 73 TEST(ArenaTest, TestVariousArenaSizes) { 74 { 75 Arena a(1024); 76 77 // Allocate blocksize 78 char* memory = a.Alloc(1024); 79 ASSERT_NE(memory, nullptr); 80 TestMemory(memory, 1024); 81 82 // Allocate another blocksize 83 char* memory2 = a.Alloc(1024); 84 ASSERT_NE(memory2, nullptr); 85 TestMemory(memory2, 1024); 86 } 87 88 // Allocate an arena and allocate two blocks 89 // that together exceed a block size 90 { 91 Arena a(1024); 92 93 // 94 char* memory = a.Alloc(768); 95 ASSERT_NE(memory, nullptr); 96 TestMemory(memory, 768); 97 98 // Allocate another blocksize 99 char* memory2 = a.Alloc(768); 100 ASSERT_NE(memory2, nullptr); 101 TestMemory(memory2, 768); 102 } 103 104 // Allocate larger than a blocksize 105 { 106 Arena a(1024); 107 108 char* memory = a.Alloc(10240); 109 ASSERT_NE(memory, nullptr); 110 TestMemory(memory, 10240); 111 112 // Allocate another blocksize 113 char* memory2 = a.Alloc(1234); 114 ASSERT_NE(memory2, nullptr); 115 TestMemory(memory2, 1234); 116 } 117 } 118 119 } // namespace 120 } // namespace core 121 } // namespace tensorflow 122