1
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>54.4. Miscellaneous Coding Conventions</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="error-style-guide.html" title="54.3. Error Message Style Guide" /><link rel="next" href="nls.html" title="Chapter 55. Native Language Support" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">54.4. Miscellaneous Coding Conventions</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="error-style-guide.html" title="54.3. Error Message Style Guide">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="source.html" title="Chapter 54. PostgreSQL Coding Conventions">Up</a></td><th width="60%" align="center">Chapter 54. PostgreSQL Coding Conventions</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 11beta1 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="nls.html" title="Chapter 55. Native Language Support">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="SOURCE-CONVENTIONS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">54.4. Miscellaneous Coding Conventions</h2></div></div></div><div class="simplesect" id="id-1.10.6.5.2"><div class="titlepage"><div><div><h3 class="title">C Standard</h3></div></div></div><p>
3
Code in <span class="productname">PostgreSQL</span> should only rely on language
4
features available in the C89 standard. That means a conforming
5
C89 compiler has to be able to compile postgres, at least aside
6
from a few platform dependent pieces. Features from later
7
revision of the C standard or compiler specific features can be
8
used, if a fallback is provided.
10
For example <code class="literal">static inline</code> and
11
<code class="literal">_StaticAssert()</code> are currently used, even
12
though they are from newer revisions of the C standard. If not
13
available we respectively fall back to defining the functions
14
without inline, and to using a C89 compatible replacement that
15
performs the same checks, but emits rather cryptic messages.
16
</p></div><div class="simplesect" id="id-1.10.6.5.3"><div class="titlepage"><div><div><h3 class="title">Function-Like Macros and Inline Functions</h3></div></div></div><p>
17
Both, macros with arguments and <code class="literal">static inline</code>
18
functions, may be used. The latter are preferable if there are
19
multiple-evaluation hazards when written as a macro, as e.g. the
21
</p><pre class="programlisting">
22
#define Max(x, y) ((x) > (y) ? (x) : (y))
24
or when the macro would be very long. In other cases it's only
25
possible to use macros, or at least easier. For example because
26
expressions of various types need to be passed to the macro.
28
When the definition of an inline function references symbols
29
(i.e. variables, functions) that are only available as part of the
30
backend, the function may not be visible when included from frontend
32
</p><pre class="programlisting">
34
static inline MemoryContext
35
MemoryContextSwitchTo(MemoryContext context)
37
MemoryContext old = CurrentMemoryContext;
39
CurrentMemoryContext = context;
44
In this example <code class="literal">CurrentMemoryContext</code>, which is only
45
available in the backend, is referenced and the function thus
46
hidden with a <code class="literal">#ifndef FRONTEND</code>. This rule
47
exists because some compilers emit references to symbols
48
contained in inline functions even if the function is not used.
49
</p></div><div class="simplesect" id="id-1.10.6.5.4"><div class="titlepage"><div><div><h3 class="title">Writing Signal Handlers</h3></div></div></div><p>
50
To be suitable to run inside a signal handler code has to be
51
written very carefully. The fundamental problem is that, unless
52
blocked, a signal handler can interrupt code at any time. If code
53
inside the signal handler uses the same state as code outside
54
chaos may ensue. As an example consider what happens if a signal
55
handler tries to acquire a lock that's already held in the
58
Barring special arrangements code in signal handlers may only
59
call async-signal safe functions (as defined in POSIX) and access
60
variables of type <code class="literal">volatile sig_atomic_t</code>. A few
61
functions in <code class="command">postgres</code> are also deemed signal safe, importantly
62
<code class="function">SetLatch()</code>.
64
In most cases signal handlers should do nothing more than note
65
that a signal has arrived, and wake up code running outside of
66
the handler using a latch. An example of such a handler is the
68
</p><pre class="programlisting">
70
handle_sighup(SIGNAL_ARGS)
72
int save_errno = errno;
80
<code class="varname">errno</code> is saved and restored because
81
<code class="function">SetLatch()</code> might change it. If that were not done
82
interrupted code that's currently inspecting <code class="varname">errno</code> might see the wrong
84
</p></div><div class="simplesect" id="id-1.10.6.5.5"><div class="titlepage"><div><div><h3 class="title">Calling Function Pointers</h3></div></div></div><p>
85
For clarity, it is preferred to explicitly dereference a function pointer
86
when calling the pointed-to function if the pointer is a simple variable,
88
</p><pre class="programlisting">
89
(*emit_log_hook) (edata);
91
(even though <code class="literal">emit_log_hook(edata)</code> would also work).
92
When the function pointer is part of a structure, then the extra
93
punctuation can and usually should be omitted, for example:
94
</p><pre class="programlisting">
95
paramInfo->paramFetch(paramInfo, paramId);
97
</p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="error-style-guide.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="source.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="nls.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">54.3. Error Message Style Guide </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 55. Native Language Support</td></tr></table></div></body></html>
b'\\ No newline at end of file'