Tom Lane wrote: > Craig Ringer <craig@xxxxxxxxxxxxxxxxxxxxx> writes: > > On 01/06/10 11:05, Tom Lane wrote: > >> I'd be interested to see a section like this written by someone who'd > >> actually done a nontrivial C++ extension and lived to tell the tale. > > > I can't speak up there - my own C++/Pg backend stuff has been fairly > > trivial, and has been where I can maintain a fairly clean separation of > > the C++-exposed and the Pg-backend-exposed parts. I was able to keep > > things separate enough that my C++ compilation units didn't include the > > Pg backend headers; they just exposed a pure C public interface. The Pg > > backend-using compilation units were written in C, and talked to the C++ > > part over its exposed pure C interfaces. > > Yeah, if you can design your code so that C++ never has to call back > into the core backend, that eliminates a large chunk of the pain. > Should we be documenting design ideas like this one? I have incorporated the new ideas into the C++ documentation section, and removed the comment block in the attached patch. -- Bruce Momjian <bruce@xxxxxxxxxx> http://momjian.us EnterpriseDB http://enterprisedb.com + None of us is going to be here forever. +
Index: doc/src/sgml/extend.sgml =================================================================== RCS file: /cvsroot/pgsql/doc/src/sgml/extend.sgml,v retrieving revision 1.42 diff -c -c -r1.42 extend.sgml *** doc/src/sgml/extend.sgml 1 Jun 2010 03:19:36 -0000 1.42 --- doc/src/sgml/extend.sgml 2 Jun 2010 01:20:23 -0000 *************** *** 273,280 **** &xoper; &xindex; - <!-- Use this someday when C++ is easier to use. bjm 2010-05-31 - <sect1 id="extend-Cpp"> <title>Using C++ for Extensibility</title> --- 273,278 ---- *************** *** 284,313 **** <para> It is possible to use a compiler in C++ mode to build ! <productname>PostgreSQL</productname> extensions; you must simply ! follow the standard methods for dynamically linking to C executables: <itemizedlist> <listitem> <para> ! Use <literal>extern C</> linkage for all functions that must ! be accessible by <function>dlopen()</>. This is also necessary ! for any functions that might be passed as pointers between ! the backend and C++ code. ! </para> ! </listitem> ! <listitem> ! <para> ! Use <function>palloc()</> to allocate any memory that might be ! freed by the backend C code (don't pass <function>new()</>-allocated ! memory). </para> </listitem> <listitem> <para> ! Use <function>pfree()</> to free memory allocated by the backend ! C code (do not use <function>delete()</> for such cases). ! </para> </listitem> <listitem> <para> --- 282,307 ---- <para> It is possible to use a compiler in C++ mode to build ! <productname>PostgreSQL</productname> extensions by following these ! guidelines: <itemizedlist> <listitem> <para> ! All functions accessed by the backend must present a C interface ! to the backend; these C functions can then call C++ functions. ! For example, <literal>extern C</> linkage is required for ! backend-accessed functions. This is also necessary for any ! functions that are passed as pointers between the backend and ! C++ code. </para> </listitem> <listitem> <para> ! Free memory using the appropriate deallocation method. For example, ! most backend memory is allocated using <function>palloc()</>, so use ! <function>pfree()</> to free it, i.e. using C++ ! <function>delete()</> in such cases will fail. </listitem> <listitem> <para> *************** *** 320,325 **** </para> </sect1> - --> </chapter> --- 314,318 ----
-- Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general