1 | //===- PerThreadBumpPtrAllocatorTest.cpp ----------------------------------===// |
2 | // |
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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "llvm/Support/PerThreadBumpPtrAllocator.h" |
10 | #include "llvm/Support/Parallel.h" |
11 | #include "gtest/gtest.h" |
12 | #include <cstdlib> |
13 | |
14 | using namespace llvm; |
15 | using namespace parallel; |
16 | |
17 | namespace { |
18 | |
19 | TEST(PerThreadBumpPtrAllocatorTest, Simple) { |
20 | PerThreadBumpPtrAllocator Allocator; |
21 | |
22 | parallel::TaskGroup tg; |
23 | |
24 | tg.spawn(f: [&]() { |
25 | uint64_t *Var = |
26 | (uint64_t *)Allocator.Allocate(Size: sizeof(uint64_t), Alignment: alignof(uint64_t)); |
27 | *Var = 0xFE; |
28 | EXPECT_EQ(0xFEul, *Var); |
29 | EXPECT_EQ(sizeof(uint64_t), Allocator.getBytesAllocated()); |
30 | EXPECT_TRUE(Allocator.getBytesAllocated() <= Allocator.getTotalMemory()); |
31 | |
32 | PerThreadBumpPtrAllocator Allocator2(std::move(Allocator)); |
33 | |
34 | EXPECT_EQ(sizeof(uint64_t), Allocator2.getBytesAllocated()); |
35 | EXPECT_TRUE(Allocator2.getBytesAllocated() <= Allocator2.getTotalMemory()); |
36 | |
37 | EXPECT_EQ(0xFEul, *Var); |
38 | }); |
39 | } |
40 | |
41 | TEST(PerThreadBumpPtrAllocatorTest, ParallelAllocation) { |
42 | PerThreadBumpPtrAllocator Allocator; |
43 | |
44 | static size_t constexpr NumAllocations = 1000; |
45 | |
46 | parallelFor(Begin: 0, End: NumAllocations, Fn: [&](size_t Idx) { |
47 | uint64_t *ptr = |
48 | (uint64_t *)Allocator.Allocate(Size: sizeof(uint64_t), Alignment: alignof(uint64_t)); |
49 | *ptr = Idx; |
50 | }); |
51 | |
52 | EXPECT_EQ(sizeof(uint64_t) * NumAllocations, Allocator.getBytesAllocated()); |
53 | EXPECT_EQ(Allocator.getNumberOfAllocators(), parallel::getThreadCount()); |
54 | } |
55 | |
56 | } // anonymous namespace |
57 | |