• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "util/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                                                 equal_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    virgl_context_table = NULL;
57 }
58 
59 void
virgl_context_table_reset(void)60 virgl_context_table_reset(void)
61 {
62    util_hash_table_clear(virgl_context_table);
63 }
64 
65 int
virgl_context_add(struct virgl_context * ctx)66 virgl_context_add(struct virgl_context *ctx)
67 {
68    const enum pipe_error err = util_hash_table_set(
69          virgl_context_table, uintptr_to_pointer(ctx->ctx_id), ctx);
70    return err == PIPE_OK ? 0 : ENOMEM;
71 }
72 
73 void
virgl_context_remove(uint32_t ctx_id)74 virgl_context_remove(uint32_t ctx_id)
75 {
76    util_hash_table_remove(virgl_context_table, uintptr_to_pointer(ctx_id));
77 }
78 
79 struct virgl_context *
virgl_context_lookup(uint32_t ctx_id)80 virgl_context_lookup(uint32_t ctx_id)
81 {
82    return util_hash_table_get(virgl_context_table,
83                               uintptr_to_pointer(ctx_id));
84 }
85 
86 static enum pipe_error
virgl_context_foreach_func(UNUSED void * key,void * val,void * data)87 virgl_context_foreach_func(UNUSED void *key, void *val, void *data)
88 {
89    const struct virgl_context_foreach_args *args = data;
90    struct virgl_context *ctx = val;
91 
92    return args->callback(ctx, args->data) ? PIPE_OK : PIPE_ERROR;
93 }
94 
95 void
virgl_context_foreach(const struct virgl_context_foreach_args * args)96 virgl_context_foreach(const struct virgl_context_foreach_args *args)
97 {
98    util_hash_table_foreach(virgl_context_table,
99                            virgl_context_foreach_func,
100                            (void *)args);
101 }
102