From mboxrd@z Thu Jan 1 00:00:00 1970 From: Andrew Cooper Subject: Re: [PATCH v2 18/30] libxl: ocaml: use the "string option" type for IDL strings Date: Thu, 13 Jun 2013 16:58:59 +0100 Message-ID: <51B9EC43.2030000@citrix.com> References: <1371137112-16692-1-git-send-email-rob.hoes@citrix.com> <1371137112-16692-19-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: <1371137112-16692-19-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: Rob Hoes Cc: "xen-devel@lists.xen.org" List-Id: xen-devel@lists.xenproject.org On 13/06/13 16:25, Rob Hoes wrote: > The libxl IDL is based on C type "char *", and therefore "strings" can > by NULL, or be an actual string. In ocaml, it is common to encode such "can be NULL" perhaps? ~Andrew > things as option types. > > Signed-off-by: Rob Hoes > --- > tools/ocaml/libs/xl/genwrap.py | 2 +- > tools/ocaml/libs/xl/xenlight_stubs.c | 21 +++++++++++++++++++++ > 2 files changed, 22 insertions(+), 1 deletion(-) > > diff --git a/tools/ocaml/libs/xl/genwrap.py b/tools/ocaml/libs/xl/genwrap.py > index f0d4885..d967ee6 100644 > --- a/tools/ocaml/libs/xl/genwrap.py > +++ b/tools/ocaml/libs/xl/genwrap.py > @@ -8,7 +8,7 @@ import idl > builtins = { > "bool": ("bool", "%(c)s = Bool_val(%(o)s)", "Val_bool(%(c)s)" ), > "int": ("int", "%(c)s = Int_val(%(o)s)", "Val_int(%(c)s)" ), > - "char *": ("string", "%(c)s = dup_String_val(%(o)s)", "caml_copy_string(%(c)s)"), > + "char *": ("string option", "%(c)s = String_option_val(%(o)s)", "Val_string_option(%(c)s)"), > "libxl_domid": ("domid", "%(c)s = Int_val(%(o)s)", "Val_int(%(c)s)" ), > "libxl_devid": ("devid", "%(c)s = Int_val(%(o)s)", "Val_int(%(c)s)" ), > "libxl_defbool": ("bool option", "%(c)s = Defbool_val(%(o)s)", "Val_defbool(%(c)s)" ), > diff --git a/tools/ocaml/libs/xl/xenlight_stubs.c b/tools/ocaml/libs/xl/xenlight_stubs.c > index 7b7d696..e801643 100644 > --- a/tools/ocaml/libs/xl/xenlight_stubs.c > +++ b/tools/ocaml/libs/xl/xenlight_stubs.c > @@ -371,6 +371,27 @@ static value Val_hwcap(libxl_hwcap *c_val) > CAMLreturn(hwcap); > } > > +static value Val_string_option(const char *c_val) > +{ > + CAMLparam0(); > + CAMLlocal2(tmp1, tmp2); > + if (c_val) { > + tmp1 = caml_copy_string(c_val); > + tmp2 = Val_some(tmp1); > + CAMLreturn(tmp2); > + } > + else > + CAMLreturn(Val_none); > +} > + > +static char *String_option_val(value v) > +{ > + char *s = NULL; > + if (v != Val_none) > + s = dup_String_val(Some_val(v)); > + return s; > +} > + > #include "_libxl_types.inc" > > #define _STRINGIFY(x) #x