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