Received: from eggs.gnu.org ([2001:4830:134:3::10]:54942) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XYrg9-0005na-Jv for qemu-devel@nongnu.org; Tue, 30 Sep 2014 03:17:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XYrg3-0006Fa-UL for qemu-devel@nongnu.org; Tue, 30 Sep 2014 03:16:57 -0400 Received: from mx-v6.kamp.de ([2a02:248:0:51::16]:52899 helo=mx01.kamp.de) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XYrg3-0006Eg-L3 for qemu-devel@nongnu.org; Tue, 30 Sep 2014 03:16:51 -0400 From: Peter Lieven Date: Tue, 30 Sep 2014 09:09:11 +0200 Message-Id: <1412060952-19407-2-git-send-email-pl@kamp.de> In-Reply-To: <1412060952-19407-1-git-send-email-pl@kamp.de> References: <1412060952-19407-1-git-send-email-pl@kamp.de> Subject: [Qemu-devel] [PATCHv2 1/2] util: introduce bitmap_try_new List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: kwolf@redhat.com, stefanha@redhat.com, Peter Lieven , ronniesahlberg@gmail.com, pbonzini@redhat.com regular bitmap_new simply aborts if the memory allocation fails. bitmap_try_new returns NULL on failure and allows for proper error handling. Signed-off-by: Peter Lieven --- include/qemu/bitmap.h | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/include/qemu/bitmap.h b/include/qemu/bitmap.h index 1babd5d..edf4f17 100644 --- a/include/qemu/bitmap.h +++ b/include/qemu/bitmap.h @@ -88,10 +88,19 @@ int slow_bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1, int slow_bitmap_intersects(const unsigned long *bitmap1, const unsigned long *bitmap2, long bits); -static inline unsigned long *bitmap_new(long nbits) +static inline unsigned long *bitmap_try_new(long nbits) { long len = BITS_TO_LONGS(nbits) * sizeof(unsigned long); - return g_malloc0(len); + return g_try_malloc0(len); +} + +static inline unsigned long *bitmap_new(long nbits) +{ + unsigned long *ptr = bitmap_try_new(nbits); + if (ptr == NULL) { + abort(); + } + return ptr; } static inline void bitmap_zero(unsigned long *dst, long nbits) -- 1.7.9.5