PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
SectionMemoryManager.h
Go to the documentation of this file.
1/*
2 * This is a copy LLVM source code modified by the PostgreSQL project.
3 * See SectionMemoryManager.cpp for notes on provenance and license.
4 */
5
6//===- SectionMemoryManager.h - Memory manager for MCJIT/RtDyld -*- C++ -*-===//
7//
8// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
9// See https://llvm.org/LICENSE.txt for license information.
10// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
11//
12//===----------------------------------------------------------------------===//
13//
14// This file contains the declaration of a section-based memory manager used by
15// the MCJIT execution engine and RuntimeDyld.
16//
17//===----------------------------------------------------------------------===//
18
19#ifndef LLVM_EXECUTIONENGINE_BACKPORT_SECTIONMEMORYMANAGER_H
20#define LLVM_EXECUTIONENGINE_BACKPORT_SECTIONMEMORYMANAGER_H
21
22#include "llvm/ADT/SmallVector.h"
23#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
24#include "llvm/Support/Alignment.h"
25#include "llvm/Support/Memory.h"
26#include <cstdint>
27#include <string>
28#include <system_error>
29
30namespace llvm {
31namespace backport {
32
46class SectionMemoryManager : public RTDyldMemoryManager {
47public:
50 enum class AllocationPurpose {
51 Code,
52 ROData,
53 RWData,
54 };
55
59 public:
79 virtual sys::MemoryBlock
80 allocateMappedMemory(AllocationPurpose Purpose, size_t NumBytes,
81 const sys::MemoryBlock *const NearBlock,
82 unsigned Flags, std::error_code &EC) = 0;
83
96 virtual std::error_code protectMappedMemory(const sys::MemoryBlock &Block,
97 unsigned Flags) = 0;
98
106 virtual std::error_code releaseMappedMemory(sys::MemoryBlock &M) = 0;
107
108 virtual ~MemoryMapper();
109 };
110
117 SectionMemoryManager(MemoryMapper *MM = nullptr, bool ReserveAlloc = false);
119 void operator=(const SectionMemoryManager &) = delete;
121
124
128#if LLVM_VERSION_MAJOR < 16
129 virtual void reserveAllocationSpace(uintptr_t CodeSize, uint32_t CodeAlign,
130 uintptr_t RODataSize,
131 uint32_t RODataAlign,
132 uintptr_t RWDataSize,
133 uint32_t RWDataAlign) override;
134#else
135 void reserveAllocationSpace(uintptr_t CodeSize, Align CodeAlign,
136 uintptr_t RODataSize, Align RODataAlign,
137 uintptr_t RWDataSize, Align RWDataAlign) override;
138#endif
139
145 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
146 unsigned SectionID,
147 StringRef SectionName) override;
148
154 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
155 unsigned SectionID, StringRef SectionName,
156 bool IsReadOnly) override;
157
169 bool finalizeMemory(std::string *ErrMsg = nullptr) override;
170
179
180private:
182 // The actual block of free memory
183 sys::MemoryBlock Free;
184 // If there is a pending allocation from the same reservation right before
185 // this block, store its index in PendingMem, to be able to update the
186 // pending region if part of this block is allocated, rather than having to
187 // create a new one
189 };
190
191 struct MemoryGroup {
192 // PendingMem contains all blocks of memory (subblocks of AllocatedMem)
193 // which have not yet had their permissions applied, but have been given
194 // out to the user. FreeMem contains all block of memory, which have
195 // neither had their permissions applied, nor been given out to the user.
196 SmallVector<sys::MemoryBlock, 16> PendingMem;
197 SmallVector<FreeMemBlock, 16> FreeMem;
198
199 // All memory blocks that have been requested from the system
200 SmallVector<sys::MemoryBlock, 16> AllocatedMem;
201
202 sys::MemoryBlock Near;
203 };
204
205 uint8_t *allocateSection(AllocationPurpose Purpose, uintptr_t Size,
206 unsigned Alignment);
207
208 std::error_code applyMemoryGroupPermissions(MemoryGroup &MemGroup,
209 unsigned Permissions);
210
211 bool hasSpace(const MemoryGroup &MemGroup, uintptr_t Size) const;
212
213 void anchor() override;
214
219 std::unique_ptr<MemoryMapper> OwnedMMapper;
221};
222
223} // end namespace backport
224} // end namespace llvm
225
226#endif // LLVM_EXECUTIONENGINE_BACKPORT_SECTIONMEMORYMANAGER_H
void * Block
Definition: bufmgr.h:25
size_t Size
Definition: c.h:576
virtual std::error_code releaseMappedMemory(sys::MemoryBlock &M)=0
virtual sys::MemoryBlock allocateMappedMemory(AllocationPurpose Purpose, size_t NumBytes, const sys::MemoryBlock *const NearBlock, unsigned Flags, std::error_code &EC)=0
virtual std::error_code protectMappedMemory(const sys::MemoryBlock &Block, unsigned Flags)=0
std::unique_ptr< MemoryMapper > OwnedMMapper
SectionMemoryManager(const SectionMemoryManager &)=delete
SectionMemoryManager(MemoryMapper *MM=nullptr, bool ReserveAlloc=false)
void operator=(const SectionMemoryManager &)=delete
virtual void reserveAllocationSpace(uintptr_t CodeSize, uint32_t CodeAlign, uintptr_t RODataSize, uint32_t RODataAlign, uintptr_t RWDataSize, uint32_t RWDataAlign) override
bool finalizeMemory(std::string *ErrMsg=nullptr) override
uint8_t * allocateSection(AllocationPurpose Purpose, uintptr_t Size, unsigned Alignment)
bool hasSpace(const MemoryGroup &MemGroup, uintptr_t Size) const
uint8_t * allocateDataSection(uintptr_t Size, unsigned Alignment, unsigned SectionID, StringRef SectionName, bool IsReadOnly) override
bool needsToReserveAllocationSpace() override
Enable reserveAllocationSpace when requested.
std::error_code applyMemoryGroupPermissions(MemoryGroup &MemGroup, unsigned Permissions)
uint8_t * allocateCodeSection(uintptr_t Size, unsigned Alignment, unsigned SectionID, StringRef SectionName) override
char string[11]
Definition: preproc-type.c:52
SmallVector< sys::MemoryBlock, 16 > AllocatedMem
SmallVector< sys::MemoryBlock, 16 > PendingMem