XROAD
xroad_hashmap.h File Reference
#include "xroad_common_fwd.h"
#include "xroad_common_types.h"
#include "xroad_aux.h"
#include <stdlib.h>
#include <stddef.h>
#include <stdalign.h>
Include dependency graph for xroad_hashmap.h:

Go to the source code of this file.

Classes

struct  xroad_hashmap_va_args_t
 
struct  xroad_hashmap_insert_result_t
 

Macros

#define XROAD_HASHMAP_ENTRY_STRUCT(key_type, value_type)   struct { int8_t info; key_type key; value_type value; int8_t last_member; }
 
#define XROAD_HASHMAP_ENTRY_KEY_OFFSET(key_type, value_type)   offsetof(XROAD_HASHMAP_ENTRY_STRUCT(key_type, value_type), key)
 
#define XROAD_HASHMAP_ENTRY_VALUE_OFFSET(key_type, value_type)   offsetof(XROAD_HASHMAP_ENTRY_STRUCT(key_type, value_type), value)
 
#define XROAD_HASHMAP_ENTRY_SIZE(key_type, value_type)   offsetof(XROAD_HASHMAP_ENTRY_STRUCT(key_type, value_type), last_member)
 
#define xroad_hashmap_create(key_type, value_type, ...)
 
#define __xroad_hashmap_dtor__   __dtor__(xroad_hashmap_destructor)
 

Typedefs

typedef uint32_t(* xroad_hashmap_hash_func_t) (const void *, uint32_t)
 
typedef int32_t(* xroad_hashmap_equal_func_t) (const void *, const void *, uint32_t)
 
typedef void(* xroad_hashmap_free_func_t) (void *)
 

Functions

uint32_t xroad_hashmap_hash_func_cstr (const void *, uint32_t)
 
int32_t xroad_hashmap_equal_func_cstr (const void *, const void *, uint32_t)
 
void xroad_hashmap_free_func_cstr (void *)
 
uint32_t xroad_hashmap_hash_func_xroad_str (const void *, uint32_t)
 
int32_t xroad_hashmap_equal_func_xroad_str (const void *, const void *, uint32_t)
 
void xroad_hashmap_free_func_xroad_str (void *)
 
xroad_hashmap_txroad_hashmap_create_ex (uint32_t sizeof_key, uint32_t sizeof_value, uint32_t sizeof_entry, uint32_t key_offset, uint32_t value_offset, uint32_t initiad_size, xroad_hashmap_hash_func_t hash_func, xroad_hashmap_equal_func_t equal_func, xroad_hashmap_free_func_t key_free_func, xroad_hashmap_free_func_t value_free_func, xroad_mem_pool_t *pool)
 
void xroad_hashmap_destroy (xroad_hashmap_t *hashmap)
 
void xroad_hashmap_destructor (void *hashmap_ptr)
 
void xroad_hashmap_reserve (xroad_hashmap_t *hashmap, uint32_t size)
 
xroad_hashmap_insert_result_t xroad_hashmap_insert (xroad_hashmap_t *hashmap, const void *key, const void *value)
 
xroad_hashmap_entry_t * xroad_hashmap_find (xroad_hashmap_t *hashmap, const void *key)
 
void xroad_hashmap_clear (xroad_hashmap_t *hashmap)
 
uint32_t xroad_hashmap_get_key_size (const xroad_hashmap_t *hashmap)
 
uint32_t xroad_hashmap_get_value_size (const xroad_hashmap_t *hashmap)
 
xroad_errno_t xroad_hashmap_delete (xroad_hashmap_t *hashmap, const void *key, bool free_resources)
 
uint32_t xroad_hashmap_get_size (xroad_hashmap_t *hashmap)
 
xroad_list_txroad_hashmap_keys_to_list (xroad_hashmap_t *hashmap)
 
xroad_list_txroad_hashmap_values_to_list (xroad_hashmap_t *hashmap)
 
void * xroad_hashmap_entry_get_key (xroad_hashmap_t *hashmap, xroad_hashmap_entry_t *entry)
 
void * xroad_hashmap_entry_get_value (xroad_hashmap_t *hashmap, xroad_hashmap_entry_t *entry)
 
void xroad_hashmap_entry_set_value (xroad_hashmap_t *hashmap, xroad_hashmap_entry_t *entry, const void *value, bool free_resources)
 
void xroad_hashmap_entry_delete (xroad_hashmap_t *hashmap, xroad_hashmap_entry_t *entry, bool free_resources)
 
xroad_hashmap_entry_t * xroad_hashmap_entry_delete_and_get_next (xroad_hashmap_t *hashmap, xroad_hashmap_entry_t *entry, bool free_resources)
 
xroad_hashmap_entry_t * xroad_hashmap_get_first (xroad_hashmap_t *hashmap)
 
xroad_hashmap_entry_t * xroad_hashmap_entry_get_next (xroad_hashmap_t *hashmap, xroad_hashmap_entry_t *entry)
 

Detailed Description

Macro Definition Documentation

◆ xroad_hashmap_create

#define xroad_hashmap_create (   key_type,
  value_type,
  ... 
)
Value:
({ \
xroad_hashmap_va_args_t args = {__VA_ARGS__}; \
xroad_hashmap_create_ex(sizeof(key_type), sizeof(value_type), \
XROAD_HASHMAP_ENTRY_SIZE(key_type, value_type), \
XROAD_HASHMAP_ENTRY_KEY_OFFSET(key_type, value_type), \
XROAD_HASHMAP_ENTRY_VALUE_OFFSET(key_type, value_type), \
args.initial_size, args.hash, args.equal, \
args.key_dtor, args.value_dtor, args.pool); \
})

◆ XROAD_HASHMAP_ENTRY_STRUCT

#define XROAD_HASHMAP_ENTRY_STRUCT (   key_type,
  value_type 
)    struct { int8_t info; key_type key; value_type value; int8_t last_member; }

sizeof(struct { ... }) isn't valid expression in C++, so we use offsetof(struct { ... }, last_member) instead as a workaround

Typedef Documentation

◆ xroad_hashmap_equal_func_t

typedef int32_t(* xroad_hashmap_equal_func_t) (const void *, const void *, uint32_t)

equal function type

◆ xroad_hashmap_free_func_t

typedef void(* xroad_hashmap_free_func_t) (void *)

free function type

◆ xroad_hashmap_hash_func_t

typedef uint32_t(* xroad_hashmap_hash_func_t) (const void *, uint32_t)

hash function type

Function Documentation

◆ xroad_hashmap_clear()

void xroad_hashmap_clear ( xroad_hashmap_t hashmap)

delete all entries from hash table

Parameters
[in]hashmap- instance of hashmap

◆ xroad_hashmap_create_ex()

xroad_hashmap_t* xroad_hashmap_create_ex ( uint32_t  sizeof_key,
uint32_t  sizeof_value,
uint32_t  sizeof_entry,
uint32_t  key_offset,
uint32_t  value_offset,
uint32_t  initiad_size,
xroad_hashmap_hash_func_t  hash_func,
xroad_hashmap_equal_func_t  equal_func,
xroad_hashmap_free_func_t  key_free_func,
xroad_hashmap_free_func_t  value_free_func,
xroad_mem_pool_t *  pool 
)

create new hashmap

Parameters
[in]sizeof_key- sizeof key
[in]sizeof_value- sizeof value
[in]key_offet- alignof key
[in]value_offset- alignof value
[in]initiad_size- initial size of hashmap. Can be 0
[in]max_load_factor- max load factor. Can be 0, so max load factor = 0.5 will be used
[in]hash_func- hash function, if NULL - default function is used
[in]equal_func- equality function, used for entry search. if NULL - default function is used
[in]key_free_func- key destructor function. if NULL - no free function will be used
[in]value_free_func- value destructor function. if NULL - no free function will be used
[in]pool- memory pool if any. Can be NULL, so libc malloc/calloc will be used
Returns
pointer to new hashmap, NULL - error

◆ xroad_hashmap_delete()

xroad_errno_t xroad_hashmap_delete ( xroad_hashmap_t hashmap,
const void *  key,
bool  free_resources 
)

delete entry from hashmap

Parameters
[in]hashmap- instance of hashmap
[in]key- key to delete
[in]free_resources- if true free all allocated resources using key_dtor and value_dtor from hashmap constructor
Returns
XROAD_OK - found and deleted, XROAD_ERROR_NOT_FOUND - entry with key not found

◆ xroad_hashmap_destroy()

void xroad_hashmap_destroy ( xroad_hashmap_t hashmap)

delete hashmap and free all resources

Parameters
[in]hashmap- hash table to delete. If hash is NULL, nothing happened

◆ xroad_hashmap_entry_delete()

void xroad_hashmap_entry_delete ( xroad_hashmap_t hashmap,
xroad_hashmap_entry_t *  entry,
bool  free_resources 
)

delete entry

Parameters
[in]hashmap- instance of hashmap
[in]entry- entry to delete
[in]free_resources- if true free all allocated resources using key_dtor and value_dtor from hashmap constructor

◆ xroad_hashmap_entry_delete_and_get_next()

xroad_hashmap_entry_t* xroad_hashmap_entry_delete_and_get_next ( xroad_hashmap_t hashmap,
xroad_hashmap_entry_t *  entry,
bool  free_resources 
)

delete entry and return next entry, NULL if there are no more

Parameters
[in]hashmap- instance of hashmap
[in]entry- entry to delete
[in]free_resources- if true free all allocated resources using key_dtor and value_dtor from hashmap constructor
Returns
next entry, NULL - no more entries

◆ xroad_hashmap_entry_get_key()

void* xroad_hashmap_entry_get_key ( xroad_hashmap_t hashmap,
xroad_hashmap_entry_t *  entry 
)

return entry key

Parameters
[in]hashmap- instance of hashmap
[in]entry- entry to explore
Returns
entry key

◆ xroad_hashmap_entry_get_next()

xroad_hashmap_entry_t* xroad_hashmap_entry_get_next ( xroad_hashmap_t hashmap,
xroad_hashmap_entry_t *  entry 
)

return next entry from hashmap

Parameters
[in]entry- entry
Returns
next entry, NULL - no more entries

◆ xroad_hashmap_entry_get_value()

void* xroad_hashmap_entry_get_value ( xroad_hashmap_t hashmap,
xroad_hashmap_entry_t *  entry 
)

return entry value

Parameters
[in]hashmap- instance of hashmap
[in]entry- entry to explore
Returns
entry value

◆ xroad_hashmap_entry_set_value()

void xroad_hashmap_entry_set_value ( xroad_hashmap_t hashmap,
xroad_hashmap_entry_t *  entry,
const void *  value,
bool  free_resources 
)

set entry value

Parameters
[in]hashmap- instance of hashmap
[in]entry- entry to set
[in]value- new value to set
[in]free_resources- if true free previously allocated resources using key_dtor and value_dtor from hashmap constructor

◆ xroad_hashmap_find()

xroad_hashmap_entry_t* xroad_hashmap_find ( xroad_hashmap_t hashmap,
const void *  key 
)

find entry in hash table

Parameters
[in]hashmap- instance of hashmap
[in]key- key to find
Returns
entry or NULL if not found

◆ xroad_hashmap_get_first()

xroad_hashmap_entry_t* xroad_hashmap_get_first ( xroad_hashmap_t hashmap)

return first entry from hashmap

Parameters
[in]hashmap- instance of hashmap
Returns
first entry, NULL - no entries

◆ xroad_hashmap_get_key_size()

uint32_t xroad_hashmap_get_key_size ( const xroad_hashmap_t hashmap)

return entry key size

Parameters
[in]hashmap- instance of hashmap
Returns
entry key size

◆ xroad_hashmap_get_size()

uint32_t xroad_hashmap_get_size ( xroad_hashmap_t hashmap)

return number of entries in hashmap

Parameters
[in]hashmap- instance of hashmap
Returns
number of entries in hashmap

◆ xroad_hashmap_get_value_size()

uint32_t xroad_hashmap_get_value_size ( const xroad_hashmap_t hashmap)

return entry value size

Parameters
[in]hashmap- instance of hashmap
Returns
entry value size

◆ xroad_hashmap_hash_func_cstr()

uint32_t xroad_hashmap_hash_func_cstr ( const void *  ,
uint32_t   
)

default hashmap functions for cstr: -> xroad_hashmap_hash_func_cstr hashes strlen() bytes -> xroad_hashmap_equal_func_cstr use strcmp for compare -> xroad_hashmap_free_func_cstr use free() to free allocated memory

◆ xroad_hashmap_hash_func_xroad_str()

uint32_t xroad_hashmap_hash_func_xroad_str ( const void *  ,
uint32_t   
)

default hashmap functions for xroad_str: -> xroad_hashmap_hash_func_xroad_str hashes str.len bytes -> xroad_hashmap_equal_func_xroad_str use xroad_str_cmp for compare -> xroad_hashmap_free_func_xroad_str use free() to free allocated memory

◆ xroad_hashmap_insert()

xroad_hashmap_insert_result_t xroad_hashmap_insert ( xroad_hashmap_t hashmap,
const void *  key,
const void *  value 
)

insert new entry into hashmap

Parameters
[in]hashmap- instance of hashmap
[in]key- pointer to key data
[in]value- pointer to value data
Returns
result of insertion. entry - result of insertion or existing entry, errno - XROAD_OK - inserted, XROAD_ERROR_ALREADY_EXISTS - entry with the same key already exists

◆ xroad_hashmap_keys_to_list()

xroad_list_t* xroad_hashmap_keys_to_list ( xroad_hashmap_t hashmap)

convert hashmap keys to list

Parameters
[in]hashmap- instance of hashmap to convert
Returns
new list structure

◆ xroad_hashmap_reserve()

void xroad_hashmap_reserve ( xroad_hashmap_t hashmap,
uint32_t  size 
)

reserve space for size items

Parameters
[in]hashmap- instance of hashmap
[in]size- size

◆ xroad_hashmap_values_to_list()

xroad_list_t* xroad_hashmap_values_to_list ( xroad_hashmap_t hashmap)

convert hashmap values to list

Parameters
[in]hashmap- instance of hashmap to convert
Returns
new list structure