CNDP  22.08.0
hmap.h File Reference
#include <stdbool.h>
#include <string.h>
#include <strings.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/queue.h>
#include <pthread.h>
#include "cne_common.h"
#include "cne_log.h"

Go to the source code of this file.

Data Structures

struct  hmap_kvp
 
struct  hmap
 

Macros

#define HMAP_MAX_NAME_SIZE   32
 
#define HMAP_MAX_KEY_SIZE   256
 
#define HMAP_STARTING_CAPACITY   32
 
#define HMAP_DEFAULT_CAPACITY   1024
 

Typedefs

typedef struct hmap_kvp hmap_kvp_t
 
typedef struct hmap hmap_t
 

Functions

CNDP_API hmap_thmap_create (const char *name, uint32_t capacity, hmap_funcs_t *funcs)
 
CNDP_API int hmap_destroy (hmap_t *hmap)
 
CNDP_API int hmap_destroy_by_name (const char *name)
 
CNDP_API hmap_kvp_thmap_kvp_lookup (hmap_t *hmap, const char *prefix, const char *key)
 
CNDP_API int hmap_lookup (hmap_t *hmap, const char *prefix, const char *key, hmap_val_t *val)
 
CNDP_API int hmap_add (hmap_t *hmap, hmap_type_t type, const char *prefix, const char *key, hmap_val_t *val)
 
CNDP_API int hmap_kvp_update (hmap_t *hmap, hmap_kvp_t *kvp, hmap_val_t *val)
 
static int hmap_add_string (hmap_t *hmap, const char *prefix, const char *key, const char *val)
 
CNDP_API int hmap_del (hmap_t *hmap, const char *prefix, const char *key)
 
CNDP_API int hmap_iterate (hmap_t *hmap, hmap_kvp_t **_kvp, uint32_t *next)
 
CNDP_API void hmap_dump (FILE *f, hmap_t *hmap, int sort)
 
static uint32_t hmap_capacity (hmap_t *hmap)
 
static uint32_t hmap_count (hmap_t *hmap)
 
CNDP_API hmap_funcs_t * hmap_get_funcs (hmap_t *hmap)
 
CNDP_API int hmap_set_funcs (hmap_t *hmap, hmap_funcs_t *funcs)
 
static hmap_kvp_t__get_kvp (hmap_t *hmap, const char *prefix, const char *key, hmap_type_t type)
 
static int hmap_get_bool (hmap_t *hmap, const char *prefix, const char *key, bool *val)
 
static int hmap_get_u64 (hmap_t *hmap, const char *prefix, const char *key, uint64_t *val)
 
static int hmap_get_u32 (hmap_t *hmap, const char *prefix, const char *key, uint32_t *val)
 
static int hmap_get_u16 (hmap_t *hmap, const char *prefix, const char *key, uint16_t *val)
 
static int hmap_get_u8 (hmap_t *hmap, const char *prefix, const char *key, uint8_t *val)
 
static int hmap_get_num (hmap_t *hmap, const char *prefix, const char *key, int *val)
 
static int hmap_get_num64 (hmap_t *hmap, const char *prefix, const char *key, int64_t *val)
 
static int hmap_get_string (hmap_t *hmap, const char *prefix, const char *key, char **val)
 
static int hmap_get_pointer (hmap_t *hmap, const char *prefix, const char *key, void **val)
 
void hmap_list_dump (FILE *f, int sort)
 

Detailed Description

This library provides an API for the hashmap data structure

Definition in file hmap.h.

Macro Definition Documentation

◆ HMAP_MAX_NAME_SIZE

#define HMAP_MAX_NAME_SIZE   32

MAX size of HMAP name

Definition at line 31 of file hmap.h.

◆ HMAP_MAX_KEY_SIZE

#define HMAP_MAX_KEY_SIZE   256

Key size

Definition at line 32 of file hmap.h.

◆ HMAP_STARTING_CAPACITY

#define HMAP_STARTING_CAPACITY   32

Starting capacity not exceeding max_capacity

Definition at line 33 of file hmap.h.

◆ HMAP_DEFAULT_CAPACITY

#define HMAP_DEFAULT_CAPACITY   1024

Default starting capacity not exceeding max_capacity

Definition at line 34 of file hmap.h.

Typedef Documentation

◆ hmap_kvp_t

typedef struct hmap_kvp hmap_kvp_t

A structure used to retrieve information of a key-value-pair hmap

◆ hmap_t

typedef struct hmap hmap_t

A structure used to retrieve information of a hmap

Function Documentation

◆ hmap_create()

CNDP_API hmap_t* hmap_create ( const char *  name,
uint32_t  capacity,
hmap_funcs_t *  funcs 
)

Create a hashmap structure with a fixed capacity hash size

Parameters
nameA string name for this hashmap
capacityThe size of the hash table
funcsOptional pointer to hmap_funcs_t structure to allow user to set hmap functions.
Returns
NULL if error or the hmap_t pointer

◆ hmap_destroy()

CNDP_API int hmap_destroy ( hmap_t hmap)

Destroy a hashmap table

Parameters
hmapPointer to the hmap structure
Returns
0 - successful or -1 on error

◆ hmap_destroy_by_name()

CNDP_API int hmap_destroy_by_name ( const char *  name)

Destroy the hmap using its name

Parameters
nameThe name of the hmap
Returns
0 on success or -1 on error.

◆ hmap_kvp_lookup()

CNDP_API hmap_kvp_t* hmap_kvp_lookup ( hmap_t hmap,
const char *  prefix,
const char *  key 
)

Lookup a prefix/key value in the hashmap

Parameters
hmapPointer to the hmap structure
prefixThe prefix string
keyThe key to search for in the hashmap
Returns
NULL on error or hmap_kvp_t pointer

◆ hmap_lookup()

CNDP_API int hmap_lookup ( hmap_t hmap,
const char *  prefix,
const char *  key,
hmap_val_t *  val 
)

Lookup a prefix/key value in the hashmap

Parameters
hmapPointer to the hmap structure
prefixThe prefix string
keyThe key to search for in the hashmap
valPointer to hmap_val_t to return value can be NULL for no return value
Returns
0 - successful or -1 on error

◆ hmap_add()

CNDP_API int hmap_add ( hmap_t hmap,
hmap_type_t  type,
const char *  prefix,
const char *  key,
hmap_val_t *  val 
)

Add a key/value pair the hashmap table

Parameters
hmapPointer to the hmap structure
typeThe hmap_type_t type for the value pointer
prefixThe prefix string to locate in the hashmap table
keyThe key value string to add
valThe value pointer to store with the key/value entry.
Returns
0 on success or -1 on error.

◆ hmap_kvp_update()

CNDP_API int hmap_kvp_update ( hmap_t hmap,
hmap_kvp_t kvp,
hmap_val_t *  val 
)

Update the value for the given key/value pair return by hmap_kvp_lookup().

Parameters
hmapPointer to hmap structure
kvpThe key/value pair structure to update
valThe pointer to the new value.
Returns
0 - successful update and -1 on error.

◆ hmap_add_string()

static int hmap_add_string ( hmap_t hmap,
const char *  prefix,
const char *  key,
const char *  val 
)
inlinestatic

Add a key/value pair the hashmap table for all of the types allowed.

Parameters
hmapPointer to the hmap structure
prefixThe prefix string to locate in the hashmap table
keyThe key value string to add
valThe value to be stored in the hmap table.
Returns
0 on success or -1 on error.

Definition at line 212 of file hmap.h.

◆ hmap_del()

CNDP_API int hmap_del ( hmap_t hmap,
const char *  prefix,
const char *  key 
)

Delete a hashmap entry

Parameters
hmapPointer to the hmap structure
prefixThe prefix string for the hashmap
keyThe Key to search for in the table
Returns
-1 on error or 0 on success

◆ hmap_iterate()

CNDP_API int hmap_iterate ( hmap_t hmap,
hmap_kvp_t **  _kvp,
uint32_t *  next 
)

Iterate over the all of the entries in the hashmap

Parameters
hmapPointer to the hmap structure
_kvpThe address to place the key/value pair structure pointer
nextThe next entry to iterate as an index value.
Returns
0 on success or -1 on error

◆ hmap_dump()

CNDP_API void hmap_dump ( FILE *  f,
hmap_t hmap,
int  sort 
)

Dump out all of the entries in a hashmap

Parameters
fThe file descriptor to use for output of the text
hmapPointer to the hmap structure to dump
sortThe sort flag is non-zero then sort the output

◆ hmap_capacity()

static uint32_t hmap_capacity ( hmap_t hmap)
inlinestatic

Get hmap MAX capacity

Parameters
hmapPointer to the hmap structure
Returns
The MAX capacity value.

Definition at line 332 of file hmap.h.

◆ hmap_count()

static uint32_t hmap_count ( hmap_t hmap)
inlinestatic

Return the current hmap capacity

Parameters
hmapPointer to the hmap structure
Returns
The current capacity of the hmap

Definition at line 346 of file hmap.h.

◆ hmap_get_funcs()

CNDP_API hmap_funcs_t* hmap_get_funcs ( hmap_t hmap)

Get the set of hmap function pointers.

Parameters
hmapThe hmap structure pointer
Returns
NULL on error or pointer to the hmap function structure.

◆ hmap_set_funcs()

CNDP_API int hmap_set_funcs ( hmap_t hmap,
hmap_funcs_t *  funcs 
)

Set the hashing function

Parameters
hmapPointer to the hmap structure
funcsPointer to a set of function pointers
Returns
-1 on if hmap is NULL or 0 on success

◆ __get_kvp()

static hmap_kvp_t* __get_kvp ( hmap_t hmap,
const char *  prefix,
const char *  key,
hmap_type_t  type 
)
inlinestatic

Get a key/value pair from the hmap if is exists. (internal)

Parameters
hmapPointer to the hmap structure
prefixThe prefix string pointer, can be NULL if a global value
keyThe key value string to get the boolean value.
typeThe key value type.
Returns
The key/value pair structure pointer or NULL if not found.

Definition at line 388 of file hmap.h.

◆ hmap_get_bool()

static int hmap_get_bool ( hmap_t hmap,
const char *  prefix,
const char *  key,
bool *  val 
)
inlinestatic

Get a boolean value from the hmap.

Parameters
hmapPointer to the hmap structure
prefixThe prefix string pointer, can be NULL if a global value
keyThe key value string to get the boolean value.
valThe location to store the value.
Returns
0 - successful or -1 - failed

Definition at line 417 of file hmap.h.

◆ hmap_get_u64()

static int hmap_get_u64 ( hmap_t hmap,
const char *  prefix,
const char *  key,
uint64_t *  val 
)
inlinestatic

Get a uint64_t value from the hmap.

Parameters
hmapPointer to the hmap structure
prefixThe prefix string pointer, can be NULL if a global value
keyThe key value string to get the uint64_t value.
valThe location to store the value.
Returns
0 - successful or -1 - failed

Definition at line 445 of file hmap.h.

◆ hmap_get_u32()

static int hmap_get_u32 ( hmap_t hmap,
const char *  prefix,
const char *  key,
uint32_t *  val 
)
inlinestatic

Get a uint32_t value from the hmap.

Parameters
hmapPointer to the hmap structure
prefixThe prefix string pointer, can be NULL if a global value
keyThe key value string to get the uint32_t value.
valThe location to store the value.
Returns
0 - successful or -1 - failed

Definition at line 473 of file hmap.h.

◆ hmap_get_u16()

static int hmap_get_u16 ( hmap_t hmap,
const char *  prefix,
const char *  key,
uint16_t *  val 
)
inlinestatic

Get a uint16_t value from the hmap.

Parameters
hmapPointer to the hmap structure
prefixThe prefix string pointer, can be NULL if a global value
keyThe key value string to get the uint16_t value.
valThe location to store the value.
Returns
0 - successful or -1 - failed

Definition at line 501 of file hmap.h.

◆ hmap_get_u8()

static int hmap_get_u8 ( hmap_t hmap,
const char *  prefix,
const char *  key,
uint8_t *  val 
)
inlinestatic

Get a uint8_t value from the hmap.

Parameters
hmapPointer to the hmap structure
prefixThe prefix string pointer, can be NULL if a global value
keyThe key value string to get the uint8_t value.
valThe location to store the value.
Returns
0 - successful or -1 - failed

Definition at line 529 of file hmap.h.

◆ hmap_get_num()

static int hmap_get_num ( hmap_t hmap,
const char *  prefix,
const char *  key,
int *  val 
)
inlinestatic

Get a number value from the hmap.

Parameters
hmapPointer to the hmap structure
prefixThe prefix string pointer, can be NULL if a global value
keyThe key value string to get the number value.
valThe location to store the value.
Returns
0 - successful or -1 - failed

Definition at line 557 of file hmap.h.

◆ hmap_get_num64()

static int hmap_get_num64 ( hmap_t hmap,
const char *  prefix,
const char *  key,
int64_t *  val 
)
inlinestatic

Get a number value from the hmap as a int64_t.

Parameters
hmapPointer to the hmap structure
prefixThe prefix string pointer, can be NULL if a global value
keyThe key value string to get the number value.
valThe location to store the value.
Returns
0 - successful or -1 - failed

Definition at line 585 of file hmap.h.

◆ hmap_get_string()

static int hmap_get_string ( hmap_t hmap,
const char *  prefix,
const char *  key,
char **  val 
)
inlinestatic

Get a string value from the hmap.

Parameters
hmapPointer to the hmap structure
prefixThe prefix string pointer, can be NULL if a global value
keyThe key value string to get the string value.
valA pointer to the return value or NULL if no return value is needed.
Returns
The string value if the key is found or NULL on error.

Definition at line 613 of file hmap.h.

◆ hmap_get_pointer()

static int hmap_get_pointer ( hmap_t hmap,
const char *  prefix,
const char *  key,
void **  val 
)
inlinestatic

Get a pointer value from the hmap.

Parameters
hmapPointer to the hmap structure
prefixThe prefix string pointer, can be NULL if a global value
keyThe key value string to get the string value.
valA pointer to the return value or NULL if no return value is needed.
Returns
0 - successful or -1 on error

Definition at line 641 of file hmap.h.

◆ hmap_list_dump()

void hmap_list_dump ( FILE *  f,
int  sort 
)

Dump all of the hmap lists

Parameters
fThe file descriptor to dump the text data.
sortif not zero then sort the output