diff mbox

[RFC,5/7] cpu: Introduce cpu_class_foreach()

Message ID 1327843531-32403-6-git-send-email-afaerber@suse.de
State New
Headers show

Commit Message

Andreas Färber Jan. 29, 2012, 1:25 p.m. UTC
Provides an easy way to loop over all non-abstract CPU classes.

Signed-off-by: Andreas Färber <afaerber@suse.de>
---
 hw/cpu.c           |   25 +++++++++++++++++++++++++
 include/qemu/cpu.h |    9 +++++++++
 2 files changed, 34 insertions(+), 0 deletions(-)

Comments

Anthony Liguori Jan. 30, 2012, 2:16 a.m. UTC | #1
On 01/29/2012 07:25 AM, Andreas Färber wrote:
> Provides an easy way to loop over all non-abstract CPU classes.
>
> Signed-off-by: Andreas Färber<afaerber@suse.de>

I have a patch in my next series which provides an interface for this.

https://github.com/aliguori/qemu/commit/42ab3f78477307438591c77816f06f12cf9d9fc0

I'll post a proper version tomorrow.

Regards,

Anthony Liguori

> ---
>   hw/cpu.c           |   25 +++++++++++++++++++++++++
>   include/qemu/cpu.h |    9 +++++++++
>   2 files changed, 34 insertions(+), 0 deletions(-)
>
> diff --git a/hw/cpu.c b/hw/cpu.c
> index c0e9cfa..ac0add7 100644
> --- a/hw/cpu.c
> +++ b/hw/cpu.c
> @@ -11,6 +11,31 @@
>   #include "qemu/cpu.h"
>   #include "qemu-common.h"
>
> +struct CPUListData {
> +    void (*fn)(ObjectClass *klass, void *opaque);
> +    void *opaque;
> +};
> +
> +static void cpu_class_foreach_tramp(ObjectClass *klass, void *opaque)
> +{
> +    struct CPUListData *s = opaque;
> +
> +    if (!object_class_is_abstract(klass)&&
> +        object_class_dynamic_cast(klass, TYPE_CPU) != NULL) {
> +        s->fn(klass, s->opaque);
> +    }
> +}
> +
> +void cpu_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
> +                       void *opaque)
> +{
> +    struct CPUListData s = {
> +        .fn = fn,
> +        .opaque = opaque,
> +    };
> +    object_class_foreach(cpu_class_foreach_tramp,&s);
> +}
> +
>   static TypeInfo cpu_type_info = {
>       .name = TYPE_CPU,
>       .parent = TYPE_OBJECT,
> diff --git a/include/qemu/cpu.h b/include/qemu/cpu.h
> index 4b81f3b..d06c87e 100644
> --- a/include/qemu/cpu.h
> +++ b/include/qemu/cpu.h
> @@ -24,4 +24,13 @@ typedef struct CPU {
>   } CPU;
>
>
> +/**
> + * cpu_class_foreach:
> + * @fn: Callback function called for each non-abstract CPU type.
> + * @opaque: Opaque data passed through to the callback function.
> + */
> +void cpu_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
> +                       void *opaque);
> +
> +
>   #endif
Andreas Färber Jan. 30, 2012, 12:02 p.m. UTC | #2
Am 30.01.2012 03:16, schrieb Anthony Liguori:
> On 01/29/2012 07:25 AM, Andreas Färber wrote:
>> Provides an easy way to loop over all non-abstract CPU classes.
>>
>> Signed-off-by: Andreas Färber<afaerber@suse.de>
> 
> I have a patch in my next series which provides an interface for this.
> 
> https://github.com/aliguori/qemu/commit/42ab3f78477307438591c77816f06f12cf9d9fc0

Looks good, sorry I missed it.

You might want to document it in object.h while you're at it.

Andreas
diff mbox

Patch

diff --git a/hw/cpu.c b/hw/cpu.c
index c0e9cfa..ac0add7 100644
--- a/hw/cpu.c
+++ b/hw/cpu.c
@@ -11,6 +11,31 @@ 
 #include "qemu/cpu.h"
 #include "qemu-common.h"
 
+struct CPUListData {
+    void (*fn)(ObjectClass *klass, void *opaque);
+    void *opaque;
+};
+
+static void cpu_class_foreach_tramp(ObjectClass *klass, void *opaque)
+{
+    struct CPUListData *s = opaque;
+
+    if (!object_class_is_abstract(klass) &&
+        object_class_dynamic_cast(klass, TYPE_CPU) != NULL) {
+        s->fn(klass, s->opaque);
+    }
+}
+
+void cpu_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
+                       void *opaque)
+{
+    struct CPUListData s = {
+        .fn = fn,
+        .opaque = opaque,
+    };
+    object_class_foreach(cpu_class_foreach_tramp, &s);
+}
+
 static TypeInfo cpu_type_info = {
     .name = TYPE_CPU,
     .parent = TYPE_OBJECT,
diff --git a/include/qemu/cpu.h b/include/qemu/cpu.h
index 4b81f3b..d06c87e 100644
--- a/include/qemu/cpu.h
+++ b/include/qemu/cpu.h
@@ -24,4 +24,13 @@  typedef struct CPU {
 } CPU;
 
 
+/**
+ * cpu_class_foreach:
+ * @fn: Callback function called for each non-abstract CPU type.
+ * @opaque: Opaque data passed through to the callback function.
+ */
+void cpu_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
+                       void *opaque);
+
+
 #endif