C Function Memory Management

Started by Martin Frenchover 13 years ago7 messagesgeneral
Jump to latest
#1Martin French
Martin.French@romaxtech.com

Ok, so just a (hopefully) quick question.

I know that I should be using the Postgres function palloc in place of
malloc, but what about realloc? Is there an equivalent?

Thanks.
=============================================

Romax Technology Limited
Rutherford House
Nottingham Science & Technology Park
Nottingham,
NG7 2PZ
England

Telephone numbers:
+44 (0)115 951 88 00 (main)

For other office locations see:
http://www.romaxtech.com/Contact
=================================
===============
E-mail: info@romaxtech.com
Website: www.romaxtech.com
=================================

================
Confidentiality Statement
This transmission is for the addressee only and contains information that
is confidential and privileged.
Unless you are the named addressee, or authorised to receive it on behalf
of the addressee
you may not copy or use it, or disclose it to anyone else.
If you have received this transmission in error please delete from your
system and contact the sender. Thank you for your cooperation.
=================================================

#2Pavel Stehule
pavel.stehule@gmail.com
In reply to: Martin French (#1)
Re: C Function Memory Management

Hello

2012/12/12 Martin French <Martin.French@romaxtech.com>:

Ok, so just a (hopefully) quick question.

I know that I should be using the Postgres function palloc in place of
malloc, but what about realloc? Is there an equivalent?

yes,

repalloc

Regards

Pavel Stehule

Thanks.
=============================================

Romax Technology Limited
Rutherford House
Nottingham Science & Technology Park
Nottingham,
NG7 2PZ
England

Telephone numbers:
+44 (0)115 951 88 00 (main)

For other office locations see:
http://www.romaxtech.com/Contact
=================================
===============
E-mail: info@romaxtech.com
Website: www.romaxtech.com
=================================

================
Confidentiality Statement
This transmission is for the addressee only and contains information that is
confidential and privileged.
Unless you are the named addressee, or authorised to receive it on behalf of
the addressee
you may not copy or use it, or disclose it to anyone else.
If you have received this transmission in error please delete from your
system and contact the sender. Thank you for your cooperation.
=================================================

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#3Martin French
Martin.French@romaxtech.com
In reply to: Pavel Stehule (#2)
Re: C Function Memory Management

Hi Pavel

yes,

repalloc

Maybe I should've looked a little harder for that one! Ha Ha!

I assume it's an exact functional copy of realloc, and see where I get
with it!

Many Thanks.
=============================================

Romax Technology Limited
Rutherford House
Nottingham Science & Technology Park
Nottingham,
NG7 2PZ
England

Telephone numbers:
+44 (0)115 951 88 00 (main)

For other office locations see:
http://www.romaxtech.com/Contact
=================================
===============
E-mail: info@romaxtech.com
Website: www.romaxtech.com
=================================

================
Confidentiality Statement
This transmission is for the addressee only and contains information that
is confidential and privileged.
Unless you are the named addressee, or authorised to receive it on behalf
of the addressee
you may not copy or use it, or disclose it to anyone else.
If you have received this transmission in error please delete from your
system and contact the sender. Thank you for your cooperation.
=================================================

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Martin French (#3)
Re: C Function Memory Management

Martin French <Martin.French@romaxtech.com> writes:

repalloc

Maybe I should've looked a little harder for that one! Ha Ha!

I assume it's an exact functional copy of realloc, and see where I get
with it!

One thing to know about it is that repalloc re-sizes the given chunk
while keeping it in the same memory context it was originally allocated
in (ie, CurrentMemoryContext doesn't matter). This may not matter much
if you're only dealing with allocations that just live as long as the
current function call --- but if you're trying to preserve data across
calls, it's critical to know.

regards, tom lane

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#5Martin French
Martin.French@romaxtech.com
In reply to: Tom Lane (#4)
Re: C Function Memory Management

One thing to know about it is that repalloc re-sizes the given chunk
while keeping it in the same memory context it was originally allocated
in (ie, CurrentMemoryContext doesn't matter). This may not matter much
if you're only dealing with allocations that just live as long as the
current function call --- but if you're trying to preserve data across
calls, it's critical to know.

regards, tom lane

Thanks for this Tom, I'll keep that in mind for future functions.

In this particular implementation, it's not necessary to preserve the data
once the function is complete.

So out of curiosity, what would be your approach for data preservation
cross call?

Cheers

Martin
=============================================

Romax Technology Limited
Rutherford House
Nottingham Science & Technology Park
Nottingham,
NG7 2PZ
England

Telephone numbers:
+44 (0)115 951 88 00 (main)

For other office locations see:
http://www.romaxtech.com/Contact
=================================
===============
E-mail: info@romaxtech.com
Website: www.romaxtech.com
=================================

================
Confidentiality Statement
This transmission is for the addressee only and contains information that
is confidential and privileged.
Unless you are the named addressee, or authorised to receive it on behalf
of the addressee
you may not copy or use it, or disclose it to anyone else.
If you have received this transmission in error please delete from your
system and contact the sender. Thank you for your cooperation.
=================================================

#6Tom Lane
tgl@sss.pgh.pa.us
In reply to: Martin French (#5)
Re: C Function Memory Management

Martin French <Martin.French@romaxtech.com> writes:

So out of curiosity, what would be your approach for data preservation
cross call?

Usually I'd allocate the space in fcinfo->flinfo->fn_mcxt (which is a
query-lifespan context) and keep a pointer to it in
fcinfo->flinfo->fn_extra (which is a void * field that a C function
can do what it pleases with). There are plenty of examples in the
Postgres source code. If you are looking to cache data longer than
one query then it takes more thought.

regards, tom lane

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#7Martin French
Martin.French@romaxtech.com
In reply to: Tom Lane (#6)
Re: C Function Memory Management

Usually I'd allocate the space in fcinfo->flinfo->fn_mcxt (which is a
query-lifespan context) and keep a pointer to it in
fcinfo->flinfo->fn_extra (which is a void * field that a C function
can do what it pleases with). There are plenty of examples in the
Postgres source code. If you are looking to cache data longer than
one query then it takes more thought.

regards, tom lane

Thanks Tom. I'll have a look into this when I have more time.

Kind Regards.

Martin.
=============================================

Romax Technology Limited
Rutherford House
Nottingham Science & Technology Park
Nottingham,
NG7 2PZ
England

Telephone numbers:
+44 (0)115 951 88 00 (main)

For other office locations see:
http://www.romaxtech.com/Contact
=================================
===============
E-mail: info@romaxtech.com
Website: www.romaxtech.com
=================================

================
Confidentiality Statement
This transmission is for the addressee only and contains information that
is confidential and privileged.
Unless you are the named addressee, or authorised to receive it on behalf
of the addressee
you may not copy or use it, or disclose it to anyone else.
If you have received this transmission in error please delete from your
system and contact the sender. Thank you for your cooperation.
=================================================