• 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 "vrend_object.h"
30 
31 struct vrend_object_types {
32    void (*unref)(void *);
33 } obj_types[VIRGL_MAX_OBJECTS];
34 
35 static void (*resource_unref)(void *);
36 
vrend_object_set_destroy_callback(int type,void (* cb)(void *))37 void vrend_object_set_destroy_callback(int type, void (*cb)(void *))
38 {
39    obj_types[type].unref = cb;
40 }
41 
vrend_resource_set_destroy_callback(void (* cb)(void *))42 void vrend_resource_set_destroy_callback(void (*cb)(void *))
43 {
44    resource_unref = cb;
45 }
46 
47 static unsigned
hash_func(void * key)48 hash_func(void *key)
49 {
50    intptr_t ip = pointer_to_intptr(key);
51    return (unsigned)(ip & 0xffffffff);
52 }
53 
54 static int
compare(void * key1,void * key2)55 compare(void *key1, void *key2)
56 {
57    if (key1 < key2)
58       return -1;
59    if (key1 > key2)
60       return 1;
61    else
62       return 0;
63 }
64 
65 static struct util_hash_table *res_hash;
66 
67 struct vrend_object {
68    enum virgl_object_type type;
69    uint32_t handle;
70    void *data;
71    bool free_data;
72 };
73 
free_object(void * value)74 static void free_object(void *value)
75 {
76    struct vrend_object *obj = value;
77 
78    if (obj->free_data) {
79       if (obj_types[obj->type].unref)
80          obj_types[obj->type].unref(obj->data);
81       else {
82          /* for objects with no callback just free them */
83          free(obj->data);
84       }
85    }
86    free(obj);
87 }
88 
vrend_object_init_ctx_table(void)89 struct util_hash_table *vrend_object_init_ctx_table(void)
90 {
91    struct util_hash_table *ctx_hash;
92    ctx_hash = util_hash_table_create(hash_func, compare, free_object);
93    return ctx_hash;
94 }
95 
vrend_object_fini_ctx_table(struct util_hash_table * ctx_hash)96 void vrend_object_fini_ctx_table(struct util_hash_table *ctx_hash)
97 {
98    if (!ctx_hash)
99       return;
100 
101    util_hash_table_destroy(ctx_hash);
102 }
103 
free_res(void * value)104 static void free_res(void *value)
105 {
106    struct vrend_object *obj = value;
107    (*resource_unref)(obj->data);
108    free(obj);
109 }
110 
111 void
vrend_object_init_resource_table(void)112 vrend_object_init_resource_table(void)
113 {
114    if (!res_hash)
115       res_hash = util_hash_table_create(hash_func, compare, free_res);
116 }
117 
vrend_object_fini_resource_table(void)118 void vrend_object_fini_resource_table(void)
119 {
120    if (res_hash) {
121       util_hash_table_destroy(res_hash);
122    }
123    res_hash = NULL;
124 }
125 
126 uint32_t
vrend_object_insert_nofree(struct util_hash_table * handle_hash,void * data,UNUSED uint32_t length,uint32_t handle,enum virgl_object_type type,bool free_data)127 vrend_object_insert_nofree(struct util_hash_table *handle_hash,
128                            void *data, UNUSED uint32_t length, uint32_t handle,
129                            enum virgl_object_type type, bool free_data)
130 {
131    struct vrend_object *obj = CALLOC_STRUCT(vrend_object);
132 
133    if (!obj)
134       return 0;
135    obj->handle = handle;
136    obj->data = data;
137    obj->type = type;
138    obj->free_data = free_data;
139    util_hash_table_set(handle_hash, intptr_to_pointer(obj->handle), obj);
140    return obj->handle;
141 }
142 
143 uint32_t
vrend_object_insert(struct util_hash_table * handle_hash,void * data,uint32_t length,uint32_t handle,enum virgl_object_type type)144 vrend_object_insert(struct util_hash_table *handle_hash,
145                     void *data, uint32_t length, uint32_t handle, enum virgl_object_type type)
146 {
147    return vrend_object_insert_nofree(handle_hash, data, length,
148                                      handle, type, true);
149 }
150 
151 void
vrend_object_remove(struct util_hash_table * handle_hash,uint32_t handle,UNUSED enum virgl_object_type type)152 vrend_object_remove(struct util_hash_table *handle_hash,
153                     uint32_t handle, UNUSED enum virgl_object_type type)
154 {
155    util_hash_table_remove(handle_hash, intptr_to_pointer(handle));
156 }
157 
vrend_object_lookup(struct util_hash_table * handle_hash,uint32_t handle,enum virgl_object_type type)158 void *vrend_object_lookup(struct util_hash_table *handle_hash,
159                           uint32_t handle, enum virgl_object_type type)
160 {
161    struct vrend_object *obj;
162 
163    obj = util_hash_table_get(handle_hash, intptr_to_pointer(handle));
164    if (!obj) {
165       return NULL;
166    }
167 
168    if (obj->type != type)
169       return NULL;
170    return obj->data;
171 }
172 
vrend_resource_insert(void * data,uint32_t handle)173 int vrend_resource_insert(void *data, uint32_t handle)
174 {
175    struct vrend_object *obj;
176 
177    if (!handle)
178       return 0;
179 
180    obj = CALLOC_STRUCT(vrend_object);
181    if (!obj)
182       return 0;
183 
184    obj->handle = handle;
185    obj->data = data;
186    util_hash_table_set(res_hash, intptr_to_pointer(obj->handle), obj);
187    return obj->handle;
188 }
189 
vrend_resource_remove(uint32_t handle)190 void vrend_resource_remove(uint32_t handle)
191 {
192    util_hash_table_remove(res_hash, intptr_to_pointer(handle));
193 }
194 
vrend_resource_lookup(uint32_t handle,UNUSED uint32_t ctx_id)195 void *vrend_resource_lookup(uint32_t handle, UNUSED uint32_t ctx_id)
196 {
197    struct vrend_object *obj;
198    obj = util_hash_table_get(res_hash, intptr_to_pointer(handle));
199    if (!obj)
200       return NULL;
201    return obj->data;
202 }
203