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

[Xen-changelog] [xen staging] xen/common: Introduce xrealloc_flex_struct() helper macros



commit efc882ffb762a0b2eeadb60ea05a1093b5a48de8
Author:     Oleksandr Tyshchenko <oleksandr_tyshchenko@xxxxxxxx>
AuthorDate: Thu Sep 26 14:20:30 2019 +0300
Commit:     Julien Grall <julien.grall@xxxxxxx>
CommitDate: Thu Sep 26 14:35:32 2019 +0100

    xen/common: Introduce xrealloc_flex_struct() helper macros
    
    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 the "data" array:
    
       struct arrlen
       {
          size_t len;
          int data[];
       };
    
    We can use the proposed macros in the following way:
    
       new_ptr = realloc_flex_struct(old_ptr, data, nr_elem);
    
    where nr_elem is the desired number of elements.
    
    Subsequent patch will use this macros.
    
    Also, while here, introduce xmalloc(xzalloc)_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>
    Reviewed-by: Jan Beulich <jbeulich@xxxxxxxx>
    CC: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
    CC: George Dunlap <George.Dunlap@xxxxxxxxxxxxx>
    CC: Ian Jackson <ian.jackson@xxxxxxxxxxxxx>
    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 831152f895..f515ceee2a 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))
 
+/* Allocate space for a structure with a flexible array of typed objects. */
+#define xzalloc_flex_struct(type, field, nr) \
+    ((type *)_xzalloc(offsetof(type, field[nr]), __alignof__(type)))
+
+#define xmalloc_flex_struct(type, field, nr) \
+    ((type *)_xmalloc(offsetof(type, field[nr]), __alignof__(type)))
+
+/* Re-allocate space for a structure with a flexible array of typed objects. */
+#define xrealloc_flex_struct(ptr, field, nr)                           \
+    ((typeof(ptr))_xrealloc(ptr, offsetof(typeof(*(ptr)), field[nr]),  \
+                            __alignof__(typeof(*(ptr)))))
+
 /* Allocate untyped storage. */
 #define xmalloc_bytes(_bytes) _xmalloc(_bytes, SMP_CACHE_BYTES)
 #define xzalloc_bytes(_bytes) _xzalloc(_bytes, SMP_CACHE_BYTES)
--
generated by git-patchbot for /home/xen/git/xen.git#staging

_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/xen-changelog

 


Rackspace

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