Skip to content

Commit

Permalink
retention: Drop prefix/checksum code if not used
Browse files Browse the repository at this point in the history
Will exclude the code for prefix and checksum functionality if none
of the entries on a device make use of these features, saves ~816
bytes when both features are excluded.

Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
  • Loading branch information
nordicjm authored and carlescufi committed Aug 24, 2023
1 parent 71eee6d commit 497b6eb
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions subsys/retention/retention.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ LOG_MODULE_REGISTER(retention, CONFIG_RETENTION_LOG_LEVEL);

#define DATA_VALID_VALUE 1

#define INST_HAS_CHECKSUM(n) DT_INST_PROP(n, checksum) ||

#define INST_HAS_PREFIX(n) COND_CODE_1(DT_INST_NODE_HAS_PROP(n, prefix), (1), (0)) ||

#if (DT_INST_FOREACH_STATUS_OKAY(INST_HAS_CHECKSUM) 0)
#define ANY_HAS_CHECKSUM
#endif

#if (DT_INST_FOREACH_STATUS_OKAY(INST_HAS_PREFIX) 0)
#define ANY_HAS_PREFIX
#endif

enum {
CHECKSUM_NONE = 0,
CHECKSUM_CRC8,
Expand Down Expand Up @@ -68,6 +80,7 @@ static inline void retention_lock_release(const struct device *dev)
#endif
}

#ifdef ANY_HAS_CHECKSUM
static int retention_checksum(const struct device *dev, uint32_t *output)
{
const struct retention_config *config = dev->config;
Expand Down Expand Up @@ -108,6 +121,7 @@ static int retention_checksum(const struct device *dev, uint32_t *output)
finish:
return rc;
}
#endif

static int retention_init(const struct device *dev)
{
Expand Down Expand Up @@ -156,10 +170,7 @@ ssize_t retention_size(const struct device *dev)
int retention_is_valid(const struct device *dev)
{
const struct retention_config *config = dev->config;
struct retention_data *data = dev->data;
int rc = 0;
uint8_t buffer[CONFIG_RETENTION_BUFFER_SIZE];
off_t pos;

retention_lock_take(dev);

Expand All @@ -169,9 +180,12 @@ int retention_is_valid(const struct device *dev)
goto finish;
}

#ifdef ANY_HAS_PREFIX
if (config->prefix_len != 0) {
/* Check magic header is present at the start of the section */
pos = 0;
struct retention_data *data = dev->data;
uint8_t buffer[CONFIG_RETENTION_BUFFER_SIZE];
off_t pos = 0;

while (pos < config->prefix_len) {
uint8_t read_size = MIN((config->prefix_len - pos), sizeof(buffer));
Expand All @@ -198,7 +212,9 @@ int retention_is_valid(const struct device *dev)
/* Header already exists so no need to re-write it again */
data->header_written = true;
}
#endif

#ifdef ANY_HAS_CHECKSUM
if (config->checksum_size != 0) {
/* Check the checksum validity, for this all the data must be read out */
uint32_t checksum = 0;
Expand Down Expand Up @@ -237,6 +253,7 @@ int retention_is_valid(const struct device *dev)
goto finish;
}
}
#endif

/* At this point, checks have passed (if enabled), mark data as being valid */
rc = DATA_VALID_VALUE;
Expand Down Expand Up @@ -270,9 +287,12 @@ int retention_read(const struct device *dev, off_t offset, uint8_t *buffer, size
int retention_write(const struct device *dev, off_t offset, const uint8_t *buffer, size_t size)
{
const struct retention_config *config = dev->config;
struct retention_data *data = dev->data;
int rc;

#ifdef ANY_HAS_PREFIX
struct retention_data *data = dev->data;
#endif

retention_lock_take(dev);

if (offset < 0 || ((size_t)offset + size) > (config->size - config->reserved_size)) {
Expand All @@ -288,6 +308,7 @@ int retention_write(const struct device *dev, off_t offset, const uint8_t *buffe
goto finish;
}

#ifdef ANY_HAS_PREFIX
/* Write optional header and footer information, these are done last to ensure data
* validity before marking it as being valid
*/
Expand All @@ -301,7 +322,9 @@ int retention_write(const struct device *dev, off_t offset, const uint8_t *buffe

data->header_written = true;
}
#endif

#ifdef ANY_HAS_CHECKSUM
if (config->checksum_size != 0) {
/* Generating a checksum requires reading out all the data in the region */
uint32_t checksum = 0;
Expand Down Expand Up @@ -330,6 +353,7 @@ int retention_write(const struct device *dev, off_t offset, const uint8_t *buffe
(void *)&checksum, sizeof(checksum));
}
}
#endif

finish:
retention_lock_release(dev);
Expand Down

0 comments on commit 497b6eb

Please sign in to comment.