[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH V3 4/8] xen/common: Introduce xrealloc_flex_struct() helper macros



From: Oleksandr Tyshchenko <oleksandr_tyshchenko@xxxxxxxx>

This patch introduces type-safe helper macros to re-allocate space
for a structure with a flexible array of typed objects.

For example, if we need to re-size an array with a single element:

   struct arrlen
   {
      size_t len;
      int data[1];
   };

We can use the proposed macros in the following way:

   new_ptr = realloc_flex_struct(old_ptr, struct arrlen, data, num_elem);

Subsequent patch will use this macros.

Also, while here, introduce xmalloc_flex_struct() to allocate space
for a structure with a flexible array of typed objects.

Suggested-by: Volodymyr Babchuk <Volodymyr_Babchuk@xxxxxxxx>
Signed-off-by: Oleksandr Tyshchenko <oleksandr_tyshchenko@xxxxxxxx>
CC: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
CC: George Dunlap <George.Dunlap@xxxxxxxxxxxxx>
CC: Ian Jackson <ian.jackson@xxxxxxxxxxxxx>
CC: Jan Beulich <jbeulich@xxxxxxxx>
CC: Julien Grall <julien.grall@xxxxxxx>
CC: Konrad Rzeszutek Wilk <konrad.wilk@xxxxxxxxxx>
CC: Stefano Stabellini <sstabellini@xxxxxxxxxx>
CC: Tim Deegan <tim@xxxxxxx>
CC: Wei Liu <wl@xxxxxxx>
---
 xen/include/xen/xmalloc.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/xen/include/xen/xmalloc.h b/xen/include/xen/xmalloc.h
index 831152f..2eb88a8 100644
--- a/xen/include/xen/xmalloc.h
+++ b/xen/include/xen/xmalloc.h
@@ -35,6 +35,18 @@
 #define xzalloc_array(_type, _num) \
     ((_type *)_xzalloc_array(sizeof(_type), __alignof__(_type), _num))
 
+/* Re-allocate space for a structure with a flexible array of typed objects. */
+#define xrealloc_flex_struct(_ptr, _type, _field, _len) ({                   \
+    /* type checking: make sure that incoming pointer is of correct type */  \
+    (void)((typeof(_ptr)) 0 == (_type *) 0);                                 \
+    ((_type *)_xrealloc(_ptr, offsetof(_type, _field[_len]),                 \
+                        __alignof__(_type)));                                \
+})
+
+/* Allocate space for a structure with a flexible array of typed objects. */
+#define xmalloc_flex_struct(_type, _field, _len) \
+    ((_type *)_xmalloc(offsetof(_type, _field[_len]), __alignof__(_type)))
+
 /* Allocate untyped storage. */
 #define xmalloc_bytes(_bytes) _xmalloc(_bytes, SMP_CACHE_BYTES)
 #define xzalloc_bytes(_bytes) _xzalloc(_bytes, SMP_CACHE_BYTES)
-- 
2.7.4


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/xen-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.