Shih-wei Liao | 21d28f5 | 2012-06-12 05:55:00 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 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 "gbc_context.h" |
| 18 | |
| 19 | #include "atomic.h" |
| 20 | |
| 21 | namespace art { |
| 22 | namespace greenland { |
| 23 | |
| 24 | GBCContext::GBCContext() |
| 25 | : context_(), module_(NULL), ref_count_(1), mem_usage_(0) { |
| 26 | module_ = new llvm::Module("art", context_); |
| 27 | |
| 28 | // Initialize the contents of an empty module |
| 29 | // Type of "JavaObject" |
| 30 | llvm::StructType::create(context_, "JavaObject"); |
| 31 | // Type of "Method" |
| 32 | llvm::StructType::create(context_, "Method"); |
| 33 | // Type of "Thread" |
| 34 | llvm::StructType::create(context_, "Thread"); |
| 35 | |
| 36 | dex_lang_ctx_ = new DexLang::Context(*module_); |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | GBCContext::~GBCContext() { |
| 41 | delete dex_lang_ctx_; |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | GBCContext& GBCContext::IncRef() { |
| 46 | android_atomic_inc(&ref_count_); |
| 47 | return *this; |
| 48 | } |
| 49 | |
| 50 | const GBCContext& GBCContext::IncRef() const { |
| 51 | android_atomic_inc(&ref_count_); |
| 52 | return *this; |
| 53 | } |
| 54 | |
| 55 | void GBCContext::DecRef() const { |
| 56 | int32_t old_ref_count = android_atomic_dec(&ref_count_); |
| 57 | if (old_ref_count <= 1) { |
| 58 | delete this; |
| 59 | } |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | void GBCContext::AddMemUsageApproximation(size_t usage) { |
| 64 | android_atomic_add(static_cast<int32_t>(usage), &mem_usage_); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | } // namespace greenland |
| 69 | } // namespace art |