Bump version to 19.1.0-rc3
[llvm-project.git] / llvm / unittests / Support / PerThreadBumpPtrAllocatorTest.cpp
blobd30de997f0fd17fea07395f755ffd1969e42e157
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 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/PerThreadBumpPtrAllocator.h"
10 #include "llvm/Support/Parallel.h"
11 #include "gtest/gtest.h"
12 #include <cstdlib>
14 using namespace llvm;
15 using namespace parallel;
17 namespace {
19 TEST(PerThreadBumpPtrAllocatorTest, Simple) {
20 PerThreadBumpPtrAllocator Allocator;
22 parallel::TaskGroup tg;
24 tg.spawn([&]() {
25 uint64_t *Var =
26 (uint64_t *)Allocator.Allocate(sizeof(uint64_t), 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());
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);
38 });
41 TEST(PerThreadBumpPtrAllocatorTest, ParallelAllocation) {
42 PerThreadBumpPtrAllocator Allocator;
44 static size_t constexpr NumAllocations = 1000;
46 parallelFor(0, NumAllocations, [&](size_t Idx) {
47 uint64_t *ptr =
48 (uint64_t *)Allocator.Allocate(sizeof(uint64_t), alignof(uint64_t));
49 *ptr = Idx;
50 });
52 EXPECT_EQ(sizeof(uint64_t) * NumAllocations, Allocator.getBytesAllocated());
53 EXPECT_EQ(Allocator.getNumberOfAllocators(), parallel::getThreadCount());
56 } // anonymous namespace