mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-23 21:15:20 +00:00
util: buffer: define cleanup function using VIR_DEFINE_AUTOPTR_FUNC
Using the new VIR_DEFINE_AUTOPTR_FUNC macro defined in src/util/viralloc.h, define a new wrapper around an existing cleanup function which will be called when a variable declared with VIR_AUTOPTR macro goes out of scope. Also, drop the redundant viralloc.h include, since that has moved from the source module into the header. When variables of type virBufferPtr and virBufferEscapePairPtr are declared using VIR_AUTOPTR, the functions virBufferFreeAndReset and virBufferEscapePairFree, respectively, will be run automatically on them when they go out of scope. Signed-off-by: Sukrit Bhatnagar <skrtbhtngr@gmail.com> Reviewed-by: Erik Skultety <eskultet@redhat.com>
This commit is contained in:
parent
d261ed2fb1
commit
96fbf6df90
@ -31,10 +31,10 @@
|
|||||||
#define __VIR_BUFFER_C__
|
#define __VIR_BUFFER_C__
|
||||||
|
|
||||||
#include "virbuffer.h"
|
#include "virbuffer.h"
|
||||||
#include "viralloc.h"
|
|
||||||
#include "virerror.h"
|
#include "virerror.h"
|
||||||
#include "virstring.h"
|
#include "virstring.h"
|
||||||
|
|
||||||
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
||||||
|
|
||||||
/* If adding more fields, ensure to edit buf.h to match
|
/* If adding more fields, ensure to edit buf.h to match
|
||||||
the number of fields */
|
the number of fields */
|
||||||
@ -656,6 +656,19 @@ struct _virBufferEscapePair {
|
|||||||
char *toescape;
|
char *toescape;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static void
|
||||||
|
virBufferEscapePairFree(virBufferEscapePairPtr pair)
|
||||||
|
{
|
||||||
|
if (!pair)
|
||||||
|
return;
|
||||||
|
|
||||||
|
VIR_FREE(pair->toescape);
|
||||||
|
VIR_FREE(pair);
|
||||||
|
}
|
||||||
|
|
||||||
|
VIR_DEFINE_AUTOPTR_FUNC(virBufferEscapePair, virBufferEscapePairFree)
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* virBufferEscapeN:
|
* virBufferEscapeN:
|
||||||
* @buf: the buffer to append to
|
* @buf: the buffer to append to
|
||||||
@ -696,7 +709,7 @@ virBufferEscapeN(virBufferPtr buf,
|
|||||||
va_start(ap, str);
|
va_start(ap, str);
|
||||||
|
|
||||||
while ((escapeItem.escape = va_arg(ap, int))) {
|
while ((escapeItem.escape = va_arg(ap, int))) {
|
||||||
if (!(escapeItem.toescape = va_arg(ap, char *))) {
|
if (VIR_STRDUP(escapeItem.toescape, va_arg(ap, char *)) < 0) {
|
||||||
virBufferSetError(buf, errno);
|
virBufferSetError(buf, errno);
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
@ -23,10 +23,12 @@
|
|||||||
#ifndef __VIR_BUFFER_H__
|
#ifndef __VIR_BUFFER_H__
|
||||||
# define __VIR_BUFFER_H__
|
# define __VIR_BUFFER_H__
|
||||||
|
|
||||||
# include "internal.h"
|
|
||||||
|
|
||||||
# include <stdarg.h>
|
# include <stdarg.h>
|
||||||
|
|
||||||
|
# include "internal.h"
|
||||||
|
# include "viralloc.h"
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* virBuffer:
|
* virBuffer:
|
||||||
*
|
*
|
||||||
@ -119,4 +121,6 @@ int virBufferGetIndent(const virBuffer *buf, bool dynamic);
|
|||||||
void virBufferTrim(virBufferPtr buf, const char *trim, int len);
|
void virBufferTrim(virBufferPtr buf, const char *trim, int len);
|
||||||
void virBufferAddStr(virBufferPtr buf, const char *str);
|
void virBufferAddStr(virBufferPtr buf, const char *str);
|
||||||
|
|
||||||
|
VIR_DEFINE_AUTOPTR_FUNC(virBuffer, virBufferFreeAndReset)
|
||||||
|
|
||||||
#endif /* __VIR_BUFFER_H__ */
|
#endif /* __VIR_BUFFER_H__ */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user