[Mesa-dev] [PATCH] ralloc: Use __attribute__((destructor)) instead of atexit(3)

Matt Turner mattst88 at gmail.com
Thu Sep 3 16:37:03 PDT 2015


On Thu, Sep 3, 2015 at 11:07 AM, Jean-Sébastien Pédron
<jean-sebastien.pedron at dumbbell.fr> wrote:
> ... to free the ralloc context at program exit.
>
> On Linux, atexit(3) handlers are called when the program exits but also
> when a library is unloaded. The latter behavior is a Glibc extension.
>
> On systems where this extension is not supported, this causes an
> application to crash when, for instance, a library using ralloc is
> dlclosed: at program exit, the registered function is no longer in
> memory. For example, this happens with OpenCL applications using an ICD
> loader on FreeBSD.
>
> __attribute__((destructor)) fixes the problem because such handlers are
> called when a library is unloaded and when the program exits.
> ---
>  src/util/ralloc.c | 3 ++-
>  src/util/ralloc.h | 9 ++++++---
>  2 files changed, 8 insertions(+), 4 deletions(-)
>
> diff --git a/src/util/ralloc.c b/src/util/ralloc.c
> index 01719c8..93933fc 100644
> --- a/src/util/ralloc.c
> +++ b/src/util/ralloc.c
> @@ -311,6 +311,7 @@ ralloc_parent(const void *ptr)
>
>  static void *autofree_context = NULL;
>
> +__attribute__((destructor))

You need to test for this support in configure.ac. It's as simple as
adding a call to AX_GCC_FUNC_ATTRIBUTE in the existing alphabetized
list and then a little bit of preprocessor in src/util/macros.h. (I
think you should put "DESTRUCTOR" or whatever macro name you choose on
the same line with "static void" like is done in intel_tiled_memcpy.c
for FLATTEN)

>  static void
>  autofree(void)
>  {


More information about the mesa-dev mailing list