aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Beulich <jbeulich@suse.com>2012-03-08 10:03:35 +0000
committerJan Beulich <jbeulich@suse.com>2012-03-08 10:03:35 +0000
commit1c0aedadd29c085e905520e11234c948fb36a859 (patch)
tree5094c81eadff04caf9668cb864d2a144af9f0b66
parentf7d9a55475e65768f6d11f05b2d6dfbd715ae109 (diff)
downloadxen-1c0aedadd29c085e905520e11234c948fb36a859.tar.gz
xen-1c0aedadd29c085e905520e11234c948fb36a859.tar.bz2
xen-1c0aedadd29c085e905520e11234c948fb36a859.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> xen-unstable changeset: 23900:e09ebf7a31f5 xen-unstable date: Tue Oct 04 14:15:26 2011 +0200
-rw-r--r--xen/common/xmalloc_tlsf.c7
-rw-r--r--xen/include/acpi/platform/aclinux.h5
-rw-r--r--xen/include/xen/xmalloc.h17
3 files changed, 24 insertions, 5 deletions
diff --git a/xen/common/xmalloc_tlsf.c b/xen/common/xmalloc_tlsf.c
index 6be78e1b98..8a82355e09 100644
--- a/xen/common/xmalloc_tlsf.c
+++ b/xen/common/xmalloc_tlsf.c
@@ -585,6 +585,13 @@ void *_xmalloc(unsigned long size, unsigned long align)
return p;
}
+void *_xzalloc(unsigned long size, unsigned long align)
+{
+ void *p = _xmalloc(size, align);
+
+ return p ? memset(p, 0, size) : p;
+}
+
void xfree(void *p)
{
struct bhdr *b;
diff --git a/xen/include/acpi/platform/aclinux.h b/xen/include/acpi/platform/aclinux.h
index 805d8e57d1..daa2d7e985 100644
--- a/xen/include/acpi/platform/aclinux.h
+++ b/xen/include/acpi/platform/aclinux.h
@@ -77,10 +77,7 @@
#define acpi_thread_id struct vcpu *
#define ACPI_ALLOCATE(a) xmalloc_bytes(a)
-#define ACPI_ALLOCATE_ZEROED(a) ({ \
- void *p = xmalloc_bytes(a); \
- if ( p ) memset(p, 0, a); \
- p; })
+#define ACPI_ALLOCATE_ZEROED(a) xzalloc_bytes(a)
#define ACPI_FREE(a) xfree(a)
#endif /* __ACLINUX_H__ */
diff --git a/xen/include/xen/xmalloc.h b/xen/include/xen/xmalloc.h
index a5188e8629..14639713e2 100644
--- a/xen/include/xen/xmalloc.h
+++ b/xen/include/xen/xmalloc.h
@@ -8,19 +8,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)
{
@@ -30,6 +36,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.
*/