All of lore.kernel.org
 help / color / mirror / Atom feed
From: Wei Liu <wei.liu2@citrix.com>
To: xen-devel@lists.xen.org
Cc: Wei Liu <wei.liu2@citrix.com>,
	ian.jackson@eu.citrix.com, ian.campbell@citrix.com
Subject: [PATCH V4 17/24] libxl IDL: generate deep copy functions
Date: Thu, 1 May 2014 13:58:14 +0100	[thread overview]
Message-ID: <1398949101-23320-18-git-send-email-wei.liu2@citrix.com> (raw)
In-Reply-To: <1398949101-23320-1-git-send-email-wei.liu2@citrix.com>

Introduces copy_fn for a type.

For most builtin types we can use direct assignment. For those builtin
types which cannot use direct assignment we use the copy functions in
previous patch.

Signed-off-by: Wei Liu <wei.liu2@citrix.com>
---
 tools/libxl/gentypes.py              |   67 ++++++++++++++++++++++++++++++++++
 tools/libxl/idl.py                   |   13 ++++++-
 tools/libxl/idl.txt                  |   12 ++++++
 tools/libxl/libxl_types.idl          |   26 ++++++++-----
 tools/libxl/libxl_types_internal.idl |    2 +-
 5 files changed, 108 insertions(+), 12 deletions(-)

diff --git a/tools/libxl/gentypes.py b/tools/libxl/gentypes.py
index 3e1140d..919d2d3 100644
--- a/tools/libxl/gentypes.py
+++ b/tools/libxl/gentypes.py
@@ -100,6 +100,61 @@ def libxl_C_type_dispose(ty, v, indent = "    ", parent = None):
         s = indent + s
     return s.replace("\n", "\n%s" % indent).rstrip(indent)
 
+def libxl_C_type_copy(ty, v, w, indent = "    ", vparent = None, wparent = None):
+    s = ""
+
+    if vparent is None:
+        s += "GC_INIT(ctx);\n";
+
+    if isinstance(ty, idl.KeyedUnion):
+        if vparent is None or wparent is None:
+            raise Exception("KeyedUnion type must have a parent")
+        s += "%s = %s;\n" % ((vparent + ty.keyvar.name), (wparent + ty.keyvar.name))
+        s += "switch (%s) {\n" % (wparent + ty.keyvar.name)
+        for f in ty.fields:
+            (vnparent,vfexpr) = ty.member(v, f, vparent is None)
+            (wnparent,wfexpr) = ty.member(w, f, wparent is None)
+            s += "case %s:\n" % f.enumname
+            if f.type is not None:
+                s += libxl_C_type_copy(f.type, vfexpr, wfexpr, indent + "    ",
+                                       vnparent, wnparent)
+            s += "    break;\n"
+        s += "}\n"
+    elif isinstance(ty, idl.Array):
+        if vparent is None or wparent is None:
+            raise Exception("Array type must have a parent")
+        s += "%s = libxl__calloc(NOGC, %s, sizeof(*%s));\n" % (ty.pass_arg(v, vparent is None),
+                                                               (wparent + ty.lenvar.name),
+                                                               ty.pass_arg(w, wparent is None))
+        s += "%s = %s;\n" % ((vparent + ty.lenvar.name), (wparent + ty.lenvar.name))
+        s += "{\n"
+        s += "    int i;\n"
+        s += "    for (i=0; i<%s; i++)\n" % (wparent + ty.lenvar.name)
+        s += libxl_C_type_copy(ty.elem_type, v+"[i]", w+"[i]",
+                               indent + "        ", vparent, wparent)
+        s += "}\n"
+    elif isinstance(ty, idl.Struct) and ((vparent is None and wparent is None) or ty.copy_fn is None):
+        for f in [f for f in ty.fields if not f.const and not f.type.private]:
+            (vnparent,vfexpr) = ty.member(v, f, vparent is None)
+            (wnparent,wfexpr) = ty.member(w, f, wparent is None)
+            s += libxl_C_type_copy(f.type, vfexpr, wfexpr, "", vnparent, wnparent)
+    else:
+        if ty.copy_fn is not None:
+            s += "%s(ctx, %s, %s);\n" % (ty.copy_fn,
+                                         ty.pass_arg(v, vparent is None, passby=idl.PASS_BY_REFERENCE),
+                                         ty.pass_arg(w, wparent is None, passby=idl.PASS_BY_REFERENCE))
+
+        else:
+            s += "%s = %s;\n" % (ty.pass_arg(v, vparent is None, passby=idl.PASS_BY_VALUE),
+                                 ty.pass_arg(w, wparent is None, passby=idl.PASS_BY_VALUE))
+
+    if vparent is None:
+        s += "GC_FREE;\n"
+
+    if s != "":
+        s = indent + s
+    return s.replace("\n", "\n%s" % indent).rstrip(indent)
+
 def libxl_init_members(ty, nesting = 0):
     """Returns a list of members of ty which require a separate init"""
 
@@ -430,6 +485,9 @@ if __name__ == '__main__':
         f.write(libxl_C_type_define(ty) + ";\n")
         if ty.dispose_fn is not None:
             f.write("%svoid %s(%s);\n" % (ty.hidden(), ty.dispose_fn, ty.make_arg("p")))
+        if ty.copy_fn is not None:
+            f.write("%svoid %s(libxl_ctx *ctx, %s, %s);\n" % (ty.hidden(), ty.copy_fn,
+                                              ty.make_arg("dst"), ty.make_arg("src")))
         if ty.init_fn is not None:
             f.write("%svoid %s(%s);\n" % (ty.hidden(), ty.init_fn, ty.make_arg("p")))
             for field in libxl_init_members(ty):
@@ -509,6 +567,15 @@ if __name__ == '__main__':
         f.write("    memset(p, LIBXL_DTOR_POISON, sizeof(*p));\n")
         f.write("}\n")
         f.write("\n")
+
+    for ty in [t for t in types if t.copy_fn and t.autogenerate_copy_fn]:
+        f.write("void %s(libxl_ctx *ctx, %s, %s)\n" % (ty.copy_fn,
+                                       ty.make_arg("dst", passby=idl.PASS_BY_REFERENCE),
+                                       ty.make_arg("src", passby=idl.PASS_BY_REFERENCE)))
+        f.write("{\n")
+        f.write(libxl_C_type_copy(ty, "dst", "src"))
+        f.write("}\n")
+        f.write("\n")
         
     for ty in [t for t in types if t.init_fn is not None and t.autogenerate_init_fn]:
         f.write(libxl_C_type_init(ty))
diff --git a/tools/libxl/idl.py b/tools/libxl/idl.py
index 9ce1e33..3498af1 100644
--- a/tools/libxl/idl.py
+++ b/tools/libxl/idl.py
@@ -60,6 +60,13 @@ class Type(object):
 
         self.autogenerate_dispose_fn = kwargs.setdefault('autogenerate_dispose_fn', True)
 
+        if self.typename is not None:
+            self.copy_fn = kwargs.setdefault('copy_fn', self.typename + "_copy")
+        else:
+            self.copy_fn = kwargs.setdefault('copy_fn', None)
+
+        self.autogenerate_copy_fn = kwargs.setdefault('autogenerate_copy_fn', True)
+
         self.init_fn = kwargs.setdefault('init_fn', None)
         self.init_val = kwargs.setdefault('init_val', None)
         self.autogenerate_init_fn = kwargs.setdefault('autogenerate_init_fn', False)
@@ -121,6 +128,7 @@ class Number(Builtin):
     def __init__(self, ctype, **kwargs):
         kwargs.setdefault('namespace', None)
         kwargs.setdefault('dispose_fn', None)
+        kwargs.setdefault('copy_fn', None)
         kwargs.setdefault('signed', False)
         kwargs.setdefault('json_gen_fn', "yajl_gen_integer")
         kwargs.setdefault('json_parse_type', "JSON_INTEGER")
@@ -134,6 +142,7 @@ class UInt(Number):
         kwargs.setdefault('namespace', None)
         kwargs.setdefault('dispose_fn', None)
         kwargs.setdefault('json_parse_fn', "libxl__uint%d_parse_json" % w)
+        kwargs.setdefault('copy_fn', None)
         Number.__init__(self, "uint%d_t" % w, **kwargs)
 
         self.width = w
@@ -150,6 +159,7 @@ class EnumerationValue(object):
 class Enumeration(Type):
     def __init__(self, typename, values, **kwargs):
         kwargs.setdefault('dispose_fn', None)
+        kwargs.setdefault('copy_fn', None)
         kwargs.setdefault('json_parse_type', "JSON_STRING")
         Type.__init__(self, typename, **kwargs)
 
@@ -266,6 +276,7 @@ class KeyedUnion(Aggregate):
 
 void = Builtin("void *", namespace = None)
 bool = Builtin("bool", namespace = None,
+               copy_fn=None,
                json_gen_fn = "yajl_gen_bool",
                json_parse_type = "JSON_BOOL",
                json_parse_fn = "libxl__bool_parse_json",
@@ -280,7 +291,7 @@ uint16 = UInt(16)
 uint32 = UInt(32)
 uint64 = UInt(64)
 
-string = Builtin("char *", namespace = None, dispose_fn = "free",
+string = Builtin("char *", namespace = None, copy_fn = "libxl_string_copy", dispose_fn = "free",
                  json_gen_fn = "libxl__string_gen_json",
                  json_parse_type = "JSON_STRING | JSON_NULL",
                  json_parse_fn = "libxl__string_parse_json",
diff --git a/tools/libxl/idl.txt b/tools/libxl/idl.txt
index 484d5d7..8b54aeb 100644
--- a/tools/libxl/idl.txt
+++ b/tools/libxl/idl.txt
@@ -44,6 +44,18 @@ Type.autogenerate_dispose_fn: (default: True)
  Indicates if the above named Type.dispose_fn should be
  autogenerated.
 
+Type.copy_fn: (default: typename + "_copy" or None if type == None)
+
+ The name of the C function which will deep copy all fields within
+ this type.
+
+Type.autogenerate_copy_fn: (default: True)
+
+ Indicates if the above named Type.copy_fn should be
+ autogenerated.
+
+Type.autogenerate_copy_fn
+
 Type.init_val: (default: None)
 
  C expression for the value to initialise instances of this type to.
diff --git a/tools/libxl/libxl_types.idl b/tools/libxl/libxl_types.idl
index 6cf99fd..1bd4185 100644
--- a/tools/libxl/libxl_types.idl
+++ b/tools/libxl/libxl_types.idl
@@ -5,23 +5,29 @@
 
 namespace("libxl_")
 
-libxl_defbool = Builtin("defbool", json_parse_type="JSON_STRING", passby=PASS_BY_REFERENCE)
+libxl_defbool = Builtin("defbool", json_parse_type="JSON_STRING", passby=PASS_BY_REFERENCE, copy_fn=None)
 
 libxl_domid = Builtin("domid", json_gen_fn = "yajl_gen_integer", json_parse_fn = "libxl__uint32_parse_json",
-                      json_parse_type = "JSON_INTEGER", autogenerate_json = False)
+                      json_parse_type = "JSON_INTEGER", autogenerate_json = False, copy_fn=None)
 libxl_devid = Builtin("devid", json_gen_fn = "yajl_gen_integer", json_parse_fn = "libxl__int_parse_json",
-                      json_parse_type = "JSON_INTEGER", autogenerate_json = False, signed = True, init_val="-1")
-libxl_uuid = Builtin("uuid", json_parse_type="JSON_STRING", passby=PASS_BY_REFERENCE)
-libxl_mac = Builtin("mac", json_parse_type="JSON_STRING", passby=PASS_BY_REFERENCE)
-libxl_bitmap = Builtin("bitmap", json_parse_type="JSON_ARRAY", dispose_fn="libxl_bitmap_dispose", passby=PASS_BY_REFERENCE)
+                      json_parse_type = "JSON_INTEGER", autogenerate_json = False, signed = True, init_val="-1",
+                      copy_fn=None)
+libxl_uuid = Builtin("uuid", json_parse_type="JSON_STRING", passby=PASS_BY_REFERENCE, copy_fn="libxl_uuid_copy_ctx")
+libxl_mac = Builtin("mac", json_parse_type="JSON_STRING", passby=PASS_BY_REFERENCE, copy_fn="libxl_mac_copy")
+libxl_bitmap = Builtin("bitmap", json_parse_type="JSON_ARRAY", dispose_fn="libxl_bitmap_dispose",
+                       copy_fn='libxl_bitmap_copy_alloc', passby=PASS_BY_REFERENCE)
 libxl_cpuid_policy_list = Builtin("cpuid_policy_list", json_parse_type="JSON_ARRAY",
-                                  dispose_fn="libxl_cpuid_dispose", passby=PASS_BY_REFERENCE)
+                                  dispose_fn="libxl_cpuid_dispose", passby=PASS_BY_REFERENCE,
+                                  copy_fn="libxl_cpuid_policy_list_copy")
 
 libxl_string_list = Builtin("string_list", json_parse_type="JSON_ARRAY",
-                            dispose_fn="libxl_string_list_dispose", passby=PASS_BY_REFERENCE)
+                            dispose_fn="libxl_string_list_dispose", passby=PASS_BY_REFERENCE,
+                            copy_fn="libxl_string_list_copy")
 libxl_key_value_list = Builtin("key_value_list", json_parse_type="JSON_MAP",
-                               dispose_fn="libxl_key_value_list_dispose", passby=PASS_BY_REFERENCE)
-libxl_hwcap = Builtin("hwcap", json_parse_type="JSON_ARRAY", passby=PASS_BY_REFERENCE)
+                               dispose_fn="libxl_key_value_list_dispose", passby=PASS_BY_REFERENCE,
+                               copy_fn="libxl_key_value_list_copy")
+libxl_hwcap = Builtin("hwcap", json_parse_type="JSON_ARRAY", passby=PASS_BY_REFERENCE,
+                      copy_fn="libxl_hwcap_copy")
 
 #
 # Specific integer types
diff --git a/tools/libxl/libxl_types_internal.idl b/tools/libxl/libxl_types_internal.idl
index 17533f1..800361b 100644
--- a/tools/libxl/libxl_types_internal.idl
+++ b/tools/libxl/libxl_types_internal.idl
@@ -3,7 +3,7 @@ hidden(True)
 
 libxl_domid = Builtin("domid", namespace="libxl_", json_gen_fn = "yajl_gen_integer",
 		      json_parse_fn = "libxl__uint32_parse_json", json_parse_type = "JSON_INTEGER",
-		      autogenerate_json = False)
+		      autogenerate_json = False, copy_fn = None)
 
 libxl__qmp_message_type = Enumeration("qmp_message_type", [
     (1, "QMP"),
-- 
1.7.10.4

  parent reply	other threads:[~2014-05-01 12:58 UTC|newest]

Thread overview: 120+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-01 12:57 [PATCH V4 00/24] JSON infrastructure, new "xl-json" format and domain configuration synchronization Wei Liu
2014-05-01 12:57 ` [PATCH V4 01/24] xl / libxl: push parsing of SSID and CPU pool ID down to libxl Wei Liu
2014-05-06 12:45   ` Ian Campbell
2014-05-06 13:03     ` Wei Liu
2014-05-07  9:50       ` Ian Campbell
2014-05-07 10:30         ` Wei Liu
2014-05-01 12:57 ` [PATCH V4 02/24] libxl: fix memory leak in libxl_cpuid_dispose Wei Liu
2014-05-06 12:47   ` Ian Campbell
2014-05-01 12:58 ` [PATCH V4 03/24] libxl.h: document the paradigm of using libxl types Wei Liu
2014-05-06 12:49   ` Ian Campbell
2014-05-06 13:12     ` Ian Jackson
2014-05-06 13:49       ` Wei Liu
2014-05-07  9:53       ` Ian Campbell
2014-05-01 12:58 ` [PATCH V4 04/24] libxl.h: move / add some libxl defbool #define here Wei Liu
2014-05-06 12:50   ` Ian Campbell
2014-05-01 12:58 ` [PATCH V4 05/24] libxl IDL: rename json_fn to json_gen_fn Wei Liu
2014-05-01 12:58 ` [PATCH V4 06/24] libxl_json: introduce libx__object_from_json Wei Liu
2014-05-06 12:53   ` Ian Campbell
2014-05-01 12:58 ` [PATCH V4 07/24] libxl_internal: make JSON_* types a bit-field Wei Liu
2014-05-01 12:58 ` [PATCH V4 08/24] libxl_internal: introduce libxl__json_object_is_{null, number, double} Wei Liu
2014-05-01 12:58 ` [PATCH V4 09/24] libxl_json: introduce parser functions for builtin types Wei Liu
2014-05-06 12:57   ` Ian Campbell
2014-05-06 13:06     ` Wei Liu
2014-05-07  9:57       ` Ian Campbell
2014-05-06 13:57     ` Anthony PERARD
2014-05-06 14:04       ` Wei Liu
2014-05-06 14:35         ` Anthony PERARD
2014-05-06 14:45           ` Wei Liu
2014-05-06 14:49             ` Wei Liu
2014-05-06 15:14               ` Anthony PERARD
2014-05-06 15:42                 ` Wei Liu
2014-05-06 15:59                   ` Anthony PERARD
2014-05-06 16:13                     ` Wei Liu
2014-05-12 16:43                   ` Wei Liu
2014-05-13  8:26                     ` Ian Campbell
2014-05-13  9:19                       ` Wei Liu
2014-05-13  9:32                         ` Ian Campbell
2014-05-13 10:40                           ` Wei Liu
2014-05-13 10:45                             ` Ian Campbell
2014-05-13 10:51                               ` Wei Liu
2014-05-13 10:55                                 ` Ian Campbell
2014-05-13 11:03                                   ` Wei Liu
2014-05-13 11:13                                     ` Ian Campbell
2014-05-13 11:38                                       ` Wei Liu
2014-05-13 11:28                                     ` Anthony PERARD
2014-05-13 11:40                                       ` Wei Liu
2014-05-13 11:54                                         ` Ian Campbell
2014-05-13 11:58                                           ` Wei Liu
2014-05-01 12:58 ` [PATCH V4 10/24] libxl_json: allow basic JSON type objects generation Wei Liu
2014-05-06 13:02   ` Ian Campbell
2014-05-01 12:58 ` [PATCH V4 11/24] libxl/gentypes.py: include discriminator in JSON output Wei Liu
2014-05-06 13:03   ` Ian Campbell
2014-05-06 13:09     ` Wei Liu
2014-05-07 10:00       ` Ian Campbell
2014-05-07 10:35         ` Wei Liu
2014-05-07 10:45           ` Ian Campbell
2014-05-01 12:58 ` [PATCH V4 12/24] libxl IDL: generate code to parse libxl__json_object to libxl_FOO struct Wei Liu
2014-05-06 13:45   ` Ian Campbell
2014-05-01 12:58 ` [PATCH V4 13/24] libxl/gentest.py: test JSON parser Wei Liu
2014-05-06 13:46   ` Ian Campbell
2014-05-06 14:11     ` Wei Liu
2014-05-01 12:58 ` [PATCH V4 14/24] libxl: introduce libxl_key_value_list_length Wei Liu
2014-05-06 13:50   ` Ian Campbell
2014-05-06 14:13     ` Wei Liu
2014-05-01 12:58 ` [PATCH V4 15/24] libxl: introduce libxl_cpuid_policy_list_length Wei Liu
2014-05-06 13:51   ` Ian Campbell
2014-05-06 14:17     ` Wei Liu
2014-05-01 12:58 ` [PATCH V4 16/24] libxl: copy function for builtin types Wei Liu
2014-05-06 14:03   ` Ian Campbell
2014-05-06 14:36     ` Wei Liu
2014-05-07 10:05       ` Ian Campbell
2014-05-07 10:19         ` Wei Liu
2014-05-07 10:40           ` Ian Campbell
2014-05-07 10:47     ` Ian Jackson
2014-05-07 10:57       ` Ian Campbell
2014-05-07 12:55         ` Wei Liu
2014-05-07 13:10           ` Ian Campbell
2014-05-01 12:58 ` Wei Liu [this message]
2014-05-06 14:06   ` [PATCH V4 17/24] libxl IDL: generate deep copy functions Ian Campbell
2014-05-01 12:58 ` [PATCH V4 18/24] libxl/gentest.py: test " Wei Liu
2014-05-06 14:06   ` Ian Campbell
2014-05-01 12:58 ` [PATCH V4 19/24] xl: introduce and use "xl-json" format Wei Liu
2014-05-06 14:26   ` Ian Campbell
2014-05-06 15:17     ` Wei Liu
2014-05-06 16:35       ` Wei Liu
2014-05-07 10:11       ` Ian Campbell
2014-05-07 11:18         ` Ian Jackson
2014-05-07 11:20         ` Wei Liu
2014-05-07 11:30           ` Ian Campbell
2014-05-07 13:00             ` Wei Liu
2014-05-07 13:08               ` Ian Campbell
2014-05-07 11:16       ` Ian Jackson
2014-05-07 11:20         ` Ian Campbell
2014-05-07 12:56         ` Wei Liu
2014-05-01 12:58 ` [PATCH V4 20/24] xl: introduce load/save_domain_config Wei Liu
2014-05-06 14:28   ` Ian Campbell
2014-05-06 15:03     ` Wei Liu
2014-05-07 10:12       ` Ian Campbell
2014-05-01 12:58 ` [PATCH V4 21/24] xl: update domain configuration when running mem-set and mem-max Wei Liu
2014-05-06 14:30   ` Ian Campbell
2014-05-06 14:56     ` Wei Liu
2014-05-01 12:58 ` [PATCH V4 22/24] xl: update domain configuration when we hotplug a device Wei Liu
2014-05-06 15:44   ` Ian Campbell
2014-05-06 15:58     ` Wei Liu
2014-05-07 10:16       ` Ian Campbell
2014-05-06 16:57   ` David Vrabel
2014-05-06 17:13     ` Wei Liu
2014-05-07 10:21       ` David Vrabel
2014-05-07 10:42         ` Ian Campbell
2014-05-07 10:59       ` Andrew Cooper
2014-05-01 12:58 ` [PATCH V4 23/24] libxl: consider force removal of device successful Wei Liu
2014-05-06 15:48   ` Ian Campbell
2014-05-06 16:04     ` Wei Liu
2014-05-07 10:18       ` Ian Campbell
2014-05-07 13:09         ` Wei Liu
2014-05-01 12:58 ` [PATCH V4 24/24] xl: update configuration when we unplug a device Wei Liu
2014-05-06 15:55   ` Ian Campbell
2014-05-06 16:12     ` Wei Liu
2014-05-07 10:20       ` Ian Campbell
2014-05-01 13:02 ` [PATCH V4 00/24] JSON infrastructure, new "xl-json" format and domain configuration synchronization Wei Liu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1398949101-23320-18-git-send-email-wei.liu2@citrix.com \
    --to=wei.liu2@citrix.com \
    --cc=ian.campbell@citrix.com \
    --cc=ian.jackson@eu.citrix.com \
    --cc=xen-devel@lists.xen.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.