1 //===- PerThreadBumpPtrAllocatorTest.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 #include "llvm/Support/PerThreadBumpPtrAllocator.h"
10 #include "llvm/Support/Parallel.h"
11 #include "gtest/gtest.h"
15 using namespace parallel
;
19 TEST(PerThreadBumpPtrAllocatorTest
, Simple
) {
20 PerThreadBumpPtrAllocator Allocator
;
22 parallel::TaskGroup tg
;
26 (uint64_t *)Allocator
.Allocate(sizeof(uint64_t), alignof(uint64_t));
28 EXPECT_EQ(0xFEul
, *Var
);
29 EXPECT_EQ(sizeof(uint64_t), Allocator
.getBytesAllocated());
30 EXPECT_TRUE(Allocator
.getBytesAllocated() <= Allocator
.getTotalMemory());
32 PerThreadBumpPtrAllocator
Allocator2(std::move(Allocator
));
34 EXPECT_EQ(sizeof(uint64_t), Allocator2
.getBytesAllocated());
35 EXPECT_TRUE(Allocator2
.getBytesAllocated() <= Allocator2
.getTotalMemory());
37 EXPECT_EQ(0xFEul
, *Var
);
41 TEST(PerThreadBumpPtrAllocatorTest
, ParallelAllocation
) {
42 PerThreadBumpPtrAllocator Allocator
;
44 static size_t constexpr NumAllocations
= 1000;
46 parallelFor(0, NumAllocations
, [&](size_t Idx
) {
48 (uint64_t *)Allocator
.Allocate(sizeof(uint64_t), alignof(uint64_t));
52 EXPECT_EQ(sizeof(uint64_t) * NumAllocations
, Allocator
.getBytesAllocated());
53 EXPECT_EQ(Allocator
.getNumberOfAllocators(), parallel::getThreadCount());
56 } // anonymous namespace