• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 #include "ErasedMessageQueue.hpp"
18 
19 /*
20  * Convert a Rust NativeHandle (passed as its individual fields) to a C++ one.
21  * Duplicates the file descriptors, which are passed as integers.
22  */
convertHandle(const int * fds,size_t n_fds,const int32_t * ints,size_t n_ints)23 NativeHandle convertHandle(const int* fds, size_t n_fds, const int32_t* ints, size_t n_ints) {
24     std::vector<ndk::ScopedFileDescriptor> fdv;
25     for (size_t i = 0; i < n_fds; i++) {
26         fdv.push_back(std::move(ndk::ScopedFileDescriptor(fds[i])));
27     }
28     std::vector<int32_t> intv(ints, ints + n_ints);
29 
30     return NativeHandle(std::move(fdv), intv);
31 }
32 
convertGrantor(int32_t fdIndex,int32_t offset,int64_t extent)33 GrantorDescriptor convertGrantor(int32_t fdIndex, int32_t offset, int64_t extent) {
34     return GrantorDescriptor(fdIndex, offset, extent);
35 }
36 
convertDesc(const GrantorDescriptor * grantors,size_t n_grantors,const int * handle_fds,size_t handle_n_fds,const int32_t * handle_ints,size_t handle_n_ints,int32_t quantum,int32_t flags)37 ErasedMessageQueueDesc* convertDesc(const GrantorDescriptor* grantors, size_t n_grantors,
38                                     const int* handle_fds, size_t handle_n_fds,
39                                     const int32_t* handle_ints, size_t handle_n_ints,
40                                     int32_t quantum, int32_t flags) {
41     std::vector<GrantorDescriptor> grantorsv(grantors, grantors + n_grantors);
42     auto&& handle = convertHandle(handle_fds, handle_n_fds, handle_ints, handle_n_ints);
43 
44     return new ErasedMessageQueueDesc{
45             grantorsv,
46             std::move(handle),
47             quantum,
48             flags,
49     };
50 }
51 
freeDesc(ErasedMessageQueueDesc * desc)52 void freeDesc(ErasedMessageQueueDesc* desc) {
53     delete desc;
54 }
55 
descGrantors(const ErasedMessageQueueDesc & desc)56 const GrantorDescriptor* descGrantors(const ErasedMessageQueueDesc& desc) {
57     return desc.grantors.data();
58 }
descNumGrantors(const ErasedMessageQueueDesc & desc)59 size_t descNumGrantors(const ErasedMessageQueueDesc& desc) {
60     return desc.grantors.size();
61 }
descHandleFDs(const ErasedMessageQueueDesc & desc)62 const ndk::ScopedFileDescriptor* descHandleFDs(const ErasedMessageQueueDesc& desc) {
63     return desc.handle.fds.data();
64 }
descHandleNumFDs(const ErasedMessageQueueDesc & desc)65 size_t descHandleNumFDs(const ErasedMessageQueueDesc& desc) {
66     return desc.handle.fds.size();
67 }
descHandleInts(const ErasedMessageQueueDesc & desc)68 const int* descHandleInts(const ErasedMessageQueueDesc& desc) {
69     return desc.handle.ints.data();
70 }
descHandleNumInts(const ErasedMessageQueueDesc & desc)71 size_t descHandleNumInts(const ErasedMessageQueueDesc& desc) {
72     return desc.handle.ints.size();
73 }
descQuantum(const ErasedMessageQueueDesc & desc)74 int32_t descQuantum(const ErasedMessageQueueDesc& desc) {
75     return desc.quantum;
76 }
descFlags(const ErasedMessageQueueDesc & desc)77 int32_t descFlags(const ErasedMessageQueueDesc& desc) {
78     return desc.flags;
79 }
80 
ErasedMessageQueue(const ErasedMessageQueueDesc & desc,bool resetPointers)81 ErasedMessageQueue::ErasedMessageQueue(const ErasedMessageQueueDesc& desc, bool resetPointers)
82     : inner(new android::AidlMessageQueue<MQErased, SynchronizedReadWrite>(desc, resetPointers)) {}
83 
ErasedMessageQueue(size_t numElementsInQueue,bool configureEventFlagWord,size_t quantum)84 ErasedMessageQueue::ErasedMessageQueue(size_t numElementsInQueue, bool configureEventFlagWord,
85                                        size_t quantum)
86     : inner(new android::AidlMessageQueue<MQErased, SynchronizedReadWrite>(
87               numElementsInQueue, configureEventFlagWord, quantum)) {}
88 
beginWrite(size_t nMessages,MemTransaction * memTx) const89 bool ErasedMessageQueue::beginWrite(size_t nMessages, MemTransaction* memTx) const {
90     MessageQueueBase<AidlMQDescriptorShim, MQErased,
91                      FlavorTypeToValue<SynchronizedReadWrite>::value>::MemTransaction memTxInternal;
92     auto result = inner->beginWrite(nMessages, &memTxInternal);
93     memTx->first = memTxInternal.getFirstRegion();
94     memTx->second = memTxInternal.getSecondRegion();
95     return result;
96 };
97 
commitWrite(size_t nMessages)98 bool ErasedMessageQueue::commitWrite(size_t nMessages) {
99     return inner->commitWrite(nMessages);
100 }
101 
beginRead(size_t nMessages,MemTransaction * memTx) const102 bool ErasedMessageQueue::beginRead(size_t nMessages, MemTransaction* memTx) const {
103     MessageQueueBase<AidlMQDescriptorShim, MQErased,
104                      FlavorTypeToValue<SynchronizedReadWrite>::value>::MemTransaction memTxInternal;
105     auto result = inner->beginRead(nMessages, &memTxInternal);
106     memTx->first = memTxInternal.getFirstRegion();
107     memTx->second = memTxInternal.getSecondRegion();
108     return result;
109 }
110 
commitRead(size_t nMessages)111 bool ErasedMessageQueue::commitRead(size_t nMessages) {
112     return inner->commitRead(nMessages);
113 }
114 
dupeDesc()115 ErasedMessageQueueDesc* ErasedMessageQueue::dupeDesc() {
116     return new ErasedMessageQueueDesc(inner->dupeDesc());
117 }
118