Thread: How to allocate space to structure instance

How to allocate space to structure instance

From
Rohit Goyal
Date:
Hi All,

I was using palloc to allocate space to my structure instance and I am loosing the information after every transaction. 
I want to hold up the information of previous transaction and create a link list of information(transaction Id) of some transaction together.

Am I using wrong memory context? Or is there anything else also I should keep in mind.

Please help.

--
Regards,
Rohit Goyal

Re: How to allocate space to structure instance

From
amul sul
Date:
 
>Am I using wrong memory context? Or
is there anything else also I should keep in mind.

Yes it might be. Memory context of
transaction is deleted at the end.
If you want to preserved the information, you can allocated space for structure before begin transaction call.


Regards,
Amul Sul


Re: How to allocate space to structure instance

From
Rohit Goyal
Date:



On Wed, Dec 11, 2013 at 5:42 AM, amul sul <sul_amul@yahoo.co.in> wrote:
 
>Am I using wrong memory context? Or
is there anything else also I should keep in mind.

Yes it might be. Memory context of
transaction is deleted at the end.
If you want to preserved the information, you can allocated space for structure before begin transaction call.


Regards,
Amul Sul

Hi, 

Can you please tel me the location in code where I can allocate space before begin transaction call?

Right now, I am using spi_palloc function to alloctae memory to my structure instance. I want to insert 10 rows in a table and want to store the corresponding transaction ID in one link list. But, I am not able to store them in a link list I alway loose the previous information and hence cannot traverse the linklist to store new transaction Id at the end of link list

Some senior in my course said, I am using wrong memory context. Can anyone explain me the actual problem?

I have defined my struct in a header file and trying to create a simple link list in one .c file.
 
Regards,
Rohit Goyal

Re: How to allocate space to structure instance

From
amul sul
Date:
>Can you please tel me the location in code where 

>I can allocate space before begin transaction call?
Its in your code.  your case it may be spi_connect(). 

> Right now, I am using spi_palloc function to alloctae memory to my structure instance

Ohh, but spi_palloc should work as you expected. I am not sure why you losing it.


>I want to insert 10 rows in a table and want to store the corresponding transaction ID in >one link list.
I am not sure is right method or not, why dont you allocated memory of structure in top context when your code start.


spi_connect()
/*
*
* SOME CODE
*/
oldcontext = CurrentMemoryContext;
        oldowner = CurrentResourceOwner;

 MemoryContextSwitchTo(mycontext);
 // Allocate memory for your structure.
 
MemoryContextSwitchTo(oldcontext);
        CurrentResourceOwner = oldowner; 
/*
*
* SOME CODE
*/
spi_finish();

let say, here mycontext is when main function of application called, you should store it as mycontext
=  CurrentMemoryContext;

I am not sure is this best practice, but it work for me.

Regards,
Amul Sul