[PATCH v3 09/35] audio: start making AudioState a QOM Object

marcandre.lureau@redhat.com posted 35 patches 3 days ago
Maintainers: Gerd Hoffmann <kraxel@redhat.com>, Christian Schoenebeck <qemu_oss@crudebyte.com>, "Marc-André Lureau" <marcandre.lureau@redhat.com>, "Philippe Mathieu-Daudé" <philmd@linaro.org>, Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp>, Thomas Huth <huth@tuxfamily.org>, Alexandre Ratchov <alex@caoua.org>, Peter Maydell <peter.maydell@linaro.org>, Jan Kiszka <jan.kiszka@web.de>, Alistair Francis <alistair@alistair23.me>, "Edgar E. Iglesias" <edgar.iglesias@gmail.com>, Laurent Vivier <laurent@vivier.eu>, "Michael S. Tsirkin" <mst@redhat.com>, Manos Pitsidianakis <manos.pitsidianakis@linaro.org>, Eduardo Habkost <eduardo@habkost.net>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, Yanan Wang <wangyanan55@huawei.com>, Zhao Liu <zhao1.liu@intel.com>, Paolo Bonzini <pbonzini@redhat.com>, "Daniel P. Berrangé" <berrange@redhat.com>, "Hervé Poussineau" <hpoussin@reactos.org>, BALATON Zoltan <balaton@eik.bme.hu>, Jiaxun Yang <jiaxun.yang@flygoat.com>, "Alex Bennée" <alex.bennee@linaro.org>
[PATCH v3 09/35] audio: start making AudioState a QOM Object
Posted by marcandre.lureau@redhat.com 3 days ago
From: Marc-André Lureau <marcandre.lureau@redhat.com>

QOM brings some conveniences for introspection, type checking, reference
counting, interfaces etc. This is only the first step to introduce QOM
in audio/ (I have more in the pipeline)

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 audio/audio.h     |  7 +++++++
 audio/audio_int.h |  2 ++
 audio/audio.c     | 43 ++++++++++++++++++++++++++++++++-----------
 3 files changed, 41 insertions(+), 11 deletions(-)

diff --git a/audio/audio.h b/audio/audio.h
index eb5b5d662d..e41c5bc55a 100644
--- a/audio/audio.h
+++ b/audio/audio.h
@@ -80,6 +80,10 @@ typedef struct SWVoiceOut SWVoiceOut;
 typedef struct CaptureVoiceOut CaptureVoiceOut;
 typedef struct SWVoiceIn SWVoiceIn;
 
+struct AudioStateClass {
+    ObjectClass parent_class;
+};
+
 typedef struct AudioState AudioState;
 typedef struct QEMUSoundCard {
     char *name;
@@ -182,4 +186,7 @@ const char *audio_get_id(QEMUSoundCard *card);
 #define DEFINE_AUDIO_PROPERTIES(_s, _f)         \
     DEFINE_PROP_AUDIODEV("audiodev", _s, _f)
 
+#define TYPE_AUDIO_STATE "audio-state"
+OBJECT_DECLARE_TYPE(AudioState, AudioStateClass, AUDIO_STATE)
+
 #endif /* QEMU_AUDIO_H */
diff --git a/audio/audio_int.h b/audio/audio_int.h
index f78ca05f92..b2b4d2d10e 100644
--- a/audio/audio_int.h
+++ b/audio/audio_int.h
@@ -217,6 +217,8 @@ struct SWVoiceCap {
 };
 
 typedef struct AudioState {
+    Object parent;
+
     struct audio_driver *drv;
     Audiodev *dev;
     void *drv_opaque;
diff --git a/audio/audio.c b/audio/audio.c
index 6197fa1788..e1696403ae 100644
--- a/audio/audio.c
+++ b/audio/audio.c
@@ -1617,8 +1617,19 @@ static void audio_vm_change_state_handler (void *opaque, bool running,
     audio_reset_timer (s);
 }
 
-static void free_audio_state(AudioState *s)
+static void audio_state_init(Object *obj)
 {
+    AudioState *s = AUDIO_STATE(obj);
+
+    QLIST_INIT(&s->hw_head_out);
+    QLIST_INIT(&s->hw_head_in);
+    QLIST_INIT(&s->cap_head);
+    s->ts = timer_new_ns(QEMU_CLOCK_VIRTUAL, audio_timer, s);
+}
+
+static void audio_state_finalize(Object *obj)
+{
+    AudioState *s = AUDIO_STATE(obj);
     HWVoiceOut *hwo, *hwon;
     HWVoiceIn *hwi, *hwin;
 
@@ -1663,8 +1674,6 @@ static void free_audio_state(AudioState *s)
         timer_free(s->ts);
         s->ts = NULL;
     }
-
-    g_free(s);
 }
 
 void audio_cleanup(void)
@@ -1673,7 +1682,7 @@ void audio_cleanup(void)
     while (!QTAILQ_EMPTY(&audio_states)) {
         AudioState *s = QTAILQ_FIRST(&audio_states);
         QTAILQ_REMOVE(&audio_states, s, list);
-        free_audio_state(s);
+        object_unref(s);
     }
 }
 
@@ -1732,18 +1741,13 @@ static AudioState *audio_init(Audiodev *dev, Error **errp)
     AudioState *s;
     struct audio_driver *driver;
 
-    s = g_new0(AudioState, 1);
+    s = AUDIO_STATE(object_new(TYPE_AUDIO_STATE));
 
-    QLIST_INIT (&s->hw_head_out);
-    QLIST_INIT (&s->hw_head_in);
-    QLIST_INIT (&s->cap_head);
     if (!atexit_registered) {
         atexit(audio_cleanup);
         atexit_registered = true;
     }
 
-    s->ts = timer_new_ns(QEMU_CLOCK_VIRTUAL, audio_timer, s);
-
     if (dev) {
         /* -audiodev option */
         s->dev = dev;
@@ -1796,7 +1800,7 @@ static AudioState *audio_init(Audiodev *dev, Error **errp)
     return s;
 
 out:
-    free_audio_state(s);
+    object_unref(s);
     return NULL;
 }
 
@@ -2320,3 +2324,20 @@ AudiodevList *qmp_query_audiodevs(Error **errp)
     }
     return ret;
 }
+
+static const TypeInfo audio_state_info = {
+    .name = TYPE_AUDIO_STATE,
+    .parent = TYPE_OBJECT,
+    .instance_size = sizeof(AudioState),
+    .instance_init = audio_state_init,
+    .instance_finalize = audio_state_finalize,
+    .abstract = false, /* TODO: subclass drivers and make it abstract */
+    .class_size = sizeof(AudioStateClass),
+};
+
+static void register_types(void)
+{
+    type_register_static(&audio_state_info);
+}
+
+type_init(register_types);
-- 
2.51.0


Re: [PATCH v3 09/35] audio: start making AudioState a QOM Object
Posted by BALATON Zoltan 2 days, 23 hours ago
On Mon, 27 Oct 2025, marcandre.lureau@redhat.com wrote:
> From: Marc-André Lureau <marcandre.lureau@redhat.com>
>
> QOM brings some conveniences for introspection, type checking, reference
> counting, interfaces etc. This is only the first step to introduce QOM
> in audio/ (I have more in the pipeline)
>
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
> audio/audio.h     |  7 +++++++
> audio/audio_int.h |  2 ++
> audio/audio.c     | 43 ++++++++++++++++++++++++++++++++-----------
> 3 files changed, 41 insertions(+), 11 deletions(-)
>
> diff --git a/audio/audio.h b/audio/audio.h
> index eb5b5d662d..e41c5bc55a 100644
> --- a/audio/audio.h
> +++ b/audio/audio.h
> @@ -80,6 +80,10 @@ typedef struct SWVoiceOut SWVoiceOut;
> typedef struct CaptureVoiceOut CaptureVoiceOut;
> typedef struct SWVoiceIn SWVoiceIn;
>
> +struct AudioStateClass {
> +    ObjectClass parent_class;
> +};
> +
> typedef struct AudioState AudioState;
> typedef struct QEMUSoundCard {
>     char *name;
> @@ -182,4 +186,7 @@ const char *audio_get_id(QEMUSoundCard *card);
> #define DEFINE_AUDIO_PROPERTIES(_s, _f)         \
>     DEFINE_PROP_AUDIODEV("audiodev", _s, _f)
>
> +#define TYPE_AUDIO_STATE "audio-state"
> +OBJECT_DECLARE_TYPE(AudioState, AudioStateClass, AUDIO_STATE)
> +
> #endif /* QEMU_AUDIO_H */
> diff --git a/audio/audio_int.h b/audio/audio_int.h
> index f78ca05f92..b2b4d2d10e 100644
> --- a/audio/audio_int.h
> +++ b/audio/audio_int.h
> @@ -217,6 +217,8 @@ struct SWVoiceCap {
> };
>
> typedef struct AudioState {
> +    Object parent;

Coding style says parent_obj, not sure other names are acceptable.

Regards,
BALATON Zoltan
Re: [PATCH v3 09/35] audio: start making AudioState a QOM Object
Posted by Marc-André Lureau 2 days, 2 hours ago
Hi

On Mon, Oct 27, 2025 at 8:29 PM BALATON Zoltan <balaton@eik.bme.hu> wrote:
>
> On Mon, 27 Oct 2025, marcandre.lureau@redhat.com wrote:
> > From: Marc-André Lureau <marcandre.lureau@redhat.com>
> >
> > QOM brings some conveniences for introspection, type checking, reference
> > counting, interfaces etc. This is only the first step to introduce QOM
> > in audio/ (I have more in the pipeline)
> >
> > Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> > Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> > ---
> > audio/audio.h     |  7 +++++++
> > audio/audio_int.h |  2 ++
> > audio/audio.c     | 43 ++++++++++++++++++++++++++++++++-----------
> > 3 files changed, 41 insertions(+), 11 deletions(-)
> >
> > diff --git a/audio/audio.h b/audio/audio.h
> > index eb5b5d662d..e41c5bc55a 100644
> > --- a/audio/audio.h
> > +++ b/audio/audio.h
> > @@ -80,6 +80,10 @@ typedef struct SWVoiceOut SWVoiceOut;
> > typedef struct CaptureVoiceOut CaptureVoiceOut;
> > typedef struct SWVoiceIn SWVoiceIn;
> >
> > +struct AudioStateClass {
> > +    ObjectClass parent_class;
> > +};
> > +
> > typedef struct AudioState AudioState;
> > typedef struct QEMUSoundCard {
> >     char *name;
> > @@ -182,4 +186,7 @@ const char *audio_get_id(QEMUSoundCard *card);
> > #define DEFINE_AUDIO_PROPERTIES(_s, _f)         \
> >     DEFINE_PROP_AUDIODEV("audiodev", _s, _f)
> >
> > +#define TYPE_AUDIO_STATE "audio-state"
> > +OBJECT_DECLARE_TYPE(AudioState, AudioStateClass, AUDIO_STATE)
> > +
> > #endif /* QEMU_AUDIO_H */
> > diff --git a/audio/audio_int.h b/audio/audio_int.h
> > index f78ca05f92..b2b4d2d10e 100644
> > --- a/audio/audio_int.h
> > +++ b/audio/audio_int.h
> > @@ -217,6 +217,8 @@ struct SWVoiceCap {
> > };
> >
> > typedef struct AudioState {
> > +    Object parent;
>
> Coding style says parent_obj, not sure other names are acceptable.
>


Alex, since you wrote that section in docs/devel/style.rst, should we
enforce that naming?

-- 
Marc-André Lureau
Re: [PATCH v3 09/35] audio: start making AudioState a QOM Object
Posted by Philippe Mathieu-Daudé 1 day, 1 hour ago
On 28/10/25 13:34, Marc-André Lureau wrote:
> Hi
> 
> On Mon, Oct 27, 2025 at 8:29 PM BALATON Zoltan <balaton@eik.bme.hu> wrote:
>>
>> On Mon, 27 Oct 2025, marcandre.lureau@redhat.com wrote:
>>> From: Marc-André Lureau <marcandre.lureau@redhat.com>
>>>
>>> QOM brings some conveniences for introspection, type checking, reference
>>> counting, interfaces etc. This is only the first step to introduce QOM
>>> in audio/ (I have more in the pipeline)
>>>
>>> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
>>> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
>>> ---
>>> audio/audio.h     |  7 +++++++
>>> audio/audio_int.h |  2 ++
>>> audio/audio.c     | 43 ++++++++++++++++++++++++++++++++-----------
>>> 3 files changed, 41 insertions(+), 11 deletions(-)


>>> diff --git a/audio/audio_int.h b/audio/audio_int.h
>>> index f78ca05f92..b2b4d2d10e 100644
>>> --- a/audio/audio_int.h
>>> +++ b/audio/audio_int.h
>>> @@ -217,6 +217,8 @@ struct SWVoiceCap {
>>> };
>>>
>>> typedef struct AudioState {
>>> +    Object parent;
>>
>> Coding style says parent_obj, not sure other names are acceptable.
>>
> 
> 
> Alex, since you wrote that section in docs/devel/style.rst, should we
> enforce that naming?

Yes please!