Skip to content

Commit 5ea63d0

Browse files
committed
fs: fcb: struct flash_sector usually const
Generally, the flash_sector array is known at compile time and is immutable, therefore developers may like to store it in ROM. The only function that requires a mutable struct flash_sector seems to be flash_area_get_sectors(), allowing the developer to store a mutable sector definition in persistent RAM rather than ROM. An example of this approach is in setting_fcb.c. Signed-off-by: JP Hutchins <[email protected]>
1 parent e472fb6 commit 5ea63d0

File tree

13 files changed

+41
-76
lines changed

13 files changed

+41
-76
lines changed

include/zephyr/fs/fcb.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ extern "C" {
4949
* within that area.
5050
*/
5151
struct fcb_entry {
52-
struct flash_sector *fe_sector;
52+
const struct flash_sector *fe_sector;
5353
/**< Pointer to info about sector where data are placed */
5454

5555
uint32_t fe_elem_off;
@@ -109,14 +109,14 @@ struct fcb {
109109
* to have scratch space for garbage collecting when FCB fills up.
110110
*/
111111

112-
struct flash_sector *f_sectors;
112+
const struct flash_sector *f_sectors;
113113
/**< Array of sectors, must be contiguous */
114114

115115
/* Flash circular buffer internal state */
116116
struct k_mutex f_mtx;
117117
/**< Locking for accessing the FCB data, internal state */
118118

119-
struct flash_sector *f_oldest;
119+
const struct flash_sector *f_oldest;
120120
/**< Pointer to flash sector containing the oldest data,
121121
* internal state
122122
*/
@@ -223,7 +223,7 @@ typedef int (*fcb_walk_cb)(struct fcb_entry_ctx *loc_ctx, void *arg);
223223
* @return 0 on success, negative on failure (or transferred form callback
224224
* return-value), positive transferred form callback return-value
225225
*/
226-
int fcb_walk(struct fcb *fcbp, struct flash_sector *sector, fcb_walk_cb cb, void *cb_arg);
226+
int fcb_walk(struct fcb *fcbp, const struct flash_sector *sector, fcb_walk_cb cb, void *cb_arg);
227227

228228
/**
229229
* Get next fcb entry location.

subsys/fs/fcb/fcb.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,13 @@ int fcb_erase_sector(const struct fcb *fcbp, const struct flash_sector *sector)
9191

9292
int fcb_init(int f_area_id, struct fcb *fcbp)
9393
{
94-
struct flash_sector *sector;
94+
const struct flash_sector *sector;
9595
int rc;
9696
int i;
9797
uint8_t align;
9898
int oldest = -1, newest = -1;
99-
struct flash_sector *oldest_sector = NULL, *newest_sector = NULL;
99+
const struct flash_sector *oldest_sector = NULL;
100+
const struct flash_sector *newest_sector = NULL;
100101
struct fcb_disk_area fda;
101102
const struct flash_parameters *fparam;
102103

@@ -174,7 +175,7 @@ int fcb_init(int f_area_id, struct fcb *fcbp)
174175
int fcb_free_sector_cnt(struct fcb *fcbp)
175176
{
176177
int i;
177-
struct flash_sector *fa;
178+
const struct flash_sector *fa;
178179

179180
fa = fcbp->f_active.fe_sector;
180181
for (i = 0; i < fcbp->f_sector_cnt; i++) {
@@ -252,7 +253,7 @@ int fcb_get_len(const struct fcb *fcbp, uint8_t *buf, uint16_t *len)
252253
/**
253254
* Initialize erased sector for use.
254255
*/
255-
int fcb_sector_hdr_init(struct fcb *fcbp, struct flash_sector *sector, uint16_t id)
256+
int fcb_sector_hdr_init(struct fcb *fcbp, const struct flash_sector *sector, uint16_t id)
256257
{
257258
struct fcb_disk_area fda;
258259
int rc;
@@ -275,7 +276,8 @@ int fcb_sector_hdr_init(struct fcb *fcbp, struct flash_sector *sector, uint16_t
275276
* Returns 0 if sector is unused;
276277
* Returns 1 if sector has data.
277278
*/
278-
int fcb_sector_hdr_read(struct fcb *fcbp, struct flash_sector *sector, struct fcb_disk_area *fdap)
279+
int fcb_sector_hdr_read(struct fcb *fcbp, const struct flash_sector *sector,
280+
struct fcb_disk_area *fdap)
279281
{
280282
struct fcb_disk_area fda;
281283
int rc;

subsys/fs/fcb/fcb_append.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
#include <zephyr/fs/fcb.h>
1212
#include "fcb_priv.h"
1313

14-
static struct flash_sector *
15-
fcb_new_sector(struct fcb *fcb, int cnt)
14+
static const struct flash_sector *fcb_new_sector(struct fcb *fcb, int cnt)
1615
{
17-
struct flash_sector *prev;
18-
struct flash_sector *cur;
16+
const struct flash_sector *prev;
17+
const struct flash_sector *cur;
1918
int i;
2019

2120
prev = NULL;
@@ -39,7 +38,7 @@ fcb_new_sector(struct fcb *fcb, int cnt)
3938
int
4039
fcb_append_to_scratch(struct fcb *fcb)
4140
{
42-
struct flash_sector *sector;
41+
const struct flash_sector *sector;
4342
int rc;
4443

4544
sector = fcb_new_sector(fcb, 0);
@@ -59,7 +58,7 @@ fcb_append_to_scratch(struct fcb *fcb)
5958
int
6059
fcb_append(struct fcb *fcb, uint16_t len, struct fcb_entry *append_loc)
6160
{
62-
struct flash_sector *sector;
61+
const struct flash_sector *sector;
6362
struct fcb_entry *active;
6463
int cnt;
6564
int rc;

subsys/fs/fcb/fcb_getnext.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ fcb_getnext_in_sector(struct fcb *fcb, struct fcb_entry *loc)
3030
return rc;
3131
}
3232

33-
struct flash_sector *
34-
fcb_getnext_sector(struct fcb *fcb, struct flash_sector *sector)
33+
const struct flash_sector *fcb_getnext_sector(struct fcb *fcb, const struct flash_sector *sector)
3534
{
3635
sector++;
3736
if (sector >= &fcb->f_sectors[fcb->f_sector_cnt]) {

subsys/fs/fcb/fcb_priv.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,15 @@ uint8_t fcb_get_align(const struct fcb *fcbp);
6969
int fcb_erase_sector(const struct fcb *fcbp, const struct flash_sector *sector);
7070

7171
int fcb_getnext_in_sector(struct fcb *fcbp, struct fcb_entry *loc);
72-
struct flash_sector *fcb_getnext_sector(struct fcb *fcbp, struct flash_sector *sector);
72+
const struct flash_sector *fcb_getnext_sector(struct fcb *fcbp, const struct flash_sector *sector);
7373
int fcb_getnext_nolock(struct fcb *fcbp, struct fcb_entry *loc);
7474

7575
int fcb_elem_info(struct fcb *fcbp, struct fcb_entry *loc);
7676
int fcb_elem_endmarker(struct fcb *fcbp, struct fcb_entry *loc, uint8_t *crc8p);
7777

78-
int fcb_sector_hdr_init(struct fcb *fcbp, struct flash_sector *sector, uint16_t id);
79-
int fcb_sector_hdr_read(struct fcb *fcbp, struct flash_sector *sector, struct fcb_disk_area *fdap);
78+
int fcb_sector_hdr_init(struct fcb *fcbp, const struct flash_sector *sector, uint16_t id);
79+
int fcb_sector_hdr_read(struct fcb *fcbp, const struct flash_sector *sector,
80+
struct fcb_disk_area *fdap);
8081

8182
#ifdef __cplusplus
8283
}

subsys/fs/fcb/fcb_rotate.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
int
1212
fcb_rotate(struct fcb *fcb)
1313
{
14-
struct flash_sector *sector;
14+
const struct flash_sector *sector;
1515
int rc = 0;
1616

1717
rc = k_mutex_lock(&fcb->f_mtx, K_FOREVER);

subsys/fs/fcb/fcb_walk.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
* Call 'cb' for every element in flash circular buffer. If sector is specified,
1313
* only elements with that flash_sector are reported.
1414
*/
15-
int
16-
fcb_walk(struct fcb *fcb, struct flash_sector *sector, fcb_walk_cb cb,
17-
void *cb_arg)
15+
int fcb_walk(struct fcb *fcb, const struct flash_sector *sector, fcb_walk_cb cb, void *cb_arg)
1816
{
1917
struct fcb_entry_ctx entry_ctx;
2018
int rc;

tests/subsys/fs/fcb/src/fcb_test.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ extern struct fcb test_fcb;
2828
extern struct fcb test_fcb_crc_disabled;
2929
#endif
3030

31-
extern struct flash_sector test_fcb_sector[];
31+
extern const struct flash_sector test_fcb_sector[];
3232

3333
extern uint8_t fcb_test_erase_value;
3434

tests/subsys/fs/fcb/src/main.c

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,11 @@ uint8_t fcb_test_erase_value;
3030
* area. This test suite is the non bootable application so 1. image slot is
3131
* suitable for it.
3232
*/
33-
struct flash_sector test_fcb_sector[] = {
34-
[0] = {
35-
.fs_off = 0,
36-
.fs_size = SECTOR_SIZE
37-
},
38-
[1] = {
39-
.fs_off = SECTOR_SIZE,
40-
.fs_size = SECTOR_SIZE
41-
},
42-
[2] = {
43-
.fs_off = 2 * SECTOR_SIZE,
44-
.fs_size = SECTOR_SIZE
45-
},
46-
[3] = {
47-
.fs_off = 3 * SECTOR_SIZE,
48-
.fs_size = SECTOR_SIZE
49-
}
50-
};
51-
33+
const struct flash_sector test_fcb_sector[] = {
34+
[0] = {.fs_off = 0, .fs_size = SECTOR_SIZE},
35+
[1] = {.fs_off = SECTOR_SIZE, .fs_size = SECTOR_SIZE},
36+
[2] = {.fs_off = 2 * SECTOR_SIZE, .fs_size = SECTOR_SIZE},
37+
[3] = {.fs_off = 3 * SECTOR_SIZE, .fs_size = SECTOR_SIZE}};
5238

5339
void test_fcb_wipe(void)
5440
{

tests/subsys/settings/fcb/src/settings_test.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ extern int test_export_block;
3535

3636
extern int c2_var_count;
3737

38-
extern struct flash_sector fcb_sectors[SETTINGS_TEST_FCB_FLASH_CNT];
38+
extern const struct flash_sector fcb_sectors[SETTINGS_TEST_FCB_FLASH_CNT];
3939

4040
extern char val_string[SETTINGS_TEST_FCB_VAL_STR_CNT][SETTINGS_MAX_VAL_LEN];
4141
extern char test_ref_value[SETTINGS_TEST_FCB_VAL_STR_CNT][SETTINGS_MAX_VAL_LEN];
@@ -46,7 +46,7 @@ void ctest_clear_call_state(void);
4646
int ctest_get_call_state(void);
4747

4848
void config_wipe_srcs(void);
49-
void config_wipe_fcb(struct flash_sector *fs, int cnt);
49+
void config_wipe_fcb(const struct flash_sector *fs, int cnt);
5050

5151
void test_config_fill_area(
5252
char test_value[SETTINGS_TEST_FCB_VAL_STR_CNT][SETTINGS_MAX_VAL_LEN],

0 commit comments

Comments
 (0)