ia64/xen-unstable

changeset 11965:4be49a3be040

[BLKTAP] Simplify linked-list insertion/deletion.
This should also avoid (bogus) compiler warnings, as reported
on IA64.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kfraser@localhost.localdomain
date Wed Oct 25 10:56:50 2006 +0100 (2006-10-25)
parents 0c7923eb6b98
children ffbd9e4668a6
files tools/blktap/drivers/blktapctrl.c tools/blktap/drivers/tapdisk.c tools/blktap/drivers/tapdisk.h
line diff
     1.1 --- a/tools/blktap/drivers/blktapctrl.c	Wed Oct 25 10:27:03 2006 +0100
     1.2 +++ b/tools/blktap/drivers/blktapctrl.c	Wed Oct 25 10:56:50 2006 +0100
     1.3 @@ -204,81 +204,49 @@ static blkif_t *test_path(char *path, ch
     1.4  
     1.5  static void add_disktype(blkif_t *blkif, int type)
     1.6  {
     1.7 -	driver_list_entry_t *entry, *ptr, *last;
     1.8 +	driver_list_entry_t *entry, **pprev;
     1.9  
    1.10 -	if (type > MAX_DISK_TYPES) return;
    1.11 +	if (type > MAX_DISK_TYPES)
    1.12 +		return;
    1.13  
    1.14  	entry = malloc(sizeof(driver_list_entry_t));
    1.15  	entry->blkif = blkif;
    1.16 -	entry->next = NULL;
    1.17 -	ptr = active_disks[type];
    1.18 -
    1.19 -	if (ptr == NULL) {
    1.20 -		active_disks[type] = entry;
    1.21 -		entry->prev = NULL;
    1.22 -		return;
    1.23 -	}
    1.24 +	entry->next  = NULL;
    1.25  
    1.26 -	while (ptr != NULL) {
    1.27 -		last = ptr;
    1.28 -		ptr = ptr->next;
    1.29 -	}
    1.30 +	pprev = &active_disks[type];
    1.31 +	while (*pprev != NULL)
    1.32 +		pprev = &(*pprev)->next;
    1.33  
    1.34 -	/*We've found the end of the list*/
    1.35 -        last->next = entry;
    1.36 -	entry->prev = last;
    1.37 -	
    1.38 -	return;
    1.39 +	*pprev = entry;
    1.40 +	entry->pprev = pprev;
    1.41  }
    1.42  
    1.43  static int del_disktype(blkif_t *blkif)
    1.44  {
    1.45 -	driver_list_entry_t *ptr, *cur, *last;
    1.46 +	driver_list_entry_t *entry, **pprev;
    1.47  	int type = blkif->drivertype, count = 0, close = 0;
    1.48  
    1.49 -	if (type > MAX_DISK_TYPES) return 1;
    1.50 +	if (type > MAX_DISK_TYPES)
    1.51 +		return 1;
    1.52 +
    1.53 +	pprev = &active_disks[type];
    1.54 +	while ((*pprev != NULL) && ((*pprev)->blkif != blkif))
    1.55 +		pprev = &(*pprev)->next;
    1.56  
    1.57 -	ptr = active_disks[type];
    1.58 -	last = NULL;
    1.59 -	while (ptr != NULL) {
    1.60 -		count++;
    1.61 -		if (blkif == ptr->blkif) {
    1.62 -			cur = ptr;
    1.63 -			if (ptr->next != NULL) {
    1.64 -				/*There's more later in the chain*/
    1.65 -				if (!last) {
    1.66 -					/*We're first in the list*/
    1.67 -					active_disks[type] = ptr->next;
    1.68 -					ptr = ptr->next;
    1.69 -					ptr->prev = NULL;
    1.70 -				}
    1.71 -				else {
    1.72 -					/*We're sandwiched*/
    1.73 -					last->next = ptr->next;
    1.74 -					ptr = ptr->next;
    1.75 -					ptr->prev = last;
    1.76 -				}
    1.77 -				
    1.78 -			} else if (last) {
    1.79 -				/*There's more earlier in the chain*/
    1.80 -				last->next = NULL;
    1.81 -			} else {
    1.82 -				/*We're the only entry*/
    1.83 -				active_disks[type] = NULL;
    1.84 -				if(dtypes[type]->single_handler == 1) 
    1.85 -					close = 1;
    1.86 -			}
    1.87 -			DPRINTF("DEL_DISKTYPE: Freeing entry\n");
    1.88 -			free(cur);
    1.89 -			if (dtypes[type]->single_handler == 0) close = 1;
    1.90 +	if ((entry = *pprev) == NULL) {
    1.91 +		DPRINTF("DEL_DISKTYPE: No match\n");
    1.92 +		return 1;
    1.93 +	}
    1.94  
    1.95 -			return close;
    1.96 -		}
    1.97 -		last = ptr;
    1.98 -		ptr = ptr->next;
    1.99 -	}
   1.100 -	DPRINTF("DEL_DISKTYPE: No match\n");
   1.101 -	return 1;
   1.102 +	*pprev = entry->next;
   1.103 +	if (entry->next)
   1.104 +		entry->next->pprev = pprev;
   1.105 +
   1.106 +	DPRINTF("DEL_DISKTYPE: Freeing entry\n");
   1.107 +	free(entry);
   1.108 +
   1.109 +	/* Caller should close() if no single controller, or list is empty. */
   1.110 +	return (!dtypes[type]->single_handler || (active_disks[type] == NULL));
   1.111  }
   1.112  
   1.113  static int write_msg(int fd, int msgtype, void *ptr, void *ptr2)
   1.114 @@ -592,8 +560,8 @@ int unmap_blktapctrl(blkif_t *blkif)
   1.115  	if (del_disktype(blkif)) {
   1.116  		close(blkif->fds[WRITE]);
   1.117  		close(blkif->fds[READ]);
   1.118 +	}
   1.119  
   1.120 -	}
   1.121  	return 0;
   1.122  }
   1.123  
     2.1 --- a/tools/blktap/drivers/tapdisk.c	Wed Oct 25 10:27:03 2006 +0100
     2.2 +++ b/tools/blktap/drivers/tapdisk.c	Wed Oct 25 10:56:50 2006 +0100
     2.3 @@ -79,31 +79,17 @@ static void unmap_disk(struct td_state *
     2.4  {
     2.5  	tapdev_info_t *info = s->ring_info;
     2.6  	struct tap_disk *drv = s->drv;
     2.7 -	fd_list_entry_t *ptr, *prev;
     2.8 +	fd_list_entry_t *entry;
     2.9  
    2.10  	drv->td_close(s);
    2.11  
    2.12  	if (info != NULL && info->mem > 0)
    2.13  	        munmap(info->mem, getpagesize() * BLKTAP_MMAP_REGION_SIZE);
    2.14  
    2.15 -	ptr = s->fd_entry;
    2.16 -	prev = ptr->prev;
    2.17 -
    2.18 -	if (prev) {
    2.19 -		/*There are entries earlier in the list*/
    2.20 -		prev->next = ptr->next;
    2.21 -		if (ptr->next) {
    2.22 -			ptr = ptr->next;
    2.23 -			ptr->prev = prev;
    2.24 -		}
    2.25 -	} else {
    2.26 -		/*We are the first entry in list*/
    2.27 -		if (ptr->next) {
    2.28 -			ptr = ptr->next;
    2.29 -			fd_start = ptr;
    2.30 -			ptr->prev = NULL;
    2.31 -		} else fd_start = NULL;
    2.32 -	}
    2.33 +	entry = s->fd_entry;
    2.34 +	*entry->pprev = entry->next;
    2.35 +	if (entry->next)
    2.36 +		entry->next->pprev = entry->pprev;
    2.37  
    2.38  	close(info->fd);
    2.39  
    2.40 @@ -144,35 +130,29 @@ static inline int LOCAL_FD_SET(fd_set *r
    2.41  	return 0;
    2.42  }
    2.43  
    2.44 -static inline fd_list_entry_t *add_fd_entry(int tap_fd, int io_fd[MAX_IOFD], struct td_state *s)
    2.45 +static inline fd_list_entry_t *add_fd_entry(
    2.46 +	int tap_fd, int io_fd[MAX_IOFD], struct td_state *s)
    2.47  {
    2.48 -	fd_list_entry_t *ptr, *last, *entry;
    2.49 +	fd_list_entry_t **pprev, *entry;
    2.50  	int i;
    2.51 +
    2.52  	DPRINTF("Adding fd_list_entry\n");
    2.53  
    2.54  	/*Add to linked list*/
    2.55  	s->fd_entry = entry = malloc(sizeof(fd_list_entry_t));
    2.56  	entry->tap_fd = tap_fd;
    2.57 -	for (i = 0; i < MAX_IOFD; i++) entry->io_fd[i] = io_fd[i];
    2.58 +	for (i = 0; i < MAX_IOFD; i++)
    2.59 +		entry->io_fd[i] = io_fd[i];
    2.60  	entry->s = s;
    2.61  	entry->next = NULL;
    2.62  
    2.63 -	ptr = fd_start;
    2.64 -	if (ptr == NULL) {
    2.65 -		/*We are the first entry*/
    2.66 -		fd_start = entry;
    2.67 -		entry->prev = NULL;
    2.68 -		goto finish;
    2.69 -	}
    2.70 +	pprev = &fd_start;
    2.71 +	while (*pprev != NULL)
    2.72 +		pprev = &(*pprev)->next;
    2.73  
    2.74 -	while (ptr != NULL) {
    2.75 -		last = ptr;
    2.76 -		ptr = ptr->next;
    2.77 -	}
    2.78 -	last->next = entry;
    2.79 -	entry->prev = last;
    2.80 +	*pprev = entry;
    2.81 +	entry->pprev = pprev;
    2.82  
    2.83 - finish:
    2.84  	return entry;
    2.85  }
    2.86  
     3.1 --- a/tools/blktap/drivers/tapdisk.h	Wed Oct 25 10:27:03 2006 +0100
     3.2 +++ b/tools/blktap/drivers/tapdisk.h	Wed Oct 25 10:56:50 2006 +0100
     3.3 @@ -191,9 +191,8 @@ static disk_info_t *dtypes[] = {
     3.4  };
     3.5  
     3.6  typedef struct driver_list_entry {
     3.7 -	void *blkif;
     3.8 -	void *prev;
     3.9 -	void *next;
    3.10 +	struct blkif *blkif;
    3.11 +	struct driver_list_entry **pprev, *next;
    3.12  } driver_list_entry_t;
    3.13  
    3.14  typedef struct fd_list_entry {
    3.15 @@ -201,8 +200,7 @@ typedef struct fd_list_entry {
    3.16  	int  tap_fd;
    3.17  	int  io_fd[MAX_IOFD];
    3.18  	struct td_state *s;
    3.19 -	void *prev;
    3.20 -	void *next;
    3.21 +	struct fd_list_entry **pprev, *next;
    3.22  } fd_list_entry_t;
    3.23  
    3.24  int qcow_create(const char *filename, uint64_t total_size,