From mboxrd@z Thu Jan 1 00:00:00 1970 From: Rob Hoes Subject: [PATCH 17/28] libxl: ocaml: add with_ctx helper function Date: Mon, 25 Mar 2013 14:45:18 +0000 Message-ID: <1364222729-6982-18-git-send-email-rob.hoes@citrix.com> References: <1364222729-6982-1-git-send-email-rob.hoes@citrix.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <1364222729-6982-1-git-send-email-rob.hoes@citrix.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel@lists.xen.org Cc: ian.campbell@citrix.com, Rob Hoes List-Id: xen-devel@lists.xenproject.org Signed-off-by: Rob Hoes --- 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