virBitmapParseInternal: Allocate the bitmap in the caller

In order to prepare for reuse of the function, move the allocation of
the bitmap to the caller.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2021-12-06 16:18:57 +01:00
parent d8579b07c8
commit 515672c0d9

View File

@ -366,8 +366,7 @@ virBitmapFormat(virBitmap *bitmap)
/** /**
* virBitmapParseInternal: * virBitmapParseInternal:
* @str: points to a string representing a human-readable bitmap * @str: points to a string representing a human-readable bitmap
* @bitmap: a bitmap created from @str * @bitmap: a bitmap populated from @str
* @bitmapSize: the upper limit of num of bits in created bitmap
* *
* This function is the counterpart of virBitmapFormat. This function creates * This function is the counterpart of virBitmapFormat. This function creates
* a bitmap, in which bits are set according to the content of @str. * a bitmap, in which bits are set according to the content of @str.
@ -380,8 +379,7 @@ virBitmapFormat(virBitmap *bitmap)
*/ */
static int static int
virBitmapParseInternal(const char *str, virBitmapParseInternal(const char *str,
virBitmap **bitmap, virBitmap *bitmap)
size_t bitmapSize)
{ {
bool neg = false; bool neg = false;
const char *cur = str; const char *cur = str;
@ -389,8 +387,6 @@ virBitmapParseInternal(const char *str,
size_t i; size_t i;
int start, last; int start, last;
*bitmap = virBitmapNew(bitmapSize);
if (!str) if (!str)
goto error; goto error;
@ -425,10 +421,10 @@ virBitmapParseInternal(const char *str,
if (*cur == ',' || *cur == 0) { if (*cur == ',' || *cur == 0) {
if (neg) { if (neg) {
if (virBitmapClearBit(*bitmap, start) < 0) if (virBitmapClearBit(bitmap, start) < 0)
goto error; goto error;
} else { } else {
if (virBitmapSetBit(*bitmap, start) < 0) if (virBitmapSetBit(bitmap, start) < 0)
goto error; goto error;
} }
} else if (*cur == '-') { } else if (*cur == '-') {
@ -446,7 +442,7 @@ virBitmapParseInternal(const char *str,
cur = tmp; cur = tmp;
for (i = start; i <= last; i++) { for (i = start; i <= last; i++) {
if (virBitmapSetBit(*bitmap, i) < 0) if (virBitmapSetBit(bitmap, i) < 0)
goto error; goto error;
} }
@ -469,8 +465,6 @@ virBitmapParseInternal(const char *str,
error: error:
virReportError(VIR_ERR_INVALID_ARG, virReportError(VIR_ERR_INVALID_ARG,
_("Failed to parse bitmap '%s'"), str); _("Failed to parse bitmap '%s'"), str);
virBitmapFree(*bitmap);
*bitmap = NULL;
return -1; return -1;
} }
@ -495,7 +489,14 @@ virBitmapParse(const char *str,
virBitmap **bitmap, virBitmap **bitmap,
size_t bitmapSize) size_t bitmapSize)
{ {
return virBitmapParseInternal(str, bitmap, bitmapSize); g_autoptr(virBitmap) tmp = virBitmapNew(bitmapSize);
if (virBitmapParseInternal(str, tmp) < 0)
return -1;
*bitmap = g_steal_pointer(&tmp);
return 0;
} }