Signed-off-by: Rafael Fonseca <r4f4rfs@xxxxxxxxx> --- src/network/bridge_driver.c | 21 ++++++-------- src/util/virdnsmasq.c | 56 +++++++++++++++++-------------------- src/util/virdnsmasq.h | 6 ++-- tests/networkxml2conftest.c | 10 ++----- 4 files changed, 41 insertions(+), 52 deletions(-) diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c index f06099297a..6148bf6a7e 100644 --- a/src/network/bridge_driver.c +++ b/src/network/bridge_driver.c @@ -117,7 +117,7 @@ networkGetDnsmasqCaps(virNetworkDriverStatePtr driver) { dnsmasqCapsPtr ret; networkDriverLock(driver); - ret = virObjectRef(driver->dnsmasqCaps); + ret = g_object_ref(driver->dnsmasqCaps); networkDriverUnlock(driver); return ret; } @@ -132,7 +132,8 @@ networkDnsmasqCapsRefresh(virNetworkDriverStatePtr driver) return -1; networkDriverLock(driver); - virObjectUnref(driver->dnsmasqCaps); + if (driver->dnsmasqCaps) + g_object_unref(driver->dnsmasqCaps); driver->dnsmasqCaps = caps; networkDriverUnlock(driver); return 0; @@ -543,7 +544,7 @@ networkUpdateState(virNetworkObjPtr obj, { virNetworkDefPtr def; virNetworkDriverStatePtr driver = opaque; - dnsmasqCapsPtr dnsmasq_caps = networkGetDnsmasqCaps(driver); + g_autoptr(dnsmasqCaps) dnsmasq_caps = networkGetDnsmasqCaps(driver); virMacMapPtr macmap; char *macMapFile = NULL; int ret = -1; @@ -629,7 +630,6 @@ networkUpdateState(virNetworkObjPtr obj, ret = 0; cleanup: virObjectUnlock(obj); - virObjectUnref(dnsmasq_caps); VIR_FREE(macMapFile); return ret; } @@ -905,7 +905,8 @@ networkStateCleanup(void) VIR_FREE(network_driver->dnsmasqStateDir); VIR_FREE(network_driver->radvdStateDir); - virObjectUnref(network_driver->dnsmasqCaps); + if (network_driver->dnsmasqCaps) + g_object_unref(network_driver->dnsmasqCaps); virMutexDestroy(&network_driver->lock); @@ -1544,7 +1545,7 @@ networkBuildDhcpDaemonCommandLine(virNetworkDriverStatePtr driver, dnsmasqContext *dctx) { virNetworkDefPtr def = virNetworkObjGetDef(obj); - dnsmasqCapsPtr dnsmasq_caps = networkGetDnsmasqCaps(driver); + g_autoptr(dnsmasqCaps) dnsmasq_caps = networkGetDnsmasqCaps(driver); virCommandPtr cmd = NULL; int ret = -1; char *configfile = NULL; @@ -1587,7 +1588,6 @@ networkBuildDhcpDaemonCommandLine(virNetworkDriverStatePtr driver, *cmdout = cmd; ret = 0; cleanup: - virObjectUnref(dnsmasq_caps); VIR_FREE(configfile); VIR_FREE(configstr); VIR_FREE(leaseshelper_path); @@ -1911,7 +1911,7 @@ networkStartRadvd(virNetworkDriverStatePtr driver, virNetworkObjPtr obj) { virNetworkDefPtr def = virNetworkObjGetDef(obj); - dnsmasqCapsPtr dnsmasq_caps = networkGetDnsmasqCaps(driver); + g_autoptr(dnsmasqCaps) dnsmasq_caps = networkGetDnsmasqCaps(driver); pid_t radvdPid; char *pidfile = NULL; char *radvdpidbase = NULL; @@ -1989,7 +1989,6 @@ networkStartRadvd(virNetworkDriverStatePtr driver, ret = 0; cleanup: - virObjectUnref(dnsmasq_caps); virCommandFree(cmd); VIR_FREE(configfile); VIR_FREE(radvdpidbase); @@ -2003,14 +2002,13 @@ networkRefreshRadvd(virNetworkDriverStatePtr driver, virNetworkObjPtr obj) { virNetworkDefPtr def = virNetworkObjGetDef(obj); - dnsmasqCapsPtr dnsmasq_caps = networkGetDnsmasqCaps(driver); + g_autoptr(dnsmasqCaps) dnsmasq_caps = networkGetDnsmasqCaps(driver); g_autofree char *radvdpidbase = NULL; g_autofree char *pidfile = NULL; pid_t radvdPid; /* Is dnsmasq handling RA? */ if (DNSMASQ_RA_SUPPORT(dnsmasq_caps)) { - virObjectUnref(dnsmasq_caps); if ((radvdpidbase = networkRadvdPidfileBasename(def->name)) && (pidfile = virPidFileBuildPath(driver->pidDir, radvdpidbase))) { /* radvd should not be running but in case it is */ @@ -2019,7 +2017,6 @@ networkRefreshRadvd(virNetworkDriverStatePtr driver, } return 0; } - virObjectUnref(dnsmasq_caps); /* if there's no running radvd, just start it */ radvdPid = virNetworkObjGetRadvdPid(obj); diff --git a/src/util/virdnsmasq.c b/src/util/virdnsmasq.c index adc6f96bb6..a8fb22a368 100644 --- a/src/util/virdnsmasq.c +++ b/src/util/virdnsmasq.c @@ -598,7 +598,7 @@ dnsmasqReload(pid_t pid G_GNUC_UNUSED) * */ struct _dnsmasqCaps { - virObject parent; + GObject parent; char *binaryPath; bool noRefresh; time_t mtime; @@ -606,26 +606,31 @@ struct _dnsmasqCaps { unsigned long version; }; -static virClassPtr dnsmasqCapsClass; +G_DEFINE_TYPE(dnsmasqCaps, dnsmasq_caps, G_TYPE_OBJECT); static void -dnsmasqCapsDispose(void *obj) +dnsmasqCapsFinalize(GObject *obj) { - dnsmasqCapsPtr caps = obj; + dnsmasqCapsPtr caps = DNSMASQ_CAPS(obj); virBitmapFree(caps->flags); VIR_FREE(caps->binaryPath); + + G_OBJECT_CLASS(dnsmasq_caps_parent_class)->finalize(obj); } -static int dnsmasqCapsOnceInit(void) +static void +dnsmasq_caps_init(dnsmasqCaps *caps G_GNUC_UNUSED) { - if (!VIR_CLASS_NEW(dnsmasqCaps, virClassForObject())) - return -1; - - return 0; } -VIR_ONCE_GLOBAL_INIT(dnsmasqCaps); +static void +dnsmasq_caps_class_init(dnsmasqCapsClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->finalize = dnsmasqCapsFinalize; +} static void dnsmasqCapsSet(dnsmasqCapsPtr caps, @@ -764,65 +769,54 @@ dnsmasqCapsRefreshInternal(dnsmasqCapsPtr caps, bool force) static dnsmasqCapsPtr dnsmasqCapsNewEmpty(const char *binaryPath) { - dnsmasqCapsPtr caps; - - if (dnsmasqCapsInitialize() < 0) - return NULL; - if (!(caps = virObjectNew(dnsmasqCapsClass))) - return NULL; + g_autoptr(dnsmasqCaps) caps = + DNSMASQ_CAPS(g_object_new(DNSMASQ_TYPE_CAPS, NULL)); if (!(caps->flags = virBitmapNew(DNSMASQ_CAPS_LAST))) - goto error; + return NULL; caps->binaryPath = g_strdup(binaryPath ? binaryPath : DNSMASQ); - return caps; - - error: - virObjectUnref(caps); - return NULL; + return g_steal_pointer(&caps); } dnsmasqCapsPtr dnsmasqCapsNewFromBuffer(const char *buf, const char *binaryPath) { - dnsmasqCapsPtr caps = dnsmasqCapsNewEmpty(binaryPath); + g_autoptr(dnsmasqCaps) caps = dnsmasqCapsNewEmpty(binaryPath); if (!caps) return NULL; if (dnsmasqCapsSetFromBuffer(caps, buf) < 0) { - virObjectUnref(caps); return NULL; } - return caps; + return g_steal_pointer(&caps); } dnsmasqCapsPtr dnsmasqCapsNewFromFile(const char *dataPath, const char *binaryPath) { - dnsmasqCapsPtr caps = dnsmasqCapsNewEmpty(binaryPath); + g_autoptr(dnsmasqCaps) caps = dnsmasqCapsNewEmpty(binaryPath); if (!caps) return NULL; if (dnsmasqCapsSetFromFile(caps, dataPath) < 0) { - virObjectUnref(caps); return NULL; } - return caps; + return g_steal_pointer(&caps); } dnsmasqCapsPtr dnsmasqCapsNewFromBinary(const char *binaryPath) { - dnsmasqCapsPtr caps = dnsmasqCapsNewEmpty(binaryPath); + g_autoptr(dnsmasqCaps) caps = dnsmasqCapsNewEmpty(binaryPath); if (!caps) return NULL; if (dnsmasqCapsRefreshInternal(caps, true) < 0) { - virObjectUnref(caps); return NULL; } - return caps; + return g_steal_pointer(&caps); } /** dnsmasqCapsRefresh: diff --git a/src/util/virdnsmasq.h b/src/util/virdnsmasq.h index ff0e56d635..9cbbd5562d 100644 --- a/src/util/virdnsmasq.h +++ b/src/util/virdnsmasq.h @@ -23,7 +23,8 @@ #pragma once -#include "virobject.h" +#include "internal.h" +#include <glib-object.h> #include "virsocketaddr.h" typedef struct @@ -75,7 +76,8 @@ typedef enum { DNSMASQ_CAPS_LAST, /* this must always be the last item */ } dnsmasqCapsFlags; -typedef struct _dnsmasqCaps dnsmasqCaps; +#define DNSMASQ_TYPE_CAPS dnsmasq_caps_get_type() +G_DECLARE_FINAL_TYPE(dnsmasqCaps, dnsmasq_caps, DNSMASQ, CAPS, GObject); typedef dnsmasqCaps *dnsmasqCapsPtr; diff --git a/tests/networkxml2conftest.c b/tests/networkxml2conftest.c index a8355272b9..ed3a2cf6ae 100644 --- a/tests/networkxml2conftest.c +++ b/tests/networkxml2conftest.c @@ -101,11 +101,11 @@ static int mymain(void) { int ret = 0; - dnsmasqCapsPtr restricted + g_autoptr(dnsmasqCaps) restricted = dnsmasqCapsNewFromBuffer("Dnsmasq version 2.48", DNSMASQ); - dnsmasqCapsPtr full + g_autoptr(dnsmasqCaps) full = dnsmasqCapsNewFromBuffer("Dnsmasq version 2.63\n--bind-dynamic", DNSMASQ); - dnsmasqCapsPtr dhcpv6 + g_autoptr(dnsmasqCaps) dhcpv6 = dnsmasqCapsNewFromBuffer("Dnsmasq version 2.64\n--bind-dynamic", DNSMASQ); #define DO_TEST(xname, xcaps) \ @@ -143,10 +143,6 @@ mymain(void) DO_TEST("ptr-domains-auto", dhcpv6); DO_TEST("dnsmasq-options", dhcpv6); - virObjectUnref(dhcpv6); - virObjectUnref(full); - virObjectUnref(restricted); - return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } -- 2.25.1