xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Oleksandr Grytsov <al1img@gmail.com>
To: xen-devel@lists.xenproject.org
Cc: ian.jackson@eu.citrix.com, wei.liu2@citrix.com,
	Oleksandr Grytsov <oleksandr_grytsov@epam.com>
Subject: [PATCH v6 02/13] libxl: add generic functions to get and free device list
Date: Tue, 12 Sep 2017 16:48:07 +0300	[thread overview]
Message-ID: <1505224098-19330-3-git-send-email-al1img@gmail.com> (raw)
In-Reply-To: <1505224098-19330-1-git-send-email-al1img@gmail.com>

From: Oleksandr Grytsov <oleksandr_grytsov@epam.com>

Add libxl__device_list and libxl__device_list_free
functions to handle device list using the device
framework.

Signed-off-by: Oleksandr Grytsov <oleksandr_grytsov@epam.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
---
 tools/libxl/libxl_device.c   | 61 ++++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_internal.h |  8 ++++++
 2 files changed, 69 insertions(+)

diff --git a/tools/libxl/libxl_device.c b/tools/libxl/libxl_device.c
index 3296e83..487be28 100644
--- a/tools/libxl/libxl_device.c
+++ b/tools/libxl/libxl_device.c
@@ -1990,6 +1990,67 @@ out:
     return rc;
 }
 
+void *libxl__device_list(libxl__gc *gc, const struct libxl_device_type *dt,
+                         uint32_t domid, const char* name, int *num)
+{
+    void *r = NULL;
+    void *list = NULL;
+    void *item = NULL;
+    char *libxl_path;
+    char **dir = NULL;
+    unsigned int ndirs = 0;
+    int rc;
+
+    *num = 0;
+
+    libxl_path = GCSPRINTF("%s/device/%s",
+                           libxl__xs_libxl_path(gc, domid), name);
+
+    dir = libxl__xs_directory(gc, XBT_NULL, libxl_path, &ndirs);
+
+    if (dir && ndirs) {
+        list = libxl__malloc(NOGC, dt->dev_elem_size * ndirs);
+        item = list;
+
+        while (*num < ndirs) {
+            dt->init(item);
+            ++(*num);
+
+            if (dt->from_xenstore) {
+                char *device_libxl_path = GCSPRINTF("%s/%s", libxl_path, *dir);
+                rc = dt->from_xenstore(gc, device_libxl_path, atoi(*dir), item);
+                if (rc) goto out;
+            }
+
+            item = (uint8_t *)item + dt->dev_elem_size;
+            ++dir;
+        }
+    }
+
+    r = list;
+    list = NULL;
+
+out:
+
+    if (list) {
+        libxl__device_list_free(dt, list, *num);
+        *num = 0;
+    }
+
+    return r;
+}
+
+void libxl__device_list_free(const struct libxl_device_type *dt,
+                             void *list, int num)
+{
+    int i;
+
+    for (i = 0; i < num; i++)
+        dt->dispose((uint8_t*)list + i * dt->dev_elem_size);
+
+    free(list);
+}
+
 /*
  * Local variables:
  * mode: C
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index c99ef3b..c94a117 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -3505,6 +3505,7 @@ struct libxl_device_type {
     int (*dm_needed)(void *, unsigned);
     void (*update_config)(libxl__gc *, void *, void *);
     int (*update_devid)(libxl__gc *, uint32_t, void *);
+    int (*from_xenstore)(libxl__gc *, const char *, libxl_devid, void *);
     int (*set_xenstore_config)(libxl__gc *, uint32_t, void *, flexarray_t *,
                                flexarray_t *, flexarray_t *);
 };
@@ -4385,6 +4386,13 @@ void libxl__device_add_async(libxl__egc *egc, uint32_t domid,
 int libxl__device_add(libxl__gc *gc, uint32_t domid,
                       const struct libxl_device_type *dt, void *type);
 
+/* Caller is responsible for freeing the memory by calling
+ * libxl__device_list_free
+ */
+void* libxl__device_list(libxl__gc *gc, const struct libxl_device_type *dt,
+                         uint32_t domid, const char* name, int *num);
+void libxl__device_list_free(const struct libxl_device_type *dt,
+                             void *list, int num);
 #endif
 
 /*
-- 
2.7.4


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

  parent reply	other threads:[~2017-09-12 13:48 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-12 13:48 [PATCH v6 00/13] libxl: add PV display device driver interface Oleksandr Grytsov
2017-09-12 13:48 ` [PATCH v6 01/13] libxl: add generic function to add device Oleksandr Grytsov
2017-09-12 13:48 ` Oleksandr Grytsov [this message]
2017-09-12 13:48 ` [PATCH v6 03/13] libxl: add vdispl device Oleksandr Grytsov
2017-09-12 13:48 ` [PATCH v6 04/13] xl: add PV display device commands Oleksandr Grytsov
2017-09-12 13:48 ` [PATCH v6 05/13] docs: add PV display driver information Oleksandr Grytsov
2017-09-12 13:48 ` [PATCH v6 06/13] libxl: change p9 to use generec add function Oleksandr Grytsov
2017-09-12 13:48 ` [PATCH v6 07/13] libxl: change vkb " Oleksandr Grytsov
2017-09-12 13:48 ` [PATCH v6 08/13] libxl: change vfb " Oleksandr Grytsov
2017-09-12 13:48 ` [PATCH v6 09/13] libxl: change disk to use generic getting list functions Oleksandr Grytsov
2017-09-12 13:48 ` [PATCH v6 10/13] libxl: change nic to use generec add function Oleksandr Grytsov
2017-09-12 16:09   ` Wei Liu
2017-09-13  8:41   ` Wei Liu
2017-09-12 13:48 ` [PATCH v6 11/13] libxl: fix memory leak in libxl__colo_save_setup Oleksandr Grytsov
2017-09-12 16:10   ` Wei Liu
2017-09-12 13:48 ` [PATCH v6 12/13] libxl: change vtpm to use generec add function Oleksandr Grytsov
2017-09-12 13:48 ` [PATCH v6 13/13] libxl: remove unneeded DEVICE_ADD macro Oleksandr Grytsov
2017-09-12 16:22 ` [PATCH v6 00/13] libxl: add PV display device driver interface Wei Liu
2017-09-13  7:59   ` Oleksandr Grytsov

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=1505224098-19330-3-git-send-email-al1img@gmail.com \
    --to=al1img@gmail.com \
    --cc=ian.jackson@eu.citrix.com \
    --cc=oleksandr_grytsov@epam.com \
    --cc=wei.liu2@citrix.com \
    --cc=xen-devel@lists.xenproject.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).