src: add support for g_autoptr with virObject instances

Libvirt currently uses the VIR_AUTOUNREF macro for auto cleanup of
virObject instances. GLib approaches things differently with GObject,
reusing their g_autoptr() concept.

This introduces support for g_autoptr() with virObject, to facilitate
the conversion to GObject.

Only virObject classes which are currently used with VIR_AUTOREF are
updated. Any others should be converted to GObject before introducing
use of autocleanup.

Reviewed-by: Ján Tomko <jtomko@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrangé 2019-10-04 17:14:10 +01:00
parent 44e7f02915
commit 667ff797e8
21 changed files with 73 additions and 0 deletions

View File

@ -1046,6 +1046,11 @@ BAD:
g_autoptr, g_auto on an object whose cleanup function g_autoptr, g_auto on an object whose cleanup function
is declared with the libvirt macros and vice-verca. is declared with the libvirt macros and vice-verca.
</dd> </dd>
<dt>VIR_AUTOUNREF</dt>
<dd>The GLib macros g_autoptr and G_DEFINE_AUTOPTR_CLEANUP_FUNC
should be used to manage autoclean of virObject classes.
This matches usage with GObject classes.</dd>
</dl> </dl>
<h2><a id="file_handling">File handling</a></h2> <h2><a id="file_handling">File handling</a></h2>

View File

@ -195,6 +195,9 @@ struct _virCaps {
virCapsStoragePoolPtr *pools; virCapsStoragePoolPtr *pools;
}; };
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virCaps, virObjectUnref);
struct _virCapsDomainData { struct _virCapsDomainData {
int ostype; int ostype;
int arch; int arch;

View File

@ -185,6 +185,9 @@ struct _virDomainCaps {
/* add new domain features here */ /* add new domain features here */
}; };
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virDomainCaps, virObjectUnref);
virDomainCapsPtr virDomainCapsNew(const char *path, virDomainCapsPtr virDomainCapsNew(const char *path,
const char *machine, const char *machine,
virArch arch, virArch arch,

View File

@ -2594,6 +2594,9 @@ struct _virDomainObj {
* restore will be required later */ * restore will be required later */
}; };
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virDomainObj, virObjectUnref);
typedef bool (*virDomainObjListACLFilter)(virConnectPtr conn, typedef bool (*virDomainObjListACLFilter)(virConnectPtr conn,
virDomainDefPtr def); virDomainDefPtr def);

View File

@ -87,6 +87,9 @@ struct _virDomainSnapshotDef {
virObjectPtr cookie; virObjectPtr cookie;
}; };
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virDomainSnapshotDef, virObjectUnref);
typedef enum { typedef enum {
VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE = 1 << 0, VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE = 1 << 0,
VIR_DOMAIN_SNAPSHOT_PARSE_DISKS = 1 << 1, VIR_DOMAIN_SNAPSHOT_PARSE_DISKS = 1 << 1,

View File

@ -30,6 +30,9 @@ struct _virStoragePoolCaps {
virCapsPtr driverCaps; virCapsPtr driverCaps;
}; };
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virStoragePoolCaps, virObjectUnref);
virStoragePoolCapsPtr virStoragePoolCapsPtr
virStoragePoolCapsNew(virCapsPtr driverCaps); virStoragePoolCapsNew(virCapsPtr driverCaps);

View File

@ -548,6 +548,9 @@ struct _virConnect {
void *userData; /* the user data */ void *userData; /* the user data */
}; };
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virConnect, virObjectUnref);
/** /**
* _virAdmConnect: * _virAdmConnect:
* *
@ -616,6 +619,9 @@ struct _virNetwork {
unsigned char uuid[VIR_UUID_BUFLEN]; /* the network unique identifier */ unsigned char uuid[VIR_UUID_BUFLEN]; /* the network unique identifier */
}; };
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virNetwork, virObjectUnref);
/** /**
* _virNetworkPort: * _virNetworkPort:
* *
@ -627,6 +633,9 @@ struct _virNetworkPort {
unsigned char uuid[VIR_UUID_BUFLEN]; /* the network unique identifier */ unsigned char uuid[VIR_UUID_BUFLEN]; /* the network unique identifier */
}; };
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virNetworkPort, virObjectUnref);
/** /**
* _virInterface: * _virInterface:
* *
@ -658,6 +667,9 @@ struct _virStoragePool {
virFreeCallback privateDataFreeFunc; virFreeCallback privateDataFreeFunc;
}; };
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virStoragePool, virObjectUnref);
/** /**
* _virStorageVol: * _virStorageVol:
* *
@ -678,6 +690,9 @@ struct _virStorageVol {
virFreeCallback privateDataFreeFunc; virFreeCallback privateDataFreeFunc;
}; };
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virStorageVol, virObjectUnref);
/** /**
* _virNodeDevice: * _virNodeDevice:
* *

View File

@ -102,6 +102,8 @@ struct _libxlDriverConfig {
size_t nfirmwares; size_t nfirmwares;
}; };
G_DEFINE_AUTOPTR_CLEANUP_FUNC(libxlDriverConfig, virObjectUnref);
struct _libxlDriverPrivate { struct _libxlDriverPrivate {
virMutex lock; virMutex lock;

View File

@ -134,6 +134,7 @@ struct _qemuBlockJobData {
bool invalidData; /* the job data (except name) is not valid */ bool invalidData; /* the job data (except name) is not valid */
bool reconnected; /* internal field for tracking whether job is live after reconnect to qemu */ bool reconnected; /* internal field for tracking whether job is live after reconnect to qemu */
}; };
G_DEFINE_AUTOPTR_CLEANUP_FUNC(qemuBlockJobData, virObjectUnref);
int int
qemuBlockJobRegister(qemuBlockJobDataPtr job, qemuBlockJobRegister(qemuBlockJobDataPtr job,

View File

@ -535,6 +535,8 @@ typedef enum { /* virQEMUCapsFlags grouping marker for syntax-check */
typedef struct _virQEMUCaps virQEMUCaps; typedef struct _virQEMUCaps virQEMUCaps;
typedef virQEMUCaps *virQEMUCapsPtr; typedef virQEMUCaps *virQEMUCapsPtr;
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virQEMUCaps, virObjectUnref);
virQEMUCapsPtr virQEMUCapsNew(void); virQEMUCapsPtr virQEMUCapsNew(void);
void virQEMUCapsSet(virQEMUCapsPtr qemuCaps, void virQEMUCapsSet(virQEMUCapsPtr qemuCaps,

View File

@ -218,6 +218,9 @@ struct _virQEMUDriverConfig {
char **capabilityfilters; char **capabilityfilters;
}; };
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virQEMUDriverConfig, virObjectUnref);
/* Main driver state */ /* Main driver state */
struct _virQEMUDriver { struct _virQEMUDriver {
virMutex lock; virMutex lock;

View File

@ -55,6 +55,9 @@ struct _virHostdevManager {
virMediatedDeviceListPtr activeMediatedHostdevs; virMediatedDeviceListPtr activeMediatedHostdevs;
}; };
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virHostdevManager, virObjectUnref);
virHostdevManagerPtr virHostdevManagerGetDefault(void); virHostdevManagerPtr virHostdevManagerGetDefault(void);
int int
virHostdevPreparePCIDevices(virHostdevManagerPtr hostdev_mgr, virHostdevPreparePCIDevices(virHostdevManagerPtr hostdev_mgr,

View File

@ -26,6 +26,8 @@
typedef struct _virIdentity virIdentity; typedef struct _virIdentity virIdentity;
typedef virIdentity *virIdentityPtr; typedef virIdentity *virIdentityPtr;
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virIdentity, virObjectUnref);
virIdentityPtr virIdentityGetCurrent(void); virIdentityPtr virIdentityGetCurrent(void);
int virIdentitySetCurrent(virIdentityPtr ident); int virIdentitySetCurrent(virIdentityPtr ident);

View File

@ -40,6 +40,9 @@ typedef virMediatedDevice *virMediatedDevicePtr;
typedef struct _virMediatedDeviceList virMediatedDeviceList; typedef struct _virMediatedDeviceList virMediatedDeviceList;
typedef virMediatedDeviceList *virMediatedDeviceListPtr; typedef virMediatedDeviceList *virMediatedDeviceListPtr;
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virMediatedDeviceList, virObjectUnref);
typedef struct _virMediatedDeviceType virMediatedDeviceType; typedef struct _virMediatedDeviceType virMediatedDeviceType;
typedef virMediatedDeviceType *virMediatedDeviceTypePtr; typedef virMediatedDeviceType *virMediatedDeviceTypePtr;
struct _virMediatedDeviceType { struct _virMediatedDeviceType {

View File

@ -116,12 +116,16 @@ virObjectAutoUnref(void *objptr);
* VIR_AUTOUNREF: * VIR_AUTOUNREF:
* @type: type of an virObject subclass to be unref'd automatically * @type: type of an virObject subclass to be unref'd automatically
* *
* DEPRECATED: Use g_autoptr(type) instead
*
* Declares a variable of @type which will be automatically unref'd when * Declares a variable of @type which will be automatically unref'd when
* control goes out of the scope. * control goes out of the scope.
*/ */
#define VIR_AUTOUNREF(type) \ #define VIR_AUTOUNREF(type) \
__attribute__((cleanup(virObjectAutoUnref))) type __attribute__((cleanup(virObjectAutoUnref))) type
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virObject, virObjectUnref);
void * void *
virObjectRef(void *obj); virObjectRef(void *obj);

View File

@ -34,6 +34,9 @@ typedef virPCIDeviceAddress *virPCIDeviceAddressPtr;
typedef struct _virPCIDeviceList virPCIDeviceList; typedef struct _virPCIDeviceList virPCIDeviceList;
typedef virPCIDeviceList *virPCIDeviceListPtr; typedef virPCIDeviceList *virPCIDeviceListPtr;
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virPCIDeviceList, virObjectUnref);
#define VIR_DOMAIN_DEVICE_ZPCI_MAX_UID UINT16_MAX #define VIR_DOMAIN_DEVICE_ZPCI_MAX_UID UINT16_MAX
#define VIR_DOMAIN_DEVICE_ZPCI_MAX_FID UINT32_MAX #define VIR_DOMAIN_DEVICE_ZPCI_MAX_FID UINT32_MAX

View File

@ -20,6 +20,7 @@
#include "internal.h" #include "internal.h"
#include "virobject.h"
#include "virbitmap.h" #include "virbitmap.h"
#include "virutil.h" #include "virutil.h"
#include "virenum.h" #include "virenum.h"
@ -114,6 +115,9 @@ virResctrlInfoGetMemoryBandwidth(virResctrlInfoPtr resctrl,
typedef struct _virResctrlAlloc virResctrlAlloc; typedef struct _virResctrlAlloc virResctrlAlloc;
typedef virResctrlAlloc *virResctrlAllocPtr; typedef virResctrlAlloc *virResctrlAllocPtr;
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virResctrlAlloc, virObjectUnref);
typedef int virResctrlAllocForeachCacheCallback(unsigned int level, typedef int virResctrlAllocForeachCacheCallback(unsigned int level,
virCacheType type, virCacheType type,
unsigned int cache, unsigned int cache,

View File

@ -30,6 +30,9 @@ typedef virSCSIDevice *virSCSIDevicePtr;
typedef struct _virSCSIDeviceList virSCSIDeviceList; typedef struct _virSCSIDeviceList virSCSIDeviceList;
typedef virSCSIDeviceList *virSCSIDeviceListPtr; typedef virSCSIDeviceList *virSCSIDeviceListPtr;
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virSCSIDeviceList, virObjectUnref);
char *virSCSIDeviceGetSgName(const char *sysfs_prefix, char *virSCSIDeviceGetSgName(const char *sysfs_prefix,
const char *adapter, const char *adapter,
unsigned int bus, unsigned int bus,

View File

@ -30,6 +30,9 @@ typedef virSCSIVHostDevice *virSCSIVHostDevicePtr;
typedef struct _virSCSIVHostDeviceList virSCSIVHostDeviceList; typedef struct _virSCSIVHostDeviceList virSCSIVHostDeviceList;
typedef virSCSIVHostDeviceList *virSCSIVHostDeviceListPtr; typedef virSCSIVHostDeviceList *virSCSIVHostDeviceListPtr;
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virSCSIVHostDeviceList, virObjectUnref);
typedef int (*virSCSIVHostDeviceFileActor)(virSCSIVHostDevicePtr dev, typedef int (*virSCSIVHostDeviceFileActor)(virSCSIVHostDevicePtr dev,
const char *name, void *opaque); const char *name, void *opaque);

View File

@ -344,6 +344,8 @@ struct _virStorageSource {
bool hostcdrom; /* backing device is a cdrom */ bool hostcdrom; /* backing device is a cdrom */
}; };
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virStorageSource, virObjectUnref);
#ifndef DEV_BSIZE #ifndef DEV_BSIZE
# define DEV_BSIZE 512 # define DEV_BSIZE 512

View File

@ -31,6 +31,9 @@ typedef virUSBDevice *virUSBDevicePtr;
typedef struct _virUSBDeviceList virUSBDeviceList; typedef struct _virUSBDeviceList virUSBDeviceList;
typedef virUSBDeviceList *virUSBDeviceListPtr; typedef virUSBDeviceList *virUSBDeviceListPtr;
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virUSBDeviceList, virObjectUnref);
virUSBDevicePtr virUSBDeviceNew(unsigned int bus, virUSBDevicePtr virUSBDeviceNew(unsigned int bus,
unsigned int devno, unsigned int devno,
const char *vroot); const char *vroot);