1 /*
2  * Copyright 2012, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "bcc/BCCContext.h"
18 
19 #include "BCCContextImpl.h"
20 #include "Log.h"
21 #include "bcc/Source.h"
22 
23 #include <new>
24 
25 using namespace bcc;
26 
27 static BCCContext *GlobalContext = nullptr;
28 
GetOrCreateGlobalContext()29 BCCContext *BCCContext::GetOrCreateGlobalContext() {
30   if (GlobalContext == nullptr) {
31     GlobalContext = new (std::nothrow) BCCContext();
32     if (GlobalContext == nullptr) {
33       ALOGE("Out of memory when allocating global BCCContext!");
34     }
35   }
36   return GlobalContext;
37 }
38 
DestroyGlobalContext()39 void BCCContext::DestroyGlobalContext() {
40   delete GlobalContext;
41   GlobalContext = nullptr;
42 }
43 
BCCContext()44 BCCContext::BCCContext() : mImpl(new BCCContextImpl(*this)) { }
45 
~BCCContext()46 BCCContext::~BCCContext() {
47   delete mImpl;
48   if (this == GlobalContext) {
49     // We're deleting the context returned from GetOrCreateGlobalContext().
50     // Reset the GlobalContext.
51     GlobalContext = nullptr;
52   }
53 }
54 
addSource(Source & pSource)55 void BCCContext::addSource(Source &pSource)
56 { mImpl->mOwnSources.insert(&pSource); }
57 
removeSource(Source & pSource)58 void BCCContext::removeSource(Source &pSource)
59 { mImpl->mOwnSources.erase(&pSource); }
60 
getLLVMContext()61 llvm::LLVMContext &BCCContext::getLLVMContext()
62 { return mImpl->mLLVMContext; }
63 
getLLVMContext() const64 const llvm::LLVMContext &BCCContext::getLLVMContext() const
65 { return mImpl->mLLVMContext; }
66