[Mesa-dev] [PATCH 04/24] ralloc: Unify overloads of the new operator and guarantee object destruction.

Chad Versace chad.versace at linux.intel.com
Thu Sep 19 12:26:52 PDT 2013


On 09/18/2013 02:57 PM, Kenneth Graunke wrote:
> On 09/15/2013 12:10 AM, Francisco Jerez wrote:
>> This patch introduces a pair of helper functions providing a common
>> implementation of the "new" and "delete" operators for all C++ classes
>> that are allocated by ralloc via placement new.  The 'ralloc_new'
>> helper function takes care of setting up an ralloc destructor callback
>> that will call the appropriate destructor before the memory allocated
>> to an object is released.
>>
>> Until now objects needed to call 'ralloc_set_destructor' explicitly
>> with a pointer to a static method which in turn called the actual
>> destructor in order to get something that should be transparent to
>> them.  After this patch they'll only need to call 'ralloc_new' from
>> the new operator and 'ralloc_delete' from the delete operator, turning
>> all overloads of new and delete into one-liners.
>
> I really like the idea of reducing the boilerplate.  I'd been meaning to
> do that for a while, but was thinking of creating a "rallocable" base
> class (which would then introduce multiple inheritance).  I never got
> around to trying it.
>
> I can't say I'm crazy about this approach, though:
>
> There's still a fair bit of boilerplate; every class still needs to do:
>
> static void* operator new(size_t size, void *ctx)
> {
>     return ralloc_new<whatever>(size, ctx);
> }
>
> static void operator delete(void *p)
> {
>     ralloc_delete(p);
> }
>
> I'd like to go all the way and condense that to a single line.
>
> This is also the first usage of templates in core Mesa, which I know
> will annoy a lot of people.  While I would be in favor of using
> templates to introduce type safety into things like exec_list or
> hash_table, I don't think they provide a very compelling advantage in
> this case.
>
> I've sent out a new series which accomplishes the same thing, but
> doesn't use templates, and reduces the boilerplate even a bit more.
> Comments welcome.

I vote for Ken's method. It reduces the boilerplate from 7 lines
(using Francisco's method) to a single line.



More information about the mesa-dev mailing list