[Spice-devel] [PATCH 5/5] vdservice/vdi_port refactor: cosmetic changes

Alon Levy alevy at redhat.com
Sun Jan 2 08:03:00 PST 2011


 * move comment about ring to the proper file, vdi_port.h
 * introduce enums for EVENT indices (0,1 for Virtio, 0 for PCI)
 * move fill_events to cpp (leave the single lines inline).
---
 vdservice/pci_vdi_port.cpp    |    4 ++
 vdservice/pci_vdi_port.h      |  113 +++++++++++++++++++++-------------------
 vdservice/vdi_port.cpp        |   28 +++++-----
 vdservice/vdi_port.h          |    8 +++
 vdservice/virtio_vdi_port.cpp |   18 +++++++
 vdservice/virtio_vdi_port.h   |   31 ++++--------
 6 files changed, 113 insertions(+), 89 deletions(-)

diff --git a/vdservice/pci_vdi_port.cpp b/vdservice/pci_vdi_port.cpp
index 3baefd9..b055c5a 100644
--- a/vdservice/pci_vdi_port.cpp
+++ b/vdservice/pci_vdi_port.cpp
@@ -52,6 +52,10 @@ PCIVDIPort::~PCIVDIPort()
     }
 }
 
+void PCIVDIPort::fill_events(HANDLE *handle) {
+    handle[PCI_VDI_PORT_EVENT] = _event;
+}
+
 bool PCIVDIPort::init()
 {
     DWORD io_ret_len;
diff --git a/vdservice/pci_vdi_port.h b/vdservice/pci_vdi_port.h
index 40a0589..d9b6d67 100644
--- a/vdservice/pci_vdi_port.h
+++ b/vdservice/pci_vdi_port.h
@@ -1,54 +1,59 @@
-/*
-   Copyright (C) 2009 Red Hat, Inc.
-
-   This program is free software; you can redistribute it and/or
-   modify it under the terms of the GNU General Public License as
-   published by the Free Software Foundation; either version 2 of
-   the License, or (at your option) any later version.
-
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU General Public License for more details.
-
-   You should have received a copy of the GNU General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.
-*/
-
-#ifndef _H_PCI_VDI_PORT
-#define _H_PCI_VDI_PORT
-
-#include "vdi_port.h"
-
-#define BUF_READ    (1 << 0)
-#define BUF_WRITE   (1 << 1)
-#define BUF_ALL     (BUF_READ | BUF_WRITE)
-
-class PCIVDIPort : public VDIPort {
-public:
-    PCIVDIPort();
-    ~PCIVDIPort();
-    virtual bool init();
-    virtual const char *name() { return "PCIVDIPort"; }
-    virtual int write();
-    virtual int read();
-    virtual unsigned get_num_events() { return 1; }
-    virtual void fill_events(HANDLE *handle) {
-        handle[0] = _event;
-    }
-    virtual void handle_event(int event);
-
-private:
-    HANDLE _handle;
-    HANDLE _event;
-};
-
-// Ring notes:
-// _end is one after the end of data
-// _start==_end means empty ring
-// _start-1==_end (modulo) means full ring
-// _start-1 is never used
-// ring_write & read on right side of the ring (update _end)
-// ring_read & write from left (update _start)
-
-#endif
+/*
+   Copyright (C) 2009 Red Hat, Inc.
+
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2 of
+   the License, or (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef _H_PCI_VDI_PORT
+#define _H_PCI_VDI_PORT
+
+#include "vdi_port.h"
+
+#define BUF_READ    (1 << 0)
+#define BUF_WRITE   (1 << 1)
+#define BUF_ALL     (BUF_READ | BUF_WRITE)
+
+enum {
+    PCI_VDI_PORT_EVENT = 0,
+    PCI_VDI_PORT_EVENT_COUNT
+};
+
+class PCIVDIPort : public VDIPort {
+public:
+    PCIVDIPort();
+    ~PCIVDIPort();
+    virtual bool init();
+    virtual const char *name() {
+        return "PCIVDIPort";
+    }
+    virtual int write();
+    virtual int read();
+    virtual unsigned get_num_events() { return PCI_VDI_PORT_EVENT_COUNT; }
+    virtual void fill_events(HANDLE* handle);
+    virtual void handle_event(int event);
+
+private:
+    HANDLE _handle;
+    HANDLE _event;
+};
+
+// Ring notes:
+// _end is one after the end of data
+// _start==_end means empty ring
+// _start-1==_end (modulo) means full ring
+// _start-1 is never used
+// ring_write & read on right side of the ring (update _end)
+// ring_read & write from left (update _start)
+
+#endif
diff --git a/vdservice/vdi_port.cpp b/vdservice/vdi_port.cpp
index 9638dc0..60bd0ef 100644
--- a/vdservice/vdi_port.cpp
+++ b/vdservice/vdi_port.cpp
@@ -74,17 +74,17 @@ size_t VDIPort::ring_read(void* buf, size_t size)
     _read.start = _read.ring + (_read.start - _read.ring + n + m) % BUF_SIZE;
     return n + m;
 }
-
-int VDIPort::handle_error()
-{
-    switch (GetLastError()) {
-    case ERROR_CONNECTION_INVALID:
-        vd_printf("port reset");
-        _write.start = _write.end = _write.ring;
-        _read.start = _read.end = _read.ring;
-        return VDI_PORT_RESET;
-    default:
-        vd_printf("port io failed: %u", GetLastError());
-        return VDI_PORT_ERROR;
-    }
-}
+
+int VDIPort::handle_error()
+{
+    switch (GetLastError()) {
+    case ERROR_CONNECTION_INVALID:
+        vd_printf("port reset");
+        _write.start = _write.end = _write.ring;
+        _read.start = _read.end = _read.ring;
+        return VDI_PORT_RESET;
+    default:
+        vd_printf("port io failed: %u", GetLastError());
+        return VDI_PORT_ERROR;
+    }
+}
diff --git a/vdservice/vdi_port.h b/vdservice/vdi_port.h
index 63b04bb..b5c6f46 100644
--- a/vdservice/vdi_port.h
+++ b/vdservice/vdi_port.h
@@ -28,6 +28,14 @@
 #define VDI_PORT_BLOCKED    0
 #define VDI_PORT_RESET      -1
 #define VDI_PORT_ERROR      -2
+
+// Ring notes:
+// _end is one after the end of data
+// _start==_end means empty ring
+// _start-1==_end (modulo) means full ring
+// _start-1 is never used
+// ring_write & read on right side of the ring (update _end)
+// ring_read & write from left (update _start)
 
 typedef struct VDIPortBuffer {
     OVERLAPPED overlap;
diff --git a/vdservice/virtio_vdi_port.cpp b/vdservice/virtio_vdi_port.cpp
index 4f24a26..bbdd59e 100644
--- a/vdservice/virtio_vdi_port.cpp
+++ b/vdservice/virtio_vdi_port.cpp
@@ -46,6 +46,24 @@ VirtioVDIPort::~VirtioVDIPort()
     }
 }
 
+void VirtioVDIPort::fill_events(HANDLE *handle) {
+    handle[VIRTIO_VDI_PORT_EVENT_WRITE] = _write.overlap.hEvent;
+    handle[VIRTIO_VDI_PORT_EVENT_READ] = _read.overlap.hEvent;
+}
+
+void VirtioVDIPort::handle_event(int event) {
+    switch (event) {
+        case VIRTIO_VDI_PORT_EVENT_WRITE:
+            write_completion();
+            break;
+        case VIRTIO_VDI_PORT_EVENT_READ:
+            read_completion();
+            break;
+        default:
+            vd_printf("ERROR: unexpected event %d", event);
+    }
+}
+
 bool VirtioVDIPort::init()
 {
     _handle = CreateFile(VIOSERIAL_PORT_PATH, GENERIC_READ | GENERIC_WRITE , 0, NULL,
diff --git a/vdservice/virtio_vdi_port.h b/vdservice/virtio_vdi_port.h
index 6738804..04d3412 100644
--- a/vdservice/virtio_vdi_port.h
+++ b/vdservice/virtio_vdi_port.h
@@ -3,23 +3,21 @@
 
 #include "vdi_port.h"
 
+enum {
+    VIRTIO_VDI_PORT_EVENT_WRITE=0,
+    VIRTIO_VDI_PORT_EVENT_READ,
+    VIRTIO_VDI_PORT_EVENT_COUNT
+};
+
 class VirtioVDIPort : public VDIPort {
 public:
     VirtioVDIPort();
     ~VirtioVDIPort();
-    virtual const char *name() { return "VirtioVDIPort"; }
+    virtual const char *name() { return "VirtioVDIPort"; }
     virtual bool init();
-    virtual unsigned get_num_events() { return 2; }
-    virtual void fill_events(HANDLE *handle) {
-        handle[0] = _write.overlap.hEvent;
-        handle[1] = _read.overlap.hEvent;
-    }
-    virtual void handle_event(int event) {
-        switch (event) {
-            case 0: write_completion(); break;
-            case 1: read_completion(); break;
-        }
-    }
+    virtual unsigned get_num_events() { return VIRTIO_VDI_PORT_EVENT_COUNT; }
+    virtual void fill_events(HANDLE *handle);
+    virtual void handle_event(int event);
     virtual int write();
     virtual int read();
 
@@ -32,13 +30,4 @@ private:
     HANDLE _handle;
 };
 
-// Ring notes:
-// _end is one after the end of data
-// _start==_end means empty ring
-// _start-1==_end (modulo) means full ring
-// _start-1 is never used
-// ring_write & read on right side of the ring (update _end)
-// ring_read & write from left (update _start)
-
-
 #endif //_H_VIRTIO_VDI_PORT
\ No newline at end of file
-- 
1.7.3.4



More information about the Spice-devel mailing list