util: Move the VIR_AUTO(CLEAN|PTR) helper macros into a separate header

Keeping them with viralloc.h forcibly pulls in the other stuff from
viralloc.h into other header files. This in turn creates a mess
as more and more headers pull in the 'viral' header file.

If we want to make 'viralloc.h' omnipresent we should pick a different
approach.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2019-04-01 15:14:30 +02:00
parent 9895f00126
commit a4bfc2521f
31 changed files with 120 additions and 66 deletions

View File

@ -662,6 +662,7 @@ libvirt_setuid_rpc_client_la_SOURCES = \
util/virarch.c \
util/viratomic.c \
util/viratomic.h \
util/virautoclean.h \
util/virbitmap.c \
util/virbuffer.c \
util/vircgroup.c \
@ -874,6 +875,7 @@ libvirt_nss_la_SOURCES = \
util/viralloc.h \
util/viratomic.c \
util/viratomic.h \
util/virautoclean.h \
util/virbitmap.c \
util/virbitmap.h \
util/virbuffer.c \

View File

@ -24,6 +24,7 @@
# include "internal.h"
# include "domain_conf.h"
# include "viralloc.h"
# include "virautoclean.h"
typedef const char * (*virDomainCapsValToStr)(int value);

View File

@ -30,6 +30,7 @@
# include "device_conf.h"
# include "object_event.h"
# include "storage_adapter_conf.h"
# include "virautoclean.h"
# include <libxml/tree.h>

View File

@ -24,6 +24,7 @@
# include "domain_conf.h"
# include "viralloc.h"
# include "qemu_conf.h"
# include "virautoclean.h"
typedef struct _qemuFirmware qemuFirmware;
typedef qemuFirmware *qemuFirmwarePtr;

View File

@ -28,6 +28,7 @@
# include "virxml.h"
# include "qemu_monitor.h"
# include "qemu_conf.h"
# include "virautoclean.h"
typedef enum {
QEMU_MIGRATION_CAP_XBZRLE,

View File

@ -15,6 +15,7 @@ UTIL_SOURCES = \
util/virauth.h \
util/virauthconfig.c \
util/virauthconfig.h \
util/virautoclean.h \
util/virbitmap.c \
util/virbitmap.h \
util/virbuffer.c \

View File

@ -606,42 +606,6 @@ int virAllocTestCount(void);
void virAllocTestOOM(int n, int m);
void virAllocTestHook(void (*func)(int, void*), void *data);
# define VIR_AUTOPTR_FUNC_NAME(type) type##AutoPtrFree
/**
* VIR_DEFINE_AUTOPTR_FUNC:
* @type: type of the variable to be freed automatically
* @func: cleanup function to be automatically called
*
* This macro defines a function for automatic freeing of
* resources allocated to a variable of type @type. This newly
* defined function works as a necessary wrapper around @func.
*/
# define VIR_DEFINE_AUTOPTR_FUNC(type, func) \
static inline void VIR_AUTOPTR_FUNC_NAME(type)(type **_ptr) \
{ \
if (*_ptr) \
(func)(*_ptr); \
*_ptr = NULL; \
}
# define VIR_AUTOCLEAN_FUNC_NAME(type) type##AutoClean
/**
* VIR_DEFINE_AUTOCLEAN_FUNC:
* @type: type of the variable to be cleared automatically
* @func: cleanup function to be automatically called
*
* This macro defines a function for automatic clearing of
* resources in a stack'd variable of type @type. Note that @func must
* take pointer to @type.
*/
# define VIR_DEFINE_AUTOCLEAN_FUNC(type, func) \
static inline void VIR_AUTOCLEAN_FUNC_NAME(type)(type *_ptr) \
{ \
(func)(_ptr); \
}
/**
* VIR_AUTOFREE:
* @type: type of the variable to be freed automatically
@ -652,34 +616,4 @@ void virAllocTestHook(void (*func)(int, void*), void *data);
*/
# define VIR_AUTOFREE(type) __attribute__((cleanup(virFree))) type
/**
* VIR_AUTOPTR:
* @type: type of the variable to be freed automatically
*
* Macro to automatically free the memory allocated to
* the variable declared with it by calling the function
* defined by VIR_DEFINE_AUTOPTR_FUNC when the variable
* goes out of scope.
*
* Note that this macro must NOT be used with vectors! The freeing function
* will not free any elements beyond the first.
*/
# define VIR_AUTOPTR(type) \
__attribute__((cleanup(VIR_AUTOPTR_FUNC_NAME(type)))) type *
/**
* VIR_AUTOCLEAN:
* @type: type of the variable to be cleared automatically
*
* Macro to automatically call clearing function registered for variable of @type
* when the variable goes out of scope.
* The cleanup function is registered by VIR_DEFINE_AUTOCLEAN_FUNC macro for
* the given type.
*
* Note that this macro must NOT be used with vectors! The cleaning function
* will not clean any elements beyond the first.
*/
# define VIR_AUTOCLEAN(type) \
__attribute__((cleanup(VIR_AUTOCLEAN_FUNC_NAME(type)))) type
#endif /* LIBVIRT_VIRALLOC_H */

View File

@ -23,6 +23,7 @@
# include "internal.h"
# include "viralloc.h"
# include "virautoclean.h"
typedef struct _virAuthConfig virAuthConfig;
typedef virAuthConfig *virAuthConfigPtr;

90
src/util/virautoclean.h Normal file
View File

@ -0,0 +1,90 @@
/*
* virautoclean.h: automatic scope-based memory clearing helper macros for
* use in header files
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#ifndef LIBVIRT_VIRAUTOCLEAN_H
# define LIBVIRT_VIRAUTOCLEAN_H
# define VIR_AUTOPTR_FUNC_NAME(type) type##AutoPtrFree
/**
* VIR_DEFINE_AUTOPTR_FUNC:
* @type: type of the variable to be freed automatically
* @func: cleanup function to be automatically called
*
* This macro defines a function for automatic freeing of
* resources allocated to a variable of type @type. This newly
* defined function works as a necessary wrapper around @func.
*/
# define VIR_DEFINE_AUTOPTR_FUNC(type, func) \
static inline void VIR_AUTOPTR_FUNC_NAME(type)(type **_ptr) \
{ \
if (*_ptr) \
(func)(*_ptr); \
*_ptr = NULL; \
}
# define VIR_AUTOCLEAN_FUNC_NAME(type) type##AutoClean
/**
* VIR_DEFINE_AUTOCLEAN_FUNC:
* @type: type of the variable to be cleared automatically
* @func: cleanup function to be automatically called
*
* This macro defines a function for automatic clearing of
* resources in a stack'd variable of type @type. Note that @func must
* take pointer to @type.
*/
# define VIR_DEFINE_AUTOCLEAN_FUNC(type, func) \
static inline void VIR_AUTOCLEAN_FUNC_NAME(type)(type *_ptr) \
{ \
(func)(_ptr); \
}
/**
* VIR_AUTOPTR:
* @type: type of the variable to be freed automatically
*
* Macro to automatically free the memory allocated to
* the variable declared with it by calling the function
* defined by VIR_DEFINE_AUTOPTR_FUNC when the variable
* goes out of scope.
*
* Note that this macro must NOT be used with vectors! The freeing function
* will not free any elements beyond the first.
*/
# define VIR_AUTOPTR(type) \
__attribute__((cleanup(VIR_AUTOPTR_FUNC_NAME(type)))) type *
/**
* VIR_AUTOCLEAN:
* @type: type of the variable to be cleared automatically
*
* Macro to automatically call clearing function registered for variable of @type
* when the variable goes out of scope.
* The cleanup function is registered by VIR_DEFINE_AUTOCLEAN_FUNC macro for
* the given type.
*
* Note that this macro must NOT be used with vectors! The cleaning function
* will not clean any elements beyond the first.
*/
# define VIR_AUTOCLEAN(type) \
__attribute__((cleanup(VIR_AUTOCLEAN_FUNC_NAME(type)))) type
#endif /* LIBVIRT_VIRAUTOCLEAN_H */

View File

@ -24,6 +24,7 @@
# include "internal.h"
# include "viralloc.h"
# include "virautoclean.h"
# include <sys/types.h>

View File

@ -25,6 +25,7 @@
# include "internal.h"
# include "viralloc.h"
# include "virautoclean.h"
/**

View File

@ -25,6 +25,7 @@
# include "internal.h"
# include "virbuffer.h"
# include "viralloc.h"
# include "virautoclean.h"
typedef struct _virCommand virCommand;
typedef virCommand *virCommandPtr;

View File

@ -24,6 +24,7 @@
# include "internal.h"
# include "viralloc.h"
# include "virautoclean.h"
# define VIR_ERROR_MAX_LENGTH 1024

View File

@ -31,6 +31,7 @@
# include "virbitmap.h"
# include "virstoragefile.h"
# include "viralloc.h"
# include "virautoclean.h"
typedef enum {
VIR_FILE_CLOSE_PRESERVE_ERRNO = 1 << 0,

View File

@ -23,6 +23,7 @@
# include "internal.h"
# include "viralloc.h"
# include "virautoclean.h"
typedef struct _virFirewall virFirewall;
typedef virFirewall *virFirewallPtr;

View File

@ -12,6 +12,7 @@
# include "viralloc.h"
# include "virautoclean.h"
/*
* The hash table.

View File

@ -27,6 +27,7 @@
# include "virbitmap.h"
# include "viralloc.h"
# include "virbuffer.h"
# include "virautoclean.h"
# include <stdarg.h>

View File

@ -23,6 +23,7 @@
# include "internal.h"
# include "viralloc.h"
# include "virautoclean.h"
# define VIR_MAC_BUFLEN 6
# define VIR_MAC_HEXLEN (VIR_MAC_BUFLEN * 2)

View File

@ -23,6 +23,7 @@
# include "virobject.h"
# include "virutil.h"
# include "viralloc.h"
# include "virautoclean.h"
typedef enum {
VIR_MDEV_MODEL_TYPE_VFIO_PCI = 0,

View File

@ -27,6 +27,7 @@
# include "virpci.h"
# include "virnetdevvlan.h"
# include "viralloc.h"
# include "virautoclean.h"
# ifdef HAVE_STRUCT_IFREQ
typedef struct ifreq virIfreq;

View File

@ -20,6 +20,7 @@
# define LIBVIRT_VIRNETDEVIP_H
# include "virsocketaddr.h"
# include "virautoclean.h"
typedef struct _virNetDevIPAddr virNetDevIPAddr;
typedef virNetDevIPAddr *virNetDevIPAddrPtr;

View File

@ -22,6 +22,7 @@
# include <virutil.h>
# include "viralloc.h"
# include "virautoclean.h"
typedef enum {
VIR_NATIVE_VLAN_MODE_DEFAULT = 0,

View File

@ -23,6 +23,7 @@
# include "internal.h"
# include "virmacaddr.h"
# include "viralloc.h"
# include "virautoclean.h"
# if defined(__linux__) && defined(HAVE_LIBNL)

View File

@ -26,6 +26,7 @@
# include "virobject.h"
# include "virutil.h"
# include "viralloc.h"
# include "virautoclean.h"
typedef struct _virPCIDevice virPCIDevice;
typedef virPCIDevice *virPCIDevicePtr;

View File

@ -21,6 +21,7 @@
# include "virutil.h"
# include "viralloc.h"
# include "virautoclean.h"
/* Some Intel processor families introduced some RDT (Resource Director
* Technology) features to monitor or control shared resource based on

View File

@ -24,6 +24,7 @@
# include "internal.h"
# include "virobject.h"
# include "viralloc.h"
# include "virautoclean.h"
typedef struct _virSCSIDevice virSCSIDevice;
typedef virSCSIDevice *virSCSIDevicePtr;

View File

@ -25,6 +25,7 @@
# include "virobject.h"
# include "virutil.h"
# include "viralloc.h"
# include "virautoclean.h"
typedef struct _virSCSIVHostDevice virSCSIVHostDevice;
typedef virSCSIVHostDevice *virSCSIVHostDevicePtr;

View File

@ -27,6 +27,7 @@
# include "internal.h"
# include "viralloc.h"
# include "virautoclean.h"
/* On architectures which lack these limits, define them (ie. Cygwin).
* Note that the libvirt code should be robust enough to handle the

View File

@ -30,6 +30,7 @@
# include "virstorageencryption.h"
# include "virutil.h"
# include "virsecret.h"
# include "virautoclean.h"
/* Minimum header size required to probe all known formats with
* virStorageFileProbeFormat, or obtain metadata from a known format.

View File

@ -24,6 +24,7 @@
# include "internal.h"
# include "virobject.h"
# include "viralloc.h"
# include "virautoclean.h"
# define USB_DEVFS "/dev/bus/usb/"

View File

@ -29,6 +29,7 @@
# include <libxml/relaxng.h>
# include "virbuffer.h"
# include "virautoclean.h"
int virXPathBoolean(const char *xpath,
xmlXPathContextPtr ctxt);