13
13
#include <zephyr/sys/crc.h>
14
14
#include "zms_priv.h"
15
15
#ifdef CONFIG_ZMS_LOOKUP_CACHE_FOR_SETTINGS
16
+ #ifdef CONFIG_SETTINGS_ZMS_LEGACY
17
+ #include <settings/settings_zms_legacy.h>
18
+ #else
16
19
#include <settings/settings_zms.h>
17
20
#endif
21
+ #endif
18
22
19
23
#include <zephyr/logging/log.h>
20
24
LOG_MODULE_REGISTER (fs_zms , CONFIG_ZMS_LOG_LEVEL );
@@ -34,6 +38,39 @@ static inline size_t zms_lookup_cache_pos(uint32_t id)
34
38
uint32_t hash = id ;
35
39
36
40
#ifdef CONFIG_ZMS_LOOKUP_CACHE_FOR_SETTINGS
41
+ #ifdef CONFIG_SETTINGS_ZMS_LEGACY
42
+ /*
43
+ * 1. The ZMS settings backend uses up to (ZMS_NAME_ID_OFFSET - 1) ZMS IDs to
44
+ store keys and equal number of ZMS IDs to store values.
45
+ * 2. For each key-value pair, the value is stored at ZMS ID greater by exactly
46
+ * ZMS_NAME_ID_OFFSET than ZMS ID that holds the key.
47
+ * 3. The backend tries to minimize the range of ZMS IDs used to store keys.
48
+ * That is, ZMS IDs are allocated sequentially, and freed ZMS IDs are reused
49
+ * before allocating new ones.
50
+ *
51
+ * Therefore, to assure the least number of collisions in the lookup cache,
52
+ * the least significant bit of the hash indicates whether the given ZMS ID
53
+ * represents a key or a value, and remaining bits of the hash are set to
54
+ * the ordinal number of the key-value pair. Consequently, the hash function
55
+ * provides the following mapping:
56
+ *
57
+ * 1st settings key => hash 0
58
+ * 1st settings value => hash 1
59
+ * 2nd settings key => hash 2
60
+ * 2nd settings value => hash 3
61
+ * ...
62
+ */
63
+ BUILD_ASSERT (IS_POWER_OF_TWO (ZMS_NAMECNT_ID ), "ZMS_NAMECNT_ID is not power of 2" );
64
+ BUILD_ASSERT (IS_POWER_OF_TWO (ZMS_NAME_ID_OFFSET ), "ZMS_NAME_ID_OFFSET is not power of 2" );
65
+
66
+ uint32_t key_value_bit ;
67
+ uint32_t key_value_ord ;
68
+
69
+ key_value_bit = (id >> LOG2 (ZMS_NAME_ID_OFFSET )) & 1 ;
70
+ key_value_ord = id & (ZMS_NAME_ID_OFFSET - 1 );
71
+
72
+ hash = ((key_value_ord << 1 ) | key_value_bit );
73
+ #else
37
74
/*
38
75
* 1. Settings subsystem is storing the name ID and the linked list node ID
39
76
* with only one bit difference at BIT(0).
@@ -58,6 +95,7 @@ static inline size_t zms_lookup_cache_pos(uint32_t id)
58
95
key_value_ll = id & BIT (0 );
59
96
60
97
hash = (key_value_hash << 2 ) | (key_value_bit << 1 ) | key_value_ll ;
98
+ #endif /* CONFIG_SETTINGS_ZMS_LEGACY */
61
99
#else
62
100
/* 32-bit integer hash function found by https://github.com/skeeto/hash-prospector. */
63
101
hash ^= hash >> 16 ;
0 commit comments