subsys: fs: fcb: remove f_area_id from fcb structure

It is faster to operate directly on flash_area pointer instead
of fetch it all the time using the fcb flash area id.
Also as f_area_id was needed only for get appropriate flash_area
pointer, so it is better to pass it only while initialization
the fcb and not store it in fcb instance data at all.

Signed-off-by: Andrzej Puzdrowski <andrzej.puzdrowski@nordicsemi.no>
This commit is contained in:
Andrzej Puzdrowski 2018-02-06 19:01:37 +01:00 committed by Anas Nashif
parent 9d56247cc7
commit b3614c0b17
5 changed files with 19 additions and 50 deletions

View file

@ -53,7 +53,6 @@ struct fcb {
u8_t f_version; /* Current version number of the data */
u8_t f_sector_cnt; /* Number of elements in sector array */
u8_t f_scratch_cnt; /* How many sectors should be kept empty */
int f_area_id;
struct flash_sector *f_sectors; /* Array of sectors, */
/* must be contiguous */
@ -80,7 +79,7 @@ struct fcb {
#define FCB_ERR_CRC -6
#define FCB_ERR_MAGIC -7
int fcb_init(struct fcb *fcb);
int fcb_init(int f_area_id, struct fcb *fcb);
/*
* fcb_append() appends an entry to circular buffer. When writing the

View file

@ -12,54 +12,34 @@
#include "fcb_priv.h"
#include "string.h"
const struct flash_area *
fcb_open_flash(const struct fcb *fcb)
{
const struct flash_area *fa;
int rc;
rc = flash_area_open(fcb->f_area_id, &fa);
if (rc != 0) {
return NULL;
}
return fa;
}
u8_t
fcb_get_align(const struct fcb *fcb)
{
const struct flash_area *fa;
u8_t align;
fa = fcb_open_flash(fcb);
if (fa == NULL) {
if (fcb->fap == NULL) {
return 0;
}
align = flash_area_align(fa);
align = flash_area_align(fcb->fap);
flash_area_close(fa);
return align;
}
int fcb_flash_read(const struct fcb *fcb, const struct flash_sector *sector,
off_t off, void *dst, size_t len)
{
const struct flash_area *fa;
int rc;
if (off + len > sector->fs_size) {
return FCB_ERR_ARGS;
}
fa = fcb_open_flash(fcb);
if (fa == NULL) {
if (fcb->fap == NULL) {
return FCB_ERR_FLASH;
}
rc = flash_area_read(fa, sector->fs_off + off, dst, len);
flash_area_close(fa);
rc = flash_area_read(fcb->fap, sector->fs_off + off, dst, len);
if (rc != 0) {
return FCB_ERR_FLASH;
@ -71,20 +51,17 @@ int fcb_flash_read(const struct fcb *fcb, const struct flash_sector *sector,
int fcb_flash_write(const struct fcb *fcb, const struct flash_sector *sector,
off_t off, const void *src, size_t len)
{
const struct flash_area *fa;
int rc;
if (off + len > sector->fs_size) {
return FCB_ERR_ARGS;
}
fa = fcb_open_flash(fcb);
if (fa == NULL) {
if (fcb->fap == NULL) {
return FCB_ERR_FLASH;
}
rc = flash_area_write(fa, sector->fs_off + off, src, len);
flash_area_close(fa);
rc = flash_area_write(fcb->fap, sector->fs_off + off, src, len);
if (rc != 0) {
return FCB_ERR_FLASH;
@ -96,16 +73,13 @@ int fcb_flash_write(const struct fcb *fcb, const struct flash_sector *sector,
int
fcb_erase_sector(const struct fcb *fcb, const struct flash_sector *sector)
{
const struct flash_area *fa;
int rc;
fa = fcb_open_flash(fcb);
if (fa == NULL) {
if (fcb->fap == NULL) {
return FCB_ERR_FLASH;
}
rc = flash_area_erase(fa, sector->fs_off, sector->fs_size);
flash_area_close(fa);
rc = flash_area_erase(fcb->fap, sector->fs_off, sector->fs_size);
if (rc != 0) {
return FCB_ERR_FLASH;
@ -115,7 +89,7 @@ fcb_erase_sector(const struct fcb *fcb, const struct flash_sector *sector)
}
int
fcb_init(struct fcb *fcb)
fcb_init(int f_area_id, struct fcb *fcb)
{
struct flash_sector *sector;
int rc;
@ -129,8 +103,8 @@ fcb_init(struct fcb *fcb)
return FCB_ERR_ARGS;
}
fcb->fap = fcb_open_flash(fcb);
if (fcb->fap == NULL) {
rc = flash_area_open(f_area_id, &fcb->fap);
if (rc != 0) {
return FCB_ERR_ARGS;
}

View file

@ -14,22 +14,21 @@ void fcb_test_init(void)
fcb = &test_fcb;
memset(fcb, 0, sizeof(*fcb));
fcb->f_area_id = TEST_FCB_FLASH_AREA_ID;
rc = fcb_init(fcb);
rc = fcb_init(TEST_FCB_FLASH_AREA_ID, fcb);
zassert_true(rc == FCB_ERR_ARGS, "fcb_init call should fail");
fcb->f_sectors = test_fcb_sector;
rc = fcb_init(fcb);
rc = fcb_init(TEST_FCB_FLASH_AREA_ID, fcb);
zassert_true(rc == FCB_ERR_ARGS, "fcb_init call should fail");
fcb->f_sector_cnt = 2;
fcb->f_magic = 0x12345678;
rc = fcb_init(fcb);
rc = fcb_init(TEST_FCB_FLASH_AREA_ID, fcb);
zassert_true(rc == FCB_ERR_MAGIC, "fcb_init call should fail");
fcb->f_magic = 0;
rc = fcb_init(fcb);
rc = fcb_init(TEST_FCB_FLASH_AREA_ID, fcb);
zassert_true(rc == 0, "fcb_init call failure");
}

View file

@ -61,9 +61,8 @@ void fcb_test_reset(void)
memset(fcb, 0, sizeof(*fcb));
fcb->f_sector_cnt = 2;
fcb->f_sectors = test_fcb_sector;
fcb->f_area_id = TEST_FCB_FLASH_AREA_ID;
rc = fcb_init(fcb);
rc = fcb_init(TEST_FCB_FLASH_AREA_ID, fcb);
zassert_true(rc == 0, "fcb_init call failure");
var_cnt = 32;
@ -100,9 +99,8 @@ void fcb_test_reset(void)
memset(fcb, 0, sizeof(*fcb));
fcb->f_sector_cnt = 2;
fcb->f_sectors = test_fcb_sector;
fcb->f_area_id = TEST_FCB_FLASH_AREA_ID;
rc = fcb_init(fcb);
rc = fcb_init(TEST_FCB_FLASH_AREA_ID, fcb);
zassert_true(rc == 0, "fcb_init call failure");
/*

View file

@ -109,10 +109,9 @@ void fcb_tc_pretest(int sectors)
memset(fcb, 0, sizeof(*fcb));
fcb->f_sector_cnt = sectors;
fcb->f_sectors = test_fcb_sector; /* XXX */
fcb->f_area_id = TEST_FCB_FLASH_AREA_ID;
rc = 0;
rc = fcb_init(fcb);
rc = fcb_init(TEST_FCB_FLASH_AREA_ID, fcb);
if (rc != 0) {
printf("%s rc == %xm, %d\n", __func__, rc, rc);
zassert_true(rc == 0, "fbc initialization failure");