Skip to content

Commit f912cd0

Browse files
committed
Extract helper for configuration index calculation
Refactors the computation of configuration register index and bit offset into a reusable helper to reduce code duplication and improve maintainability. Updates existing code to use the new helper.
1 parent 4a5c450 commit f912cd0

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

arch/riscv/pmp.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ static const mempool_t kernel_mempools[] = {
113113
/* Global PMP configuration (shadow of hardware state) */
114114
static pmp_config_t pmp_global_config;
115115

116+
/* Helper to compute pmpcfg register index and bit offset for a given region */
117+
static inline void pmp_get_cfg_indices(uint8_t region_idx, uint8_t *cfg_idx,
118+
uint8_t *cfg_offset)
119+
{
120+
*cfg_idx = region_idx / 4;
121+
*cfg_offset = (region_idx % 4) * 8;
122+
}
123+
116124
pmp_config_t *pmp_get_config(void)
117125
{
118126
return &pmp_global_config;
@@ -205,8 +213,8 @@ int32_t pmp_set_region(pmp_config_t *config, const pmp_region_t *region)
205213
return ERR_PMP_LOCKED;
206214

207215
uint8_t region_idx = region->region_id;
208-
uint8_t pmpcfg_idx = region_idx / 4;
209-
uint8_t pmpcfg_offset = (region_idx % 4) * 8;
216+
uint8_t pmpcfg_idx, pmpcfg_offset;
217+
pmp_get_cfg_indices(region_idx, &pmpcfg_idx, &pmpcfg_offset);
210218

211219
/* Build configuration byte with TOR mode and permissions */
212220
uint8_t pmpcfg_perm = region->permissions & (PMPCFG_R | PMPCFG_W | PMPCFG_X);

0 commit comments

Comments
 (0)