diff mbox

[3/4] file_ram_alloc(): extract temporary-file creation code to separate function

Message ID 1340743869-12293-4-git-send-email-ehabkost@redhat.com
State New
Headers show

Commit Message

Eduardo Habkost June 26, 2012, 8:51 p.m. UTC
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 exec.c |   35 +++++++++++++++++++++++++----------
 1 file changed, 25 insertions(+), 10 deletions(-)

Comments

Blue Swirl June 27, 2012, 5:27 p.m. UTC | #1
On Tue, Jun 26, 2012 at 8:51 PM, Eduardo Habkost <ehabkost@redhat.com> wrote:
> Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
> ---
>  exec.c |   35 +++++++++++++++++++++++++----------
>  1 file changed, 25 insertions(+), 10 deletions(-)
>
> diff --git a/exec.c b/exec.c
> index 5f3b265..dcbe4e1 100644
> --- a/exec.c
> +++ b/exec.c
> @@ -2353,11 +2353,34 @@ static long gethugepagesize(const char *path)
>     return fs.f_bsize;
>  }
>
> +/* Return FD to temporary file inside directory at 'path',
> + * truncated to size 'length'
> + */
> +static int get_temp_fd(const char *path)
> +{
> +    int fd;
> +    char *filename;
> +
> +    if (asprintf(&filename, "%s/qemu_back_mem.XXXXXX", path) == -1) {
> +        return -1;
> +    }
> +
> +    fd = mkstemp(filename);
> +    if (fd < 0) {
> +        perror("unable to create backing store for hugepages");
> +        free(filename);
> +        return -1;
> +    }
> +    unlink(filename);
> +    free(filename);
> +
> +    return fd;

See my comments against 4/4.

> +}
> +
>  static void *file_ram_alloc(RAMBlock *block,
>                             size_t length,
>                             const char *path)
>  {
> -    char *filename;
>     void *area;
>     int fd;
>  #ifdef MAP_POPULATE
> @@ -2379,18 +2402,10 @@ static void *file_ram_alloc(RAMBlock *block,
>         return NULL;
>     }
>
> -    if (asprintf(&filename, "%s/qemu_back_mem.XXXXXX", path) == -1) {
> -        return NULL;
> -    }
> -
> -    fd = mkstemp(filename);
> +    fd = get_temp_fd(path);
>     if (fd < 0) {
> -        perror("unable to create backing store for hugepages");
> -        free(filename);
>         return NULL;
>     }
> -    unlink(filename);
> -    free(filename);
>
>     length = (length+hpagesize-1) & ~(hpagesize-1);
>
> --
> 1.7.10.4
>
>
diff mbox

Patch

diff --git a/exec.c b/exec.c
index 5f3b265..dcbe4e1 100644
--- a/exec.c
+++ b/exec.c
@@ -2353,11 +2353,34 @@  static long gethugepagesize(const char *path)
     return fs.f_bsize;
 }
 
+/* Return FD to temporary file inside directory at 'path',
+ * truncated to size 'length'
+ */
+static int get_temp_fd(const char *path)
+{
+    int fd;
+    char *filename;
+
+    if (asprintf(&filename, "%s/qemu_back_mem.XXXXXX", path) == -1) {
+        return -1;
+    }
+
+    fd = mkstemp(filename);
+    if (fd < 0) {
+        perror("unable to create backing store for hugepages");
+        free(filename);
+        return -1;
+    }   
+    unlink(filename);
+    free(filename);
+
+    return fd;
+}
+
 static void *file_ram_alloc(RAMBlock *block,
                             size_t length,
                             const char *path)
 {
-    char *filename;
     void *area;
     int fd;
 #ifdef MAP_POPULATE
@@ -2379,18 +2402,10 @@  static void *file_ram_alloc(RAMBlock *block,
         return NULL;
     }
 
-    if (asprintf(&filename, "%s/qemu_back_mem.XXXXXX", path) == -1) {
-        return NULL;
-    }
-
-    fd = mkstemp(filename);
+    fd = get_temp_fd(path);
     if (fd < 0) {
-        perror("unable to create backing store for hugepages");
-        free(filename);
         return NULL;
     }
-    unlink(filename);
-    free(filename);
 
     length = (length+hpagesize-1) & ~(hpagesize-1);