|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Xen-devel] [PATCH 16 of 27 V4] libxl: add new "defbool" built in type
# HG changeset patch
# User Ian Campbell <ian.campbell@xxxxxxxxxx>
# Date 1330533036 0
# Node ID 023e6c3f206511f46ef9582aa245b92bf27ed390
# Parent 0b79a94f4cab781fea6692414a8d7f8734309bfc
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 0b79a94f4cab -r 023e6c3f2065 tools/libxl/gentest.py
--- a/tools/libxl/gentest.py Wed Feb 29 16:30:36 2012 +0000
+++ b/tools/libxl/gentest.py Wed Feb 29 16:30:36 2012 +0000
@@ -20,7 +20,8 @@ def randomize_case(s):
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 @@ def gen_rand_init(ty, v, indent = " "
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 0b79a94f4cab -r 023e6c3f2065 tools/libxl/libxl.c
--- a/tools/libxl/libxl.c Wed Feb 29 16:30:36 2012 +0000
+++ b/tools/libxl/libxl.c Wed Feb 29 16:30:36 2012 +0000
@@ -184,6 +184,47 @@ void libxl_key_value_list_dispose(libxl_
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 0b79a94f4cab -r 023e6c3f2065 tools/libxl/libxl.h
--- a/tools/libxl/libxl.h Wed Feb 29 16:30:36 2012 +0000
+++ b/tools/libxl/libxl.h Wed Feb 29 16:30:36 2012 +0000
@@ -243,6 +243,30 @@ typedef struct {
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 0b79a94f4cab -r 023e6c3f2065 tools/libxl/libxl_json.c
--- a/tools/libxl/libxl_json.c Wed Feb 29 16:30:36 2012 +0000
+++ b/tools/libxl/libxl_json.c Wed Feb 29 16:30:36 2012 +0000
@@ -85,6 +85,12 @@ yajl_gen_status libxl__yajl_gen_enum(yaj
/*
* 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 0b79a94f4cab -r 023e6c3f2065 tools/libxl/libxl_types.idl
--- a/tools/libxl/libxl_types.idl Wed Feb 29 16:30:36 2012 +0000
+++ b/tools/libxl/libxl_types.idl Wed Feb 29 16:30:36 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 0b79a94f4cab -r 023e6c3f2065 tools/libxl/libxlu_cfg.c
--- a/tools/libxl/libxlu_cfg.c Wed Feb 29 16:30:36 2012 +0000
+++ b/tools/libxl/libxlu_cfg.c Wed Feb 29 16:30:36 2012 +0000
@@ -237,6 +237,17 @@ int xlu_cfg_get_long(const XLU_Config *c
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 0b79a94f4cab -r 023e6c3f2065 tools/libxl/libxlutil.h
--- a/tools/libxl/libxlutil.h Wed Feb 29 16:30:36 2012 +0000
+++ b/tools/libxl/libxlutil.h Wed Feb 29 16:30:36 2012 +0000
@@ -52,6 +52,8 @@ int xlu_cfg_replace_string(const XLU_Con
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 0b79a94f4cab -r 023e6c3f2065 tools/ocaml/libs/xl/genwrap.py
--- a/tools/ocaml/libs/xl/genwrap.py Wed Feb 29 16:30:36 2012 +0000
+++ b/tools/ocaml/libs/xl/genwrap.py Wed Feb 29 16:30:36 2012 +0000
@@ -10,6 +10,7 @@ builtins = {
"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 0b79a94f4cab -r 023e6c3f2065 tools/ocaml/libs/xl/xenlight_stubs.c
--- a/tools/ocaml/libs/xl/xenlight_stubs.c Wed Feb 29 16:30:36 2012 +0000
+++ b/tools/ocaml/libs/xl/xenlight_stubs.c Wed Feb 29 16:30:36 2012 +0000
@@ -195,6 +195,33 @@ static int Uuid_val(caml_gc *gc, struct
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 0b79a94f4cab -r 023e6c3f2065 tools/python/genwrap.py
--- a/tools/python/genwrap.py Wed Feb 29 16:30:36 2012 +0000
+++ b/tools/python/genwrap.py Wed Feb 29 16:30:36 2012 +0000
@@ -4,11 +4,13 @@ import sys,os
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 @@ def py_decls(ty):
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 @@ def py_attrib_get(ty, f):
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 @@ def py_attrib_set(ty, f):
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 PyObject *genwrap__ull_get(unsig
_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 0b79a94f4cab -r 023e6c3f2065 tools/python/xen/lowlevel/xl/xl.c
--- a/tools/python/xen/lowlevel/xl/xl.c Wed Feb 29 16:30:36 2012 +0000
+++ b/tools/python/xen/lowlevel/xl/xl.c Wed Feb 29 16:30:36 2012 +0000
@@ -156,6 +156,21 @@ int genwrap__ll_set(PyObject *v, long lo
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-devel mailing list
Xen-devel@xxxxxxxxxxxxx
http://lists.xen.org/xen-devel
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |