1*83d59e05SAlexandre Ganea //===- CommonLinkerContext.cpp --------------------------------------------===//
2*83d59e05SAlexandre Ganea //
3*83d59e05SAlexandre Ganea // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*83d59e05SAlexandre Ganea // See https://llvm.org/LICENSE.txt for license information.
5*83d59e05SAlexandre Ganea // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*83d59e05SAlexandre Ganea //
7*83d59e05SAlexandre Ganea //===----------------------------------------------------------------------===//
8*83d59e05SAlexandre Ganea 
9*83d59e05SAlexandre Ganea #include "lld/Common/CommonLinkerContext.h"
10*83d59e05SAlexandre Ganea #include "lld/Common/ErrorHandler.h"
11*83d59e05SAlexandre Ganea #include "lld/Common/Memory.h"
12*83d59e05SAlexandre Ganea 
13*83d59e05SAlexandre Ganea using namespace llvm;
14*83d59e05SAlexandre Ganea using namespace lld;
15*83d59e05SAlexandre Ganea 
16*83d59e05SAlexandre Ganea // Reference to the current LLD instance. This is a temporary situation, until
17*83d59e05SAlexandre Ganea // we pass this context everywhere by reference, or we make it a thread_local,
18*83d59e05SAlexandre Ganea // as in https://reviews.llvm.org/D108850?id=370678 where each thread can be
19*83d59e05SAlexandre Ganea // associated with a LLD instance. Only then will LLD be free of global
20*83d59e05SAlexandre Ganea // state.
21*83d59e05SAlexandre Ganea static CommonLinkerContext *lctx;
22*83d59e05SAlexandre Ganea 
23*83d59e05SAlexandre Ganea CommonLinkerContext::CommonLinkerContext() { lctx = this; }
24*83d59e05SAlexandre Ganea 
25*83d59e05SAlexandre Ganea CommonLinkerContext::~CommonLinkerContext() {
26*83d59e05SAlexandre Ganea   assert(lctx);
27*83d59e05SAlexandre Ganea   // Explicitly call the destructors since we created the objects with placement
28*83d59e05SAlexandre Ganea   // new in SpecificAlloc::create().
29*83d59e05SAlexandre Ganea   for (auto &it : instances)
30*83d59e05SAlexandre Ganea     it.second->~SpecificAllocBase();
31*83d59e05SAlexandre Ganea   lctx = nullptr;
32*83d59e05SAlexandre Ganea }
33*83d59e05SAlexandre Ganea 
34*83d59e05SAlexandre Ganea CommonLinkerContext &lld::commonContext() {
35*83d59e05SAlexandre Ganea   assert(lctx);
36*83d59e05SAlexandre Ganea   return *lctx;
37*83d59e05SAlexandre Ganea }
38*83d59e05SAlexandre Ganea 
39*83d59e05SAlexandre Ganea bool lld::hasContext() { return lctx != nullptr; }
40*83d59e05SAlexandre Ganea 
41*83d59e05SAlexandre Ganea void CommonLinkerContext::destroy() {
42*83d59e05SAlexandre Ganea   if (lctx == nullptr)
43*83d59e05SAlexandre Ganea     return;
44*83d59e05SAlexandre Ganea   delete lctx;
45*83d59e05SAlexandre Ganea }
46