Zonr Chang | c09dee6 | 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 | |
Stephen Hines | 2f6a493 | 2012-05-03 12:27:13 -0700 | [diff] [blame] | 17 | #include "BCCContext.h" |
Zonr Chang | c09dee6 | 2012-04-12 17:15:30 +0800 | [diff] [blame] | 18 | |
| 19 | #include <new> |
| 20 | |
| 21 | #include "BCCContextImpl.h" |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame^] | 22 | #include "Compiler.h" |
Stephen Hines | 2f6a493 | 2012-05-03 12:27:13 -0700 | [diff] [blame] | 23 | #include "DebugHelper.h" |
| 24 | #include "Source.h" |
Zonr Chang | c09dee6 | 2012-04-12 17:15:30 +0800 | [diff] [blame] | 25 | |
| 26 | using namespace bcc; |
| 27 | |
| 28 | static BCCContext *GlobalContext = NULL; |
| 29 | |
| 30 | BCCContext *BCCContext::GetOrCreateGlobalContext() { |
| 31 | if (GlobalContext == NULL) { |
| 32 | GlobalContext = new (std::nothrow) BCCContext(); |
| 33 | if (GlobalContext == NULL) { |
| 34 | ALOGE("Out of memory when allocate global BCCContext!"); |
| 35 | } |
| 36 | } |
| 37 | return GlobalContext; |
| 38 | } |
| 39 | |
| 40 | void BCCContext::DestroyGlobalContext() { |
| 41 | delete GlobalContext; |
| 42 | GlobalContext = NULL; |
| 43 | } |
| 44 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame^] | 45 | BCCContext::BCCContext() : mImpl(new BCCContextImpl(*this)) { |
| 46 | // Initialize the LLVM compiler infrastructure. |
| 47 | Compiler::GlobalInitialization(); |
| 48 | } |
Zonr Chang | c09dee6 | 2012-04-12 17:15:30 +0800 | [diff] [blame] | 49 | |
| 50 | BCCContext::~BCCContext() { |
| 51 | delete mImpl; |
| 52 | if (this == GlobalContext) { |
| 53 | // We're deleting the context returned from GetOrCreateGlobalContext(). |
| 54 | // Reset the GlobalContext. |
| 55 | GlobalContext = NULL; |
| 56 | } |
Zonr Chang | c09dee6 | 2012-04-12 17:15:30 +0800 | [diff] [blame] | 57 | } |
| 58 | |
Shih-wei Liao | 4ce024b | 2012-04-25 03:40:50 -0700 | [diff] [blame] | 59 | void BCCContext::addSource(Source &pSource) |
| 60 | { mImpl->mOwnSources.insert(&pSource); } |
| 61 | |
| 62 | void BCCContext::removeSource(Source &pSource) |
| 63 | { mImpl->mOwnSources.erase(&pSource); } |
| 64 | |
Zonr Chang | c09dee6 | 2012-04-12 17:15:30 +0800 | [diff] [blame] | 65 | llvm::LLVMContext &BCCContext::getLLVMContext() |
| 66 | { return mImpl->mLLVMContext; } |
| 67 | |
| 68 | const llvm::LLVMContext &BCCContext::getLLVMContext() const |
| 69 | { return mImpl->mLLVMContext; } |