Path: blob/main/contrib/llvm-project/clang/lib/AST/Interp/DynamicAllocator.h
35292 views
//==--------- DynamicAllocator.h - Dynamic allocations ------------*- C++ -*-=//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLVM_CLANG_AST_INTERP_DYNAMIC_ALLOCATOR_H9#define LLVM_CLANG_AST_INTERP_DYNAMIC_ALLOCATOR_H1011#include "Descriptor.h"12#include "InterpBlock.h"13#include "llvm/ADT/SmallVector.h"14#include "llvm/ADT/iterator_range.h"15#include "llvm/Support/Allocator.h"1617namespace clang {18class Expr;19namespace interp {20class Block;21class InterpState;2223/// Manages dynamic memory allocations done during bytecode interpretation.24///25/// We manage allocations as a map from their new-expression to a list26/// of allocations. This is called an AllocationSite. For each site, we27/// record whether it was allocated using new or new[], the28/// IsArrayAllocation flag.29///30/// For all array allocations, we need to allocate new Descriptor instances,31/// so the DynamicAllocator has a llvm::BumpPtrAllocator similar to Program.32class DynamicAllocator final {33struct Allocation {34std::unique_ptr<std::byte[]> Memory;35Allocation(std::unique_ptr<std::byte[]> Memory)36: Memory(std::move(Memory)) {}37};3839struct AllocationSite {40llvm::SmallVector<Allocation> Allocations;41bool IsArrayAllocation = false;4243AllocationSite(std::unique_ptr<std::byte[]> Memory, bool Array)44: IsArrayAllocation(Array) {45Allocations.push_back({std::move(Memory)});46}4748size_t size() const { return Allocations.size(); }49};5051public:52DynamicAllocator() = default;53~DynamicAllocator();5455void cleanup();5657unsigned getNumAllocations() const { return AllocationSites.size(); }5859/// Allocate ONE element of the given descriptor.60Block *allocate(const Descriptor *D, unsigned EvalID);61/// Allocate \p NumElements primitive elements of the given type.62Block *allocate(const Expr *Source, PrimType T, size_t NumElements,63unsigned EvalID);64/// Allocate \p NumElements elements of the given descriptor.65Block *allocate(const Descriptor *D, size_t NumElements, unsigned EvalID);6667/// Deallocate the given source+block combination.68/// Returns \c true if anything has been deallocatd, \c false otherwise.69bool deallocate(const Expr *Source, const Block *BlockToDelete,70InterpState &S);7172/// Checks whether the allocation done at the given source is an array73/// allocation.74bool isArrayAllocation(const Expr *Source) const {75if (auto It = AllocationSites.find(Source); It != AllocationSites.end())76return It->second.IsArrayAllocation;77return false;78}7980/// Allocation site iterator.81using const_virtual_iter =82llvm::DenseMap<const Expr *, AllocationSite>::const_iterator;83llvm::iterator_range<const_virtual_iter> allocation_sites() const {84return llvm::make_range(AllocationSites.begin(), AllocationSites.end());85}8687private:88llvm::DenseMap<const Expr *, AllocationSite> AllocationSites;8990using PoolAllocTy = llvm::BumpPtrAllocatorImpl<llvm::MallocAllocator>;91PoolAllocTy DescAllocator;9293/// Allocates a new descriptor.94template <typename... Ts> Descriptor *allocateDescriptor(Ts &&...Args) {95return new (DescAllocator) Descriptor(std::forward<Ts>(Args)...);96}97};9899} // namespace interp100} // namespace clang101#endif102103104