Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 17 | #ifndef UTILITY_HANDLE_TRACKER_H |
| 18 | #define UTILITY_HANDLE_TRACKER_H |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 19 | |
| 20 | #include <stdint.h> |
Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 21 | #include <string> |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 22 | #include <utils/Mutex.h> |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 23 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 24 | typedef int32_t aaudio_handle_t; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 25 | typedef int32_t handle_tracker_type_t; // what kind of handle |
| 26 | typedef int32_t handle_tracker_slot_t; // index in allocation table |
| 27 | typedef int32_t handle_tracker_generation_t; // incremented when slot used |
| 28 | typedef uint16_t handle_tracker_header_t; // combines type and generation |
| 29 | typedef void *handle_tracker_address_t; // address of something that is stored here |
| 30 | |
| 31 | #define HANDLE_TRACKER_MAX_TYPES (1 << 3) |
| 32 | #define HANDLE_TRACKER_MAX_HANDLES (1 << 16) |
| 33 | |
| 34 | /** |
| 35 | * Represent Objects using an integer handle that can be used with Java. |
| 36 | * This also makes the 'C' ABI more robust. |
| 37 | * |
| 38 | * Note that this should only be called from a single thread. |
| 39 | * If you call it from more than one thread then you need to use your own mutex. |
| 40 | */ |
| 41 | class HandleTracker { |
| 42 | |
| 43 | public: |
| 44 | /** |
| 45 | * @param maxHandles cannot exceed HANDLE_TRACKER_MAX_HANDLES |
| 46 | */ |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 47 | HandleTracker(uint32_t maxHandles = 256); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 48 | virtual ~HandleTracker(); |
| 49 | |
| 50 | /** |
| 51 | * Don't use if this returns false; |
| 52 | * @return true if the internal allocation succeeded |
| 53 | */ |
| 54 | bool isInitialized() const; |
| 55 | |
| 56 | /** |
Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 57 | * Returns HandleTracker information. |
| 58 | * |
| 59 | * Will attempt to get the object lock, but will proceed |
| 60 | * even if it cannot. |
| 61 | * |
| 62 | * Each line of information ends with a newline. |
| 63 | * |
| 64 | * @return a string representing the HandleTracker info. |
| 65 | */ |
| 66 | std::string dump() const; |
| 67 | |
| 68 | /** |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 69 | * Store a pointer and return a handle that can be used to retrieve the pointer. |
| 70 | * |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 71 | * It is safe to call put() or remove() from multiple threads. |
| 72 | * |
Glenn Kasten | 3205f5e | 2017-01-10 14:24:17 -0800 | [diff] [blame] | 73 | * @param expectedType the type of the object to be tracked |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 74 | * @param address pointer to be converted to a handle |
| 75 | * @return a valid handle or a negative error |
| 76 | */ |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 77 | aaudio_handle_t put(handle_tracker_type_t expectedType, handle_tracker_address_t address); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * Get the original pointer associated with the handle. |
| 81 | * The handle will be validated to prevent stale handles from being reused. |
| 82 | * Note that the validation is designed to prevent common coding errors and not |
| 83 | * to prevent deliberate hacking. |
| 84 | * |
| 85 | * @param expectedType shouldmatch the type we passed to put() |
| 86 | * @param handle to be converted to a pointer |
| 87 | * @return address associated with handle or nullptr |
| 88 | */ |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 89 | handle_tracker_address_t get(handle_tracker_type_t expectedType, aaudio_handle_t handle) const; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 90 | |
| 91 | /** |
| 92 | * Free up the storage associated with the handle. |
| 93 | * Subsequent attempts to use the handle will fail. |
| 94 | * |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 95 | * Do NOT remove() a handle while get() is being called for the same handle from another thread. |
| 96 | * |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 97 | * @param expectedType shouldmatch the type we passed to put() |
| 98 | * @param handle to be removed from tracking |
| 99 | * @return address associated with handle or nullptr if not found |
| 100 | */ |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 101 | handle_tracker_address_t remove(handle_tracker_type_t expectedType, aaudio_handle_t handle); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 102 | |
| 103 | private: |
| 104 | const int32_t mMaxHandleCount; // size of array |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 105 | // This address is const after initialization. |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 106 | handle_tracker_address_t * mHandleAddresses; // address of objects or a free linked list node |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 107 | // This address is const after initialization. |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 108 | handle_tracker_header_t * mHandleHeaders; // combination of type and generation |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 109 | // head of the linked list of free nodes in mHandleAddresses |
| 110 | handle_tracker_address_t * mNextFreeAddress; |
| 111 | |
| 112 | // This Mutex protects the linked list of free nodes. |
| 113 | // The list is managed using mHandleAddresses and mNextFreeAddress. |
| 114 | // The data in mHandleHeaders is only changed by put() and remove(). |
Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 115 | mutable android::Mutex mLock; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 116 | |
| 117 | /** |
| 118 | * Pull slot off of a list of empty slots. |
| 119 | * @return index or a negative error |
| 120 | */ |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 121 | handle_tracker_slot_t allocateSlot_l(); |
| 122 | |
| 123 | /** |
| 124 | * Increment the generation for the slot, avoiding zero. |
| 125 | */ |
| 126 | handle_tracker_generation_t nextGeneration_l(handle_tracker_slot_t index); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 127 | |
| 128 | /** |
| 129 | * Validate the handle and return the corresponding index. |
| 130 | * @return slot index or a negative error |
| 131 | */ |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 132 | handle_tracker_slot_t handleToIndex(aaudio_handle_t handle, handle_tracker_type_t type) const; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 133 | |
| 134 | /** |
| 135 | * Construct a handle from a header and an index. |
| 136 | * @param header combination of a type and a generation |
| 137 | * @param index slot index returned from allocateSlot |
| 138 | * @return handle or a negative error |
| 139 | */ |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 140 | static aaudio_handle_t buildHandle(handle_tracker_header_t header, handle_tracker_slot_t index); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * Combine a type and a generation field into a header. |
| 144 | */ |
| 145 | static handle_tracker_header_t buildHeader(handle_tracker_type_t type, |
| 146 | handle_tracker_generation_t generation); |
| 147 | |
| 148 | /** |
| 149 | * Extract the index from a handle. |
| 150 | * Does not validate the handle. |
| 151 | * @return index associated with a handle |
| 152 | */ |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 153 | static handle_tracker_slot_t extractIndex(aaudio_handle_t handle); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 154 | |
| 155 | /** |
| 156 | * Extract the generation from a handle. |
| 157 | * Does not validate the handle. |
| 158 | * @return generation associated with a handle |
| 159 | */ |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 160 | static handle_tracker_generation_t extractGeneration(aaudio_handle_t handle); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 161 | |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 162 | }; |
| 163 | |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 164 | #endif //UTILITY_HANDLE_TRACKER_H |