Signed-off-by: Rafael Fonseca <r4f4rfs@xxxxxxxxx> --- src/bhyve/bhyve_monitor.c | 48 +++++++++++++++++++++++++-------------- src/bhyve/bhyve_monitor.h | 3 ++- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/bhyve/bhyve_monitor.c b/src/bhyve/bhyve_monitor.c index e5cd39a086..3efa3323ff 100644 --- a/src/bhyve/bhyve_monitor.c +++ b/src/bhyve/bhyve_monitor.c @@ -39,7 +39,7 @@ VIR_LOG_INIT("bhyve.bhyve_monitor"); struct _bhyveMonitor { - virObject parent; + GObject parent; bhyveConnPtr driver; virDomainObjPtr vm; @@ -48,44 +48,59 @@ struct _bhyveMonitor { bool reboot; }; -static virClassPtr bhyveMonitorClass; +G_DEFINE_TYPE(bhyveMonitor, bhyve_monitor, G_TYPE_OBJECT); static void -bhyveMonitorDispose(void *obj) +bhyveMonitorDispose(GObject *obj) { - bhyveMonitorPtr mon = obj; + bhyveMonitorPtr mon = BHYVE_MONITOR(obj); - VIR_FORCE_CLOSE(mon->kq); virObjectUnref(mon->vm); + mon->vm = NULL; + + G_OBJECT_CLASS(bhyve_monitor_parent_class)->dispose(obj); } -static int -bhyveMonitorOnceInit(void) +static void +bhyveMonitorFinalize(GObject *obj) { - if (!VIR_CLASS_NEW(bhyveMonitor, virClassForObject())) - return -1; + bhyveMonitorPtr mon = BHYVE_MONITOR(obj); + + VIR_FORCE_CLOSE(mon->kq); + + G_OBJECT_CLASS(bhyve_monitor_parent_class)->finalize(obj); +} - return 0; +static void +bhyve_monitor_init(bhyveMonitor *mon G_GNUC_UNUSED) +{ } -VIR_ONCE_GLOBAL_INIT(bhyveMonitor); +static void +bhyve_monitor_class_init(bhyveMonitorClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->dispose = bhyveMonitorDispose; + obj->finalize = bhyveMonitorFinalize; +} static void bhyveMonitorIO(int, int, int, void *); static bool bhyveMonitorRegister(bhyveMonitorPtr mon) { - virObjectRef(mon); + g_object_ref(mon); mon->watch = virEventAddHandle(mon->kq, VIR_EVENT_HANDLE_READABLE | VIR_EVENT_HANDLE_ERROR | VIR_EVENT_HANDLE_HANGUP, bhyveMonitorIO, mon, - virObjectFreeCallback); + g_object_unref); if (mon->watch < 0) { VIR_DEBUG("failed to add event handle for mon %p", mon); - virObjectUnref(mon); + g_object_unref(mon); return false; } return true; @@ -181,8 +196,7 @@ bhyveMonitorOpenImpl(virDomainObjPtr vm, bhyveConnPtr driver) if (bhyveMonitorInitialize() < 0) return NULL; - if (!(mon = virObjectNew(bhyveMonitorClass))) - return NULL; + mon = BHYVE_MONITOR(g_object_new(BHYVE_TYPE_MONITOR, NULL)); mon->driver = driver; mon->reboot = false; @@ -238,5 +252,5 @@ bhyveMonitorClose(bhyveMonitorPtr mon) VIR_DEBUG("cleaning up bhyveMonitor %p", mon); bhyveMonitorUnregister(mon); - virObjectUnref(mon); + g_clear_object(&mon); } diff --git a/src/bhyve/bhyve_monitor.h b/src/bhyve/bhyve_monitor.h index 175cc87192..dcc6929696 100644 --- a/src/bhyve/bhyve_monitor.h +++ b/src/bhyve/bhyve_monitor.h @@ -24,7 +24,8 @@ #include "domain_conf.h" #include "bhyve_utils.h" -typedef struct _bhyveMonitor bhyveMonitor; +#define BHYVE_TYPE_MONITOR bhyve_monitor_get_type() +G_DECLARE_FINAL_TYPE(bhyveMonitor, bhyve_monitor, BHYVE, MONITOR, GObject); typedef bhyveMonitor *bhyveMonitorPtr; bhyveMonitorPtr bhyveMonitorOpen(virDomainObjPtr vm, bhyveConnPtr driver); -- 2.25.3