1*6ab43f9bSAlexey Lapshin //===- PerThreadBumpPtrAllocatorTest.cpp ----------------------------------===//
2*6ab43f9bSAlexey Lapshin //
3*6ab43f9bSAlexey Lapshin // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*6ab43f9bSAlexey Lapshin // See https://llvm.org/LICENSE.txt for license information.
5*6ab43f9bSAlexey Lapshin // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*6ab43f9bSAlexey Lapshin //
7*6ab43f9bSAlexey Lapshin //===----------------------------------------------------------------------===//
8*6ab43f9bSAlexey Lapshin
9*6ab43f9bSAlexey Lapshin #include "llvm/Support/PerThreadBumpPtrAllocator.h"
10*6ab43f9bSAlexey Lapshin #include "llvm/Support/Parallel.h"
11*6ab43f9bSAlexey Lapshin #include "gtest/gtest.h"
12*6ab43f9bSAlexey Lapshin #include <cstdlib>
13*6ab43f9bSAlexey Lapshin
14*6ab43f9bSAlexey Lapshin using namespace llvm;
15*6ab43f9bSAlexey Lapshin using namespace parallel;
16*6ab43f9bSAlexey Lapshin
17*6ab43f9bSAlexey Lapshin namespace {
18*6ab43f9bSAlexey Lapshin
TEST(PerThreadBumpPtrAllocatorTest,Simple)19*6ab43f9bSAlexey Lapshin TEST(PerThreadBumpPtrAllocatorTest, Simple) {
20*6ab43f9bSAlexey Lapshin PerThreadBumpPtrAllocator Allocator;
21*6ab43f9bSAlexey Lapshin
22*6ab43f9bSAlexey Lapshin parallel::TaskGroup tg;
23*6ab43f9bSAlexey Lapshin
24*6ab43f9bSAlexey Lapshin tg.spawn([&]() {
25*6ab43f9bSAlexey Lapshin uint64_t *Var =
26*6ab43f9bSAlexey Lapshin (uint64_t *)Allocator.Allocate(sizeof(uint64_t), alignof(uint64_t));
27*6ab43f9bSAlexey Lapshin *Var = 0xFE;
28*6ab43f9bSAlexey Lapshin EXPECT_EQ(0xFEul, *Var);
29*6ab43f9bSAlexey Lapshin EXPECT_EQ(sizeof(uint64_t), Allocator.getBytesAllocated());
30*6ab43f9bSAlexey Lapshin EXPECT_TRUE(Allocator.getBytesAllocated() <= Allocator.getTotalMemory());
31*6ab43f9bSAlexey Lapshin
32*6ab43f9bSAlexey Lapshin PerThreadBumpPtrAllocator Allocator2(std::move(Allocator));
33*6ab43f9bSAlexey Lapshin
34*6ab43f9bSAlexey Lapshin EXPECT_EQ(sizeof(uint64_t), Allocator2.getBytesAllocated());
35*6ab43f9bSAlexey Lapshin EXPECT_TRUE(Allocator2.getBytesAllocated() <= Allocator2.getTotalMemory());
36*6ab43f9bSAlexey Lapshin
37*6ab43f9bSAlexey Lapshin EXPECT_EQ(0xFEul, *Var);
38*6ab43f9bSAlexey Lapshin });
39*6ab43f9bSAlexey Lapshin }
40*6ab43f9bSAlexey Lapshin
TEST(PerThreadBumpPtrAllocatorTest,ParallelAllocation)41*6ab43f9bSAlexey Lapshin TEST(PerThreadBumpPtrAllocatorTest, ParallelAllocation) {
42*6ab43f9bSAlexey Lapshin PerThreadBumpPtrAllocator Allocator;
43*6ab43f9bSAlexey Lapshin
44*6ab43f9bSAlexey Lapshin static size_t constexpr NumAllocations = 1000;
45*6ab43f9bSAlexey Lapshin
46*6ab43f9bSAlexey Lapshin parallelFor(0, NumAllocations, [&](size_t Idx) {
47*6ab43f9bSAlexey Lapshin uint64_t *ptr =
48*6ab43f9bSAlexey Lapshin (uint64_t *)Allocator.Allocate(sizeof(uint64_t), alignof(uint64_t));
49*6ab43f9bSAlexey Lapshin *ptr = Idx;
50*6ab43f9bSAlexey Lapshin });
51*6ab43f9bSAlexey Lapshin
52*6ab43f9bSAlexey Lapshin EXPECT_EQ(sizeof(uint64_t) * NumAllocations, Allocator.getBytesAllocated());
53*6ab43f9bSAlexey Lapshin EXPECT_EQ(Allocator.getNumberOfAllocators(), parallel::getThreadCount());
54*6ab43f9bSAlexey Lapshin }
55*6ab43f9bSAlexey Lapshin
56*6ab43f9bSAlexey Lapshin } // anonymous namespace
57