[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-changelog] [xen-unstable] [BLKTAP] Simplify linked-list insertion/deletion.



# HG changeset patch
# User kfraser@xxxxxxxxxxxxxxxxxxxxx
# Node ID 4be49a3be040f0951a9be971e9b4cf0a399589ec
# Parent  0c7923eb6b9846c92f1c15486e06ee9745bcf676
[BLKTAP] Simplify linked-list insertion/deletion.
This should also avoid (bogus) compiler warnings, as reported
on IA64.
Signed-off-by: Keir Fraser <keir@xxxxxxxxxxxxx>
---
 tools/blktap/drivers/blktapctrl.c |  104 +++++++++++++-------------------------
 tools/blktap/drivers/tapdisk.c    |   58 ++++++---------------
 tools/blktap/drivers/tapdisk.h    |    8 +-
 3 files changed, 58 insertions(+), 112 deletions(-)

diff -r 0c7923eb6b98 -r 4be49a3be040 tools/blktap/drivers/blktapctrl.c
--- a/tools/blktap/drivers/blktapctrl.c Wed Oct 25 10:27:03 2006 +0100
+++ b/tools/blktap/drivers/blktapctrl.c Wed Oct 25 10:56:50 2006 +0100
@@ -204,81 +204,49 @@ static blkif_t *test_path(char *path, ch
 
 static void add_disktype(blkif_t *blkif, int type)
 {
-       driver_list_entry_t *entry, *ptr, *last;
-
-       if (type > MAX_DISK_TYPES) return;
+       driver_list_entry_t *entry, **pprev;
+
+       if (type > MAX_DISK_TYPES)
+               return;
 
        entry = malloc(sizeof(driver_list_entry_t));
        entry->blkif = blkif;
-       entry->next = NULL;
-       ptr = active_disks[type];
-
-       if (ptr == NULL) {
-               active_disks[type] = entry;
-               entry->prev = NULL;
-               return;
-       }
-
-       while (ptr != NULL) {
-               last = ptr;
-               ptr = ptr->next;
-       }
-
-       /*We've found the end of the list*/
-        last->next = entry;
-       entry->prev = last;
-       
-       return;
+       entry->next  = NULL;
+
+       pprev = &active_disks[type];
+       while (*pprev != NULL)
+               pprev = &(*pprev)->next;
+
+       *pprev = entry;
+       entry->pprev = pprev;
 }
 
 static int del_disktype(blkif_t *blkif)
 {
-       driver_list_entry_t *ptr, *cur, *last;
+       driver_list_entry_t *entry, **pprev;
        int type = blkif->drivertype, count = 0, close = 0;
 
-       if (type > MAX_DISK_TYPES) return 1;
-
-       ptr = active_disks[type];
-       last = NULL;
-       while (ptr != NULL) {
-               count++;
-               if (blkif == ptr->blkif) {
-                       cur = ptr;
-                       if (ptr->next != NULL) {
-                               /*There's more later in the chain*/
-                               if (!last) {
-                                       /*We're first in the list*/
-                                       active_disks[type] = ptr->next;
-                                       ptr = ptr->next;
-                                       ptr->prev = NULL;
-                               }
-                               else {
-                                       /*We're sandwiched*/
-                                       last->next = ptr->next;
-                                       ptr = ptr->next;
-                                       ptr->prev = last;
-                               }
-                               
-                       } else if (last) {
-                               /*There's more earlier in the chain*/
-                               last->next = NULL;
-                       } else {
-                               /*We're the only entry*/
-                               active_disks[type] = NULL;
-                               if(dtypes[type]->single_handler == 1) 
-                                       close = 1;
-                       }
-                       DPRINTF("DEL_DISKTYPE: Freeing entry\n");
-                       free(cur);
-                       if (dtypes[type]->single_handler == 0) close = 1;
-
-                       return close;
-               }
-               last = ptr;
-               ptr = ptr->next;
-       }
-       DPRINTF("DEL_DISKTYPE: No match\n");
-       return 1;
+       if (type > MAX_DISK_TYPES)
+               return 1;
+
+       pprev = &active_disks[type];
+       while ((*pprev != NULL) && ((*pprev)->blkif != blkif))
+               pprev = &(*pprev)->next;
+
+       if ((entry = *pprev) == NULL) {
+               DPRINTF("DEL_DISKTYPE: No match\n");
+               return 1;
+       }
+
+       *pprev = entry->next;
+       if (entry->next)
+               entry->next->pprev = pprev;
+
+       DPRINTF("DEL_DISKTYPE: Freeing entry\n");
+       free(entry);
+
+       /* Caller should close() if no single controller, or list is empty. */
+       return (!dtypes[type]->single_handler || (active_disks[type] == NULL));
 }
 
 static int write_msg(int fd, int msgtype, void *ptr, void *ptr2)
@@ -592,8 +560,8 @@ int unmap_blktapctrl(blkif_t *blkif)
        if (del_disktype(blkif)) {
                close(blkif->fds[WRITE]);
                close(blkif->fds[READ]);
-
-       }
+       }
+
        return 0;
 }
 
diff -r 0c7923eb6b98 -r 4be49a3be040 tools/blktap/drivers/tapdisk.c
--- a/tools/blktap/drivers/tapdisk.c    Wed Oct 25 10:27:03 2006 +0100
+++ b/tools/blktap/drivers/tapdisk.c    Wed Oct 25 10:56:50 2006 +0100
@@ -79,31 +79,17 @@ static void unmap_disk(struct td_state *
 {
        tapdev_info_t *info = s->ring_info;
        struct tap_disk *drv = s->drv;
-       fd_list_entry_t *ptr, *prev;
+       fd_list_entry_t *entry;
 
        drv->td_close(s);
 
        if (info != NULL && info->mem > 0)
                munmap(info->mem, getpagesize() * BLKTAP_MMAP_REGION_SIZE);
 
-       ptr = s->fd_entry;
-       prev = ptr->prev;
-
-       if (prev) {
-               /*There are entries earlier in the list*/
-               prev->next = ptr->next;
-               if (ptr->next) {
-                       ptr = ptr->next;
-                       ptr->prev = prev;
-               }
-       } else {
-               /*We are the first entry in list*/
-               if (ptr->next) {
-                       ptr = ptr->next;
-                       fd_start = ptr;
-                       ptr->prev = NULL;
-               } else fd_start = NULL;
-       }
+       entry = s->fd_entry;
+       *entry->pprev = entry->next;
+       if (entry->next)
+               entry->next->pprev = entry->pprev;
 
        close(info->fd);
 
@@ -144,35 +130,29 @@ static inline int LOCAL_FD_SET(fd_set *r
        return 0;
 }
 
-static inline fd_list_entry_t *add_fd_entry(int tap_fd, int io_fd[MAX_IOFD], 
struct td_state *s)
-{
-       fd_list_entry_t *ptr, *last, *entry;
+static inline fd_list_entry_t *add_fd_entry(
+       int tap_fd, int io_fd[MAX_IOFD], struct td_state *s)
+{
+       fd_list_entry_t **pprev, *entry;
        int i;
+
        DPRINTF("Adding fd_list_entry\n");
 
        /*Add to linked list*/
        s->fd_entry = entry = malloc(sizeof(fd_list_entry_t));
        entry->tap_fd = tap_fd;
-       for (i = 0; i < MAX_IOFD; i++) entry->io_fd[i] = io_fd[i];
+       for (i = 0; i < MAX_IOFD; i++)
+               entry->io_fd[i] = io_fd[i];
        entry->s = s;
        entry->next = NULL;
 
-       ptr = fd_start;
-       if (ptr == NULL) {
-               /*We are the first entry*/
-               fd_start = entry;
-               entry->prev = NULL;
-               goto finish;
-       }
-
-       while (ptr != NULL) {
-               last = ptr;
-               ptr = ptr->next;
-       }
-       last->next = entry;
-       entry->prev = last;
-
- finish:
+       pprev = &fd_start;
+       while (*pprev != NULL)
+               pprev = &(*pprev)->next;
+
+       *pprev = entry;
+       entry->pprev = pprev;
+
        return entry;
 }
 
diff -r 0c7923eb6b98 -r 4be49a3be040 tools/blktap/drivers/tapdisk.h
--- a/tools/blktap/drivers/tapdisk.h    Wed Oct 25 10:27:03 2006 +0100
+++ b/tools/blktap/drivers/tapdisk.h    Wed Oct 25 10:56:50 2006 +0100
@@ -191,9 +191,8 @@ static disk_info_t *dtypes[] = {
 };
 
 typedef struct driver_list_entry {
-       void *blkif;
-       void *prev;
-       void *next;
+       struct blkif *blkif;
+       struct driver_list_entry **pprev, *next;
 } driver_list_entry_t;
 
 typedef struct fd_list_entry {
@@ -201,8 +200,7 @@ typedef struct fd_list_entry {
        int  tap_fd;
        int  io_fd[MAX_IOFD];
        struct td_state *s;
-       void *prev;
-       void *next;
+       struct fd_list_entry **pprev, *next;
 } fd_list_entry_t;
 
 int qcow_create(const char *filename, uint64_t total_size,

_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-changelog


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.