Annotation of embedaddon/iftop/ns_hash.c, revision 1.1.1.1

1.1       misho       1: /* hash table */
                      2: 
                      3: #include <stdio.h>
                      4: #include <stdlib.h>
                      5: #include <sys/types.h>
                      6: #include <sys/socket.h>
                      7: #include <netinet/in_systm.h>
                      8: #include <netinet/in.h>
                      9: #include <arpa/inet.h>
                     10: #include "ns_hash.h"
                     11: #include "hash.h"
                     12: #include "iftop.h"
                     13: 
                     14: #define hash_table_size 256
                     15: 
                     16: int ns_hash_compare(void* a, void* b) {
                     17:     struct in_addr* aa = (struct in_addr*)a;
                     18:     struct in_addr* bb = (struct in_addr*)b;
                     19:     return (aa->s_addr == bb->s_addr);
                     20: }
                     21: 
                     22: int ns_hash_hash(void* key) {
                     23:     int hash;
                     24:     long addr;
                     25:         
                     26:     addr = (long)((struct in_addr*)key)->s_addr;
                     27: 
                     28:     hash = ((addr & 0x000000FF)
                     29:             + (addr & 0x0000FF00 >> 8)
                     30:             + (addr & 0x00FF0000 >> 16)
                     31:             + (addr & 0xFF000000 >> 24)) % 0xFF;
                     32: 
                     33:     return hash;
                     34: }
                     35: 
                     36: void* ns_hash_copy_key(void* orig) {
                     37:     struct in_addr* copy;
                     38:     copy = xmalloc(sizeof *copy);
                     39:     *copy = *(struct in_addr*)orig;
                     40:     return copy;
                     41: }
                     42: 
                     43: void ns_hash_delete_key(void* key) {
                     44:     free(key);
                     45: }
                     46: 
                     47: /*
                     48:  * Allocate and return a hash
                     49:  */
                     50: hash_type* ns_hash_create() {
                     51:     hash_type* hash_table;
                     52:     hash_table = xcalloc(hash_table_size, sizeof *hash_table);
                     53:     hash_table->size = hash_table_size;
                     54:     hash_table->compare = &ns_hash_compare;
                     55:     hash_table->hash = &ns_hash_hash;
                     56:     hash_table->delete_key = &ns_hash_delete_key;
                     57:     hash_table->copy_key = &ns_hash_copy_key;
                     58:     hash_initialise(hash_table);
                     59:     return hash_table;
                     60: }
                     61: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>