[Libguestfs] [PATCH] lib: Add COMPILE_REGEXP macro to hide regexp constructors/destructors.

Richard W.M. Jones rjones at redhat.com
Fri Nov 28 19:58:38 UTC 2014


On Fri, Nov 28, 2014 at 06:15:02PM +0100, Pino Toscano wrote:
> Sounds okay, I guess it shouldn't introduce much overhead in the
> library loading?
> 
> I also wonder whether we can delay the creation of those regexps
> (i.e. not just the ones mentioned in this patch) when just needed;
> something like the (untested):
> 
> #define COMPILE_REGEXP(name,pattern,options)                            \
>   static void free_regexp_##name (void) __attribute__((destructor));    \
>   static pcre *_internal_##name;                                        \
>   static void                                                           \
>   ##name (void)                                                         \
>   {                                                                     \
>     if (_internal_##name == NULL) {                                     \
>       const char *err;                                                  \
>       int offset;                                                       \
>       _internal_##name = pcre_compile ((pattern), (options),            \
>                                        &err, &offset, NULL);            \
>       if (_internal_##name == NULL) {                                   \
>         ignore_value (write (2, err, strlen (err)));                    \
>         abort ();                                                       \
>       }                                                                 \
>     }                                                                   \
>     return _internal_##name;                                            \
>   }                                                                     \
>   static void                                                           \
>   free_regexp_##name (void)                                             \
>   {                                                                     \
>     if (_internal_##name)                                               \
>       pcre_free (_internal_##name);                                     \
>   }
> 
> using it e.g.: match (re_major_minor (), ...)
> 
> The only issue I could see is that the above is not thread-safe.

Yeah, thread safety is a problem.  I wonder if ELF weak symbols can
help?  However AFAIK they only apply to functions.

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
Fedora Windows cross-compiler. Compile Windows programs, test, and
build Windows installers. Over 100 libraries supported.
http://fedoraproject.org/wiki/MinGW




More information about the Libguestfs mailing list