Changeset 062d900 in mainline for uspace/lib/nic
- Timestamp:
- 2012-10-09T11:49:43Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 4e00f87
- Parents:
- 87e9392
- git-author:
- Adam Hraska <adam.hraska+hos@…> (2012-10-09 11:49:43)
- git-committer:
- Jakub Jermar <jakub@…> (2012-10-09 11:49:43)
- Location:
- uspace/lib/nic
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/nic/include/nic.h
r87e9392 r062d900 40 40 41 41 #include <adt/list.h> 42 #include <adt/hash_table.h> 42 43 #include <ddf/driver.h> 43 44 #include <device/hw_res_parsed.h> … … 53 54 */ 54 55 typedef struct nic_wol_virtue { 55 link_t item;56 ht_link_t item; 56 57 nic_wv_id_t id; 57 58 nic_wv_type_t type; -
uspace/lib/nic/include/nic_addr_db.h
r87e9392 r062d900 43 43 #endif 44 44 45 #include <adt/hash_set.h> 46 47 /** 48 * Initial size of DB's hash set 49 */ 50 #define NIC_ADDR_DB_INIT_SIZE 8 51 /** 52 * Maximal length of addresses in the DB (in bytes). 53 */ 54 #define NIC_ADDR_MAX_LENGTH 16 45 #include <adt/hash_table.h> 55 46 56 47 /** … … 58 49 */ 59 50 typedef struct nic_addr_db { 60 hash_ set_t set;51 hash_table_t set; 61 52 size_t addr_len; 62 53 } nic_addr_db_t; 63 54 64 /**65 * Helper structure for keeping the address in the hash set.66 */67 typedef struct nic_addr_entry {68 link_t item;69 size_t addr_len;70 uint8_t addr[NIC_ADDR_MAX_LENGTH];71 } nic_addr_entry_t;72 55 73 56 extern int nic_addr_db_init(nic_addr_db_t *db, size_t addr_len); 74 57 extern void nic_addr_db_clear(nic_addr_db_t *db); 75 58 extern void nic_addr_db_destroy(nic_addr_db_t *db); 76 extern size_t nic_addr_db_count(const nic_addr_db_t *db);77 59 extern int nic_addr_db_insert(nic_addr_db_t *db, const uint8_t *addr); 78 60 extern int nic_addr_db_remove(nic_addr_db_t *db, const uint8_t *addr); 79 extern void nic_addr_db_remove_selected(nic_addr_db_t *db,80 int (*func)(const uint8_t *, void *), void *arg);81 61 extern int nic_addr_db_contains(const nic_addr_db_t *db, const uint8_t *addr); 82 62 extern void nic_addr_db_foreach(const nic_addr_db_t *db, -
uspace/lib/nic/include/nic_wol_virtues.h
r87e9392 r062d900 51 51 * Operations for table 52 52 */ 53 hash_table_op erations_t table_operations;53 hash_table_ops_t table_operations; 54 54 /** 55 55 * WOL virtues hashed by their ID's. -
uspace/lib/nic/src/nic_addr_db.c
r87e9392 r062d900 35 35 * @brief Generic hash-set based database of addresses 36 36 */ 37 #include "nic_addr_db.h" 38 #include "libarch/common.h" 37 39 #include <assert.h> 38 40 #include <stdlib.h> … … 40 42 #include <errno.h> 41 43 #include <mem.h> 42 43 #include "nic_addr_db.h" 44 45 /** 46 * Hash set helper function 47 */ 48 static int nic_addr_equals(const link_t *item1, const link_t *item2) 49 { 50 assert(item1 && item2); 51 size_t addr_len = ((const nic_addr_entry_t *) item1)->addr_len; 52 53 assert(addr_len == ((const nic_addr_entry_t *) item2)->addr_len); 54 55 size_t i; 56 for (i = 0; i < addr_len; ++i) { 57 if (((nic_addr_entry_t *) item1)->addr[i] != 58 ((nic_addr_entry_t *) item2)->addr[i]) 59 return false; 44 #include <adt/hash_table.h> 45 #include <macros.h> 46 #include <stdint.h> 47 48 49 /** 50 * Helper structure for keeping the address in the hash set. 51 */ 52 typedef 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 /* 60 * Hash table helper functions 61 */ 62 typedef struct { 63 size_t len; 64 const uint8_t *addr; 65 } addr_key_t; 66 67 static bool nic_addr_key_equal(void *key_arg, const ht_link_t *item) 68 { 69 addr_key_t *key = (addr_key_t*)key_arg; 70 nic_addr_entry_t *entry = member_to_inst(item, nic_addr_entry_t, link); 71 72 return 0 == bcmp(entry->addr, key->addr, entry->len); 73 } 74 75 static size_t addr_hash(size_t len, const uint8_t *addr) 76 { 77 size_t hash = 0; 78 79 for (size_t i = 0; i < len; ++i) { 80 hash = (hash << 5) ^ addr[i]; 60 81 } 61 return true; 62 } 63 64 /** 65 * Hash set helper function 66 */ 67 static unsigned long nic_addr_hash(const link_t *item) 68 { 69 assert(item); 70 const nic_addr_entry_t *entry = (const nic_addr_entry_t *) item; 71 unsigned long hash = 0; 72 73 size_t i; 74 for (i = 0; i < entry->addr_len; ++i) { 75 hash = (hash << 8) ^ (hash >> 24) ^ entry->addr[i]; 76 } 82 77 83 return hash; 78 84 } 79 85 80 /** 81 * Helper wrapper 82 */ 83 static void nic_addr_destroy(link_t *item, void *unused) 84 { 85 free(item); 86 } 86 static size_t nic_addr_key_hash(void *k) 87 { 88 addr_key_t *key = (addr_key_t*)k; 89 return addr_hash(key->len, key->addr); 90 } 91 92 static size_t nic_addr_hash(const ht_link_t *item) 93 { 94 nic_addr_entry_t *entry = member_to_inst(item, nic_addr_entry_t, link); 95 return addr_hash(entry->len, entry->addr); 96 } 97 98 static void nic_addr_removed(ht_link_t *item) 99 { 100 nic_addr_entry_t *entry = member_to_inst(item, nic_addr_entry_t, link); 101 102 free(entry); 103 } 104 105 static hash_table_ops_t set_ops = { 106 .hash = nic_addr_hash, 107 .key_hash = nic_addr_key_hash, 108 .key_equal = nic_addr_key_equal, 109 .equal = 0, 110 .remove_callback = nic_addr_removed 111 }; 87 112 88 113 /** … … 99 124 { 100 125 assert(db); 101 if (addr_len > NIC_ADDR_MAX_LENGTH) { 126 127 if (addr_len > UCHAR_MAX) 102 128 return EINVAL; 103 } 104 if (!hash_set_init(&db->set, nic_addr_hash, nic_addr_equals, 105 NIC_ADDR_DB_INIT_SIZE)) { 129 130 if (!hash_table_create(&db->set, 0, 0, &set_ops)) 106 131 return ENOMEM; 107 }132 108 133 db->addr_len = addr_len; 109 134 return EOK; … … 118 143 { 119 144 assert(db); 120 hash_ set_clear(&db->set, nic_addr_destroy, NULL);145 hash_table_clear(&db->set); 121 146 } 122 147 … … 129 154 { 130 155 assert(db); 131 hash_set_apply(&db->set, nic_addr_destroy, NULL); 132 hash_set_destroy(&db->set); 133 } 134 135 /** 136 * Get number of addresses in the db 137 * 138 * @param db 139 * 140 * @return Number of adresses 141 */ 142 size_t nic_addr_db_count(const nic_addr_db_t *db) 143 { 144 assert(db); 145 return hash_set_count(&db->set); 146 } 156 hash_table_destroy(&db->set); 157 } 158 147 159 148 160 /** … … 160 172 { 161 173 assert(db && addr); 162 nic_addr_entry_t *entry = malloc(sizeof (nic_addr_entry_t)); 163 if (entry == NULL) { 174 175 addr_key_t key = { 176 .len = db->addr_len, 177 .addr = addr 178 }; 179 180 if (hash_table_find(&db->set, &key)) 181 return EEXIST; 182 183 nic_addr_entry_t *entry = malloc(sizeof(nic_addr_entry_t) + db->addr_len - 1); 184 if (entry == NULL) 164 185 return ENOMEM; 165 } 166 entry-> addr_len =db->addr_len;186 187 entry->len = (uint8_t) db->addr_len; 167 188 memcpy(entry->addr, addr, db->addr_len); 168 169 return hash_set_insert(&db->set, &entry->item) ? EOK : EEXIST; 189 190 hash_table_insert(&db->set, &entry->link); 191 return EOK; 170 192 } 171 193 … … 182 204 { 183 205 assert(db && addr); 184 nic_addr_entry_t entry; 185 entry.addr_len = db->addr_len; 186 memcpy(entry.addr, addr, db->addr_len); 187 188 link_t *removed = hash_set_remove(&db->set, &entry.item); 189 free(removed); 190 return removed != NULL ? EOK : ENOENT; 206 207 addr_key_t key = { 208 .len = db->addr_len, 209 .addr = addr 210 }; 211 212 if (hash_table_remove(&db->set, &key)) 213 return EOK; 214 else 215 return ENOENT; 191 216 } 192 217 … … 202 227 { 203 228 assert(db && addr); 204 nic_addr_entry_t entry; 205 entry.addr_len = db->addr_len; 206 memcpy(entry.addr, addr, db->addr_len); 207 208 return hash_set_contains(&db->set, &entry.item); 229 230 addr_key_t key = { 231 .len = db->addr_len, 232 .addr = addr 233 }; 234 235 return 0 != hash_table_find(&db->set, &key); 209 236 } 210 237 … … 220 247 * Helper function for nic_addr_db_foreach 221 248 */ 222 static void nic_addr_db_fe_helper(link_t *item, void *arg) { 249 static bool nic_addr_db_fe_helper(ht_link_t *item, void *arg) 250 { 223 251 nic_addr_db_fe_arg_t *hs = (nic_addr_db_fe_arg_t *) arg; 224 hs->func(((nic_addr_entry_t *) item)->addr, hs->arg); 252 nic_addr_entry_t *entry = member_to_inst(item, nic_addr_entry_t, link); 253 hs->func(entry->addr, hs->arg); 254 return true; 225 255 } 226 256 … … 237 267 { 238 268 nic_addr_db_fe_arg_t hs = { .func = func, .arg = arg }; 239 hash_set_apply((hash_set_t *) &db->set, nic_addr_db_fe_helper, &hs); 240 } 241 242 /** 243 * Helper structure for nic_addr_db_remove_selected 244 */ 245 typedef struct { 246 int (*func)(const uint8_t *, void *); 247 void *arg; 248 } nic_addr_db_rs_arg_t; 249 250 /** 251 * Helper function for nic_addr_db_foreach 252 */ 253 static int nic_addr_db_rs_helper(link_t *item, void *arg) { 254 nic_addr_db_rs_arg_t *hs = (nic_addr_db_rs_arg_t *) arg; 255 int retval = hs->func(((nic_addr_entry_t *) item)->addr, hs->arg); 256 if (retval) { 257 free(item); 258 } 259 return retval; 260 } 261 262 /** 263 * Removes all addresses for which the function returns non-zero. 264 * 265 * @param db 266 * @param func User-defined function 267 * @param arg Custom argument passed to the function 268 */ 269 void nic_addr_db_remove_selected(nic_addr_db_t *db, 270 int (*func)(const uint8_t *, void *), void *arg) 271 { 272 nic_addr_db_rs_arg_t hs = { .func = func, .arg = arg }; 273 hash_set_remove_selected(&db->set, nic_addr_db_rs_helper, &hs); 269 hash_table_apply((hash_table_t*)&db->set, nic_addr_db_fe_helper, &hs); 274 270 } 275 271 -
uspace/lib/nic/src/nic_wol_virtues.c
r87e9392 r062d900 37 37 38 38 #include "nic_wol_virtues.h" 39 #include "nic.h" 39 40 #include <assert.h> 40 41 #include <errno.h> 41 42 42 #define NIC_WV_HASH_COUNT 32 43 44 /** 45 * Hash table helper function 46 */ 47 static int nic_wv_compare(unsigned long key[], hash_count_t keys, 48 link_t *item) 43 44 /* 45 * Hash table helper functions 46 */ 47 48 static size_t nic_wv_key_hash(void *key) 49 { 50 return *(nic_wv_id_t*) key; 51 } 52 53 static size_t nic_wv_hash(const ht_link_t *item) 49 54 { 50 55 nic_wol_virtue_t *virtue = (nic_wol_virtue_t *) item; 51 return (virtue->id == (nic_wv_id_t) key[0]); 52 } 53 54 /** 55 * Hash table helper function 56 */ 57 static void nic_wv_rc(link_t *item) 58 { 59 } 60 61 /** 62 * Hash table helper function 63 */ 64 static hash_index_t nic_wv_hash(unsigned long keys[]) 65 { 66 return keys[0] % NIC_WV_HASH_COUNT; 56 return virtue->id; 57 } 58 59 static bool nic_wv_key_equal(void *key, const ht_link_t *item) 60 { 61 nic_wol_virtue_t *virtue = (nic_wol_virtue_t *) item; 62 return (virtue->id == *(nic_wv_id_t*) key); 67 63 } 68 64 … … 77 73 int nic_wol_virtues_init(nic_wol_virtues_t *wvs) 78 74 { 79 bzero(wvs, sizeof (nic_wol_virtues_t)); 80 wvs->table_operations.compare = nic_wv_compare; 75 bzero(wvs, sizeof(nic_wol_virtues_t)); 81 76 wvs->table_operations.hash = nic_wv_hash; 82 wvs->table_operations.remove_callback = nic_wv_rc; 83 if (!hash_table_create(&wvs->table, NIC_WV_HASH_COUNT, 1, 84 &wvs->table_operations)) { 77 wvs->table_operations.key_hash = nic_wv_key_hash; 78 wvs->table_operations.key_equal = nic_wv_key_equal; 79 wvs->table_operations.equal = 0; 80 wvs->table_operations.remove_callback = 0; 81 82 if (!hash_table_create(&wvs->table, 0, 0, &wvs->table_operations)) { 85 83 return ENOMEM; 86 84 } … … 170 168 do { 171 169 virtue->id = wvs->next_id++; 172 } while (NULL != 173 hash_table_find(&wvs->table, (unsigned long *) &virtue->id)); 174 hash_table_insert(&wvs->table, 175 (unsigned long *) &virtue->id, &virtue->item); 170 } while (NULL != hash_table_find(&wvs->table, &virtue->id)); 171 hash_table_insert(&wvs->table, &virtue->item); 176 172 virtue->next = wvs->lists[virtue->type]; 177 173 wvs->lists[virtue->type] = virtue; … … 191 187 nic_wol_virtue_t *nic_wol_virtues_remove(nic_wol_virtues_t *wvs, nic_wv_id_t id) 192 188 { 193 nic_wol_virtue_t *virtue = (nic_wol_virtue_t *)194 hash_table_find(&wvs->table, (unsigned long *)&id);189 nic_wol_virtue_t *virtue = 190 (nic_wol_virtue_t *) hash_table_find(&wvs->table, &id); 195 191 if (virtue == NULL) { 196 192 return NULL; … … 198 194 199 195 /* Remove from filter_table */ 200 hash_table_remove (&wvs->table, (unsigned long *) &id, 1);196 hash_table_remove_item(&wvs->table, &virtue->item); 201 197 202 198 /* Remove from filter_types */ … … 235 231 * constant virtue the retyping is correct. 236 232 */ 237 link_t *virtue = hash_table_find( 238 &((nic_wol_virtues_t *) wvs)->table, (unsigned long *) &id); 233 ht_link_t *virtue = hash_table_find(&((nic_wol_virtues_t *) wvs)->table, &id); 239 234 return (const nic_wol_virtue_t *) virtue; 240 235 }
Note:
See TracChangeset
for help on using the changeset viewer.