From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35327) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XYwW4-0000Tp-Qx for qemu-devel@nongnu.org; Tue, 30 Sep 2014 08:26:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XYwVy-0003He-0w for qemu-devel@nongnu.org; Tue, 30 Sep 2014 08:26:52 -0400 Received: from mail-we0-x231.google.com ([2a00:1450:400c:c03::231]:43548) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XYwVx-0003GJ-Qz for qemu-devel@nongnu.org; Tue, 30 Sep 2014 08:26:45 -0400 Received: by mail-we0-f177.google.com with SMTP id k48so85093wev.22 for ; Tue, 30 Sep 2014 05:26:40 -0700 (PDT) Sender: Paolo Bonzini From: Paolo Bonzini Date: Tue, 30 Sep 2014 14:25:18 +0200 Message-Id: <1412079919-18857-39-git-send-email-pbonzini@redhat.com> In-Reply-To: <1412079919-18857-1-git-send-email-pbonzini@redhat.com> References: <1412079919-18857-1-git-send-email-pbonzini@redhat.com> Subject: [Qemu-devel] [PULL 38/39] util: introduce bitmap_try_new List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Peter Lieven From: Peter Lieven 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 Signed-off-by: Paolo Bonzini --- 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.8.3.1