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 
26 // For the given data key, look up the layer_data instance from given layer_data_map
27 template <typename DATA_T>
GetLayerDataPtr(void * data_key,std::unordered_map<void *,DATA_T * > & layer_data_map)28 DATA_T *GetLayerDataPtr(void *data_key, std::unordered_map<void *, DATA_T *> &layer_data_map) {
29     DATA_T *debug_data;
30     typename std::unordered_map<void *, DATA_T *>::const_iterator got;
31 
32     /* TODO: We probably should lock here, or have caller lock */
33     got = layer_data_map.find(data_key);
34 
35     if (got == layer_data_map.end()) {
36         debug_data = new DATA_T;
37         layer_data_map[(void *)data_key] = debug_data;
38     } else {
39         debug_data = got->second;
40     }
41 
42     return debug_data;
43 }
44 
45 template <typename DATA_T>
FreeLayerDataPtr(void * data_key,std::unordered_map<void *,DATA_T * > & layer_data_map)46 void FreeLayerDataPtr(void *data_key, std::unordered_map<void *, DATA_T *> &layer_data_map) {
47     auto got = layer_data_map.find(data_key);
48     assert(got != layer_data_map.end());
49 
50     delete got->second;
51     layer_data_map.erase(got);
52 }
53 
54 #endif  // LAYER_DATA_H
55