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