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