]> git.proxmox.com Git - grub2.git/commitdiff
* grub-core/kern/partition.c (grub_partition_get_name): Simplify logic
authorVladimir 'phcoder' Serbinenko <phcoder@gmail.com>
Sun, 29 Jan 2012 15:24:07 +0000 (16:24 +0100)
committerVladimir 'phcoder' Serbinenko <phcoder@gmail.com>
Sun, 29 Jan 2012 15:24:07 +0000 (16:24 +0100)
and improve performance.

ChangeLog
grub-core/kern/partition.c

index 09512ea9efba2d0e2e49b572cd226945a7e3b708..2af861197669b76aed1b4f63ef46bc0cd434c4ac 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2012-01-29  Vladimir Serbinenko  <phcoder@gmail.com>
+
+       * grub-core/kern/partition.c (grub_partition_get_name): Simplify logic
+       and improve performance.
+
 2012-01-29  Vladimir Serbinenko  <phcoder@gmail.com>
 
        * grub-core/kern/ieee1275/openfw.c (grub_ieee1275_encode_devname): Fix
index cd9e8ae0f6dcd669c6e01b8b0e83a2a9755921ef..a083f5cbbb8991e239f7c64627706d63523d5f27 100644 (file)
@@ -223,32 +223,23 @@ grub_partition_iterate (struct grub_disk *disk,
 char *
 grub_partition_get_name (const grub_partition_t partition)
 {
-  char *out = 0;
-  int curlen = 0;
+  char *out = 0, *ptr;
+  grub_size_t needlen = 0;
   grub_partition_t part;
+  for (part = partition; part; part = part->parent)
+    /* Even on 64-bit machines this buffer is enough to hold
+       longest number.  */
+    needlen += grub_strlen (part->partmap->name) + 27;
+  out = grub_malloc (needlen);
+  if (!out)
+    return NULL;
+
+  ptr = out;
   for (part = partition; part; part = part->parent)
     {
-      /* Even on 64-bit machines this buffer is enough to hold
-        longest number.  */
-      char buf[grub_strlen (part->partmap->name) + 25];
-      int strl;
-      grub_snprintf (buf, sizeof (buf), "%s%d", part->partmap->name,
+      grub_snprintf (ptr, needlen - (out - ptr), "%s%d", part->partmap->name,
                     part->number + 1);
-      strl = grub_strlen (buf);
-      if (curlen)
-       {
-         out = grub_realloc (out, curlen + strl + 2);
-         grub_memcpy (out + strl + 1, out, curlen);
-         out[curlen + 1 + strl] = 0;
-         grub_memcpy (out, buf, strl);
-         out[strl] = ',';
-         curlen = curlen + 1 + strl;
-       }
-      else
-       {
-         curlen = strl;
-         out = grub_strdup (buf);
-       }
+      ptr += grub_strlen (ptr);
     }
   return out;
 }