1 /**************************************************************************
2 *
3 * Copyright (C) 2020 Chromium.
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 "virgl_context.h"
26
27 #include <errno.h>
28
29 #include "os/os_misc.h"
30 #include "util/u_hash_table.h"
31 #include "util/u_pointer.h"
32 #include "virgl_util.h"
33
34 static struct util_hash_table *virgl_context_table;
35
36 static void
virgl_context_destroy_func(void * val)37 virgl_context_destroy_func(void *val)
38 {
39 struct virgl_context *ctx = val;
40 ctx->destroy(ctx);
41 }
42
43 int
virgl_context_table_init(void)44 virgl_context_table_init(void)
45 {
46 virgl_context_table = util_hash_table_create(hash_func_u32,
47 compare_func,
48 virgl_context_destroy_func);
49 return virgl_context_table ? 0 : ENOMEM;
50 }
51
52 void
virgl_context_table_cleanup(void)53 virgl_context_table_cleanup(void)
54 {
55 util_hash_table_destroy(virgl_context_table);
56 }
57
58 void
virgl_context_table_reset(void)59 virgl_context_table_reset(void)
60 {
61 util_hash_table_clear(virgl_context_table);
62 }
63
64 int
virgl_context_add(struct virgl_context * ctx)65 virgl_context_add(struct virgl_context *ctx)
66 {
67 const enum pipe_error err = util_hash_table_set(
68 virgl_context_table, uintptr_to_pointer(ctx->ctx_id), ctx);
69 return err == PIPE_OK ? 0 : ENOMEM;
70 }
71
72 void
virgl_context_remove(uint32_t ctx_id)73 virgl_context_remove(uint32_t ctx_id)
74 {
75 util_hash_table_remove(virgl_context_table, uintptr_to_pointer(ctx_id));
76 }
77
78 struct virgl_context *
virgl_context_lookup(uint32_t ctx_id)79 virgl_context_lookup(uint32_t ctx_id)
80 {
81 return util_hash_table_get(virgl_context_table,
82 uintptr_to_pointer(ctx_id));
83 }
84
85 static enum pipe_error
virgl_context_foreach_func(UNUSED void * key,void * val,void * data)86 virgl_context_foreach_func(UNUSED void *key, void *val, void *data)
87 {
88 const struct virgl_context_foreach_args *args = data;
89 struct virgl_context *ctx = val;
90
91 return args->callback(ctx, args->data) ? PIPE_OK : PIPE_ERROR;
92 }
93
94 void
virgl_context_foreach(const struct virgl_context_foreach_args * args)95 virgl_context_foreach(const struct virgl_context_foreach_args *args)
96 {
97 util_hash_table_foreach(virgl_context_table,
98 virgl_context_foreach_func,
99 (void *)args);
100 }
101