| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -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 |  | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 18 | #define LOG_TAG "AAudio" | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 19 | //#define LOG_NDEBUG 0 | 
 | 20 | #include <utils/Log.h> | 
 | 21 |  | 
| Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 22 | #include <assert.h> | 
| Phil Burk | d8bdcab | 2017-01-03 17:20:30 -0800 | [diff] [blame] | 23 | #include <new> | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 24 | #include <stdint.h> | 
| Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 25 | #include <utils/Mutex.h> | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 26 |  | 
| Phil Burk | a4eb0d8 | 2017-04-12 15:44:06 -0700 | [diff] [blame] | 27 | #include <aaudio/AAudio.h> | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 28 | #include "HandleTracker.h" | 
 | 29 |  | 
| Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 30 | using android::Mutex; | 
 | 31 |  | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 32 | // Handle format is: tgggiiii | 
 | 33 | // where each letter is 4 bits, t=type, g=generation, i=index | 
 | 34 |  | 
 | 35 | #define TYPE_SIZE           4 | 
 | 36 | #define GENERATION_SIZE    12 | 
 | 37 | #define INDEX_SIZE         16 | 
 | 38 |  | 
 | 39 | #define GENERATION_INVALID  0 | 
 | 40 | #define GENERATION_SHIFT    INDEX_SIZE | 
 | 41 |  | 
 | 42 | #define TYPE_MASK           ((1 << TYPE_SIZE) - 1) | 
 | 43 | #define GENERATION_MASK     ((1 << GENERATION_SIZE) - 1) | 
 | 44 | #define INDEX_MASK          ((1 << INDEX_SIZE) - 1) | 
 | 45 |  | 
 | 46 | #define SLOT_UNAVAILABLE    (-1) | 
 | 47 |  | 
 | 48 | // Error if handle is negative so type is limited to bottom half. | 
 | 49 | #define HANDLE_INVALID_TYPE TYPE_MASK | 
 | 50 |  | 
 | 51 | static_assert(HANDLE_TRACKER_MAX_TYPES == (1 << (TYPE_SIZE - 1)), | 
 | 52 |     "Mismatch between header and cpp."); | 
 | 53 | static_assert(HANDLE_TRACKER_MAX_HANDLES == (1 << (INDEX_SIZE)), | 
 | 54 |     "Mismatch between header and cpp."); | 
 | 55 |  | 
 | 56 | HandleTracker::HandleTracker(uint32_t maxHandles) | 
 | 57 |         : mMaxHandleCount(maxHandles) | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 58 |         , mHandleHeaders(nullptr) | 
 | 59 | { | 
 | 60 |     assert(maxHandles <= HANDLE_TRACKER_MAX_HANDLES); | 
 | 61 |     // Allocate arrays to hold addresses and validation info. | 
| Phil Burk | d8bdcab | 2017-01-03 17:20:30 -0800 | [diff] [blame] | 62 |     mHandleAddresses = (handle_tracker_address_t *) | 
 | 63 |             new(std::nothrow) handle_tracker_address_t[maxHandles]; | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 64 |     if (mHandleAddresses != nullptr) { | 
| Phil Burk | d8bdcab | 2017-01-03 17:20:30 -0800 | [diff] [blame] | 65 |         mHandleHeaders = new(std::nothrow) handle_tracker_header_t[maxHandles]; | 
 | 66 |  | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 67 |         if (mHandleHeaders != nullptr) { | 
| Phil Burk | d8bdcab | 2017-01-03 17:20:30 -0800 | [diff] [blame] | 68 |             handle_tracker_header_t initialHeader = buildHeader(0, 1); | 
 | 69 |             // Initialize linked list of free nodes. nullptr terminated. | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 70 |             for (uint32_t i = 0; i < (maxHandles - 1); i++) { | 
 | 71 |                 mHandleAddresses[i] = &mHandleAddresses[i + 1]; // point to next node | 
| Phil Burk | d8bdcab | 2017-01-03 17:20:30 -0800 | [diff] [blame] | 72 |                 mHandleHeaders[i] = initialHeader; | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 73 |             } | 
 | 74 |             mNextFreeAddress = &mHandleAddresses[0]; | 
 | 75 |             mHandleAddresses[maxHandles - 1] = nullptr; | 
 | 76 |             mHandleHeaders[maxHandles - 1] = 0; | 
 | 77 |         } else { | 
 | 78 |             delete[] mHandleAddresses; // so the class appears uninitialized | 
| Phil Burk | d8bdcab | 2017-01-03 17:20:30 -0800 | [diff] [blame] | 79 |             mHandleAddresses = nullptr; | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 80 |         } | 
 | 81 |     } | 
 | 82 | } | 
 | 83 |  | 
 | 84 | HandleTracker::~HandleTracker() | 
 | 85 | { | 
| Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 86 |     Mutex::Autolock _l(mLock); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 87 |     delete[] mHandleAddresses; | 
 | 88 |     delete[] mHandleHeaders; | 
| Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 89 |     mHandleAddresses = nullptr; | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 90 | } | 
 | 91 |  | 
 | 92 | bool HandleTracker::isInitialized() const { | 
 | 93 |     return mHandleAddresses != nullptr; | 
 | 94 | } | 
 | 95 |  | 
| Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 96 | handle_tracker_slot_t HandleTracker::allocateSlot_l() { | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 97 |     void **allocated = mNextFreeAddress; | 
 | 98 |     if (allocated == nullptr) { | 
 | 99 |         return SLOT_UNAVAILABLE; | 
 | 100 |     } | 
 | 101 |     // Remove this slot from the head of the linked list. | 
 | 102 |     mNextFreeAddress = (void **) *allocated; | 
 | 103 |     return (allocated - mHandleAddresses); | 
 | 104 | } | 
 | 105 |  | 
| Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 106 | handle_tracker_generation_t HandleTracker::nextGeneration_l(handle_tracker_slot_t index) { | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 107 |     handle_tracker_generation_t generation = (mHandleHeaders[index] + 1) & GENERATION_MASK; | 
 | 108 |     // Avoid generation zero so that 0x0 is not a valid handle. | 
 | 109 |     if (generation == GENERATION_INVALID) { | 
 | 110 |         generation++; | 
 | 111 |     } | 
 | 112 |     return generation; | 
 | 113 | } | 
 | 114 |  | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 115 | aaudio_handle_t HandleTracker::put(handle_tracker_type_t type, void *address) | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 116 | { | 
 | 117 |     if (type < 0 || type >= HANDLE_TRACKER_MAX_TYPES) { | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 118 |         return static_cast<aaudio_handle_t>(AAUDIO_ERROR_OUT_OF_RANGE); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 119 |     } | 
 | 120 |     if (!isInitialized()) { | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 121 |         return static_cast<aaudio_handle_t>(AAUDIO_ERROR_NO_MEMORY); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 122 |     } | 
 | 123 |  | 
| Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 124 |     Mutex::Autolock _l(mLock); | 
 | 125 |  | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 126 |     // Find an empty slot. | 
| Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 127 |     handle_tracker_slot_t index = allocateSlot_l(); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 128 |     if (index == SLOT_UNAVAILABLE) { | 
 | 129 |         ALOGE("HandleTracker::put() no room for more handles"); | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 130 |         return static_cast<aaudio_handle_t>(AAUDIO_ERROR_NO_FREE_HANDLES); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 131 |     } | 
 | 132 |  | 
 | 133 |     // Cycle the generation counter so stale handles can be detected. | 
| Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 134 |     handle_tracker_generation_t generation = nextGeneration_l(index); // reads header table | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 135 |     handle_tracker_header_t inputHeader = buildHeader(type, generation); | 
 | 136 |  | 
 | 137 |     // These two writes may need to be observed by other threads or cores during get(). | 
 | 138 |     mHandleHeaders[index] = inputHeader; | 
 | 139 |     mHandleAddresses[index] = address; | 
 | 140 |     // TODO use store release to enforce memory order with get() | 
 | 141 |  | 
 | 142 |     // Generate a handle. | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 143 |     aaudio_handle_t handle = buildHandle(inputHeader, index); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 144 |  | 
| Phil Burk | d8bdcab | 2017-01-03 17:20:30 -0800 | [diff] [blame] | 145 |     ALOGV("HandleTracker::put(%p) returns 0x%08x", address, handle); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 146 |     return handle; | 
 | 147 | } | 
 | 148 |  | 
 | 149 | handle_tracker_slot_t HandleTracker::handleToIndex(handle_tracker_type_t type, | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 150 |                                                    aaudio_handle_t handle) const | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 151 | { | 
 | 152 |     // Validate the handle. | 
 | 153 |     handle_tracker_slot_t index = extractIndex(handle); | 
 | 154 |     if (index >= mMaxHandleCount) { | 
 | 155 |         ALOGE("HandleTracker::handleToIndex() invalid handle = 0x%08X", handle); | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 156 |         return static_cast<aaudio_handle_t>(AAUDIO_ERROR_INVALID_HANDLE); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 157 |     } | 
 | 158 |     handle_tracker_generation_t handleGeneration = extractGeneration(handle); | 
 | 159 |     handle_tracker_header_t inputHeader = buildHeader(type, handleGeneration); | 
| Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 160 |     // We do not need to synchronize this access to mHandleHeaders because it is constant for | 
 | 161 |     // the lifetime of the handle. | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 162 |     if (inputHeader != mHandleHeaders[index]) { | 
 | 163 |         ALOGE("HandleTracker::handleToIndex() inputHeader = 0x%08x != mHandleHeaders[%d] = 0x%08x", | 
 | 164 |              inputHeader, index, mHandleHeaders[index]); | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 165 |         return static_cast<aaudio_handle_t>(AAUDIO_ERROR_INVALID_HANDLE); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 166 |     } | 
 | 167 |     return index; | 
 | 168 | } | 
 | 169 |  | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 170 | handle_tracker_address_t HandleTracker::get(handle_tracker_type_t type, aaudio_handle_t handle) const | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 171 | { | 
 | 172 |     if (!isInitialized()) { | 
 | 173 |         return nullptr; | 
 | 174 |     } | 
 | 175 |     handle_tracker_slot_t index = handleToIndex(type, handle); | 
 | 176 |     if (index >= 0) { | 
| Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 177 |         // We do not need to synchronize this access to mHandleHeaders because this slot | 
 | 178 |         // is allocated and, therefore, not part of the linked list of free slots. | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 179 |         return mHandleAddresses[index]; | 
 | 180 |     } else { | 
 | 181 |         return nullptr; | 
 | 182 |     } | 
 | 183 | } | 
 | 184 |  | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 185 | handle_tracker_address_t HandleTracker::remove(handle_tracker_type_t type, aaudio_handle_t handle) { | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 186 |     if (!isInitialized()) { | 
 | 187 |         return nullptr; | 
 | 188 |     } | 
| Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 189 |  | 
 | 190 |     Mutex::Autolock _l(mLock); | 
 | 191 |  | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 192 |     handle_tracker_slot_t index = handleToIndex(type,handle); | 
 | 193 |     if (index >= 0) { | 
 | 194 |         handle_tracker_address_t address = mHandleAddresses[index]; | 
 | 195 |  | 
 | 196 |         // Invalidate the header type but preserve the generation count. | 
 | 197 |         handle_tracker_generation_t generation = mHandleHeaders[index] & GENERATION_MASK; | 
 | 198 |         handle_tracker_header_t inputHeader = buildHeader( | 
 | 199 |                 (handle_tracker_type_t) HANDLE_INVALID_TYPE, generation); | 
 | 200 |         mHandleHeaders[index] = inputHeader; | 
 | 201 |  | 
 | 202 |         // Add this slot to the head of the linked list. | 
 | 203 |         mHandleAddresses[index] = mNextFreeAddress; | 
 | 204 |         mNextFreeAddress = (handle_tracker_address_t *) &mHandleAddresses[index]; | 
 | 205 |         return address; | 
 | 206 |     } else { | 
 | 207 |         return nullptr; | 
 | 208 |     } | 
 | 209 | } | 
 | 210 |  | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 211 | aaudio_handle_t HandleTracker::buildHandle(handle_tracker_header_t typeGeneration, | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 212 |                                          handle_tracker_slot_t index) { | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 213 |     return (aaudio_handle_t)((typeGeneration << GENERATION_SHIFT) | (index & INDEX_MASK)); | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 214 | } | 
 | 215 |  | 
 | 216 | handle_tracker_header_t HandleTracker::buildHeader(handle_tracker_type_t type, | 
 | 217 |                                     handle_tracker_generation_t generation) | 
 | 218 | { | 
 | 219 |     return (handle_tracker_header_t) (((type & TYPE_MASK) << GENERATION_SIZE) | 
 | 220 |         | (generation & GENERATION_MASK)); | 
 | 221 | } | 
 | 222 |  | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 223 | handle_tracker_slot_t HandleTracker::extractIndex(aaudio_handle_t handle) | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 224 | { | 
 | 225 |     return handle & INDEX_MASK; | 
 | 226 | } | 
 | 227 |  | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 228 | handle_tracker_generation_t HandleTracker::extractGeneration(aaudio_handle_t handle) | 
| Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 229 | { | 
 | 230 |     return (handle >> GENERATION_SHIFT) & GENERATION_MASK; | 
 | 231 | } |