summaryrefslogtreecommitdiffstats
path: root/libvirt-utils.h
diff options
context:
space:
mode:
authorDaniel P. Berrange <berrange@redhat.com>2013-09-09 09:59:45 +0100
committerDaniel P. Berrange <berrange@redhat.com>2013-11-12 18:02:07 +0000
commita825a495cd7ba38edd5d085db51780a861ae6a0d (patch)
tree0a1cdeaba5beb22e4c04df52d2085e8fa73faf3d /libvirt-utils.h
parent096b1553b3c0974a5cc2821d60ce11112fc92ae0 (diff)
downloadlibvirt-python-v6-a825a495cd7ba38edd5d085db51780a861ae6a0d.tar.gz
libvirt-python-v6-a825a495cd7ba38edd5d085db51780a861ae6a0d.tar.xz
libvirt-python-v6-a825a495cd7ba38edd5d085db51780a861ae6a0d.zip
Import gnulib's xalloc_oversized macro
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Diffstat (limited to 'libvirt-utils.h')
-rw-r--r--libvirt-utils.h15
1 files changed, 15 insertions, 0 deletions
diff --git a/libvirt-utils.h b/libvirt-utils.h
index 65b9b3b..05c1135 100644
--- a/libvirt-utils.h
+++ b/libvirt-utils.h
@@ -28,4 +28,19 @@
# define MIN(a,b) (((a) < (b)) ? (a) : (b))
#endif
+/* Return 1 if an array of N objects, each of size S, cannot exist due
+ to size arithmetic overflow. S must be positive and N must be
+ nonnegative. This is a macro, not a function, so that it
+ works correctly even when SIZE_MAX < N.
+
+ By gnulib convention, SIZE_MAX represents overflow in size
+ calculations, so the conservative dividend to use here is
+ SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
+ However, malloc (SIZE_MAX) fails on all known hosts where
+ sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
+ exactly-SIZE_MAX allocations on such hosts; this avoids a test and
+ branch when S is known to be 1. */
+#define xalloc_oversized(n, s) \
+ ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
+
#endif /* __LIBVIRT_UTILS_H__ */