[RFC] Make getenv O(1)

Ondřej Bílka neleai@seznam.cz
Mon Oct 21 17:09:00 GMT 2013


On Mon, Oct 21, 2013 at 08:55:37PM +0400, Alexander Monakov wrote:
> On Mon, 21 Oct 2013, Ondřej Bílka wrote:
> > Why, following does not need synchronization.
> > 
> > #define GETENV(x) ({ \
> >   static char *__ret = NULL; \
> >   if (__builtin_constant_p (x) && !__cached) \
> >     __ret = getenv (x) + 1; \
> >   __builtin_constant_p (x) ? __ret - 1: getenv (x);})
> 
> This wouldn't compile as __cached is not defined (and I'll admit I don't see
> what approach you're trying to convey).
> 
s/__cached/__ret/ 

Its having one variable instead two so it concurent access cannot
return unitialized ret.



More information about the Libc-alpha mailing list