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