brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.5 KiB · 005c67b Raw
79 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "llvm/ExecutionEngine/Orc/TargetProcess/SimpleExecutorMemoryManager.h"10#include "llvm/Testing/Support/Error.h"11#include "gtest/gtest.h"12 13#include <limits>14 15using namespace llvm;16using namespace llvm::orc;17using namespace llvm::orc::shared;18using namespace llvm::orc::rt_bootstrap;19 20namespace {21 22CWrapperFunctionResult incrementWrapper(const char *ArgData, size_t ArgSize) {23  return WrapperFunction<SPSError(SPSExecutorAddr)>::handle(24             ArgData, ArgSize,25             [](ExecutorAddr A) -> Error {26               *A.toPtr<int *>() += 1;27               return Error::success();28             })29      .release();30}31 32TEST(SimpleExecutorMemoryManagerTest, AllocFinalizeFree) {33  SimpleExecutorMemoryManager MemMgr;34 35  constexpr unsigned AllocSize = 16384;36  auto Mem = MemMgr.reserve(AllocSize);37  EXPECT_THAT_ERROR(Mem.takeError(), Succeeded());38 39  std::string HW = "Hello, world!";40 41  int InitializeCounter = 0;42  int DeallocateCounter = 0;43 44  tpctypes::FinalizeRequest FR;45  FR.Segments.push_back(46      tpctypes::SegFinalizeRequest{MemProt::Read | MemProt::Write,47                                   *Mem,48                                   AllocSize,49                                   {HW.data(), HW.size() + 1}});50  FR.Actions.push_back(51      {/* Finalize: */52       cantFail(WrapperFunctionCall::Create<SPSArgList<SPSExecutorAddr>>(53           ExecutorAddr::fromPtr(incrementWrapper),54           ExecutorAddr::fromPtr(&InitializeCounter))),55       /*  Deallocate: */56       cantFail(WrapperFunctionCall::Create<SPSArgList<SPSExecutorAddr>>(57           ExecutorAddr::fromPtr(incrementWrapper),58           ExecutorAddr::fromPtr(&DeallocateCounter)))});59 60  EXPECT_EQ(InitializeCounter, 0);61  EXPECT_EQ(DeallocateCounter, 0);62 63  auto InitializeErr = MemMgr.initialize(FR);64  EXPECT_THAT_EXPECTED(std::move(InitializeErr), Succeeded());65 66  EXPECT_EQ(InitializeCounter, 1);67  EXPECT_EQ(DeallocateCounter, 0);68 69  EXPECT_EQ(HW, std::string(Mem->toPtr<const char *>()));70 71  auto ReleaseErr = MemMgr.release({*Mem});72  EXPECT_THAT_ERROR(std::move(ReleaseErr), Succeeded());73 74  EXPECT_EQ(InitializeCounter, 1);75  EXPECT_EQ(DeallocateCounter, 1);76}77 78} // namespace79