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
is declared with the libvirt macros and vice-verca.
</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>
<h2><a id="file_handling">File handling</a></h2>

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -116,12 +116,16 @@ virObjectAutoUnref(void *objptr);
* VIR_AUTOUNREF:
* @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
* control goes out of the scope.
*/
#define VIR_AUTOUNREF(type) \
__attribute__((cleanup(virObjectAutoUnref))) type
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virObject, virObjectUnref);
void *
virObjectRef(void *obj);

View File

@ -34,6 +34,9 @@ typedef virPCIDeviceAddress *virPCIDeviceAddressPtr;
typedef struct _virPCIDeviceList virPCIDeviceList;
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_FID UINT32_MAX

View File

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

View File

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

View File

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

View File

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

View File

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