1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "gtest/gtest.h" 18 19 #include "chre/platform/log.h" 20 #include "chre/platform/memory.h" 21 #include "chre/platform/memory_manager.h" 22 23 using chre::MemoryManager; 24 using chre::Nanoapp; 25 26 namespace { 27 struct node { 28 node *next; 29 }; 30 } // namespace 31 32 TEST(MemoryManager, DefaultTotalMemoryAllocatedIsZero) { 33 MemoryManager manager; 34 EXPECT_EQ(manager.getTotalAllocatedBytes(), 0); 35 } 36 37 TEST(MemoryManager, BasicAllocationFree) { 38 MemoryManager manager; 39 Nanoapp app; 40 void *ptr = manager.nanoappAlloc(&app, 1); 41 EXPECT_NE(ptr, nullptr); 42 EXPECT_EQ(manager.getTotalAllocatedBytes(), 1); 43 EXPECT_EQ(manager.getAllocationCount(), 1); 44 manager.nanoappFree(&app, ptr); 45 EXPECT_EQ(manager.getTotalAllocatedBytes(), 0); 46 EXPECT_EQ(manager.getAllocationCount(), 0); 47 } 48 49 TEST(MemoryManager, NullPointerFree) { 50 MemoryManager manager; 51 Nanoapp app; 52 manager.nanoappFree(&app, nullptr); 53 EXPECT_EQ(manager.getTotalAllocatedBytes(), 0); 54 EXPECT_EQ(manager.getAllocationCount(), 0); 55 } 56 57 TEST(MemoryManager, ZeroAllocationFails) { 58 MemoryManager manager; 59 Nanoapp app; 60 void *ptr = manager.nanoappAlloc(&app, 0); 61 EXPECT_EQ(ptr, nullptr); 62 EXPECT_EQ(manager.getTotalAllocatedBytes(), 0); 63 EXPECT_EQ(manager.getAllocationCount(), 0); 64 } 65 66 TEST(MemoryManager, HugeAllocationFails) { 67 MemoryManager manager; 68 Nanoapp app; 69 void *ptr = manager.nanoappAlloc(&app, manager.getMaxAllocationBytes() + 1); 70 EXPECT_EQ(ptr, nullptr); 71 EXPECT_EQ(manager.getTotalAllocatedBytes(), 0); 72 } 73 74 TEST(MemoryManager, ManyAllocationsTest) { 75 MemoryManager manager; 76 Nanoapp app; 77 size_t maxCount = manager.getMaxAllocationCount(); 78 node *head = static_cast<node *>(manager.nanoappAlloc(&app, sizeof(node))); 79 node *curr = nullptr, *prev = head; 80 for (size_t i = 0; i < maxCount - 1; i++) { 81 curr = static_cast<node *>(manager.nanoappAlloc(&app, sizeof(node))); 82 EXPECT_NE(curr, nullptr); 83 prev->next = curr; 84 prev = curr; 85 } 86 EXPECT_EQ(manager.getTotalAllocatedBytes(), maxCount * sizeof(node)); 87 EXPECT_EQ(manager.getAllocationCount(), maxCount); 88 EXPECT_EQ(manager.nanoappAlloc(&app, 1), nullptr); 89 90 curr = head; 91 for (size_t i = 0; i < maxCount; i++) { 92 node *temp = curr->next; 93 manager.nanoappFree(&app, curr); 94 curr = temp; 95 } 96 EXPECT_EQ(manager.getTotalAllocatedBytes(), 0); 97 EXPECT_EQ(manager.getAllocationCount(), 0); 98 } 99 100 TEST(MemoryManager, NegativeAllocationFails) { 101 MemoryManager manager; 102 Nanoapp app; 103 void *ptr = manager.nanoappAlloc(&app, -1); 104 EXPECT_EQ(ptr, nullptr); 105 EXPECT_EQ(manager.getTotalAllocatedBytes(), 0); 106 EXPECT_EQ(manager.getAllocationCount(), 0); 107 } 108