Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3b19a45

Browse files
committed
Document use of C++ for extension use.
1 parent7dff726 commit3b19a45

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

‎doc/src/sgml/extend.sgml

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/extend.sgml,v 1.42 2010/06/01 03:19:36 momjian Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/extend.sgml,v 1.43 2010/06/03 14:39:58 momjian Exp $ -->
22

33
<chapter id="extend">
44
<title>Extending <acronym>SQL</acronym></title>
@@ -273,8 +273,6 @@
273273
&xoper;
274274
&xindex;
275275

276-
<!-- Use this someday when C++ is easier to use. bjm 2010-05-31
277-
278276
<sect1 id="extend-Cpp">
279277
<title>Using C++ for Extensibility</title>
280278

@@ -284,42 +282,57 @@
284282

285283
<para>
286284
It is possible to use a compiler in C++ mode to build
287-
<productname>PostgreSQL</productname> extensions; you must simply
288-
follow the standard methods for dynamically linking to C executables:
285+
<productname>PostgreSQL</productname> extensions by following these
286+
guidelines:
289287

290288
<itemizedlist>
291289
<listitem>
292290
<para>
293-
Use <literal>extern C</> linkage for all functions that must
294-
be accessible by <function>dlopen()</>. This is also necessary
295-
for any functions that might be passed as pointers between
296-
the backend and C++ code.
291+
All functions accessed by the backend must present a C interface
292+
to the backend; these C functions can then call C++ functions.
293+
For example, <literal>extern C</> linkage is required for
294+
backend-accessed functions. This is also necessary for any
295+
functions that are passed as pointers between the backend and
296+
C++ code.
297297
</para>
298298
</listitem>
299299
<listitem>
300300
<para>
301-
Use <function>palloc()</> to allocate any memory that might be
302-
freed by thebackendC code (don't pass <function>new()</>-allocated
303-
memory).
304-
</para>
301+
Free memory using the appropriate deallocation method. For example,
302+
mostbackendmemory is allocated using <function>palloc()</>, so use
303+
<function>pfree()</> to free it, i.e. using C++
304+
<function>delete()</> in such cases will fail.
305305
</listitem>
306306
<listitem>
307307
<para>
308-
Use <function>pfree()</> to free memory allocated by the backend
309-
C code (do not use <function>delete()</> for such cases).
308+
Prevent exceptions from propagating into the C code (use a
309+
catch-all block at the top level of all <literal>extern C</>
310+
functions). This is necessary even if the C++ code does not
311+
throw any exceptions because events like out-of-memory still
312+
throw exceptions. Any exceptions must be caught and appropriate
313+
errors passed back to the C interface. If possible, compile C++
314+
with <option>-fno-exceptions</> to eliminate exceptions entirely;
315+
in such cases, you must check for failures in your C++ code, e.g.
316+
check for NULL returned by <function>new()</>.
310317
</para>
311318
</listitem>
312319
<listitem>
313320
<para>
314-
Prevent exceptions from propagating into the C code (use a
315-
catch-all block at the top level of all <literal>extern C</>
316-
functions).
321+
If calling backend functions from C++ code, be sure that the
322+
C++ call stack contains only plain old data structure
323+
(<acronym>POD</>). This is necessary because backend errors
324+
generate a distant <function>longjump()</> that does not properly
325+
unroll a C++ call stack with non-POD objects.
317326
</para>
318327
</listitem>
319328
</itemizedlist>
320329
</para>
321330

331+
<para>
332+
In summary, it is best to place C++ code behind a wall of
333+
<literal>extern C</> functions that interface to the backend,
334+
and avoid exception, memory, and call stack leakage.
335+
</para>
322336
</sect1>
323-
-->
324337

325338
</chapter>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp