//===- Allocator.h - Simple memory allocation abstraction -------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// /// \file /// /// This file defines the BumpPtrAllocator interface. BumpPtrAllocator conforms /// to the LLVM "Allocator" concept and is similar to MallocAllocator, but /// objects cannot be deallocated. Their lifetime is tied to the lifetime of the /// allocator. /// //===----------------------------------------------------------------------===// #ifndef LLVM_SUPPORT_ALLOCATOR_H #define LLVM_SUPPORT_ALLOCATOR_H #include "llvm/ADT/SmallVector.h" #include "llvm/Support/Alignment.h" #include "llvm/Support/AllocatorBase.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/MathExtras.h" #include <algorithm> #include <cassert> #include <cstddef> #include <cstdint> #include <iterator> #include <optional> #include <utility> namespace llvm { namespace detail { // We call out to an external function to actually print the message as the // printing code uses Allocator.h in its implementation. void printBumpPtrAllocatorStats(unsigned NumSlabs, size_t BytesAllocated, size_t TotalMemory); } // end namespace detail /// Allocate memory in an ever growing pool, as if by bump-pointer. /// /// This isn't strictly a bump-pointer allocator as it uses backing slabs of /// memory rather than relying on a boundless contiguous heap. However, it has /// bump-pointer semantics in that it is a monotonically growing pool of memory /// where every allocation is found by merely allocating the next N bytes in /// the slab, or the next N bytes in the next slab. /// /// Note that this also has a threshold for forcing allocations above a certain /// size into their own slab. /// /// The BumpPtrAllocatorImpl template defaults to using a MallocAllocator /// object, which wraps malloc, to allocate memory, but it can be changed to /// use a custom allocator. /// /// The GrowthDelay specifies after how many allocated slabs the allocator /// increases the size of the slabs. template <typename AllocatorT = MallocAllocator, size_t SlabSize = 4096, size_t SizeThreshold = SlabSize, size_t GrowthDelay = 128> class BumpPtrAllocatorImpl : public AllocatorBase<BumpPtrAllocatorImpl<AllocatorT, SlabSize, SizeThreshold, GrowthDelay>>, private detail::AllocatorHolder<AllocatorT> { … }; /// The standard BumpPtrAllocator which just uses the default template /// parameters. BumpPtrAllocator; /// A BumpPtrAllocator that allows only elements of a specific type to be /// allocated. /// /// This allows calling the destructor in DestroyAll() and when the allocator is /// destroyed. template <typename T> class SpecificBumpPtrAllocator { … }; } // end namespace llvm template <typename AllocatorT, size_t SlabSize, size_t SizeThreshold, size_t GrowthDelay> void * operator new(size_t Size, llvm::BumpPtrAllocatorImpl<AllocatorT, SlabSize, SizeThreshold, GrowthDelay> &Allocator) { … } template <typename AllocatorT, size_t SlabSize, size_t SizeThreshold, size_t GrowthDelay> void operator delete(void *, llvm::BumpPtrAllocatorImpl<AllocatorT, SlabSize, SizeThreshold, GrowthDelay> &) { … } #endif // LLVM_SUPPORT_ALLOCATOR_H