• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**************************************************************************
2  *
3  * Copyright (C) 2014 Red Hat Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included
13  * in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  **************************************************************************/
24 
25 #include "util/u_pointer.h"
26 #include "util/u_memory.h"
27 #include "util/u_hash_table.h"
28 
29 #include "virgl_util.h"
30 #include "vrend_object.h"
31 
32 struct vrend_object_types {
33    void (*unref)(void *);
34 } obj_types[VIRGL_MAX_OBJECTS];
35 
vrend_object_set_destroy_callback(int type,void (* cb)(void *))36 void vrend_object_set_destroy_callback(int type, void (*cb)(void *))
37 {
38    obj_types[type].unref = cb;
39 }
40 
41 struct vrend_object {
42    enum virgl_object_type type;
43    uint32_t handle;
44    void *data;
45 };
46 
free_object(void * value)47 static void free_object(void *value)
48 {
49    struct vrend_object *obj = value;
50 
51    if (obj_types[obj->type].unref)
52       obj_types[obj->type].unref(obj->data);
53    else {
54       /* for objects with no callback just free them */
55       free(obj->data);
56    }
57 
58    free(obj);
59 }
60 
vrend_object_init_ctx_table(void)61 struct util_hash_table *vrend_object_init_ctx_table(void)
62 {
63    struct util_hash_table *ctx_hash;
64    ctx_hash = util_hash_table_create(hash_func_u32, compare_func, free_object);
65    return ctx_hash;
66 }
67 
vrend_object_fini_ctx_table(struct util_hash_table * ctx_hash)68 void vrend_object_fini_ctx_table(struct util_hash_table *ctx_hash)
69 {
70    if (!ctx_hash)
71       return;
72 
73    util_hash_table_destroy(ctx_hash);
74 }
75 
vrend_ctx_resource_destroy_func(UNUSED void * val)76 static void vrend_ctx_resource_destroy_func(UNUSED void *val)
77 {
78    /* we don't own a reference of vrend_resource */
79 }
80 
81 struct util_hash_table *
vrend_ctx_resource_init_table(void)82 vrend_ctx_resource_init_table(void)
83 {
84    return util_hash_table_create(hash_func_u32,
85                                  compare_func,
86                                  vrend_ctx_resource_destroy_func);
87 }
88 
vrend_ctx_resource_fini_table(struct util_hash_table * res_hash)89 void vrend_ctx_resource_fini_table(struct util_hash_table *res_hash)
90 {
91    util_hash_table_destroy(res_hash);
92 }
93 
94 uint32_t
vrend_object_insert(struct util_hash_table * handle_hash,void * data,uint32_t handle,enum virgl_object_type type)95 vrend_object_insert(struct util_hash_table *handle_hash,
96                     void *data, uint32_t handle,
97                     enum virgl_object_type type)
98 {
99    struct vrend_object *obj = CALLOC_STRUCT(vrend_object);
100 
101    if (!obj)
102       return 0;
103    obj->handle = handle;
104    obj->data = data;
105    obj->type = type;
106    util_hash_table_set(handle_hash, intptr_to_pointer(obj->handle), obj);
107    return obj->handle;
108 }
109 
110 void
vrend_object_remove(struct util_hash_table * handle_hash,uint32_t handle,UNUSED enum virgl_object_type type)111 vrend_object_remove(struct util_hash_table *handle_hash,
112                     uint32_t handle, UNUSED enum virgl_object_type type)
113 {
114    util_hash_table_remove(handle_hash, intptr_to_pointer(handle));
115 }
116 
vrend_object_lookup(struct util_hash_table * handle_hash,uint32_t handle,enum virgl_object_type type)117 void *vrend_object_lookup(struct util_hash_table *handle_hash,
118                           uint32_t handle, enum virgl_object_type type)
119 {
120    struct vrend_object *obj;
121 
122    obj = util_hash_table_get(handle_hash, intptr_to_pointer(handle));
123    if (!obj) {
124       return NULL;
125    }
126 
127    if (obj->type != type)
128       return NULL;
129    return obj->data;
130 }
131 
vrend_ctx_resource_insert(struct util_hash_table * res_hash,uint32_t res_id,struct vrend_resource * res)132 void vrend_ctx_resource_insert(struct util_hash_table *res_hash,
133                                uint32_t res_id,
134                                struct vrend_resource *res)
135 {
136    util_hash_table_set(res_hash, uintptr_to_pointer(res_id), res);
137 }
138 
vrend_ctx_resource_remove(struct util_hash_table * res_hash,uint32_t res_id)139 void vrend_ctx_resource_remove(struct util_hash_table *res_hash,
140                                uint32_t res_id)
141 {
142    util_hash_table_remove(res_hash, uintptr_to_pointer(res_id));
143 }
144 
vrend_ctx_resource_lookup(struct util_hash_table * res_hash,uint32_t res_id)145 struct vrend_resource *vrend_ctx_resource_lookup(struct util_hash_table *res_hash,
146                                                  uint32_t res_id)
147 {
148    return util_hash_table_get(res_hash, uintptr_to_pointer(res_id));
149 }
150