[libvirt] PATCH: 23/28: Replace client linked list with array

Daniel P. Berrange berrange at redhat.com
Mon Dec 1 00:14:04 UTC 2008


This replaces the linked list of 'struct qemud_client' instances with an
array instead, allowing easy fine grained per-client locking

 qemud.c |   43 ++++++++++++++++++++++++-------------------
 qemud.h |    4 +---
 2 files changed, 25 insertions(+), 22 deletions(-)


Daniel

diff --git a/qemud/qemud.c b/qemud/qemud.c
--- a/qemud/qemud.c
+++ b/qemud/qemud.c
@@ -1151,6 +1151,12 @@ static int qemudDispatchServer(struct qe
         return -1;
     }
 
+    if (VIR_REALLOC_N(server->clients, server->nclients+1) < 0) {
+        qemudLog(QEMUD_ERR, "%s", _("Out of memory allocating clients"));
+        close(fd);
+        return -1;
+    }
+
     /* Disable Nagle.  Unix sockets will ignore this. */
     setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, (void *)&no_slow_start,
                 sizeof no_slow_start);
@@ -1231,9 +1237,7 @@ static int qemudDispatchServer(struct qe
         }
     }
 
-    client->next = server->clients;
-    server->clients = client;
-    server->nclients++;
+    server->clients[server->nclients++] = client;
 
     return 0;
 
@@ -1248,19 +1252,19 @@ static int qemudDispatchServer(struct qe
 
 
 static void qemudDispatchClientFailure(struct qemud_server *server, struct qemud_client *client) {
-    struct qemud_client *tmp = server->clients;
-    struct qemud_client *prev = NULL;
-    while (tmp) {
-        if (tmp == client) {
-            if (prev == NULL)
-                server->clients = client->next;
-            else
-                prev->next = client->next;
-            server->nclients--;
+    int i, n = -1;
+    for (i = 0 ; i < server->nclients ; i++) {
+        if (server->clients[i] == client) {
+            n = i;
             break;
         }
-        prev = tmp;
-        tmp = tmp->next;
+    }
+    if (n != -1) {
+        if (n < (server->nclients-1))
+            memmove(server->clients + n,
+                    server->clients + n + 1,
+                    server->nclients - (n + 1));
+        server->nclients--;
     }
 
     virEventRemoveHandleImpl(client->watch);
@@ -1629,13 +1633,14 @@ static void
 static void
 qemudDispatchClientEvent(int watch, int fd, int events, void *opaque) {
     struct qemud_server *server = (struct qemud_server *)opaque;
-    struct qemud_client *client = server->clients;
+    struct qemud_client *client = NULL;
+    int i;
 
-    while (client) {
-        if (client->watch == watch)
+    for (i = 0 ; i < server->nclients ; i++) {
+        if (server->clients[i]->watch == watch) {
+            client = server->clients[i];
             break;
-
-        client = client->next;
+        }
     }
 
     if (!client)
diff --git a/qemud/qemud.h b/qemud/qemud.h
--- a/qemud/qemud.h
+++ b/qemud/qemud.h
@@ -133,8 +133,6 @@ struct qemud_client {
 
     /* back-pointer to our server */
     struct qemud_server *server;
-
-    struct qemud_client *next;
 };
 
 #define QEMUD_CLIENT_MAGIC 0x7788aaee
@@ -155,7 +153,7 @@ struct qemud_server {
     int nsockets;
     struct qemud_socket *sockets;
     int nclients;
-    struct qemud_client *clients;
+    struct qemud_client **clients;
     int sigread;
     char logDir[PATH_MAX];
     unsigned int shutdown : 1;

-- 
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|




More information about the libvir-list mailing list