1 // Copyright 2019 The Marl Authors.
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 // https://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 "marl/memory.h"
16
17 #include "marl_test.h"
18
19 class AllocatorTest : public testing::Test {
20 public:
21 marl::Allocator* allocator = marl::Allocator::Default;
22 };
23
TEST_F(AllocatorTest,AlignedAllocate)24 TEST_F(AllocatorTest, AlignedAllocate) {
25 std::vector<bool> guards = {false, true};
26 std::vector<size_t> sizes = {1, 2, 3, 4, 5, 7, 8, 14, 16, 17,
27 31, 34, 50, 63, 64, 65, 100, 127, 128, 129,
28 200, 255, 256, 257, 500, 511, 512, 513};
29 std::vector<size_t> alignments = {1, 2, 4, 8, 16, 32, 64, 128};
30 for (auto useGuards : guards) {
31 for (auto alignment : alignments) {
32 for (auto size : sizes) {
33 marl::Allocation::Request request;
34 request.alignment = alignment;
35 request.size = size;
36 request.useGuards = useGuards;
37
38 auto allocation = allocator->allocate(request);
39 auto ptr = allocation.ptr;
40 ASSERT_EQ(allocation.request.size, request.size);
41 ASSERT_EQ(allocation.request.alignment, request.alignment);
42 ASSERT_EQ(allocation.request.useGuards, request.useGuards);
43 ASSERT_EQ(allocation.request.usage, request.usage);
44 ASSERT_EQ(reinterpret_cast<uintptr_t>(ptr) & (alignment - 1), 0U);
45 memset(ptr, 0,
46 size); // Check the memory was actually allocated.
47 allocator->free(allocation);
48 }
49 }
50 }
51 }
52
53 struct alignas(16) StructWith16ByteAlignment {
54 uint8_t i;
55 uint8_t padding[15];
56 };
57 struct alignas(32) StructWith32ByteAlignment {
58 uint8_t i;
59 uint8_t padding[31];
60 };
61 struct alignas(64) StructWith64ByteAlignment {
62 uint8_t i;
63 uint8_t padding[63];
64 };
65
TEST_F(AllocatorTest,Create)66 TEST_F(AllocatorTest, Create) {
67 auto s16 = allocator->create<StructWith16ByteAlignment>();
68 auto s32 = allocator->create<StructWith32ByteAlignment>();
69 auto s64 = allocator->create<StructWith64ByteAlignment>();
70 ASSERT_EQ(alignof(StructWith16ByteAlignment), 16U);
71 ASSERT_EQ(alignof(StructWith32ByteAlignment), 32U);
72 ASSERT_EQ(alignof(StructWith64ByteAlignment), 64U);
73 ASSERT_EQ(reinterpret_cast<uintptr_t>(s16) & 15U, 0U);
74 ASSERT_EQ(reinterpret_cast<uintptr_t>(s32) & 31U, 0U);
75 ASSERT_EQ(reinterpret_cast<uintptr_t>(s64) & 63U, 0U);
76 allocator->destroy(s64);
77 allocator->destroy(s32);
78 allocator->destroy(s16);
79 }
80
TEST_F(AllocatorTest,Guards)81 TEST_F(AllocatorTest, Guards) {
82 marl::Allocation::Request request;
83 request.alignment = 16;
84 request.size = 16;
85 request.useGuards = true;
86 auto alloc = allocator->allocate(request);
87 auto ptr = reinterpret_cast<uint8_t*>(alloc.ptr);
88 EXPECT_DEATH(ptr[-1] = 1, "");
89 EXPECT_DEATH(ptr[marl::pageSize()] = 1, "");
90 }
91