[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Xen-changelog] [xen-unstable] libxl: add new "defbool" built in type.
# HG changeset patch # User Ian Campbell <ian.campbell@xxxxxxxxxx> # Date 1330604774 0 # Node ID 2aae04f3240cfc43ab31073ac6f5a4f2a702b80c # Parent a207b589a972a9168ef53d24035ce48b6adffa49 libxl: add new "defbool" built in type. This type is a but like a "boolean" but with a third state "default" (so really I suppose it's a tristate). Signed-off-by: Ian Campbell <ian.campbell@xxxxxxxxxx> Acked-by: Ian Jackson <ian.jackson@xxxxxxxxxxxxx> --- diff -r a207b589a972 -r 2aae04f3240c tools/libxl/gentest.py --- a/tools/libxl/gentest.py Thu Mar 01 12:26:14 2012 +0000 +++ b/tools/libxl/gentest.py Thu Mar 01 12:26:14 2012 +0000 @@ -20,7 +20,8 @@ def randomize_enum(e): return random.choice([v.name for v in e.values]) -handcoded = ["libxl_cpumap", "libxl_key_value_list", +handcoded = ["libxl_defbool", # Temp until a user appears in the next patch + "libxl_cpumap", "libxl_key_value_list", "libxl_cpuid_policy_list", "libxl_file_reference", "libxl_string_list"] @@ -55,6 +56,8 @@ ty.pass_arg(v, parent is None)) elif ty.typename in ["bool"]: s += "%s = rand() %% 2;\n" % v + elif ty.typename in ["libxl_defbool"]: + s += "libxl_defbool_set(%s, !!rand() %% 1);\n" % v elif ty.typename in ["char *"]: s += "%s = rand_str();\n" % v elif ty.private: diff -r a207b589a972 -r 2aae04f3240c tools/libxl/libxl.c --- a/tools/libxl/libxl.c Thu Mar 01 12:26:14 2012 +0000 +++ b/tools/libxl/libxl.c Thu Mar 01 12:26:14 2012 +0000 @@ -184,6 +184,47 @@ free(kvl); } +#define LIBXL__DEFBOOL_DEFAULT (0) +#define LIBXL__DEFBOOL_FALSE (-1) +#define LIBXL__DEFBOOL_TRUE (1) + +void libxl_defbool_set(libxl_defbool *db, bool b) +{ + db->val = b ? LIBXL__DEFBOOL_TRUE : LIBXL__DEFBOOL_FALSE; +} + +void libxl_defbool_unset(libxl_defbool *db) +{ + db->val = LIBXL__DEFBOOL_DEFAULT; +} + +bool libxl_defbool_is_default(libxl_defbool db) +{ + return !db.val; +} + +void libxl_defbool_setdefault(libxl_defbool *db, bool b) +{ + if (libxl_defbool_is_default(*db)) + libxl_defbool_set(db, b); +} + +bool libxl_defbool_val(libxl_defbool db) +{ + assert(!libxl_defbool_is_default(db)); + return db.val > 0; +} + +const char *libxl_defbool_to_string(libxl_defbool b) +{ + if (b.val < 0) + return "False"; + else if (b.val > 0) + return "True"; + else + return "<default>"; +} + /******************************************************************************/ diff -r a207b589a972 -r 2aae04f3240c tools/libxl/libxl.h --- a/tools/libxl/libxl.h Thu Mar 01 12:26:14 2012 +0000 +++ b/tools/libxl/libxl.h Thu Mar 01 12:26:14 2012 +0000 @@ -243,6 +243,30 @@ struct libxl_event; typedef LIBXL_TAILQ_ENTRY(struct libxl_event) libxl_ev_link; +/* + * A boolean variable with an explicit default state. + * + * Users should treat this struct as opaque and use the following + * defined macros and accessor functions. + * + * To allow users of the library to naively select all defaults this + * state is represented as 0. False is < 0 and True is > 0. + */ +typedef struct { + int val; +} libxl_defbool; + +void libxl_defbool_set(libxl_defbool *db, bool b); +/* Resets to default */ +void libxl_defbool_unset(libxl_defbool *db); +/* Sets db only if it is currently == default */ +void libxl_defbool_setdefault(libxl_defbool *db, bool b); +bool libxl_defbool_is_default(libxl_defbool db); +/* db must not be == default */ +bool libxl_defbool_val(libxl_defbool db); + +const char *libxl_defbool_to_string(libxl_defbool b); + typedef struct libxl__ctx libxl_ctx; #define LIBXL_TIMER_MODE_DEFAULT -1 diff -r a207b589a972 -r 2aae04f3240c tools/libxl/libxl_json.c --- a/tools/libxl/libxl_json.c Thu Mar 01 12:26:14 2012 +0000 +++ b/tools/libxl/libxl_json.c Thu Mar 01 12:26:14 2012 +0000 @@ -85,6 +85,12 @@ /* * YAJL generators for builtin libxl types. */ +yajl_gen_status libxl_defbool_gen_json(yajl_gen hand, + libxl_defbool *db) +{ + return libxl__yajl_gen_asciiz(hand, libxl_defbool_to_string(*db)); +} + yajl_gen_status libxl_uuid_gen_json(yajl_gen hand, libxl_uuid *uuid) { diff -r a207b589a972 -r 2aae04f3240c tools/libxl/libxl_types.idl --- a/tools/libxl/libxl_types.idl Thu Mar 01 12:26:14 2012 +0000 +++ b/tools/libxl/libxl_types.idl Thu Mar 01 12:26:14 2012 +0000 @@ -5,6 +5,8 @@ namespace("libxl_") +libxl_defbool = Builtin("defbool", passby=PASS_BY_REFERENCE) + libxl_domid = Builtin("domid", json_fn = "yajl_gen_integer", autogenerate_json = False) libxl_uuid = Builtin("uuid", passby=PASS_BY_REFERENCE) libxl_mac = Builtin("mac", passby=PASS_BY_REFERENCE) diff -r a207b589a972 -r 2aae04f3240c tools/libxl/libxlu_cfg.c --- a/tools/libxl/libxlu_cfg.c Thu Mar 01 12:26:14 2012 +0000 +++ b/tools/libxl/libxlu_cfg.c Thu Mar 01 12:26:14 2012 +0000 @@ -237,6 +237,17 @@ return 0; } +int xlu_cfg_get_defbool(const XLU_Config *cfg, const char *n, libxl_defbool *b, + int dont_warn) +{ + int ret; + long l; + + ret = xlu_cfg_get_long(cfg, n, &l, dont_warn); + if (ret) return ret; + libxl_defbool_set(b, !!l); + return 0; +} int xlu_cfg_get_list(const XLU_Config *cfg, const char *n, XLU_ConfigList **list_r, int *entries_r, int dont_warn) { diff -r a207b589a972 -r 2aae04f3240c tools/libxl/libxlutil.h --- a/tools/libxl/libxlutil.h Thu Mar 01 12:26:14 2012 +0000 +++ b/tools/libxl/libxlutil.h Thu Mar 01 12:26:14 2012 +0000 @@ -52,6 +52,8 @@ char **value_r, int dont_warn); int xlu_cfg_get_long(const XLU_Config*, const char *n, long *value_r, int dont_warn); +int xlu_cfg_get_defbool(const XLU_Config*, const char *n, libxl_defbool *b, + int dont_warn); int xlu_cfg_get_list(const XLU_Config*, const char *n, XLU_ConfigList **list_r /* may be 0 */, diff -r a207b589a972 -r 2aae04f3240c tools/ocaml/libs/xl/genwrap.py --- a/tools/ocaml/libs/xl/genwrap.py Thu Mar 01 12:26:14 2012 +0000 +++ b/tools/ocaml/libs/xl/genwrap.py Thu Mar 01 12:26:14 2012 +0000 @@ -10,6 +10,7 @@ "int": ("int", "%(c)s = Int_val(%(o)s)", "Val_int(%(c)s)" ), "char *": ("string", "%(c)s = dup_String_val(gc, %(o)s)", "caml_copy_string(%(c)s)"), "libxl_domid": ("domid", "%(c)s = Int_val(%(o)s)", "Val_int(%(c)s)" ), + "libxl_defbool": ("bool option", "%(c)s = Defbool_val(%(o)s)", "Val_defbool(%(c)s)" ), "libxl_uuid": ("int array", "Uuid_val(gc, lg, &%(c)s, %(o)s)", "Val_uuid(&%(c)s)"), "libxl_key_value_list": ("(string * string) list", None, None), "libxl_mac": ("int array", "Mac_val(gc, lg, &%(c)s, %(o)s)", "Val_mac(&%(c)s)"), diff -r a207b589a972 -r 2aae04f3240c tools/ocaml/libs/xl/xenlight_stubs.c --- a/tools/ocaml/libs/xl/xenlight_stubs.c Thu Mar 01 12:26:14 2012 +0000 +++ b/tools/ocaml/libs/xl/xenlight_stubs.c Thu Mar 01 12:26:14 2012 +0000 @@ -195,6 +195,33 @@ CAMLreturn(0); } +static value Val_defbool(libxl_defbool c_val) +{ + CAMLparam0(); + CAMLlocal1(v); + + if (libxl_defbool_is_default(c_val)) + v = Val_none; + else { + bool b = libxl_defbool_val(c_val); + v = Val_some(b ? Val_bool(true) : Val_bool(false)); + } + CAMLreturn(v); +} + +static libxl_defbool Defbool_val(value v) +{ + CAMLparam1(v); + libxl_defbool db; + if (v == Val_none) + libxl_defbool_unset(&db); + else { + bool b = Bool_val(Some_val(v)); + libxl_defbool_set(&db, b); + } + return db; +} + static value Val_hwcap(libxl_hwcap *c_val) { CAMLparam0(); diff -r a207b589a972 -r 2aae04f3240c tools/python/genwrap.py --- a/tools/python/genwrap.py Thu Mar 01 12:26:14 2012 +0000 +++ b/tools/python/genwrap.py Thu Mar 01 12:26:14 2012 +0000 @@ -4,11 +4,13 @@ import idl -(TYPE_BOOL, TYPE_INT, TYPE_UINT, TYPE_STRING, TYPE_AGGREGATE) = range(5) +(TYPE_DEFBOOL, TYPE_BOOL, TYPE_INT, TYPE_UINT, TYPE_STRING, TYPE_AGGREGATE) = range(6) def py_type(ty): if ty == idl.bool: return TYPE_BOOL + if ty.typename == "libxl_defbool": + return TYPE_DEFBOOL if isinstance(ty, idl.Enumeration): return TYPE_UINT if isinstance(ty, idl.Number): @@ -44,6 +46,8 @@ for f in ty.fields: if py_type(f.type) is not None: continue + if py_type(f.type) == TYPE_DEFBOOL: + continue if ty.marshal_out(): l.append('_hidden PyObject *attrib__%s_get(%s *%s);'%(\ fsanitize(f.type.typename), f.type.typename, f.name)) @@ -62,6 +66,8 @@ l.append(' ret = (self->obj.%s) ? Py_True : Py_False;'%f.name) l.append(' Py_INCREF(ret);') l.append(' return ret;') + elif t == TYPE_DEFBOOL: + l.append(' return genwrap__defbool_get(&self->obj.%s);'%f.name) elif t == TYPE_INT: l.append(' return genwrap__ll_get(self->obj.%s);'%f.name) elif t == TYPE_UINT: @@ -85,6 +91,8 @@ if t == TYPE_BOOL: l.append(' self->obj.%s = (NULL == v || Py_None == v || Py_False == v) ? 0 : 1;'%f.name) l.append(' return 0;') + elif t == TYPE_DEFBOOL: + l.append(' return genwrap__defbool_set(v, &self->obj.%s);'%f.name) elif t == TYPE_UINT or t == TYPE_INT: l.append(' %slong long tmp;'%(t == TYPE_UINT and 'unsigned ' or '')) l.append(' int ret;') @@ -275,6 +283,8 @@ _hidden int genwrap__ull_set(PyObject *v, unsigned long long *val, unsigned long long mask); _hidden PyObject *genwrap__ll_get(long long val); _hidden int genwrap__ll_set(PyObject *v, long long *val, long long mask); +_hidden PyObject *genwrap__defbool_get(libxl_defbool *db); +_hidden int genwrap__defbool_set(PyObject *v, libxl_defbool *db); """ % " ".join(sys.argv)) for ty in [ty for ty in types if isinstance(ty, idl.Aggregate)]: diff -r a207b589a972 -r 2aae04f3240c tools/python/xen/lowlevel/xl/xl.c --- a/tools/python/xen/lowlevel/xl/xl.c Thu Mar 01 12:26:14 2012 +0000 +++ b/tools/python/xen/lowlevel/xl/xl.c Thu Mar 01 12:26:14 2012 +0000 @@ -156,6 +156,21 @@ return 0; } +PyObject *genwrap__defbool_get(libxl_defbool *db) +{ + PyObject *ret; + ret = libxl_defbool_val(*db) ? Py_True : Py_False; + Py_INCREF(ret); + return ret; +} + +int genwrap__defbool_set(PyObject *v, libxl_defbool *db) +{ + bool val = !(NULL == v || Py_None == v || Py_False == v); + libxl_defbool_set(db, val); + return 0; +} + static int fixed_bytearray_set(PyObject *v, uint8_t *ptr, size_t len) { char *tmp; _______________________________________________ Xen-changelog mailing list Xen-changelog@xxxxxxxxxxxxx http://lists.xensource.com/xen-changelog
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |