1 | //===-- allocator_test.cpp ------------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file is a part of XRay, a function call tracing system. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #include "xray_allocator.h" |
14 | #include "xray_buffer_queue.h" |
15 | #include "gtest/gtest.h" |
16 | |
17 | namespace __xray { |
18 | namespace { |
19 | |
20 | struct TestData { |
21 | s64 First; |
22 | s64 Second; |
23 | }; |
24 | |
25 | TEST(AllocatorTest, Construction) { Allocator<sizeof(TestData)> A(2 << 11); } |
26 | |
27 | TEST(AllocatorTest, Allocate) { |
28 | Allocator<sizeof(TestData)> A(2 << 11); |
29 | auto B = A.Allocate(); |
30 | ASSERT_NE(B.Data, nullptr); |
31 | } |
32 | |
33 | TEST(AllocatorTest, OverAllocate) { |
34 | Allocator<sizeof(TestData)> A(sizeof(TestData)); |
35 | auto B1 = A.Allocate(); |
36 | ASSERT_NE(B1.Data, nullptr); |
37 | auto B2 = A.Allocate(); |
38 | ASSERT_EQ(B2.Data, nullptr); |
39 | } |
40 | |
41 | struct OddSizedData { |
42 | s64 A; |
43 | s32 B; |
44 | }; |
45 | |
46 | TEST(AllocatorTest, AllocateBoundaries) { |
47 | Allocator<sizeof(OddSizedData)> A(GetPageSizeCached()); |
48 | |
49 | // Keep allocating until we hit a nullptr block. |
50 | unsigned C = 0; |
51 | auto Expected = |
52 | GetPageSizeCached() / RoundUpTo(size: sizeof(OddSizedData), boundary: kCacheLineSize); |
53 | for (auto B = A.Allocate(); B.Data != nullptr; B = A.Allocate(), ++C) |
54 | ; |
55 | |
56 | ASSERT_EQ(C, Expected); |
57 | } |
58 | |
59 | TEST(AllocatorTest, AllocateFromNonOwned) { |
60 | bool Success = false; |
61 | BufferQueue BQ(GetPageSizeCached(), 10, Success); |
62 | ASSERT_TRUE(Success); |
63 | BufferQueue::Buffer B; |
64 | ASSERT_EQ(BQ.getBuffer(Buf&: B), BufferQueue::ErrorCode::Ok); |
65 | { |
66 | Allocator<sizeof(OddSizedData)> A(B.Data, B.Size); |
67 | |
68 | // Keep allocating until we hit a nullptr block. |
69 | unsigned C = 0; |
70 | auto Expected = |
71 | GetPageSizeCached() / RoundUpTo(size: sizeof(OddSizedData), boundary: kCacheLineSize); |
72 | for (auto B = A.Allocate(); B.Data != nullptr; B = A.Allocate(), ++C) |
73 | ; |
74 | |
75 | ASSERT_EQ(C, Expected); |
76 | } |
77 | ASSERT_EQ(BQ.releaseBuffer(Buf&: B), BufferQueue::ErrorCode::Ok); |
78 | } |
79 | |
80 | } // namespace |
81 | } // namespace __xray |
82 | |