xref: /dpdk/doc/guides/contributing/coding_style.rst (revision 6bdae907647691fe2f380efcd281cc276fafb3d0)
1*6bdae907SThomas Monjalon.. _coding_style:
2*6bdae907SThomas Monjalon
3*6bdae907SThomas MonjalonDPDK Coding Style
4*6bdae907SThomas Monjalon=================
5*6bdae907SThomas Monjalon
6*6bdae907SThomas MonjalonDescription
7*6bdae907SThomas Monjalon-----------
8*6bdae907SThomas Monjalon
9*6bdae907SThomas MonjalonThis document specifies the preferred style for source files in the DPDK source tree.
10*6bdae907SThomas MonjalonIt is based on the Linux Kernel coding guidelines and the FreeBSD 7.2 Kernel Developer's Manual (see man style(9)), but was heavily modified for the needs of the DPDK.
11*6bdae907SThomas Monjalon
12*6bdae907SThomas MonjalonGeneral Guidelines
13*6bdae907SThomas Monjalon------------------
14*6bdae907SThomas Monjalon
15*6bdae907SThomas MonjalonThe rules and guidelines given in this document cannot cover every situation, so the following general guidelines should be used as a fallback:
16*6bdae907SThomas Monjalon
17*6bdae907SThomas Monjalon* The code style should be consistent within each individual file.
18*6bdae907SThomas Monjalon* In the case of creating new files, the style should be consistent within each file in a given directory or module.
19*6bdae907SThomas Monjalon* The primary reason for coding standards is to increase code readability and comprehensibility, therefore always use whatever option will make the code easiest to read.
20*6bdae907SThomas Monjalon
21*6bdae907SThomas MonjalonLine length is recommended to be not more than 80 characters, including comments.
22*6bdae907SThomas Monjalon[Tab stop size should be assumed to be 8-characters wide].
23*6bdae907SThomas Monjalon
24*6bdae907SThomas Monjalon.. note::
25*6bdae907SThomas Monjalon
26*6bdae907SThomas Monjalon	The above is recommendation, and not a hard limit.
27*6bdae907SThomas Monjalon	However, it is expected that the recommendations should be followed in all but the rarest situations.
28*6bdae907SThomas Monjalon
29*6bdae907SThomas MonjalonC Comment Style
30*6bdae907SThomas Monjalon---------------
31*6bdae907SThomas Monjalon
32*6bdae907SThomas MonjalonUsual Comments
33*6bdae907SThomas Monjalon~~~~~~~~~~~~~~
34*6bdae907SThomas Monjalon
35*6bdae907SThomas MonjalonThese comments should be used in normal cases.
36*6bdae907SThomas MonjalonTo document a public API, a doxygen-like format must be used: refer to :ref:`doxygen_guidelines`.
37*6bdae907SThomas Monjalon
38*6bdae907SThomas Monjalon.. code-block:: c
39*6bdae907SThomas Monjalon
40*6bdae907SThomas Monjalon /*
41*6bdae907SThomas Monjalon  * VERY important single-line comments look like this.
42*6bdae907SThomas Monjalon  */
43*6bdae907SThomas Monjalon
44*6bdae907SThomas Monjalon /* Most single-line comments look like this. */
45*6bdae907SThomas Monjalon
46*6bdae907SThomas Monjalon /*
47*6bdae907SThomas Monjalon  * Multi-line comments look like this.  Make them real sentences. Fill
48*6bdae907SThomas Monjalon  * them so they look like real paragraphs.
49*6bdae907SThomas Monjalon  */
50*6bdae907SThomas Monjalon
51*6bdae907SThomas MonjalonLicense Header
52*6bdae907SThomas Monjalon~~~~~~~~~~~~~~
53*6bdae907SThomas Monjalon
54*6bdae907SThomas MonjalonEach file should begin with a special comment containing the appropriate copyright and license for the file.
55*6bdae907SThomas MonjalonGenerally this is the BSD License, except for code for Linux Kernel modules.
56*6bdae907SThomas MonjalonAfter any copyright header, a blank line should be left before any other contents, e.g. include statements in a C file.
57*6bdae907SThomas Monjalon
58*6bdae907SThomas MonjalonC Preprocessor Directives
59*6bdae907SThomas Monjalon-------------------------
60*6bdae907SThomas Monjalon
61*6bdae907SThomas MonjalonHeader Includes
62*6bdae907SThomas Monjalon~~~~~~~~~~~~~~~
63*6bdae907SThomas Monjalon
64*6bdae907SThomas MonjalonIn DPDK sources, the include files should be ordered as following:
65*6bdae907SThomas Monjalon
66*6bdae907SThomas Monjalon#. libc includes (system includes first)
67*6bdae907SThomas Monjalon#. DPDK EAL includes
68*6bdae907SThomas Monjalon#. DPDK misc libraries includes
69*6bdae907SThomas Monjalon#. application-specific includes
70*6bdae907SThomas Monjalon
71*6bdae907SThomas MonjalonInclude files from the local application directory are included using quotes, while includes from other paths are included using angle brackets: "<>".
72*6bdae907SThomas Monjalon
73*6bdae907SThomas MonjalonExample:
74*6bdae907SThomas Monjalon
75*6bdae907SThomas Monjalon.. code-block:: c
76*6bdae907SThomas Monjalon
77*6bdae907SThomas Monjalon #include <stdio.h>
78*6bdae907SThomas Monjalon #include <stdlib.h>
79*6bdae907SThomas Monjalon
80*6bdae907SThomas Monjalon #include <rte_eal.h>
81*6bdae907SThomas Monjalon
82*6bdae907SThomas Monjalon #include <rte_ring.h>
83*6bdae907SThomas Monjalon #include <rte_mempool.h>
84*6bdae907SThomas Monjalon
85*6bdae907SThomas Monjalon #include "application.h"
86*6bdae907SThomas Monjalon
87*6bdae907SThomas MonjalonHeader File Guards
88*6bdae907SThomas Monjalon~~~~~~~~~~~~~~~~~~
89*6bdae907SThomas Monjalon
90*6bdae907SThomas MonjalonHeaders should be protected against multiple inclusion with the usual:
91*6bdae907SThomas Monjalon
92*6bdae907SThomas Monjalon.. code-block:: c
93*6bdae907SThomas Monjalon
94*6bdae907SThomas Monjalon   #ifndef _FILE_H_
95*6bdae907SThomas Monjalon   #define _FILE_H_
96*6bdae907SThomas Monjalon
97*6bdae907SThomas Monjalon   /* Code */
98*6bdae907SThomas Monjalon
99*6bdae907SThomas Monjalon   #endif /* _FILE_H_ */
100*6bdae907SThomas Monjalon
101*6bdae907SThomas Monjalon
102*6bdae907SThomas MonjalonMacros
103*6bdae907SThomas Monjalon~~~~~~
104*6bdae907SThomas Monjalon
105*6bdae907SThomas MonjalonDo not ``#define`` or declare names except with the standard DPDK prefix: ``RTE_``.
106*6bdae907SThomas MonjalonThis is to ensure there are no collisions with definitions in the application itself.
107*6bdae907SThomas Monjalon
108*6bdae907SThomas MonjalonThe names of "unsafe" macros (ones that have side effects), and the names of macros for manifest constants, are all in uppercase.
109*6bdae907SThomas Monjalon
110*6bdae907SThomas MonjalonThe expansions of expression-like macros are either a single token or have outer parentheses.
111*6bdae907SThomas MonjalonIf a macro is an inline expansion of a function, the function name is all in lowercase and the macro has the same name all in uppercase.
112*6bdae907SThomas MonjalonIf the macro encapsulates a compound statement, enclose it in a do-while loop, so that it can be used safely in if statements.
113*6bdae907SThomas MonjalonAny final statement-terminating semicolon should be supplied by the macro invocation rather than the macro, to make parsing easier for pretty-printers and editors.
114*6bdae907SThomas Monjalon
115*6bdae907SThomas MonjalonFor example:
116*6bdae907SThomas Monjalon
117*6bdae907SThomas Monjalon.. code-block:: c
118*6bdae907SThomas Monjalon
119*6bdae907SThomas Monjalon #define MACRO(x, y) do {                                        \
120*6bdae907SThomas Monjalon         variable = (x) + (y);                                   \
121*6bdae907SThomas Monjalon         (y) += 2;                                               \
122*6bdae907SThomas Monjalon } while(0)
123*6bdae907SThomas Monjalon
124*6bdae907SThomas Monjalon.. note::
125*6bdae907SThomas Monjalon
126*6bdae907SThomas Monjalon Wherever possible, enums and inline functions should be preferred to macros, since they provide additional degrees of type-safety and can allow compilers to emit extra warnings about unsafe code.
127*6bdae907SThomas Monjalon
128*6bdae907SThomas MonjalonConditional Compilation
129*6bdae907SThomas Monjalon~~~~~~~~~~~~~~~~~~~~~~~
130*6bdae907SThomas Monjalon
131*6bdae907SThomas Monjalon* When code is conditionally compiled using ``#ifdef`` or ``#if``, a comment may be added following the matching
132*6bdae907SThomas Monjalon  ``#endif`` or ``#else`` to permit the reader to easily discern where conditionally compiled code regions end.
133*6bdae907SThomas Monjalon* This comment should be used only for (subjectively) long regions, regions greater than 20 lines, or where a series of nested ``#ifdef``'s may be confusing to the reader.
134*6bdae907SThomas Monjalon  Exceptions may be made for cases where code is conditionally not compiled for the purposes of lint(1), or other tools, even though the uncompiled region may be small.
135*6bdae907SThomas Monjalon* The comment should be separated from the ``#endif`` or ``#else`` by a single space.
136*6bdae907SThomas Monjalon* For short conditionally compiled regions, a closing comment should not be used.
137*6bdae907SThomas Monjalon* The comment for ``#endif`` should match the expression used in the corresponding ``#if`` or ``#ifdef``.
138*6bdae907SThomas Monjalon* The comment for ``#else`` and ``#elif`` should match the inverse of the expression(s) used in the preceding ``#if`` and/or ``#elif`` statements.
139*6bdae907SThomas Monjalon* In the comments, the subexpression ``defined(FOO)`` is abbreviated as "FOO".
140*6bdae907SThomas Monjalon  For the purposes of comments, ``#ifndef FOO`` is treated as ``#if !defined(FOO)``.
141*6bdae907SThomas Monjalon
142*6bdae907SThomas Monjalon.. code-block:: c
143*6bdae907SThomas Monjalon
144*6bdae907SThomas Monjalon #ifdef KTRACE
145*6bdae907SThomas Monjalon #include <sys/ktrace.h>
146*6bdae907SThomas Monjalon #endif
147*6bdae907SThomas Monjalon
148*6bdae907SThomas Monjalon #ifdef COMPAT_43
149*6bdae907SThomas Monjalon /* A large region here, or other conditional code. */
150*6bdae907SThomas Monjalon #else /* !COMPAT_43 */
151*6bdae907SThomas Monjalon /* Or here. */
152*6bdae907SThomas Monjalon #endif /* COMPAT_43 */
153*6bdae907SThomas Monjalon
154*6bdae907SThomas Monjalon #ifndef COMPAT_43
155*6bdae907SThomas Monjalon /* Yet another large region here, or other conditional code. */
156*6bdae907SThomas Monjalon #else /* COMPAT_43 */
157*6bdae907SThomas Monjalon /* Or here. */
158*6bdae907SThomas Monjalon #endif /* !COMPAT_43 */
159*6bdae907SThomas Monjalon
160*6bdae907SThomas Monjalon.. note::
161*6bdae907SThomas Monjalon
162*6bdae907SThomas Monjalon Conditional compilation should be used only when absolutely necessary, as it increases the number of target binaries that need to be built and tested.
163*6bdae907SThomas Monjalon
164*6bdae907SThomas MonjalonC Types
165*6bdae907SThomas Monjalon-------
166*6bdae907SThomas Monjalon
167*6bdae907SThomas MonjalonIntegers
168*6bdae907SThomas Monjalon~~~~~~~~
169*6bdae907SThomas Monjalon
170*6bdae907SThomas MonjalonFor fixed/minimum-size integer values, the project uses the form uintXX_t (from stdint.h) instead of older BSD-style integer identifiers of the form u_intXX_t.
171*6bdae907SThomas Monjalon
172*6bdae907SThomas MonjalonEnumerations
173*6bdae907SThomas Monjalon~~~~~~~~~~~~
174*6bdae907SThomas Monjalon
175*6bdae907SThomas Monjalon* Enumeration values are all uppercase.
176*6bdae907SThomas Monjalon
177*6bdae907SThomas Monjalon.. code-block:: c
178*6bdae907SThomas Monjalon
179*6bdae907SThomas Monjalon enum enumtype { ONE, TWO } et;
180*6bdae907SThomas Monjalon
181*6bdae907SThomas Monjalon* Enum types should be used in preference to macros #defining a set of (sequential) values.
182*6bdae907SThomas Monjalon* Enum types should be prefixed with ``rte_`` and the elements by a suitable prefix [generally starting ``RTE_<enum>_`` - where <enum> is a shortname for the enum type] to avoid namespace collisions.
183*6bdae907SThomas Monjalon
184*6bdae907SThomas MonjalonBitfields
185*6bdae907SThomas Monjalon~~~~~~~~~
186*6bdae907SThomas Monjalon
187*6bdae907SThomas MonjalonThe developer should group bitfields that are included in the same integer, as follows:
188*6bdae907SThomas Monjalon
189*6bdae907SThomas Monjalon.. code-block:: c
190*6bdae907SThomas Monjalon
191*6bdae907SThomas Monjalon struct grehdr {
192*6bdae907SThomas Monjalon   uint16_t rec:3,
193*6bdae907SThomas Monjalon       srr:1,
194*6bdae907SThomas Monjalon       seq:1,
195*6bdae907SThomas Monjalon       key:1,
196*6bdae907SThomas Monjalon       routing:1,
197*6bdae907SThomas Monjalon       csum:1,
198*6bdae907SThomas Monjalon       version:3,
199*6bdae907SThomas Monjalon       reserved:4,
200*6bdae907SThomas Monjalon       ack:1;
201*6bdae907SThomas Monjalon /* ... */
202*6bdae907SThomas Monjalon }
203*6bdae907SThomas Monjalon
204*6bdae907SThomas MonjalonVariable Declarations
205*6bdae907SThomas Monjalon~~~~~~~~~~~~~~~~~~~~~
206*6bdae907SThomas Monjalon
207*6bdae907SThomas MonjalonIn declarations, do not put any whitespace between asterisks and adjacent tokens, except for tokens that are identifiers related to types.
208*6bdae907SThomas Monjalon(These identifiers are the names of basic types, type qualifiers, and typedef-names other than the one being declared.)
209*6bdae907SThomas MonjalonSeparate these identifiers from asterisks using a single space.
210*6bdae907SThomas Monjalon
211*6bdae907SThomas MonjalonFor example:
212*6bdae907SThomas Monjalon
213*6bdae907SThomas Monjalon.. code-block:: c
214*6bdae907SThomas Monjalon
215*6bdae907SThomas Monjalon   int *x;         /* no space after asterisk */
216*6bdae907SThomas Monjalon   int * const x;  /* space after asterisk when using a type qualifier */
217*6bdae907SThomas Monjalon
218*6bdae907SThomas Monjalon* All externally-visible variables should have an ``rte_`` prefix in the name to avoid namespace collisions.
219*6bdae907SThomas Monjalon* Do not use uppercase letters - either in the form of ALL_UPPERCASE, or CamelCase - in variable names.
220*6bdae907SThomas Monjalon  Lower-case letters and underscores only.
221*6bdae907SThomas Monjalon
222*6bdae907SThomas MonjalonStructure Declarations
223*6bdae907SThomas Monjalon~~~~~~~~~~~~~~~~~~~~~~
224*6bdae907SThomas Monjalon
225*6bdae907SThomas Monjalon* In general, when declaring variables in new structures, declare them sorted by use, then by size (largest to smallest), and then in alphabetical order.
226*6bdae907SThomas Monjalon  Sorting by use means that commonly used variables are used together and that the structure layout makes logical sense.
227*6bdae907SThomas Monjalon  Ordering by size then ensures that as little padding is added to the structure as possible.
228*6bdae907SThomas Monjalon* For existing structures, additions to structures should be added to the end so for backward compatibility reasons.
229*6bdae907SThomas Monjalon* Each structure element gets its own line.
230*6bdae907SThomas Monjalon* Try to make the structure readable by aligning the member names using spaces as shown below.
231*6bdae907SThomas Monjalon* Names following extremely long types, which therefore cannot be easily aligned with the rest, should be separated by a single space.
232*6bdae907SThomas Monjalon
233*6bdae907SThomas Monjalon.. code-block:: c
234*6bdae907SThomas Monjalon
235*6bdae907SThomas Monjalon struct foo {
236*6bdae907SThomas Monjalon         struct foo      *next;          /* List of active foo. */
237*6bdae907SThomas Monjalon         struct mumble   amumble;        /* Comment for mumble. */
238*6bdae907SThomas Monjalon         int             bar;            /* Try to align the comments. */
239*6bdae907SThomas Monjalon         struct verylongtypename *baz;   /* Won't fit with other members */
240*6bdae907SThomas Monjalon };
241*6bdae907SThomas Monjalon
242*6bdae907SThomas Monjalon
243*6bdae907SThomas Monjalon* Major structures should be declared at the top of the file in which they are used, or in separate header files if they are used in multiple source files.
244*6bdae907SThomas Monjalon* Use of the structures should be by separate variable declarations and those declarations must be extern if they are declared in a header file.
245*6bdae907SThomas Monjalon* Externally visible structure definitions should have the structure name prefixed by ``rte_`` to avoid namespace collisions.
246*6bdae907SThomas Monjalon
247*6bdae907SThomas MonjalonQueues
248*6bdae907SThomas Monjalon~~~~~~
249*6bdae907SThomas Monjalon
250*6bdae907SThomas MonjalonUse queue(3) macros rather than rolling your own lists, whenever possible.
251*6bdae907SThomas MonjalonThus, the previous example would be better written:
252*6bdae907SThomas Monjalon
253*6bdae907SThomas Monjalon.. code-block:: c
254*6bdae907SThomas Monjalon
255*6bdae907SThomas Monjalon #include <sys/queue.h>
256*6bdae907SThomas Monjalon
257*6bdae907SThomas Monjalon struct foo {
258*6bdae907SThomas Monjalon         LIST_ENTRY(foo) link;      /* Use queue macros for foo lists. */
259*6bdae907SThomas Monjalon         struct mumble   amumble;   /* Comment for mumble. */
260*6bdae907SThomas Monjalon         int             bar;       /* Try to align the comments. */
261*6bdae907SThomas Monjalon         struct verylongtypename *baz;   /* Won't fit with other members */
262*6bdae907SThomas Monjalon };
263*6bdae907SThomas Monjalon LIST_HEAD(, foo) foohead;          /* Head of global foo list. */
264*6bdae907SThomas Monjalon
265*6bdae907SThomas Monjalon
266*6bdae907SThomas MonjalonDPDK also provides an optimized way to store elements in lockless rings.
267*6bdae907SThomas MonjalonThis should be used in all data-path code, when there are several consumer and/or producers to avoid locking for concurrent access.
268*6bdae907SThomas Monjalon
269*6bdae907SThomas MonjalonTypedefs
270*6bdae907SThomas Monjalon~~~~~~~~
271*6bdae907SThomas Monjalon
272*6bdae907SThomas MonjalonAvoid using typedefs for structure types.
273*6bdae907SThomas Monjalon
274*6bdae907SThomas MonjalonFor example, use:
275*6bdae907SThomas Monjalon
276*6bdae907SThomas Monjalon.. code-block:: c
277*6bdae907SThomas Monjalon
278*6bdae907SThomas Monjalon struct my_struct_type {
279*6bdae907SThomas Monjalon /* ... */
280*6bdae907SThomas Monjalon };
281*6bdae907SThomas Monjalon
282*6bdae907SThomas Monjalon struct my_struct_type my_var;
283*6bdae907SThomas Monjalon
284*6bdae907SThomas Monjalon
285*6bdae907SThomas Monjalonrather than:
286*6bdae907SThomas Monjalon
287*6bdae907SThomas Monjalon.. code-block:: c
288*6bdae907SThomas Monjalon
289*6bdae907SThomas Monjalon typedef struct my_struct_type {
290*6bdae907SThomas Monjalon /* ... */
291*6bdae907SThomas Monjalon } my_struct_type;
292*6bdae907SThomas Monjalon
293*6bdae907SThomas Monjalon my_struct_type my_var
294*6bdae907SThomas Monjalon
295*6bdae907SThomas Monjalon
296*6bdae907SThomas MonjalonTypedefs are problematic because they do not properly hide their underlying type;
297*6bdae907SThomas Monjalonfor example, you need to know if the typedef is the structure itself, as shown above, or a pointer to the structure.
298*6bdae907SThomas MonjalonIn addition, they must be declared exactly once, whereas an incomplete structure type can be mentioned as many times as necessary.
299*6bdae907SThomas MonjalonTypedefs are difficult to use in stand-alone header files.
300*6bdae907SThomas MonjalonThe header that defines the typedef must be included before the header that uses it, or by the header that uses it (which causes namespace pollution),
301*6bdae907SThomas Monjalonor there must be a back-door mechanism for obtaining the typedef.
302*6bdae907SThomas Monjalon
303*6bdae907SThomas MonjalonNote that #defines used instead of typedefs also are problematic (since they do not propagate the pointer type correctly due to direct text replacement).
304*6bdae907SThomas MonjalonFor example, ``#define pint int *`` does not work as expected, while ``typedef int *pint`` does work.
305*6bdae907SThomas MonjalonAs stated when discussing macros, typedefs should be preferred to macros in cases like this.
306*6bdae907SThomas Monjalon
307*6bdae907SThomas MonjalonWhen convention requires a typedef; make its name match the struct tag.
308*6bdae907SThomas MonjalonAvoid typedefs ending in ``_t``, except as specified in Standard C or by POSIX.
309*6bdae907SThomas Monjalon
310*6bdae907SThomas Monjalon.. note::
311*6bdae907SThomas Monjalon
312*6bdae907SThomas Monjalon	It is recommended to use typedefs to define function pointer types, for reasons of code readability.
313*6bdae907SThomas Monjalon	This is especially true when the function type is used as a parameter to another function.
314*6bdae907SThomas Monjalon
315*6bdae907SThomas MonjalonFor example:
316*6bdae907SThomas Monjalon
317*6bdae907SThomas Monjalon.. code-block:: c
318*6bdae907SThomas Monjalon
319*6bdae907SThomas Monjalon	/**
320*6bdae907SThomas Monjalon	 * Definition of a remote launch function.
321*6bdae907SThomas Monjalon	 */
322*6bdae907SThomas Monjalon	typedef int (lcore_function_t)(void *);
323*6bdae907SThomas Monjalon
324*6bdae907SThomas Monjalon	/* launch a function of lcore_function_t type */
325*6bdae907SThomas Monjalon	int rte_eal_remote_launch(lcore_function_t *f, void *arg, unsigned slave_id);
326*6bdae907SThomas Monjalon
327*6bdae907SThomas Monjalon
328*6bdae907SThomas MonjalonC Indentation
329*6bdae907SThomas Monjalon-------------
330*6bdae907SThomas Monjalon
331*6bdae907SThomas MonjalonGeneral
332*6bdae907SThomas Monjalon~~~~~~~
333*6bdae907SThomas Monjalon
334*6bdae907SThomas Monjalon* Indentation is a hard tab, that is, a tab character, not a sequence of spaces,
335*6bdae907SThomas Monjalon
336*6bdae907SThomas Monjalon.. note::
337*6bdae907SThomas Monjalon
338*6bdae907SThomas Monjalon	Global whitespace rule in DPDK, use tabs for indentation, spaces for alignment.
339*6bdae907SThomas Monjalon
340*6bdae907SThomas Monjalon* Do not put any spaces before a tab for indentation.
341*6bdae907SThomas Monjalon* If you have to wrap a long statement, put the operator at the end of the line, and indent again.
342*6bdae907SThomas Monjalon* For control statements (if, while, etc.), continuation it is recommended that the next line be indented by two tabs, rather than one,
343*6bdae907SThomas Monjalon  to prevent confusion as to whether the second line of the control statement forms part of the statement body or not.
344*6bdae907SThomas Monjalon  Alternatively, the line continuation may use additional spaces to line up to an appropriately point on the preceding line, for example, to align to an opening brace.
345*6bdae907SThomas Monjalon
346*6bdae907SThomas Monjalon.. note::
347*6bdae907SThomas Monjalon
348*6bdae907SThomas Monjalon	As with all style guidelines, code should match style already in use in an existing file.
349*6bdae907SThomas Monjalon
350*6bdae907SThomas Monjalon.. code-block:: c
351*6bdae907SThomas Monjalon
352*6bdae907SThomas Monjalon while (really_long_variable_name_1 == really_long_variable_name_2 &&
353*6bdae907SThomas Monjalon     var3 == var4){  /* confusing to read as */
354*6bdae907SThomas Monjalon     x = y + z;      /* control stmt body lines up with second line of */
355*6bdae907SThomas Monjalon     a = b + c;      /* control statement itself if single indent used */
356*6bdae907SThomas Monjalon }
357*6bdae907SThomas Monjalon
358*6bdae907SThomas Monjalon if (really_long_variable_name_1 == really_long_variable_name_2 &&
359*6bdae907SThomas Monjalon         var3 == var4){  /* two tabs used */
360*6bdae907SThomas Monjalon     x = y + z;          /* statement body no longer lines up */
361*6bdae907SThomas Monjalon     a = b + c;
362*6bdae907SThomas Monjalon }
363*6bdae907SThomas Monjalon
364*6bdae907SThomas Monjalon z = a + really + long + statement + that + needs +
365*6bdae907SThomas Monjalon         two + lines + gets + indented + on + the +
366*6bdae907SThomas Monjalon         second + and + subsequent + lines;
367*6bdae907SThomas Monjalon
368*6bdae907SThomas Monjalon
369*6bdae907SThomas Monjalon* Do not add whitespace at the end of a line.
370*6bdae907SThomas Monjalon
371*6bdae907SThomas Monjalon* Do not add whitespace or a blank line at the end of a file.
372*6bdae907SThomas Monjalon
373*6bdae907SThomas Monjalon
374*6bdae907SThomas MonjalonControl Statements and Loops
375*6bdae907SThomas Monjalon~~~~~~~~~~~~~~~~~~~~~~~~~~~~
376*6bdae907SThomas Monjalon
377*6bdae907SThomas Monjalon* Include a space after keywords (if, while, for, return, switch).
378*6bdae907SThomas Monjalon* Do not use braces (``{`` and ``}``) for control statements with zero or just a single statement, unless that statement is more than a single line in which case the braces are permitted.
379*6bdae907SThomas Monjalon
380*6bdae907SThomas Monjalon.. code-block:: c
381*6bdae907SThomas Monjalon
382*6bdae907SThomas Monjalon for (p = buf; *p != '\0'; ++p)
383*6bdae907SThomas Monjalon         ;       /* nothing */
384*6bdae907SThomas Monjalon for (;;)
385*6bdae907SThomas Monjalon         stmt;
386*6bdae907SThomas Monjalon for (;;) {
387*6bdae907SThomas Monjalon         z = a + really + long + statement + that + needs +
388*6bdae907SThomas Monjalon                 two + lines + gets + indented + on + the +
389*6bdae907SThomas Monjalon                 second + and + subsequent + lines;
390*6bdae907SThomas Monjalon }
391*6bdae907SThomas Monjalon for (;;) {
392*6bdae907SThomas Monjalon         if (cond)
393*6bdae907SThomas Monjalon                 stmt;
394*6bdae907SThomas Monjalon }
395*6bdae907SThomas Monjalon if (val != NULL)
396*6bdae907SThomas Monjalon         val = realloc(val, newsize);
397*6bdae907SThomas Monjalon
398*6bdae907SThomas Monjalon
399*6bdae907SThomas Monjalon* Parts of a for loop may be left empty.
400*6bdae907SThomas Monjalon
401*6bdae907SThomas Monjalon.. code-block:: c
402*6bdae907SThomas Monjalon
403*6bdae907SThomas Monjalon for (; cnt < 15; cnt++) {
404*6bdae907SThomas Monjalon         stmt1;
405*6bdae907SThomas Monjalon         stmt2;
406*6bdae907SThomas Monjalon }
407*6bdae907SThomas Monjalon
408*6bdae907SThomas Monjalon* Closing and opening braces go on the same line as the else keyword.
409*6bdae907SThomas Monjalon* Braces that are not necessary should be left out.
410*6bdae907SThomas Monjalon
411*6bdae907SThomas Monjalon.. code-block:: c
412*6bdae907SThomas Monjalon
413*6bdae907SThomas Monjalon if (test)
414*6bdae907SThomas Monjalon         stmt;
415*6bdae907SThomas Monjalon else if (bar) {
416*6bdae907SThomas Monjalon         stmt;
417*6bdae907SThomas Monjalon         stmt;
418*6bdae907SThomas Monjalon } else
419*6bdae907SThomas Monjalon         stmt;
420*6bdae907SThomas Monjalon
421*6bdae907SThomas Monjalon
422*6bdae907SThomas MonjalonFunction Calls
423*6bdae907SThomas Monjalon~~~~~~~~~~~~~~
424*6bdae907SThomas Monjalon
425*6bdae907SThomas Monjalon* Do not use spaces after function names.
426*6bdae907SThomas Monjalon* Commas should have a space after them.
427*6bdae907SThomas Monjalon* No spaces after ``(`` or ``[`` or preceding the ``]`` or ``)`` characters.
428*6bdae907SThomas Monjalon
429*6bdae907SThomas Monjalon.. code-block:: c
430*6bdae907SThomas Monjalon
431*6bdae907SThomas Monjalon	error = function(a1, a2);
432*6bdae907SThomas Monjalon	if (error != 0)
433*6bdae907SThomas Monjalon		exit(error);
434*6bdae907SThomas Monjalon
435*6bdae907SThomas Monjalon
436*6bdae907SThomas MonjalonOperators
437*6bdae907SThomas Monjalon~~~~~~~~~
438*6bdae907SThomas Monjalon
439*6bdae907SThomas Monjalon* Unary operators do not require spaces, binary operators do.
440*6bdae907SThomas Monjalon* Do not use parentheses unless they are required for precedence or unless the statement is confusing without them.
441*6bdae907SThomas Monjalon  However, remember that other people may be more easily confused than you.
442*6bdae907SThomas Monjalon
443*6bdae907SThomas MonjalonExit
444*6bdae907SThomas Monjalon~~~~
445*6bdae907SThomas Monjalon
446*6bdae907SThomas MonjalonExits should be 0 on success, or 1 on failure.
447*6bdae907SThomas Monjalon
448*6bdae907SThomas Monjalon.. code-block:: c
449*6bdae907SThomas Monjalon
450*6bdae907SThomas Monjalon         exit(0);        /*
451*6bdae907SThomas Monjalon                          * Avoid obvious comments such as
452*6bdae907SThomas Monjalon                          * "Exit 0 on success."
453*6bdae907SThomas Monjalon                          */
454*6bdae907SThomas Monjalon }
455*6bdae907SThomas Monjalon
456*6bdae907SThomas MonjalonLocal Variables
457*6bdae907SThomas Monjalon~~~~~~~~~~~~~~~
458*6bdae907SThomas Monjalon
459*6bdae907SThomas Monjalon* Variables should be declared at the start of a block of code rather than in the middle.
460*6bdae907SThomas Monjalon  The exception to this is when the variable is ``const`` in which case the declaration must be at the point of first use/assignment.
461*6bdae907SThomas Monjalon* When declaring variables in functions, multiple variables per line are OK.
462*6bdae907SThomas Monjalon  However, if multiple declarations would cause the line to exceed a reasonable line length, begin a new set of declarations on the next line rather than using a line continuation.
463*6bdae907SThomas Monjalon* Be careful to not obfuscate the code by initializing variables in the declarations, only the last variable on a line should be initialized.
464*6bdae907SThomas Monjalon  If multiple variables are to be initialised when defined, put one per line.
465*6bdae907SThomas Monjalon* Do not use function calls in initializers, except for ``const`` variables.
466*6bdae907SThomas Monjalon
467*6bdae907SThomas Monjalon.. code-block:: c
468*6bdae907SThomas Monjalon
469*6bdae907SThomas Monjalon int i = 0, j = 0, k = 0;  /* bad, too many initializer */
470*6bdae907SThomas Monjalon
471*6bdae907SThomas Monjalon char a = 0;        /* OK, one variable per line with initializer */
472*6bdae907SThomas Monjalon char b = 0;
473*6bdae907SThomas Monjalon
474*6bdae907SThomas Monjalon float x, y = 0.0;  /* OK, only last variable has initializer */
475*6bdae907SThomas Monjalon
476*6bdae907SThomas Monjalon
477*6bdae907SThomas MonjalonCasts and sizeof
478*6bdae907SThomas Monjalon~~~~~~~~~~~~~~~~
479*6bdae907SThomas Monjalon
480*6bdae907SThomas Monjalon* Casts and sizeof statements are not followed by a space.
481*6bdae907SThomas Monjalon* Always write sizeof statements with parenthesis.
482*6bdae907SThomas Monjalon  The redundant parenthesis rules do not apply to sizeof(var) instances.
483*6bdae907SThomas Monjalon
484*6bdae907SThomas MonjalonC Function Definition, Declaration and Use
485*6bdae907SThomas Monjalon-------------------------------------------
486*6bdae907SThomas Monjalon
487*6bdae907SThomas MonjalonPrototypes
488*6bdae907SThomas Monjalon~~~~~~~~~~
489*6bdae907SThomas Monjalon
490*6bdae907SThomas Monjalon* It is recommended (and generally required by the compiler) that all non-static functions are prototyped somewhere.
491*6bdae907SThomas Monjalon* Functions local to one source module should be declared static, and should not be prototyped unless absolutely necessary.
492*6bdae907SThomas Monjalon* Functions used from other parts of code (external API) must be prototyped in the relevant include file.
493*6bdae907SThomas Monjalon* Function prototypes should be listed in a logical order, preferably alphabetical unless there is a compelling reason to use a different ordering.
494*6bdae907SThomas Monjalon* Functions that are used locally in more than one module go into a separate header file, for example, "extern.h".
495*6bdae907SThomas Monjalon* Do not use the ``__P`` macro.
496*6bdae907SThomas Monjalon* Functions that are part of an external API should be documented using Doxygen-like comments above declarations. See :ref:`doxygen_guidelines` for details.
497*6bdae907SThomas Monjalon* Functions that are part of the external API must have an ``rte_`` prefix on the function name.
498*6bdae907SThomas Monjalon* Do not use uppercase letters - either in the form of ALL_UPPERCASE, or CamelCase - in function names. Lower-case letters and underscores only.
499*6bdae907SThomas Monjalon* When prototyping functions, associate names with parameter types, for example:
500*6bdae907SThomas Monjalon
501*6bdae907SThomas Monjalon.. code-block:: c
502*6bdae907SThomas Monjalon
503*6bdae907SThomas Monjalon void function1(int fd); /* good */
504*6bdae907SThomas Monjalon void function2(int);    /* bad */
505*6bdae907SThomas Monjalon
506*6bdae907SThomas Monjalon* Short function prototypes should be contained on a single line.
507*6bdae907SThomas Monjalon  Longer prototypes, e.g. those with many parameters, can be split across multiple lines.
508*6bdae907SThomas Monjalon  The second and subsequent lines should be further indented as for line statement continuations as described in the previous section.
509*6bdae907SThomas Monjalon
510*6bdae907SThomas Monjalon.. code-block:: c
511*6bdae907SThomas Monjalon
512*6bdae907SThomas Monjalon static char *function1(int _arg, const char *_arg2,
513*6bdae907SThomas Monjalon        struct foo *_arg3,
514*6bdae907SThomas Monjalon        struct bar *_arg4,
515*6bdae907SThomas Monjalon        struct baz *_arg5);
516*6bdae907SThomas Monjalon static void usage(void);
517*6bdae907SThomas Monjalon
518*6bdae907SThomas Monjalon.. note::
519*6bdae907SThomas Monjalon
520*6bdae907SThomas Monjalon	Unlike function definitions, the function prototypes do not need to place the function return type on a separate line.
521*6bdae907SThomas Monjalon
522*6bdae907SThomas MonjalonDefinitions
523*6bdae907SThomas Monjalon~~~~~~~~~~~
524*6bdae907SThomas Monjalon
525*6bdae907SThomas Monjalon* The function type should be on a line by itself preceding the function.
526*6bdae907SThomas Monjalon* The opening brace of the function body should be on a line by itself.
527*6bdae907SThomas Monjalon
528*6bdae907SThomas Monjalon.. code-block:: c
529*6bdae907SThomas Monjalon
530*6bdae907SThomas Monjalon static char *
531*6bdae907SThomas Monjalon function(int a1, int a2, float fl, int a4)
532*6bdae907SThomas Monjalon {
533*6bdae907SThomas Monjalon
534*6bdae907SThomas Monjalon
535*6bdae907SThomas Monjalon* Do not declare functions inside other functions.
536*6bdae907SThomas Monjalon  ANSI C states that such declarations have file scope regardless of the nesting of the declaration.
537*6bdae907SThomas Monjalon  Hiding file declarations in what appears to be a local scope is undesirable and will elicit complaints from a good compiler.
538*6bdae907SThomas Monjalon* Old-style (K&R) function declaration should not be used, use ANSI function declarations instead as shown below.
539*6bdae907SThomas Monjalon* Long argument lists should be wrapped as described above in the function prototypes section.
540*6bdae907SThomas Monjalon
541*6bdae907SThomas Monjalon.. code-block:: c
542*6bdae907SThomas Monjalon
543*6bdae907SThomas Monjalon /*
544*6bdae907SThomas Monjalon  * All major routines should have a comment briefly describing what
545*6bdae907SThomas Monjalon  * they do. The comment before the "main" routine should describe
546*6bdae907SThomas Monjalon  * what the program does.
547*6bdae907SThomas Monjalon  */
548*6bdae907SThomas Monjalon int
549*6bdae907SThomas Monjalon main(int argc, char *argv[])
550*6bdae907SThomas Monjalon {
551*6bdae907SThomas Monjalon         char *ep;
552*6bdae907SThomas Monjalon         long num;
553*6bdae907SThomas Monjalon         int ch;
554*6bdae907SThomas Monjalon
555*6bdae907SThomas MonjalonC Statement Style and Conventions
556*6bdae907SThomas Monjalon---------------------------------
557*6bdae907SThomas Monjalon
558*6bdae907SThomas MonjalonNULL Pointers
559*6bdae907SThomas Monjalon~~~~~~~~~~~~~
560*6bdae907SThomas Monjalon
561*6bdae907SThomas Monjalon* NULL is the preferred null pointer constant.
562*6bdae907SThomas Monjalon  Use NULL instead of ``(type *)0`` or ``(type *)NULL``, except where the compiler does not know the destination type e.g. for variadic args to a function.
563*6bdae907SThomas Monjalon* Test pointers against NULL, for example, use:
564*6bdae907SThomas Monjalon
565*6bdae907SThomas Monjalon.. code-block:: c
566*6bdae907SThomas Monjalon
567*6bdae907SThomas Monjalon if (p == NULL) /* Good, compare pointer to NULL */
568*6bdae907SThomas Monjalon
569*6bdae907SThomas Monjalon if (!p) /* Bad, using ! on pointer */
570*6bdae907SThomas Monjalon
571*6bdae907SThomas Monjalon
572*6bdae907SThomas Monjalon* Do not use ! for tests unless it is a boolean, for example, use:
573*6bdae907SThomas Monjalon
574*6bdae907SThomas Monjalon.. code-block:: c
575*6bdae907SThomas Monjalon
576*6bdae907SThomas Monjalon	if (*p == '\0') /* check character against (char)0 */
577*6bdae907SThomas Monjalon
578*6bdae907SThomas MonjalonReturn Value
579*6bdae907SThomas Monjalon~~~~~~~~~~~~
580*6bdae907SThomas Monjalon
581*6bdae907SThomas Monjalon* Functions which create objects, or allocate memory, should return pointer types, and NULL on error.
582*6bdae907SThomas Monjalon  The error type should be indicated may setting the variable ``rte_errno`` appropriately.
583*6bdae907SThomas Monjalon* Functions which work on bursts of packets, such as RX-like or TX-like functions, should return the number of packets handled.
584*6bdae907SThomas Monjalon* Other functions returning int should generally behave like system calls:
585*6bdae907SThomas Monjalon  returning 0 on success and -1 on error, setting ``rte_errno`` to indicate the specific type of error.
586*6bdae907SThomas Monjalon* Where already standard in a given library, the alternative error approach may be used where the negative value is not -1 but is instead ``-errno`` if relevant, for example, ``-EINVAL``.
587*6bdae907SThomas Monjalon  Note, however, to allow consistency across functions returning integer or pointer types, the previous approach is preferred for any new libraries.
588*6bdae907SThomas Monjalon* For functions where no error is possible, the function type should be ``void`` not ``int``.
589*6bdae907SThomas Monjalon* Routines returning ``void *`` should not have their return values cast to any pointer type.
590*6bdae907SThomas Monjalon  (Typecasting can prevent the compiler from warning about missing prototypes as any implicit definition of a function returns int,
591*6bdae907SThomas Monjalon  which, unlike ``void *``, needs a typecast to assign to a pointer variable.)
592*6bdae907SThomas Monjalon
593*6bdae907SThomas Monjalon.. note::
594*6bdae907SThomas Monjalon
595*6bdae907SThomas Monjalon	The above rule about not typecasting ``void *`` applies to malloc, as well as to DPDK functions.
596*6bdae907SThomas Monjalon
597*6bdae907SThomas Monjalon* Values in return statements should not be enclosed in parentheses.
598*6bdae907SThomas Monjalon
599*6bdae907SThomas MonjalonLogging and Errors
600*6bdae907SThomas Monjalon~~~~~~~~~~~~~~~~~~
601*6bdae907SThomas Monjalon
602*6bdae907SThomas MonjalonIn the DPDK environment, use the logging interface provided:
603*6bdae907SThomas Monjalon
604*6bdae907SThomas Monjalon.. code-block:: c
605*6bdae907SThomas Monjalon
606*6bdae907SThomas Monjalon #define RTE_LOGTYPE_TESTAPP1 RTE_LOGTYPE_USER1
607*6bdae907SThomas Monjalon #define RTE_LOGTYPE_TESTAPP2 RTE_LOGTYPE_USER2
608*6bdae907SThomas Monjalon
609*6bdae907SThomas Monjalon /* enable these logs type */
610*6bdae907SThomas Monjalon rte_set_log_type(RTE_LOGTYPE_TESTAPP1, 1);
611*6bdae907SThomas Monjalon rte_set_log_type(RTE_LOGTYPE_TESTAPP2, 1);
612*6bdae907SThomas Monjalon
613*6bdae907SThomas Monjalon /* log in debug level */
614*6bdae907SThomas Monjalon rte_set_log_level(RTE_LOG_DEBUG);
615*6bdae907SThomas Monjalon RTE_LOG(DEBUG, TESTAPP1, "this is is a debug level message\n");
616*6bdae907SThomas Monjalon RTE_LOG(INFO, TESTAPP1, "this is is a info level message\n");
617*6bdae907SThomas Monjalon RTE_LOG(WARNING, TESTAPP1, "this is is a warning level message\n");
618*6bdae907SThomas Monjalon
619*6bdae907SThomas Monjalon /* log in info level */
620*6bdae907SThomas Monjalon rte_set_log_level(RTE_LOG_INFO);
621*6bdae907SThomas Monjalon RTE_LOG(DEBUG, TESTAPP2, "debug level message (not displayed)\n");
622*6bdae907SThomas Monjalon
623*6bdae907SThomas MonjalonBranch Prediction
624*6bdae907SThomas Monjalon~~~~~~~~~~~~~~~~~
625*6bdae907SThomas Monjalon
626*6bdae907SThomas Monjalon* When a test is done in a critical zone (called often or in a data path) the code can use the ``likely()`` and ``unlikely()`` macros to indicate the expected, or preferred fast path.
627*6bdae907SThomas Monjalon  They are expanded as a compiler builtin and allow the developer to indicate if the branch is likely to be taken or not. Example:
628*6bdae907SThomas Monjalon
629*6bdae907SThomas Monjalon.. code-block:: c
630*6bdae907SThomas Monjalon
631*6bdae907SThomas Monjalon #include <rte_branch_prediction.h>
632*6bdae907SThomas Monjalon if (likely(x > 1))
633*6bdae907SThomas Monjalon   do_stuff();
634*6bdae907SThomas Monjalon
635*6bdae907SThomas Monjalon.. note::
636*6bdae907SThomas Monjalon
637*6bdae907SThomas Monjalon	The use of ``likely()`` and ``unlikely()`` should only be done in performance critical paths,
638*6bdae907SThomas Monjalon	and only when there is a clearly preferred path, or a measured performance increase gained from doing so.
639*6bdae907SThomas Monjalon	These macros should be avoided in non-performance-critical code.
640*6bdae907SThomas Monjalon
641*6bdae907SThomas MonjalonStatic Variables and Functions
642*6bdae907SThomas Monjalon~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
643*6bdae907SThomas Monjalon
644*6bdae907SThomas Monjalon* All functions and variables that are local to a file must be declared as ``static`` because it can often help the compiler to do some optimizations (such as, inlining the code).
645*6bdae907SThomas Monjalon* Functions that should be inlined should to be declared as ``static inline`` and can be defined in a .c or a .h file.
646*6bdae907SThomas Monjalon
647*6bdae907SThomas Monjalon.. note::
648*6bdae907SThomas Monjalon	Static functions defined in a header file must be declared as ``static inline`` in order to prevent compiler warnings about the function being unused.
649*6bdae907SThomas Monjalon
650*6bdae907SThomas MonjalonConst Attribute
651*6bdae907SThomas Monjalon~~~~~~~~~~~~~~~
652*6bdae907SThomas Monjalon
653*6bdae907SThomas MonjalonThe ``const`` attribute should be used as often as possible when a variable is read-only.
654*6bdae907SThomas Monjalon
655*6bdae907SThomas MonjalonInline ASM in C code
656*6bdae907SThomas Monjalon~~~~~~~~~~~~~~~~~~~~
657*6bdae907SThomas Monjalon
658*6bdae907SThomas MonjalonThe ``asm`` and ``volatile`` keywords do not have underscores. The AT&T syntax should be used.
659*6bdae907SThomas MonjalonInput and output operands should be named to avoid confusion, as shown in the following example:
660*6bdae907SThomas Monjalon
661*6bdae907SThomas Monjalon.. code-block:: c
662*6bdae907SThomas Monjalon
663*6bdae907SThomas Monjalon	asm volatile("outb %[val], %[port]"
664*6bdae907SThomas Monjalon		: :
665*6bdae907SThomas Monjalon		[port] "dN" (port),
666*6bdae907SThomas Monjalon		[val] "a" (val));
667*6bdae907SThomas Monjalon
668*6bdae907SThomas MonjalonControl Statements
669*6bdae907SThomas Monjalon~~~~~~~~~~~~~~~~~~
670*6bdae907SThomas Monjalon
671*6bdae907SThomas Monjalon* Forever loops are done with for statements, not while statements.
672*6bdae907SThomas Monjalon* Elements in a switch statement that cascade should have a FALLTHROUGH comment. For example:
673*6bdae907SThomas Monjalon
674*6bdae907SThomas Monjalon.. code-block:: c
675*6bdae907SThomas Monjalon
676*6bdae907SThomas Monjalon         switch (ch) {         /* Indent the switch. */
677*6bdae907SThomas Monjalon         case 'a':             /* Don't indent the case. */
678*6bdae907SThomas Monjalon                 aflag = 1;    /* Indent case body one tab. */
679*6bdae907SThomas Monjalon                 /* FALLTHROUGH */
680*6bdae907SThomas Monjalon         case 'b':
681*6bdae907SThomas Monjalon                 bflag = 1;
682*6bdae907SThomas Monjalon                 break;
683*6bdae907SThomas Monjalon         case '?':
684*6bdae907SThomas Monjalon         default:
685*6bdae907SThomas Monjalon                 usage();
686*6bdae907SThomas Monjalon                 /* NOTREACHED */
687*6bdae907SThomas Monjalon         }
688