source: mainline/uspace/lib/nic/src/nic_addr_db.c@ b9be9b0

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b9be9b0 was 36795edf, checked in by Martin Decky <martin@…>, 4 years ago

Improve lists and other data structures

Provide more standard-compliant member_to_inst implementation that uses
offsetof. Avoid potential undefined behavior in list_foreach and
list_foreach_rev by avoiding assinging an unaligned pointer value. Use
size_t instead of unsigned long for list length.

  • Property mode set to 100644
File size: 6.3 KB
Line 
1/*
2 * Copyright (c) 2011 Radim Vansa
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/**
30 * @addtogroup libnic
31 * @{
32 */
33/**
34 * @file
35 * @brief Generic hash-set based database of addresses
36 */
37
38#include "nic_addr_db.h"
39#include <assert.h>
40#include <stdlib.h>
41#include <stdbool.h>
42#include <errno.h>
43#include <mem.h>
44#include <member.h>
45#include <adt/hash_table.h>
46#include <macros.h>
47#include <stdint.h>
48
49/**
50 * Helper structure for keeping the address in the hash set.
51 */
52typedef struct nic_addr_entry {
53 ht_link_t link;
54 uint8_t len;
55 uint8_t addr[1];
56} nic_addr_entry_t;
57
58/*
59 * Hash table helper functions
60 */
61typedef struct {
62 size_t len;
63 const uint8_t *addr;
64} addr_key_t;
65
66static bool nic_addr_key_equal(const void *key_arg, const ht_link_t *item)
67{
68 const addr_key_t *key = key_arg;
69 nic_addr_entry_t *entry = member_to_inst(item, nic_addr_entry_t, link);
70
71 return memcmp(entry->addr, key->addr, entry->len) == 0;
72}
73
74static size_t addr_hash(size_t len, const uint8_t *addr)
75{
76 size_t hash = 0;
77
78 for (size_t i = 0; i < len; ++i) {
79 hash = (hash << 5) ^ addr[i];
80 }
81
82 return hash;
83}
84
85static size_t nic_addr_key_hash(const void *k)
86{
87 const addr_key_t *key = k;
88 return addr_hash(key->len, key->addr);
89}
90
91static size_t nic_addr_hash(const ht_link_t *item)
92{
93 nic_addr_entry_t *entry = member_to_inst(item, nic_addr_entry_t, link);
94 return addr_hash(entry->len, entry->addr);
95}
96
97static void nic_addr_removed(ht_link_t *item)
98{
99 nic_addr_entry_t *entry = member_to_inst(item, nic_addr_entry_t, link);
100
101 free(entry);
102}
103
104static hash_table_ops_t set_ops = {
105 .hash = nic_addr_hash,
106 .key_hash = nic_addr_key_hash,
107 .key_equal = nic_addr_key_equal,
108 .equal = NULL,
109 .remove_callback = nic_addr_removed
110};
111
112/**
113 * Initialize the database
114 *
115 * @param[in,out] db Uninitialized storage
116 * @param[in] addr_len Size of addresses in the db
117 *
118 * @return EOK If successfully initialized
119 * @return EINVAL If the address length is too big
120 * @return ENOMEM If there was not enough memory to initialize the storage
121 */
122errno_t nic_addr_db_init(nic_addr_db_t *db, size_t addr_len)
123{
124 assert(db);
125
126 if (addr_len > UCHAR_MAX)
127 return EINVAL;
128
129 if (!hash_table_create(&db->set, 0, 0, &set_ops))
130 return ENOMEM;
131
132 db->addr_len = addr_len;
133 return EOK;
134}
135
136/**
137 * Remove all records from the DB
138 *
139 * @param db
140 */
141void nic_addr_db_clear(nic_addr_db_t *db)
142{
143 assert(db);
144 hash_table_clear(&db->set);
145}
146
147/**
148 * Free the memory used by db, including all records...
149 *
150 * @param db
151 */
152void nic_addr_db_destroy(nic_addr_db_t *db)
153{
154 assert(db);
155 hash_table_destroy(&db->set);
156}
157
158/**
159 * Insert an address to the db
160 *
161 * @param db
162 * @param addr Inserted address. Length is implicitly concluded from the
163 * db's address length.
164 *
165 * @return EOK If the address was inserted
166 * @return ENOMEM If there was not enough memory
167 * @return EEXIST If this address already is in the db
168 */
169errno_t nic_addr_db_insert(nic_addr_db_t *db, const uint8_t *addr)
170{
171 assert(db && addr);
172
173 addr_key_t key = {
174 .len = db->addr_len,
175 .addr = addr
176 };
177
178 if (hash_table_find(&db->set, &key))
179 return EEXIST;
180
181 nic_addr_entry_t *entry = malloc(sizeof(nic_addr_entry_t) + db->addr_len - 1);
182 if (entry == NULL)
183 return ENOMEM;
184
185 entry->len = (uint8_t) db->addr_len;
186 memcpy(entry->addr, addr, db->addr_len);
187
188 hash_table_insert(&db->set, &entry->link);
189 return EOK;
190}
191
192/**
193 * Remove the address from the db
194 *
195 * @param db
196 * @param addr Removed address.
197 *
198 * @return EOK If the address was removed
199 * @return ENOENT If there is no address
200 */
201errno_t nic_addr_db_remove(nic_addr_db_t *db, const uint8_t *addr)
202{
203 assert(db && addr);
204
205 addr_key_t key = {
206 .len = db->addr_len,
207 .addr = addr
208 };
209
210 if (hash_table_remove(&db->set, &key))
211 return EOK;
212 else
213 return ENOENT;
214}
215
216/**
217 * Test if the address is contained in the db
218 *
219 * @param db
220 * @param addr Tested addresss
221 *
222 * @return true if the address is in the db, false otherwise
223 */
224bool nic_addr_db_contains(const nic_addr_db_t *db, const uint8_t *addr)
225{
226 assert(db && addr);
227
228 addr_key_t key = {
229 .len = db->addr_len,
230 .addr = addr
231 };
232
233 return 0 != hash_table_find(&db->set, &key);
234}
235
236/**
237 * Helper structure for nic_addr_db_foreach
238 */
239typedef struct {
240 void (*func)(const uint8_t *, void *);
241 void *arg;
242} nic_addr_db_fe_arg_t;
243
244/**
245 * Helper function for nic_addr_db_foreach
246 */
247static bool nic_addr_db_fe_helper(ht_link_t *item, void *arg)
248{
249 nic_addr_db_fe_arg_t *hs = (nic_addr_db_fe_arg_t *) arg;
250 nic_addr_entry_t *entry = member_to_inst(item, nic_addr_entry_t, link);
251 hs->func(entry->addr, hs->arg);
252 return true;
253}
254
255/**
256 * Executes a user-defined function on all addresses in the DB. The function
257 * must not change the addresses.
258 *
259 * @param db
260 * @param func User-defined function
261 * @param arg Custom argument passed to the function
262 */
263void nic_addr_db_foreach(const nic_addr_db_t *db,
264 void (*func)(const uint8_t *, void *), void *arg)
265{
266 nic_addr_db_fe_arg_t hs = { .func = func, .arg = arg };
267 hash_table_apply((hash_table_t *)&db->set, nic_addr_db_fe_helper, &hs);
268}
269
270/** @}
271 */
Note: See TracBrowser for help on using the repository browser.