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