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