1 /* 2 * Copyright (C) 2020 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 <hidl/MQDescriptor.h> 20 #include "MessageQueueBase.h" 21 22 namespace android { 23 namespace hardware { 24 25 template <typename T, MQFlavor flavor> 26 struct MessageQueue final : public MessageQueueBase<MQDescriptor, T, flavor> { 27 typedef MQDescriptor<T, flavor> Descriptor; 28 MessageQueue(const Descriptor& Desc, bool resetPointers = true) 29 : MessageQueueBase<MQDescriptor, T, flavor>(Desc, resetPointers) {} 30 ~MessageQueue() = default; 31 32 /** 33 * This constructor uses Ashmem shared memory to create an FMQ 34 * that can contain a maximum of 'numElementsInQueue' elements of type T. 35 * 36 * @param numElementsInQueue Capacity of the MessageQueue in terms of T. 37 * @param configureEventFlagWord Boolean that specifies if memory should 38 * also be allocated and mapped for an EventFlag word. 39 * @param bufferFd User-supplied file descriptor to map the memory for the ringbuffer 40 * By default, bufferFd=-1 means library will allocate ashmem region for ringbuffer. 41 * MessageQueue takes ownership of the file descriptor. 42 * @param bufferSize size of buffer in bytes that bufferFd represents. This 43 * size must be larger than or equal to (numElementsInQueue * sizeof(T)). 44 * Otherwise, operations will cause out-of-bounds memory access. 45 */ MessageQueuefinal46 MessageQueue(size_t numElementsInQueue, bool configureEventFlagWord, 47 android::base::unique_fd bufferFd, size_t bufferSize) 48 : MessageQueueBase<MQDescriptor, T, flavor>(numElementsInQueue, configureEventFlagWord, 49 std::move(bufferFd), bufferSize) {} 50 51 MessageQueue(size_t numElementsInQueue, bool configureEventFlagWord = false) 52 : MessageQueueBase<MQDescriptor, T, flavor>(numElementsInQueue, configureEventFlagWord, 53 android::base::unique_fd(), 0) {} 54 55 private: 56 MessageQueue(const MessageQueue& other) = delete; 57 MessageQueue& operator=(const MessageQueue& other) = delete; 58 MessageQueue() = delete; 59 }; 60 61 } // namespace hardware 62 } // namespace android 63