aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorIan Campbell <ian.campbell@citrix.com>2012-03-01 12:26:14 +0000
committerIan Campbell <ian.campbell@citrix.com>2012-03-01 12:26:14 +0000
commit50cbda0d0b1bffa4a7af91b5ff8a65d88191fdbb (patch)
treefcc97d3b3fbf51bdccae615127ae6c94e4ff6a0c /tools
parent966deb4db61ea44e2487c84daa0939f52416fa83 (diff)
downloadxen-50cbda0d0b1bffa4a7af91b5ff8a65d88191fdbb.tar.gz
xen-50cbda0d0b1bffa4a7af91b5ff8a65d88191fdbb.tar.bz2
xen-50cbda0d0b1bffa4a7af91b5ff8a65d88191fdbb.zip
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@citrix.com> Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
Diffstat (limited to 'tools')
-rw-r--r--tools/libxl/gentest.py5
-rw-r--r--tools/libxl/libxl.c41
-rw-r--r--tools/libxl/libxl.h24
-rw-r--r--tools/libxl/libxl_json.c6
-rw-r--r--tools/libxl/libxl_types.idl2
-rw-r--r--tools/libxl/libxlu_cfg.c11
-rw-r--r--tools/libxl/libxlutil.h2
-rw-r--r--tools/ocaml/libs/xl/genwrap.py1
-rw-r--r--tools/ocaml/libs/xl/xenlight_stubs.c27
-rw-r--r--tools/python/genwrap.py12
-rw-r--r--tools/python/xen/lowlevel/xl/xl.c15
11 files changed, 144 insertions, 2 deletions
diff --git a/tools/libxl/gentest.py b/tools/libxl/gentest.py
index f8dc43b88d..903070393a 100644
--- a/tools/libxl/gentest.py
+++ b/tools/libxl/gentest.py
@@ -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 = " ", parent = None):
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 --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c
index ae57703261..3d58c65807 100644
--- a/tools/libxl/libxl.c
+++ b/tools/libxl/libxl.c
@@ -184,6 +184,47 @@ void libxl_key_value_list_dispose(libxl_key_value_list *pkvl)
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 --git a/tools/libxl/libxl.h b/tools/libxl/libxl.h
index 564ad67e4d..94e079b939 100644
--- a/tools/libxl/libxl.h
+++ b/tools/libxl/libxl.h
@@ -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 --git a/tools/libxl/libxl_json.c b/tools/libxl/libxl_json.c
index 70909fbd0f..7c068d3e81 100644
--- a/tools/libxl/libxl_json.c
+++ b/tools/libxl/libxl_json.c
@@ -85,6 +85,12 @@ yajl_gen_status libxl__yajl_gen_enum(yajl_gen hand, const char *str)
/*
* 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 --git a/tools/libxl/libxl_types.idl b/tools/libxl/libxl_types.idl
index b8a39efd95..95c7d52dda 100644
--- a/tools/libxl/libxl_types.idl
+++ b/tools/libxl/libxl_types.idl
@@ -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 --git a/tools/libxl/libxlu_cfg.c b/tools/libxl/libxlu_cfg.c
index e3659c7654..c32cf046c1 100644
--- a/tools/libxl/libxlu_cfg.c
+++ b/tools/libxl/libxlu_cfg.c
@@ -237,6 +237,17 @@ int xlu_cfg_get_long(const XLU_Config *cfg, const char *n,
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 --git a/tools/libxl/libxlutil.h b/tools/libxl/libxlutil.h
index edb3264ce6..620b9dbb47 100644
--- a/tools/libxl/libxlutil.h
+++ b/tools/libxl/libxlutil.h
@@ -52,6 +52,8 @@ int xlu_cfg_replace_string(const XLU_Config *cfg, const char *n,
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 --git a/tools/ocaml/libs/xl/genwrap.py b/tools/ocaml/libs/xl/genwrap.py
index 22cca80bcb..384ec5b4ea 100644
--- a/tools/ocaml/libs/xl/genwrap.py
+++ b/tools/ocaml/libs/xl/genwrap.py
@@ -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 --git a/tools/ocaml/libs/xl/xenlight_stubs.c b/tools/ocaml/libs/xl/xenlight_stubs.c
index f1511d46dd..2ba07fc579 100644
--- a/tools/ocaml/libs/xl/xenlight_stubs.c
+++ b/tools/ocaml/libs/xl/xenlight_stubs.c
@@ -195,6 +195,33 @@ static int Uuid_val(caml_gc *gc, struct caml_logger *lg, libxl_uuid *c_val, valu
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 --git a/tools/python/genwrap.py b/tools/python/genwrap.py
index aa5c723c3d..af8a5e9c46 100644
--- a/tools/python/genwrap.py
+++ b/tools/python/genwrap.py
@@ -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(unsigned long long val);
_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 --git a/tools/python/xen/lowlevel/xl/xl.c b/tools/python/xen/lowlevel/xl/xl.c
index 48a828645a..74c8c887e5 100644
--- a/tools/python/xen/lowlevel/xl/xl.c
+++ b/tools/python/xen/lowlevel/xl/xl.c
@@ -156,6 +156,21 @@ int genwrap__ll_set(PyObject *v, long long *val, long long mask)
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;