Nagios  4.4.6
Dev docs for Nagios core and neb-module hackers
dkhash.h File Reference

Dual-key hash functions for Nagios. More...

#include <errno.h>

Go to the source code of this file.

#define DKHASH_WALK_REMOVE   1
 return flags usable from the callback function of dkhash_walk_data() More...
 
#define DKHASH_WALK_STOP   2
 Cause walking to stop.
 
#define DKHASH_OK   0
 return values for dkhash_insert() More...
 
#define DKHASH_EDUPE   (-EPERM)
 duplicate insert attempted
 
#define DKHASH_EPERM   (-EPERM)
 duplicate insert attempted
 
#define DKHASH_EINVAL   (-EINVAL)
 Invalid parameters passed.
 
#define DKHASH_ENOMEM   (-ENOMEM)
 Memory allocation failed.
 
typedef struct dkhash_table dkhash_table
 opaque type
 
dkhash_tabledkhash_create (unsigned int size)
 Create a dual-keyed hash-table of the given size Note that it's generally useful to make the table 25-30% larger than the number of items you intend to store, and also note that the 'size' arguments gets rounded up to the nearest power of 2. More...
 
int dkhash_destroy (dkhash_table *t)
 Destroy a dual-keyed hash table. More...
 
void * dkhash_get (dkhash_table *t, const char *k1, const char *k2)
 Fetch the data associated with a particular key. More...
 
int dkhash_insert (dkhash_table *t, const char *k1, const char *k2, void *data)
 Insert a new entry into the hash table. More...
 
void * dkhash_remove (dkhash_table *t, const char *k1, const char *k2)
 Remove data from the hash table Note that this does not free() the pointer to the data stored in the table. More...
 
void dkhash_walk_data (dkhash_table *t, int(*walker)(void *data))
 Call a function once for each item in the hash-table The callback function can return DKHASH_WALK_{REMOVE,STOP} or any OR'ed combination thereof to control the walking procedure, and should return 0 on the normal case. More...
 
unsigned int dkhash_collisions (dkhash_table *t)
 Get number of collisions in hash table Many collisions is a sign of a too small hash table or poor hash-function. More...
 
unsigned int dkhash_num_entries (dkhash_table *t)
 Get number of items in the hash table. More...
 
unsigned int dkhash_num_entries_max (dkhash_table *t)
 Get max number of items stored in the hash table. More...
 
unsigned int dkhash_num_entries_added (dkhash_table *t)
 Get number of entries added to hash table Note that some of them may have been removed. More...
 
unsigned int dkhash_num_entries_removed (dkhash_table *t)
 Get number of removed items from hash table. More...
 
unsigned int dkhash_table_size (dkhash_table *t)
 Get actual table size (in number of buckets) More...
 

Detailed Description

Dual-key hash functions for Nagios.

Having a dual-key hash function is pretty unusual, but since so much data in Nagios pertains to services (which are uniquely identified based on both host_name and service_description), it makes sense here.

Macro Definition Documentation

◆ DKHASH_OK

#define DKHASH_OK   0

return values for dkhash_insert()

Success

◆ DKHASH_WALK_REMOVE

#define DKHASH_WALK_REMOVE   1

return flags usable from the callback function of dkhash_walk_data()

Remove the most recently visited object

Function Documentation

◆ dkhash_collisions()

unsigned int dkhash_collisions ( dkhash_table t)

Get number of collisions in hash table Many collisions is a sign of a too small hash table or poor hash-function.

Parameters
tThe hash table to report on
Returns
The total number of collisions (not duplicates) from inserts

◆ dkhash_create()

dkhash_table* dkhash_create ( unsigned int  size)

Create a dual-keyed hash-table of the given size Note that it's generally useful to make the table 25-30% larger than the number of items you intend to store, and also note that the 'size' arguments gets rounded up to the nearest power of 2.

Parameters
sizeThe desired size of the hash-table.

◆ dkhash_destroy()

int dkhash_destroy ( dkhash_table t)

Destroy a dual-keyed hash table.

Parameters
tThe table to destroy
Returns
0 on success, -1 on errors

◆ dkhash_get()

void* dkhash_get ( dkhash_table t,
const char *  k1,
const char *  k2 
)

Fetch the data associated with a particular key.

Parameters
tThe table to get the data from
k1The first key
k2The second key
Returns
The data on success, NULL on errors or if data isn't found

◆ dkhash_insert()

int dkhash_insert ( dkhash_table t,
const char *  k1,
const char *  k2,
void *  data 
)

Insert a new entry into the hash table.

Parameters
tThe hash table
k1The first key
k2The second key (may be null)
dataThe data to insert
Returns
0 on success, < 0 on errors

◆ dkhash_num_entries()

unsigned int dkhash_num_entries ( dkhash_table t)

Get number of items in the hash table.

Parameters
tThe hash table
Returns
Number of items currently in the hash-table

◆ dkhash_num_entries_added()

unsigned int dkhash_num_entries_added ( dkhash_table t)

Get number of entries added to hash table Note that some of them may have been removed.

Parameters
tThe hash table
Returns
The number of items added to the table

◆ dkhash_num_entries_max()

unsigned int dkhash_num_entries_max ( dkhash_table t)

Get max number of items stored in the hash table.

Parameters
tThe hash table
Returns
Max number of items stored in hash-table

◆ dkhash_num_entries_removed()

unsigned int dkhash_num_entries_removed ( dkhash_table t)

Get number of removed items from hash table.

Parameters
tThe hash table
Returns
Number of items removed from hash table

◆ dkhash_remove()

void* dkhash_remove ( dkhash_table t,
const char *  k1,
const char *  k2 
)

Remove data from the hash table Note that this does not free() the pointer to the data stored in the table.

It just destroys containers for that data in the hash table.

Parameters
tThe hash table
k1The first key
k2The second key
Returns
The removed data on success, or NULL on errors

◆ dkhash_table_size()

unsigned int dkhash_table_size ( dkhash_table t)

Get actual table size (in number of buckets)

Parameters
tThe hash table
Returns
Number of bucket-slots in hash table

◆ dkhash_walk_data()

void dkhash_walk_data ( dkhash_table t,
int(*)(void *data)  walker 
)

Call a function once for each item in the hash-table The callback function can return DKHASH_WALK_{REMOVE,STOP} or any OR'ed combination thereof to control the walking procedure, and should return 0 on the normal case.

Parameters
tThe hash table
walkerThe callback function to send the data to