debuggers.hg

changeset 22726:03718b569d97

xl: Implement flexarray_append() and flexarray_vappend()

Makes a lot of code simpler and nicer and saves a fair amount of screen
real-estate

Signed-off-by: Gianni Tedesco <gianni.tedesco@citrix.com>
Committed-by: Ian Jackson <ian.jackson@eu.citrix.com>
author Gianni Tedesco <gianni.tedesco@citrix.com>
date Fri Jan 07 18:53:50 2011 +0000 (2011-01-07)
parents e1392f5327ce
children 2dd233e88838
files tools/libxl/flexarray.c tools/libxl/flexarray.h tools/libxl/libxl.c tools/libxl/libxl_dm.c tools/libxl/libxl_pci.c
line diff
     1.1 --- a/tools/libxl/flexarray.c	Fri Jan 07 18:24:54 2011 +0000
     1.2 +++ b/tools/libxl/flexarray.c	Fri Jan 07 18:53:50 2011 +0000
     1.3 @@ -14,6 +14,7 @@
     1.4   */
     1.5  
     1.6  #include "libxl_internal.h"
     1.7 +#include <stdarg.h>
     1.8  
     1.9  flexarray_t *flexarray_make(int size, int autogrow)
    1.10  {
    1.11 @@ -21,6 +22,7 @@ flexarray_t *flexarray_make(int size, in
    1.12      if (array) {
    1.13          array->size = size;
    1.14          array->autogrow = autogrow;
    1.15 +        array->count = 0;
    1.16          array->data = calloc(size, sizeof(void *));
    1.17      }
    1.18      return array;
    1.19 @@ -56,10 +58,32 @@ int flexarray_set(flexarray_t *array, un
    1.20          if (flexarray_grow(array, newsize - array->size))
    1.21              return 2;
    1.22      }
    1.23 +    if ( index + 1 > array->count )
    1.24 +        array->count = index + 1;
    1.25      array->data[index] = ptr;
    1.26      return 0;
    1.27  }
    1.28  
    1.29 +int flexarray_append(flexarray_t *array, void *ptr)
    1.30 +{
    1.31 +    return flexarray_set(array, array->count, ptr);
    1.32 +}
    1.33 +
    1.34 +int flexarray_vappend(flexarray_t *array, ...)
    1.35 +{
    1.36 +    va_list va;
    1.37 +    void *ptr;
    1.38 +    int ret;
    1.39 +
    1.40 +    va_start(va, array);
    1.41 +    for(ret = 0; (ptr = va_arg(va, void *)); ret++) {
    1.42 +        if ( flexarray_append(array, ptr) )
    1.43 +            break;
    1.44 +    }
    1.45 +    va_end(va);
    1.46 +    return ret;
    1.47 +}
    1.48 +
    1.49  int flexarray_get(flexarray_t *array, int index, void **ptr)
    1.50  {
    1.51      if (index >= array->size)
     2.1 --- a/tools/libxl/flexarray.h	Fri Jan 07 18:24:54 2011 +0000
     2.2 +++ b/tools/libxl/flexarray.h	Fri Jan 07 18:53:50 2011 +0000
     2.3 @@ -19,6 +19,7 @@
     2.4  typedef struct flexarray {
     2.5      int size;
     2.6      int autogrow;
     2.7 +    unsigned int count;
     2.8      void **data; /* array of pointer */
     2.9  } flexarray_t;
    2.10  
    2.11 @@ -26,6 +27,8 @@ typedef struct flexarray {
    2.12  _hidden void flexarray_free(flexarray_t *array);
    2.13  _hidden int flexarray_grow(flexarray_t *array, int extents);
    2.14  _hidden int flexarray_set(flexarray_t *array, unsigned int index, void *ptr);
    2.15 +_hidden int flexarray_append(flexarray_t *array, void *ptr);
    2.16 +_hidden int flexarray_vappend(flexarray_t *array, ...);
    2.17  _hidden int flexarray_get(flexarray_t *array, int index, void **ptr);
    2.18  
    2.19  _hidden void **flexarray_contents(flexarray_t *array);
     3.1 --- a/tools/libxl/libxl.c	Fri Jan 07 18:24:54 2011 +0000
     3.2 +++ b/tools/libxl/libxl.c	Fri Jan 07 18:53:50 2011 +0000
     3.3 @@ -1075,8 +1075,6 @@ int libxl_device_disk_add(libxl_ctx *ctx
     3.4      flexarray_t *front;
     3.5      flexarray_t *back;
     3.6      char *backend_type;
     3.7 -    unsigned int boffset = 0;
     3.8 -    unsigned int foffset = 0;
     3.9      int devid;
    3.10      libxl__device device;
    3.11      int major, minor, rc;
    3.12 @@ -1111,11 +1109,11 @@ int libxl_device_disk_add(libxl_ctx *ctx
    3.13          case PHYSTYPE_PHY: {
    3.14  
    3.15              libxl__device_physdisk_major_minor(disk->physpath, &major, &minor);
    3.16 -            flexarray_set(back, boffset++, "physical-device");
    3.17 -            flexarray_set(back, boffset++, libxl__sprintf(&gc, "%x:%x", major, minor));
    3.18 -
    3.19 -            flexarray_set(back, boffset++, "params");
    3.20 -            flexarray_set(back, boffset++, disk->physpath);
    3.21 +            flexarray_append(back, "physical-device");
    3.22 +            flexarray_append(back, libxl__sprintf(&gc, "%x:%x", major, minor));
    3.23 +
    3.24 +            flexarray_append(back, "params");
    3.25 +            flexarray_append(back, disk->physpath);
    3.26  
    3.27              device.backend_kind = DEVICE_VBD;
    3.28              break;
    3.29 @@ -1134,20 +1132,20 @@ int libxl_device_disk_add(libxl_ctx *ctx
    3.30                      rc = ERROR_FAIL;
    3.31                      goto out_free;
    3.32                  }
    3.33 -                flexarray_set(back, boffset++, "tapdisk-params");
    3.34 -                flexarray_set(back, boffset++, libxl__sprintf(&gc, "%s:%s", libxl__device_disk_string_of_phystype(disk->phystype), disk->physpath));
    3.35 -                flexarray_set(back, boffset++, "params");
    3.36 -                flexarray_set(back, boffset++, libxl__strdup(&gc, dev));
    3.37 +                flexarray_append(back, "tapdisk-params");
    3.38 +                flexarray_append(back, libxl__sprintf(&gc, "%s:%s", libxl__device_disk_string_of_phystype(disk->phystype), disk->physpath));
    3.39 +                flexarray_append(back, "params");
    3.40 +                flexarray_append(back, libxl__strdup(&gc, dev));
    3.41                  backend_type = "phy";
    3.42                  libxl__device_physdisk_major_minor(dev, &major, &minor);
    3.43 -                flexarray_set(back, boffset++, "physical-device");
    3.44 -                flexarray_set(back, boffset++, libxl__sprintf(&gc, "%x:%x", major, minor));
    3.45 +                flexarray_append(back, "physical-device");
    3.46 +                flexarray_append(back, libxl__sprintf(&gc, "%x:%x", major, minor));
    3.47                  device.backend_kind = DEVICE_VBD;
    3.48  
    3.49                  break;
    3.50              }
    3.51 -            flexarray_set(back, boffset++, "params");
    3.52 -            flexarray_set(back, boffset++, libxl__sprintf(&gc, "%s:%s",
    3.53 +            flexarray_append(back, "params");
    3.54 +            flexarray_append(back, libxl__sprintf(&gc, "%s:%s",
    3.55                            libxl__device_disk_string_of_phystype(disk->phystype), disk->physpath));
    3.56  
    3.57              if (libxl__blktap_enabled(&gc))
    3.58 @@ -1162,40 +1160,40 @@ int libxl_device_disk_add(libxl_ctx *ctx
    3.59              goto out_free;
    3.60      }
    3.61  
    3.62 -    flexarray_set(back, boffset++, "frontend-id");
    3.63 -    flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", disk->domid));
    3.64 -    flexarray_set(back, boffset++, "online");
    3.65 -    flexarray_set(back, boffset++, "1");
    3.66 -    flexarray_set(back, boffset++, "removable");
    3.67 -    flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", (disk->unpluggable) ? 1 : 0));
    3.68 -    flexarray_set(back, boffset++, "bootable");
    3.69 -    flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", 1));
    3.70 -    flexarray_set(back, boffset++, "state");
    3.71 -    flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", 1));
    3.72 -    flexarray_set(back, boffset++, "dev");
    3.73 -    flexarray_set(back, boffset++, disk->virtpath);
    3.74 -    flexarray_set(back, boffset++, "type");
    3.75 -    flexarray_set(back, boffset++, backend_type);
    3.76 -    flexarray_set(back, boffset++, "mode");
    3.77 -    flexarray_set(back, boffset++, disk->readwrite ? "w" : "r");
    3.78 -
    3.79 -    flexarray_set(front, foffset++, "backend-id");
    3.80 -    flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", disk->backend_domid));
    3.81 -    flexarray_set(front, foffset++, "state");
    3.82 -    flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", 1));
    3.83 -    flexarray_set(front, foffset++, "virtual-device");
    3.84 -    flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", devid));
    3.85 -    flexarray_set(front, foffset++, "device-type");
    3.86 -    flexarray_set(front, foffset++, disk->is_cdrom ? "cdrom" : "disk");
    3.87 +    flexarray_append(back, "frontend-id");
    3.88 +    flexarray_append(back, libxl__sprintf(&gc, "%d", disk->domid));
    3.89 +    flexarray_append(back, "online");
    3.90 +    flexarray_append(back, "1");
    3.91 +    flexarray_append(back, "removable");
    3.92 +    flexarray_append(back, libxl__sprintf(&gc, "%d", (disk->unpluggable) ? 1 : 0));
    3.93 +    flexarray_append(back, "bootable");
    3.94 +    flexarray_append(back, libxl__sprintf(&gc, "%d", 1));
    3.95 +    flexarray_append(back, "state");
    3.96 +    flexarray_append(back, libxl__sprintf(&gc, "%d", 1));
    3.97 +    flexarray_append(back, "dev");
    3.98 +    flexarray_append(back, disk->virtpath);
    3.99 +    flexarray_append(back, "type");
   3.100 +    flexarray_append(back, backend_type);
   3.101 +    flexarray_append(back, "mode");
   3.102 +    flexarray_append(back, disk->readwrite ? "w" : "r");
   3.103 +
   3.104 +    flexarray_append(front, "backend-id");
   3.105 +    flexarray_append(front, libxl__sprintf(&gc, "%d", disk->backend_domid));
   3.106 +    flexarray_append(front, "state");
   3.107 +    flexarray_append(front, libxl__sprintf(&gc, "%d", 1));
   3.108 +    flexarray_append(front, "virtual-device");
   3.109 +    flexarray_append(front, libxl__sprintf(&gc, "%d", devid));
   3.110 +    flexarray_append(front, "device-type");
   3.111 +    flexarray_append(front, disk->is_cdrom ? "cdrom" : "disk");
   3.112  
   3.113      if (0 /* protocol != native*/) {
   3.114 -        flexarray_set(front, foffset++, "protocol");
   3.115 -        flexarray_set(front, foffset++, "x86_32-abi"); /* hardcoded ! */
   3.116 +        flexarray_append(front, "protocol");
   3.117 +        flexarray_append(front, "x86_32-abi"); /* hardcoded ! */
   3.118      }
   3.119  
   3.120      libxl__device_generic_add(ctx, &device,
   3.121 -                             libxl__xs_kvs_of_flexarray(&gc, back, boffset),
   3.122 -                             libxl__xs_kvs_of_flexarray(&gc, front, foffset));
   3.123 +                             libxl__xs_kvs_of_flexarray(&gc, back, back->count),
   3.124 +                             libxl__xs_kvs_of_flexarray(&gc, front, front->count));
   3.125  
   3.126      rc = 0;
   3.127  
   3.128 @@ -1274,8 +1272,6 @@ int libxl_device_nic_add(libxl_ctx *ctx,
   3.129      libxl__gc gc = LIBXL_INIT_GC(ctx);
   3.130      flexarray_t *front;
   3.131      flexarray_t *back;
   3.132 -    unsigned int boffset = 0;
   3.133 -    unsigned int foffset = 0;
   3.134      libxl__device device;
   3.135      char *dompath, **l;
   3.136      unsigned int nb, rc;
   3.137 @@ -1311,41 +1307,41 @@ int libxl_device_nic_add(libxl_ctx *ctx,
   3.138      device.domid = nic->domid;
   3.139      device.kind = DEVICE_VIF;
   3.140  
   3.141 -    flexarray_set(back, boffset++, "frontend-id");
   3.142 -    flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", nic->domid));
   3.143 -    flexarray_set(back, boffset++, "online");
   3.144 -    flexarray_set(back, boffset++, "1");
   3.145 -    flexarray_set(back, boffset++, "state");
   3.146 -    flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", 1));
   3.147 -    flexarray_set(back, boffset++, "script");
   3.148 -    flexarray_set(back, boffset++, nic->script);
   3.149 -    flexarray_set(back, boffset++, "mac");
   3.150 -    flexarray_set(back, boffset++, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
   3.151 +    flexarray_append(back, "frontend-id");
   3.152 +    flexarray_append(back, libxl__sprintf(&gc, "%d", nic->domid));
   3.153 +    flexarray_append(back, "online");
   3.154 +    flexarray_append(back, "1");
   3.155 +    flexarray_append(back, "state");
   3.156 +    flexarray_append(back, libxl__sprintf(&gc, "%d", 1));
   3.157 +    flexarray_append(back, "script");
   3.158 +    flexarray_append(back, nic->script);
   3.159 +    flexarray_append(back, "mac");
   3.160 +    flexarray_append(back, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
   3.161                                                   nic->mac[0], nic->mac[1], nic->mac[2],
   3.162                                                   nic->mac[3], nic->mac[4], nic->mac[5]));
   3.163 -    flexarray_set(back, boffset++, "bridge");
   3.164 -    flexarray_set(back, boffset++, libxl__strdup(&gc, nic->bridge));
   3.165 -    flexarray_set(back, boffset++, "handle");
   3.166 -    flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", nic->devid));
   3.167 -
   3.168 -    flexarray_set(front, foffset++, "backend-id");
   3.169 -    flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", nic->backend_domid));
   3.170 -    flexarray_set(front, foffset++, "state");
   3.171 -    flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", 1));
   3.172 -    flexarray_set(front, foffset++, "handle");
   3.173 -    flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", nic->devid));
   3.174 -    flexarray_set(front, foffset++, "mac");
   3.175 -    flexarray_set(front, foffset++, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
   3.176 +    flexarray_append(back, "bridge");
   3.177 +    flexarray_append(back, libxl__strdup(&gc, nic->bridge));
   3.178 +    flexarray_append(back, "handle");
   3.179 +    flexarray_append(back, libxl__sprintf(&gc, "%d", nic->devid));
   3.180 +
   3.181 +    flexarray_append(front, "backend-id");
   3.182 +    flexarray_append(front, libxl__sprintf(&gc, "%d", nic->backend_domid));
   3.183 +    flexarray_append(front, "state");
   3.184 +    flexarray_append(front, libxl__sprintf(&gc, "%d", 1));
   3.185 +    flexarray_append(front, "handle");
   3.186 +    flexarray_append(front, libxl__sprintf(&gc, "%d", nic->devid));
   3.187 +    flexarray_append(front, "mac");
   3.188 +    flexarray_append(front, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
   3.189                                                    nic->mac[0], nic->mac[1], nic->mac[2],
   3.190                                                    nic->mac[3], nic->mac[4], nic->mac[5]));
   3.191      if (0 /* protocol != native*/) {
   3.192 -        flexarray_set(front, foffset++, "protocol");
   3.193 -        flexarray_set(front, foffset++, "x86_32-abi"); /* hardcoded ! */
   3.194 +        flexarray_append(front, "protocol");
   3.195 +        flexarray_append(front, "x86_32-abi"); /* hardcoded ! */
   3.196      }
   3.197  
   3.198      libxl__device_generic_add(ctx, &device,
   3.199 -                             libxl__xs_kvs_of_flexarray(&gc, back, boffset),
   3.200 -                             libxl__xs_kvs_of_flexarray(&gc, front, foffset));
   3.201 +                             libxl__xs_kvs_of_flexarray(&gc, back, back->count),
   3.202 +                             libxl__xs_kvs_of_flexarray(&gc, front, front->count));
   3.203  
   3.204      /* FIXME: wait for plug */
   3.205      rc = 0;
   3.206 @@ -1433,7 +1429,6 @@ int libxl_device_net2_add(libxl_ctx *ctx
   3.207  {
   3.208      libxl__gc gc = LIBXL_INIT_GC(ctx);
   3.209      flexarray_t *front, *back;
   3.210 -    unsigned int boffset = 0, foffset = 0;
   3.211      libxl__device device;
   3.212      char *dompath, *dom, **l;
   3.213      unsigned int nb;
   3.214 @@ -1472,65 +1467,65 @@ int libxl_device_net2_add(libxl_ctx *ctx
   3.215      device.domid = net2->domid;
   3.216      device.kind = DEVICE_VIF2;
   3.217  
   3.218 -    flexarray_set(back, boffset++, "domain");
   3.219 -    flexarray_set(back, boffset++, dom);
   3.220 -    flexarray_set(back, boffset++, "frontend-id");
   3.221 -    flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", net2->domid));
   3.222 -
   3.223 -    flexarray_set(back, boffset++, "local-trusted");
   3.224 -    flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", net2->back_trusted));
   3.225 -    flexarray_set(back, boffset++, "mac");
   3.226 -    flexarray_set(back, boffset++, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
   3.227 +    flexarray_append(back, "domain");
   3.228 +    flexarray_append(back, dom);
   3.229 +    flexarray_append(back, "frontend-id");
   3.230 +    flexarray_append(back, libxl__sprintf(&gc, "%d", net2->domid));
   3.231 +
   3.232 +    flexarray_append(back, "local-trusted");
   3.233 +    flexarray_append(back, libxl__sprintf(&gc, "%d", net2->back_trusted));
   3.234 +    flexarray_append(back, "mac");
   3.235 +    flexarray_append(back, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
   3.236                                                   net2->back_mac[0], net2->back_mac[1],
   3.237                                                   net2->back_mac[2], net2->back_mac[3],
   3.238                                                   net2->back_mac[4], net2->back_mac[5]));
   3.239  
   3.240 -    flexarray_set(back, boffset++, "remote-trusted");
   3.241 -    flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", net2->trusted));
   3.242 -    flexarray_set(back, boffset++, "remote-mac");
   3.243 -    flexarray_set(back, boffset++, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
   3.244 +    flexarray_append(back, "remote-trusted");
   3.245 +    flexarray_append(back, libxl__sprintf(&gc, "%d", net2->trusted));
   3.246 +    flexarray_append(back, "remote-mac");
   3.247 +    flexarray_append(back, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
   3.248                                                   net2->front_mac[0], net2->front_mac[1],
   3.249                                                   net2->front_mac[2], net2->front_mac[3],
   3.250                                                   net2->front_mac[4], net2->front_mac[5]));
   3.251  
   3.252 -    flexarray_set(back, boffset++, "max-bypasses");
   3.253 -    flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", net2->max_bypasses));
   3.254 -    flexarray_set(back, boffset++, "filter-mac");
   3.255 -    flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", !!(net2->filter_mac)));
   3.256 -    flexarray_set(back, boffset++, "handle");
   3.257 -    flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", net2->devid));
   3.258 -    flexarray_set(back, boffset++, "online");
   3.259 -    flexarray_set(back, boffset++, "1");
   3.260 -    flexarray_set(back, boffset++, "state");
   3.261 -    flexarray_set(back, boffset++, "1");
   3.262 -
   3.263 -    flexarray_set(front, foffset++, "backend-id");
   3.264 -    flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", net2->backend_domid));
   3.265 -
   3.266 -    flexarray_set(front, foffset++, "local-trusted");
   3.267 -    flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", net2->trusted));
   3.268 -    flexarray_set(front, foffset++, "mac");
   3.269 -    flexarray_set(front, foffset++, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
   3.270 +    flexarray_append(back, "max-bypasses");
   3.271 +    flexarray_append(back, libxl__sprintf(&gc, "%d", net2->max_bypasses));
   3.272 +    flexarray_append(back, "filter-mac");
   3.273 +    flexarray_append(back, libxl__sprintf(&gc, "%d", !!(net2->filter_mac)));
   3.274 +    flexarray_append(back, "handle");
   3.275 +    flexarray_append(back, libxl__sprintf(&gc, "%d", net2->devid));
   3.276 +    flexarray_append(back, "online");
   3.277 +    flexarray_append(back, "1");
   3.278 +    flexarray_append(back, "state");
   3.279 +    flexarray_append(back, "1");
   3.280 +
   3.281 +    flexarray_append(front, "backend-id");
   3.282 +    flexarray_append(front, libxl__sprintf(&gc, "%d", net2->backend_domid));
   3.283 +
   3.284 +    flexarray_append(front, "local-trusted");
   3.285 +    flexarray_append(front, libxl__sprintf(&gc, "%d", net2->trusted));
   3.286 +    flexarray_append(front, "mac");
   3.287 +    flexarray_append(front, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
   3.288                                                    net2->front_mac[0], net2->front_mac[1],
   3.289                                                    net2->front_mac[2], net2->front_mac[3],
   3.290                                                    net2->front_mac[4], net2->front_mac[5]));
   3.291  
   3.292 -    flexarray_set(front, foffset++, "remote-trusted");
   3.293 -    flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", net2->back_trusted));
   3.294 -    flexarray_set(front, foffset++, "remote-mac");
   3.295 -    flexarray_set(front, foffset++, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
   3.296 +    flexarray_append(front, "remote-trusted");
   3.297 +    flexarray_append(front, libxl__sprintf(&gc, "%d", net2->back_trusted));
   3.298 +    flexarray_append(front, "remote-mac");
   3.299 +    flexarray_append(front, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
   3.300                                                    net2->back_mac[0], net2->back_mac[1],
   3.301                                                    net2->back_mac[2], net2->back_mac[3],
   3.302                                                    net2->back_mac[4], net2->back_mac[5]));
   3.303  
   3.304 -    flexarray_set(front, foffset++, "filter-mac");
   3.305 -    flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", !!(net2->filter_mac)));
   3.306 -    flexarray_set(front, foffset++, "state");
   3.307 -    flexarray_set(front, foffset++, "1");
   3.308 +    flexarray_append(front, "filter-mac");
   3.309 +    flexarray_append(front, libxl__sprintf(&gc, "%d", !!(net2->filter_mac)));
   3.310 +    flexarray_append(front, "state");
   3.311 +    flexarray_append(front, "1");
   3.312  
   3.313      libxl__device_generic_add(ctx, &device,
   3.314 -                             libxl__xs_kvs_of_flexarray(&gc, back, boffset),
   3.315 -                             libxl__xs_kvs_of_flexarray(&gc, front, foffset));
   3.316 +                             libxl__xs_kvs_of_flexarray(&gc, back, back->count),
   3.317 +                             libxl__xs_kvs_of_flexarray(&gc, front, front->count));
   3.318  
   3.319      /* FIXME: wait for plug */
   3.320      rc = 0;
   3.321 @@ -1624,8 +1619,6 @@ int libxl_device_console_add(libxl_ctx *
   3.322      libxl__gc gc = LIBXL_INIT_GC(ctx);
   3.323      flexarray_t *front;
   3.324      flexarray_t *back;
   3.325 -    unsigned int boffset = 0;
   3.326 -    unsigned int foffset = 0;
   3.327      libxl__device device;
   3.328      int rc;
   3.329  
   3.330 @@ -1647,48 +1640,48 @@ int libxl_device_console_add(libxl_ctx *
   3.331      device.domid = console->domid;
   3.332      device.kind = DEVICE_CONSOLE;
   3.333  
   3.334 -    flexarray_set(back, boffset++, "frontend-id");
   3.335 -    flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", console->domid));
   3.336 -    flexarray_set(back, boffset++, "online");
   3.337 -    flexarray_set(back, boffset++, "1");
   3.338 -    flexarray_set(back, boffset++, "state");
   3.339 -    flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", 1));
   3.340 -    flexarray_set(back, boffset++, "domain");
   3.341 -    flexarray_set(back, boffset++, libxl__domid_to_name(&gc, domid));
   3.342 -    flexarray_set(back, boffset++, "protocol");
   3.343 -    flexarray_set(back, boffset++, LIBXL_XENCONSOLE_PROTOCOL);
   3.344 -
   3.345 -    flexarray_set(front, foffset++, "backend-id");
   3.346 -    flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", console->backend_domid));
   3.347 -    flexarray_set(front, foffset++, "limit");
   3.348 -    flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", LIBXL_XENCONSOLE_LIMIT));
   3.349 -    flexarray_set(front, foffset++, "type");
   3.350 +    flexarray_append(back, "frontend-id");
   3.351 +    flexarray_append(back, libxl__sprintf(&gc, "%d", console->domid));
   3.352 +    flexarray_append(back, "online");
   3.353 +    flexarray_append(back, "1");
   3.354 +    flexarray_append(back, "state");
   3.355 +    flexarray_append(back, libxl__sprintf(&gc, "%d", 1));
   3.356 +    flexarray_append(back, "domain");
   3.357 +    flexarray_append(back, libxl__domid_to_name(&gc, domid));
   3.358 +    flexarray_append(back, "protocol");
   3.359 +    flexarray_append(back, LIBXL_XENCONSOLE_PROTOCOL);
   3.360 +
   3.361 +    flexarray_append(front, "backend-id");
   3.362 +    flexarray_append(front, libxl__sprintf(&gc, "%d", console->backend_domid));
   3.363 +    flexarray_append(front, "limit");
   3.364 +    flexarray_append(front, libxl__sprintf(&gc, "%d", LIBXL_XENCONSOLE_LIMIT));
   3.365 +    flexarray_append(front, "type");
   3.366      if (console->consback == LIBXL_CONSBACK_XENCONSOLED)
   3.367 -        flexarray_set(front, foffset++, "xenconsoled");
   3.368 +        flexarray_append(front, "xenconsoled");
   3.369      else
   3.370 -        flexarray_set(front, foffset++, "ioemu");
   3.371 -    flexarray_set(front, foffset++, "output");
   3.372 -    flexarray_set(front, foffset++, console->output);
   3.373 +        flexarray_append(front, "ioemu");
   3.374 +    flexarray_append(front, "output");
   3.375 +    flexarray_append(front, console->output);
   3.376  
   3.377      if (device.devid == 0) {
   3.378          if (console->build_state == NULL) {
   3.379              rc = ERROR_INVAL;
   3.380              goto out_free;
   3.381          }
   3.382 -        flexarray_set(front, foffset++, "port");
   3.383 -        flexarray_set(front, foffset++, libxl__sprintf(&gc, "%"PRIu32, console->build_state->console_port));
   3.384 -        flexarray_set(front, foffset++, "ring-ref");
   3.385 -        flexarray_set(front, foffset++, libxl__sprintf(&gc, "%lu", console->build_state->console_mfn));
   3.386 +        flexarray_append(front, "port");
   3.387 +        flexarray_append(front, libxl__sprintf(&gc, "%"PRIu32, console->build_state->console_port));
   3.388 +        flexarray_append(front, "ring-ref");
   3.389 +        flexarray_append(front, libxl__sprintf(&gc, "%lu", console->build_state->console_mfn));
   3.390      } else {
   3.391 -        flexarray_set(front, foffset++, "state");
   3.392 -        flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", 1));
   3.393 -        flexarray_set(front, foffset++, "protocol");
   3.394 -        flexarray_set(front, foffset++, LIBXL_XENCONSOLE_PROTOCOL);
   3.395 +        flexarray_append(front, "state");
   3.396 +        flexarray_append(front, libxl__sprintf(&gc, "%d", 1));
   3.397 +        flexarray_append(front, "protocol");
   3.398 +        flexarray_append(front, LIBXL_XENCONSOLE_PROTOCOL);
   3.399      }
   3.400  
   3.401      libxl__device_generic_add(ctx, &device,
   3.402 -                             libxl__xs_kvs_of_flexarray(&gc, back, boffset),
   3.403 -                             libxl__xs_kvs_of_flexarray(&gc, front, foffset));
   3.404 +                             libxl__xs_kvs_of_flexarray(&gc, back, back->count),
   3.405 +                             libxl__xs_kvs_of_flexarray(&gc, front, front->count));
   3.406      rc = 0;
   3.407  out_free:
   3.408      flexarray_free(back);
   3.409 @@ -1704,8 +1697,6 @@ int libxl_device_vkb_add(libxl_ctx *ctx,
   3.410      libxl__gc gc = LIBXL_INIT_GC(ctx);
   3.411      flexarray_t *front;
   3.412      flexarray_t *back;
   3.413 -    unsigned int boffset = 0;
   3.414 -    unsigned int foffset = 0;
   3.415      libxl__device device;
   3.416      int rc;
   3.417  
   3.418 @@ -1727,23 +1718,23 @@ int libxl_device_vkb_add(libxl_ctx *ctx,
   3.419      device.domid = vkb->domid;
   3.420      device.kind = DEVICE_VKBD;
   3.421  
   3.422 -    flexarray_set(back, boffset++, "frontend-id");
   3.423 -    flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vkb->domid));
   3.424 -    flexarray_set(back, boffset++, "online");
   3.425 -    flexarray_set(back, boffset++, "1");
   3.426 -    flexarray_set(back, boffset++, "state");
   3.427 -    flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", 1));
   3.428 -    flexarray_set(back, boffset++, "domain");
   3.429 -    flexarray_set(back, boffset++, libxl__domid_to_name(&gc, domid));
   3.430 -
   3.431 -    flexarray_set(front, foffset++, "backend-id");
   3.432 -    flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", vkb->backend_domid));
   3.433 -    flexarray_set(front, foffset++, "state");
   3.434 -    flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", 1));
   3.435 +    flexarray_append(back, "frontend-id");
   3.436 +    flexarray_append(back, libxl__sprintf(&gc, "%d", vkb->domid));
   3.437 +    flexarray_append(back, "online");
   3.438 +    flexarray_append(back, "1");
   3.439 +    flexarray_append(back, "state");
   3.440 +    flexarray_append(back, libxl__sprintf(&gc, "%d", 1));
   3.441 +    flexarray_append(back, "domain");
   3.442 +    flexarray_append(back, libxl__domid_to_name(&gc, domid));
   3.443 +
   3.444 +    flexarray_append(front, "backend-id");
   3.445 +    flexarray_append(front, libxl__sprintf(&gc, "%d", vkb->backend_domid));
   3.446 +    flexarray_append(front, "state");
   3.447 +    flexarray_append(front, libxl__sprintf(&gc, "%d", 1));
   3.448  
   3.449      libxl__device_generic_add(ctx, &device,
   3.450 -                             libxl__xs_kvs_of_flexarray(&gc, back, boffset),
   3.451 -                             libxl__xs_kvs_of_flexarray(&gc, front, foffset));
   3.452 +                             libxl__xs_kvs_of_flexarray(&gc, back, back->count),
   3.453 +                             libxl__xs_kvs_of_flexarray(&gc, front, front->count));
   3.454      rc = 0;
   3.455  out_free:
   3.456      flexarray_free(back);
   3.457 @@ -1904,8 +1895,6 @@ int libxl_device_vfb_add(libxl_ctx *ctx,
   3.458      libxl__gc gc = LIBXL_INIT_GC(ctx);
   3.459      flexarray_t *front;
   3.460      flexarray_t *back;
   3.461 -    unsigned int boffset = 0;
   3.462 -    unsigned int foffset = 0;
   3.463      libxl__device device;
   3.464      int rc;
   3.465  
   3.466 @@ -1927,45 +1916,31 @@ int libxl_device_vfb_add(libxl_ctx *ctx,
   3.467      device.domid = vfb->domid;
   3.468      device.kind = DEVICE_VFB;
   3.469  
   3.470 -    flexarray_set(back, boffset++, "frontend-id");
   3.471 -    flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vfb->domid));
   3.472 -    flexarray_set(back, boffset++, "online");
   3.473 -    flexarray_set(back, boffset++, "1");
   3.474 -    flexarray_set(back, boffset++, "state");
   3.475 -    flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", 1));
   3.476 -    flexarray_set(back, boffset++, "domain");
   3.477 -    flexarray_set(back, boffset++, libxl__domid_to_name(&gc, domid));
   3.478 -    flexarray_set(back, boffset++, "vnc");
   3.479 -    flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vfb->vnc));
   3.480 -    flexarray_set(back, boffset++, "vnclisten");
   3.481 -    flexarray_set(back, boffset++, vfb->vnclisten);
   3.482 -    flexarray_set(back, boffset++, "vncpasswd");
   3.483 -    flexarray_set(back, boffset++, vfb->vncpasswd);
   3.484 -    flexarray_set(back, boffset++, "vncdisplay");
   3.485 -    flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vfb->vncdisplay));
   3.486 -    flexarray_set(back, boffset++, "vncunused");
   3.487 -    flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vfb->vncunused));
   3.488 -    flexarray_set(back, boffset++, "sdl");
   3.489 -    flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vfb->sdl));
   3.490 -    flexarray_set(back, boffset++, "opengl");
   3.491 -    flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vfb->opengl));
   3.492 +    flexarray_vappend(back, "frontend-id", libxl__sprintf(&gc, "%d", vfb->domid), NULL);
   3.493 +    flexarray_vappend(back, "online", "1", NULL);
   3.494 +    flexarray_vappend(back, "state", libxl__sprintf(&gc, "%d", 1), NULL);
   3.495 +    flexarray_vappend(back, "domain", libxl__domid_to_name(&gc, domid), NULL);
   3.496 +    flexarray_vappend(back, "vnc", libxl__sprintf(&gc, "%d", vfb->vnc), NULL);
   3.497 +    flexarray_vappend(back, "vnclisten", vfb->vnclisten, NULL);
   3.498 +    flexarray_append(back, "vncpasswd");
   3.499 +    flexarray_append(back, vfb->vncpasswd);
   3.500 +    flexarray_vappend(back, "vncdisplay", libxl__sprintf(&gc, "%d", vfb->vncdisplay), NULL);
   3.501 +    flexarray_vappend(back, "vncunused", libxl__sprintf(&gc, "%d", vfb->vncunused), NULL);
   3.502 +    flexarray_vappend(back, "sdl", libxl__sprintf(&gc, "%d", vfb->sdl), NULL);
   3.503 +    flexarray_vappend(back, "opengl", libxl__sprintf(&gc, "%d", vfb->opengl), NULL);
   3.504      if (vfb->xauthority) {
   3.505 -        flexarray_set(back, boffset++, "xauthority");
   3.506 -        flexarray_set(back, boffset++, vfb->xauthority);
   3.507 +        flexarray_vappend(back, "xauthority", vfb->xauthority, NULL);
   3.508      }
   3.509      if (vfb->display) {
   3.510 -        flexarray_set(back, boffset++, "display");
   3.511 -        flexarray_set(back, boffset++, vfb->display);
   3.512 +        flexarray_vappend(back, "display", vfb->display, NULL);
   3.513      }
   3.514  
   3.515 -    flexarray_set(front, foffset++, "backend-id");
   3.516 -    flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", vfb->backend_domid));
   3.517 -    flexarray_set(front, foffset++, "state");
   3.518 -    flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", 1));
   3.519 +    flexarray_vappend(front, "backend-id", libxl__sprintf(&gc, "%d", vfb->backend_domid), NULL);
   3.520 +    flexarray_vappend(front, "state", libxl__sprintf(&gc, "%d", 1), NULL);
   3.521  
   3.522      libxl__device_generic_add(ctx, &device,
   3.523 -                             libxl__xs_kvs_of_flexarray(&gc, back, boffset),
   3.524 -                             libxl__xs_kvs_of_flexarray(&gc, front, foffset));
   3.525 +                             libxl__xs_kvs_of_flexarray(&gc, back, back->count),
   3.526 +                             libxl__xs_kvs_of_flexarray(&gc, front, front->count));
   3.527      rc = 0;
   3.528  out_free:
   3.529      flexarray_free(front);
     4.1 --- a/tools/libxl/libxl_dm.c	Fri Jan 07 18:24:54 2011 +0000
     4.2 +++ b/tools/libxl/libxl_dm.c	Fri Jan 07 18:53:50 2011 +0000
     4.3 @@ -32,101 +32,88 @@ static char ** libxl_build_device_model_
     4.4                                               libxl_device_nic *vifs,
     4.5                                               int num_vifs)
     4.6  {
     4.7 -    int num = 0, i;
     4.8 +    int i;
     4.9      flexarray_t *dm_args;
    4.10      dm_args = flexarray_make(16, 1);
    4.11  
    4.12      if (!dm_args)
    4.13          return NULL;
    4.14  
    4.15 -    flexarray_set(dm_args, num++, "qemu-dm");
    4.16 -    flexarray_set(dm_args, num++, "-d");
    4.17 -
    4.18 -    flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d", info->domid));
    4.19 +    flexarray_vappend(dm_args, "qemu-dm", "-d", libxl__sprintf(gc, "%d", info->domid), NULL);
    4.20  
    4.21 -    if (info->dom_name) {
    4.22 -        flexarray_set(dm_args, num++, "-domain-name");
    4.23 -        flexarray_set(dm_args, num++, info->dom_name);
    4.24 -    }
    4.25 +    if (info->dom_name)
    4.26 +        flexarray_vappend(dm_args, "-domain-name", info->dom_name, NULL);
    4.27 +
    4.28      if (info->vnc || info->vncdisplay || info->vnclisten || info->vncunused) {
    4.29 -        flexarray_set(dm_args, num++, "-vnc");
    4.30 +        flexarray_append(dm_args, "-vnc");
    4.31          if (info->vncdisplay) {
    4.32              if (info->vnclisten && strchr(info->vnclisten, ':') == NULL) {
    4.33 -                flexarray_set(
    4.34 -                    dm_args, num++,
    4.35 +                flexarray_append(dm_args, 
    4.36                      libxl__sprintf(gc, "%s:%d%s",
    4.37                                    info->vnclisten,
    4.38                                    info->vncdisplay,
    4.39                                    info->vncpasswd ? ",password" : ""));
    4.40              } else {
    4.41 -                flexarray_set(dm_args, num++, libxl__sprintf(gc, "127.0.0.1:%d", info->vncdisplay));
    4.42 +                flexarray_append(dm_args, libxl__sprintf(gc, "127.0.0.1:%d", info->vncdisplay));
    4.43              }
    4.44          } else if (info->vnclisten) {
    4.45              if (strchr(info->vnclisten, ':') != NULL) {
    4.46 -                flexarray_set(dm_args, num++, info->vnclisten);
    4.47 +                flexarray_append(dm_args, info->vnclisten);
    4.48              } else {
    4.49 -                flexarray_set(dm_args, num++, libxl__sprintf(gc, "%s:0", info->vnclisten));
    4.50 +                flexarray_append(dm_args, libxl__sprintf(gc, "%s:0", info->vnclisten));
    4.51              }
    4.52          } else {
    4.53 -            flexarray_set(dm_args, num++, "127.0.0.1:0");
    4.54 +            flexarray_append(dm_args, "127.0.0.1:0");
    4.55          }
    4.56          if (info->vncunused) {
    4.57 -            flexarray_set(dm_args, num++, "-vncunused");
    4.58 +            flexarray_append(dm_args, "-vncunused");
    4.59          }
    4.60      }
    4.61      if (info->sdl) {
    4.62 -        flexarray_set(dm_args, num++, "-sdl");
    4.63 +        flexarray_append(dm_args, "-sdl");
    4.64          if (!info->opengl) {
    4.65 -            flexarray_set(dm_args, num++, "-disable-opengl");
    4.66 +            flexarray_append(dm_args, "-disable-opengl");
    4.67          }
    4.68      }
    4.69      if (info->keymap) {
    4.70 -        flexarray_set(dm_args, num++, "-k");
    4.71 -        flexarray_set(dm_args, num++, info->keymap);
    4.72 +        flexarray_vappend(dm_args, "-k", info->keymap, NULL);
    4.73      }
    4.74      if (info->nographic && (!info->sdl && !info->vnc)) {
    4.75 -        flexarray_set(dm_args, num++, "-nographic");
    4.76 +        flexarray_append(dm_args, "-nographic");
    4.77      }
    4.78      if (info->serial) {
    4.79 -        flexarray_set(dm_args, num++, "-serial");
    4.80 -        flexarray_set(dm_args, num++, info->serial);
    4.81 +        flexarray_vappend(dm_args, "-serial", info->serial, NULL);
    4.82      }
    4.83      if (info->type == XENFV) {
    4.84          int ioemu_vifs = 0;
    4.85  
    4.86          if (info->videoram) {
    4.87 -            flexarray_set(dm_args, num++, "-videoram");
    4.88 -            flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d", info->videoram));
    4.89 +            flexarray_vappend(dm_args, "-videoram", libxl__sprintf(gc, "%d", info->videoram), NULL);
    4.90          }
    4.91          if (info->stdvga) {
    4.92 -            flexarray_set(dm_args, num++, "-std-vga");
    4.93 +            flexarray_append(dm_args, "-std-vga");
    4.94          }
    4.95  
    4.96          if (info->boot) {
    4.97 -            flexarray_set(dm_args, num++, "-boot");
    4.98 -            flexarray_set(dm_args, num++, info->boot);
    4.99 +            flexarray_vappend(dm_args, "-boot", info->boot, NULL);
   4.100          }
   4.101          if (info->usb || info->usbdevice) {
   4.102 -            flexarray_set(dm_args, num++, "-usb");
   4.103 +            flexarray_append(dm_args, "-usb");
   4.104              if (info->usbdevice) {
   4.105 -                flexarray_set(dm_args, num++, "-usbdevice");
   4.106 -                flexarray_set(dm_args, num++, info->usbdevice);
   4.107 +                flexarray_vappend(dm_args, "-usbdevice", info->usbdevice, NULL);
   4.108              }
   4.109          }
   4.110          if (info->soundhw) {
   4.111 -            flexarray_set(dm_args, num++, "-soundhw");
   4.112 -            flexarray_set(dm_args, num++, info->soundhw);
   4.113 +            flexarray_vappend(dm_args, "-soundhw", info->soundhw, NULL);
   4.114          }
   4.115          if (info->apic) {
   4.116 -            flexarray_set(dm_args, num++, "-acpi");
   4.117 +            flexarray_append(dm_args, "-acpi");
   4.118          }
   4.119          if (info->vcpus > 1) {
   4.120 -            flexarray_set(dm_args, num++, "-vcpus");
   4.121 -            flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d", info->vcpus));
   4.122 +            flexarray_vappend(dm_args, "-vcpus", libxl__sprintf(gc, "%d", info->vcpus), NULL);
   4.123          }
   4.124          if (info->vcpu_avail) {
   4.125 -            flexarray_set(dm_args, num++, "-vcpu_avail");
   4.126 -            flexarray_set(dm_args, num++, libxl__sprintf(gc, "0x%x", info->vcpu_avail));
   4.127 +            flexarray_vappend(dm_args, "-vcpu_avail", libxl__sprintf(gc, "0x%x", info->vcpu_avail), NULL);
   4.128          }
   4.129          for (i = 0; i < num_vifs; i++) {
   4.130              if (vifs[i].nictype == NICTYPE_IOEMU) {
   4.131 @@ -138,33 +125,30 @@ static char ** libxl_build_device_model_
   4.132                      ifname = libxl__sprintf(gc, "tap%d.%d", info->domid, vifs[i].devid);
   4.133                  else
   4.134                      ifname = vifs[i].ifname;
   4.135 -                flexarray_set(dm_args, num++, "-net");
   4.136 -                flexarray_set(dm_args, num++, libxl__sprintf(gc, "nic,vlan=%d,macaddr=%s,model=%s",
   4.137 -                            vifs[i].devid, smac, vifs[i].model));
   4.138 -                flexarray_set(dm_args, num++, "-net");
   4.139 -                flexarray_set(dm_args, num++, libxl__sprintf(gc, "tap,vlan=%d,ifname=%s,bridge=%s,script=no",
   4.140 -                            vifs[i].devid, ifname, vifs[i].bridge));
   4.141 +                flexarray_vappend(dm_args,
   4.142 +                                "-net", libxl__sprintf(gc, "nic,vlan=%d,macaddr=%s,model=%s",
   4.143 +                                                       vifs[i].devid, smac, vifs[i].model),
   4.144 +                                "-net", libxl__sprintf(gc, "tap,vlan=%d,ifname=%s,bridge=%s,script=no",
   4.145 +                                                       vifs[i].devid, ifname, vifs[i].bridge), NULL);
   4.146                  ioemu_vifs++;
   4.147              }
   4.148          }
   4.149          /* If we have no emulated nics, tell qemu not to create any */
   4.150          if ( ioemu_vifs == 0 ) {
   4.151 -            flexarray_set(dm_args, num++, "-net");
   4.152 -            flexarray_set(dm_args, num++, "none");
   4.153 +            flexarray_vappend(dm_args, "-net", "none", NULL);
   4.154          }
   4.155      }
   4.156      if (info->saved_state) {
   4.157 -        flexarray_set(dm_args, num++, "-loadvm");
   4.158 -        flexarray_set(dm_args, num++, info->saved_state);
   4.159 +        flexarray_vappend(dm_args, "-loadvm", info->saved_state, NULL);
   4.160      }
   4.161      for (i = 0; info->extra && info->extra[i] != NULL; i++)
   4.162 -        flexarray_set(dm_args, num++, info->extra[i]);
   4.163 -    flexarray_set(dm_args, num++, "-M");
   4.164 +        flexarray_append(dm_args, info->extra[i]);
   4.165 +    flexarray_append(dm_args, "-M");
   4.166      if (info->type == XENPV)
   4.167 -        flexarray_set(dm_args, num++, "xenpv");
   4.168 +        flexarray_append(dm_args, "xenpv");
   4.169      else
   4.170 -        flexarray_set(dm_args, num++, "xenfv");
   4.171 -    flexarray_set(dm_args, num++, NULL);
   4.172 +        flexarray_append(dm_args, "xenfv");
   4.173 +    flexarray_append(dm_args, NULL);
   4.174      return (char **) flexarray_contents(dm_args);
   4.175  }
   4.176  
   4.177 @@ -173,33 +157,29 @@ static char ** libxl_build_device_model_
   4.178                                               libxl_device_nic *vifs,
   4.179                                               int num_vifs)
   4.180  {
   4.181 -    int num = 0, i;
   4.182      flexarray_t *dm_args;
   4.183 -    int nb;
   4.184      libxl_device_disk *disks;
   4.185 +    int nb, i;
   4.186  
   4.187      dm_args = flexarray_make(16, 1);
   4.188      if (!dm_args)
   4.189          return NULL;
   4.190  
   4.191 -    flexarray_set(dm_args, num++, libxl__strdup(gc, info->device_model));
   4.192 -
   4.193 -    flexarray_set(dm_args, num++, "-xen-domid");
   4.194 -    flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d", info->domid));
   4.195 +    flexarray_vappend(dm_args, libxl__strdup(gc, info->device_model),
   4.196 +                        "-xen-domid", libxl__sprintf(gc, "%d", info->domid), NULL);
   4.197  
   4.198      if (info->type == XENPV) {
   4.199 -        flexarray_set(dm_args, num++, "-xen-attach");
   4.200 +        flexarray_append(dm_args, "-xen-attach");
   4.201      }
   4.202  
   4.203      if (info->dom_name) {
   4.204 -        flexarray_set(dm_args, num++, "-name");
   4.205 -        flexarray_set(dm_args, num++, info->dom_name);
   4.206 +        flexarray_vappend(dm_args, "-name", info->dom_name, NULL);
   4.207      }
   4.208      if (info->vnc || info->vncdisplay || info->vnclisten || info->vncunused) {
   4.209          int display = 0;
   4.210          const char *listen = "127.0.0.1";
   4.211  
   4.212 -        flexarray_set(dm_args, num++, "-vnc");
   4.213 +        flexarray_append(dm_args, "-vnc");
   4.214  
   4.215          if (info->vncdisplay) {
   4.216              display = info->vncdisplay;
   4.217 @@ -211,66 +191,59 @@ static char ** libxl_build_device_model_
   4.218          }
   4.219  
   4.220          if (strchr(listen, ':') != NULL)
   4.221 -            flexarray_set(dm_args, num++,
   4.222 +            flexarray_append(dm_args, 
   4.223                      libxl__sprintf(gc, "%s%s", listen,
   4.224                          info->vncunused ? ",to=99" : ""));
   4.225          else
   4.226 -            flexarray_set(dm_args, num++,
   4.227 +            flexarray_append(dm_args, 
   4.228                      libxl__sprintf(gc, "%s:%d%s", listen, display,
   4.229                          info->vncunused ? ",to=99" : ""));
   4.230      }
   4.231      if (info->sdl) {
   4.232 -        flexarray_set(dm_args, num++, "-sdl");
   4.233 +        flexarray_append(dm_args, "-sdl");
   4.234      }
   4.235  
   4.236      if (info->type == XENPV && !info->nographic) {
   4.237 -        flexarray_set(dm_args, num++, "-vga");
   4.238 -        flexarray_set(dm_args, num++, "xenfb");
   4.239 +        flexarray_vappend(dm_args, "-vga", "xenfb", NULL);
   4.240      }
   4.241  
   4.242      if (info->keymap) {
   4.243 -        flexarray_set(dm_args, num++, "-k");
   4.244 -        flexarray_set(dm_args, num++, info->keymap);
   4.245 +        flexarray_vappend(dm_args, "-k", info->keymap, NULL);
   4.246      }
   4.247      if (info->nographic && (!info->sdl && !info->vnc)) {
   4.248 -        flexarray_set(dm_args, num++, "-nographic");
   4.249 +        flexarray_append(dm_args, "-nographic");
   4.250      }
   4.251      if (info->serial) {
   4.252 -        flexarray_set(dm_args, num++, "-serial");
   4.253 -        flexarray_set(dm_args, num++, info->serial);
   4.254 +        flexarray_vappend(dm_args, "-serial", info->serial, NULL);
   4.255      }
   4.256      if (info->type == XENFV) {
   4.257          int ioemu_vifs = 0;
   4.258  
   4.259          if (info->stdvga) {
   4.260 -                flexarray_set(dm_args, num++, "-vga");
   4.261 -                flexarray_set(dm_args, num++, "std");
   4.262 +                flexarray_vappend(dm_args, "-vga", "std", NULL);
   4.263          }
   4.264  
   4.265          if (info->boot) {
   4.266 -            flexarray_set(dm_args, num++, "-boot");
   4.267 -            flexarray_set(dm_args, num++, libxl__sprintf(gc, "order=%s", info->boot));
   4.268 +            flexarray_vappend(dm_args, "-boot", libxl__sprintf(gc, "order=%s", info->boot), NULL);
   4.269          }
   4.270          if (info->usb || info->usbdevice) {
   4.271 -            flexarray_set(dm_args, num++, "-usb");
   4.272 +            flexarray_append(dm_args, "-usb");
   4.273              if (info->usbdevice) {
   4.274 -                flexarray_set(dm_args, num++, "-usbdevice");
   4.275 -                flexarray_set(dm_args, num++, info->usbdevice);
   4.276 +                flexarray_vappend(dm_args, "-usbdevice", info->usbdevice, NULL);
   4.277              }
   4.278          }
   4.279          if (info->soundhw) {
   4.280 -            flexarray_set(dm_args, num++, "-soundhw");
   4.281 -            flexarray_set(dm_args, num++, info->soundhw);
   4.282 +            flexarray_vappend(dm_args, "-soundhw", info->soundhw, NULL);
   4.283          }
   4.284          if (!info->apic) {
   4.285 -            flexarray_set(dm_args, num++, "-no-acpi");
   4.286 +            flexarray_append(dm_args, "-no-acpi");
   4.287          }
   4.288          if (info->vcpus > 1) {
   4.289 -            flexarray_set(dm_args, num++, "-smp");
   4.290 +            flexarray_append(dm_args, "-smp");
   4.291              if (info->vcpu_avail)
   4.292 -                flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d,maxcpus=%d", info->vcpus, info->vcpu_avail));
   4.293 +                flexarray_append(dm_args, libxl__sprintf(gc, "%d,maxcpus=%d", info->vcpus, info->vcpu_avail));
   4.294              else
   4.295 -                flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d", info->vcpus));
   4.296 +                flexarray_append(dm_args, libxl__sprintf(gc, "%d", info->vcpus));
   4.297          }
   4.298          for (i = 0; i < num_vifs; i++) {
   4.299              if (vifs[i].nictype == NICTYPE_IOEMU) {
   4.300 @@ -283,52 +256,52 @@ static char ** libxl_build_device_model_
   4.301                  } else {
   4.302                      ifname = vifs[i].ifname;
   4.303                  }
   4.304 -                flexarray_set(dm_args, num++, "-net");
   4.305 -                flexarray_set(dm_args, num++, libxl__sprintf(gc, "nic,vlan=%d,macaddr=%s,model=%s",
   4.306 +                flexarray_append(dm_args, "-net");
   4.307 +                flexarray_append(dm_args, libxl__sprintf(gc, "nic,vlan=%d,macaddr=%s,model=%s",
   4.308                              vifs[i].devid, smac, vifs[i].model));
   4.309 -                flexarray_set(dm_args, num++, "-net");
   4.310 -                flexarray_set(dm_args, num++, libxl__sprintf(gc, "tap,vlan=%d,ifname=%s,script=no",
   4.311 +                flexarray_append(dm_args, "-net");
   4.312 +                flexarray_append(dm_args, libxl__sprintf(gc, "tap,vlan=%d,ifname=%s,script=no",
   4.313                              vifs[i].devid, ifname));
   4.314                  ioemu_vifs++;
   4.315              }
   4.316          }
   4.317          /* If we have no emulated nics, tell qemu not to create any */
   4.318          if ( ioemu_vifs == 0 ) {
   4.319 -            flexarray_set(dm_args, num++, "-net");
   4.320 -            flexarray_set(dm_args, num++, "none");
   4.321 +            flexarray_append(dm_args, "-net");
   4.322 +            flexarray_append(dm_args, "none");
   4.323          }
   4.324      }
   4.325      if (info->saved_state) {
   4.326 -        flexarray_set(dm_args, num++, "-loadvm");
   4.327 -        flexarray_set(dm_args, num++, info->saved_state);
   4.328 +        flexarray_append(dm_args, "-loadvm");
   4.329 +        flexarray_append(dm_args, info->saved_state);
   4.330      }
   4.331      for (i = 0; info->extra && info->extra[i] != NULL; i++)
   4.332 -        flexarray_set(dm_args, num++, info->extra[i]);
   4.333 -    flexarray_set(dm_args, num++, "-M");
   4.334 +        flexarray_append(dm_args, info->extra[i]);
   4.335 +    flexarray_append(dm_args, "-M");
   4.336      if (info->type == XENPV)
   4.337 -        flexarray_set(dm_args, num++, "xenpv");
   4.338 +        flexarray_append(dm_args, "xenpv");
   4.339      else
   4.340 -        flexarray_set(dm_args, num++, "xenfv");
   4.341 +        flexarray_append(dm_args, "xenfv");
   4.342  
   4.343      /* RAM Size */
   4.344 -    flexarray_set(dm_args, num++, "-m");
   4.345 -    flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d", info->target_ram));
   4.346 +    flexarray_append(dm_args, "-m");
   4.347 +    flexarray_append(dm_args, libxl__sprintf(gc, "%d", info->target_ram));
   4.348  
   4.349      if (info->type == XENFV) {
   4.350          disks = libxl_device_disk_list(libxl__gc_owner(gc), info->domid, &nb);
   4.351          for (i; i < nb; i++) {
   4.352              if (disks[i].is_cdrom) {
   4.353 -                flexarray_set(dm_args, num++, "-cdrom");
   4.354 -                flexarray_set(dm_args, num++, libxl__strdup(gc, disks[i].physpath));
   4.355 +                flexarray_append(dm_args, "-cdrom");
   4.356 +                flexarray_append(dm_args, libxl__strdup(gc, disks[i].physpath));
   4.357              } else {
   4.358 -                flexarray_set(dm_args, num++, libxl__sprintf(gc, "-%s", disks[i].virtpath));
   4.359 -                flexarray_set(dm_args, num++, libxl__strdup(gc, disks[i].physpath));
   4.360 +                flexarray_append(dm_args, libxl__sprintf(gc, "-%s", disks[i].virtpath));
   4.361 +                flexarray_append(dm_args, libxl__strdup(gc, disks[i].physpath));
   4.362              }
   4.363              libxl_device_disk_destroy(&disks[i]);
   4.364          }
   4.365          free(disks);
   4.366      }
   4.367 -    flexarray_set(dm_args, num++, NULL);
   4.368 +    flexarray_append(dm_args, NULL);
   4.369      return (char **) flexarray_contents(dm_args);
   4.370  }
   4.371  
     5.1 --- a/tools/libxl/libxl_pci.c	Fri Jan 07 18:24:54 2011 +0000
     5.2 +++ b/tools/libxl/libxl_pci.c	Fri Jan 07 18:53:50 2011 +0000
     5.3 @@ -226,8 +226,6 @@ static int libxl_create_pci_backend(libx
     5.4      libxl_ctx *ctx = libxl__gc_owner(gc);
     5.5      flexarray_t *front;
     5.6      flexarray_t *back;
     5.7 -    unsigned int boffset = 0;
     5.8 -    unsigned int foffset = 0;
     5.9      libxl__device device;
    5.10      int i;
    5.11  
    5.12 @@ -248,39 +246,28 @@ static int libxl_create_pci_backend(libx
    5.13      device.domid = domid;
    5.14      device.kind = DEVICE_PCI;
    5.15  
    5.16 -    flexarray_set(back, boffset++, "frontend-id");
    5.17 -    flexarray_set(back, boffset++, libxl__sprintf(gc, "%d", domid));
    5.18 -    flexarray_set(back, boffset++, "online");
    5.19 -    flexarray_set(back, boffset++, "1");
    5.20 -    flexarray_set(back, boffset++, "state");
    5.21 -    flexarray_set(back, boffset++, libxl__sprintf(gc, "%d", 1));
    5.22 -    flexarray_set(back, boffset++, "domain");
    5.23 -    flexarray_set(back, boffset++, libxl__domid_to_name(gc, domid));
    5.24 +    flexarray_vappend(back, "frontend-id", libxl__sprintf(gc, "%d", domid),
    5.25 +                     "online", "1", "state", libxl__sprintf(gc, "%d", 1),
    5.26 +                    "domain", libxl__domid_to_name(gc, domid), NULL);
    5.27      for (i = 0; i < num; i++) {
    5.28 -        flexarray_set(back, boffset++, libxl__sprintf(gc, "key-%d", i));
    5.29 -        flexarray_set(back, boffset++, libxl__sprintf(gc, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
    5.30 -        flexarray_set(back, boffset++, libxl__sprintf(gc, "dev-%d", i));
    5.31 -        flexarray_set(back, boffset++, libxl__sprintf(gc, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
    5.32 +        flexarray_append(back, libxl__sprintf(gc, "key-%d", i));
    5.33 +        flexarray_append(back, libxl__sprintf(gc, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
    5.34 +        flexarray_append(back, libxl__sprintf(gc, "dev-%d", i));
    5.35 +        flexarray_append(back, libxl__sprintf(gc, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
    5.36          if (pcidev->vdevfn) {
    5.37 -            flexarray_set(back, boffset++, libxl__sprintf(gc, "vdevfn-%d", i));
    5.38 -            flexarray_set(back, boffset++, libxl__sprintf(gc, "%x", pcidev->vdevfn));
    5.39 +            flexarray_vappend(back, libxl__sprintf(gc, "vdevfn-%d", i), libxl__sprintf(gc, "%x", pcidev->vdevfn), NULL);
    5.40          }
    5.41 -        flexarray_set(back, boffset++, libxl__sprintf(gc, "opts-%d", i));
    5.42 -        flexarray_set(back, boffset++, libxl__sprintf(gc, "msitranslate=%d,power_mgmt=%d", pcidev->msitranslate, pcidev->power_mgmt));
    5.43 -        flexarray_set(back, boffset++, libxl__sprintf(gc, "state-%d", i));
    5.44 -        flexarray_set(back, boffset++, libxl__sprintf(gc, "%d", 1));
    5.45 +        flexarray_append(back, libxl__sprintf(gc, "opts-%d", i));
    5.46 +        flexarray_append(back, libxl__sprintf(gc, "msitranslate=%d,power_mgmt=%d", pcidev->msitranslate, pcidev->power_mgmt));
    5.47 +        flexarray_vappend(back, libxl__sprintf(gc, "state-%d", i), libxl__sprintf(gc, "%d", 1), NULL);
    5.48      }
    5.49 -    flexarray_set(back, boffset++, "num_devs");
    5.50 -    flexarray_set(back, boffset++, libxl__sprintf(gc, "%d", num));
    5.51 -
    5.52 -    flexarray_set(front, foffset++, "backend-id");
    5.53 -    flexarray_set(front, foffset++, libxl__sprintf(gc, "%d", 0));
    5.54 -    flexarray_set(front, foffset++, "state");
    5.55 -    flexarray_set(front, foffset++, libxl__sprintf(gc, "%d", 1));
    5.56 +    flexarray_vappend(back, "num_devs", libxl__sprintf(gc, "%d", num),
    5.57 +                    "backend-id", libxl__sprintf(gc, "%d", 0),
    5.58 +                    "state", libxl__sprintf(gc, "%d", 1), NULL);
    5.59  
    5.60      libxl__device_generic_add(ctx, &device,
    5.61 -                             libxl__xs_kvs_of_flexarray(gc, back, boffset),
    5.62 -                             libxl__xs_kvs_of_flexarray(gc, front, foffset));
    5.63 +                             libxl__xs_kvs_of_flexarray(gc, back, back->count),
    5.64 +                             libxl__xs_kvs_of_flexarray(gc, front, front->count));
    5.65  
    5.66      flexarray_free(back);
    5.67      flexarray_free(front);
    5.68 @@ -293,7 +280,6 @@ static int libxl_device_pci_add_xenstore
    5.69      flexarray_t *back;
    5.70      char *num_devs, *be_path;
    5.71      int num = 0;
    5.72 -    unsigned int boffset = 0;
    5.73      xs_transaction_t t;
    5.74  
    5.75      be_path = libxl__sprintf(gc, "%s/backend/pci/%d/0", libxl__xs_get_dompath(gc, 0), domid);
    5.76 @@ -312,27 +298,24 @@ static int libxl_device_pci_add_xenstore
    5.77  
    5.78      LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, "Adding new pci device to xenstore");
    5.79      num = atoi(num_devs);
    5.80 -    flexarray_set(back, boffset++, libxl__sprintf(gc, "key-%d", num));
    5.81 -    flexarray_set(back, boffset++, libxl__sprintf(gc, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
    5.82 -    flexarray_set(back, boffset++, libxl__sprintf(gc, "dev-%d", num));
    5.83 -    flexarray_set(back, boffset++, libxl__sprintf(gc, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
    5.84 +    flexarray_append(back, libxl__sprintf(gc, "key-%d", num));
    5.85 +    flexarray_append(back, libxl__sprintf(gc, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
    5.86 +    flexarray_append(back, libxl__sprintf(gc, "dev-%d", num));
    5.87 +    flexarray_append(back, libxl__sprintf(gc, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
    5.88      if (pcidev->vdevfn) {
    5.89 -        flexarray_set(back, boffset++, libxl__sprintf(gc, "vdevfn-%d", num));
    5.90 -        flexarray_set(back, boffset++, libxl__sprintf(gc, "%x", pcidev->vdevfn));
    5.91 +        flexarray_append(back, libxl__sprintf(gc, "vdevfn-%d", num));
    5.92 +        flexarray_append(back, libxl__sprintf(gc, "%x", pcidev->vdevfn));
    5.93      }
    5.94 -    flexarray_set(back, boffset++, libxl__sprintf(gc, "opts-%d", num));
    5.95 -    flexarray_set(back, boffset++, libxl__sprintf(gc, "msitranslate=%d,power_mgmt=%d", pcidev->msitranslate, pcidev->power_mgmt));
    5.96 -    flexarray_set(back, boffset++, libxl__sprintf(gc, "state-%d", num));
    5.97 -    flexarray_set(back, boffset++, libxl__sprintf(gc, "%d", 1));
    5.98 -    flexarray_set(back, boffset++, "num_devs");
    5.99 -    flexarray_set(back, boffset++, libxl__sprintf(gc, "%d", num + 1));
   5.100 -    flexarray_set(back, boffset++, "state");
   5.101 -    flexarray_set(back, boffset++, libxl__sprintf(gc, "%d", 7));
   5.102 +    flexarray_append(back, libxl__sprintf(gc, "opts-%d", num));
   5.103 +    flexarray_append(back, libxl__sprintf(gc, "msitranslate=%d,power_mgmt=%d", pcidev->msitranslate, pcidev->power_mgmt));
   5.104 +    flexarray_vappend(back, libxl__sprintf(gc, "state-%d", num), libxl__sprintf(gc, "%d", 1), NULL);
   5.105 +    flexarray_vappend(back, "num_devs", libxl__sprintf(gc, "%d", num + 1), NULL);
   5.106 +    flexarray_vappend(back, "state", libxl__sprintf(gc, "%d", 7), NULL);
   5.107  
   5.108  retry_transaction:
   5.109      t = xs_transaction_start(ctx->xsh);
   5.110      libxl__xs_writev(gc, t, be_path,
   5.111 -                    libxl__xs_kvs_of_flexarray(gc, back, boffset));
   5.112 +                    libxl__xs_kvs_of_flexarray(gc, back, back->count));
   5.113      if (!xs_transaction_end(ctx->xsh, t, 0))
   5.114          if (errno == EAGAIN)
   5.115              goto retry_transaction;