Re: CurrentMemoryContext and MemoryContextStrdup
От | Yessica Brinkmann |
---|---|
Тема | Re: CurrentMemoryContext and MemoryContextStrdup |
Дата | |
Msg-id | CABrYqSOv2GEHmZcOmJPrrhVTmO-HHActL7z54b9yxO1H61r_wA@mail.gmail.com обсуждение исходный текст |
Ответ на | CurrentMemoryContext and MemoryContextStrdup (Yessica Brinkmann <yessica.brinkmann@gmail.com>) |
Список | pgsql-novice |
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 = 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 = 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
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 = 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 = 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 (<tgl@sss.pgh.pa.us>) escribió:
Yessica Brinkmann <yessica.brinkmann@gmail.com> 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
В списке pgsql-novice по дате отправления: