From bc2d8e5b065ef2cb8479ee3304dabbeeab018b4d Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Fri, 30 Jan 2015 11:19:59 +0100 Subject: [PATCH] util: bitmap: Add option to allocate bitmap without reporting error The virBitmapNew() function reports only OOM errors. Split out the internals into a "quiet" function and add a wrapper that reports the error. --- src/libvirt_private.syms | 1 + src/util/virbitmap.c | 42 +++++++++++++++++++++++++++++----------- src/util/virbitmap.h | 1 + 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index cb8a84276c..2908a1c231 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1048,6 +1048,7 @@ virBitmapLastSetBit; virBitmapNew; virBitmapNewCopy; virBitmapNewData; +virBitmapNewQuiet; virBitmapNextClearBit; virBitmapNextSetBit; virBitmapOverlaps; diff --git a/src/util/virbitmap.c b/src/util/virbitmap.c index d5b0035f50..b531be5e8b 100644 --- a/src/util/virbitmap.c +++ b/src/util/virbitmap.c @@ -54,31 +54,29 @@ struct _virBitmap { /** - * virBitmapNew: + * virBitmapNewQuiet: * @size: number of bits * * Allocate a bitmap capable of containing @size bits. * - * Returns a pointer to the allocated bitmap or NULL if - * memory cannot be allocated. + * Returns a pointer to the allocated bitmap or NULL if memory cannot be + * allocated. Does not report libvirt errors. */ -virBitmapPtr virBitmapNew(size_t size) +virBitmapPtr +virBitmapNewQuiet(size_t size) { virBitmapPtr bitmap; size_t sz; - if (SIZE_MAX - VIR_BITMAP_BITS_PER_UNIT < size || size == 0) { - virReportOOMError(); + if (SIZE_MAX - VIR_BITMAP_BITS_PER_UNIT < size || size == 0) return NULL; - } - sz = (size + VIR_BITMAP_BITS_PER_UNIT - 1) / - VIR_BITMAP_BITS_PER_UNIT; + sz = (size + VIR_BITMAP_BITS_PER_UNIT - 1) / VIR_BITMAP_BITS_PER_UNIT; - if (VIR_ALLOC(bitmap) < 0) + if (VIR_ALLOC_QUIET(bitmap) < 0) return NULL; - if (VIR_ALLOC_N(bitmap->map, sz) < 0) { + if (VIR_ALLOC_N_QUIET(bitmap->map, sz) < 0) { VIR_FREE(bitmap); return NULL; } @@ -88,6 +86,28 @@ virBitmapPtr virBitmapNew(size_t size) return bitmap; } + +/** + * virBitmapNew: + * @size: number of bits + * + * Allocate a bitmap capable of containing @size bits. + * + * Returns a pointer to the allocated bitmap or NULL if memory cannot be + * allocated. Reports libvirt errors. + */ +virBitmapPtr +virBitmapNew(size_t size) +{ + virBitmapPtr ret; + + if (!(ret = virBitmapNewQuiet(size))) + virReportOOMError(); + + return ret; +} + + /** * virBitmapFree: * @bitmap: previously allocated bitmap diff --git a/src/util/virbitmap.h b/src/util/virbitmap.h index a347f0afdc..136e8197d0 100644 --- a/src/util/virbitmap.h +++ b/src/util/virbitmap.h @@ -35,6 +35,7 @@ typedef virBitmap *virBitmapPtr; /* * Allocate a bitmap capable of containing @size bits. */ +virBitmapPtr virBitmapNewQuiet(size_t size) ATTRIBUTE_RETURN_CHECK; virBitmapPtr virBitmapNew(size_t size) ATTRIBUTE_RETURN_CHECK; /* -- GitLab