diff options
author | Rui Ueyama <ruiu@google.com> | 2017-11-28 20:39:17 +0000 |
---|---|---|
committer | Rui Ueyama <ruiu@google.com> | 2017-11-28 20:39:17 +0000 |
commit | 2017d52b54c181f2236e6c94d43f4aa43c30583f (patch) | |
tree | 9c6f4a2dab676e0725580e905e9a1d01be4287ac /lld/include/lld | |
parent | 7b361b50d88fd142cada51b3545ae6c0803c44a5 (diff) | |
download | bcm5719-llvm-2017d52b54c181f2236e6c94d43f4aa43c30583f.tar.gz bcm5719-llvm-2017d52b54c181f2236e6c94d43f4aa43c30583f.zip |
Move Memory.{h,cpp} to Common.
Differential Revision: https://reviews.llvm.org/D40571
llvm-svn: 319221
Diffstat (limited to 'lld/include/lld')
-rw-r--r-- | lld/include/lld/Common/Memory.h | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/lld/include/lld/Common/Memory.h b/lld/include/lld/Common/Memory.h new file mode 100644 index 00000000000..699f7c1654c --- /dev/null +++ b/lld/include/lld/Common/Memory.h @@ -0,0 +1,60 @@ +//===- Memory.h -------------------------------------------------*- C++ -*-===// +// +// The LLVM Linker +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// 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 this arena if your object has a destructor. +// Your destructor will be invoked from freeArena(). +template <typename T, typename... U> T *make(U &&... Args) { + static SpecificAlloc<T> Alloc; + return new (Alloc.Alloc.Allocate()) T(std::forward<U>(Args)...); +} + +} // namespace lld + +#endif |