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 <new>
20
21#include "bcc/Source.h"
22#include "bcc/Support/Log.h"
23
24#include "BCCContextImpl.h"
25
26using namespace bcc;
27
28static BCCContext *GlobalContext = nullptr;
29
30BCCContext *BCCContext::GetOrCreateGlobalContext() {
31  if (GlobalContext == nullptr) {
32    GlobalContext = new (std::nothrow) BCCContext();
33    if (GlobalContext == nullptr) {
34      ALOGE("Out of memory when allocating global BCCContext!");
35    }
36  }
37  return GlobalContext;
38}
39
40void BCCContext::DestroyGlobalContext() {
41  delete GlobalContext;
42  GlobalContext = nullptr;
43}
44
45BCCContext::BCCContext() : mImpl(new BCCContextImpl(*this)) { }
46
47BCCContext::~BCCContext() {
48  delete mImpl;
49  if (this == GlobalContext) {
50    // We're deleting the context returned from GetOrCreateGlobalContext().
51    // Reset the GlobalContext.
52    GlobalContext = nullptr;
53  }
54}
55
56void BCCContext::addSource(Source &pSource)
57{ mImpl->mOwnSources.insert(&pSource); }
58
59void BCCContext::removeSource(Source &pSource)
60{ mImpl->mOwnSources.erase(&pSource); }
61
62llvm::LLVMContext &BCCContext::getLLVMContext()
63{ return mImpl->mLLVMContext; }
64
65const llvm::LLVMContext &BCCContext::getLLVMContext() const
66{ return mImpl->mLLVMContext; }
67