Zonr Chang | 1c30cee | 2012-04-12 17:15:30 +0800 | [diff] [blame] | 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 | |
Zonr Chang | c72c4dd | 2012-04-12 15:38:53 +0800 | [diff] [blame] | 17 | #include "bcc/BCCContext.h" |
Zonr Chang | 1c30cee | 2012-04-12 17:15:30 +0800 | [diff] [blame] | 18 | |
Zonr Chang | 1c30cee | 2012-04-12 17:15:30 +0800 | [diff] [blame] | 19 | #include "BCCContextImpl.h" |
Jean-Luc Brouillet | a2dd52f | 2017-02-16 20:57:26 -0800 | [diff] [blame] | 20 | #include "Log.h" |
| 21 | #include "bcc/Source.h" |
| 22 | |
| 23 | #include <new> |
Zonr Chang | 1c30cee | 2012-04-12 17:15:30 +0800 | [diff] [blame] | 24 | |
| 25 | using namespace bcc; |
| 26 | |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 27 | static BCCContext *GlobalContext = nullptr; |
Zonr Chang | 1c30cee | 2012-04-12 17:15:30 +0800 | [diff] [blame] | 28 | |
| 29 | BCCContext *BCCContext::GetOrCreateGlobalContext() { |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 30 | if (GlobalContext == nullptr) { |
Zonr Chang | 1c30cee | 2012-04-12 17:15:30 +0800 | [diff] [blame] | 31 | GlobalContext = new (std::nothrow) BCCContext(); |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 32 | if (GlobalContext == nullptr) { |
Zonr Chang | 1c30cee | 2012-04-12 17:15:30 +0800 | [diff] [blame] | 33 | ALOGE("Out of memory when allocating global BCCContext!"); |
| 34 | } |
| 35 | } |
| 36 | return GlobalContext; |
| 37 | } |
| 38 | |
| 39 | void BCCContext::DestroyGlobalContext() { |
| 40 | delete GlobalContext; |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 41 | GlobalContext = nullptr; |
Zonr Chang | 1c30cee | 2012-04-12 17:15:30 +0800 | [diff] [blame] | 42 | } |
| 43 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 44 | BCCContext::BCCContext() : mImpl(new BCCContextImpl(*this)) { } |
Zonr Chang | 1c30cee | 2012-04-12 17:15:30 +0800 | [diff] [blame] | 45 | |
| 46 | BCCContext::~BCCContext() { |
| 47 | delete mImpl; |
| 48 | if (this == GlobalContext) { |
| 49 | // We're deleting the context returned from GetOrCreateGlobalContext(). |
| 50 | // Reset the GlobalContext. |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 51 | GlobalContext = nullptr; |
Zonr Chang | 1c30cee | 2012-04-12 17:15:30 +0800 | [diff] [blame] | 52 | } |
Zonr Chang | 1c30cee | 2012-04-12 17:15:30 +0800 | [diff] [blame] | 53 | } |
| 54 | |
Shih-wei Liao | d2a5a0e | 2012-04-25 03:40:50 -0700 | [diff] [blame] | 55 | void BCCContext::addSource(Source &pSource) |
| 56 | { mImpl->mOwnSources.insert(&pSource); } |
| 57 | |
| 58 | void BCCContext::removeSource(Source &pSource) |
| 59 | { mImpl->mOwnSources.erase(&pSource); } |
| 60 | |
Zonr Chang | 1c30cee | 2012-04-12 17:15:30 +0800 | [diff] [blame] | 61 | llvm::LLVMContext &BCCContext::getLLVMContext() |
| 62 | { return mImpl->mLLVMContext; } |
| 63 | |
| 64 | const llvm::LLVMContext &BCCContext::getLLVMContext() const |
| 65 | { return mImpl->mLLVMContext; } |