[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH 17/28] libxl: ocaml: add with_ctx helper function



Signed-off-by: Rob Hoes <rob.hoes@xxxxxxxxxx>
---
 tools/ocaml/libs/xl/xenlight.ml.in  |   15 +++++++++++++++
 tools/ocaml/libs/xl/xenlight.mli.in |    4 +++-
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/tools/ocaml/libs/xl/xenlight.ml.in 
b/tools/ocaml/libs/xl/xenlight.ml.in
index 3e83355..991b2bf 100644
--- a/tools/ocaml/libs/xl/xenlight.ml.in
+++ b/tools/ocaml/libs/xl/xenlight.ml.in
@@ -53,6 +53,21 @@ type ctx
 external ctx_alloc: Xentoollog.handle -> ctx = "stub_libxl_ctx_alloc"
 external ctx_free: ctx -> unit = "stub_libxl_ctx_free"
 
+let with_ctx ?logger f =
+       let logger' = match logger with
+               | None -> Xentoollog.create_stdio_logger 
(*~level:Xentoollog.Debug*) ()
+               | Some l -> l
+       in
+       let ctx = ctx_alloc logger' in
+       let res = try f ctx with exn ->
+               ctx_free ctx;
+               if logger = None then Xentoollog.destroy logger';
+               raise exn
+       in
+       ctx_free ctx;
+       if logger = None then Xentoollog.destroy logger';
+       res
+
 external test_raise_exception: unit -> unit = "stub_raise_exception"
 
 type domid = int
diff --git a/tools/ocaml/libs/xl/xenlight.mli.in 
b/tools/ocaml/libs/xl/xenlight.mli.in
index e562c4b..12568ca 100644
--- a/tools/ocaml/libs/xl/xenlight.mli.in
+++ b/tools/ocaml/libs/xl/xenlight.mli.in
@@ -38,7 +38,9 @@ type ctx
 external ctx_alloc: Xentoollog.handle -> ctx = "stub_libxl_ctx_alloc"
 external ctx_free: ctx -> unit = "stub_libxl_ctx_free"
 
-external test_raise_exception: unit = "stub_raise_exception"
+val with_ctx : ?logger:Xentoollog.handle -> (ctx -> 'a) -> 'a
+
+external test_raise_exception: unit -> unit = "stub_raise_exception"
 
 type domid = int
 type devid = int
-- 
1.7.10.4


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


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.