[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Xen-devel] [PATCH V2 3/6] [RFC] xen/common: Introduce _xrealloc function
- To: Jan Beulich <jbeulich@xxxxxxxx>, Volodymyr Babchuk <Volodymyr_Babchuk@xxxxxxxx>
- From: Oleksandr <olekstysh@xxxxxxxxx>
- Date: Wed, 7 Aug 2019 21:36:43 +0300
- Cc: "sstabellini@xxxxxxxxxx" <sstabellini@xxxxxxxxxx>, Wei Liu <wl@xxxxxxx>, Konrad Rzeszutek Wilk <konrad.wilk@xxxxxxxxxx>, George Dunlap <George.Dunlap@xxxxxxxxxxxxx>, AndrewCooper <andrew.cooper3@xxxxxxxxxx>, Ian Jackson <ian.jackson@xxxxxxxxxxxxx>, Tim Deegan <tim@xxxxxxx>, Oleksandr Tyshchenko <Oleksandr_Tyshchenko@xxxxxxxx>, "julien.grall@xxxxxxx" <julien.grall@xxxxxxx>, "xen-devel@xxxxxxxxxxxxxxxxxxxx" <xen-devel@xxxxxxxxxxxxxxxxxxxx>
- Delivery-date: Wed, 07 Aug 2019 18:36:54 +0000
- List-id: Xen developer discussion <xen-devel.lists.xenproject.org>
Hi, Jan, Volodymyr.
c. re-allocate struct with flexible buffer.
option c. is about structures like this:
struct arrlen
{
size_t len;
int data[1];
};
This is Oleksandr's case.
So for option a. we can use _xreallocate(ptr, size, align)
For option b. we can use xrealloc_array(_ptr, _type, _num)
And for option c. I propose to implement the following macro:
#define realloc_flex_struct(_ptr, _type, _field,
_len) \
((_type *)_xrealloc(_ptr, offsetof(_type, _field[_len]) ,
__alignof__(_type)))
It can be used in the following way:
newptr = realloc_flex_struct(ptr, struct arrlen, newsize);
As you can see, this approach is type-safe and covers Oleksanrd's case.
This looks fine to me, but then wants to be accompanied by a
similar xmalloc_flex_struct(), which could be used right away
to replace a number of open-coded instances of the above.
Thank you Volodymyr for the idea. Looks like we can get a type-safe
approach which looks suitable for my particular case.
So, I need to focus on the proper implementation of non type-safe
(_xrealloc) variant in the first place taking into the account Jan's
comments. Then I will be back to the suggested type-safe marco
(realloc_flex_struct).
There's one more thing for the re-alloc case though (besides
cosmetic aspects): The incoming pointer should also be verified
to be of correct type.
Jan, how this could be technically implemented, or are these any
existing examples in Xen?
Jan
--
Regards,
Oleksandr Tyshchenko
_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/xen-devel
|