-
Notifications
You must be signed in to change notification settings - Fork 3
Code Style
Since the kernel has switched to C++, coding style has changed and has been updated here also.
Transparent types must be named in the following fashion - struct KernelThread; struct Runnable; class RBTree; class BuddyAllocator;
But opaque types should be aliased like this - struct Handle; typedef struct HANDLE;
Although the kernel is built using C++, the C coding style is allowed (for non-core code) and is supported using function-attribute macros -
decl_c void Main(args);// C-style function
import_asm unsigned long ReadMSR(args);// Function implemented in assembly
export_asm unsigned long DbgInt(args);// Function exported to another .asm file
Here,
- decl_c means the function is to declared in "C" style and to be used in *.c files.
- import_asm means the function is implemented in assembly files.
- export_asm means the function will be used by assembly files.
A structure is always typedef-ed, if it is abstract, and is declared as -
/**
* Struct: DataTypeName / Type: ABSTRACT_TYPE
* Attributes: file-only (iff not in header)
*
* Summary:
* blah-blah-blah
*
* Version: x.y.z
* Since: Circuit X.xy,++
* Author: Shukant Pal
*/
struct DataTypeName {
VAR_TYPE variableName;
struct ChildStruct otherStruct;
} ABSTRACT_TYPE;/* Used only if the type is opaque! */
Structures are a fundamental part of the coding style, as only they are supported in data structures. The kernel does not (like) use of templates for widely-used classes, thus, the Void *pointer (instead of template-types). All stuff that needs to be stored in a data structure should be organized into a struct declaration.
A global function is defined as
function_attribute retType PlainCFunctionName(args);
All function names are written in PascalCase for differentiating with C++ class functions which are in camelCase.
A class is defined as (source or header) -
/**
* Class: ClassName
*
* Summary:
* This class implements the management of ....
*
* Important Functions:
* functionName -
*
* Version: Version of class
* Since: Circuit x.yz,++
* Author: Implementor's Name
*/
class ClassName {
public:
varType variable;
retType functionName(args);
private:
};
Documentation should be provided above each declaration of a method. This allows the readers to determine what the code does and its proper purpose. It helps in maintaining code and removing legacy code that is not used anywhere else in the kernel.
Documentation provides a lot of detail about how the method is structured and what algorithm it uses. Newbies will also easily understand how the method will use the arguments and what effects will shadow the arguments after calling any method.
/**
* Method: Class::SubClass::MethodName
* Attributes: (optional, possible values are) virtual, interface, huge, deprecated
*
* Summary:
* (Give a brief description on what the method does and
* where is it to be used. Limit the amount of columns in the
* summary to 80 characters.)
*
* Args:
* argType0 arg0 - describe the role of the argument
* argType1 arg1 - ...
* argType2 arg2 - ...
* ...
*
* Returns:
* tell what the function returns, if non-void;
*
* Version: No. of major changes done to this method
* Since: First version of kernel on which this was implemented
* Author: <Documentation Writer>
*/
retType functionName(argType argName)
{
}