1 /* MIT License
2 *
3 * Copyright (c) 2023 Brad House
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 * SPDX-License-Identifier: MIT
25 */
26 #include "ares_private.h"
27 #include "ares_htable.h"
28 #include "ares_htable_szvp.h"
29
30 struct ares_htable_szvp {
31 ares_htable_szvp_val_free_t free_val;
32 ares_htable_t *hash;
33 };
34
35 typedef struct {
36 size_t key;
37 void *val;
38 ares_htable_szvp_t *parent;
39 } ares_htable_szvp_bucket_t;
40
ares_htable_szvp_destroy(ares_htable_szvp_t * htable)41 void ares_htable_szvp_destroy(ares_htable_szvp_t *htable)
42 {
43 if (htable == NULL) {
44 return;
45 }
46
47 ares_htable_destroy(htable->hash);
48 ares_free(htable);
49 }
50
hash_func(const void * key,unsigned int seed)51 static unsigned int hash_func(const void *key, unsigned int seed)
52 {
53 const size_t *arg = key;
54 return ares_htable_hash_FNV1a((const unsigned char *)arg, sizeof(*arg), seed);
55 }
56
bucket_key(const void * bucket)57 static const void *bucket_key(const void *bucket)
58 {
59 const ares_htable_szvp_bucket_t *arg = bucket;
60 return &arg->key;
61 }
62
bucket_free(void * bucket)63 static void bucket_free(void *bucket)
64 {
65 ares_htable_szvp_bucket_t *arg = bucket;
66
67 if (arg->parent->free_val) {
68 arg->parent->free_val(arg->val);
69 }
70
71 ares_free(arg);
72 }
73
key_eq(const void * key1,const void * key2)74 static ares_bool_t key_eq(const void *key1, const void *key2)
75 {
76 const size_t *k1 = key1;
77 const size_t *k2 = key2;
78
79 if (*k1 == *k2) {
80 return ARES_TRUE;
81 }
82
83 return ARES_FALSE;
84 }
85
86 ares_htable_szvp_t *
ares_htable_szvp_create(ares_htable_szvp_val_free_t val_free)87 ares_htable_szvp_create(ares_htable_szvp_val_free_t val_free)
88 {
89 ares_htable_szvp_t *htable = ares_malloc(sizeof(*htable));
90 if (htable == NULL) {
91 goto fail;
92 }
93
94 htable->hash = ares_htable_create(hash_func, bucket_key, bucket_free, key_eq);
95 if (htable->hash == NULL) {
96 goto fail;
97 }
98
99 htable->free_val = val_free;
100
101 return htable;
102
103 fail:
104 if (htable) {
105 ares_htable_destroy(htable->hash);
106 ares_free(htable);
107 }
108 return NULL;
109 }
110
ares_htable_szvp_insert(ares_htable_szvp_t * htable,size_t key,void * val)111 ares_bool_t ares_htable_szvp_insert(ares_htable_szvp_t *htable, size_t key,
112 void *val)
113 {
114 ares_htable_szvp_bucket_t *bucket = NULL;
115
116 if (htable == NULL) {
117 goto fail;
118 }
119
120 bucket = ares_malloc(sizeof(*bucket));
121 if (bucket == NULL) {
122 goto fail; /* LCOV_EXCL_LINE: OutOfMemory */
123 }
124
125 bucket->parent = htable;
126 bucket->key = key;
127 bucket->val = val;
128
129 if (!ares_htable_insert(htable->hash, bucket)) {
130 goto fail; /* LCOV_EXCL_LINE: OutOfMemory */
131 }
132
133 return ARES_TRUE;
134
135 fail:
136 if (bucket) {
137 ares_free(bucket); /* LCOV_EXCL_LINE: OutOfMemory */
138 }
139 return ARES_FALSE;
140 }
141
ares_htable_szvp_get(const ares_htable_szvp_t * htable,size_t key,void ** val)142 ares_bool_t ares_htable_szvp_get(const ares_htable_szvp_t *htable, size_t key,
143 void **val)
144 {
145 ares_htable_szvp_bucket_t *bucket = NULL;
146
147 if (val) {
148 *val = NULL;
149 }
150
151 if (htable == NULL) {
152 return ARES_FALSE;
153 }
154
155 bucket = ares_htable_get(htable->hash, &key);
156 if (bucket == NULL) {
157 return ARES_FALSE;
158 }
159
160 if (val) {
161 *val = bucket->val;
162 }
163 return ARES_TRUE;
164 }
165
ares_htable_szvp_get_direct(const ares_htable_szvp_t * htable,size_t key)166 void *ares_htable_szvp_get_direct(const ares_htable_szvp_t *htable, size_t key)
167 {
168 void *val = NULL;
169 ares_htable_szvp_get(htable, key, &val);
170 return val;
171 }
172
ares_htable_szvp_remove(ares_htable_szvp_t * htable,size_t key)173 ares_bool_t ares_htable_szvp_remove(ares_htable_szvp_t *htable, size_t key)
174 {
175 if (htable == NULL) {
176 return ARES_FALSE;
177 }
178
179 return ares_htable_remove(htable->hash, &key);
180 }
181
ares_htable_szvp_num_keys(const ares_htable_szvp_t * htable)182 size_t ares_htable_szvp_num_keys(const ares_htable_szvp_t *htable)
183 {
184 if (htable == NULL) {
185 return 0;
186 }
187 return ares_htable_num_keys(htable->hash);
188 }
189