aboutsummaryrefslogtreecommitdiffstats
path: root/xen/include/xen/xmalloc.h
diff options
context:
space:
mode:
authorJan Beulich <jbeulich@suse.com>2011-10-04 14:15:26 +0200
committerJan Beulich <jbeulich@suse.com>2011-10-04 14:15:26 +0200
commitbeb8eac93c9a38c417db4ae77430af5568e54f1c (patch)
tree1b95fb18c531342b3eb88a47c8069bf3e08826a6 /xen/include/xen/xmalloc.h
parent148ed3108078b354f4c7620c495e300d54ec2724 (diff)
downloadxen-beb8eac93c9a38c417db4ae77430af5568e54f1c.tar.gz
xen-beb8eac93c9a38c417db4ae77430af5568e54f1c.tar.bz2
xen-beb8eac93c9a38c417db4ae77430af5568e54f1c.zip
introduce xzalloc() & Co
Rather than having to match a call to one of the xmalloc() flavors with a subsequent memset(), introduce a zeroing variant of each of those flavors. Signed-off-by: Jan Beulich <jbeulich@suse.com> Acked-by: Keir Fraser <keir@xen.org>
Diffstat (limited to 'xen/include/xen/xmalloc.h')
-rw-r--r--xen/include/xen/xmalloc.h17
1 files changed, 16 insertions, 1 deletions
diff --git a/xen/include/xen/xmalloc.h b/xen/include/xen/xmalloc.h
index d9df7dc50e..24a99ac244 100644
--- a/xen/include/xen/xmalloc.h
+++ b/xen/include/xen/xmalloc.h
@@ -11,19 +11,25 @@
/* Allocate space for typed object. */
#define xmalloc(_type) ((_type *)_xmalloc(sizeof(_type), __alignof__(_type)))
+#define xzalloc(_type) ((_type *)_xzalloc(sizeof(_type), __alignof__(_type)))
/* Allocate space for array of typed objects. */
#define xmalloc_array(_type, _num) \
((_type *)_xmalloc_array(sizeof(_type), __alignof__(_type), _num))
+#define xzalloc_array(_type, _num) \
+ ((_type *)_xzalloc_array(sizeof(_type), __alignof__(_type), _num))
/* Allocate untyped storage. */
-#define xmalloc_bytes(_bytes) (_xmalloc(_bytes, SMP_CACHE_BYTES))
+#define xmalloc_bytes(_bytes) _xmalloc(_bytes, SMP_CACHE_BYTES)
+#define xzalloc_bytes(_bytes) _xzalloc(_bytes, SMP_CACHE_BYTES)
/* Free any of the above. */
extern void xfree(void *);
/* Underlying functions */
extern void *_xmalloc(unsigned long size, unsigned long align);
+extern void *_xzalloc(unsigned long size, unsigned long align);
+
static inline void *_xmalloc_array(
unsigned long size, unsigned long align, unsigned long num)
{
@@ -33,6 +39,15 @@ static inline void *_xmalloc_array(
return _xmalloc(size * num, align);
}
+static inline void *_xzalloc_array(
+ unsigned long size, unsigned long align, unsigned long num)
+{
+ /* Check for overflow. */
+ if (size && num > UINT_MAX / size)
+ return NULL;
+ return _xzalloc(size * num, align);
+}
+
/*
* Pooled allocator interface.
*/