blob: 892fd5aacd0147d35643423704e9c61808788cd8 [file] [log] [blame]
Zonr Changc09dee62012-04-12 17:15:30 +08001/*
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 Hines2f6a4932012-05-03 12:27:13 -070017#include "BCCContext.h"
Zonr Changc09dee62012-04-12 17:15:30 +080018
19#include <new>
20
21#include "BCCContextImpl.h"
Stephen Hines4a68b1c2012-05-03 12:28:14 -070022#include "Compiler.h"
Stephen Hines2f6a4932012-05-03 12:27:13 -070023#include "DebugHelper.h"
24#include "Source.h"
Zonr Changc09dee62012-04-12 17:15:30 +080025
26using namespace bcc;
27
28static BCCContext *GlobalContext = NULL;
29
30BCCContext *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
40void BCCContext::DestroyGlobalContext() {
41 delete GlobalContext;
42 GlobalContext = NULL;
43}
44
Stephen Hines4a68b1c2012-05-03 12:28:14 -070045BCCContext::BCCContext() : mImpl(new BCCContextImpl(*this)) {
46 // Initialize the LLVM compiler infrastructure.
47 Compiler::GlobalInitialization();
48}
Zonr Changc09dee62012-04-12 17:15:30 +080049
50BCCContext::~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 Changc09dee62012-04-12 17:15:30 +080057}
58
Shih-wei Liao4ce024b2012-04-25 03:40:50 -070059void BCCContext::addSource(Source &pSource)
60{ mImpl->mOwnSources.insert(&pSource); }
61
62void BCCContext::removeSource(Source &pSource)
63{ mImpl->mOwnSources.erase(&pSource); }
64
Zonr Changc09dee62012-04-12 17:15:30 +080065llvm::LLVMContext &BCCContext::getLLVMContext()
66{ return mImpl->mLLVMContext; }
67
68const llvm::LLVMContext &BCCContext::getLLVMContext() const
69{ return mImpl->mLLVMContext; }