1 /*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9 #include <executorch/runtime/executor/memory_manager.h>
10
11 #include <executorch/runtime/core/memory_allocator.h>
12
13 #include <executorch/test/utils/DeathTest.h>
14 #include <gtest/gtest.h>
15
16 using namespace ::testing;
17 using executorch::runtime::HierarchicalAllocator;
18 using executorch::runtime::MemoryAllocator;
19 using executorch::runtime::MemoryManager;
20
TEST(MemoryManagerTest,MinimalCtor)21 TEST(MemoryManagerTest, MinimalCtor) {
22 MemoryAllocator method_allocator(0, nullptr);
23
24 MemoryManager mm(&method_allocator);
25
26 EXPECT_EQ(mm.method_allocator(), &method_allocator);
27 EXPECT_EQ(mm.planned_memory(), nullptr);
28 EXPECT_EQ(mm.temp_allocator(), nullptr);
29 }
30
TEST(MemoryManagerTest,CtorWithPlannedMemory)31 TEST(MemoryManagerTest, CtorWithPlannedMemory) {
32 MemoryAllocator method_allocator(0, nullptr);
33 HierarchicalAllocator planned_memory({});
34
35 MemoryManager mm(&method_allocator, &planned_memory);
36
37 EXPECT_EQ(mm.method_allocator(), &method_allocator);
38 EXPECT_EQ(mm.planned_memory(), &planned_memory);
39 EXPECT_EQ(mm.temp_allocator(), nullptr);
40 }
41
TEST(MemoryManagerTest,CtorWithPlannedMemoryAndTemp)42 TEST(MemoryManagerTest, CtorWithPlannedMemoryAndTemp) {
43 MemoryAllocator method_allocator(0, nullptr);
44 HierarchicalAllocator planned_memory({});
45 MemoryAllocator temp_allocator(0, nullptr);
46
47 MemoryManager mm(&method_allocator, &planned_memory, &temp_allocator);
48
49 EXPECT_EQ(mm.method_allocator(), &method_allocator);
50 EXPECT_EQ(mm.planned_memory(), &planned_memory);
51 EXPECT_EQ(mm.temp_allocator(), &temp_allocator);
52 }
53
TEST(MemoryManagerTest,DEPRECATEDCtor)54 TEST(MemoryManagerTest, DEPRECATEDCtor) {
55 MemoryAllocator method_allocator(0, nullptr);
56 HierarchicalAllocator planned_memory({});
57 MemoryAllocator temp_allocator(0, nullptr);
58 MemoryAllocator const_allocator(0, nullptr);
59
60 // NOLINTNEXTLINE(facebook-hte-Deprecated)
61 MemoryManager mm(
62 /*constant_allocator=*/&const_allocator,
63 /*non_constant_allocator=*/&planned_memory,
64 /*runtime_allocator=*/&method_allocator,
65 /*temporary_allocator=*/&temp_allocator);
66
67 EXPECT_EQ(mm.method_allocator(), &method_allocator);
68 EXPECT_EQ(mm.planned_memory(), &planned_memory);
69 EXPECT_EQ(mm.temp_allocator(), &temp_allocator);
70 }
71
TEST(MemoryManagerTest,DeprecatedCtorWithSameAllocator)72 TEST(MemoryManagerTest, DeprecatedCtorWithSameAllocator) {
73 MemoryAllocator method_allocator(0, nullptr);
74 HierarchicalAllocator planned_memory({});
75 MemoryAllocator const_allocator(0, nullptr);
76 ET_EXPECT_DEATH(
77 MemoryManager(
78 /*constant_allocator=*/&const_allocator,
79 /*non_constant_allocator=*/&planned_memory,
80 /*runtime_allocator=*/&method_allocator,
81 /*temp_allocator=*/&method_allocator),
82 "");
83 }
84
TEST(MemoryManagerTest,CtorWithSameAllocator)85 TEST(MemoryManagerTest, CtorWithSameAllocator) {
86 MemoryAllocator method_allocator(0, nullptr);
87 HierarchicalAllocator planned_memory({});
88 MemoryAllocator const_allocator(0, nullptr);
89 ET_EXPECT_DEATH(
90 MemoryManager(
91 /*runtime_allocator=*/&method_allocator,
92 /*non_constant_allocator=*/&planned_memory,
93 /*temp_allocator=*/&method_allocator),
94 "");
95 }
96