libxc: refactor memory allocation functions
authorWei Liu <wei.liu2@citrix.com>
Tue, 1 Dec 2015 11:39:16 +0000 (11:39 +0000)
committerIan Campbell <ian.campbell@citrix.com>
Tue, 1 Dec 2015 12:13:33 +0000 (12:13 +0000)
There were some problems with the original memory allocation functions:
1. xc_dom_alloc_segment and xc_dom_alloc_pad ended up calling
   xc_dom_chk_alloc_pages while xc_dom_alloc_page open-coded everything.
2. xc_dom_alloc_pad didn't call dom->allocate.

Refactor the code so that:
1. xc_dom_alloc_{segment,pad,page} end up calling
   xc_dom_chk_alloc_pages.
2. xc_dom_chk_alloc_pages calls dom->allocate.

This way we avoid scattering dom->allocate over multiple locations and
open-coding.

Also change the return type of xc_dom_alloc_page to xen_pfn_t and return
an invalid pfn when xc_dom_chk_alloc_pages fails.

Signed-off-by: Wei Liu <wei.liu2@citrix.com>
Reviewed-by: Juergen Gross <jgross@suse.com>
Acked-by: Ian Campbell <ian.campbell@citrix.com>
Tested-by: Ian Campbell <ian.campbell@citrix.com>
tools/libxc/include/xc_dom.h
tools/libxc/xc_dom_core.c

index fd8c5e89323a6140db6c22b3e319abd8c7cbddb6..43a65ee85645c1edc84fc21b15ebf01846542512 100644 (file)
@@ -355,7 +355,7 @@ char *xc_dom_strdup(struct xc_dom_image *dom, const char *str);
 
 /* --- alloc memory pool ------------------------------------------- */
 
-int xc_dom_alloc_page(struct xc_dom_image *dom, char *name);
+xen_pfn_t xc_dom_alloc_page(struct xc_dom_image *dom, char *name);
 int xc_dom_alloc_segment(struct xc_dom_image *dom,
                          struct xc_dom_seg *seg, char *name,
                          xen_vaddr_t start, xen_vaddr_t size);
index 5d6c3ba352c0dec8705c0cfe884ac3728268c23c..89679706806ac0be293e01c41bdab691454b4041 100644 (file)
@@ -555,6 +555,9 @@ static int xc_dom_chk_alloc_pages(struct xc_dom_image *dom, char *name,
     dom->pfn_alloc_end += pages;
     dom->virt_alloc_end += pages * page_size;
 
+    if ( dom->allocate )
+        dom->allocate(dom);
+
     return 0;
 }
 
@@ -602,9 +605,6 @@ int xc_dom_alloc_segment(struct xc_dom_image *dom,
     if ( xc_dom_chk_alloc_pages(dom, name, pages) )
         return -1;
 
-    if (dom->allocate)
-        dom->allocate(dom);
-
     /* map and clear pages */
     ptr = xc_dom_seg_to_ptr(dom, seg);
     if ( ptr == NULL )
@@ -621,18 +621,16 @@ int xc_dom_alloc_segment(struct xc_dom_image *dom,
     return 0;
 }
 
-int xc_dom_alloc_page(struct xc_dom_image *dom, char *name)
+xen_pfn_t xc_dom_alloc_page(struct xc_dom_image *dom, char *name)
 {
-    unsigned int page_size = XC_DOM_PAGE_SIZE(dom);
     xen_vaddr_t start;
     xen_pfn_t pfn;
 
     start = dom->virt_alloc_end;
     pfn = dom->pfn_alloc_end - dom->rambase_pfn;
-    dom->virt_alloc_end += page_size;
-    dom->pfn_alloc_end++;
-    if ( dom->allocate )
-        dom->allocate(dom);
+
+    if ( xc_dom_chk_alloc_pages(dom, name, 1) )
+        return (xen_pfn_t)-1;
 
     DOMPRINTF("%-20s:   %-12s : 0x%" PRIx64 " (pfn 0x%" PRIpfn ")",
               __FUNCTION__, name, start, pfn);