Skip to content

Commit

Permalink
More consistent locking, and a more simple hash store.
Browse files Browse the repository at this point in the history
  • Loading branch information
dustin committed May 26, 2006
1 parent fd775cd commit edf1fd0
Showing 1 changed file with 8 additions and 24 deletions.
32 changes: 8 additions & 24 deletions hash.c
Expand Up @@ -62,7 +62,7 @@ hash_init(int size)
struct hash_container *
hash_store(struct hashtable *hash, pcap_t *pcap_thing, unsigned int key)
{
struct hash_container *c=NULL, *p=NULL;
struct hash_container *c=NULL;
int hashval=0;
char time_buf[16];
time_t now=0;
Expand Down Expand Up @@ -95,21 +95,11 @@ hash_store(struct hashtable *hash, pcap_t *pcap_thing, unsigned int key)
exit(1);
}

c->next = NULL;

hashval = _do_hash(hash, key);

lock(hashval);

p = hash->buckets[hashval];

if (p) {
for (; p->next; p = p->next);
p->next = c;
} else {
hash->buckets[hashval] = c;
}

c->next=hash->buckets[hashval];
hash->buckets[hashval]=c;
unlock(hashval);

printf("# Created %s\n", c->filename);
Expand Down Expand Up @@ -142,8 +132,7 @@ hash_find(struct hashtable *hash, unsigned int key)
struct hash_container *p;
int hashval;

if(hash==NULL)
return(NULL);
assert(hash != NULL);

hashval = _do_hash(hash, key);

Expand Down Expand Up @@ -174,7 +163,6 @@ hash_delete(struct hashtable *hash, unsigned int key)
hashval = _do_hash(hash, key);

lock(hashval);

if(hash->buckets[hashval] != NULL) {
/* Special case the first one */
if(hash->buckets[hashval]->key == key) {
Expand All @@ -191,15 +179,14 @@ hash_delete(struct hashtable *hash, unsigned int key)
}
}
}
unlock(hashval);

if (deleteme) {
pcap_dump_close(deleteme->pcap_dumper);
free(deleteme->filename);
free(deleteme);
deleteme = NULL;
}

unlock(hashval);
}

/* Destroy a hash */
Expand Down Expand Up @@ -246,10 +233,8 @@ struct hash_keylist hash_keys(struct hashtable *hash)
list.entries[list.nentries++]=a;

for (i = 0; i < hash->hashsize; i++) {
p = hash->buckets[i];

lock(i);

p = hash->buckets[i];
if (p) {
for (; p; p = p->next) {
LAPPEND(p->key);
Expand All @@ -270,10 +255,9 @@ _hash_dump(struct hashtable *hash)
printf("Hash dump for hash at %p, size is %d:\n", hash, hash->hashsize);

for (i = 0; i < hash->hashsize; i++) {
lock(i);
p = hash->buckets[i];

if (p) {
lock(i);
printf("\tMatches at %d\n", i);
for (; p; p = p->next) {
#ifdef MYMALLOC
Expand All @@ -284,7 +268,7 @@ _hash_dump(struct hashtable *hash)
#endif
printf("\t\t%s -> d=%p\n", ntoa(p->key), p->pcap_dumper);
}
unlock(i);
}
unlock(i);
}
}

0 comments on commit edf1fd0

Please sign in to comment.