Re: CurrentMemoryContext and MemoryContextStrdup

Yessica Brinkmann <[email protected]> Tue, 26 Nov 2019 14:34:32 -0300
Newsgroups gmane.comp.db.postgresql.novice
Message-ID <CABrYqSOv2GEHmZcOmJPrrhVTmO-HHActL7z54b9yxO1H61r_wA@mail.gmail.com>
--0000000000009055300598434bdb
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Thank you very much for your answer.
Well, you told me that pstrdup was not going to help me in the context of
my use case.
And that I would have to use MemoryContextStrdup.
And the truth is that this part costs me a bit, even though I have the
definitions of MemoryContextStrdup in the .c, since I can't find an example
of a source code that uses it, it's hard for me to apply things by
definition just.
By telling me: "Copying it into a different context would be the way to do
that", I understand better.
Now, as I understand it (I don't know if I'm right), what I would have to
do would be this:
1. I save the CurrentMemoryContext, for example as follows:
MemoryContext oldcontext =3D CurrentMemoryContext;
2. I make the call to SPI, which was what caused the context problem.
3. I copy my variable in a different context, for example as follows:
MemoryContext newcontext;
char * copy =3D MemoryContextStrdup (newcontext, data);
4. Then, at the end of my call to SPI, after SPI_finish () I would have in
the copy variable, the copy of the data variable, to use it as I want.
Is that so? I am correct at least to try to modify my program or is it
totally something else what should I do?
Best regards,
Yessica Brinkmann

El mar., 26 nov. 2019 a las 13:48, Tom Lane (<[email protected]>) escribi=
=C3=B3:

> Yessica Brinkmann <[email protected]> writes:
> > Thank you very much for the reply!
> > Not really, I don't feel better informed because MemoryContextStrdup is
> not
> > even mentioned once in the README.
>
> The next thing to do would be to look at that function's header comment
> (find it in src/backend/utils/mmgr/mcxt.c):
>
> /*
>  * MemoryContextStrdup
>  *              Like strdup(), but allocate from the specified context
>  */
> char *
> MemoryContextStrdup(MemoryContext context, const char *string)
>
>
> As I recall your original problem, people were suggesting that
> you make a longer-lived copy of some transiently-allocated
> string.  Copying it into a different context would be the
> way to do that, as I hope you now understand from the README
> discussion, and this function is the easiest way to do that.
>
> Or at least the second easiest; the very easiest is pstrdup,
> which is just
>
> char *
> pstrdup(const char *in)
> {
>         return MemoryContextStrdup(CurrentMemoryContext, in);
> }
>
> but I don't remember whether the current context was a suitable
> target for your use-case.
>
>                         regards, tom lane
>

--0000000000009055300598434bdb
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Thank you very much for your answer.<br>Well, you told me =
that pstrdup was not going to help me in the context of my use case.<br>And=
 that I would have to use MemoryContextStrdup.<br>And the truth is that thi=
s part costs me a bit, even though I have the definitions of MemoryContextS=
trdup in the .c, since=C2=A0I can&#39;t find an example of a source code th=
at uses it, it&#39;s hard for me to apply things by definition just.<br>By =
telling me: &quot;Copying it into a different context would be the way to d=
o that&quot;, I understand better.<br>Now, as I understand it (I don&#39;t =
know if I&#39;m right), what I would have to do would be this:<br>1. I save=
 the CurrentMemoryContext, for example as follows:<br>MemoryContext oldcont=
ext =3D CurrentMemoryContext;<br>2. I make the call to SPI, which was what =
caused the context problem.<br>3. I copy my variable in a different context=
, for example as follows:<br>MemoryContext newcontext;<br>char * copy =3D M=
emoryContextStrdup (newcontext, data);<br>4. Then, at the end of my call to=
 SPI, after SPI_finish () I would have in the copy variable, the copy of th=
e data variable, to use it as I want.<br>Is that so? I am correct at least =
to try to modify my program or is it totally something else what should I d=
o?<br>Best regards,<br>Yessica Brinkmann<br></div><br><div class=3D"gmail_q=
uote"><div dir=3D"ltr" class=3D"gmail_attr">El mar., 26 nov. 2019 a las 13:=
48, Tom Lane (&lt;<a href=3D"mailto:[email protected]">[email protected]</a=
>&gt;) escribi=C3=B3:<br></div><blockquote class=3D"gmail_quote" style=3D"m=
argin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left=
:1ex">Yessica Brinkmann &lt;<a href=3D"mailto:[email protected]" =
target=3D"_blank">[email protected]</a>&gt; writes:<br>
&gt; Thank you very much for the reply!<br>
&gt; Not really, I don&#39;t feel better informed because MemoryContextStrd=
up is not<br>
&gt; even mentioned once in the README.<br>
<br>
The next thing to do would be to look at that function&#39;s header comment=
<br>
(find it in src/backend/utils/mmgr/mcxt.c):<br>
<br>
/*<br>
=C2=A0* MemoryContextStrdup<br>
=C2=A0*=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Like strdup(), but =
allocate from the specified context<br>
=C2=A0*/<br>
char *<br>
MemoryContextStrdup(MemoryContext context, const char *string)<br>
<br>
<br>
As I recall your original problem, people were suggesting that<br>
you make a longer-lived copy of some transiently-allocated<br>
string.=C2=A0 Copying it into a different context would be the<br>
way to do that, as I hope you now understand from the README<br>
discussion, and this function is the easiest way to do that.<br>
<br>
Or at least the second easiest; the very easiest is pstrdup,<br>
which is just<br>
<br>
char *<br>
pstrdup(const char *in)<br>
{<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return MemoryContextStrdup(CurrentMemoryContext=
, in);<br>
}<br>
<br>
but I don&#39;t remember whether the current context was a suitable<br>
target for your use-case.<br>
<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 regards, tom lane<br>
</blockquote></div>

--0000000000009055300598434bdb--