[Mesa-dev] [RFC PATCH 4/5] ralloc: Make the C++ macros arrange for class destructors to be called.

Ian Romanick idr at freedesktop.org
Thu Sep 19 15:26:01 PDT 2013


On 09/18/2013 04:55 PM, Kenneth Graunke wrote:
> Previously, almost all classes didn't actually call their destructors,
> which is non-intuitive for C++ code.  Setting them up to be called was
> somewhat of a pain, since it required defining a helper function.
> 
> With the new macros, we can easily encapsulate this complexity, making
> destructors just happen automatically.
> 
> Signed-off-by: Kenneth Graunke <kenneth at whitecape.org>
> ---
>  src/glsl/ralloc.h | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/src/glsl/ralloc.h b/src/glsl/ralloc.h
> index 799d3a9..82a3daa 100644
> --- a/src/glsl/ralloc.h
> +++ b/src/glsl/ralloc.h
> @@ -405,15 +405,23 @@ bool ralloc_vasprintf_append(char **str, const char *fmt, va_list args);
>  #endif
>  
>  #define _RALLOC_OPS(ALLOC, TYPE)                                         \
> +private:                                                                 \
> +   static void _ralloc_##TYPE##_destructor_callback(void *p)             \
> +   {                                                                     \
> +      reinterpret_cast<TYPE *>(p)->~TYPE();                              \
> +   }                                                                     \
> +public:                                                                  \
>     static void* operator new(size_t size, void *mem_ctx)                 \
>     {                                                                     \
>        void *p = ALLOC(mem_ctx, size);                                    \
>        assert(p != NULL);                                                 \
> +      ralloc_set_destructor(p, _ralloc_##TYPE##_destructor_callback);    \
>        return p;                                                          \
>     }                                                                     \
>                                                                           \
>     static void operator delete(void *p)                                  \
>     {                                                                     \
> +      ralloc_set_destructor(p, NULL);                                    \

I had to think about why this is correct.  A comment would help.

      /* The delete operator will invoke the destructor before getting
       * here.  Uninstalled the destructor handler to prevent it from
       * being invoked twice.
       */

Right?

>        ralloc_free(p);                                                    \
>     }
>  
> 



More information about the mesa-dev mailing list