reduce code duplication
cover reduce code duplication
This is just a tiny nitpick I came across while hacking on other stuff. It reduces the code duplication, hopefully in a good way for other eyes too.
PATCH 1/2 pass parameter as char.
pass parameter to tb_invalidate_phys_page_fast() as a char, that we'll actually transform into an integer. It will make it easier for generator macros to deal with it.
Signed-off-by Glauber Costa <[email protected]>
--- exec.c | 26 ++++++++++++++++++++++---- 1 files changed, 22 insertions(+), 4 deletions(-)
exec.c +22 -4
@@ -997,11 +997,29 @@ void tb_invalidate_phys_page_range(target_phys_addr_t start, target_phys_addr_t#endif } +static inline int size2len (char size) +{ +switch (size) { +case 'b' : +return 1 ; +case 'w' : +return 2 ; +case 'l' : +return 4 ; + } + +/* should never happen */ +fprintf (stderr,"%s: invalid size %d\n" , __func__, size); +exit (1 ); +return -1 ; +} +/* len must be <= 8 and start must be a multiple of len */ -static inline void tb_invalidate_phys_page_fast (target_phys_addr_t start,int len) +static inline void tb_invalidate_phys_page_fast (target_phys_addr_t start,char size) {PageDesc *p;int offset, b; +int len =size2len (size);#if 0 if (1 ) {if (loglevel) {
@@ -2444,7 +2462,7 @@ static void notdirty_mem_writeb(void *opaque, target_phys_addr_t ram_addr, dirty_flags = phys_ram_dirty[ram_addr >>TARGET_PAGE_BITS ];if (!(dirty_flags &CODE_DIRTY_FLAG )) {#if !defined(CONFIG_USER_ONLY ) -tb_invalidate_phys_page_fast (ram_addr,1 ); +tb_invalidate_phys_page_fast (ram_addr,'b' ); dirty_flags = phys_ram_dirty[ram_addr >>TARGET_PAGE_BITS ];#endif }
@@ -2469,7 +2487,7 @@ static void notdirty_mem_writew(void *opaque, target_phys_addr_t ram_addr, dirty_flags = phys_ram_dirty[ram_addr >>TARGET_PAGE_BITS ];if (!(dirty_flags &CODE_DIRTY_FLAG )) {#if !defined(CONFIG_USER_ONLY ) -tb_invalidate_phys_page_fast (ram_addr,2 ); +tb_invalidate_phys_page_fast (ram_addr,'w' ); dirty_flags = phys_ram_dirty[ram_addr >>TARGET_PAGE_BITS ];#endif }
@@ -2494,7 +2512,7 @@ static void notdirty_mem_writel(void *opaque, target_phys_addr_t ram_addr, dirty_flags = phys_ram_dirty[ram_addr >>TARGET_PAGE_BITS ];if (!(dirty_flags &CODE_DIRTY_FLAG )) {#if !defined(CONFIG_USER_ONLY ) -tb_invalidate_phys_page_fast (ram_addr,4 ); +tb_invalidate_phys_page_fast (ram_addr,'l' ); dirty_flags = phys_ram_dirty[ram_addr >>TARGET_PAGE_BITS ];#endif }
Signature · 1 line
1.5.6.5
PATCH 2/2 reduce code duplication
Code for all versions of notdirty_mem_write are quite alike. Rewrite it as a generator macro.
Signed-off-by Glauber Costa <[email protected]>
--- exec.c | 66 ++++++++++++++++----------------------------------------------- 1 files changed, 17 insertions(+), 49 deletions(-)
exec.c +17 -49
@@ -2455,43 +2455,27 @@ static CPUWriteMemoryFunc *unassigned_mem_write[3] = { unassigned_mem_writel, }; -static void notdirty_mem_writeb (void *opaque,target_phys_addr_t ram_addr, -uint32_t val) +static inline void store_size (target_phys_addr_t addr,uint32_t val,char siz) { -int dirty_flags; - dirty_flags = phys_ram_dirty[ram_addr >>TARGET_PAGE_BITS ]; -if (!(dirty_flags &CODE_DIRTY_FLAG )) { -#if !defined(CONFIG_USER_ONLY ) -tb_invalidate_phys_page_fast (ram_addr,'b' ); - dirty_flags = phys_ram_dirty[ram_addr >>TARGET_PAGE_BITS ]; -#endif +switch (siz) { +case 'b' :stb_p (addr, val);break ; +case 'w' :stw_p (addr, val);break ; +case 'l' :stl_p (addr, val);break ; } -stb_p (phys_ram_base + ram_addr, val); -#ifdef USE_KQEMU -if (cpu_single_env->kqemu_enabled && - (dirty_flags &KQEMU_MODIFY_PAGE_MASK ) !=KQEMU_MODIFY_PAGE_MASK ) -kqemu_modify_page (cpu_single_env, ram_addr); -#endif - dirty_flags |= (0xff & ~CODE_DIRTY_FLAG ); - phys_ram_dirty[ram_addr >>TARGET_PAGE_BITS ] = dirty_flags; -/* we remove the notdirty callback only if the code has been -flushed */ -if (dirty_flags ==0xff ) -tlb_set_dirty (cpu_single_env, cpu_single_env->mem_io_vaddr); } -static void notdirty_mem_writew (void *opaque,target_phys_addr_t ram_addr, -uint32_t val) +static inline void notdirty_mem_write_size (void *opaque,target_phys_addr_t ram_addr, +uint32_t val,char siz) {int dirty_flags; dirty_flags = phys_ram_dirty[ram_addr >>TARGET_PAGE_BITS ];if (!(dirty_flags &CODE_DIRTY_FLAG )) {#if !defined(CONFIG_USER_ONLY ) -tb_invalidate_phys_page_fast (ram_addr,'w' ); +tb_invalidate_phys_page_fast (ram_addr, siz); dirty_flags = phys_ram_dirty[ram_addr >>TARGET_PAGE_BITS ];#endif } -stw_p (phys_ram_base + ram_addr, val); +store_size ((target_phys_addr_t )phys_ram_base + ram_addr, val, siz);#ifdef USE_KQEMU if (cpu_single_env->kqemu_enabled && (dirty_flags &KQEMU_MODIFY_PAGE_MASK ) !=KQEMU_MODIFY_PAGE_MASK )
@@ -2505,30 +2489,14 @@ static void notdirty_mem_writew(void *opaque, target_phys_addr_t ram_addr,tlb_set_dirty (cpu_single_env, cpu_single_env->mem_io_vaddr); } -static void notdirty_mem_writel (void *opaque,target_phys_addr_t ram_addr, -uint32_t val) -{ -int dirty_flags; - dirty_flags = phys_ram_dirty[ram_addr >>TARGET_PAGE_BITS ]; -if (!(dirty_flags &CODE_DIRTY_FLAG )) { -#if !defined(CONFIG_USER_ONLY ) -tb_invalidate_phys_page_fast (ram_addr,'l' ); - dirty_flags = phys_ram_dirty[ram_addr >>TARGET_PAGE_BITS ]; -#endif - } -stl_p (phys_ram_base + ram_addr, val); -#ifdef USE_KQEMU -if (cpu_single_env->kqemu_enabled && - (dirty_flags &KQEMU_MODIFY_PAGE_MASK ) !=KQEMU_MODIFY_PAGE_MASK ) -kqemu_modify_page (cpu_single_env, ram_addr); -#endif - dirty_flags |= (0xff & ~CODE_DIRTY_FLAG ); - phys_ram_dirty[ram_addr >>TARGET_PAGE_BITS ] = dirty_flags; -/* we remove the notdirty callback only if the code has been -flushed */ -if (dirty_flags ==0xff ) -tlb_set_dirty (cpu_single_env, cpu_single_env->mem_io_vaddr); -} +#define gen_notdirty (s) static void notdirty_mem_write##s(void *opaque, \ + target_phys_addr_t ram_addr, uint32_t val)\ +{ notdirty_mem_write_size(opaque, ram_addr, val, #s[0]); } + +gen_notdirty(b) +gen_notdirty(w) +gen_notdirty(l) +#undef gen_notdirtystatic CPUReadMemoryFunc *error_mem_read[3 ] = {NULL ,/* never used */
Signature · 1 line
1.5.6.5