Skip to content

Commit 33d978b

Browse files
committed
lib: os: Extend hasmap support
1 parent a6eef0b commit 33d978b

File tree

5 files changed

+148
-0
lines changed

5 files changed

+148
-0
lines changed

include/zephyr/sys/hash_map.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,48 @@
2727
extern "C" {
2828
#endif
2929

30+
/**
31+
* @brief Declare a Hashmap (advanced)
32+
*
33+
* Declare a Hashmap with control over advanced parameters.
34+
*
35+
* @param _name Name of the Hashmap.
36+
* @param _config_type Variant of @ref sys_hashmap_config.
37+
* @param _data_type Variant of @ref sys_hashmap_data.
38+
*/
39+
#define SYS_HASHMAP_DECLARE_ADVANCED(_name, _config_type, _data_type) \
40+
struct _config_type _name##_config; \
41+
struct _data_type _name##_data; \
42+
struct sys_hashmap _name;
43+
44+
/**
45+
* @brief Init a Hashmap (advanced)
46+
*
47+
* Init a Hashmap with control over advanced parameters.
48+
*
49+
* @note The allocator @p _alloc is used for allocating internal Hashmap
50+
* entries and does not interact with any user-provided keys or values.
51+
*
52+
* @param _name Name of the Hashmap.
53+
* @param _api API pointer of type @ref sys_hashmap_api.
54+
* @param _hash_func Hash function pointer of type @ref sys_hash_func32_t.
55+
* @param _alloc_func Allocator function pointer of type @ref sys_hashmap_allocator_t.
56+
* @param _max_size Maximum number of entries
57+
* @param _load_factor Maximum load factor of expressed in hundredths
58+
*/
59+
#define SYS_HASHMAP_INIT_ADVANCED(_name, _api, _hash_func, _alloc_func, \
60+
_max_size, _load_factor) \
61+
{ \
62+
SYS_HASHMAP_CONFIG_INIT(_name, _max_size, _load_factor) \
63+
memset(&_name##_data, 0, sizeof(_name##_data)); \
64+
memset(&_name, 0, sizeof(_name)); \
65+
_name.api = (const struct sys_hashmap_api *)(_api); \
66+
_name.config = (const struct sys_hashmap_config *)&_name##_config; \
67+
_name.data = (struct sys_hashmap_data *)&_name##_data; \
68+
_name.hash_func = (_hash_func); \
69+
_name.alloc_func = (_alloc_func); \
70+
}
71+
3072
/**
3173
* @brief Declare a Hashmap (advanced)
3274
*

include/zephyr/sys/hash_map_api.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,23 @@ struct sys_hashmap_config {
217217
.initial_n_buckets = NHPOT(DIV_ROUND_UP(100, _load_factor)), \
218218
}
219219

220+
/**
221+
* @brief Initialize @p sys_hashmap_config
222+
*
223+
* This macro helps to initialize a structure of type @p sys_hashmap_config.
224+
*
225+
* @param _name Name of the Hashmap
226+
* @param _max_size Maximum number of entries
227+
* @param _load_factor Maximum load factor of expressed in hundredths
228+
*/
229+
#define SYS_HASHMAP_CONFIG_INIT(_name, _max_size, _load_factor) \
230+
{ \
231+
memset(&_name##_config, 0, sizeof(_name##_config)); \
232+
_name##_config.max_size = (size_t)_max_size; \
233+
_name##_config.load_factor = (uint8_t)_load_factor; \
234+
_name##_config.initial_n_buckets = NHPOT(DIV_ROUND_UP(100, _load_factor)); \
235+
}
236+
220237
/**
221238
* @brief Generic Hashmap data
222239
*

include/zephyr/sys/hash_map_cxx.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,34 @@
2727
extern "C" {
2828
#endif
2929

30+
/**
31+
* @brief Declare a C++ Hashmap (advanced)
32+
*
33+
* Declare a C++ Hashmap with control over advanced parameters.
34+
*
35+
* @note The allocator @p _alloc is used for allocating internal Hashmap
36+
* entries and does not interact with any user-provided keys or values.
37+
*
38+
* @param _name Name of the Hashmap.
39+
*/
40+
#define SYS_HASHMAP_CXX_DECLARE(_name) \
41+
SYS_HASHMAP_DECLARE_ADVANCED(_name, sys_hashmap_config, sys_hashmap_data)
42+
43+
/**
44+
* @brief Init a C++ Hashmap (advanced)
45+
*
46+
* Init a C++ Hashmap with control over advanced parameters.
47+
*
48+
* @note The allocator @p _alloc is used for allocating internal Hashmap
49+
* entries and does not interact with any user-provided keys or values.
50+
*
51+
* @param _name Name of the Hashmap.
52+
* @param _hash_func Hash function pointer of type @ref sys_hash_func32_t.
53+
* @param _alloc_func Allocator function pointer of type @ref sys_hashmap_allocator_t.
54+
*/
55+
#define SYS_HASHMAP_CXX_INIT_ADVANCED(_name, _hash_func, _alloc_func, _max_size, _load_factor) \
56+
SYS_HASHMAP_INIT_ADVANCED(_name, &sys_hashmap_cxx_api, _hash_func, _alloc_func, _max_size, _load_factor)
57+
3058
/**
3159
* @brief Declare a C++ Hashmap (advanced)
3260
*
@@ -86,8 +114,11 @@ extern "C" {
86114
SYS_HASHMAP_CONFIG(SIZE_MAX, SYS_HASHMAP_DEFAULT_LOAD_FACTOR))
87115

88116
#ifdef CONFIG_SYS_HASH_MAP_CHOICE_CXX
117+
#define SYS_HASHMAP_DEFAULT_DECLARE(_name) SYS_HASHMAP_CXX_DECLARE(_name)
89118
#define SYS_HASHMAP_DEFAULT_DEFINE(_name) SYS_HASHMAP_CXX_DEFINE(_name)
90119
#define SYS_HASHMAP_DEFAULT_DEFINE_STATIC(_name) SYS_HASHMAP_CXX_DEFINE_STATIC(_name)
120+
#define SYS_HASHMAP_DEFAULT_INIT_ADVANCED(_name, _hash_func, _alloc_func, _max_size, _load_factor) \
121+
SYS_HASHMAP_CXX_INIT_ADVANCED(_name, _hash_func, _alloc_func, _max_size, _load_factor)
91122
#define SYS_HASHMAP_DEFAULT_DEFINE_ADVANCED(_name, _hash_func, _alloc_func, ...) \
92123
SYS_HASHMAP_CXX_DEFINE_ADVANCED(_name, _hash_func, _alloc_func, __VA_ARGS__)
93124
#define SYS_HASHMAP_DEFAULT_DEFINE_STATIC_ADVANCED(_name, _hash_func, _alloc_func, ...) \

include/zephyr/sys/hash_map_oa_lp.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,32 @@ struct sys_hashmap_oa_lp_data {
3131
size_t n_tombstones;
3232
};
3333

34+
/**
35+
* @brief Declare a Open Addressing Linear Probe Hashmap
36+
*
37+
* Declare a Open Addressing Linear Probe Hashmap
38+
*
39+
* @param _name Name of the Hashmap.
40+
*/
41+
#define SYS_HASHMAP_OA_LP_DECLARE(_name) \
42+
SYS_HASHMAP_DECLARE_ADVANCED(_name, sys_hashmap_config, sys_hashmap_oa_lp_data)
43+
44+
/**
45+
* @brief Init a Open Addressing Linear Probe Hashmap (advanced)
46+
*
47+
* Init a Open Addressing Linear Probe Hashmap with control over advanced parameters.
48+
*
49+
* @note The allocator @p _alloc is used for allocating internal Hashmap
50+
* entries and does not interact with any user-provided keys or values.
51+
*
52+
* @param _name Name of the Hashmap.
53+
* @param _hash_func Hash function pointer of type @ref sys_hash_func32_t.
54+
* @param _alloc_func Allocator function pointer of type @ref sys_hashmap_allocator_t.
55+
* @param ... Variant-specific details for @ref sys_hashmap_config.
56+
*/
57+
#define SYS_HASHMAP_OA_LP_INIT_ADVANCED(_name, _hash_func, _alloc_func, _max_size, _load_factor) \
58+
SYS_HASHMAP_INIT_ADVANCED(_name, &sys_hashmap_oa_lp_api, _hash_func, _alloc_func, _max_size, _load_factor)
59+
3460
/**
3561
* @brief Declare a Open Addressing Linear Probe Hashmap (advanced)
3662
*
@@ -91,8 +117,11 @@ struct sys_hashmap_oa_lp_data {
91117
SYS_HASHMAP_CONFIG(SIZE_MAX, SYS_HASHMAP_DEFAULT_LOAD_FACTOR))
92118

93119
#ifdef CONFIG_SYS_HASH_MAP_CHOICE_OA_LP
120+
#define SYS_HASHMAP_DEFAULT_DECLARE(_name) SYS_HASHMAP_OA_LP_DECLARE(_name)
94121
#define SYS_HASHMAP_DEFAULT_DEFINE(_name) SYS_HASHMAP_OA_LP_DEFINE(_name)
95122
#define SYS_HASHMAP_DEFAULT_DEFINE_STATIC(_name) SYS_HASHMAP_OA_LP_DEFINE_STATIC(_name)
123+
#define SYS_HASHMAP_DEFAULT_INIT_ADVANCED(_name, _hash_func, _alloc_func, _max_size, _load_factor) \
124+
SYS_HASHMAP_OA_LP_INIT_ADVANCED(_name, _hash_func, _alloc_func, _max_size, _load_factor)
96125
#define SYS_HASHMAP_DEFAULT_DEFINE_ADVANCED(_name, _hash_func, _alloc_func, ...) \
97126
SYS_HASHMAP_OA_LP_DEFINE_ADVANCED(_name, _hash_func, _alloc_func, __VA_ARGS__)
98127
#define SYS_HASHMAP_DEFAULT_DEFINE_STATIC_ADVANCED(_name, _hash_func, _alloc_func, ...) \

include/zephyr/sys/hash_map_sc.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,32 @@
2727
extern "C" {
2828
#endif
2929

30+
/**
31+
* @brief Declare a Separate Chaining Hashmap
32+
*
33+
* Declare a Separate Chaining Hashmap
34+
*
35+
* @param _name Name of the Hashmap.
36+
*/
37+
#define SYS_HASHMAP_SC_DECLARE(_name) \
38+
SYS_HASHMAP_DECLARE_ADVANCED(_name, sys_hashmap_config, sys_hashmap_data)
39+
40+
/**
41+
* @brief Init a Separate Chaining Hashmap (advanced)
42+
*
43+
* Init a Separate Chaining Hashmap with control over advanced parameters.
44+
*
45+
* @note The allocator @p _alloc_func is used for allocating internal Hashmap
46+
* entries and does not interact with any user-provided keys or values.
47+
*
48+
* @param _name Name of the Hashmap.
49+
* @param _hash_func Hash function pointer of type @ref sys_hash_func32_t.
50+
* @param _alloc_func Allocator function pointer of type @ref sys_hashmap_allocator_t.
51+
* @param ... Details for @ref sys_hashmap_config.
52+
*/
53+
#define SYS_HASHMAP_SC_INIT_ADVANCED(_name, _hash_func, _alloc_func, _max_size, _load_factor) \
54+
SYS_HASHMAP_INIT_ADVANCED(_name, &sys_hashmap_sc_api, _hash_func, _alloc_func, _max_size, _load_factor)
55+
3056
/**
3157
* @brief Declare a Separate Chaining Hashmap (advanced)
3258
*
@@ -86,8 +112,11 @@ extern "C" {
86112
SYS_HASHMAP_CONFIG(SIZE_MAX, SYS_HASHMAP_DEFAULT_LOAD_FACTOR))
87113

88114
#ifdef CONFIG_SYS_HASH_MAP_CHOICE_SC
115+
#define SYS_HASHMAP_DEFAULT_DECLARE(_name) SYS_HASHMAP_SC_DECLARE(_name)
89116
#define SYS_HASHMAP_DEFAULT_DEFINE(_name) SYS_HASHMAP_SC_DEFINE(_name)
90117
#define SYS_HASHMAP_DEFAULT_DEFINE_STATIC(_name) SYS_HASHMAP_SC_DEFINE_STATIC(_name)
118+
#define SYS_HASHMAP_DEFAULT_INIT_ADVANCED(_name, _hash_func, _alloc_func, _max_size, _load_factor) \
119+
SYS_HASHMAP_SC_INIT_ADVANCED(_name, _hash_func, _alloc_func, _max_size, _load_factor)
91120
#define SYS_HASHMAP_DEFAULT_DEFINE_ADVANCED(_name, _hash_func, _alloc_func, ...) \
92121
SYS_HASHMAP_SC_DEFINE_ADVANCED(_name, _hash_func, _alloc_func, __VA_ARGS__)
93122
#define SYS_HASHMAP_DEFAULT_DEFINE_STATIC_ADVANCED(_name, _hash_func, _alloc_func, ...) \

0 commit comments

Comments
 (0)