1 //===-- allocator_test.cpp ------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is a part of XRay, a function call tracing system.
11 //===----------------------------------------------------------------------===//
13 #include "xray_allocator.h"
14 #include "xray_buffer_queue.h"
15 #include "gtest/gtest.h"
25 TEST(AllocatorTest
, Construction
) { Allocator
<sizeof(TestData
)> A(2 << 11); }
27 TEST(AllocatorTest
, Allocate
) {
28 Allocator
<sizeof(TestData
)> A(2 << 11);
29 auto B
= A
.Allocate();
30 ASSERT_NE(B
.Data
, nullptr);
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);
46 TEST(AllocatorTest
, AllocateBoundaries
) {
47 Allocator
<sizeof(OddSizedData
)> A(GetPageSizeCached());
49 // Keep allocating until we hit a nullptr block.
52 GetPageSizeCached() / RoundUpTo(sizeof(OddSizedData
), kCacheLineSize
);
53 for (auto B
= A
.Allocate(); B
.Data
!= nullptr; B
= A
.Allocate(), ++C
)
56 ASSERT_EQ(C
, Expected
);
59 TEST(AllocatorTest
, AllocateFromNonOwned
) {
61 BufferQueue
BQ(GetPageSizeCached(), 10, Success
);
63 BufferQueue::Buffer B
;
64 ASSERT_EQ(BQ
.getBuffer(B
), BufferQueue::ErrorCode::Ok
);
66 Allocator
<sizeof(OddSizedData
)> A(B
.Data
, B
.Size
);
68 // Keep allocating until we hit a nullptr block.
71 GetPageSizeCached() / RoundUpTo(sizeof(OddSizedData
), kCacheLineSize
);
72 for (auto B
= A
.Allocate(); B
.Data
!= nullptr; B
= A
.Allocate(), ++C
)
75 ASSERT_EQ(C
, Expected
);
77 ASSERT_EQ(BQ
.releaseBuffer(B
), BufferQueue::ErrorCode::Ok
);