blob: c80860c038a482c32fbd6df7752054483f0531df [file] [log] [blame]
Phil Burke1ce4912016-11-21 10:40:25 -08001/*
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 Burkdec33ab2017-01-17 14:48:16 -080017#ifndef UTILITY_HANDLE_TRACKER_H
18#define UTILITY_HANDLE_TRACKER_H
Phil Burke1ce4912016-11-21 10:40:25 -080019
20#include <stdint.h>
Phil Burkdec33ab2017-01-17 14:48:16 -080021#include <utils/Mutex.h>
Phil Burke1ce4912016-11-21 10:40:25 -080022
23typedef int32_t handle_tracker_type_t; // what kind of handle
24typedef int32_t handle_tracker_slot_t; // index in allocation table
25typedef int32_t handle_tracker_generation_t; // incremented when slot used
26typedef uint16_t handle_tracker_header_t; // combines type and generation
27typedef void *handle_tracker_address_t; // address of something that is stored here
28
29#define HANDLE_TRACKER_MAX_TYPES (1 << 3)
30#define HANDLE_TRACKER_MAX_HANDLES (1 << 16)
31
32/**
33 * Represent Objects using an integer handle that can be used with Java.
34 * This also makes the 'C' ABI more robust.
35 *
36 * Note that this should only be called from a single thread.
37 * If you call it from more than one thread then you need to use your own mutex.
38 */
39class HandleTracker {
40
41public:
42 /**
43 * @param maxHandles cannot exceed HANDLE_TRACKER_MAX_HANDLES
44 */
Phil Burk204a1632017-01-03 17:23:43 -080045 HandleTracker(uint32_t maxHandles = 256);
Phil Burke1ce4912016-11-21 10:40:25 -080046 virtual ~HandleTracker();
47
48 /**
49 * Don't use if this returns false;
50 * @return true if the internal allocation succeeded
51 */
52 bool isInitialized() const;
53
54 /**
55 * Store a pointer and return a handle that can be used to retrieve the pointer.
56 *
Phil Burkdec33ab2017-01-17 14:48:16 -080057 * It is safe to call put() or remove() from multiple threads.
58 *
Glenn Kasten3205f5e2017-01-10 14:24:17 -080059 * @param expectedType the type of the object to be tracked
Phil Burke1ce4912016-11-21 10:40:25 -080060 * @param address pointer to be converted to a handle
61 * @return a valid handle or a negative error
62 */
Phil Burk5ed503c2017-02-01 09:38:15 -080063 aaudio_handle_t put(handle_tracker_type_t expectedType, handle_tracker_address_t address);
Phil Burke1ce4912016-11-21 10:40:25 -080064
65 /**
66 * Get the original pointer associated with the handle.
67 * The handle will be validated to prevent stale handles from being reused.
68 * Note that the validation is designed to prevent common coding errors and not
69 * to prevent deliberate hacking.
70 *
71 * @param expectedType shouldmatch the type we passed to put()
72 * @param handle to be converted to a pointer
73 * @return address associated with handle or nullptr
74 */
Phil Burk5ed503c2017-02-01 09:38:15 -080075 handle_tracker_address_t get(handle_tracker_type_t expectedType, aaudio_handle_t handle) const;
Phil Burke1ce4912016-11-21 10:40:25 -080076
77 /**
78 * Free up the storage associated with the handle.
79 * Subsequent attempts to use the handle will fail.
80 *
Phil Burkdec33ab2017-01-17 14:48:16 -080081 * Do NOT remove() a handle while get() is being called for the same handle from another thread.
82 *
Phil Burke1ce4912016-11-21 10:40:25 -080083 * @param expectedType shouldmatch the type we passed to put()
84 * @param handle to be removed from tracking
85 * @return address associated with handle or nullptr if not found
86 */
Phil Burk5ed503c2017-02-01 09:38:15 -080087 handle_tracker_address_t remove(handle_tracker_type_t expectedType, aaudio_handle_t handle);
Phil Burke1ce4912016-11-21 10:40:25 -080088
89private:
90 const int32_t mMaxHandleCount; // size of array
Phil Burkdec33ab2017-01-17 14:48:16 -080091 // This address is const after initialization.
Phil Burke1ce4912016-11-21 10:40:25 -080092 handle_tracker_address_t * mHandleAddresses; // address of objects or a free linked list node
Phil Burkdec33ab2017-01-17 14:48:16 -080093 // This address is const after initialization.
Phil Burke1ce4912016-11-21 10:40:25 -080094 handle_tracker_header_t * mHandleHeaders; // combination of type and generation
Phil Burkdec33ab2017-01-17 14:48:16 -080095 // head of the linked list of free nodes in mHandleAddresses
96 handle_tracker_address_t * mNextFreeAddress;
97
98 // This Mutex protects the linked list of free nodes.
99 // The list is managed using mHandleAddresses and mNextFreeAddress.
100 // The data in mHandleHeaders is only changed by put() and remove().
101 android::Mutex mLock;
Phil Burke1ce4912016-11-21 10:40:25 -0800102
103 /**
104 * Pull slot off of a list of empty slots.
105 * @return index or a negative error
106 */
Phil Burkdec33ab2017-01-17 14:48:16 -0800107 handle_tracker_slot_t allocateSlot_l();
108
109 /**
110 * Increment the generation for the slot, avoiding zero.
111 */
112 handle_tracker_generation_t nextGeneration_l(handle_tracker_slot_t index);
Phil Burke1ce4912016-11-21 10:40:25 -0800113
114 /**
115 * Validate the handle and return the corresponding index.
116 * @return slot index or a negative error
117 */
Phil Burk5ed503c2017-02-01 09:38:15 -0800118 handle_tracker_slot_t handleToIndex(aaudio_handle_t handle, handle_tracker_type_t type) const;
Phil Burke1ce4912016-11-21 10:40:25 -0800119
120 /**
121 * Construct a handle from a header and an index.
122 * @param header combination of a type and a generation
123 * @param index slot index returned from allocateSlot
124 * @return handle or a negative error
125 */
Phil Burk5ed503c2017-02-01 09:38:15 -0800126 static aaudio_handle_t buildHandle(handle_tracker_header_t header, handle_tracker_slot_t index);
Phil Burke1ce4912016-11-21 10:40:25 -0800127
128 /**
129 * Combine a type and a generation field into a header.
130 */
131 static handle_tracker_header_t buildHeader(handle_tracker_type_t type,
132 handle_tracker_generation_t generation);
133
134 /**
135 * Extract the index from a handle.
136 * Does not validate the handle.
137 * @return index associated with a handle
138 */
Phil Burk5ed503c2017-02-01 09:38:15 -0800139 static handle_tracker_slot_t extractIndex(aaudio_handle_t handle);
Phil Burke1ce4912016-11-21 10:40:25 -0800140
141 /**
142 * Extract the generation from a handle.
143 * Does not validate the handle.
144 * @return generation associated with a handle
145 */
Phil Burk5ed503c2017-02-01 09:38:15 -0800146 static handle_tracker_generation_t extractGeneration(aaudio_handle_t handle);
Phil Burke1ce4912016-11-21 10:40:25 -0800147
Phil Burke1ce4912016-11-21 10:40:25 -0800148};
149
Phil Burkdec33ab2017-01-17 14:48:16 -0800150#endif //UTILITY_HANDLE_TRACKER_H