11556Srgrimes//===- CommonLinkerContext.h ------------------------------------*- C++ -*-===// 21556Srgrimes// 31556Srgrimes// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 41556Srgrimes// See https://llvm.org/LICENSE.txt for license information. 51556Srgrimes// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 61556Srgrimes// 71556Srgrimes//===----------------------------------------------------------------------===// 81556Srgrimes// 91556Srgrimes// Entry point for all global state in lldCommon. The objective is for LLD to be 101556Srgrimes// used "as a library" in a thread-safe manner. 111556Srgrimes// 121556Srgrimes// Instead of program-wide globals or function-local statics, we prefer 131556Srgrimes// aggregating all "global" states into a heap-based structure 141556Srgrimes// (CommonLinkerContext). This also achieves deterministic initialization & 151556Srgrimes// shutdown for all "global" states. 161556Srgrimes// 171556Srgrimes//===----------------------------------------------------------------------===// 181556Srgrimes 191556Srgrimes#ifndef LLD_COMMON_COMMONLINKINGCONTEXT_H 201556Srgrimes#define LLD_COMMON_COMMONLINKINGCONTEXT_H 211556Srgrimes 221556Srgrimes#include "lld/Common/ErrorHandler.h" 231556Srgrimes#include "lld/Common/Memory.h" 241556Srgrimes#include "llvm/Support/StringSaver.h" 251556Srgrimes 261556Srgrimesnamespace llvm { 271556Srgrimesclass raw_ostream; 281556Srgrimes} // namespace llvm 291556Srgrimes 301556Srgrimesnamespace lld { 311556Srgrimesstruct SpecificAllocBase; 323044Sdgclass CommonLinkerContext { 3313978Spstpublic: 341556Srgrimes CommonLinkerContext(); 351556Srgrimes virtual ~CommonLinkerContext(); 361556Srgrimes 371556Srgrimes static void destroy(); 381556Srgrimes 391556Srgrimes llvm::BumpPtrAllocator bAlloc; 401556Srgrimes llvm::StringSaver saver{bAlloc}; 411556Srgrimes llvm::DenseMap<void *, SpecificAllocBase *> instances; 421556Srgrimes 431556Srgrimes ErrorHandler e; 441556Srgrimes}; 451556Srgrimes 461556Srgrimes// Retrieve the global state. Currently only one state can exist per process, 471556Srgrimes// but in the future we plan on supporting an arbitrary number of LLD instances 481556Srgrimes// in a single process. 491556SrgrimesCommonLinkerContext &commonContext(); 501556Srgrimes 511556Srgrimestemplate <typename T = CommonLinkerContext> T &context() { 521556Srgrimes return static_cast<T &>(commonContext()); 531556Srgrimes} 541556Srgrimes 551556Srgrimesbool hasContext(); 561556Srgrimes 571556Srgrimesinline llvm::StringSaver &saver() { return context().saver; } 581556Srgrimesinline llvm::BumpPtrAllocator &bAlloc() { return context().bAlloc; } 591556Srgrimes} // namespace lld 601556Srgrimes 611556Srgrimes#endif 621556Srgrimes