qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [RFC] qom: Sorted class enumeration
@ 2012-02-22  9:44 Andreas Färber
  2012-02-22  9:44 ` [Qemu-devel] [PATCH RFC] qom: Introduce object_class_foreach_ordered() Andreas Färber
  2012-02-22 13:25 ` [Qemu-devel] [RFC] qom: Sorted class enumeration Anthony Liguori
  0 siblings, 2 replies; 3+ messages in thread
From: Andreas Färber @ 2012-02-22  9:44 UTC (permalink / raw)
  To: qemu-devel; +Cc: pbonzini, Andreas Färber

Hello,

For listing registered CPU classes I needed a way to sort classes in a
custom (i.e., non-hashtable) order. I found it easiest to sort the classes
using the existing foreach infrastructure, on the go via GLib's binary tree.

Patch is still missing documentation, but do you think this is the right
direction, Anthony?

I've been wondering if it might make sense to replace the current filtering
mechanism (abstract and type) with another callback function or whether that
would be overkill - currently the only other filtering I needed to do was
to ignore the "host" CPU class, which can be done by simple if in the callback.

Regards,
Andreas

Andreas Färber (1):
  qom: Introduce object_class_foreach_ordered()

 include/qemu/object.h |    6 ++++++
 qom/object.c          |   43 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 0 deletions(-)

-- 
1.7.7

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Qemu-devel] [PATCH RFC] qom: Introduce object_class_foreach_ordered()
  2012-02-22  9:44 [Qemu-devel] [RFC] qom: Sorted class enumeration Andreas Färber
@ 2012-02-22  9:44 ` Andreas Färber
  2012-02-22 13:25 ` [Qemu-devel] [RFC] qom: Sorted class enumeration Anthony Liguori
  1 sibling, 0 replies; 3+ messages in thread
From: Andreas Färber @ 2012-02-22  9:44 UTC (permalink / raw)
  To: qemu-devel; +Cc: pbonzini, Andreas Färber, Peter Maydell

This functions allows to walk the classes in a custom order rather than
in hashtable order.

Signed-off-by: Andreas Färber <afaerber@suse.de>
Cc: Anthony Liguori <anthony@codemonkey.ws>
Cc: Peter Maydell <peter.maydell@linaro.org>
---
 include/qemu/object.h |    6 ++++++
 qom/object.c          |   43 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 0 deletions(-)

diff --git a/include/qemu/object.h b/include/qemu/object.h
index 5179c0c..ab3597a 100644
--- a/include/qemu/object.h
+++ b/include/qemu/object.h
@@ -560,6 +560,12 @@ ObjectClass *object_class_by_name(const char *typename);
 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
                           const char *implements_type, bool include_abstract,
                           void *opaque);
+
+void object_class_foreach_ordered(void (*fn)(ObjectClass *klass, void *opaque),
+                                  const char *implements_type,
+                                  bool include_abstract, void *opaque,
+                                  GCompareFunc compfn);
+
 /**
  * object_ref:
  * @obj: the object
diff --git a/qom/object.c b/qom/object.c
index 2f05ca8..dbfd6a4 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -570,6 +570,49 @@ void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
     g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
 }
 
+typedef struct OCFSData {
+    void (*fn)(ObjectClass *klass, void *opaque);
+    void *opaque;
+    GTree *classes;
+} OCFSData;
+
+static void object_class_foreach_ordered_sort(ObjectClass *klass,
+                                              void *opaque)
+{
+    OCFSData *data = opaque;
+
+    g_tree_insert(data->classes, klass, NULL);
+}
+
+static gboolean object_class_foreach_ordered_tramp(gpointer key,
+                                                   gpointer value,
+                                                   gpointer data)
+{
+    ObjectClass *klass = key;
+    OCFSData *d = data;
+
+    d->fn(klass, d->opaque);
+
+    return FALSE;
+}
+
+void object_class_foreach_ordered(void (*fn)(ObjectClass *klass, void *opaque),
+                                  const char *implements_type,
+                                  bool include_abstract, void *opaque,
+                                  GCompareFunc compfn)
+{
+    OCFSData data = {
+        .fn = fn,
+        .opaque = opaque,
+    };
+
+    data.classes = g_tree_new(compfn);
+    object_class_foreach(object_class_foreach_ordered_sort,
+                         implements_type, include_abstract, &data);
+    g_tree_foreach(data.classes, object_class_foreach_ordered_tramp, &data);
+    g_tree_destroy(data.classes);
+}
+
 void object_ref(Object *obj)
 {
     obj->ref++;
-- 
1.7.7

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [Qemu-devel] [RFC] qom: Sorted class enumeration
  2012-02-22  9:44 [Qemu-devel] [RFC] qom: Sorted class enumeration Andreas Färber
  2012-02-22  9:44 ` [Qemu-devel] [PATCH RFC] qom: Introduce object_class_foreach_ordered() Andreas Färber
@ 2012-02-22 13:25 ` Anthony Liguori
  1 sibling, 0 replies; 3+ messages in thread
From: Anthony Liguori @ 2012-02-22 13:25 UTC (permalink / raw)
  To: Andreas Färber; +Cc: pbonzini, qemu-devel

On 02/22/2012 03:44 AM, Andreas Färber wrote:
> Hello,
>
> For listing registered CPU classes I needed a way to sort classes in a
> custom (i.e., non-hashtable) order. I found it easiest to sort the classes
> using the existing foreach infrastructure, on the go via GLib's binary tree.
>
> Patch is still missing documentation, but do you think this is the right
> direction, Anthony?

I think it might be better to have an object_class_search() function that acts 
like object_class_foreach() but returns a GSList.  You can then call 
g_list_sort() on the resulting data structure.

> I've been wondering if it might make sense to replace the current filtering
> mechanism (abstract and type) with another callback function or whether that
> would be overkill - currently the only other filtering I needed to do was
> to ignore the "host" CPU class, which can be done by simple if in the callback.

I'd prefer not to do callbacks because this interface gets exposed via QMP and 
callbacks can't be modeled via QMP.

Regards,

Anthony Liguori

>
> Regards,
> Andreas
>
> Andreas Färber (1):
>    qom: Introduce object_class_foreach_ordered()
>
>   include/qemu/object.h |    6 ++++++
>   qom/object.c          |   43 +++++++++++++++++++++++++++++++++++++++++++
>   2 files changed, 49 insertions(+), 0 deletions(-)
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-02-22 13:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-02-22  9:44 [Qemu-devel] [RFC] qom: Sorted class enumeration Andreas Färber
2012-02-22  9:44 ` [Qemu-devel] [PATCH RFC] qom: Introduce object_class_foreach_ordered() Andreas Färber
2012-02-22 13:25 ` [Qemu-devel] [RFC] qom: Sorted class enumeration Anthony Liguori

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).