FreeBSD/sys/netpfil/ipfw/dn_heap.c
/* lookup and optionally create or delete element */
lookup and optionally create or delete element
lookup and optionally create or delete element
void *
dn_ht_find(struct dn_ht *ht, uintptr_t key, int flags, void *arg)
{
	int i;
	void **pp, *p;
	if (ht == NULL)	
		return NULL;
	i = (ht->buckets == 1) ? 0 :
		(ht->hash(key, flags, arg) & ht->buckets);
	for (pp = &ht->ht[i]; (p = *pp); pp = (void **)((char *)p + ht->ofs)) {
		if (flags & DNHT_MATCH_PTR) {
			if (key == (uintptr_t)p)
				break;
		} else if (ht->match(p, key, flags, arg)) 
			break;
	}
	if (p) {
		if (flags & DNHT_REMOVE) {
			*pp = *(void **)((char *)p + ht->ofs);
			*(void **)((char *)p + ht->ofs) = NULL;
			ht->entries--;
		}
	} else if (flags & DNHT_INSERT) {
		p = ht->newh ? ht->newh(key, flags, arg) : (void *)key;
		if (p) {
			ht->entries++;
			*(void **)((char *)p + ht->ofs) = ht->ht[i];
			ht->ht[i] = p;
		}
	}
	return p;
}
void * dn_ht_find ( struct dn_ht * ht , uintptr_t key , int flags , void * arg ) { int i ; void * * pp , * p ; if ( ht == NULL ) return NULL ; i = ( ht -> buckets == 1 ) ? 0 : ( ht -> hash ( key , flags , arg ) & ht -> buckets ) ; for ( pp = & ht -> ht [ i ] ; ( p = * pp ) ; pp = ( void * * ) ( ( char * ) p + ht -> ofs ) ) { if ( flags & DNHT_MATCH_PTR ) { if ( key == ( uintptr_t ) p ) break ; } else if ( ht -> match ( p , key , flags , arg ) ) break ; } if ( p ) { if ( flags & DNHT_REMOVE ) { * pp = * ( void * * ) ( ( char * ) p + ht -> ofs ) ; * ( void * * ) ( ( char * ) p + ht -> ofs ) = NULL ; ht -> entries -- ; } } else if ( flags & DNHT_INSERT ) { p = ht -> newh ? ht -> newh ( key , flags , arg ) : ( void * ) key ; if ( p ) { ht -> entries ++ ; * ( void * * ) ( ( char * ) p + ht -> ofs ) = ht -> ht [ i ] ; ht -> ht [ i ] = p ; } } return p ; }
lookup and optionally create or delete element
lookup and optionally create or delete element
/* easy on an empty hash */ /* found match */ /* link in the next element */ // printf("%s before calling new, bucket %d ofs %d\n", //	__FUNCTION__, i, ht->ofs); // printf("%s newh returns %p\n", __FUNCTION__, p);