1 //===- CommonLinkerContext.h ------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Entry point for all global state in lldCommon. The objective is for LLD to be 10 // used "as a library" in a thread-safe manner. 11 // 12 // Instead of program-wide globals or function-local statics, we prefer 13 // aggregating all "global" states into a heap-based structure 14 // (CommonLinkerContext). This also achieves deterministic initialization & 15 // shutdown for all "global" states. 16 // 17 //===----------------------------------------------------------------------===// 18 19 #ifndef LLD_COMMON_COMMONLINKINGCONTEXT_H 20 #define LLD_COMMON_COMMONLINKINGCONTEXT_H 21 22 #include "lld/Common/ErrorHandler.h" 23 #include "lld/Common/Memory.h" 24 #include "llvm/Support/StringSaver.h" 25 26 namespace llvm { 27 class raw_ostream; 28 } // namespace llvm 29 30 namespace lld { 31 struct SpecificAllocBase; 32 class CommonLinkerContext { 33 public: 34 CommonLinkerContext(); 35 virtual ~CommonLinkerContext(); 36 37 static void destroy(); 38 39 llvm::BumpPtrAllocator bAlloc; 40 llvm::StringSaver saver{bAlloc}; 41 llvm::UniqueStringSaver uniqueSaver{bAlloc}; 42 llvm::DenseMap<void *, SpecificAllocBase *> instances; 43 44 ErrorHandler e; 45 }; 46 47 // Retrieve the global state. Currently only one state can exist per process, 48 // but in the future we plan on supporting an arbitrary number of LLD instances 49 // in a single process. 50 CommonLinkerContext &commonContext(); 51 52 template <typename T = CommonLinkerContext> T &context() { 53 return static_cast<T &>(commonContext()); 54 } 55 56 bool hasContext(); 57 58 inline llvm::BumpPtrAllocator &bAlloc() { return context().bAlloc; } 59 inline llvm::StringSaver &saver() { return context().saver; } 60 inline llvm::UniqueStringSaver &uniqueSaver() { return context().uniqueSaver; } 61 } // namespace lld 62 63 #endif 64