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     delete(mModule);
39 }
40 
getMemory(int32_t pid,MemtrackType type,getMemory_cb _hidl_cb)41 Return<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 
HIDL_FETCH_IMemtrack(const char *)76 IMemtrack* HIDL_FETCH_IMemtrack(const char* /* name */) {
77     const hw_module_t* hw_module = nullptr;
78     const memtrack_module_t* memtrack_module = nullptr;
79     int err = hw_get_module(MEMTRACK_HARDWARE_MODULE_ID, &hw_module);
80     if (err) {
81         ALOGE ("hw_get_module %s failed: %d", MEMTRACK_HARDWARE_MODULE_ID, err);
82         return nullptr;
83     }
84 
85     if (!hw_module->methods || !hw_module->methods->open) {
86         memtrack_module = reinterpret_cast<const memtrack_module_t*>(hw_module);
87     } else {
88         err = hw_module->methods->open(hw_module, MEMTRACK_HARDWARE_MODULE_ID,
89                 reinterpret_cast<hw_device_t**>(const_cast<memtrack_module_t**>(&memtrack_module)));
90         if (err) {
91             ALOGE("Passthrough failed to load legacy HAL.");
92             return nullptr;
93         }
94     }
95     return new Memtrack(memtrack_module);
96 }
97 
98 } // namespace implementation
99 }  // namespace V1_0
100 }  // namespace memtrack
101 }  // namespace hardware
102 }  // namespace android
103