qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] ioport: Fix duplicated code
@ 2010-11-10 14:21 Luiz Capitulino
  2010-11-10 21:47 ` Stefan Weil
  0 siblings, 1 reply; 2+ messages in thread
From: Luiz Capitulino @ 2010-11-10 14:21 UTC (permalink / raw)
  To: qemu-devel; +Cc: Markus Armbruster, gleb

Functions register_ioport_read() and register_ioport_write() are almost
identical, the only difference is that they write to different arrays.

Introduce register_ioport() to handle this difference and change both
functions to use it instead of duplicating code.

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---
 ioport.c |   32 +++++++++++++++-----------------
 1 files changed, 15 insertions(+), 17 deletions(-)

diff --git a/ioport.c b/ioport.c
index ec3dc65..ae1bf8c 100644
--- a/ioport.c
+++ b/ioport.c
@@ -137,8 +137,9 @@ static int ioport_bsize(int size, int *bsize)
 }
 
 /* size is the word size in byte */
-int register_ioport_read(pio_addr_t start, int length, int size,
-                         IOPortReadFunc *func, void *opaque)
+static int register_ioport_rw(pio_addr_t start, int length, int size,
+                              IOPortReadFunc *read_func,
+                              IOPortWriteFunc *write_func, void *opaque)
 {
     int i, bsize;
 
@@ -147,7 +148,11 @@ int register_ioport_read(pio_addr_t start, int length, int size,
         return -1;
     }
     for(i = start; i < start + length; i += size) {
-        ioport_read_table[bsize][i] = func;
+        if (read_func) {
+            ioport_read_table[bsize][i] = read_func;
+        } else if (write_func) {
+            ioport_write_table[bsize][i] = write_func;
+        }
         if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
             hw_error("register_ioport_read: invalid opaque");
         ioport_opaque[i] = opaque;
@@ -155,23 +160,16 @@ int register_ioport_read(pio_addr_t start, int length, int size,
     return 0;
 }
 
-/* size is the word size in byte */
+int register_ioport_read(pio_addr_t start, int length, int size,
+                         IOPortReadFunc *func, void *opaque)
+{
+    return register_ioport_rw(start, length, size, func, NULL, opaque);
+}
+
 int register_ioport_write(pio_addr_t start, int length, int size,
                           IOPortWriteFunc *func, void *opaque)
 {
-    int i, bsize;
-
-    if (ioport_bsize(size, &bsize)) {
-        hw_error("register_ioport_write: invalid size");
-        return -1;
-    }
-    for(i = start; i < start + length; i += size) {
-        ioport_write_table[bsize][i] = func;
-        if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
-            hw_error("register_ioport_write: invalid opaque");
-        ioport_opaque[i] = opaque;
-    }
-    return 0;
+    return register_ioport_rw(start, length, size, NULL, func, opaque);
 }
 
 void isa_unassign_ioport(pio_addr_t start, int length)
-- 
1.7.3.2.161.g3089c

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [Qemu-devel] [PATCH] ioport: Fix duplicated code
  2010-11-10 14:21 [Qemu-devel] [PATCH] ioport: Fix duplicated code Luiz Capitulino
@ 2010-11-10 21:47 ` Stefan Weil
  0 siblings, 0 replies; 2+ messages in thread
From: Stefan Weil @ 2010-11-10 21:47 UTC (permalink / raw)
  To: Luiz Capitulino; +Cc: qemu-devel, gleb, Markus Armbruster

Am 10.11.2010 15:21, schrieb Luiz Capitulino:
> Functions register_ioport_read() and register_ioport_write() are almost
> identical, the only difference is that they write to different arrays.
>
> Introduce register_ioport() to handle this difference and change both
> functions to use it instead of duplicating code.
>
> Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
> ---
> ioport.c | 32 +++++++++++++++-----------------
> 1 files changed, 15 insertions(+), 17 deletions(-)
>
> diff --git a/ioport.c b/ioport.c
> index ec3dc65..ae1bf8c 100644
> --- a/ioport.c
> +++ b/ioport.c
> @@ -137,8 +137,9 @@ static int ioport_bsize(int size, int *bsize)
> }
>
> /* size is the word size in byte */
> -int register_ioport_read(pio_addr_t start, int length, int size,
> - IOPortReadFunc *func, void *opaque)
> +static int register_ioport_rw(pio_addr_t start, int length, int size,
> + IOPortReadFunc *read_func,
> + IOPortWriteFunc *write_func, void *opaque)
> {
> int i, bsize;
>
> @@ -147,7 +148,11 @@ int register_ioport_read(pio_addr_t start, int 
> length, int size,
> return -1;
> }
> for(i = start; i < start + length; i += size) {
> - ioport_read_table[bsize][i] = func;
> + if (read_func) {
> + ioport_read_table[bsize][i] = read_func;
> + } else if (write_func) {
> + ioport_write_table[bsize][i] = write_func;
> + }
> if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
> hw_error("register_ioport_read: invalid opaque");

Please fix the error message, too (register_ioport_read -> 
register_ioport_rw).

> ioport_opaque[i] = opaque;
> @@ -155,23 +160,16 @@ int register_ioport_read(pio_addr_t start, int 
> length, int size,
> return 0;
> }
>
> -/* size is the word size in byte */
> +int register_ioport_read(pio_addr_t start, int length, int size,
> + IOPortReadFunc *func, void *opaque)
> +{
> + return register_ioport_rw(start, length, size, func, NULL, opaque);
> +}
> +
> int register_ioport_write(pio_addr_t start, int length, int size,
> IOPortWriteFunc *func, void *opaque)
> {
> - int i, bsize;
> -
> - if (ioport_bsize(size, &bsize)) {
> - hw_error("register_ioport_write: invalid size");
> - return -1;
> - }
> - for(i = start; i < start + length; i += size) {
> - ioport_write_table[bsize][i] = func;
> - if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
> - hw_error("register_ioport_write: invalid opaque");
> - ioport_opaque[i] = opaque;
> - }
> - return 0;
> + return register_ioport_rw(start, length, size, NULL, func, opaque);
> }
>
> void isa_unassign_ioport(pio_addr_t start, int length)

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2010-11-10 21:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-11-10 14:21 [Qemu-devel] [PATCH] ioport: Fix duplicated code Luiz Capitulino
2010-11-10 21:47 ` Stefan Weil

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).