blob: 928341d12d63a831bfd7ed2c032dd1ef214e6bab [file] [log] [blame]
Zonr Chang1c30cee2012-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
Zonr Changc72c4dd2012-04-12 15:38:53 +080017#include "bcc/BCCContext.h"
Zonr Chang1c30cee2012-04-12 17:15:30 +080018
Zonr Chang1c30cee2012-04-12 17:15:30 +080019#include "BCCContextImpl.h"
Jean-Luc Brouilleta2dd52f2017-02-16 20:57:26 -080020#include "Log.h"
21#include "bcc/Source.h"
22
23#include <new>
Zonr Chang1c30cee2012-04-12 17:15:30 +080024
25using namespace bcc;
26
Chris Wailes900c6c12014-08-13 15:40:00 -070027static BCCContext *GlobalContext = nullptr;
Zonr Chang1c30cee2012-04-12 17:15:30 +080028
29BCCContext *BCCContext::GetOrCreateGlobalContext() {
Chris Wailes900c6c12014-08-13 15:40:00 -070030 if (GlobalContext == nullptr) {
Zonr Chang1c30cee2012-04-12 17:15:30 +080031 GlobalContext = new (std::nothrow) BCCContext();
Chris Wailes900c6c12014-08-13 15:40:00 -070032 if (GlobalContext == nullptr) {
Zonr Chang1c30cee2012-04-12 17:15:30 +080033 ALOGE("Out of memory when allocating global BCCContext!");
34 }
35 }
36 return GlobalContext;
37}
38
39void BCCContext::DestroyGlobalContext() {
40 delete GlobalContext;
Chris Wailes900c6c12014-08-13 15:40:00 -070041 GlobalContext = nullptr;
Zonr Chang1c30cee2012-04-12 17:15:30 +080042}
43
Zonr Changade92772012-04-13 15:58:24 +080044BCCContext::BCCContext() : mImpl(new BCCContextImpl(*this)) { }
Zonr Chang1c30cee2012-04-12 17:15:30 +080045
46BCCContext::~BCCContext() {
47 delete mImpl;
48 if (this == GlobalContext) {
49 // We're deleting the context returned from GetOrCreateGlobalContext().
50 // Reset the GlobalContext.
Chris Wailes900c6c12014-08-13 15:40:00 -070051 GlobalContext = nullptr;
Zonr Chang1c30cee2012-04-12 17:15:30 +080052 }
Zonr Chang1c30cee2012-04-12 17:15:30 +080053}
54
Shih-wei Liaod2a5a0e2012-04-25 03:40:50 -070055void BCCContext::addSource(Source &pSource)
56{ mImpl->mOwnSources.insert(&pSource); }
57
58void BCCContext::removeSource(Source &pSource)
59{ mImpl->mOwnSources.erase(&pSource); }
60
Zonr Chang1c30cee2012-04-12 17:15:30 +080061llvm::LLVMContext &BCCContext::getLLVMContext()
62{ return mImpl->mLLVMContext; }
63
64const llvm::LLVMContext &BCCContext::getLLVMContext() const
65{ return mImpl->mLLVMContext; }