1 /*
2  * Copyright (c) 2022 Meta
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @ingroup hashmap_implementations
10  * @brief Separate Chaining Hashmap Implementation
11  *
12  * @note Enable with @kconfig{CONFIG_SYS_HASH_MAP_SC}
13  */
14 
15 #ifndef ZEPHYR_INCLUDE_SYS_HASH_MAP_SC_H_
16 #define ZEPHYR_INCLUDE_SYS_HASH_MAP_SC_H_
17 
18 #include <stdbool.h>
19 #include <stddef.h>
20 #include <stdint.h>
21 #include <stdlib.h>
22 
23 #include <zephyr/sys/hash_function.h>
24 #include <zephyr/sys/hash_map_api.h>
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 /**
31  * @brief Declare a Separate Chaining Hashmap (advanced)
32  *
33  * Declare a Separate Chaining Hashmap with control over advanced parameters.
34  *
35  * @note The allocator @p _alloc_func 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  * @param _hash_func Hash function pointer of type @ref sys_hash_func32_t.
40  * @param _alloc_func Allocator function pointer of type @ref sys_hashmap_allocator_t.
41  * @param ... Details for @ref sys_hashmap_config.
42  */
43 #define SYS_HASHMAP_SC_DEFINE_ADVANCED(_name, _hash_func, _alloc_func, ...)                        \
44 	SYS_HASHMAP_DEFINE_ADVANCED(_name, &sys_hashmap_sc_api, sys_hashmap_config,                \
45 				    sys_hashmap_data, _hash_func, _alloc_func, __VA_ARGS__)
46 
47 /**
48  * @brief Declare a Separate Chaining Hashmap (advanced)
49  *
50  * Declare a Separate Chaining Hashmap with control over advanced parameters.
51  *
52  * @note The allocator @p _alloc is used for allocating internal Hashmap
53  * entries and does not interact with any user-provided keys or values.
54  *
55  * @param _name Name of the Hashmap.
56  * @param _hash_func Hash function pointer of type @ref sys_hash_func32_t.
57  * @param _alloc_func Allocator function pointer of type @ref sys_hashmap_allocator_t.
58  * @param ... Details for @ref sys_hashmap_config.
59  */
60 #define SYS_HASHMAP_SC_DEFINE_STATIC_ADVANCED(_name, _hash_func, _alloc_func, ...)                 \
61 	SYS_HASHMAP_DEFINE_STATIC_ADVANCED(_name, &sys_hashmap_sc_api, sys_hashmap_config,         \
62 					   sys_hashmap_data, _hash_func, _alloc_func, __VA_ARGS__)
63 
64 /**
65  * @brief Declare a Separate Chaining Hashmap statically
66  *
67  * Declare a Separate Chaining Hashmap statically with default parameters.
68  *
69  * @param _name Name of the Hashmap.
70  */
71 #define SYS_HASHMAP_SC_DEFINE_STATIC(_name)                                                        \
72 	SYS_HASHMAP_SC_DEFINE_STATIC_ADVANCED(                                                     \
73 		_name, sys_hash32, SYS_HASHMAP_DEFAULT_ALLOCATOR,                                  \
74 		SYS_HASHMAP_CONFIG(SIZE_MAX, SYS_HASHMAP_DEFAULT_LOAD_FACTOR))
75 
76 /**
77  * @brief Declare a Separate Chaining Hashmap
78  *
79  * Declare a Separate Chaining Hashmap with default parameters.
80  *
81  * @param _name Name of the Hashmap.
82  */
83 #define SYS_HASHMAP_SC_DEFINE(_name)                                                               \
84 	SYS_HASHMAP_SC_DEFINE_ADVANCED(                                                            \
85 		_name, sys_hash32, SYS_HASHMAP_DEFAULT_ALLOCATOR,                                  \
86 		SYS_HASHMAP_CONFIG(SIZE_MAX, SYS_HASHMAP_DEFAULT_LOAD_FACTOR))
87 
88 #ifdef CONFIG_SYS_HASH_MAP_CHOICE_SC
89 #define SYS_HASHMAP_DEFAULT_DEFINE(_name)	 SYS_HASHMAP_SC_DEFINE(_name)
90 #define SYS_HASHMAP_DEFAULT_DEFINE_STATIC(_name) SYS_HASHMAP_SC_DEFINE_STATIC(_name)
91 #define SYS_HASHMAP_DEFAULT_DEFINE_ADVANCED(_name, _hash_func, _alloc_func, ...)                   \
92 	SYS_HASHMAP_SC_DEFINE_ADVANCED(_name, _hash_func, _alloc_func, __VA_ARGS__)
93 #define SYS_HASHMAP_DEFAULT_DEFINE_STATIC_ADVANCED(_name, _hash_func, _alloc_func, ...)            \
94 	SYS_HASHMAP_SC_DEFINE_STATIC_ADVANCED(_name, _hash_func, _alloc_func, __VA_ARGS__)
95 #endif
96 
97 extern const struct sys_hashmap_api sys_hashmap_sc_api;
98 
99 #ifdef __cplusplus
100 }
101 #endif
102 
103 #endif /* ZEPHYR_INCLUDE_SYS_HASH_MAP_SC_H_ */
104