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 #pragma once
18
19 #include <android-base/unique_fd.h>
20 #include <cutils/ashmem.h>
21 #include <fmq/EventFlag.h>
22 #include <sys/mman.h>
23 #include <sys/user.h>
24 #include <utils/Log.h>
25 #include <utils/SystemClock.h>
26 #include <atomic>
27 #include <new>
28
29 using android::hardware::kSynchronizedReadWrite;
30 using android::hardware::kUnsynchronizedWrite;
31 using android::hardware::MQFlavor;
32
33 namespace android {
34
35 template <template <typename, MQFlavor> class MQDescriptorType, typename T, MQFlavor flavor>
36 struct MessageQueueBase {
37 typedef MQDescriptorType<T, flavor> Descriptor;
38
39 /**
40 * @param Desc MQDescriptor describing the FMQ.
41 * @param resetPointers bool indicating whether the read/write pointers
42 * should be reset or not.
43 */
44 MessageQueueBase(const Descriptor& Desc, bool resetPointers = true);
45
46 ~MessageQueueBase();
47
48 /**
49 * This constructor uses Ashmem shared memory to create an FMQ
50 * that can contain a maximum of 'numElementsInQueue' elements of type T.
51 *
52 * @param numElementsInQueue Capacity of the MessageQueue in terms of T.
53 * @param configureEventFlagWord Boolean that specifies if memory should
54 * also be allocated and mapped for an EventFlag word.
55 * @param bufferFd User-supplied file descriptor to map the memory for the ringbuffer
56 * By default, bufferFd=-1 means library will allocate ashmem region for ringbuffer.
57 * MessageQueue takes ownership of the file descriptor.
58 * @param bufferSize size of buffer in bytes that bufferFd represents. This
59 * size must be larger than or equal to (numElementsInQueue * sizeof(T)).
60 * Otherwise, operations will cause out-of-bounds memory access.
61 */
62
63 MessageQueueBase(size_t numElementsInQueue, bool configureEventFlagWord,
64 android::base::unique_fd bufferFd, size_t bufferSize);
65
66 MessageQueueBase(size_t numElementsInQueue, bool configureEventFlagWord = false)
67 : MessageQueueBase(numElementsInQueue, configureEventFlagWord, android::base::unique_fd(),
68 0) {}
69
70 /**
71 * @return Number of items of type T that can be written into the FMQ
72 * without a read.
73 */
74 size_t availableToWrite() const;
75
76 /**
77 * @return Number of items of type T that are waiting to be read from the
78 * FMQ.
79 */
80 size_t availableToRead() const;
81
82 /**
83 * Returns the size of type T in bytes.
84 *
85 * @param Size of T.
86 */
87 size_t getQuantumSize() const;
88
89 /**
90 * Returns the size of the FMQ in terms of the size of type T.
91 *
92 * @return Number of items of type T that will fit in the FMQ.
93 */
94 size_t getQuantumCount() const;
95
96 /**
97 * @return Whether the FMQ is configured correctly.
98 */
99 bool isValid() const;
100
101 /**
102 * Non-blocking write to FMQ.
103 *
104 * @param data Pointer to the object of type T to be written into the FMQ.
105 *
106 * @return Whether the write was successful.
107 */
108 bool write(const T* data);
109
110 /**
111 * Non-blocking read from FMQ.
112 *
113 * @param data Pointer to the memory where the object read from the FMQ is
114 * copied to.
115 *
116 * @return Whether the read was successful.
117 */
118 bool read(T* data);
119
120 /**
121 * Write some data into the FMQ without blocking.
122 *
123 * @param data Pointer to the array of items of type T.
124 * @param count Number of items in array.
125 *
126 * @return Whether the write was successful.
127 */
128 bool write(const T* data, size_t count);
129
130 /**
131 * Perform a blocking write of 'count' items into the FMQ using EventFlags.
132 * Does not support partial writes.
133 *
134 * If 'evFlag' is nullptr, it is checked whether there is an EventFlag object
135 * associated with the FMQ and it is used in that case.
136 *
137 * The application code must ensure that 'evFlag' used by the
138 * reader(s)/writer is based upon the same EventFlag word.
139 *
140 * The method will return false without blocking if any of the following
141 * conditions are true:
142 * - If 'evFlag' is nullptr and the FMQ does not own an EventFlag object.
143 * - If the 'readNotification' bit mask is zero.
144 * - If 'count' is greater than the FMQ size.
145 *
146 * If the there is insufficient space available to write into it, the
147 * EventFlag bit mask 'readNotification' is is waited upon.
148 *
149 * This method should only be used with a MessageQueue of the flavor
150 * 'kSynchronizedReadWrite'.
151 *
152 * Upon a successful write, wake is called on 'writeNotification' (if
153 * non-zero).
154 *
155 * @param data Pointer to the array of items of type T.
156 * @param count Number of items in array.
157 * @param readNotification The EventFlag bit mask to wait on if there is not
158 * enough space in FMQ to write 'count' items.
159 * @param writeNotification The EventFlag bit mask to call wake on
160 * a successful write. No wake is called if 'writeNotification' is zero.
161 * @param timeOutNanos Number of nanoseconds after which the blocking
162 * write attempt is aborted.
163 * @param evFlag The EventFlag object to be used for blocking. If nullptr,
164 * it is checked whether the FMQ owns an EventFlag object and that is used
165 * for blocking instead.
166 *
167 * @return Whether the write was successful.
168 */
169 bool writeBlocking(const T* data, size_t count, uint32_t readNotification,
170 uint32_t writeNotification, int64_t timeOutNanos = 0,
171 android::hardware::EventFlag* evFlag = nullptr);
172
173 bool writeBlocking(const T* data, size_t count, int64_t timeOutNanos = 0);
174
175 /**
176 * Read some data from the FMQ without blocking.
177 *
178 * @param data Pointer to the array to which read data is to be written.
179 * @param count Number of items to be read.
180 *
181 * @return Whether the read was successful.
182 */
183 bool read(T* data, size_t count);
184
185 /**
186 * Perform a blocking read operation of 'count' items from the FMQ. Does not
187 * perform a partial read.
188 *
189 * If 'evFlag' is nullptr, it is checked whether there is an EventFlag object
190 * associated with the FMQ and it is used in that case.
191 *
192 * The application code must ensure that 'evFlag' used by the
193 * reader(s)/writer is based upon the same EventFlag word.
194 *
195 * The method will return false without blocking if any of the following
196 * conditions are true:
197 * -If 'evFlag' is nullptr and the FMQ does not own an EventFlag object.
198 * -If the 'writeNotification' bit mask is zero.
199 * -If 'count' is greater than the FMQ size.
200 *
201 * This method should only be used with a MessageQueue of the flavor
202 * 'kSynchronizedReadWrite'.
203
204 * If FMQ does not contain 'count' items, the eventFlag bit mask
205 * 'writeNotification' is waited upon. Upon a successful read from the FMQ,
206 * wake is called on 'readNotification' (if non-zero).
207 *
208 * @param data Pointer to the array to which read data is to be written.
209 * @param count Number of items to be read.
210 * @param readNotification The EventFlag bit mask to call wake on after
211 * a successful read. No wake is called if 'readNotification' is zero.
212 * @param writeNotification The EventFlag bit mask to call a wait on
213 * if there is insufficient data in the FMQ to be read.
214 * @param timeOutNanos Number of nanoseconds after which the blocking
215 * read attempt is aborted.
216 * @param evFlag The EventFlag object to be used for blocking.
217 *
218 * @return Whether the read was successful.
219 */
220 bool readBlocking(T* data, size_t count, uint32_t readNotification, uint32_t writeNotification,
221 int64_t timeOutNanos = 0, android::hardware::EventFlag* evFlag = nullptr);
222
223 bool readBlocking(T* data, size_t count, int64_t timeOutNanos = 0);
224
225 /**
226 * Get a pointer to the MQDescriptor object that describes this FMQ.
227 *
228 * @return Pointer to the MQDescriptor associated with the FMQ.
229 */
getDescMessageQueueBase230 const Descriptor* getDesc() const { return mDesc.get(); }
231
232 /**
233 * Get a pointer to the EventFlag word if there is one associated with this FMQ.
234 *
235 * @return Pointer to an EventFlag word, will return nullptr if not
236 * configured. This method does not transfer ownership. The EventFlag
237 * word will be unmapped by the MessageQueue destructor.
238 */
getEventFlagWordMessageQueueBase239 std::atomic<uint32_t>* getEventFlagWord() const { return mEvFlagWord; }
240
241 /**
242 * Describes a memory region in the FMQ.
243 */
244 struct MemRegion {
MemRegionMessageQueueBase::MemRegion245 MemRegion() : MemRegion(nullptr, 0) {}
246
MemRegionMessageQueueBase::MemRegion247 MemRegion(T* base, size_t size) : address(base), length(size) {}
248
249 MemRegion& operator=(const MemRegion& other) {
250 address = other.address;
251 length = other.length;
252 return *this;
253 }
254
255 /**
256 * Gets a pointer to the base address of the MemRegion.
257 */
getAddressMessageQueueBase::MemRegion258 inline T* getAddress() const { return address; }
259
260 /**
261 * Gets the length of the MemRegion. This would equal to the number
262 * of items of type T that can be read from/written into the MemRegion.
263 */
getLengthMessageQueueBase::MemRegion264 inline size_t getLength() const { return length; }
265
266 /**
267 * Gets the length of the MemRegion in bytes.
268 */
getLengthInBytesMessageQueueBase::MemRegion269 inline size_t getLengthInBytes() const { return length * sizeof(T); }
270
271 private:
272 /* Base address */
273 T* address;
274
275 /*
276 * Number of items of type T that can be written to/read from the base
277 * address.
278 */
279 size_t length;
280 };
281
282 /**
283 * Describes the memory regions to be used for a read or write.
284 * The struct contains two MemRegion objects since the FMQ is a ring
285 * buffer and a read or write operation can wrap around. A single message
286 * of type T will never be broken between the two MemRegions.
287 */
288 struct MemTransaction {
MemTransactionMessageQueueBase::MemTransaction289 MemTransaction() : MemTransaction(MemRegion(), MemRegion()) {}
290
MemTransactionMessageQueueBase::MemTransaction291 MemTransaction(const MemRegion& regionFirst, const MemRegion& regionSecond)
292 : first(regionFirst), second(regionSecond) {}
293
294 MemTransaction& operator=(const MemTransaction& other) {
295 first = other.first;
296 second = other.second;
297 return *this;
298 }
299
300 /**
301 * Helper method to calculate the address for a particular index for
302 * the MemTransaction object.
303 *
304 * @param idx Index of the slot to be read/written. If the
305 * MemTransaction object is representing the memory region to read/write
306 * N items of type T, the valid range of idx is between 0 and N-1.
307 *
308 * @return Pointer to the slot idx. Will be nullptr for an invalid idx.
309 */
310 T* getSlot(size_t idx);
311
312 /**
313 * Helper method to write 'nMessages' items of type T into the memory
314 * regions described by the object starting from 'startIdx'. This method
315 * uses memcpy() and is not to meant to be used for a zero copy operation.
316 * Partial writes are not supported.
317 *
318 * @param data Pointer to the source buffer.
319 * @param nMessages Number of items of type T.
320 * @param startIdx The slot number to begin the write from. If the
321 * MemTransaction object is representing the memory region to read/write
322 * N items of type T, the valid range of startIdx is between 0 and N-1;
323 *
324 * @return Whether the write operation of size 'nMessages' succeeded.
325 */
326 bool copyTo(const T* data, size_t startIdx, size_t nMessages = 1);
327
328 /*
329 * Helper method to read 'nMessages' items of type T from the memory
330 * regions described by the object starting from 'startIdx'. This method uses
331 * memcpy() and is not meant to be used for a zero copy operation. Partial reads
332 * are not supported.
333 *
334 * @param data Pointer to the destination buffer.
335 * @param nMessages Number of items of type T.
336 * @param startIdx The slot number to begin the read from. If the
337 * MemTransaction object is representing the memory region to read/write
338 * N items of type T, the valid range of startIdx is between 0 and N-1.
339 *
340 * @return Whether the read operation of size 'nMessages' succeeded.
341 */
342 bool copyFrom(T* data, size_t startIdx, size_t nMessages = 1);
343
344 /**
345 * Returns a const reference to the first MemRegion in the
346 * MemTransaction object.
347 */
getFirstRegionMessageQueueBase::MemTransaction348 inline const MemRegion& getFirstRegion() const { return first; }
349
350 /**
351 * Returns a const reference to the second MemRegion in the
352 * MemTransaction object.
353 */
getSecondRegionMessageQueueBase::MemTransaction354 inline const MemRegion& getSecondRegion() const { return second; }
355
356 private:
357 /*
358 * Given a start index and the number of messages to be
359 * read/written, this helper method calculates the
360 * number of messages that should should be written to both the first
361 * and second MemRegions and the base addresses to be used for
362 * the read/write operation.
363 *
364 * Returns false if the 'startIdx' and 'nMessages' is
365 * invalid for the MemTransaction object.
366 */
367 bool inline getMemRegionInfo(size_t idx, size_t nMessages, size_t& firstCount,
368 size_t& secondCount, T** firstBaseAddress,
369 T** secondBaseAddress);
370 MemRegion first;
371 MemRegion second;
372 };
373
374 /**
375 * Get a MemTransaction object to write 'nMessages' items of type T.
376 * Once the write is performed using the information from MemTransaction,
377 * the write operation is to be committed using a call to commitWrite().
378 *
379 * @param nMessages Number of messages of type T.
380 * @param Pointer to MemTransaction struct that describes memory to write 'nMessages'
381 * items of type T. If a write of size 'nMessages' is not possible, the base
382 * addresses in the MemTransaction object would be set to nullptr.
383 *
384 * @return Whether it is possible to write 'nMessages' items of type T
385 * into the FMQ.
386 */
387 bool beginWrite(size_t nMessages, MemTransaction* memTx) const;
388
389 /**
390 * Commit a write of size 'nMessages'. To be only used after a call to beginWrite().
391 *
392 * @param nMessages number of messages of type T to be written.
393 *
394 * @return Whether the write operation of size 'nMessages' succeeded.
395 */
396 bool commitWrite(size_t nMessages);
397
398 /**
399 * Get a MemTransaction object to read 'nMessages' items of type T.
400 * Once the read is performed using the information from MemTransaction,
401 * the read operation is to be committed using a call to commitRead().
402 *
403 * @param nMessages Number of messages of type T.
404 * @param pointer to MemTransaction struct that describes memory to read 'nMessages'
405 * items of type T. If a read of size 'nMessages' is not possible, the base
406 * pointers in the MemTransaction object returned will be set to nullptr.
407 *
408 * @return bool Whether it is possible to read 'nMessages' items of type T
409 * from the FMQ.
410 */
411 bool beginRead(size_t nMessages, MemTransaction* memTx) const;
412
413 /**
414 * Commit a read of size 'nMessages'. To be only used after a call to beginRead().
415 * For the unsynchronized flavor of FMQ, this method will return a failure
416 * if a write overflow happened after beginRead() was invoked.
417 *
418 * @param nMessages number of messages of type T to be read.
419 *
420 * @return bool Whether the read operation of size 'nMessages' succeeded.
421 */
422 bool commitRead(size_t nMessages);
423
424 private:
425 size_t availableToWriteBytes() const;
426 size_t availableToReadBytes() const;
427
428 MessageQueueBase(const MessageQueueBase& other) = delete;
429 MessageQueueBase& operator=(const MessageQueueBase& other) = delete;
430
431 void* mapGrantorDescr(uint32_t grantorIdx);
432 void unmapGrantorDescr(void* address, uint32_t grantorIdx);
433 void initMemory(bool resetPointers);
434
435 enum DefaultEventNotification : uint32_t {
436 /*
437 * These are only used internally by the readBlocking()/writeBlocking()
438 * methods and hence once other bit combinations are not required.
439 */
440 FMQ_NOT_FULL = 0x01,
441 FMQ_NOT_EMPTY = 0x02
442 };
443 std::unique_ptr<Descriptor> mDesc;
444 uint8_t* mRing = nullptr;
445 /*
446 * TODO(b/31550092): Change to 32 bit read and write pointer counters.
447 */
448 std::atomic<uint64_t>* mReadPtr = nullptr;
449 std::atomic<uint64_t>* mWritePtr = nullptr;
450
451 std::atomic<uint32_t>* mEvFlagWord = nullptr;
452
453 /*
454 * This EventFlag object will be owned by the FMQ and will have the same
455 * lifetime.
456 */
457 android::hardware::EventFlag* mEventFlag = nullptr;
458 };
459
460 template <template <typename, MQFlavor> typename MQDescriptorType, typename T, MQFlavor flavor>
getSlot(size_t idx)461 T* MessageQueueBase<MQDescriptorType, T, flavor>::MemTransaction::getSlot(size_t idx) {
462 size_t firstRegionLength = first.getLength();
463 size_t secondRegionLength = second.getLength();
464
465 if (idx > firstRegionLength + secondRegionLength) {
466 return nullptr;
467 }
468
469 if (idx < firstRegionLength) {
470 return first.getAddress() + idx;
471 }
472
473 return second.getAddress() + idx - firstRegionLength;
474 }
475
476 template <template <typename, MQFlavor> typename MQDescriptorType, typename T, MQFlavor flavor>
getMemRegionInfo(size_t startIdx,size_t nMessages,size_t & firstCount,size_t & secondCount,T ** firstBaseAddress,T ** secondBaseAddress)477 bool MessageQueueBase<MQDescriptorType, T, flavor>::MemTransaction::getMemRegionInfo(
478 size_t startIdx, size_t nMessages, size_t& firstCount, size_t& secondCount,
479 T** firstBaseAddress, T** secondBaseAddress) {
480 size_t firstRegionLength = first.getLength();
481 size_t secondRegionLength = second.getLength();
482
483 if (startIdx + nMessages > firstRegionLength + secondRegionLength) {
484 /*
485 * Return false if 'nMessages' starting at 'startIdx' cannot be
486 * accommodated by the MemTransaction object.
487 */
488 return false;
489 }
490
491 /* Number of messages to be read/written to the first MemRegion. */
492 firstCount =
493 startIdx < firstRegionLength ? std::min(nMessages, firstRegionLength - startIdx) : 0;
494
495 /* Number of messages to be read/written to the second MemRegion. */
496 secondCount = nMessages - firstCount;
497
498 if (firstCount != 0) {
499 *firstBaseAddress = first.getAddress() + startIdx;
500 }
501
502 if (secondCount != 0) {
503 size_t secondStartIdx = startIdx > firstRegionLength ? startIdx - firstRegionLength : 0;
504 *secondBaseAddress = second.getAddress() + secondStartIdx;
505 }
506
507 return true;
508 }
509
510 template <template <typename, MQFlavor> typename MQDescriptorType, typename T, MQFlavor flavor>
copyFrom(T * data,size_t startIdx,size_t nMessages)511 bool MessageQueueBase<MQDescriptorType, T, flavor>::MemTransaction::copyFrom(T* data,
512 size_t startIdx,
513 size_t nMessages) {
514 if (data == nullptr) {
515 return false;
516 }
517
518 size_t firstReadCount = 0, secondReadCount = 0;
519 T *firstBaseAddress = nullptr, *secondBaseAddress = nullptr;
520
521 if (getMemRegionInfo(startIdx, nMessages, firstReadCount, secondReadCount, &firstBaseAddress,
522 &secondBaseAddress) == false) {
523 /*
524 * Returns false if 'startIdx' and 'nMessages' are invalid for this
525 * MemTransaction object.
526 */
527 return false;
528 }
529
530 if (firstReadCount != 0) {
531 memcpy(data, firstBaseAddress, firstReadCount * sizeof(T));
532 }
533
534 if (secondReadCount != 0) {
535 memcpy(data + firstReadCount, secondBaseAddress, secondReadCount * sizeof(T));
536 }
537
538 return true;
539 }
540
541 template <template <typename, MQFlavor> typename MQDescriptorType, typename T, MQFlavor flavor>
copyTo(const T * data,size_t startIdx,size_t nMessages)542 bool MessageQueueBase<MQDescriptorType, T, flavor>::MemTransaction::copyTo(const T* data,
543 size_t startIdx,
544 size_t nMessages) {
545 if (data == nullptr) {
546 return false;
547 }
548
549 size_t firstWriteCount = 0, secondWriteCount = 0;
550 T *firstBaseAddress = nullptr, *secondBaseAddress = nullptr;
551
552 if (getMemRegionInfo(startIdx, nMessages, firstWriteCount, secondWriteCount, &firstBaseAddress,
553 &secondBaseAddress) == false) {
554 /*
555 * Returns false if 'startIdx' and 'nMessages' are invalid for this
556 * MemTransaction object.
557 */
558 return false;
559 }
560
561 if (firstWriteCount != 0) {
562 memcpy(firstBaseAddress, data, firstWriteCount * sizeof(T));
563 }
564
565 if (secondWriteCount != 0) {
566 memcpy(secondBaseAddress, data + firstWriteCount, secondWriteCount * sizeof(T));
567 }
568
569 return true;
570 }
571
572 template <template <typename, MQFlavor> typename MQDescriptorType, typename T, MQFlavor flavor>
initMemory(bool resetPointers)573 void MessageQueueBase<MQDescriptorType, T, flavor>::initMemory(bool resetPointers) {
574 /*
575 * Verify that the Descriptor contains the minimum number of grantors
576 * the native_handle is valid and T matches quantum size.
577 */
578 if ((mDesc == nullptr) || !mDesc->isHandleValid() ||
579 (mDesc->countGrantors() < hardware::details::kMinGrantorCount)) {
580 return;
581 }
582 if (mDesc->getQuantum() != sizeof(T)) {
583 hardware::details::logError(
584 "Payload size differs between the queue instantiation and the "
585 "MQDescriptor.");
586 return;
587 }
588
589 if (flavor == kSynchronizedReadWrite) {
590 mReadPtr = reinterpret_cast<std::atomic<uint64_t>*>(
591 mapGrantorDescr(hardware::details::READPTRPOS));
592 } else {
593 /*
594 * The unsynchronized write flavor of the FMQ may have multiple readers
595 * and each reader would have their own read pointer counter.
596 */
597 mReadPtr = new (std::nothrow) std::atomic<uint64_t>;
598 }
599 if (mReadPtr == nullptr) goto error;
600
601 mWritePtr = reinterpret_cast<std::atomic<uint64_t>*>(
602 mapGrantorDescr(hardware::details::WRITEPTRPOS));
603 if (mWritePtr == nullptr) goto error;
604
605 if (resetPointers) {
606 mReadPtr->store(0, std::memory_order_release);
607 mWritePtr->store(0, std::memory_order_release);
608 } else if (flavor != kSynchronizedReadWrite) {
609 // Always reset the read pointer.
610 mReadPtr->store(0, std::memory_order_release);
611 }
612
613 mRing = reinterpret_cast<uint8_t*>(mapGrantorDescr(hardware::details::DATAPTRPOS));
614 if (mRing == nullptr) goto error;
615
616 if (mDesc->countGrantors() > hardware::details::EVFLAGWORDPOS) {
617 mEvFlagWord = static_cast<std::atomic<uint32_t>*>(
618 mapGrantorDescr(hardware::details::EVFLAGWORDPOS));
619 if (mEvFlagWord == nullptr) goto error;
620 android::hardware::EventFlag::createEventFlag(mEvFlagWord, &mEventFlag);
621 }
622 return;
623 error:
624 if (mReadPtr) {
625 if (flavor == kSynchronizedReadWrite) {
626 unmapGrantorDescr(mReadPtr, hardware::details::READPTRPOS);
627 } else {
628 delete mReadPtr;
629 }
630 mReadPtr = nullptr;
631 }
632 if (mWritePtr) {
633 unmapGrantorDescr(mWritePtr, hardware::details::WRITEPTRPOS);
634 mWritePtr = nullptr;
635 }
636 if (mRing) {
637 unmapGrantorDescr(mRing, hardware::details::EVFLAGWORDPOS);
638 mRing = nullptr;
639 }
640 }
641
642 template <template <typename, MQFlavor> typename MQDescriptorType, typename T, MQFlavor flavor>
MessageQueueBase(const Descriptor & Desc,bool resetPointers)643 MessageQueueBase<MQDescriptorType, T, flavor>::MessageQueueBase(const Descriptor& Desc,
644 bool resetPointers) {
645 mDesc = std::unique_ptr<Descriptor>(new (std::nothrow) Descriptor(Desc));
646 if (mDesc == nullptr) {
647 return;
648 }
649
650 initMemory(resetPointers);
651 }
652
653 template <template <typename, MQFlavor> typename MQDescriptorType, typename T, MQFlavor flavor>
MessageQueueBase(size_t numElementsInQueue,bool configureEventFlagWord,android::base::unique_fd bufferFd,size_t bufferSize)654 MessageQueueBase<MQDescriptorType, T, flavor>::MessageQueueBase(size_t numElementsInQueue,
655 bool configureEventFlagWord,
656 android::base::unique_fd bufferFd,
657 size_t bufferSize) {
658 // Check if the buffer size would not overflow size_t
659 if (numElementsInQueue > SIZE_MAX / sizeof(T)) {
660 hardware::details::logError("Requested message queue size too large. Size of elements: " +
661 std::to_string(sizeof(T)) +
662 ". Number of elements: " + std::to_string(numElementsInQueue));
663 return;
664 }
665 if (bufferFd != -1 && numElementsInQueue * sizeof(T) > bufferSize) {
666 hardware::details::logError("The supplied buffer size(" + std::to_string(bufferSize) +
667 ") is smaller than the required size(" +
668 std::to_string(numElementsInQueue * sizeof(T)) + ").");
669 return;
670 }
671 /*
672 * The FMQ needs to allocate memory for the ringbuffer as well as for the
673 * read and write pointer counters. If an EventFlag word is to be configured,
674 * we also need to allocate memory for the same/
675 */
676 size_t kQueueSizeBytes = numElementsInQueue * sizeof(T);
677 size_t kMetaDataSize = 2 * sizeof(android::hardware::details::RingBufferPosition);
678
679 if (configureEventFlagWord) {
680 kMetaDataSize += sizeof(std::atomic<uint32_t>);
681 }
682
683 /*
684 * Ashmem memory region size needs to be specified in page-aligned bytes.
685 * kQueueSizeBytes needs to be aligned to word boundary so that all offsets
686 * in the grantorDescriptor will be word aligned.
687 */
688 size_t kAshmemSizePageAligned;
689 if (bufferFd != -1) {
690 // Allocate read counter and write counter only. User-supplied memory will be used for the
691 // ringbuffer.
692 kAshmemSizePageAligned = (kMetaDataSize + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
693 } else {
694 // Allocate ringbuffer, read counter and write counter.
695 kAshmemSizePageAligned = (hardware::details::alignToWordBoundary(kQueueSizeBytes) +
696 kMetaDataSize + PAGE_SIZE - 1) &
697 ~(PAGE_SIZE - 1);
698 }
699
700 /*
701 * The native handle will contain the fds to be mapped.
702 */
703 int numFds = (bufferFd != -1) ? 2 : 1;
704 native_handle_t* mqHandle = native_handle_create(numFds, 0 /* numInts */);
705 if (mqHandle == nullptr) {
706 return;
707 }
708
709 /*
710 * Create an ashmem region to map the memory.
711 */
712 int ashmemFd = ashmem_create_region("MessageQueue", kAshmemSizePageAligned);
713 ashmem_set_prot_region(ashmemFd, PROT_READ | PROT_WRITE);
714 mqHandle->data[0] = ashmemFd;
715
716 if (bufferFd != -1) {
717 // Use user-supplied file descriptor for fdIndex 1
718 mqHandle->data[1] = bufferFd.get();
719 // release ownership of fd. mqHandle owns it now.
720 if (bufferFd.release() < 0) {
721 hardware::details::logError("Error releasing supplied bufferFd");
722 }
723
724 std::vector<android::hardware::GrantorDescriptor> grantors;
725 grantors.resize(configureEventFlagWord ? hardware::details::kMinGrantorCountForEvFlagSupport
726 : hardware::details::kMinGrantorCount);
727
728 size_t memSize[] = {
729 sizeof(hardware::details::RingBufferPosition), /* memory to be allocated for read
730 pointer counter */
731 sizeof(hardware::details::RingBufferPosition), /* memory to be allocated for write
732 pointer counter */
733 kQueueSizeBytes, /* memory to be allocated for data buffer */
734 sizeof(std::atomic<uint32_t>) /* memory to be allocated for EventFlag word */
735 };
736
737 for (size_t grantorPos = 0, offset = 0; grantorPos < grantors.size(); grantorPos++) {
738 uint32_t grantorFdIndex;
739 size_t grantorOffset;
740 if (grantorPos == hardware::details::DATAPTRPOS) {
741 grantorFdIndex = 1;
742 grantorOffset = 0;
743 } else {
744 grantorFdIndex = 0;
745 grantorOffset = offset;
746 offset += memSize[grantorPos];
747 }
748 grantors[grantorPos] = {
749 0 /* grantor flags */, grantorFdIndex,
750 static_cast<uint32_t>(hardware::details::alignToWordBoundary(grantorOffset)),
751 memSize[grantorPos]};
752 }
753
754 mDesc = std::unique_ptr<Descriptor>(new (std::nothrow)
755 Descriptor(grantors, mqHandle, sizeof(T)));
756 } else {
757 mDesc = std::unique_ptr<Descriptor>(new (std::nothrow) Descriptor(
758 kQueueSizeBytes, mqHandle, sizeof(T), configureEventFlagWord));
759 }
760 if (mDesc == nullptr) {
761 native_handle_close(mqHandle);
762 native_handle_delete(mqHandle);
763 return;
764 }
765 initMemory(true);
766 }
767
768 template <template <typename, MQFlavor> typename MQDescriptorType, typename T, MQFlavor flavor>
~MessageQueueBase()769 MessageQueueBase<MQDescriptorType, T, flavor>::~MessageQueueBase() {
770 if (flavor == kUnsynchronizedWrite && mReadPtr != nullptr) {
771 delete mReadPtr;
772 } else if (mReadPtr != nullptr) {
773 unmapGrantorDescr(mReadPtr, hardware::details::READPTRPOS);
774 }
775 if (mWritePtr != nullptr) {
776 unmapGrantorDescr(mWritePtr, hardware::details::WRITEPTRPOS);
777 }
778 if (mRing != nullptr) {
779 unmapGrantorDescr(mRing, hardware::details::DATAPTRPOS);
780 }
781 if (mEvFlagWord != nullptr) {
782 unmapGrantorDescr(mEvFlagWord, hardware::details::EVFLAGWORDPOS);
783 android::hardware::EventFlag::deleteEventFlag(&mEventFlag);
784 }
785 }
786
787 template <template <typename, MQFlavor> typename MQDescriptorType, typename T, MQFlavor flavor>
write(const T * data)788 bool MessageQueueBase<MQDescriptorType, T, flavor>::write(const T* data) {
789 return write(data, 1);
790 }
791
792 template <template <typename, MQFlavor> typename MQDescriptorType, typename T, MQFlavor flavor>
read(T * data)793 bool MessageQueueBase<MQDescriptorType, T, flavor>::read(T* data) {
794 return read(data, 1);
795 }
796
797 template <template <typename, MQFlavor> typename MQDescriptorType, typename T, MQFlavor flavor>
write(const T * data,size_t nMessages)798 bool MessageQueueBase<MQDescriptorType, T, flavor>::write(const T* data, size_t nMessages) {
799 MemTransaction tx;
800 return beginWrite(nMessages, &tx) && tx.copyTo(data, 0 /* startIdx */, nMessages) &&
801 commitWrite(nMessages);
802 }
803
804 template <template <typename, MQFlavor> typename MQDescriptorType, typename T, MQFlavor flavor>
writeBlocking(const T * data,size_t count,uint32_t readNotification,uint32_t writeNotification,int64_t timeOutNanos,android::hardware::EventFlag * evFlag)805 bool MessageQueueBase<MQDescriptorType, T, flavor>::writeBlocking(
806 const T* data, size_t count, uint32_t readNotification, uint32_t writeNotification,
807 int64_t timeOutNanos, android::hardware::EventFlag* evFlag) {
808 static_assert(flavor == kSynchronizedReadWrite,
809 "writeBlocking can only be used with the "
810 "kSynchronizedReadWrite flavor.");
811 /*
812 * If evFlag is null and the FMQ does not have its own EventFlag object
813 * return false;
814 * If the flavor is kSynchronizedReadWrite and the readNotification
815 * bit mask is zero return false;
816 * If the count is greater than queue size, return false
817 * to prevent blocking until timeOut.
818 */
819 if (evFlag == nullptr) {
820 evFlag = mEventFlag;
821 if (evFlag == nullptr) {
822 hardware::details::logError(
823 "writeBlocking failed: called on MessageQueue with no Eventflag"
824 "configured or provided");
825 return false;
826 }
827 }
828
829 if (readNotification == 0 || (count > getQuantumCount())) {
830 return false;
831 }
832
833 /*
834 * There is no need to wait for a readNotification if there is sufficient
835 * space to write is already present in the FMQ. The latter would be the case when
836 * read operations read more number of messages than write operations write.
837 * In other words, a single large read may clear the FMQ after multiple small
838 * writes. This would fail to clear a pending readNotification bit since
839 * EventFlag bits can only be cleared by a wait() call, however the bit would
840 * be correctly cleared by the next writeBlocking() call.
841 */
842
843 bool result = write(data, count);
844 if (result) {
845 if (writeNotification) {
846 evFlag->wake(writeNotification);
847 }
848 return result;
849 }
850
851 bool shouldTimeOut = timeOutNanos != 0;
852 int64_t prevTimeNanos = shouldTimeOut ? android::elapsedRealtimeNano() : 0;
853
854 while (true) {
855 /* It is not required to adjust 'timeOutNanos' if 'shouldTimeOut' is false */
856 if (shouldTimeOut) {
857 /*
858 * The current time and 'prevTimeNanos' are both CLOCK_BOOTTIME clock values(converted
859 * to Nanoseconds)
860 */
861 int64_t currentTimeNs = android::elapsedRealtimeNano();
862 /*
863 * Decrement 'timeOutNanos' to account for the time taken to complete the last
864 * iteration of the while loop.
865 */
866 timeOutNanos -= currentTimeNs - prevTimeNanos;
867 prevTimeNanos = currentTimeNs;
868
869 if (timeOutNanos <= 0) {
870 /*
871 * Attempt write in case a context switch happened outside of
872 * evFlag->wait().
873 */
874 result = write(data, count);
875 break;
876 }
877 }
878
879 /*
880 * wait() will return immediately if there was a pending read
881 * notification.
882 */
883 uint32_t efState = 0;
884 status_t status = evFlag->wait(readNotification, &efState, timeOutNanos,
885 true /* retry on spurious wake */);
886
887 if (status != android::TIMED_OUT && status != android::NO_ERROR) {
888 hardware::details::logError("Unexpected error code from EventFlag Wait status " +
889 std::to_string(status));
890 break;
891 }
892
893 if (status == android::TIMED_OUT) {
894 break;
895 }
896
897 /*
898 * If there is still insufficient space to write to the FMQ,
899 * keep waiting for another readNotification.
900 */
901 if ((efState & readNotification) && write(data, count)) {
902 result = true;
903 break;
904 }
905 }
906
907 if (result && writeNotification != 0) {
908 evFlag->wake(writeNotification);
909 }
910
911 return result;
912 }
913
914 template <template <typename, MQFlavor> typename MQDescriptorType, typename T, MQFlavor flavor>
writeBlocking(const T * data,size_t count,int64_t timeOutNanos)915 bool MessageQueueBase<MQDescriptorType, T, flavor>::writeBlocking(const T* data, size_t count,
916 int64_t timeOutNanos) {
917 return writeBlocking(data, count, FMQ_NOT_FULL, FMQ_NOT_EMPTY, timeOutNanos);
918 }
919
920 template <template <typename, MQFlavor> typename MQDescriptorType, typename T, MQFlavor flavor>
readBlocking(T * data,size_t count,uint32_t readNotification,uint32_t writeNotification,int64_t timeOutNanos,android::hardware::EventFlag * evFlag)921 bool MessageQueueBase<MQDescriptorType, T, flavor>::readBlocking(
922 T* data, size_t count, uint32_t readNotification, uint32_t writeNotification,
923 int64_t timeOutNanos, android::hardware::EventFlag* evFlag) {
924 static_assert(flavor == kSynchronizedReadWrite,
925 "readBlocking can only be used with the "
926 "kSynchronizedReadWrite flavor.");
927
928 /*
929 * If evFlag is null and the FMQ does not own its own EventFlag object
930 * return false;
931 * If the writeNotification bit mask is zero return false;
932 * If the count is greater than queue size, return false to prevent
933 * blocking until timeOut.
934 */
935 if (evFlag == nullptr) {
936 evFlag = mEventFlag;
937 if (evFlag == nullptr) {
938 hardware::details::logError(
939 "readBlocking failed: called on MessageQueue with no Eventflag"
940 "configured or provided");
941 return false;
942 }
943 }
944
945 if (writeNotification == 0 || count > getQuantumCount()) {
946 return false;
947 }
948
949 /*
950 * There is no need to wait for a write notification if sufficient
951 * data to read is already present in the FMQ. This would be the
952 * case when read operations read lesser number of messages than
953 * a write operation and multiple reads would be required to clear the queue
954 * after a single write operation. This check would fail to clear a pending
955 * writeNotification bit since EventFlag bits can only be cleared
956 * by a wait() call, however the bit would be correctly cleared by the next
957 * readBlocking() call.
958 */
959
960 bool result = read(data, count);
961 if (result) {
962 if (readNotification) {
963 evFlag->wake(readNotification);
964 }
965 return result;
966 }
967
968 bool shouldTimeOut = timeOutNanos != 0;
969 int64_t prevTimeNanos = shouldTimeOut ? android::elapsedRealtimeNano() : 0;
970
971 while (true) {
972 /* It is not required to adjust 'timeOutNanos' if 'shouldTimeOut' is false */
973 if (shouldTimeOut) {
974 /*
975 * The current time and 'prevTimeNanos' are both CLOCK_BOOTTIME clock values(converted
976 * to Nanoseconds)
977 */
978 int64_t currentTimeNs = android::elapsedRealtimeNano();
979 /*
980 * Decrement 'timeOutNanos' to account for the time taken to complete the last
981 * iteration of the while loop.
982 */
983 timeOutNanos -= currentTimeNs - prevTimeNanos;
984 prevTimeNanos = currentTimeNs;
985
986 if (timeOutNanos <= 0) {
987 /*
988 * Attempt read in case a context switch happened outside of
989 * evFlag->wait().
990 */
991 result = read(data, count);
992 break;
993 }
994 }
995
996 /*
997 * wait() will return immediately if there was a pending write
998 * notification.
999 */
1000 uint32_t efState = 0;
1001 status_t status = evFlag->wait(writeNotification, &efState, timeOutNanos,
1002 true /* retry on spurious wake */);
1003
1004 if (status != android::TIMED_OUT && status != android::NO_ERROR) {
1005 hardware::details::logError("Unexpected error code from EventFlag Wait status " +
1006 std::to_string(status));
1007 break;
1008 }
1009
1010 if (status == android::TIMED_OUT) {
1011 break;
1012 }
1013
1014 /*
1015 * If the data in FMQ is still insufficient, go back to waiting
1016 * for another write notification.
1017 */
1018 if ((efState & writeNotification) && read(data, count)) {
1019 result = true;
1020 break;
1021 }
1022 }
1023
1024 if (result && readNotification != 0) {
1025 evFlag->wake(readNotification);
1026 }
1027 return result;
1028 }
1029
1030 template <template <typename, MQFlavor> typename MQDescriptorType, typename T, MQFlavor flavor>
readBlocking(T * data,size_t count,int64_t timeOutNanos)1031 bool MessageQueueBase<MQDescriptorType, T, flavor>::readBlocking(T* data, size_t count,
1032 int64_t timeOutNanos) {
1033 return readBlocking(data, count, FMQ_NOT_FULL, FMQ_NOT_EMPTY, timeOutNanos);
1034 }
1035
1036 template <template <typename, MQFlavor> typename MQDescriptorType, typename T, MQFlavor flavor>
availableToWriteBytes()1037 size_t MessageQueueBase<MQDescriptorType, T, flavor>::availableToWriteBytes() const {
1038 return mDesc->getSize() - availableToReadBytes();
1039 }
1040
1041 template <template <typename, MQFlavor> typename MQDescriptorType, typename T, MQFlavor flavor>
availableToWrite()1042 size_t MessageQueueBase<MQDescriptorType, T, flavor>::availableToWrite() const {
1043 return availableToWriteBytes() / sizeof(T);
1044 }
1045
1046 template <template <typename, MQFlavor> typename MQDescriptorType, typename T, MQFlavor flavor>
availableToRead()1047 size_t MessageQueueBase<MQDescriptorType, T, flavor>::availableToRead() const {
1048 return availableToReadBytes() / sizeof(T);
1049 }
1050
1051 template <template <typename, MQFlavor> typename MQDescriptorType, typename T, MQFlavor flavor>
beginWrite(size_t nMessages,MemTransaction * result)1052 bool MessageQueueBase<MQDescriptorType, T, flavor>::beginWrite(size_t nMessages,
1053 MemTransaction* result) const {
1054 /*
1055 * If nMessages is greater than size of FMQ or in case of the synchronized
1056 * FMQ flavor, if there is not enough space to write nMessages, then return
1057 * result with null addresses.
1058 */
1059 if ((flavor == kSynchronizedReadWrite && (availableToWrite() < nMessages)) ||
1060 nMessages > getQuantumCount()) {
1061 *result = MemTransaction();
1062 return false;
1063 }
1064
1065 auto writePtr = mWritePtr->load(std::memory_order_relaxed);
1066 if (writePtr % sizeof(T) != 0) {
1067 hardware::details::logError(
1068 "The write pointer has become misaligned. Writing to the queue is no longer "
1069 "possible.");
1070 hardware::details::errorWriteLog(0x534e4554, "184963385");
1071 return false;
1072 }
1073 size_t writeOffset = writePtr % mDesc->getSize();
1074
1075 /*
1076 * From writeOffset, the number of messages that can be written
1077 * contiguously without wrapping around the ring buffer are calculated.
1078 */
1079 size_t contiguousMessages = (mDesc->getSize() - writeOffset) / sizeof(T);
1080
1081 if (contiguousMessages < nMessages) {
1082 /*
1083 * Wrap around is required. Both result.first and result.second are
1084 * populated.
1085 */
1086 *result = MemTransaction(
1087 MemRegion(reinterpret_cast<T*>(mRing + writeOffset), contiguousMessages),
1088 MemRegion(reinterpret_cast<T*>(mRing), nMessages - contiguousMessages));
1089 } else {
1090 /*
1091 * A wrap around is not required to write nMessages. Only result.first
1092 * is populated.
1093 */
1094 *result = MemTransaction(MemRegion(reinterpret_cast<T*>(mRing + writeOffset), nMessages),
1095 MemRegion());
1096 }
1097
1098 return true;
1099 }
1100
1101 template <template <typename, MQFlavor> typename MQDescriptorType, typename T, MQFlavor flavor>
1102 /*
1103 * Disable integer sanitization since integer overflow here is allowed
1104 * and legal.
1105 */
1106 __attribute__((no_sanitize("integer"))) bool
commitWrite(size_t nMessages)1107 MessageQueueBase<MQDescriptorType, T, flavor>::commitWrite(size_t nMessages) {
1108 size_t nBytesWritten = nMessages * sizeof(T);
1109 auto writePtr = mWritePtr->load(std::memory_order_relaxed);
1110 writePtr += nBytesWritten;
1111 mWritePtr->store(writePtr, std::memory_order_release);
1112 /*
1113 * This method cannot fail now since we are only incrementing the writePtr
1114 * counter.
1115 */
1116 return true;
1117 }
1118
1119 template <template <typename, MQFlavor> typename MQDescriptorType, typename T, MQFlavor flavor>
availableToReadBytes()1120 size_t MessageQueueBase<MQDescriptorType, T, flavor>::availableToReadBytes() const {
1121 /*
1122 * This method is invoked by implementations of both read() and write() and
1123 * hence requires a memory_order_acquired load for both mReadPtr and
1124 * mWritePtr.
1125 */
1126 return mWritePtr->load(std::memory_order_acquire) - mReadPtr->load(std::memory_order_acquire);
1127 }
1128
1129 template <template <typename, MQFlavor> typename MQDescriptorType, typename T, MQFlavor flavor>
read(T * data,size_t nMessages)1130 bool MessageQueueBase<MQDescriptorType, T, flavor>::read(T* data, size_t nMessages) {
1131 MemTransaction tx;
1132 return beginRead(nMessages, &tx) && tx.copyFrom(data, 0 /* startIdx */, nMessages) &&
1133 commitRead(nMessages);
1134 }
1135
1136 template <template <typename, MQFlavor> typename MQDescriptorType, typename T, MQFlavor flavor>
1137 /*
1138 * Disable integer sanitization since integer overflow here is allowed
1139 * and legal.
1140 */
1141 __attribute__((no_sanitize("integer"))) bool
beginRead(size_t nMessages,MemTransaction * result)1142 MessageQueueBase<MQDescriptorType, T, flavor>::beginRead(size_t nMessages,
1143 MemTransaction* result) const {
1144 *result = MemTransaction();
1145 /*
1146 * If it is detected that the data in the queue was overwritten
1147 * due to the reader process being too slow, the read pointer counter
1148 * is set to the same as the write pointer counter to indicate error
1149 * and the read returns false;
1150 * Need acquire/release memory ordering for mWritePtr.
1151 */
1152 auto writePtr = mWritePtr->load(std::memory_order_acquire);
1153 /*
1154 * A relaxed load is sufficient for mReadPtr since there will be no
1155 * stores to mReadPtr from a different thread.
1156 */
1157 auto readPtr = mReadPtr->load(std::memory_order_relaxed);
1158 if (writePtr % sizeof(T) != 0 || readPtr % sizeof(T) != 0) {
1159 hardware::details::logError(
1160 "The write or read pointer has become misaligned. Reading from the queue is no "
1161 "longer possible.");
1162 hardware::details::errorWriteLog(0x534e4554, "184963385");
1163 return false;
1164 }
1165
1166 if (writePtr - readPtr > mDesc->getSize()) {
1167 mReadPtr->store(writePtr, std::memory_order_release);
1168 return false;
1169 }
1170
1171 size_t nBytesDesired = nMessages * sizeof(T);
1172 /*
1173 * Return if insufficient data to read in FMQ.
1174 */
1175 if (writePtr - readPtr < nBytesDesired) {
1176 return false;
1177 }
1178
1179 size_t readOffset = readPtr % mDesc->getSize();
1180 /*
1181 * From readOffset, the number of messages that can be read contiguously
1182 * without wrapping around the ring buffer are calculated.
1183 */
1184 size_t contiguousMessages = (mDesc->getSize() - readOffset) / sizeof(T);
1185
1186 if (contiguousMessages < nMessages) {
1187 /*
1188 * A wrap around is required. Both result.first and result.second
1189 * are populated.
1190 */
1191 *result = MemTransaction(
1192 MemRegion(reinterpret_cast<T*>(mRing + readOffset), contiguousMessages),
1193 MemRegion(reinterpret_cast<T*>(mRing), nMessages - contiguousMessages));
1194 } else {
1195 /*
1196 * A wrap around is not required. Only result.first need to be
1197 * populated.
1198 */
1199 *result = MemTransaction(MemRegion(reinterpret_cast<T*>(mRing + readOffset), nMessages),
1200 MemRegion());
1201 }
1202
1203 return true;
1204 }
1205
1206 template <template <typename, MQFlavor> typename MQDescriptorType, typename T, MQFlavor flavor>
1207 /*
1208 * Disable integer sanitization since integer overflow here is allowed
1209 * and legal.
1210 */
1211 __attribute__((no_sanitize("integer"))) bool
commitRead(size_t nMessages)1212 MessageQueueBase<MQDescriptorType, T, flavor>::commitRead(size_t nMessages) {
1213 // TODO: Use a local copy of readPtr to avoid relazed mReadPtr loads.
1214 auto readPtr = mReadPtr->load(std::memory_order_relaxed);
1215 auto writePtr = mWritePtr->load(std::memory_order_acquire);
1216 /*
1217 * If the flavor is unsynchronized, it is possible that a write overflow may
1218 * have occurred between beginRead() and commitRead().
1219 */
1220 if (writePtr - readPtr > mDesc->getSize()) {
1221 mReadPtr->store(writePtr, std::memory_order_release);
1222 return false;
1223 }
1224
1225 size_t nBytesRead = nMessages * sizeof(T);
1226 readPtr += nBytesRead;
1227 mReadPtr->store(readPtr, std::memory_order_release);
1228 return true;
1229 }
1230
1231 template <template <typename, MQFlavor> typename MQDescriptorType, typename T, MQFlavor flavor>
getQuantumSize()1232 size_t MessageQueueBase<MQDescriptorType, T, flavor>::getQuantumSize() const {
1233 return mDesc->getQuantum();
1234 }
1235
1236 template <template <typename, MQFlavor> typename MQDescriptorType, typename T, MQFlavor flavor>
getQuantumCount()1237 size_t MessageQueueBase<MQDescriptorType, T, flavor>::getQuantumCount() const {
1238 return mDesc->getSize() / mDesc->getQuantum();
1239 }
1240
1241 template <template <typename, MQFlavor> typename MQDescriptorType, typename T, MQFlavor flavor>
isValid()1242 bool MessageQueueBase<MQDescriptorType, T, flavor>::isValid() const {
1243 return mRing != nullptr && mReadPtr != nullptr && mWritePtr != nullptr;
1244 }
1245
1246 template <template <typename, MQFlavor> typename MQDescriptorType, typename T, MQFlavor flavor>
mapGrantorDescr(uint32_t grantorIdx)1247 void* MessageQueueBase<MQDescriptorType, T, flavor>::mapGrantorDescr(uint32_t grantorIdx) {
1248 const native_handle_t* handle = mDesc->handle();
1249 const std::vector<android::hardware::GrantorDescriptor> grantors = mDesc->grantors();
1250 if (handle == nullptr) {
1251 hardware::details::logError("mDesc->handle is null");
1252 return nullptr;
1253 }
1254
1255 if (grantorIdx >= grantors.size()) {
1256 hardware::details::logError(std::string("grantorIdx must be less than ") +
1257 std::to_string(grantors.size()));
1258 return nullptr;
1259 }
1260
1261 int fdIndex = grantors[grantorIdx].fdIndex;
1262 if (fdIndex < 0 || fdIndex >= handle->numFds) {
1263 hardware::details::logError(
1264 std::string("fdIndex (" + std::to_string(fdIndex) + ") from grantor (index " +
1265 std::to_string(grantorIdx) +
1266 ") must be smaller than the number of fds in the handle: " +
1267 std::to_string(handle->numFds)));
1268 return nullptr;
1269 }
1270
1271 /*
1272 * Offset for mmap must be a multiple of PAGE_SIZE.
1273 */
1274 if (!hardware::details::isAlignedToWordBoundary(grantors[grantorIdx].offset)) {
1275 hardware::details::logError("Grantor (index " + std::to_string(grantorIdx) +
1276 ") offset needs to be aligned to word boundary but is: " +
1277 std::to_string(grantors[grantorIdx].offset));
1278 return nullptr;
1279 }
1280
1281 int mapOffset = (grantors[grantorIdx].offset / PAGE_SIZE) * PAGE_SIZE;
1282 if (grantors[grantorIdx].extent < 0 || grantors[grantorIdx].extent > INT_MAX - PAGE_SIZE) {
1283 hardware::details::logError(std::string("Grantor (index " + std::to_string(grantorIdx) +
1284 ") extent value is too large or negative: " +
1285 std::to_string(grantors[grantorIdx].extent)));
1286 return nullptr;
1287 }
1288 int mapLength = grantors[grantorIdx].offset - mapOffset + grantors[grantorIdx].extent;
1289
1290 void* address = mmap(0, mapLength, PROT_READ | PROT_WRITE, MAP_SHARED, handle->data[fdIndex],
1291 mapOffset);
1292 if (address == MAP_FAILED) {
1293 hardware::details::logError(std::string("mmap failed: ") + std::to_string(errno));
1294 return nullptr;
1295 }
1296 return reinterpret_cast<uint8_t*>(address) + (grantors[grantorIdx].offset - mapOffset);
1297 }
1298
1299 template <template <typename, MQFlavor> typename MQDescriptorType, typename T, MQFlavor flavor>
unmapGrantorDescr(void * address,uint32_t grantorIdx)1300 void MessageQueueBase<MQDescriptorType, T, flavor>::unmapGrantorDescr(void* address,
1301 uint32_t grantorIdx) {
1302 auto grantors = mDesc->grantors();
1303 if ((address == nullptr) || (grantorIdx >= grantors.size())) {
1304 return;
1305 }
1306
1307 int mapOffset = (grantors[grantorIdx].offset / PAGE_SIZE) * PAGE_SIZE;
1308 int mapLength = grantors[grantorIdx].offset - mapOffset + grantors[grantorIdx].extent;
1309 void* baseAddress =
1310 reinterpret_cast<uint8_t*>(address) - (grantors[grantorIdx].offset - mapOffset);
1311 if (baseAddress) munmap(baseAddress, mapLength);
1312 }
1313
1314 } // namespace hardware
1315