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/core/event.h"
20 #include "chre/platform/log.h"
21 #include "chre/platform/memory.h"
22 #include "chre/platform/memory_manager.h"
23 
24 using chre::kInvalidInstanceId;
25 using chre::MemoryManager;
26 using chre::Nanoapp;
27 
28 namespace {
29 struct node {
30   node *next;
31 };
32 }  // namespace
33 
TEST(MemoryManager,DefaultTotalMemoryAllocatedIsZero)34 TEST(MemoryManager, DefaultTotalMemoryAllocatedIsZero) {
35   MemoryManager manager;
36   EXPECT_EQ(manager.getTotalAllocatedBytes(), 0u);
37 }
38 
TEST(MemoryManager,BasicAllocationFree)39 TEST(MemoryManager, BasicAllocationFree) {
40   MemoryManager manager;
41   Nanoapp app(kInvalidInstanceId);
42   void *ptr = manager.nanoappAlloc(&app, 1u);
43   EXPECT_NE(ptr, nullptr);
44   EXPECT_EQ(manager.getTotalAllocatedBytes(), 1u);
45   EXPECT_EQ(manager.getAllocationCount(), 1u);
46   manager.nanoappFree(&app, ptr);
47   EXPECT_EQ(manager.getTotalAllocatedBytes(), 0u);
48   EXPECT_EQ(manager.getAllocationCount(), 0u);
49 }
50 
TEST(MemoryManager,NullPointerFree)51 TEST(MemoryManager, NullPointerFree) {
52   MemoryManager manager;
53   Nanoapp app(kInvalidInstanceId);
54   manager.nanoappFree(&app, nullptr);
55   EXPECT_EQ(manager.getTotalAllocatedBytes(), 0u);
56   EXPECT_EQ(manager.getAllocationCount(), 0u);
57 }
58 
TEST(MemoryManager,ZeroAllocationFails)59 TEST(MemoryManager, ZeroAllocationFails) {
60   MemoryManager manager;
61   Nanoapp app(kInvalidInstanceId);
62   void *ptr = manager.nanoappAlloc(&app, 0u);
63   EXPECT_EQ(ptr, nullptr);
64   EXPECT_EQ(manager.getTotalAllocatedBytes(), 0u);
65   EXPECT_EQ(manager.getAllocationCount(), 0u);
66 }
67 
TEST(MemoryManager,HugeAllocationFails)68 TEST(MemoryManager, HugeAllocationFails) {
69   MemoryManager manager;
70   Nanoapp app(kInvalidInstanceId);
71   void *ptr = manager.nanoappAlloc(&app, manager.getMaxAllocationBytes() + 1);
72   EXPECT_EQ(ptr, nullptr);
73   EXPECT_EQ(manager.getTotalAllocatedBytes(), 0u);
74 }
75 
TEST(MemoryManager,ManyAllocationsTest)76 TEST(MemoryManager, ManyAllocationsTest) {
77   MemoryManager manager;
78   Nanoapp app(kInvalidInstanceId);
79   size_t maxCount = manager.getMaxAllocationCount();
80   node *head = static_cast<node *>(manager.nanoappAlloc(&app, sizeof(node)));
81   node *curr = nullptr, *prev = head;
82   for (size_t i = 0; i < maxCount - 1; i++) {
83     curr = static_cast<node *>(manager.nanoappAlloc(&app, sizeof(node)));
84     EXPECT_NE(curr, nullptr);
85     prev->next = curr;
86     prev = curr;
87   }
88   EXPECT_EQ(manager.getTotalAllocatedBytes(), maxCount * sizeof(node));
89   EXPECT_EQ(manager.getAllocationCount(), maxCount);
90   EXPECT_EQ(manager.nanoappAlloc(&app, 1u), nullptr);
91 
92   curr = head;
93   for (size_t i = 0; i < maxCount; i++) {
94     node *temp = curr->next;
95     manager.nanoappFree(&app, curr);
96     curr = temp;
97   }
98   EXPECT_EQ(manager.getTotalAllocatedBytes(), 0u);
99   EXPECT_EQ(manager.getAllocationCount(), 0u);
100 }
101 
TEST(MemoryManager,NegativeAllocationFails)102 TEST(MemoryManager, NegativeAllocationFails) {
103   MemoryManager manager;
104   Nanoapp app(kInvalidInstanceId);
105   void *ptr = manager.nanoappAlloc(&app, -1u);
106   EXPECT_EQ(ptr, nullptr);
107   EXPECT_EQ(manager.getTotalAllocatedBytes(), 0u);
108   EXPECT_EQ(manager.getAllocationCount(), 0u);
109 }
110