Re: [HACKERS] Does this make sense: - Mailing list pgsql-hackers

From jwieck@debis.com (Jan Wieck)
Subject Re: [HACKERS] Does this make sense:
Date
Msg-id m0zpajr-000EBSC@orion.SAPserv.Hamburg.dsh.de
Whole thread Raw
In response to Re: [HACKERS] Does this make sense:  (Bruce Momjian <maillist@candle.pha.pa.us>)
List pgsql-hackers
>
> >
> > char *
> > crypt_getpwdfilename()
> > {
> >
> >   static char *pfnam = NULL;
> >
> >   if (!pfnam)
> >   {
> >     int bufsize;
> >     bufsize = strlen(DataDir) + strlen(CRYPT_PWD_FILE) + 2;
> >     pfnam = (char *) palloc(bufsize);
> >     spprintf(pfnam, bufsize, "%s/%s", DataDir, CRYPT_PWD_FILE);
> >   }
> >
> >   return pfnam;
> > }
> >
> > Why the check for '!ipfnam'?  Seems useless since we are setting it to
> > NULL the line before...no?
>
> Actually, no.  We are declaring it as static, so the first time the
> function is called, it is set to NULL.  After that, it is not
> initialized for each function call because a static local variable's
> value is kept between function calls.  It is like a global variable in
> its duration, but in local scope.
>
> This is an old trick to run the initialization code only the first time
> the function is called.

    But  is  it  good  then  to use palloc() instead of malloc()?
    Anything palloc()'d is thrown away when the memory context in
    which  it  is made get's destroyed. So you have to care about
    the memory context in which the call is made. If  under  some
    (but  not  all)  circumstances  the FIRST call is made in the
    wrong mcxt, the pointer maybe get's corrupted later.


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#======================================== jwieck@debis.com (Jan Wieck) #

pgsql-hackers by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: [HACKERS] Does this make sense:
Next
From: geek+@cmu.edu
Date:
Subject: Re: [HACKERS] Does this make sense: