vrend_object.c 5.03 KB
Newer Older
Dave Airlie's avatar
Dave Airlie committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/**************************************************************************
 *
 * Copyright (C) 2014 Red Hat Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

25 26 27 28 29 30 31 32 33 34
#include "util/u_pointer.h"
#include "util/u_memory.h"
#include "util/u_hash_table.h"

#include "vrend_object.h"

struct vrend_object_types {
   void (*unref)(void *);
} obj_types[VIRGL_MAX_OBJECTS];

35 36
static void (*resource_unref)(void *);

37
void vrend_object_set_destroy_callback(int type, void (*cb)(void *))
38 39 40 41
{
   obj_types[type].unref = cb;
}

42 43 44 45 46
void vrend_resource_set_destroy_callback(void (*cb)(void *))
{
   resource_unref = cb;
}

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
static unsigned
hash_func(void *key)
{
   intptr_t ip = pointer_to_intptr(key);
   return (unsigned)(ip & 0xffffffff);
}

static int
compare(void *key1, void *key2)
{
   if (key1 < key2)
      return -1;
   if (key1 > key2)
      return 1;
   else
      return 0;
}

static struct util_hash_table *res_hash;

struct vrend_object {
   enum virgl_object_type type;
   uint32_t handle;
   void *data;
Dave Airlie's avatar
Dave Airlie committed
71
   bool free_data;
72 73
};

74
static void free_object(void *value)
75
{
76
   struct vrend_object *obj = value;
77

Dave Airlie's avatar
Dave Airlie committed
78 79 80 81 82 83 84
   if (obj->free_data) {
      if (obj_types[obj->type].unref)
         obj_types[obj->type].unref(obj->data);
      else {
         /* for objects with no callback just free them */
         free(obj->data);
      }
85 86 87 88
   }
   free(obj);
}

89
struct util_hash_table *vrend_object_init_ctx_table(void)
90
{
91 92 93
   struct util_hash_table *ctx_hash;
   ctx_hash = util_hash_table_create(hash_func, compare, free_object);
   return ctx_hash;
94 95 96 97 98 99
}

void vrend_object_fini_ctx_table(struct util_hash_table *ctx_hash)
{
   if (!ctx_hash)
      return;
100

101 102 103
   util_hash_table_destroy(ctx_hash);
}

104
static void free_res(void *value)
105 106 107 108
{
   struct vrend_object *obj = value;
   (*resource_unref)(obj->data);
   free(obj);
109 110 111 112 113 114 115
}

void
vrend_object_init_resource_table(void)
{
   if (!res_hash)
      res_hash = util_hash_table_create(hash_func, compare, free_res);
116 117
}

118 119
void vrend_object_fini_resource_table(void)
{
120
   if (res_hash) {
121
      util_hash_table_destroy(res_hash);
122
   }
123 124 125 126
   res_hash = NULL;
}

uint32_t
Dave Airlie's avatar
Dave Airlie committed
127 128
vrend_object_insert_nofree(struct util_hash_table *handle_hash,
                           void *data, uint32_t length, uint32_t handle, enum virgl_object_type type, bool free_data)
129 130 131 132 133 134 135 136
{
   struct vrend_object *obj = CALLOC_STRUCT(vrend_object);

   if (!obj)
      return 0;
   obj->handle = handle;
   obj->data = data;
   obj->type = type;
Dave Airlie's avatar
Dave Airlie committed
137
   obj->free_data = free_data;
138 139 140 141
   util_hash_table_set(handle_hash, intptr_to_pointer(obj->handle), obj);
   return obj->handle;
}

Dave Airlie's avatar
Dave Airlie committed
142 143 144 145 146 147 148 149
uint32_t
vrend_object_insert(struct util_hash_table *handle_hash,
                    void *data, uint32_t length, uint32_t handle, enum virgl_object_type type)
{
   return vrend_object_insert_nofree(handle_hash, data, length,
                                     handle, type, true);
}

150 151 152 153 154 155 156 157 158
void
vrend_object_remove(struct util_hash_table *handle_hash,
                    uint32_t handle, enum virgl_object_type type)
{
   util_hash_table_remove(handle_hash, intptr_to_pointer(handle));

}

void *vrend_object_lookup(struct util_hash_table *handle_hash,
159
                          uint32_t handle, enum virgl_object_type type)
160 161 162 163 164 165 166 167 168 169 170 171 172
{
   struct vrend_object *obj;

   obj = util_hash_table_get(handle_hash, intptr_to_pointer(handle));
   if (!obj) {
      return NULL;
   }

   if (obj->type != type)
      return NULL;
   return obj->data;
}

173
int vrend_resource_insert(void *data, uint32_t handle)
174
{
175
   struct vrend_object *obj;
176

177 178 179 180
   if (!handle)
      return 0;

   obj = CALLOC_STRUCT(vrend_object);
181 182
   if (!obj)
      return 0;
183

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
   obj->handle = handle;
   obj->data = data;
   util_hash_table_set(res_hash, intptr_to_pointer(obj->handle), obj);
   return obj->handle;
}

void vrend_resource_remove(uint32_t handle)
{
   util_hash_table_remove(res_hash, intptr_to_pointer(handle));
}

void *vrend_resource_lookup(uint32_t handle, uint32_t ctx_id)
{
   struct vrend_object *obj;
   obj = util_hash_table_get(res_hash, intptr_to_pointer(handle));
   if (!obj)
      return NULL;
   return obj->data;
}