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::DenseMap<void *, SpecificAllocBase *> instances; |
42 | |
43 | ErrorHandler e; |
44 | }; |
45 | |
46 | // Retrieve the global state. Currently only one state can exist per process, |
47 | // but in the future we plan on supporting an arbitrary number of LLD instances |
48 | // in a single process. |
49 | CommonLinkerContext &commonContext(); |
50 | |
51 | template <typename T = CommonLinkerContext> T &context() { |
52 | return static_cast<T &>(commonContext()); |
53 | } |
54 | |
55 | bool hasContext(); |
56 | |
57 | inline llvm::StringSaver &saver() { return context().saver; } |
58 | inline llvm::BumpPtrAllocator &bAlloc() { return context().bAlloc; } |
59 | } // namespace lld |
60 | |
61 | #endif |
62 | |