[cfe-dev] __attribute__((alloc_size(foo))

Nuno Lopes nunoplopes at sapo.pt
Wed May 23 17:24:43 PDT 2012


It's in:
http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20120521/058005.html

Thank you both,
Nuno


Citando Xi Wang <xi.wang at gmail.com>:

> Jordy,
>
> Thanks for letting me know.  I am happy to see alloc_size in Clang. ;-)
>
> Also thanks to Nuno for bringing this up.
>
> - xi
>
> On May 23, 2012, at 3:29 PM, Jordan Rose wrote:
>
>> Xi Wang took a stab at this back in February but no one finished  
>> reviewing his patch.[1][2] I'll take a look at yours soon, but  
>> maybe Xi has an opinion as well?
>>
>> Jordy
>>
>> P.S. If we want to go really crazy, alloc_foo_objects(int howmany)  
>> could be handled with an extension to alloc_size:  
>> __attribute__((alloc_size(1,sizeof(struct foo)))). But that can  
>> wait for later.
>>
>> [1]:  
>> http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20120227/054264.html
>> [2]: http://lists.cs.uiuc.edu/pipermail/cfe-dev/2012-February/019952.html
>>
>>
>>
>> On May 23, 2012, at 13:54, Nuno Lopes wrote:
>>
>>> Hi,
>>>
>>> Please find in attach a patch that implements the attribute  
>>> alloc_size. This attribute was introduced with GCC 4.3.
>>>
>>> Intended usage:
>>>
>>> void* my_malloc(size_t) __attribute__((alloc_size(1)));
>>> void* my_calloc(size_t, size_t) __attribute__((alloc_size(1,2)));
>>> void* my_realloc(void*, size_t) __attribute__((alloc_size(2)));
>>>
>>> The attribute basically declares that a function returns memory of  
>>> size given by the product of the parameters listed in the attribute.
>>>
>>> More info: http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
>>>
>>> This patch only adds Sema support for the attribute. Wiring for  
>>> codegen and static analyzer will come later.
>>>
>>> Any comments, suggestions, etc?
>>>
>>> Thanks,
>>> Nuno
>>>
>>> P.S.: This attribute is not expressive enough for functions that  
>>> e.g. return x elements, like 'alloc_foo_objects(int howmany)'.  
>>> However, since gcc already has this attribute and software out  
>>> there is already using it, I think we should support it, even if  
>>> we come up with a better attribute later.



More information about the cfe-dev mailing list