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