Skip to contentSkip to conversation

introduce xzalloc() & Co

PATCH In review Jan Beulich 1 participant 1 message Oct 4, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 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.

xen/common/xmalloc_tlsf.c +7 -0
@@ -585,6 +585,13 @@ void *_xmalloc(unsigned long size, unsig
     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;
xen/include/acpi/platform/aclinux.h +1 -4
@@ -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__ */
xen/include/xen/xmalloc.h +16 -1
@@ -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.
  */