<literal>__builtin_constant_p</literal>.
</para>
</simplesect>
<simplesect id="source-conventions-macros-inline">
<title>Function-Like Macros and Inline Functions</title>
<para>
Both macros with arguments and <literal>static inline</literal>
functions may be used. The latter are preferable if there are
multiple-evaluation hazards when written as a macro, as e.g., the
case with
<programlisting>
#define Max(x, y) ((x) > (y) ? (x) : (y))
</programlisting>
or when the macro would be very long. In other cases it's only
possible to use macros, or at least easier. For example because
expressions of various types need to be passed to the macro.
</para>
<para>
When the definition of an inline function references symbols
(i.e., variables, functions) that are only available as part of the
backend, the function may not be visible when included from frontend
code.
<programlisting>
#ifndef FRONTEND
static inline MemoryContext
MemoryContextSwitchTo(MemoryContext context)
{
MemoryContext old = CurrentMemoryContext;
CurrentMemoryContext = context;
return old;
}
#endif /* FRONTEND */
</programlisting>
In this example <literal>CurrentMemoryContext</literal>, which is only
available in the backend, is referenced and the function thus
hidden with a <literal>#ifndef FRONTEND</literal>. This rule
exists because some compilers emit references to symbols
contained in inline functions even if the function is not used.
</para>
</simplesect>
<simplesect id="source-conventions-signal-handlers">
<title>Writing Signal Handlers</title>
<para>
To be suitable to run inside a signal handler code has to be
written very carefully. The fundamental problem is that, unless
blocked, a signal handler can interrupt code at any time. If code
inside the signal handler uses the same state as code outside
chaos may ensue. As an example consider what happens if a signal
handler tries to acquire a lock that's already held in the
interrupted code.
</para>
<para>
Barring special arrangements code in signal handlers may only
call async-signal safe functions (as defined in POSIX) and access
variables of type <literal>volatile sig_atomic_t</literal>. A few
functions in <command>postgres</command> are also deemed signal safe, importantly
<function>SetLatch()</function>.
</para>
<para>
In most cases signal handlers should do nothing more than note
that a signal has arrived, and wake up code running outside of
the handler using a latch. An example of such a handler is the
following:
<programlisting>
static void
handle_sighup(SIGNAL_ARGS)
{
got_SIGHUP = true;
SetLatch(MyLatch);
}
</programlisting>
</para>
</simplesect>
<simplesect id="source-conventions-function-pointers">
<title>Calling Function Pointers</title>
<para>
For clarity, it is preferred to explicitly dereference a function pointer
when calling the pointed-to function if the pointer is a simple variable,
for example:
<programlisting>
(*emit_log_hook) (edata);
</programlisting>
(even though <literal>emit_log_hook(edata)</literal> would also work).
When the function pointer is part of a structure, then the extra
punctuation can and usually should be omitted, for example:
<programlisting>
paramInfo->paramFetch(paramInfo, paramId);
</programlisting>
</para>
</simplesect>
</sect1>
</chapter>