Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:41

0001 //===- CommonLinkerContext.h ------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // Entry point for all global state in lldCommon. The objective is for LLD to be
0010 // used "as a library" in a thread-safe manner.
0011 //
0012 // Instead of program-wide globals or function-local statics, we prefer
0013 // aggregating all "global" states into a heap-based structure
0014 // (CommonLinkerContext). This also achieves deterministic initialization &
0015 // shutdown for all "global" states.
0016 //
0017 //===----------------------------------------------------------------------===//
0018 
0019 #ifndef LLD_COMMON_COMMONLINKINGCONTEXT_H
0020 #define LLD_COMMON_COMMONLINKINGCONTEXT_H
0021 
0022 #include "lld/Common/ErrorHandler.h"
0023 #include "lld/Common/Memory.h"
0024 #include "llvm/Support/StringSaver.h"
0025 
0026 namespace llvm {
0027 class raw_ostream;
0028 } // namespace llvm
0029 
0030 namespace lld {
0031 struct SpecificAllocBase;
0032 class CommonLinkerContext {
0033 public:
0034   CommonLinkerContext();
0035   virtual ~CommonLinkerContext();
0036 
0037   static void destroy();
0038 
0039   llvm::BumpPtrAllocator bAlloc;
0040   llvm::StringSaver saver{bAlloc};
0041   llvm::UniqueStringSaver uniqueSaver{bAlloc};
0042   llvm::DenseMap<void *, SpecificAllocBase *> instances;
0043 
0044   ErrorHandler e;
0045 };
0046 
0047 // Retrieve the global state. Currently only one state can exist per process,
0048 // but in the future we plan on supporting an arbitrary number of LLD instances
0049 // in a single process.
0050 CommonLinkerContext &commonContext();
0051 
0052 template <typename T = CommonLinkerContext> T &context() {
0053   return static_cast<T &>(commonContext());
0054 }
0055 
0056 bool hasContext();
0057 
0058 inline llvm::BumpPtrAllocator &bAlloc() { return context().bAlloc; }
0059 inline llvm::StringSaver &saver() { return context().saver; }
0060 inline llvm::UniqueStringSaver &uniqueSaver() { return context().uniqueSaver; }
0061 } // namespace lld
0062 
0063 #endif