wiki:Developer/Coding/Conventions

Version 5 (modified by Gedare, on Dec 13, 2012 at 5:49:13 PM) (diff)

/* New section */ Source Documentation section added.

Coding Conventions

Unfortunately, RTEMS does not have a formal written set of coding conventions which have been reviewed and blessed by an ISO9000 committee of non-coders. However, the style of the code in RTEMS is generally very consistent in the core areas of RTEMS. This page attempts to capture some of the generally accepted practices.

Historically, the RTEMS project has not outright rejected any submission of general utility. We have worked with the authors to ensure that the code conforms to the standards. In many cases, the source code has been merged into the source base under the good faith assumption that concerns will be addressed.

As an exception, code the is merged from another source such as FreeBSD or another open source project into the RTEMS source tree will NOT be reformatted!!! This is critical as it makes it difficult to merge future updates and bug fixes.

Source Documentation

RTEMS uses Doxygen to generate documentation from source code. See Doxygen Recommendations for how to use Doxygen with RTEMS sources.

File Issues

  • Every file should include a header with the copyright notice. This is a typical example:
    /**
     * @file
     * @brief short "Table of Contents" description of code
     *
     * A short description of the purpose of this file.
     */
    
    /*
     *  COPYRIGHT (c) 1989-2012.
     *  On-Line Applications Research Corporation (OAR).
     *
     *  The license and distribution terms for this file may be
     *  found in the file LICENSE in this distribution or at
     *  http://www.rtems.com/license/LICENSE.
     */
    
  • Do not put unrelated functions or data in a single file. This results in object code bloat.
  • If adding code to cpukit be sure the filename is unique since it will be merged all code under that directory gets merged into a single library.
  • The Doxygen header block is separate from the copyright notice intentionally so the copyright notice is not included in the Doxygen output.
  • The copyright owner and specific license terms may vary. The above is typical and the preferred license but BSD, "use as is" and others are acceptable. Pure GPL licensed code is NOT acceptable.

Source Code Style

  • Use ANSI C not K&R.
  • Do not use compiler extensions.
  • Use spaces instead of tabs.
  • Use two spaces for indentation.
  • Adhere to a limit of 80 characters per line.
  • Pay attention to warnings.
  • Check all return statuses.
  • Favor C over assembly language and use inline assembly.
  • Share code with other targets where possible.
  • Think portable -- RTEMS supports a lot of target hardware.
  • Simple code is easier to debug and easier to read than clever code.

When in doubt, use the source code in cpukit/rtems as a style reference.

If you are a vim user then the following additions to your .vimrc will assist in the indentation:

 set tabstop=2
 set shiftwidth=2
 set expandtab

Eric Valette did some research and came up with a set of arguments for version 2.2.7 of the indent program which seem to match the RTEMS style. So if you need help, try this and see how it does:

indent -br -ce -i2 -nut FILE.c

But remember, it is better to write code in the accepted style than to use a program to get it there.

Performance Issues

Although style is important, RTEMS is a real-time operating system and, consequently, any enhancements or modifications should also have certain execution characteristics. The following is a list of guidelines which RTEMS adheres to in order to improve the performance and stability of the services it provides.

  • Favor C automatic variables over global or static variables.
  • Use global variables only when necessary and be conscious that RTEMS is a multitasking system. This means that you often have to ensure atomicity of operations.
  • Avoid declaring large buffers or structures on the stack.
  • If you need to temporarily change the execution mode of a task/thread, restore it.
  • Favor mutual exclusion primitives over disabling preemption.
  • Do not duplicate standard OS or C Library routines.
  • Avoid unnecessary dependencies. This can mean splitting your code into multiple file to limit the minimum footprint or not calling printf() on error paths.
  • Use algorithms with the lowest order of execution. By favoring O(constant) over O(n) algorithms, RTEMS works hard to ensure deterministic execution times as much as possible.
  • Limit execution times in Interrupt and Timer Service Routines (TSRs). TSRs scheduled by rtems_timer_fire_XXX run in the context of the clock tick ISR while TSRs scheduled by rtems_timer_server_fire_XXX run in the context of a dedicated Timer Server task. Either way, excessive execution times could have a negative impact on the system performance.
  • Do not hard code limits such as maximum instances into your code.

Makefile.am Issues

TBD

=configure.ac Issues=

TBD

Attachments (1)

Download all attachments as: .zip