Optimize cmp x, 0 case
[qemu] / block-vpc.c
index 3eea506..71a171d 100644 (file)
@@ -196,8 +196,6 @@ static int vpc_open(BlockDriverState *bs, const char *filename, int flags)
 
     s->max_table_entries = be32_to_cpu(dyndisk_header->max_table_entries);
     s->pagetable = qemu_malloc(s->max_table_entries * 4);
-    if (!s->pagetable)
-        goto fail;
 
     s->bat_offset = be64_to_cpu(dyndisk_header->table_offset);
     if (bdrv_pread(s->hd, s->bat_offset, s->pagetable,
@@ -222,8 +220,6 @@ static int vpc_open(BlockDriverState *bs, const char *filename, int flags)
 
 #ifdef CACHE
     s->pageentry_u8 = qemu_malloc(512);
-    if (!s->pageentry_u8)
-       goto fail;
     s->pageentry_u32 = s->pageentry_u8;
     s->pageentry_u16 = s->pageentry_u8;
     s->last_pagetable = -1;
@@ -437,14 +433,16 @@ static int vpc_write(BlockDriverState *bs, int64_t sector_num,
  *
  * Note that the geometry doesn't always exactly match total_sectors but
  * may round it down.
+ *
+ * Returns 0 on success, -EFBIG if the size is larger than 127 GB
  */
-static void calculate_geometry(int64_t total_sectors, uint16_t* cyls,
+static int calculate_geometry(int64_t total_sectors, uint16_t* cyls,
     uint8_t* heads, uint8_t* secs_per_cyl)
 {
     uint32_t cyls_times_heads;
 
     if (total_sectors > 65535 * 16 * 255)
-        total_sectors = 65535 * 16 * 255;
+        return -EFBIG;
 
     if (total_sectors > 65535 * 16 * 63) {
         *secs_per_cyl = 255;
@@ -474,6 +472,8 @@ static void calculate_geometry(int64_t total_sectors, uint16_t* cyls,
     // Note: Rounding up deviates from the Virtual PC behaviour
     // However, we need this to avoid truncating images in qemu-img convert
     *cyls = (cyls_times_heads + *heads - 1) / *heads;
+
+    return 0;
 }
 
 static int vpc_create(const char *filename, int64_t total_sectors,
@@ -497,7 +497,8 @@ static int vpc_create(const char *filename, int64_t total_sectors,
         return -EIO;
 
     // Calculate matching total_size and geometry
-    calculate_geometry(total_sectors, &cyls, &heads, &secs_per_cyl);
+    if (calculate_geometry(total_sectors, &cyls, &heads, &secs_per_cyl))
+        return -EFBIG;
     total_sectors = (int64_t) cyls * heads * secs_per_cyl;
 
     // Prepare the Hard Disk Footer
@@ -586,12 +587,12 @@ static void vpc_close(BlockDriverState *bs)
 }
 
 BlockDriver bdrv_vpc = {
-    "vpc",
-    sizeof(BDRVVPCState),
-    vpc_probe,
-    vpc_open,
-    vpc_read,
-    vpc_write,
-    vpc_close,
-    vpc_create,
+    .format_name       = "vpc",
+    .instance_size     = sizeof(BDRVVPCState),
+    .bdrv_probe                = vpc_probe,
+    .bdrv_open         = vpc_open,
+    .bdrv_read         = vpc_read,
+    .bdrv_write                = vpc_write,
+    .bdrv_close                = vpc_close,
+    .bdrv_create       = vpc_create,
 };