This looks to be a reasonably common case (many of the previous
callers to xc_map_foreign_pages use it) and it is easy enough to avoid
a malloc for it.

Signed-off-by: Ian Campbell <ian.campb...@citrix.com>
---
v6: New

I'm not 100% sure this is worth the complexity, hence not folding
into the base patch yet, it could easily be dropped.
---
 tools/libs/foreignmemory/core.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/tools/libs/foreignmemory/core.c b/tools/libs/foreignmemory/core.c
index 738bb45..26c131b 100644
--- a/tools/libs/foreignmemory/core.c
+++ b/tools/libs/foreignmemory/core.c
@@ -14,6 +14,7 @@
  */
 
 #include <stdlib.h>
+#include <stdbool.h>
 #include <assert.h>
 #include <errno.h>
 
@@ -68,14 +69,26 @@ void *xenforeignmemory_map(xenforeignmemory_handle *fmem,
                            const xen_pfn_t arr[/*num*/], int err[/*num*/])
 {
     void *ret;
-    int *err_to_free = NULL;
+    bool check_errs = false;
+    int single_err, *err_to_free = NULL;
 
     if ( err == NULL )
-        err = err_to_free = malloc(num * sizeof(int));
+    {
+        /* singleton case is common enough to be worth short circuiting */
+        if ( num == 1 )
+            err = &single_err;
+        else
+            err = err_to_free = malloc(num * sizeof(int));
+
+        if ( !err )
+            return NULL;
+
+        check_errs = true;
+    }
 
     ret = osdep_xenforeignmemory_map(fmem, dom, prot, num, arr, err);
 
-    if ( ret == 0 && err_to_free )
+    if ( ret == 0 && check_errs )
     {
         int i;
 
-- 
2.1.4


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

Reply via email to