1170159Sariff//===- CommonLinkerContext.h ------------------------------------*- C++ -*-===// 2170159Sariff// 3170159Sariff// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4170159Sariff// See https://llvm.org/LICENSE.txt for license information. 5170159Sariff// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6170159Sariff// 7170159Sariff//===----------------------------------------------------------------------===// 8170159Sariff// 9170159Sariff// Entry point for all global state in lldCommon. The objective is for LLD to be 10170159Sariff// used "as a library" in a thread-safe manner. 11170159Sariff// 12170159Sariff// Instead of program-wide globals or function-local statics, we prefer 13170159Sariff// aggregating all "global" states into a heap-based structure 14170159Sariff// (CommonLinkerContext). This also achieves deterministic initialization & 15170159Sariff// shutdown for all "global" states. 16170159Sariff// 17170159Sariff//===----------------------------------------------------------------------===// 18170159Sariff 19170159Sariff#ifndef LLD_COMMON_COMMONLINKINGCONTEXT_H 20170159Sariff#define LLD_COMMON_COMMONLINKINGCONTEXT_H 21170159Sariff 22170159Sariff#include "lld/Common/ErrorHandler.h" 23170159Sariff#include "lld/Common/Memory.h" 24170159Sariff#include "llvm/Support/StringSaver.h" 25170159Sariff 26170159Sariffnamespace llvm { 27170159Sariffclass raw_ostream; 28170159Sariff} // namespace llvm 29170159Sariff 30170159Sariffnamespace lld { 31170159Sariffstruct SpecificAllocBase; 32170159Sariffclass CommonLinkerContext { 33170159Sariffpublic: 34170159Sariff CommonLinkerContext(); 35170159Sariff virtual ~CommonLinkerContext(); 36170159Sariff 37170159Sariff static void destroy(); 38170159Sariff 39170159Sariff llvm::BumpPtrAllocator bAlloc; 40170159Sariff llvm::StringSaver saver{bAlloc}; 41170159Sariff llvm::DenseMap<void *, SpecificAllocBase *> instances; 42170159Sariff 43170159Sariff ErrorHandler e; 44170159Sariff}; 45170159Sariff 46170159Sariff// Retrieve the global state. Currently only one state can exist per process, 47170159Sariff// but in the future we plan on supporting an arbitrary number of LLD instances 48170159Sariff// in a single process. 49170159SariffCommonLinkerContext &commonContext(); 50170159Sariff 51170159Sarifftemplate <typename T = CommonLinkerContext> T &context() { 52170159Sariff return static_cast<T &>(commonContext()); 53170159Sariff} 54170159Sariff 55170159Sariffbool hasContext(); 56170159Sariff 57170159Sariffinline llvm::StringSaver &saver() { return context().saver; } 58170159Sariffinline llvm::BumpPtrAllocator &bAlloc() { return context().bAlloc; } 59170719Sariff} // namespace lld 60170719Sariff 61170159Sariff#endif 62170159Sariff