xref: /aosp_15_r20/system/libhidl/libhidlmemory/AshmemMemory.h (revision 8222fbe171c3d6fadfe95119c180cf3010c392a8)
1 /*
2  * Copyright (C) 2023 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 #pragma once
17 
18 #include <android/hidl/memory/1.0/IMemory.h>
19 #include <hidl/Status.h>
20 #include <inttypes.h>
21 #include <log/log.h>
22 #include <sys/mman.h>
23 
24 namespace android {
25 namespace hardware {
26 namespace impl {
27 
28 using ::android::sp;
29 using ::android::hardware::hidl_memory;
30 using ::android::hardware::Return;
31 using ::android::hardware::Void;
32 using ::android::hidl::memory::V1_0::IMemory;
33 
34 struct AshmemMemory : public IMemory {
AshmemMemoryAshmemMemory35     AshmemMemory(const hidl_memory& memory, void* data) : mMemory(memory), mData(data) {}
36 
~AshmemMemoryAshmemMemory37     ~AshmemMemory() { munmap(mData, mMemory.size()); }
38 
39     // Methods from ::android::hidl::memory::V1_0::IMemory follow.
getPointerAshmemMemory40     Return<void*> getPointer() override { return mData; }
getSizeAshmemMemory41     Return<uint64_t> getSize() override { return mMemory.size(); }
42     // NOOPs (since non0remoted memory)
updateAshmemMemory43     Return<void> update() override { return Void(); }
updateRangeAshmemMemory44     Return<void> updateRange(uint64_t /*start*/, uint64_t /*length*/) override { return Void(); }
readAshmemMemory45     Return<void> read() override { return Void(); }
readRangeAshmemMemory46     Return<void> readRange(uint64_t /*start*/, uint64_t /*length*/) override { return Void(); }
commitAshmemMemory47     Return<void> commit() override { return Void(); }
48 
49   private:
50     // Holding onto hidl_memory reference because it contains
51     // handle and size, and handle will also be required for
52     // the remoted case.
53     hidl_memory mMemory;
54 
55     // Mapped memory in process.
56     void* mData;
57 };
58 
59 }  // namespace impl
60 }  // namespace hardware
61 }  // namespace android
62