hw/pci-host/pam: Replace magic number by PAM_REGIONS_COUNT definition
PATCH 1/1 hw/pci-host/pam: Replace magic number by PAM_REGIONS_COUNT definition
While this change helps triskaidekaphobic developers, it is a good practice to avoid magic values and using constant definitions instead.
Introduce the PAM_REGIONS_COUNT and use it. No logical change.
--- include/hw/pci-host/i440fx.h | 2 +- include/hw/pci-host/pam.h | 2 ++ include/hw/pci-host/q35.h | 2 +- hw/pci-host/pam.c | 2 +- hw/pci-host/q35.c | 2 +- 5 files changed, 6 insertions(+), 4 deletions(-)
include/hw/pci-host/i440fx.h +1 -1
@@ -29,7 +29,7 @@ struct PCII440FXState {MemoryRegion *system_memory;MemoryRegion *pci_address_space;MemoryRegion *ram_memory; -PAMMemoryRegion pam_regions[13 ]; +PAMMemoryRegion pam_regions[PAM_REGIONS_COUNT ];MemoryRegion smram_region;MemoryRegion smram, low_smram; };
include/hw/pci-host/pam.h +2 -0
@@ -80,6 +80,8 @@#define SMRAM_C_BASE_SEG_MASK ((uint8_t)0x7)#define SMRAM_C_BASE_SEG ((uint8_t)0x2)/* hardwired to b010 */ +#define PAM_REGIONS_COUNT 13 + typedef struct PAMMemoryRegion { MemoryRegion alias[4]; /* index = PAM value */ unsigned current;
include/hw/pci-host/q35.h +1 -1
@@ -44,7 +44,7 @@ struct MCHPCIState {MemoryRegion *pci_address_space;MemoryRegion *system_memory;MemoryRegion *address_space_io; -PAMMemoryRegion pam_regions[13 ]; +PAMMemoryRegion pam_regions[PAM_REGIONS_COUNT ];MemoryRegion smram_region, open_high_smram;MemoryRegion smram, low_smram, high_smram;MemoryRegion tseg_blackhole, tseg_window;
hw/pci-host/pam.c +1 -1
@@ -62,7 +62,7 @@ void init_pam(DeviceState *dev, MemoryRegion *ram_memory,void pam_update (PAMMemoryRegion *pam,int idx,uint8_t val) { -assert (0 <= idx && idx <=12 ); +assert (0 <= idx && idx <PAM_REGIONS_COUNT );memory_region_set_enabled (&pam->alias[pam->current], false); pam->current = (val >> ((!(idx &1 )) *4 )) &PAM_ATTR_MASK ;
hw/pci-host/q35.c +1 -1
@@ -641,7 +641,7 @@ static void mch_realize(PCIDevice *d, Error **errp)init_pam (DEVICE (mch), mch->ram_memory, mch->system_memory, mch->pci_address_space, &mch->pam_regions[0 ],PAM_BIOS_BASE ,PAM_BIOS_SIZE ); -for (i =0 ; i <12 ; ++i) { +for (i =0 ; i <ARRAY_SIZE (mch->pam_regions) -1 ; ++i) {init_pam (DEVICE (mch), mch->ram_memory, mch->system_memory, mch->pci_address_space, &mch->pam_regions[i+1 ],PAM_EXPAN_BASE + i *PAM_EXPAN_SIZE ,PAM_EXPAN_SIZE );
Signature · 1 line
2.26.2
Quoted 7 lines · Philippe Mathieu-Daudé
While this change helps triskaidekaphobic developers, it is a good practice to avoid magic values and using constant definitions instead.
Introduce the
PAM_REGIONS_COUNTand use it. No logical change.
Quoted 8 lines
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- include/hw/pci-host/i440fx.h | 2 +- include/hw/pci-host/pam.h | 2 ++ include/hw/pci-host/q35.h | 2 +- hw/pci-host/pam.c | 2 +- hw/pci-host/q35.c | 2 +- 5 files changed, 6 insertions(+), 4 deletions(-)
:D
Laurent Vivier
AppliedLe 02/12/2020 à 14:20, Philippe Mathieu-Daudé a écrit :
Quoted 73 lines
While this change helps triskaidekaphobic developers, it is a good practice to avoid magic values and using constant definitions instead.
Introduce the
PAM_REGIONS_COUNTand use it. No logical change.Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- include/hw/pci-host/i440fx.h | 2 +- include/hw/pci-host/pam.h | 2 ++ include/hw/pci-host/q35.h | 2 +- hw/pci-host/pam.c | 2 +- hw/pci-host/q35.c | 2 +- 5 files changed, 6 insertions(+), 4 deletions(-)include/hw/pci-host/i440fx.h +1
-1@@ -29,7 +29,7 @@ struct PCII440FXState {MemoryRegion *system_memory;MemoryRegion *pci_address_space;MemoryRegion *ram_memory; -PAMMemoryRegion pam_regions[13 ]; +PAMMemoryRegion pam_regions[PAM_REGIONS_COUNT ];MemoryRegion smram_region;MemoryRegion smram, low_smram; };include/hw/pci-host/pam.h +2
-0@@ -80,6 +80,8 @@#define SMRAM_C_BASE_SEG_MASK ((uint8_t)0x7)#define SMRAM_C_BASE_SEG ((uint8_t)0x2)/* hardwired to b010 */ +#define PAM_REGIONS_COUNT 13 + typedef struct PAMMemoryRegion { MemoryRegion alias[4]; /* index = PAM value */ unsigned current;include/hw/pci-host/q35.h +1
-1@@ -44,7 +44,7 @@ struct MCHPCIState {MemoryRegion *pci_address_space;MemoryRegion *system_memory;MemoryRegion *address_space_io; -PAMMemoryRegion pam_regions[13 ]; +PAMMemoryRegion pam_regions[PAM_REGIONS_COUNT ];MemoryRegion smram_region, open_high_smram;MemoryRegion smram, low_smram, high_smram;MemoryRegion tseg_blackhole, tseg_window;hw/pci-host/pam.c +1
-1@@ -62,7 +62,7 @@ void init_pam(DeviceState *dev, MemoryRegion *ram_memory,void pam_update (PAMMemoryRegion *pam,int idx,uint8_t val) { -assert (0 <= idx && idx <=12 ); +assert (0 <= idx && idx <PAM_REGIONS_COUNT );memory_region_set_enabled (&pam->alias[pam->current], false); pam->current = (val >> ((!(idx &1 )) *4 )) &PAM_ATTR_MASK ;hw/pci-host/q35.c +0
-0
hw/pci-host/q35.c static void mch_realize(PCIDevice *d, Error **errp) · line 647
Quoted 8 lines
hw/pci-host/q35.c +1
-1@@ -641,7 +641,7 @@ static void mch_realize(PCIDevice *d, Error **errp)init_pam (DEVICE (mch), mch->ram_memory, mch->system_memory, mch->pci_address_space, &mch->pam_regions[0 ],PAM_BIOS_BASE ,PAM_BIOS_SIZE ); -for (i =0 ; i <12 ; ++i) { +for (i =0 ; i <ARRAY_SIZE (mch->pam_regions) -1 ; ++i) {init_pam (DEVICE (mch), mch->ram_memory, mch->system_memory, mch->pci_address_space, &mch->pam_regions[i+1 ],PAM_EXPAN_BASE + i *PAM_EXPAN_SIZE ,PAM_EXPAN_SIZE );
Applied to my trivial-patches branch.
Thanks,
Laurent