]> xenbits.xensource.com Git - xen.git/commitdiff
introduce xzalloc() & Co
authorJan Beulich <jbeulich@suse.com>
Thu, 8 Mar 2012 10:03:35 +0000 (10:03 +0000)
committerJan Beulich <jbeulich@suse.com>
Thu, 8 Mar 2012 10:03:35 +0000 (10:03 +0000)
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

xen/common/xmalloc_tlsf.c
xen/include/acpi/platform/aclinux.h
xen/include/xen/xmalloc.h

index 6be78e1b98d14e9ded22afcdc04aac34cf897d70..8a82355e0998b5d4df3858ccc941d752a665e4e6 100644 (file)
@@ -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;
index 805d8e57d1353f7b30f41e90035980815e7a5c21..daa2d7e9858dfb214128ee5e05eda60aabdf60f5 100644 (file)
 #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__ */
index a5188e8629874fddfcd4e8ced025ad4099aeb8dd..14639713e27c5b2e46e6ed89f9fa627cb4593610 100644 (file)
@@ -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.
  */