lruhash.c File Reference

This file contains a hashtable with LRU keeping of entries. More...

#include "config.h"
#include "util/storage/lruhash.h"
#include "util/fptr_wlist.h"

Functions

void bin_init (struct lruhash_bin *array, size_t size)
 init the hash bins for the table
struct lruhashlruhash_create (size_t start_size, size_t maxmem, lruhash_sizefunc_t sizefunc, lruhash_compfunc_t compfunc, lruhash_delkeyfunc_t delkeyfunc, lruhash_deldatafunc_t deldatafunc, void *arg)
 Create new hash table.
void bin_delete (struct lruhash *table, struct lruhash_bin *bin)
 delete the hash bin and entries inside it
void bin_split (struct lruhash *table, struct lruhash_bin *newa, int newmask)
 Split hash bin into two new ones.
void lruhash_delete (struct lruhash *table)
 Delete hash table.
void bin_overflow_remove (struct lruhash_bin *bin, struct lruhash_entry *entry)
 Remove entry from bin overflow chain.
void reclaim_space (struct lruhash *table, struct lruhash_entry **list)
 Try to make space available by deleting old entries.
struct lruhash_entrybin_find_entry (struct lruhash *table, struct lruhash_bin *bin, hashvalue_t hash, void *key)
 Find entry in hash bin.
void table_grow (struct lruhash *table)
 Grow the table lookup array.
void lru_front (struct lruhash *table, struct lruhash_entry *entry)
 Put entry at front of lru.
void lru_remove (struct lruhash *table, struct lruhash_entry *entry)
 Remove entry from lru list.
void lru_touch (struct lruhash *table, struct lruhash_entry *entry)
 Touch entry, so it becomes the most recently used in the LRU list.
void lruhash_insert (struct lruhash *table, hashvalue_t hash, struct lruhash_entry *entry, void *data, void *cb_arg)
 Insert a new element into the hashtable.
struct lruhash_entrylruhash_lookup (struct lruhash *table, hashvalue_t hash, void *key, int wr)
 Lookup an entry in the hashtable.
void lruhash_remove (struct lruhash *table, hashvalue_t hash, void *key)
 Remove entry from hashtable.
static void bin_clear (struct lruhash *table, struct lruhash_bin *bin)
 clear bin, respecting locks, does not do space, LRU
void lruhash_clear (struct lruhash *table)
 Clear hash table.
void lruhash_status (struct lruhash *table, const char *id, int extended)
 Output debug info to the log as to state of the hash table.
size_t lruhash_get_mem (struct lruhash *table)
 Get memory in use now by the lruhash table.
void lruhash_setmarkdel (struct lruhash *table, lruhash_markdelfunc_t md)
 Set the markdelfunction (or NULL).


Detailed Description

This file contains a hashtable with LRU keeping of entries.


Function Documentation

struct lruhash* lruhash_create ( size_t  start_size,
size_t  maxmem,
lruhash_sizefunc_t  sizefunc,
lruhash_compfunc_t  compfunc,
lruhash_delkeyfunc_t  delkeyfunc,
lruhash_deldatafunc_t  deldatafunc,
void *  arg 
) [read]

Create new hash table.

Parameters:
start_size,: size of hashtable array at start, must be power of 2.
maxmem,: maximum amount of memory this table is allowed to use.
sizefunc,: calculates memory usage of entries.
compfunc,: compares entries, 0 on equality.
delkeyfunc,: deletes key. Calling both delkey and deldata will also free the struct lruhash_entry. Make it part of the key structure and delete it in delkeyfunc.
deldatafunc,: deletes data.
arg,: user argument that is passed to user function calls.
Returns:
: new hash table or NULL on malloc failure.

References lruhash::array, bin_init(), lruhash::cb_arg, lruhash::compfunc, lruhash::deldatafunc, lruhash::delkeyfunc, lruhash::lock, lruhash::lru_end, lruhash::lru_start, lruhash::num, lruhash::size, lruhash::size_mask, lruhash::sizefunc, lruhash::space_max, and lruhash::space_used.

Referenced by infra_set_lame(), lruhash_test(), and slabhash_create().

void bin_split ( struct lruhash table,
struct lruhash_bin newa,
int  newmask 
)

Split hash bin into two new ones.

Based on increased size_mask. Caller must hold hash table lock. At the end the routine acquires all hashbin locks (in the old array). This makes it wait for other threads to finish with the bins. So the bins are ready to be deleted after this function.

Parameters:
table,: hash table with function pointers.
newa,: new increased array.
newmask,: new lookup mask.

References lruhash::array, lruhash_entry::hash, lruhash_bin::lock, lruhash_bin::overflow_list, lruhash_entry::overflow_next, lruhash::size, and lruhash::size_mask.

Referenced by table_grow().

void lruhash_delete ( struct lruhash table  ) 

Delete hash table.

Entries are all deleted.

Parameters:
table,: to delete.

References lruhash::array, bin_delete(), lruhash::lock, and lruhash::size.

Referenced by infra_host_deldatafunc(), lruhash_test(), and slabhash_delete().

void bin_overflow_remove ( struct lruhash_bin bin,
struct lruhash_entry entry 
)

Remove entry from bin overflow chain.

You must have locked the bin.

Parameters:
bin,: hash bin to look into.
entry,: entry ptr that needs removal.

References lruhash_bin::overflow_list, and lruhash_entry::overflow_next.

Referenced by lruhash_remove(), reclaim_space(), and test_bin_find_entry().

void reclaim_space ( struct lruhash table,
struct lruhash_entry **  list 
)

Try to make space available by deleting old entries.

Assumes that the lock on the hashtable is being held by caller. Caller must not hold bin locks.

Parameters:
table,: hash table.
list,: list of entries that are to be deleted later. Entries have been removed from the hash table and writelock is held.

References lruhash::array, bin_overflow_remove(), lruhash_entry::data, lruhash_entry::hash, lruhash_entry::key, lruhash_entry::lock, lruhash_bin::lock, log_assert, lruhash::lru_end, lruhash_entry::lru_next, lruhash_entry::lru_prev, lruhash::markdelfunc, lruhash::num, lruhash_entry::overflow_next, lruhash::size_mask, lruhash::sizefunc, lruhash::space_max, and lruhash::space_used.

Referenced by lruhash_insert().

struct lruhash_entry* bin_find_entry ( struct lruhash table,
struct lruhash_bin bin,
hashvalue_t  hash,
void *  key 
) [read]

Find entry in hash bin.

You must have locked the bin.

Parameters:
table,: hash table with function pointers.
bin,: hash bin to look into.
hash,: hash value to look for.
key,: key to look for.
Returns:
: the entry or NULL if not found.

References lruhash::compfunc, lruhash_entry::hash, lruhash_entry::key, lruhash_bin::overflow_list, and lruhash_entry::overflow_next.

Referenced by lruhash_insert(), lruhash_lookup(), lruhash_remove(), and test_bin_find_entry().

void table_grow ( struct lruhash table  ) 

Grow the table lookup array.

Becomes twice as large. Caller must hold the hash table lock. Must not hold any bin locks. Tries to grow, on malloc failure, nothing happened.

Parameters:
table,: hash table.

References lruhash::array, bin_init(), bin_split(), lruhash_bin::lock, lruhash::lock, log_err(), lruhash::size, and lruhash::size_mask.

Referenced by lruhash_insert().

void lru_front ( struct lruhash table,
struct lruhash_entry entry 
)

Put entry at front of lru.

entry must be unlinked from lru. Caller must hold hash table lock.

Parameters:
table,: hash table with lru head and tail.
entry,: entry to make most recently used.

References lruhash::lru_end, lruhash_entry::lru_next, lruhash_entry::lru_prev, and lruhash::lru_start.

Referenced by lru_touch(), lruhash_insert(), and test_lru().

void lru_remove ( struct lruhash table,
struct lruhash_entry entry 
)

Remove entry from lru list.

Caller must hold hash table lock.

Parameters:
table,: hash table with lru head and tail.
entry,: entry to remove from lru.

References lruhash::lru_end, lruhash_entry::lru_next, lruhash_entry::lru_prev, and lruhash::lru_start.

Referenced by lru_touch(), lruhash_remove(), and test_lru().

void lru_touch ( struct lruhash table,
struct lruhash_entry entry 
)

Touch entry, so it becomes the most recently used in the LRU list.

Caller must hold hash table lock. The entry must be inserted already.

Parameters:
table,: hash table.
entry,: entry to make first in LRU.

References log_assert, lru_front(), lru_remove(), and lruhash::lru_start.

Referenced by lruhash_insert(), lruhash_lookup(), and rrset_cache_touch().

void lruhash_insert ( struct lruhash table,
hashvalue_t  hash,
struct lruhash_entry entry,
void *  data,
void *  cb_override 
)

Insert a new element into the hashtable.

If key is already present data pointer in that entry is updated. The space calculation function is called with the key, data. If necessary the least recently used entries are deleted to make space. If necessary the hash array is grown up.

Parameters:
table,: hash table.
hash,: hash value. User calculates the hash.
entry,: identifies the entry. If key already present, this entry->key is deleted immediately. But entry->data is set to NULL before deletion, and put into the existing entry. The data is then freed.
data,: the data.
cb_override,: if not null overrides the cb_arg for the deletefunc.

References lruhash::array, bin_find_entry(), lruhash::cb_arg, lruhash::compfunc, lruhash_entry::data, lruhash::deldatafunc, lruhash::delkeyfunc, fptr_ok, fptr_whitelist_hash_compfunc(), fptr_whitelist_hash_deldatafunc(), fptr_whitelist_hash_delkeyfunc(), fptr_whitelist_hash_markdelfunc(), fptr_whitelist_hash_sizefunc(), lruhash_entry::key, lruhash_entry::lock, lruhash_bin::lock, lruhash::lock, lru_front(), lru_touch(), lruhash::markdelfunc, lruhash::num, lruhash_bin::overflow_list, lruhash_entry::overflow_next, reclaim_space(), lruhash::size, lruhash::size_mask, lruhash::sizefunc, lruhash::space_max, lruhash::space_used, and table_grow().

Referenced by infra_set_lame(), slabhash_insert(), test_short_table(), testadd(), and testadd_unlim().

struct lruhash_entry* lruhash_lookup ( struct lruhash table,
hashvalue_t  hash,
void *  key,
int  wr 
) [read]

Lookup an entry in the hashtable.

At the end of the function you hold a (read/write)lock on the entry. The LRU is updated for the entry (if found).

Parameters:
table,: hash table.
hash,: hash of key.
key,: what to look for, compared against entries in overflow chain. the hash value must be set, and must work with compare function.
wr,: set to true if you desire a writelock on the entry. with a writelock you can update the data part.
Returns:
: pointer to the entry or NULL. The entry is locked. The user must unlock the entry when done.

References lruhash::array, bin_find_entry(), lruhash::compfunc, fptr_ok, fptr_whitelist_hash_compfunc(), lruhash_entry::lock, lruhash_bin::lock, lruhash::lock, lru_touch(), and lruhash::size_mask.

Referenced by infra_lookup_lame(), slabhash_lookup(), test_short_table(), testlookup(), and testlookup_unlim().

void lruhash_remove ( struct lruhash table,
hashvalue_t  hash,
void *  key 
)

void lruhash_clear ( struct lruhash table  ) 

Clear hash table.

Entries are all deleted, while locking them before doing so. At end the table is empty.

Parameters:
table,: to make empty.

References lruhash::array, bin_clear(), lruhash::deldatafunc, lruhash::delkeyfunc, fptr_ok, fptr_whitelist_hash_deldatafunc(), fptr_whitelist_hash_delkeyfunc(), fptr_whitelist_hash_markdelfunc(), lruhash::lock, lruhash::lru_end, lruhash::lru_start, lruhash::markdelfunc, lruhash::num, lruhash::size, and lruhash::space_used.

Referenced by slabhash_clear(), and test_long_table().

void lruhash_status ( struct lruhash table,
const char *  id,
int  extended 
)

Output debug info to the log as to state of the hash table.

Parameters:
table,: hash table.
id,: string printed with table to identify the hash table.
extended,: set to true to print statistics on overflow bin lengths.

References lruhash::array, lruhash_bin::lock, lruhash::lock, log_info(), lruhash::num, lruhash_bin::overflow_list, lruhash_entry::overflow_next, lruhash::size, lruhash::size_mask, lruhash::space_max, and lruhash::space_used.

Referenced by slabhash_status(), test_long_table(), test_thr_main(), and test_threaded_table().

size_t lruhash_get_mem ( struct lruhash table  ) 

Get memory in use now by the lruhash table.

Parameters:
table,: hash table. Will be locked before use. And unlocked after.
Returns:
size in bytes.

References lruhash::array, lruhash_bin::lock, lruhash::lock, and lruhash::size.

Referenced by count_host_lame(), and slabhash_get_mem().


Generated on Thu Mar 26 10:03:54 2009 for unbound by  doxygen 1.5.8