Ruchi Kandoi | 97b2f25 | 2016-09-29 13:55:10 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #define LOG_TAG "android.hardware.memtrack@1.0-impl" |
| 18 | #include <hardware/hardware.h> |
| 19 | #include <hardware/memtrack.h> |
| 20 | |
| 21 | #include "Memtrack.h" |
| 22 | namespace android { |
| 23 | namespace hardware { |
| 24 | namespace memtrack { |
| 25 | namespace V1_0 { |
| 26 | namespace implementation { |
| 27 | |
| 28 | Memtrack::Memtrack(memtrack_module_t *module) : mModule(module) { |
| 29 | if (mModule) |
| 30 | mModule->init(mModule); |
| 31 | } |
| 32 | |
| 33 | Memtrack::~Memtrack() { |
| 34 | delete(mModule); |
| 35 | } |
| 36 | |
| 37 | Return<void> Memtrack::getMemory(int32_t pid, MemtrackType type, |
| 38 | getMemory_cb _hidl_cb) { |
| 39 | hidl_vec<MemtrackRecord> records; |
| 40 | size_t temp = 0; |
| 41 | size_t *size = &temp; |
| 42 | int ret = 0; |
| 43 | |
| 44 | if (mModule->getMemory == nullptr) |
| 45 | { |
| 46 | _hidl_cb(MemtrackStatus::SUCCESS, records); |
| 47 | return Void(); |
| 48 | } |
| 49 | ret = mModule->getMemory(mModule, pid, static_cast<memtrack_type>(type), |
| 50 | NULL, size); |
| 51 | if (ret == 0) |
| 52 | { |
| 53 | memtrack_record *legacy_records = new memtrack_record[*size]; |
| 54 | ret = mModule->getMemory(mModule, pid, |
| 55 | static_cast<memtrack_type>(type), legacy_records, size); |
| 56 | if (ret == 0) |
| 57 | { |
| 58 | records.resize(*size); |
| 59 | for(size_t i = 0; i < *size; i++) |
| 60 | { |
| 61 | records[i].sizeInBytes = legacy_records[i].size_in_bytes; |
| 62 | records[i].flags = legacy_records[i].flags; |
| 63 | } |
| 64 | } |
| 65 | delete[] legacy_records; |
| 66 | } |
| 67 | _hidl_cb(MemtrackStatus::SUCCESS, records); |
| 68 | return Void(); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | IMemtrack* HIDL_FETCH_IMemtrack(const char* name) { |
| 73 | int ret = 0; |
| 74 | const hw_module_t* hw_module = NULL; |
| 75 | memtrack_module_t *memtrack_module = NULL; |
| 76 | |
| 77 | ret = hw_get_module(name, &hw_module); |
| 78 | if (ret == 0 && hw_module->methods->open > 0) |
| 79 | { |
| 80 | ret = hw_module->methods->open(hw_module, name, |
| 81 | reinterpret_cast<hw_device_t**>(&memtrack_module)); |
| 82 | if (ret == 0) |
| 83 | return new Memtrack(memtrack_module); |
| 84 | else { |
| 85 | ALOGE("Passthrough failed to load legacy HAL."); |
| 86 | } |
| 87 | } |
| 88 | else { |
| 89 | ALOGE ("hw_get_module %s failed: %d", name, ret); |
| 90 | } |
| 91 | return nullptr; |
| 92 | } |
| 93 | |
| 94 | } // namespace implementation |
| 95 | } // namespace V1_0 |
| 96 | } // namespace memtrack |
| 97 | } // namespace hardware |
| 98 | } // namespace android |