Previously, the SpecificAllocator was a static local in the `make<T>` function template. Using static locals is nice because they are only constructed and registered if they are accessed. However, if there are multiple calls to make<> with different constructor parameters, we would get multiple static local variable instances. This is undesirable and leads to extra memory allocations. I noticed there were two sources of DefinedRegular allocations while checking heap profiles.
69 lines
2.4 KiB
C++
69 lines
2.4 KiB
C++
//===- Memory.h -------------------------------------------------*- 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file defines arena allocators.
|
|
//
|
|
// Almost all large objects, such as files, sections or symbols, are
|
|
// used for the entire lifetime of the linker once they are created.
|
|
// This usage characteristic makes arena allocator an attractive choice
|
|
// where the entire linker is one arena. With an arena, newly created
|
|
// objects belong to the arena and freed all at once when everything is done.
|
|
// Arena allocators are efficient and easy to understand.
|
|
// Most objects are allocated using the arena allocators defined by this file.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef LLD_COMMON_MEMORY_H
|
|
#define LLD_COMMON_MEMORY_H
|
|
|
|
#include "llvm/Support/Allocator.h"
|
|
#include "llvm/Support/StringSaver.h"
|
|
#include <vector>
|
|
|
|
namespace lld {
|
|
|
|
// Use this arena if your object doesn't have a destructor.
|
|
extern llvm::BumpPtrAllocator bAlloc;
|
|
extern llvm::StringSaver saver;
|
|
|
|
void freeArena();
|
|
|
|
// These two classes are hack to keep track of all
|
|
// SpecificBumpPtrAllocator instances.
|
|
struct SpecificAllocBase {
|
|
SpecificAllocBase() { instances.push_back(this); }
|
|
virtual ~SpecificAllocBase() = default;
|
|
virtual void reset() = 0;
|
|
static std::vector<SpecificAllocBase *> instances;
|
|
};
|
|
|
|
template <class T> struct SpecificAlloc : public SpecificAllocBase {
|
|
void reset() override { alloc.DestroyAll(); }
|
|
llvm::SpecificBumpPtrAllocator<T> alloc;
|
|
};
|
|
|
|
// Use a static local for these singletons so they are only registered if an
|
|
// object of this instance is ever constructed. Otherwise we will create and
|
|
// register ELF allocators for COFF and the reverse.
|
|
template <typename T>
|
|
inline llvm::SpecificBumpPtrAllocator<T> &getSpecificAllocSingleton() {
|
|
static SpecificAlloc<T> instance;
|
|
return instance.alloc;
|
|
}
|
|
|
|
// Use this arena if your object has a destructor.
|
|
// Your destructor will be invoked from freeArena().
|
|
template <typename T, typename... U> T *make(U &&... args) {
|
|
return new (getSpecificAllocSingleton<T>().Allocate())
|
|
T(std::forward<U>(args)...);
|
|
}
|
|
|
|
} // namespace lld
|
|
|
|
#endif
|