[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>
  • From: Volodymyr Babchuk <Volodymyr_Babchuk@xxxxxxxx>
  • Date: Tue, 6 Aug 2019 19:51:36 +0000
  • Accept-language: en-US
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=epam.com; dmarc=pass action=none header.from=epam.com; dkim=pass header.d=epam.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=zHkWOSqyPXIegIGPw4ZaV24ElYWmS7SQ2w1ONmn8xGE=; b=JQIKEZ0z06JbYNgNe4pv3C/KhmXvpga+PLWLcjo7CGiiBNQNXmaWw7IDlDwf8i9lGtIY16B2m+ISi9wbapfoWxJ6JSCrX06uGpKdMyGyZMEJeJr/YWE7zS7zE2f2nNa6tLfgLQJWgoqm3psElQD7v70bKREQLimJ4lXN5creJcNIvgY+p1vjkOBlpbKrQ4cRGC1HeQ2ZBIEk0ItSIs/GNsdlthk8QYq9qWyA9Xjf2e0EKewunbXi2KqDHFwgdD8eNacZMrYkut/JUOIiPQ8HgOv44intAytkPM6oZZ2h24jAHv5S2cxnz3Vj31VY1RoGGa0Ts+34DMV6rZHNt+ZJ2Q==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=J38rtVUlFLDFRdSuf/mXOeQ/Vwki7o8ikLEfdHWiGjl0rKK+dr8qvWuXsbA93xxK/du5rI32RbtrpMJsVSjfADrYSAvP9jUEAbBhxYzE7uGPzySysMlRKs9WCz5gtZ4Ki0jcjIOxEuQfszwGctm3kxGC/e+HENJdrnBE3LM+wd3nRuZ23G3aUgAH43dPvD3sKSDPECLz1T+6jR8cl5Cx66+S+YGJcuMXYbI+reLOl22NtXLVsIul7bAQs6uSJWcewPspwxJM/ViCRZ8SbYLXsWR3rEF1dWcP2NsAUqa2d0oT/o9BFq6KQiAjB+dXxJWU5JS7/xwDypeysaoYwfmRdw==
  • Authentication-results: spf=none (sender IP is ) smtp.mailfrom=Volodymyr_Babchuk@xxxxxxxx;
  • Cc: "sstabellini@xxxxxxxxxx" <sstabellini@xxxxxxxxxx>, Wei Liu <wl@xxxxxxx>, Konrad Rzeszutek Wilk <konrad.wilk@xxxxxxxxxx>, George Dunlap <George.Dunlap@xxxxxxxxxxxxx>, Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, Ian Jackson <ian.jackson@xxxxxxxxxxxxx>, Tim Deegan <tim@xxxxxxx>, Oleksandr Tyshchenko <olekstysh@xxxxxxxxx>, Oleksandr Tyshchenko <Oleksandr_Tyshchenko@xxxxxxxx>, "julien.grall@xxxxxxx" <julien.grall@xxxxxxx>, "xen-devel@xxxxxxxxxxxxxxxxxxxx" <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • Delivery-date: Tue, 06 Aug 2019 19:51:47 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>
  • Thread-index: AQHVS3WhxEP7sPmFBk6t/8cQy9ir3qbuicmA
  • Thread-topic: [Xen-devel] [PATCH V2 3/6] [RFC] xen/common: Introduce _xrealloc function

Hi Jan,

Jan Beulich writes:

> On 02.08.2019 18:39, Oleksandr Tyshchenko wrote:
>> --- a/xen/common/xmalloc_tlsf.c
>> +++ b/xen/common/xmalloc_tlsf.c
>> @@ -610,6 +610,27 @@ void *_xzalloc(unsigned long size, unsigned long align)
>>       return p ? memset(p, 0, size) : p;
>>   }
>>   
>> +void *_xrealloc(void *p, unsigned long new_size, unsigned long align)
>> +{
>> +    void *new_p;
>> +
>> +    if ( !new_size )
>> +    {
>> +        xfree(p);
>> +        return NULL;
>> +    }
>> +
>> +    new_p = _xmalloc(new_size, align);
>> +
>> +    if ( new_p && p )
>> +    {
>> +        memcpy(new_p, p, new_size);
>> +        xfree(p);
>> +    }
>> +
>> +    return new_p;
>> +}
>
> While I can see why having a re-allocation function may be handy,
> explicit / direct use of _xmalloc() and _xzalloc() are discouraged,
> in favor of the more type-safe underscore-less variants. I can't
> see though how a type-safe "realloc" could look like, except for
> arrays. If resizing arrays is all you're after, I'd like to
> recommend to go that route rather then the suggested one here. If
> resizing arbitrary objects is the goal, then what you suggest may
> be the only route, but I'd still be not overly happy to see such
> added.

I can see 3 uses for realloc:

 a. re-allocate generic data buffer
 b. re-allocate array
 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.

-- 
Volodymyr Babchuk at EPAM
_______________________________________________
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®.