Skip to content

Commit f7a29f4

Browse files
committed
[nrf fromlist] settings: zms: code style clean up
Clean some parts of the code and refactor it to avoid multiple nested conditions Upstream PR #: 87792 Signed-off-by: Riadh Ghaddab <[email protected]> (cherry picked from commit a11a6114156690aa3271193ca05f1210c8686ec2)
1 parent 0efeab4 commit f7a29f4

File tree

1 file changed

+41
-29
lines changed

1 file changed

+41
-29
lines changed

subsys/settings/src/settings_zms.c

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,8 @@ static int settings_zms_load_subtree(struct settings_store *cs, const struct set
254254
ret = settings_call_set_handler(arg->subtree, rc2, settings_zms_read_fn,
255255
&read_fn_arg, (void *)arg);
256256
/* We should return here as there are no need to look for the next
257-
* hash collision */
257+
* hash collision
258+
*/
258259
return ret;
259260
}
260261

@@ -660,6 +661,44 @@ static ssize_t settings_zms_get_val_len(struct settings_store *cs, const char *n
660661
return 0;
661662
}
662663

664+
/* This function inits the linked list head if it doesn't exist or recover it
665+
* if the ll_last_hash_id is different than the head hash ID
666+
*/
667+
static int settings_zms_init_or_recover_ll(struct settings_zms *cf, uint32_t ll_last_hash_id)
668+
{
669+
struct settings_hash_linked_list settings_element;
670+
int rc = 0;
671+
672+
if (ll_last_hash_id == ZMS_LL_HEAD_HASH_ID) {
673+
/* header doesn't exist */
674+
settings_element.previous_hash = 0;
675+
settings_element.next_hash = 0;
676+
rc = zms_write(&cf->cf_zms, ZMS_LL_HEAD_HASH_ID, &settings_element,
677+
sizeof(struct settings_hash_linked_list));
678+
if (rc < 0) {
679+
return rc;
680+
}
681+
cf->last_hash_id = ZMS_LL_HEAD_HASH_ID;
682+
cf->second_to_last_hash_id = 0;
683+
#ifdef CONFIG_SETTINGS_ZMS_LL_CACHE
684+
/* store the LL header in cache */
685+
cf->ll_cache_next = 0;
686+
cf->ll_cache[cf->ll_cache_next++] = settings_element;
687+
#endif
688+
} else {
689+
/* let's recover it by keeping all nodes until the last one */
690+
settings_element.previous_hash = cf->second_to_last_hash_id;
691+
settings_element.next_hash = 0;
692+
rc = zms_write(&cf->cf_zms, cf->last_hash_id, &settings_element,
693+
sizeof(struct settings_hash_linked_list));
694+
if (rc < 0) {
695+
return rc;
696+
}
697+
}
698+
699+
return 0;
700+
}
701+
663702
static int settings_zms_get_last_hash_ids(struct settings_zms *cf)
664703
{
665704
struct settings_hash_linked_list settings_element;
@@ -679,34 +718,7 @@ static int settings_zms_get_last_hash_ids(struct settings_zms *cf)
679718
/* header doesn't exist or linked list broken, reinitialize the header
680719
* if it doesn't exist and recover it if it is broken
681720
*/
682-
if (ll_last_hash_id == ZMS_LL_HEAD_HASH_ID) {
683-
/* header doesn't exist */
684-
const struct settings_hash_linked_list settings_element = {
685-
.previous_hash = 0, .next_hash = 0};
686-
rc = zms_write(&cf->cf_zms, ZMS_LL_HEAD_HASH_ID, &settings_element,
687-
sizeof(struct settings_hash_linked_list));
688-
if (rc < 0) {
689-
return rc;
690-
}
691-
cf->last_hash_id = ZMS_LL_HEAD_HASH_ID;
692-
cf->second_to_last_hash_id = 0;
693-
#ifdef CONFIG_SETTINGS_ZMS_LL_CACHE
694-
/* store the LL header in cache */
695-
cf->ll_cache_next = 0;
696-
cf->ll_cache[cf->ll_cache_next++] = settings_element;
697-
#endif
698-
} else {
699-
/* let's recover it by keeping all nodes until the last one */
700-
const struct settings_hash_linked_list settings_element = {
701-
.previous_hash = cf->second_to_last_hash_id,
702-
.next_hash = 0};
703-
rc = zms_write(&cf->cf_zms, cf->last_hash_id, &settings_element,
704-
sizeof(struct settings_hash_linked_list));
705-
if (rc < 0) {
706-
return rc;
707-
}
708-
}
709-
return 0;
721+
return settings_zms_init_or_recover_ll(cf, ll_last_hash_id);
710722
} else if (rc < 0) {
711723
return rc;
712724
}

0 commit comments

Comments
 (0)