[docs] Fix build-docs.sh
[llvm-project.git] / llvm / unittests / ExecutionEngine / Orc / SimpleExecutorMemoryManagerTest.cpp
blobf127e7713c5eb14ac4a9cd5d645268f196b04ee8
1 //===---------------- SimpleExecutorMemoryManagerTest.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/ExecutionEngine/Orc/TargetProcess/SimpleExecutorMemoryManager.h"
10 #include "llvm/Testing/Support/Error.h"
11 #include "gtest/gtest.h"
13 #include <limits>
14 #include <vector>
16 using namespace llvm;
17 using namespace llvm::orc;
18 using namespace llvm::orc::shared;
19 using namespace llvm::orc::rt_bootstrap;
21 namespace {
23 orc::shared::CWrapperFunctionResult incrementWrapper(const char *ArgData,
24 size_t ArgSize) {
25 return WrapperFunction<SPSError(SPSExecutorAddr)>::handle(
26 ArgData, ArgSize,
27 [](ExecutorAddr A) -> Error {
28 *A.toPtr<int *>() += 1;
29 return Error::success();
31 .release();
34 TEST(SimpleExecutorMemoryManagerTest, AllocFinalizeFree) {
35 SimpleExecutorMemoryManager MemMgr;
37 constexpr unsigned AllocSize = 16384;
38 auto Mem = MemMgr.allocate(AllocSize);
39 EXPECT_THAT_ERROR(Mem.takeError(), Succeeded());
41 std::string HW = "Hello, world!";
43 int FinalizeCounter = 0;
44 int DeallocateCounter = 0;
46 tpctypes::FinalizeRequest FR;
47 FR.Segments.push_back(
48 tpctypes::SegFinalizeRequest{tpctypes::WPF_Read | tpctypes::WPF_Write,
49 *Mem,
50 AllocSize,
51 {HW.data(), HW.size() + 1}});
52 FR.Actions.push_back(
53 {/* Finalize: */
54 cantFail(WrapperFunctionCall::Create<SPSArgList<SPSExecutorAddr>>(
55 ExecutorAddr::fromPtr(incrementWrapper),
56 ExecutorAddr::fromPtr(&FinalizeCounter))),
57 /* Deallocate: */
58 cantFail(WrapperFunctionCall::Create<SPSArgList<SPSExecutorAddr>>(
59 ExecutorAddr::fromPtr(incrementWrapper),
60 ExecutorAddr::fromPtr(&DeallocateCounter)))});
62 EXPECT_EQ(FinalizeCounter, 0);
63 EXPECT_EQ(DeallocateCounter, 0);
65 auto FinalizeErr = MemMgr.finalize(FR);
66 EXPECT_THAT_ERROR(std::move(FinalizeErr), Succeeded());
68 EXPECT_EQ(FinalizeCounter, 1);
69 EXPECT_EQ(DeallocateCounter, 0);
71 EXPECT_EQ(HW, std::string(Mem->toPtr<const char *>()));
73 auto DeallocateErr = MemMgr.deallocate({*Mem});
74 EXPECT_THAT_ERROR(std::move(DeallocateErr), Succeeded());
76 EXPECT_EQ(FinalizeCounter, 1);
77 EXPECT_EQ(DeallocateCounter, 1);
80 } // namespace