[RFC] Make getenv O(1)

Ondřej Bílka neleai@seznam.cz
Fri Oct 18 14:07:00 GMT 2013


On Fri, Oct 18, 2013 at 05:46:57PM +0400, Alexander Monakov wrote:
> On Fri, 18 Oct 2013, Ondřej Bílka wrote:
> > You could argue that this does not help when likely case is that
> > variable is not defined. For that I would need posix say that changing
> > variable name via environ pointers is illegal.
> 
> Indeed; I would also ask if such functions are allowed to query interesting
> environment variables once on first call and then cache the result (similar to
> how the dynamic linker stashes initial LD_LIBRARY_PATH on the side and calls
> to dlopen do not check the environment again).
> 
> Alexander

This could be done by following macro with global renaming of
getenv->GETENV.

Also do we support in libc setenv to change behaviour?

#define GETENV(x) ({ \
  static char *__ret; \
  static int __cached; \
  if (__builtin_constant_p (x) && !__cached) \
  { \
    __ret = getenv (x); \
    __cached = 1; \
  } \
  __builtin_constant_p (x) ? __ret : getenv (x);})



More information about the Libc-alpha mailing list