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