Assembly Coding Style:
Use Intel or equivalent syntax, NOT AT&T syntax:
opcode dest, source
If using macros, use m4 macro processor.
Use ; for one-line comments as this is the most universal for most assemblers.
C Coding Style:
C Features:
- Use C89 except for the following caveats:
- Allocating variable length array size at runtime (C99).
- Need fixed integer sizes in stdint.h (C99).
- _Thread_local (C11)
- _Atomic (C11)
- Use /* ... */ for comments, not //.
Blocks:
- All variables declared at top of block except for the following caveats:
- Can't allocate variable length array until later.
- { on same line preceded by a single space (except functions).
- } on own line unless continuing statement.
- Do not use blocks unless necessary.
- Do not use blocks for single statements.
Functions:
- Return type and modifiers on one line.
- Function name and argument list on one line.
- Opening { on own line.
- Functions not used outside of translation unit should be declared and
defined static.
Variables:
- Global variables not used outside of translation unit should be declared
static.
- In declaration of pointers the * is adjacent to variable name, not type.
Arrays:
- Prefer arrays of structs rather than struct of arrays.
- Struct of arrays is better for alignment and wasted bytes but
worse for cache locality.
Branches:
- Prefer conditional increment rather than branch.
i += (val < test);
i += (val < test)*incr; /* Also acceptable. */
Switch:
- Do not indent cases on another level.
Data allocation:
- In general prefer stack allocation for better cache locality.
- Avoid functions that use malloc(), unless necessary. If can't avoid the
use of malloc() it must be used for all program allocation.
- If program heavily uses the stack (recursion, etc) opt to allocate
globally or dynamically if possible.
- Buffers:
- Prefer to allocate small buffers on stack.
- If program has one single large buffer it should be allocated on
the stack.
- If program needs lots of the virtual address space, prefer
to dynamically allocate buffers when needed.
- If program uses one data structure prefer _brk() and mmap() anonymous
page mappings. Otherwise use malloc().
- NOTE: Using _brk() can be dangerous depending on the C library
implementation of malloc().
Files:
- Use file descriptors always! Unless there is some specific reason that
file streams need to be used (example: using threads).
- For sequential file access use read() family of system calls otherwise
prefer mmap().