xref: /aosp_15_r20/external/vulkan-validation-layers/layers/vk_layer_data.h (revision b7893ccf7851cd6a48cc5a1e965257d8a5cdcc70)
1*b7893ccfSSadaf Ebrahimi /* Copyright (c) 2015-2017 The Khronos Group Inc.
2*b7893ccfSSadaf Ebrahimi  * Copyright (c) 2015-2017 Valve Corporation
3*b7893ccfSSadaf Ebrahimi  * Copyright (c) 2015-2017 LunarG, Inc.
4*b7893ccfSSadaf Ebrahimi  *
5*b7893ccfSSadaf Ebrahimi  * Licensed under the Apache License, Version 2.0 (the "License");
6*b7893ccfSSadaf Ebrahimi  * you may not use this file except in compliance with the License.
7*b7893ccfSSadaf Ebrahimi  * You may obtain a copy of the License at
8*b7893ccfSSadaf Ebrahimi  *
9*b7893ccfSSadaf Ebrahimi  *     http://www.apache.org/licenses/LICENSE-2.0
10*b7893ccfSSadaf Ebrahimi  *
11*b7893ccfSSadaf Ebrahimi  * Unless required by applicable law or agreed to in writing, software
12*b7893ccfSSadaf Ebrahimi  * distributed under the License is distributed on an "AS IS" BASIS,
13*b7893ccfSSadaf Ebrahimi  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*b7893ccfSSadaf Ebrahimi  * See the License for the specific language governing permissions and
15*b7893ccfSSadaf Ebrahimi  * limitations under the License.
16*b7893ccfSSadaf Ebrahimi  *
17*b7893ccfSSadaf Ebrahimi  * Author: Tobin Ehlis <[email protected]>
18*b7893ccfSSadaf Ebrahimi  */
19*b7893ccfSSadaf Ebrahimi 
20*b7893ccfSSadaf Ebrahimi #ifndef LAYER_DATA_H
21*b7893ccfSSadaf Ebrahimi #define LAYER_DATA_H
22*b7893ccfSSadaf Ebrahimi 
23*b7893ccfSSadaf Ebrahimi #include <cassert>
24*b7893ccfSSadaf Ebrahimi #include <unordered_map>
25*b7893ccfSSadaf Ebrahimi 
26*b7893ccfSSadaf Ebrahimi // For the given data key, look up the layer_data instance from given layer_data_map
27*b7893ccfSSadaf Ebrahimi template <typename DATA_T>
GetLayerDataPtr(void * data_key,std::unordered_map<void *,DATA_T * > & layer_data_map)28*b7893ccfSSadaf Ebrahimi DATA_T *GetLayerDataPtr(void *data_key, std::unordered_map<void *, DATA_T *> &layer_data_map) {
29*b7893ccfSSadaf Ebrahimi     DATA_T *debug_data;
30*b7893ccfSSadaf Ebrahimi     typename std::unordered_map<void *, DATA_T *>::const_iterator got;
31*b7893ccfSSadaf Ebrahimi 
32*b7893ccfSSadaf Ebrahimi     /* TODO: We probably should lock here, or have caller lock */
33*b7893ccfSSadaf Ebrahimi     got = layer_data_map.find(data_key);
34*b7893ccfSSadaf Ebrahimi 
35*b7893ccfSSadaf Ebrahimi     if (got == layer_data_map.end()) {
36*b7893ccfSSadaf Ebrahimi         debug_data = new DATA_T;
37*b7893ccfSSadaf Ebrahimi         layer_data_map[(void *)data_key] = debug_data;
38*b7893ccfSSadaf Ebrahimi     } else {
39*b7893ccfSSadaf Ebrahimi         debug_data = got->second;
40*b7893ccfSSadaf Ebrahimi     }
41*b7893ccfSSadaf Ebrahimi 
42*b7893ccfSSadaf Ebrahimi     return debug_data;
43*b7893ccfSSadaf Ebrahimi }
44*b7893ccfSSadaf Ebrahimi 
45*b7893ccfSSadaf Ebrahimi template <typename DATA_T>
FreeLayerDataPtr(void * data_key,std::unordered_map<void *,DATA_T * > & layer_data_map)46*b7893ccfSSadaf Ebrahimi void FreeLayerDataPtr(void *data_key, std::unordered_map<void *, DATA_T *> &layer_data_map) {
47*b7893ccfSSadaf Ebrahimi     auto got = layer_data_map.find(data_key);
48*b7893ccfSSadaf Ebrahimi     assert(got != layer_data_map.end());
49*b7893ccfSSadaf Ebrahimi 
50*b7893ccfSSadaf Ebrahimi     delete got->second;
51*b7893ccfSSadaf Ebrahimi     layer_data_map.erase(got);
52*b7893ccfSSadaf Ebrahimi }
53*b7893ccfSSadaf Ebrahimi 
54*b7893ccfSSadaf Ebrahimi #endif  // LAYER_DATA_H
55