qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [6594] qemu: dynamic drive/drive_opt index allocation (Marcelo Tosatti)
@ 2009-02-11 15:19 Anthony Liguori
  0 siblings, 0 replies; only message in thread
From: Anthony Liguori @ 2009-02-11 15:19 UTC (permalink / raw)
  To: qemu-devel

Revision: 6594
          http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=6594
Author:   aliguori
Date:     2009-02-11 15:19:58 +0000 (Wed, 11 Feb 2009)

Log Message:
-----------
qemu: dynamic drive/drive_opt index allocation (Marcelo Tosatti)

Dynamically allocate drive options and drive table index, to reuse
indexes when devices are removed.

Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>

Modified Paths:
--------------
    trunk/sysemu.h
    trunk/vl.c

Modified: trunk/sysemu.h
===================================================================
--- trunk/sysemu.h	2009-02-11 15:19:52 UTC (rev 6593)
+++ trunk/sysemu.h	2009-02-11 15:19:58 UTC (rev 6594)
@@ -134,6 +134,7 @@
     BlockInterfaceType type;
     int bus;
     int unit;
+    int used;
     BlockInterfaceErrorAction onerror;
     char serial[21];
 } DriveInfo;

Modified: trunk/vl.c
===================================================================
--- trunk/vl.c	2009-02-11 15:19:52 UTC (rev 6593)
+++ trunk/vl.c	2009-02-11 15:19:58 UTC (rev 6594)
@@ -246,6 +246,7 @@
 static struct drive_opt {
     const char *file;
     char opt[1024];
+    int used;
 } drives_opt[MAX_DRIVES];
 
 static CPUState *cur_cpu;
@@ -2135,22 +2136,50 @@
 #define MTD_ALIAS "if=mtd"
 #define SD_ALIAS "index=0,if=sd"
 
+static int drive_opt_get_free_idx(void)
+{
+    int index;
+
+    for (index = 0; index < MAX_DRIVES; index++)
+        if (!drives_opt[index].used) {
+            drives_opt[index].used = 1;
+            return index;
+        }
+
+    return -1;
+}
+
+static int drive_get_free_idx(void)
+{
+    int index;
+
+    for (index = 0; index < MAX_DRIVES; index++)
+        if (!drives_table[index].used) {
+            drives_table[index].used = 1;
+            return index;
+        }
+
+    return -1;
+}
+
 static int drive_add(const char *file, const char *fmt, ...)
 {
     va_list ap;
+    int index = drive_opt_get_free_idx();
 
-    if (nb_drives_opt >= MAX_DRIVES) {
+    if (nb_drives_opt >= MAX_DRIVES || index == -1) {
         fprintf(stderr, "qemu: too many drives\n");
         exit(1);
     }
 
-    drives_opt[nb_drives_opt].file = file;
+    drives_opt[index].file = file;
     va_start(ap, fmt);
-    vsnprintf(drives_opt[nb_drives_opt].opt,
+    vsnprintf(drives_opt[index].opt,
               sizeof(drives_opt[0].opt), fmt, ap);
     va_end(ap);
 
-    return nb_drives_opt++;
+    nb_drives_opt++;
+    return index;
 }
 
 int drive_get_index(BlockInterfaceType type, int bus, int unit)
@@ -2159,10 +2188,11 @@
 
     /* seek interface, bus and unit */
 
-    for (index = 0; index < nb_drives; index++)
+    for (index = 0; index < MAX_DRIVES; index++)
         if (drives_table[index].type == type &&
 	    drives_table[index].bus == bus &&
-	    drives_table[index].unit == unit)
+	    drives_table[index].unit == unit &&
+	    drives_table[index].used)
         return index;
 
     return -1;
@@ -2227,6 +2257,7 @@
     int index;
     int cache;
     int bdrv_flags, onerror;
+    int drives_table_idx;
     char *str = arg->opt;
     static const char * const params[] = { "bus", "unit", "if", "index",
                                            "cyls", "heads", "secs", "trans",
@@ -2501,11 +2532,12 @@
         snprintf(buf, sizeof(buf), "%s%s%i",
                  devname, mediastr, unit_id);
     bdrv = bdrv_new(buf);
-    drives_table[nb_drives].bdrv = bdrv;
-    drives_table[nb_drives].type = type;
-    drives_table[nb_drives].bus = bus_id;
-    drives_table[nb_drives].unit = unit_id;
-    drives_table[nb_drives].onerror = onerror;
+    drives_table_idx = drive_get_free_idx();
+    drives_table[drives_table_idx].bdrv = bdrv;
+    drives_table[drives_table_idx].type = type;
+    drives_table[drives_table_idx].bus = bus_id;
+    drives_table[drives_table_idx].unit = unit_id;
+    drives_table[drives_table_idx].onerror = onerror;
     strncpy(drives_table[nb_drives].serial, serial, sizeof(serial));
     nb_drives++;
 

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2009-02-11 15:20 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-02-11 15:19 [Qemu-devel] [6594] qemu: dynamic drive/drive_opt index allocation (Marcelo Tosatti) Anthony Liguori

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).