1.4 Linux下编码风格
Linux 高级程序设计(第三版)
下面为读者列出GNU编码规范和Linux内核编码规范示例。
1.4.1 GNU编码规范
下面是GNU emacs中的一段代码。
/* Interface from Emacs to terminfo.
Copyright (C) 1985, 1986 Free Software Foundation, Inc.
This file is part of GNU Emacs.
GNU Emacs is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version.
GNU Emacs is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with GNU Emacs; see the file COPYING. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#include <config.h>
#include "lisp.h"
/ Define these variables that serve as global parameters to termcap, so that we do not need to conditionalize the places in Emacs that set them. /
char UP, BC, PC;
/ Interface to curses/terminfo library. Turns out that all of the terminfo-level routines look like their termcap counterparts except for tparm, which replaces tgoto. Not only is the calling sequence different, but the string format is different too./
char *tparam (string, outstring, len, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)
char *string;
char *outstring;
int arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9;
{
char *temp;
extern char *tparm();
temp = tparm (string, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
if (outstring == 0)
outstring = ((char *) (xmalloc ((strlen (temp)) + 1)));
strcpy (outstring, temp);
return outstring;
}
分析上面的代码可以看出GNU具有如下编码风格。
函数开头的左花括号放到最左边,避免把任何其他的左花括号、左括号或者左方括号放到最左边。
尽力避免让两个不同优先级的操作符出现在相同的对齐方式中。
每个程序开头都应该有一段简短的说明其功能的注释。例如GNU emacs上面的代码中的注释。
每个函数都加上注释,以说明函数做了些什么,需要哪些种类的参数,参数可能值的含义以及用途。
不要跨行声明多个变量。在每一行中都以一个新的声明开头。
当在一个if语句中嵌套了另一个if-else语句时,应用花括号把if-else括起来。
要在同一个声明中同时说明结构标识和变量,或者结构标识和类型定义(typedef)。
尽量避免在if的条件中进行赋值。
在名字中使用下划线以分隔单词,尽量使用小写;在宏或者枚举中通常使用大写常量。
使用一个命令行选项时,给出的变量应该在选项含义的说明之后,而不是选项字符之后。
另外,Linux有很多工具来帮助程序员养成良好编码规范。除了vim和emacs以外,还有indent工具可以帮程序员美化C/C++源代码。下面用这条命令可将Linux内核编程风格的程序quan.c转变为GNU编程风格。
[root@localhost ~]#$ indent -gnu quan.c
1.4.2 Linux内核编码规范
下面是Linux内核2.6.13目录\arch\i386\kernel中的numaq.c的节选代码,现在并不要求读懂此代码,而是看Linux的内核编程风格。
/*
* Written by: Patricia Gaughen, IBM Corporation
*
* Copyright (C) 2002, IBM Corp.
*
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
* NON INFRINGEMENT. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Send feedback to <gone@us.ibm.com>
*/
#include <linux/config.h>
#include <linux/mm.h>
#include <linux/bootmem.h>
#include <linux/mmzone.h>
#include <linux/module.h>
#include <linux/nodemask.h>
#include <asm/numaq.h>
#include <asm/topology.h>
#include <asm/processor.h>
#define MB_TO_PAGES(addr) ((addr) << (20 - PAGE_SHIFT))
/*
* Function: smp_dump_qct()
*
* Description: gets memory layout from the quad config table. This
* function also updates node_online_map with the nodes (quads) present.
*/
static void __init smp_dump_qct(void)
{
int node;
struct eachquadmem *eq;
struct sys_cfg_data scd = (struct sys_cfg_data )__va(SYS_CFG_DATA_PRIV_ADDR);
nodes_clear(node_online_map);
for_each_node(node) {
if (scd->quads_present31_0 & (1 << node)) {
node_set_online(node);
eq = &scd->eq[node];
/ Convert to pages /
node_start_pfn[node] = MB_TO_PAGES(
eq->hi_shrd_mem_start - eq->priv_mem_size);
node_end_pfn[node] = MB_TO_PAGES(
eq->hi_shrd_mem_start + eq->hi_shrd_mem_size);
memory_present(node,
node_start_pfn[node], node_end_pfn[node]);
node_remap_size[node] = node_memmap_size_bytes(node,
node_start_pfn[node],
node_end_pfn[node]);
}
}
}
分析以上代码可以看出Linux内核代码具有如下风格。
缩进采用tab制表符。
在if或者for循环中,将开始的大括号放在一行的最后,而将结束大括号放在本段语句结束行的第一位,函数中的大括号除外。
变量命名尽量使用简短的名字,简写或者单词间采用了_隔开,比如代码中的sys_cfg_data。
函数最好短小精悍,一个函数最好只做一件事情,而且函数中的变量一般不超过10个,大小一般都小于80行。
一个模块的注释一般注明了作者、版权、注释说明代码的功能,而不是说明其实现原理,这也和Linux的文化有关。
以上是GNU编码风格和Linux内核编码风格,两种风格体现着两种不同的文化精神,有些地方相似,有些地方迥然不同,但是目的只有一个:形成一个清晰、美观、可维护、方便扩展的代码。读者可以根据个人情况选择其中一种编码规范来编写自己的程序。