diff mbox series

[V4,5/8] xen/common: Introduce xrealloc_flex_struct() helper macros

Message ID 1568388917-7287-6-git-send-email-olekstysh@gmail.com (mailing list archive)
State Superseded
Headers show
Series iommu/arm: Add Renesas IPMMU-VMSA support + Linux's iommu_fwspec | expand

Commit Message

Oleksandr Tyshchenko Sept. 13, 2019, 3:35 p.m. UTC
From: Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com>

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, data, nr_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@epam.com>
Signed-off-by: Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com>
CC: Andrew Cooper <andrew.cooper3@citrix.com>
CC: George Dunlap <George.Dunlap@eu.citrix.com>
CC: Ian Jackson <ian.jackson@eu.citrix.com>
CC: Jan Beulich <jbeulich@suse.com>
CC: Julien Grall <julien.grall@arm.com>
CC: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
CC: Stefano Stabellini <sstabellini@kernel.org>
CC: Tim Deegan <tim@xen.org>
CC: Wei Liu <wl@xen.org>
CC: Paul Durrant <paul.durrant@citrix.com>

---
Changes V3 -> V4:
    - clarified patch description
    - modified to not use leading underscores
    - removed unnecessary pair of outermost parentheses
    - modified to use "nr" instead of "len"
    - placed xmalloc_flex_struct before xrealloc_flex_struct
    - simplified xrealloc_flex_struct macros
---
 xen/include/xen/xmalloc.h | 9 +++++++++
 1 file changed, 9 insertions(+)

Comments

Jan Beulich Sept. 16, 2019, 10:37 a.m. UTC | #1
On 13.09.2019 17:35, Oleksandr Tyshchenko wrote:
> --- a/xen/include/xen/xmalloc.h
> +++ b/xen/include/xen/xmalloc.h
> @@ -35,6 +35,15 @@
>  #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 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))))

With the missing parentheses around the entire constructs added
Reviewed-by: Jan Beulich <jbeulich@suse.com>

I'd like to note though that it sort of feels as if this notation
isn't going to provide maximum flexibility. I therefore wonder
whether the last two parameters shouldn't be combined, resulting
in an invocation like

    ptr = xmalloc_flex_struct(struct s, field[5]);

But I realize this would allow for (more; I'll reply to patch 6
in a minute) abuse, so this wouldn't be a clear win.

Jan
Oleksandr Tyshchenko Sept. 16, 2019, 6:11 p.m. UTC | #2
On 16.09.19 13:37, Jan Beulich wrote:

Hi, Jan

> On 13.09.2019 17:35, Oleksandr Tyshchenko wrote:
>> --- a/xen/include/xen/xmalloc.h
>> +++ b/xen/include/xen/xmalloc.h
>> @@ -35,6 +35,15 @@
>>   #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 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))))
> With the missing parentheses around the entire constructs added
> Reviewed-by: Jan Beulich <jbeulich@suse.com>

Thank you.


> I'd like to note though that it sort of feels as if this notation
> isn't going to provide maximum flexibility. I therefore wonder
> whether the last two parameters shouldn't be combined, resulting
> in an invocation like
>
>      ptr = xmalloc_flex_struct(struct s, field[5]);
>
> But I realize this would allow for (more; I'll reply to patch 6
> in a minute) abuse, so this wouldn't be a clear win.

Agree.
Oleksandr Tyshchenko Sept. 20, 2019, 9:51 a.m. UTC | #3
Hi Jan


>
>> On 13.09.2019 17:35, Oleksandr Tyshchenko wrote:
>>> --- a/xen/include/xen/xmalloc.h
>>> +++ b/xen/include/xen/xmalloc.h
>>> @@ -35,6 +35,15 @@
>>>   #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 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))))
>> With the missing parentheses around the entire constructs added
>> Reviewed-by: Jan Beulich <jbeulich@suse.com>
>
> Thank you.


Would you be happy if I add xzalloc_flex_struct here as well (may I 
retain your R-b)?

Actually the xzalloc_flex_struct better fits in [1] ...


[1] 
https://www.mail-archive.com/xen-devel@lists.xenproject.org/msg55557.html
Jan Beulich Sept. 20, 2019, 10:25 a.m. UTC | #4
On 20.09.2019 11:51, Oleksandr wrote:
>>> On 13.09.2019 17:35, Oleksandr Tyshchenko wrote:
>>>> --- a/xen/include/xen/xmalloc.h
>>>> +++ b/xen/include/xen/xmalloc.h
>>>> @@ -35,6 +35,15 @@
>>>>   #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 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))))
>>> With the missing parentheses around the entire constructs added
>>> Reviewed-by: Jan Beulich <jbeulich@suse.com>
>>
>> Thank you.
> 
> 
> Would you be happy if I add xzalloc_flex_struct here as well (may I 
> retain your R-b)?

Yes to both.

Jan
diff mbox series

Patch

diff --git a/xen/include/xen/xmalloc.h b/xen/include/xen/xmalloc.h
index 831152f..f0736ce 100644
--- a/xen/include/xen/xmalloc.h
+++ b/xen/include/xen/xmalloc.h
@@ -35,6 +35,15 @@ 
 #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 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)