SharedRegionParcelable.cpp revision e1ce491a25faf06fdeab00dd938515f71f28b095
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 17#ifndef UTILITY_HANDLETRACKER_H 18#define UTILITY_HANDLETRACKER_H 19 20#include <stdint.h> 21 22typedef int32_t handle_tracker_type_t; // what kind of handle 23typedef int32_t handle_tracker_slot_t; // index in allocation table 24typedef int32_t handle_tracker_generation_t; // incremented when slot used 25typedef uint16_t handle_tracker_header_t; // combines type and generation 26typedef void *handle_tracker_address_t; // address of something that is stored here 27 28#define HANDLE_TRACKER_MAX_TYPES (1 << 3) 29#define HANDLE_TRACKER_MAX_HANDLES (1 << 16) 30 31/** 32 * Represent Objects using an integer handle that can be used with Java. 33 * This also makes the 'C' ABI more robust. 34 * 35 * Note that this should only be called from a single thread. 36 * If you call it from more than one thread then you need to use your own mutex. 37 */ 38class HandleTracker { 39 40public: 41 /** 42 * @param maxHandles cannot exceed HANDLE_TRACKER_MAX_HANDLES 43 */ 44 HandleTracker(uint32_t maxHandles); 45 virtual ~HandleTracker(); 46 47 /** 48 * Don't use if this returns false; 49 * @return true if the internal allocation succeeded 50 */ 51 bool isInitialized() const; 52 53 /** 54 * Store a pointer and return a handle that can be used to retrieve the pointer. 55 * 56 * @param type the type of the object to be tracked 57 * @param address pointer to be converted to a handle 58 * @return a valid handle or a negative error 59 */ 60 oboe_handle_t put(handle_tracker_type_t expectedType, handle_tracker_address_t address); 61 62 /** 63 * Get the original pointer associated with the handle. 64 * The handle will be validated to prevent stale handles from being reused. 65 * Note that the validation is designed to prevent common coding errors and not 66 * to prevent deliberate hacking. 67 * 68 * @param expectedType shouldmatch the type we passed to put() 69 * @param handle to be converted to a pointer 70 * @return address associated with handle or nullptr 71 */ 72 handle_tracker_address_t get(handle_tracker_type_t expectedType, oboe_handle_t handle) const; 73 74 /** 75 * Free up the storage associated with the handle. 76 * Subsequent attempts to use the handle will fail. 77 * 78 * @param expectedType shouldmatch the type we passed to put() 79 * @param handle to be removed from tracking 80 * @return address associated with handle or nullptr if not found 81 */ 82 handle_tracker_address_t remove(handle_tracker_type_t expectedType, oboe_handle_t handle); 83 84private: 85 const int32_t mMaxHandleCount; // size of array 86 // This is const after initialization. 87 handle_tracker_address_t * mHandleAddresses; // address of objects or a free linked list node 88 // This is const after initialization. 89 handle_tracker_header_t * mHandleHeaders; // combination of type and generation 90 handle_tracker_address_t * mNextFreeAddress; // head of the linked list of free nodes in mHandleAddresses 91 92 /** 93 * Pull slot off of a list of empty slots. 94 * @return index or a negative error 95 */ 96 handle_tracker_slot_t allocateSlot(); 97 98 /** 99 * Validate the handle and return the corresponding index. 100 * @return slot index or a negative error 101 */ 102 handle_tracker_slot_t handleToIndex(oboe_handle_t handle, handle_tracker_type_t type) const; 103 104 /** 105 * Construct a handle from a header and an index. 106 * @param header combination of a type and a generation 107 * @param index slot index returned from allocateSlot 108 * @return handle or a negative error 109 */ 110 oboe_handle_t buildHandle(handle_tracker_header_t header, handle_tracker_slot_t index); 111 112 /** 113 * Combine a type and a generation field into a header. 114 */ 115 static handle_tracker_header_t buildHeader(handle_tracker_type_t type, 116 handle_tracker_generation_t generation); 117 118 /** 119 * Extract the index from a handle. 120 * Does not validate the handle. 121 * @return index associated with a handle 122 */ 123 static handle_tracker_slot_t extractIndex(oboe_handle_t handle); 124 125 /** 126 * Extract the generation from a handle. 127 * Does not validate the handle. 128 * @return generation associated with a handle 129 */ 130 static handle_tracker_generation_t extractGeneration(oboe_handle_t handle); 131 132 /** 133 * Increment the generation for the slot, avoiding zero. 134 */ 135 handle_tracker_generation_t nextGeneration(handle_tracker_slot_t index); 136 137}; 138 139#endif //UTILITY_HANDLETRACKER_H 140