From: Zhang Le <[EMAIL PROTECTED]> Signed-off-by: Zhang Le <[EMAIL PROTECTED]> Signed-off-by: Li Yang <[EMAIL PROTECTED]> Signed-off-by: Greg Kroah-Hartman <[EMAIL PROTECTED]> --- Documentation/zh_CN/CodingStyle | 701 +++++++++++++++++++++++++++++++++++++++ 1 files changed, 701 insertions(+), 0 deletions(-) create mode 100644 Documentation/zh_CN/CodingStyle
diff --git a/Documentation/zh_CN/CodingStyle b/Documentation/zh_CN/CodingStyle new file mode 100644 index 0000000..ecd9307 --- /dev/null +++ b/Documentation/zh_CN/CodingStyle @@ -0,0 +1,701 @@ +Chinese translated version of Documentation/CodingStyle + +If you have any comment or update to the content, please post to LKML directly. +However, if you have problem communicating in English you can also ask the +Chinese maintainer for help. Contact the Chinese maintainer, if this +translation is outdated or there is problem with translation. + +Chinese maintainer: Zhang Le <[EMAIL PROTECTED]> +--------------------------------------------------------------------- +Documentation/CodingStyleçä¸æç¿»è¯ + +å¦ææ³è¯è®ºææ´æ°æ¬æçå 容ï¼è¯·ç´æ¥åä¿¡å°LKMLãå¦æä½ ä½¿ç¨è±æ交æµæå°é¾çè¯ï¼ä¹å¯ +以åä¸æçç»´æ¤è æ±å©ãå¦ææ¬ç¿»è¯æ´æ°ä¸åæ¶æè ç¿»è¯åå¨é®é¢ï¼è¯·èç³»ä¸æçç»´æ¤è ã + +ä¸æçç»´æ¤è ï¼ å¼ ä¹ Zhang Le <[EMAIL PROTECTED]> +ä¸æçç¿»è¯è ï¼ å¼ ä¹ Zhang Le <[EMAIL PROTECTED]> +ä¸æçæ ¡è¯è ï¼ çèª Wang Cong <[EMAIL PROTECTED]> + wheelz <[EMAIL PROTECTED]> + 管æä¸ Xudong Guan <[EMAIL PROTECTED]> + Li Zefan <[EMAIL PROTECTED]> + Wang Chen <[EMAIL PROTECTED]> +以ä¸ä¸ºæ£æ +--------------------------------------------------------------------- + + Linuxå æ ¸ä»£ç é£æ ¼ + +è¿æ¯ä¸ä¸ªç®ççææ¡£ï¼æè¿°äºlinuxå æ ¸çé¦é代ç é£æ ¼ã代ç é£æ ¼æ¯å 人èå¼çï¼èä¸æ +ä¸æ¿æææçè§ç¹å¼ºå ç»ä»»ä½äººï¼ä¸è¿è¿éæ讲述çæ¯æå¿ é¡»è¦ç»´æ¤ç代ç æéµå®çé£æ ¼ï¼ +并ä¸æä¹å¸æç»å¤§å¤æ°å ¶ä»ä»£ç ä¹è½éµå®è¿ä¸ªé£æ ¼ã请å¨å代ç æ¶è³å°èèä¸ä¸æ¬ææè¿°ç +é£æ ¼ã + +é¦å ï¼æå»ºè®®ä½ æå°ä¸ä»½GNU代ç è§èï¼ç¶åä¸è¦è¯»å®ãç§äºå®ï¼è¿æ¯ä¸ä¸ªå ·æé大象å¾æ§ +æä¹çå¨ä½ã + +ä¸ç®¡ææ ·ï¼ç°å¨æ们å¼å§ï¼ + + + 第ä¸ç« ï¼ç¼©è¿ + +å¶è¡¨ç¬¦æ¯8个å符ï¼æ以缩è¿ä¹æ¯8个å符ãæäºå¼ç«¯è¿å¨è¯å¾å°ç¼©è¿å为4ï¼ä¹è³2ï¼ä¸ªå符 +æ·±ï¼è¿å ä¹ç¸å½äºå°è¯å°åå¨ççå¼å®ä¹ä¸º3ã + +çç±ï¼ç¼©è¿çå ¨é¨æä¹å°±å¨äºæ¸ æ¥çå®ä¹ä¸ä¸ªæ§å¶åèµ·æ¢äºä½å¤ãå°¤å ¶æ¯å½ä½ ç¯çä½ çå±å¹ +è¿ç»çäº20å°æ¶ä¹åï¼ä½ å°ä¼åç°å¤§ä¸ç¹ç缩è¿ä¼ä½¿ä½ æ´å®¹æå辨缩è¿ã + +ç°å¨ï¼æäºäººä¼æ±æ¨8个å符ç缩è¿ä¼ä½¿ä»£ç åå³è¾¹ç§»å¨ç太è¿ï¼å¨80个å符çç»ç«¯å±å¹ä¸ +å°±å¾é¾è¯»è¿æ ·ç代ç ãè¿ä¸ªé®é¢ççæ¡æ¯ï¼å¦æä½ éè¦3级以ä¸ç缩è¿ï¼ä¸ç®¡ç¨ä½ç§æ¹å¼ä½ +ç代ç å·²ç»æé®é¢äºï¼åºè¯¥ä¿®æ£ä½ çç¨åºã + +ç®èè¨ä¹ï¼8个å符ç缩è¿å¯ä»¥è®©ä»£ç æ´å®¹æé 读ï¼è¿æä¸ä¸ªå¥½å¤æ¯å½ä½ çå½æ°åµå¥å¤ªæ·±ç +æ¶åå¯ä»¥ç»ä½ è¦åãçå¿è¿ä¸ªè¦åã + +å¨switchè¯å¥ä¸æ¶é¤å¤çº§ç¼©è¿çé¦éçæ¹å¼æ¯è®©âswitchâåä»å±äºå®çâcaseâæ ç¾å¯¹é½äºå +ä¸åï¼èä¸è¦â两次缩è¿ââcaseâæ ç¾ãæ¯å¦ï¼ + + switch (suffix) { + case 'G': + case 'g': + mem <<= 30; + break; + case 'M': + case 'm': + mem <<= 20; + break; + case 'K': + case 'k': + mem <<= 10; + /* fall through */ + default: + break; + } + + +ä¸è¦æå¤ä¸ªè¯å¥æ¾å¨ä¸è¡éï¼é¤éä½ æä»ä¹ä¸è¥¿è¦éèï¼ + + if (condition) do_this; + do_something_everytime; + +ä¹ä¸è¦å¨ä¸è¡éæ¾å¤ä¸ªèµå¼è¯å¥ãå æ ¸ä»£ç é£æ ¼è¶ 级ç®åãå°±æ¯é¿å å¯è½å¯¼è´å«äººè¯¯è¯»ç表 +è¾¾å¼ã + +é¤äºæ³¨éãææ¡£åKconfigä¹å¤ï¼ä¸è¦ä½¿ç¨ç©ºæ ¼æ¥ç¼©è¿ï¼åé¢çä¾åæ¯ä¾å¤ï¼æ¯ææ为ä¹ã + +éç¨ä¸ä¸ªå¥½çç¼è¾å¨ï¼ä¸è¦å¨è¡å°¾çç©ºæ ¼ã + + + 第äºç« ï¼æé¿çè¡åå符串ææ£ + +代ç é£æ ¼çæä¹å°±å¨äºä½¿ç¨å¹³å¸¸ä½¿ç¨çå·¥å ·æ¥ç»´æ代ç çå¯è¯»æ§åå¯ç»´æ¤æ§ã + +æ¯ä¸è¡çé¿åº¦çéå¶æ¯80åï¼æ们强ç建议æ¨éµå®è¿ä¸ªæ¯ä¾ã + +é¿äº80åçè¯å¥è¦ææ£æææä¹çç段ãæ¯ä¸ªç段è¦ææ¾çäºåæ¥çè¯å¥ï¼èä¸æ¾ç½®çä½ç½® +ä¹ææ¾çé å³ãåæ ·çè§åä¹éç¨äºæå¾é¿åæ°å表çå½æ°å¤´ãé¿å符串ä¹è¦ææ£æè¾çç +å符串ãå¯ä¸çä¾å¤æ¯è¶ è¿80åå¯ä»¥å¤§å¹ 度æé«å¯è¯»æ§å¹¶ä¸ä¸ä¼éèä¿¡æ¯çæ åµã + +void fun(int a, int b, int c) +{ + if (condition) + printk(KERN_WARNING "Warning this is a long printk with " + "3 parameters a: %u b: %u " + "c: %u \n", a, b, c); + else + next_statement; +} + + 第ä¸ç« ï¼å¤§æ¬å·åç©ºæ ¼çæ¾ç½® + +Cè¯è¨é£æ ¼ä¸å¦å¤ä¸ä¸ªå¸¸è§é®é¢æ¯å¤§æ¬å·çæ¾ç½®ãå缩è¿å¤§å°ä¸åï¼éæ©æå¼ç¨æç§æ¾ç½®ç +ç¥å¹¶æ²¡æå¤å°ææ¯ä¸çåå ï¼ä¸è¿é¦éçæ¹å¼ï¼å°±åKernighanåRitchieå±ç¤ºç»æ们çï¼æ¯ +æèµ·å§å¤§æ¬å·æ¾å¨è¡å°¾ï¼èæç»æ大æ¬å·æ¾å¨è¡é¦ï¼æä»¥ï¼ + + if (x is true) { + we do y + } + +è¿éç¨äºææçéå½æ°è¯å¥åï¼ifãswitchãforãwhileãdoï¼ãæ¯å¦ï¼ + + switch (action) { + case KOBJ_ADD: + return "add"; + case KOBJ_REMOVE: + return "remove"; + case KOBJ_CHANGE: + return "change"; + default: + return NULL; + } + +ä¸è¿ï¼æä¸ä¸ªä¾å¤ï¼é£å°±æ¯å½æ°ï¼å½æ°çèµ·å§å¤§æ¬å·æ¾ç½®äºä¸ä¸è¡çå¼å¤´ï¼æä»¥ï¼ + + int function(int x) + { + body of function + } + +å ¨ä¸ççå¼ç«¯å¯è½ä¼æ±æ¨è¿ä¸ªä¸ä¸è´æ§æ¯â¦â¦åâ¦â¦ä¸ä¸è´çï¼ä¸è¿æææç»´å¥å ¨ç人é½ç¥éï¼ +aï¼K&Ræ¯_æ£ç¡®ç_ï¼å¹¶ä¸ï¼bï¼K&Ræ¯æ£ç¡®çãæ¤å¤ï¼ä¸ç®¡ææ ·å½æ°é½æ¯ç¹æ®çï¼å¨Cè¯è¨ä¸ +ï¼å½æ°æ¯ä¸è½åµå¥çï¼ã + +注æç»æ大æ¬å·ç¬èªå æ®ä¸è¡ï¼é¤éå®åé¢è·çåä¸ä¸ªè¯å¥çå©ä½é¨åï¼ä¹å°±æ¯doè¯å¥ä¸ç +âwhileâæè ifè¯å¥ä¸çâelseâï¼åè¿æ ·ï¼ + + do { + body of do-loop + } while (condition); + +å + + if (x == y) { + .. + } else if (x > y) { + ... + } else { + .... + } + +çç±ï¼K&Rã + +ä¹è¯·æ³¨æè¿ç§å¤§æ¬å·çæ¾ç½®æ¹å¼ä¹è½ä½¿ç©ºï¼æè å·®ä¸å¤ç©ºçï¼è¡çæ°éæå°åï¼åæ¶ä¸å¤±å¯ +读æ§ãå æ¤ï¼ç±äºä½ çå±å¹ä¸çæ°è¡æ¯ä¸å¯åçèµæºï¼æ³æ³25è¡çç»ç«¯å±å¹ï¼ï¼ä½ å°ä¼ææ´ +å¤ç空è¡æ¥æ¾ç½®æ³¨éã + +å½åªæä¸ä¸ªåç¬çè¯å¥çæ¶åï¼ä¸ç¨å ä¸å¿ è¦ç大æ¬å·ã + +if (condition) + action(); + +è¿ç¹ä¸éç¨äºæ¬èº«ä¸ºæ个æ¡ä»¶è¯å¥çä¸ä¸ªåæ¯çåç¬è¯å¥ãè¿æ¶éè¦å¨ä¸¤ä¸ªåæ¯éé½ä½¿ç¨å¤§ +æ¬å·ã + +if (condition) { + do_this(); + do_that(); +} else { + otherwise(); +} + + 3.1ï¼ç©ºæ ¼ + +Linuxå æ ¸çç©ºæ ¼ä½¿ç¨æ¹å¼ï¼ä¸»è¦ï¼åå³äºå®æ¯ç¨äºå½æ°è¿æ¯å ³é®åãï¼å¤§å¤æ°ï¼å ³é®åå +è¦å ä¸ä¸ªç©ºæ ¼ãå¼å¾æ³¨æçä¾å¤æ¯sizeofãtypeofãalignofå__attribute__ï¼è¿äºå ³é®å +æäºç¨åº¦ä¸çèµ·æ¥æ´åå½æ°ï¼å®ä»¬å¨Linuxéä¹å¸¸å¸¸ä¼´éå°æ¬å·è使ç¨ï¼å°½ç®¡å¨Cè¯è¨éè¿æ · +çå°æ¬å·ä¸æ¯å¿ éçï¼å°±åâstruct fileinfo infoâ声æè¿åçâsizeof infoâï¼ã + +æ以å¨è¿äºå ³é®åä¹åæ¾ä¸ä¸ªç©ºæ ¼ï¼ + if, switch, case, for, do, while +ä½æ¯ä¸è¦å¨sizeofãtypeofãalignofæè __attribute__è¿äºå ³é®åä¹åæ¾ç©ºæ ¼ãä¾å¦ï¼ + s = sizeof(struct file); + +ä¸è¦å¨å°æ¬å·éç表达å¼ä¸¤ä¾§å ç©ºæ ¼ãè¿æ¯ä¸ä¸ªåä¾ï¼ + + s = sizeof( struct file ); + +å½å£°ææéç±»åæè è¿åæéç±»åçå½æ°æ¶ï¼â*âçé¦é使ç¨æ¹å¼æ¯ä½¿ä¹é è¿åéåæè å½ +æ°åï¼èä¸æ¯é è¿ç±»ååãä¾åï¼ + + char *linux_banner; + unsigned long long memparse(char *ptr, char **retptr); + char *match_strdup(substring_t *s); + +å¨å¤§å¤æ°äºå åä¸å æä½ç¬¦ä¸¤ä¾§ä½¿ç¨ä¸ä¸ªç©ºæ ¼ï¼ä¾å¦ä¸é¢ææè¿äºæä½ç¬¦ï¼ + + = + - < > * / % | & ^ <= >= == != ? : + +ä½æ¯ä¸å æä½ç¬¦åä¸è¦å ç©ºæ ¼ï¼ + & * + - ~ ! sizeof typeof alignof __attribute__ defined + +åç¼èªå åèªåä¸å æä½ç¬¦åä¸å ç©ºæ ¼ï¼ + ++ -- + +åç¼èªå åèªåä¸å æä½ç¬¦åä¸å ç©ºæ ¼ï¼ + ++ -- + +â.âåâ->âç»æä½æåæä½ç¬¦ååä¸å ç©ºæ ¼ã + +ä¸è¦å¨è¡å°¾ç空ç½ãæäºå¯ä»¥èªå¨ç¼©è¿çç¼è¾å¨ä¼å¨æ°è¡çè¡é¦å å ¥ééç空ç½ï¼ç¶åä½ +å°±å¯ä»¥ç´æ¥å¨é£ä¸è¡è¾å ¥ä»£ç ãä¸è¿åå¦ä½ æå没æå¨é£ä¸è¡è¾å ¥ä»£ç ï¼æäºç¼è¾å¨å°±ä¸ +ä¼ç§»é¤å·²ç»å å ¥ç空ç½ï¼å°±åä½ æ æçä¸ä¸ä¸ªåªæ空ç½çè¡ãå å«è¡å°¾ç©ºç½çè¡å°±è¿æ ·äº§ +çäºã + +å½gitåç°è¡¥ä¸å å«äºè¡å°¾ç©ºç½çæ¶åä¼è¦åä½ ï¼å¹¶ä¸å¯ä»¥åºä½ çè¦æ±å»æè¡å°¾ç©ºç½ï¼ä¸è¿ +å¦æä½ æ¯æ£å¨æä¸ç³»åè¡¥ä¸ï¼è¿æ ·åä¼å¯¼è´åé¢çè¡¥ä¸å¤±è´¥ï¼å ä¸ºä½ æ¹åäºè¡¥ä¸çä¸ä¸æã + + + 第åç« ï¼å½å + +Cæ¯ä¸ä¸ªç®æ´çè¯è¨ï¼ä½ çå½åä¹åºè¯¥è¿æ ·ãåModula-2åPascalç¨åºåä¸åï¼Cç¨åºåä¸ä½¿ +ç¨ç±»ä¼¼ThisVariableIsATemporaryCounterè¿æ ·å丽çååãCç¨åºåä¼ç§°é£ä¸ªåé为âtmpâ +ï¼è¿æ ·åèµ·æ¥ä¼æ´å®¹æï¼èä¸è³å°ä¸ä¼ä»¤å ¶é¾äºç解ã + +ä¸è¿ï¼è½ç¶æ··ç¨å¤§å°åçååæ¯ä¸æå¡ä½¿ç¨çï¼ä½æ¯å ¨å±åéè¿æ¯éè¦ä¸ä¸ªå ·æè¿°æ§çåå +ã称ä¸ä¸ªå ¨å±å½æ°ä¸ºâfooâæ¯ä¸ä¸ªé¾ä»¥é¥¶æçé误ã + +å ¨å±åéï¼åªæå½ä½ çæ£éè¦å®ä»¬çæ¶ååç¨å®ï¼éè¦æä¸ä¸ªå ·æè¿°æ§çååï¼å°±åå ¨å±å½ +æ°ãå¦æä½ æä¸ä¸ªå¯ä»¥è®¡ç®æ´»å¨ç¨æ·æ°éçå½æ°ï¼ä½ åºè¯¥å«å®âcount_active_users()âæè +类似çååï¼ä½ ä¸åºè¯¥å«å®âcntuser()âã + +å¨å½æ°åä¸å å«å½æ°ç±»åï¼æè°çåçå©å½åæ³ï¼æ¯èååºäºé®é¢ââç¼è¯å¨ç¥éé£äºç±»åè +ä¸è½å¤æ£æ¥é£äºç±»åï¼è¿æ ·ååªè½æç¨åºåå¼ç³æ¶äºãé¾æªå¾®è½¯æ»æ¯å¶é åºæé®é¢çç¨åºã + +æ¬å°åéååºè¯¥ç®çï¼èä¸è½å¤è¡¨è¾¾ç¸å ³çå«ä¹ãå¦æä½ æä¸äºéæºçæ´æ°åç循ç¯è®¡æ°å¨ +ï¼å®åºè¯¥è¢«ç§°ä¸ºâiâãå«å®âloop_counterâ并æ çå¤ï¼å¦æå®æ²¡æ被误解çå¯è½çè¯ã类似 +çï¼âtmpâå¯ä»¥ç¨æ¥ç§°å¼ä»»æç±»åç临æ¶åéã + +å¦æä½ ææ··æ·äºä½ çæ¬å°åéåï¼ä½ å°±éå°å¦ä¸ä¸ªé®é¢äºï¼å«åå½æ°å¢é¿è·å°è失衡综åç +ã请ç第å ç« ï¼å½æ°ï¼ã + + + 第äºç« ï¼Typedef + +ä¸è¦ä½¿ç¨ç±»ä¼¼âvps_tâä¹ç±»çä¸è¥¿ã + +对ç»æä½åæé使ç¨typedefæ¯ä¸ä¸ªé误ãå½ä½ å¨ä»£ç éçå°ï¼ + + vps_t a; + +è¿ä»£è¡¨ä»ä¹ææå¢ï¼ + +ç¸åï¼å¦ææ¯è¿æ · + + struct virtual_container *a; + +ä½ å°±ç¥éâaâæ¯ä»ä¹äºã + +å¾å¤äººè®¤ä¸ºtypedefâè½æé«å¯è¯»æ§âãå®é ä¸æ¯è¿æ ·çãå®ä»¬åªå¨ä¸åæ åµä¸æç¨ï¼ + + (a) å®å ¨ä¸éæç对象ï¼è¿ç§æ åµä¸è¦ä¸»å¨ä½¿ç¨typedefæ¥éèè¿ä¸ªå¯¹è±¡å®é ä¸æ¯ä»ä¹ï¼ã + + ä¾å¦ï¼âpte_tâçä¸éæ对象ï¼ä½ åªè½ç¨åéç访é®å½æ°æ¥è®¿é®å®ä»¬ã + + 注æï¼ä¸éææ§åâ访é®å½æ°âæ¬èº«æ¯ä¸å¥½çãæ们使ç¨pte_tçç±»åçåå å¨äºççæ¯ + å®å ¨æ²¡æä»»ä½å ±ç¨çå¯è®¿é®ä¿¡æ¯ã + + (b) æ¸ æ¥çæ´æ°ç±»åï¼å¦æ¤ï¼è¿å±æ½è±¡å°±å¯ä»¥å¸®å©æ¶é¤å°åºæ¯âintâè¿æ¯âlongâçæ··æ·ã + + u8/u16/u32æ¯å®å ¨æ²¡æé®é¢çtypedefï¼ä¸è¿å®ä»¬æ´ç¬¦åç±»å«(d)èä¸æ¯è¿éã + + å次注æï¼è¦è¿æ ·åï¼å¿ é¡»äºåºæå ãå¦ææ个åéæ¯âunsigned longâï¼é£ä¹æ²¡æå¿ è¦ + + typedef unsigned long myflags_t; + + ä¸è¿å¦ææä¸ä¸ªæç¡®çåå ï¼æ¯å¦å®å¨æç§æ åµä¸å¯è½ä¼æ¯ä¸ä¸ªâunsigned intâèå¨ + å ¶ä»æ åµä¸å¯è½ä¸ºâunsigned longâï¼é£ä¹å°±ä¸è¦ç¹è±«ï¼è¯·å¡å¿ 使ç¨typedefã + + (c) å½ä½ 使ç¨sparseæåé¢çå建ä¸ä¸ªæ°ç±»åæ¥åç±»åæ£æ¥çæ¶åã + + (d) åæ åC99ç±»åç¸åçç±»åï¼å¨æäºä¾å¤çæ åµä¸ã + + è½ç¶è®©ç¼çåèçæ¥éåºæ°çæ åç±»åæ¯å¦âuint32_tâä¸éè¦è±å¾å¤æ¶é´ï¼å¯æ¯æäº + 人ä»ç¶æç»ä½¿ç¨å®ä»¬ã + + å æ¤ï¼Linuxç¹æççåäºæ åç±»åçâu8/u16/u32/u64âç±»ååå®ä»¬çæ符å·ç±»åæ¯è¢« + å 许çââ尽管å¨ä½ èªå·±çæ°ä»£ç ä¸ï¼å®ä»¬ä¸æ¯å¼ºå¶è¦æ±è¦ä½¿ç¨çã + + å½ç¼è¾å·²ç»ä½¿ç¨äºæ个类åéçå·²æ代ç æ¶ï¼ä½ åºè¯¥éµå¾ªé£äºä»£ç ä¸å·²ç»ååºçéæ©ã + + (e) å¯ä»¥å¨ç¨æ·ç©ºé´å®å ¨ä½¿ç¨çç±»åã + + å¨æäºç¨æ·ç©ºé´å¯è§çç»æä½éï¼æ们ä¸è½è¦æ±C99ç±»åèä¸ä¸è½ç¨ä¸é¢æå°çâu32â + ç±»åãå æ¤ï¼æ们å¨ä¸ç¨æ·ç©ºé´å ±äº«çææç»æä½ä¸ä½¿ç¨__u32å类似çç±»åã + +å¯è½è¿æå ¶ä»çæ åµï¼ä¸è¿åºæ¬çè§åæ¯æ°¸è¿ä¸è¦ä½¿ç¨typedefï¼é¤éä½ å¯ä»¥æç¡®çåºç¨ä¸ +è¿°æ个è§åä¸çä¸ä¸ªã + +æ»çæ¥è¯´ï¼å¦æä¸ä¸ªæéæè ä¸ä¸ªç»æä½éçå ç´ å¯ä»¥åçç被ç´æ¥è®¿é®å°ï¼é£ä¹å®ä»¬å°±ä¸ +åºè¯¥æ¯ä¸ä¸ªtypedefã + + + 第å ç« ï¼å½æ° + +å½æ°åºè¯¥ç®çèæ¼äº®ï¼å¹¶ä¸åªå®æä¸ä»¶äºæ ãå½æ°åºè¯¥å¯ä»¥ä¸å±æè 两å±æ¾ç¤ºå®ï¼æ们é½ç¥ +éISO/ANSIå±å¹å¤§å°æ¯80x24ï¼ï¼åªåä¸ä»¶äºæ ï¼èä¸æå®å好ã + +ä¸ä¸ªå½æ°çæ大é¿åº¦æ¯å该å½æ°çå¤æ度å缩è¿çº§æ°æåæ¯çãæ以ï¼å¦æä½ æä¸ä¸ªçè®ºä¸ +å¾ç®åçåªæä¸ä¸ªå¾é¿ï¼ä½æ¯ç®åï¼çcaseè¯å¥çå½æ°ï¼èä¸ä½ éè¦å¨æ¯ä¸ªcaseéåå¾å¤å¾ +å°çäºæ ï¼è¿æ ·çå½æ°å°½ç®¡å¾é¿ï¼ä½ä¹æ¯å¯ä»¥çã + +ä¸è¿ï¼å¦æä½ æä¸ä¸ªå¤æçå½æ°ï¼èä¸ä½ æçä¸ä¸ªå¤©åä¸æ¯å¾é«çé«ä¸ä¸å¹´çº§å¦çå¯è½çè³ +æä¸æ¸ æ¥è¿ä¸ªå½æ°çç®çï¼ä½ åºè¯¥ä¸¥æ ¼çéµå®åé¢æå°çé¿åº¦éå¶ã使ç¨è¾ å©å½æ°ï¼å¹¶ä¸ºä¹ +åä¸ªå ·æè¿°æ§çååï¼å¦æä½ è§å¾å®ä»¬çæ§è½å¾éè¦çè¯ï¼å¯ä»¥è®©ç¼è¯å¨å èå®ä»¬ï¼è¿æ ·ç +ææå¾å¾ä¼æ¯ä½ åä¸ä¸ªå¤æå½æ°çææè¦å¥½ãï¼ + +å½æ°çå¦å¤ä¸ä¸ªè¡¡éæ åæ¯æ¬å°åéçæ°éãæ¤æ°éä¸åºè¶ è¿5ï¼10个ï¼å¦åä½ çå½æ°å°±æ +é®é¢äºãéæ°èèä¸ä¸ä½ çå½æ°ï¼æå®åæææ´å°çå½æ°ã人ç大èä¸è¬å¯ä»¥è½»æ¾çåæ¶è· +踪7个ä¸åçäºç©ï¼å¦æåå¢å¤çè¯ï¼å°±ä¼ç³æ¶äºãå³ä¾¿ä½ èªé¢è¿äººï¼ä½ ä¹å¯è½ä¼è®°ä¸æ¸ ä½ 2 +个ææååè¿çäºæ ã + +å¨æºæ件éï¼ä½¿ç¨ç©ºè¡éå¼ä¸åçå½æ°ãå¦æ该å½æ°éè¦è¢«å¯¼åºï¼å®çEXPORT*å®åºè¯¥ç´§è´´ +å¨å®çç»æ大æ¬å·ä¹ä¸ãæ¯å¦ï¼ + +int system_is_up(void) +{ + return system_state == SYSTEM_RUNNING; +} +EXPORT_SYMBOL(system_is_up); + +å¨å½æ°ååä¸ï¼å å«å½æ°ååå®ä»¬çæ°æ®ç±»åãè½ç¶Cè¯è¨é没æè¿æ ·çè¦æ±ï¼å¨Linuxéè¿ +æ¯æå¡çåæ³ï¼å 为è¿æ ·å¯ä»¥å¾ç®åçç»è¯»è æä¾æ´å¤çæä»·å¼çä¿¡æ¯ã + + + 第ä¸ç« ï¼éä¸çå½æ°éåºéå¾ + +è½ç¶è¢«æäºäººå£°ç§°å·²ç»è¿æ¶ï¼ä½æ¯gotoè¯å¥ççä»·ç©è¿æ¯ç»å¸¸è¢«ç¼è¯å¨æ使ç¨ï¼å ·ä½å½¢å¼æ¯ +æ æ¡ä»¶è·³è½¬æ令ã + +å½ä¸ä¸ªå½æ°ä»å¤ä¸ªä½ç½®éåºå¹¶ä¸éè¦åä¸äºéç¨çæ¸ çå·¥ä½çæ¶åï¼gotoç好å¤å°±æ¾ç°åºæ¥ +äºã + +çç±æ¯ï¼ + +- æ æ¡ä»¶è¯å¥å®¹æç解åè·è¸ª +- åµå¥ç¨åº¦åå° +- å¯ä»¥é¿å ç±äºä¿®æ¹æ¶å¿è®°æ´æ°æ个åç¬çéåºç¹è导è´çé误 +- åè½»äºç¼è¯å¨çå·¥ä½ï¼æ éå é¤åä½ä»£ç ;) + +int fun(int a) +{ + int result = 0; + char *buffer = kmalloc(SIZE); + + if (buffer == NULL) + return -ENOMEM; + + if (condition1) { + while (loop1) { + ... + } + result = 1; + goto out; + } + ... +out: + kfree(buffer); + return result; +} + + ç¬¬å «ç« ï¼æ³¨é + +注éæ¯å¥½çï¼ä¸è¿æè¿åº¦æ³¨éçå±é©ãæ°¸è¿ä¸è¦å¨æ³¨éé解éä½ ç代ç æ¯å¦ä½è¿ä½çï¼æ´å¥½ +çåæ³æ¯è®©å«äººä¸çä½ ç代ç å°±å¯ä»¥æç½ï¼è§£éåçå¾å·®ç代ç æ¯æµªè´¹æ¶é´ã + +ä¸è¬çï¼ä½ æ³è¦ä½ ç注éåè¯å«äººä½ ç代ç åäºä»ä¹ï¼èä¸æ¯æä¹åçãä¹è¯·ä½ ä¸è¦æ注é +æ¾å¨ä¸ä¸ªå½æ°ä½å é¨ï¼å¦æå½æ°å¤æå°ä½ éè¦ç¬ç«ç注éå ¶ä¸çä¸é¨åï¼ä½ å¾å¯è½éè¦åå° +第å ç« çä¸çãä½ å¯ä»¥åä¸äºå°æ³¨éæ¥æ³¨ææè¦åæäºå¾èªæï¼æè 槽ç³ï¼çåæ³ï¼ä½ä¸è¦ +å 太å¤ãä½ åºè¯¥åçï¼æ¯æ注éæ¾å¨å½æ°ç头é¨ï¼åè¯äººä»¬å®åäºä»ä¹ï¼ä¹å¯ä»¥å ä¸å®åè¿ +äºäºæ çåå ã + +å½æ³¨éå æ ¸APIå½æ°æ¶ï¼è¯·ä½¿ç¨kernel-docæ ¼å¼ã请ç +Documentation/kernel-doc-nano-HOWTO.txtåscripts/kernel-doc以è·å¾è¯¦ç»ä¿¡æ¯ã + +Linuxç注éé£æ ¼æ¯C89â/* ... */âé£æ ¼ãä¸è¦ä½¿ç¨C99é£æ ¼â// ...â注éã + +é¿ï¼å¤è¡ï¼çé¦é注éé£æ ¼æ¯ï¼ + + /* + * This is the preferred style for multi-line + * comments in the Linux kernel source code. + * Please use it consistently. + * + * Description: A column of asterisks on the left side, + * with beginning and ending almost-blank lines. + */ + +注éæ°æ®ä¹æ¯å¾éè¦çï¼ä¸ç®¡æ¯åºæ¬ç±»åè¿æ¯è¡çç±»åã为äºæ¹ä¾¿å®ç°è¿ä¸ç¹ï¼æ¯ä¸è¡åºåª +声æä¸ä¸ªæ°æ®ï¼ä¸è¦ä½¿ç¨éå·æ¥ä¸æ¬¡å£°æå¤ä¸ªæ°æ®ï¼ãè¿æ ·ä½ å°±æ空é´æ¥ä¸ºæ¯ä¸ªæ°æ®åä¸æ®µ +å°æ³¨éæ¥è§£éå®ä»¬çç¨éäºã + + + 第ä¹ç« ï¼ä½ å·²ç»æäºæ å¼ç³äº + +è¿æ²¡ä»ä¹ï¼æ们é½æ¯è¿æ ·ãå¯è½ä½ ç使ç¨äºå¾é¿æ¶é´Unixçæåå·²ç»åè¯ä½ âGNU emacsâè½ +èªå¨å¸®ä½ æ ¼å¼åCæºä»£ç ï¼èä¸ä½ ä¹æ³¨æå°äºï¼ç¡®å®æ¯è¿æ ·ï¼ä¸è¿å®æ使ç¨çé»è®¤å¼åæ们 +æ³è¦çç¸å»çè¿ï¼å®é ä¸ï¼çè³æ¯éæºæçè¿è¦å·®ââæ æ°ä¸ªç´åå¨GNU emacséæåæ°¸è¿ä¸ +ä¼åé åºä¸ä¸ªå¥½ç¨åºï¼ï¼è¯æ³¨ï¼è¯·åèInfinite Monkey Theoremï¼ + +æä»¥ä½ è¦ä¹æ¾å¼GNU emacsï¼è¦ä¹æ¹åå®è®©å®ä½¿ç¨æ´åçç设å®ãè¦éç¨åä¸ä¸ªæ¹æ¡ï¼ä½ å¯ +以æä¸é¢è¿æ®µç²è´´å°ä½ ç.emacsæ件éã + +(defun linux-c-mode () + "C mode with adjusted defaults for use with the Linux kernel." + (interactive) + (c-mode) + (c-set-style "K&R") + (setq tab-width 8) + (setq indent-tabs-mode t) + (setq c-basic-offset 8)) + +è¿æ ·å°±å®ä¹äºM-x linux-c-modeå½ä»¤ãå½ä½ hackä¸ä¸ªæ¨¡åçæ¶åï¼å¦æä½ æå符串 +-*- linux-c -*-æ¾å¨å¤´ä¸¤è¡çæ个ä½ç½®ï¼è¿ä¸ªæ¨¡å¼å°ä¼è¢«èªå¨è°ç¨ãå¦æä½ å¸æå¨ä½ ä¿®æ¹ +/usr/src/linuxéçæ件æ¶éæ¯è¬èªå¨æå¼linux-c-modeçè¯ï¼ä½ ä¹å¯è½éè¦æ·»å + +(setq auto-mode-alist (cons '("/usr/src/linux.*/.*\\.[ch]$" . linux-c-mode) + auto-mode-alist)) + +å°ä½ ç.emacsæ件éã + +ä¸è¿å°±ç®ä½ å°è¯è®©emacsæ£ç¡®çæ ¼å¼å代ç 失败äºï¼ä¹å¹¶ä¸æå³çä½ å¤±å»äºä¸åï¼è¿å¯ä»¥ç¨â +indentâã + +ä¸è¿ï¼GNU indentä¹æåGNU emacsä¸æ ·æé®é¢ç设å®ï¼æä»¥ä½ éè¦ç»å®ä¸äºå½ä»¤é项ãä¸ +è¿ï¼è¿è¿ä¸ç®å¤ªç³ç³ï¼å 为就ç®æ¯GNU indentçä½è ä¹è®¤åK&Rçæå¨æ§ï¼GNUç人并ä¸æ¯å +人ï¼ä»ä»¬åªæ¯å¨è¿ä¸ªé®é¢ä¸è¢«ä¸¥éç误导äºï¼ï¼æä»¥ä½ åªè¦ç»indentæå®é项â-kr -i8â +ï¼ä»£è¡¨âK&Rï¼8个å符缩è¿âï¼ï¼æè 使ç¨âscripts/Lindentâï¼è¿æ ·å°±å¯ä»¥ä»¥ææ¶é«¦çæ¹å¼ +缩è¿æºä»£ç ã + +âindentâæå¾å¤é项ï¼ç¹å«æ¯éæ°æ ¼å¼å注éçæ¶åï¼ä½ å¯è½éè¦çä¸ä¸å®çæå页ãä¸è¿ +è®°ä½ï¼âindentâä¸è½ä¿®æ£åçç¼ç¨ä¹ æ¯ã + + + 第åç« ï¼Kconfigé ç½®æ件 + +对äºéå¸æºç æ çææKconfig*é ç½®æ件æ¥è¯´ï¼å®ä»¬ç¼©è¿æ¹å¼ä¸C代ç ç¸æ¯ææä¸åãç´§æ¨ +å¨âconfigâå®ä¹ä¸é¢çè¡ç¼©è¿ä¸ä¸ªå¶è¡¨ç¬¦ï¼å¸®å©ä¿¡æ¯ååå¤ç¼©è¿2ä¸ªç©ºæ ¼ãæ¯å¦ï¼ + +config AUDIT + bool "Auditing support" + depends on NET + help + Enable auditing infrastructure that can be used with another + kernel subsystem, such as SELinux (which requires this for + logging of avc messages output). Does not do system-call + auditing without CONFIG_AUDITSYSCALL. + +ä»ç¶è¢«è®¤ä¸ºä¸å¤ç¨³å®çåè½åºè¯¥è¢«å®ä¹ä¸ºä¾èµäºâEXPERIMENTALâï¼ + +config SLUB + depends on EXPERIMENTAL && !ARCH_USES_SLAB_PAGE_STRUCT + bool "SLUB (Unqueued Allocator)" + ... + +èé£äºå±é©çåè½ï¼æ¯å¦æäºæ件系ç»çåæ¯æï¼åºè¯¥å¨å®ä»¬çæ示å符串éæ¾èç声æè¿ +ä¸ç¹ï¼ + +config ADFS_FS_RW + bool "ADFS write support (DANGEROUS)" + depends on ADFS_FS + ... + +è¦æ¥çé ç½®æ件çå®æ´ææ¡£ï¼è¯·çDocumentation/kbuild/kconfig-language.txtã + + + 第åä¸ç« ï¼æ°æ®ç»æ + +å¦æä¸ä¸ªæ°æ®ç»æï¼å¨å建åéæ¯å®çå线æ§è¡ç¯å¢ä¹å¤å¯è§ï¼é£ä¹å®å¿ é¡»è¦æä¸ä¸ªå¼ç¨è®¡ +æ°å¨ãå æ ¸é没æåå¾æ¶éï¼å¹¶ä¸å æ ¸ä¹å¤çåå¾æ¶éæ ¢ä¸æçä½ä¸ï¼ï¼è¿æå³çä½ ç»å¯¹é +è¦è®°å½ä½ 对è¿ç§æ°æ®ç»æç使ç¨æ åµã + +å¼ç¨è®¡æ°æå³çä½ è½å¤é¿å ä¸éï¼å¹¶ä¸å 许å¤ä¸ªç¨æ·å¹¶è¡è®¿é®è¿ä¸ªæ°æ®ç»æââèä¸éè¦æ å¿ +è¿ä¸ªæ°æ®ç»æä» ä» å 为ææ¶ä¸è¢«ä½¿ç¨å°±æ¶å¤±äºï¼é£äºç¨æ·å¯è½ä¸è¿æ¯æ²ç¡äºä¸éµæè åäºä¸ +äºå ¶ä»äºæ èå·²ã + +注æä¸éä¸è½å代å¼ç¨è®¡æ°ãä¸éæ¯ä¸ºäºä¿ææ°æ®ç»æçä¸è´æ§ï¼èå¼ç¨è®¡æ°æ¯ä¸ä¸ªå å管 +çæå·§ãé常äºè é½éè¦ï¼ä¸è¦æ两个ææ··äºã + +å¾å¤æ°æ®ç»æå®é ä¸æ2级å¼ç¨è®¡æ°ï¼å®ä»¬é常æä¸åâç±»âçç¨æ·ãå类计æ°å¨ç»è®¡åç±»ç¨ +æ·çæ°éï¼æ¯å½å类计æ°å¨åè³é¶æ¶ï¼å ¨å±è®¡æ°å¨åä¸ã + +è¿ç§âå¤çº§å¼ç¨è®¡æ°âçä¾åå¯ä»¥å¨å å管çï¼âstruct mm_structâï¼mm_usersåmm_countï¼ +åæ件系ç»ï¼âstruct super_blockâï¼s_countås_activeï¼ä¸æ¾å°ã + +è®°ä½ï¼å¦æå¦ä¸ä¸ªæ§è¡çº¿ç´¢å¯ä»¥æ¾å°ä½ çæ°æ®ç»æï¼ä½æ¯è¿ä¸ªæ°æ®ç»æ没æå¼ç¨è®¡æ°å¨ï¼è¿ +éå ä¹è¯å®æ¯ä¸ä¸ªbugã + + + 第åäºç« ï¼å®ï¼æ举åRTL + +ç¨äºå®ä¹å¸¸éçå®çåååæ举éçæ ç¾éè¦å¤§åã + +#define CONSTANT 0x12345 + +å¨å®ä¹å 个ç¸å ³ç常éæ¶ï¼æ好ç¨æ举ã + +å®çåå请ç¨å¤§ååæ¯ï¼ä¸è¿å½¢å¦å½æ°çå®çååå¯ä»¥ç¨å°ååæ¯ã + +ä¸è¬çï¼å¦æè½åæå èå½æ°å°±ä¸è¦åæåå½æ°çå®ã + +å«æå¤ä¸ªè¯å¥çå®åºè¯¥è¢«å å«å¨ä¸ä¸ªdo-while代ç åéï¼ + +#define macrofun(a, b, c) \ + do { \ + if (a == 5) \ + do_this(b, c); \ + } while (0) + +使ç¨å®çæ¶ååºé¿å çäºæ ï¼ + +1) å½±åæ§å¶æµç¨çå®ï¼ + +#define FOO(x) \ + do { \ + if (blah(x) < 0) \ + return -EBUGGERED; \ + } while(0) + +é常ä¸å¥½ãå®çèµ·æ¥åä¸ä¸ªå½æ°ï¼ä¸è¿å´è½å¯¼è´âè°ç¨âå®çå½æ°éåºï¼ä¸è¦æ乱读è 大èé +çè¯æ³åæå¨ã + +2) ä¾èµäºä¸ä¸ªåºå®ååçæ¬å°åéçå®ï¼ + +#define FOO(val) bar(index, val) + +å¯è½çèµ·æ¥åæ¯ä¸ªä¸éçä¸è¥¿ï¼ä¸è¿å®é常容ææ读代ç ç人æç³æ¶ï¼èä¸å®¹æ导è´çèµ·æ¥ +ä¸ç¸å ³çæ¹å¨å¸¦æ¥é误ã + +3) ä½ä¸ºå·¦å¼ç带åæ°çå®ï¼ FOO(x) = yï¼å¦ææ人æFOOåæä¸ä¸ªå èå½æ°çè¯ï¼è¿ç§ç¨ +æ³å°±ä¼åºéäºã + +4) å¿è®°äºä¼å 级ï¼ä½¿ç¨è¡¨è¾¾å¼å®ä¹å¸¸éçå®å¿ é¡»å°è¡¨è¾¾å¼ç½®äºä¸å¯¹å°æ¬å·ä¹å ã带åæ°ç +å®ä¹è¦æ³¨ææ¤ç±»é®é¢ã + +#define CONSTANT 0x4000 +#define CONSTEXP (CONSTANT | 3) + +cppæå对å®ç讲解å¾è¯¦ç»ãGcc internalsæåä¹è¯¦ç»è®²è§£äºRTLï¼è¯æ³¨ï¼register +transfer languageï¼ï¼å æ ¸éçæ±ç¼è¯è¨ç»å¸¸ç¨å°å®ã + + + 第åä¸ç« ï¼æå°å æ ¸æ¶æ¯ + +å æ ¸å¼åè åºè¯¥æ¯åè¿è¯å¥½æè²çã请ä¸å®æ³¨æå æ ¸ä¿¡æ¯çæ¼åï¼ä»¥ç»äººä»¥å¥½çå°è±¡ãä¸è¦ +ç¨ä¸è§èçåè¯æ¯å¦âdontâï¼èè¦ç¨âdo notâæè âdon'tâãä¿è¯è¿äºä¿¡æ¯ç®åãæäºãæ +æ§ä¹ã + +å æ ¸ä¿¡æ¯ä¸å¿ 以å¥å·ï¼è¯æ³¨ï¼è±æå¥å·ï¼å³ç¹ï¼ç»æã + +å¨å°æ¬å·éæå°æ°å(%d)没æä»»ä½ä»·å¼ï¼åºè¯¥é¿å è¿æ ·åã + +<linux/device.h>éæä¸äºé©±å¨æ¨¡åè¯æå®ï¼ä½ åºè¯¥ä½¿ç¨å®ä»¬ï¼ä»¥ç¡®ä¿ä¿¡æ¯å¯¹åºäºæ£ç¡®ç +设å¤å驱å¨ï¼å¹¶ä¸è¢«æ è®°äºæ£ç¡®çæ¶æ¯çº§å«ãè¿äºå®æï¼dev_err(), dev_warn(), +dev_info()ççã对äºé£äºä¸åæ个ç¹å®è®¾å¤ç¸å ³è¿çä¿¡æ¯ï¼<linux/kernel.h>å®ä¹äº +pr_debug()åpr_info()ã + +ååºå¥½çè°è¯ä¿¡æ¯å¯ä»¥æ¯ä¸ä¸ªå¾å¤§çææï¼å½ä½ ååºæ¥ä¹åï¼è¿äºä¿¡æ¯å¨è¿ç¨é¤éçæ¶å +å°±ä¼æ为æ大ç帮å©ãå½DEBUG符å·æ²¡æ被å®ä¹çæ¶åï¼è¿äºä¿¡æ¯ä¸åºè¯¥è¢«ç¼è¯è¿å æ ¸é +ï¼ä¹å°±æ¯è¯´ï¼é»è®¤å°ï¼å®ä»¬ä¸åºè¯¥è¢«å å«å¨å ï¼ãå¦æä½ ä½¿ç¨dev_dbg()æè pr_debug()ï¼ +å°±è½èªå¨è¾¾å°è¿ä¸ªææãå¾å¤åç³»ç»æ¥æKconfigé项æ¥å¯ç¨-DDEBUGãè¿æä¸ä¸ªç¸å ³çæ¯ä¾ +æ¯ä½¿ç¨VERBOSE_DEBUGæ¥æ·»å dev_vdbg()æ¶æ¯å°é£äºå·²ç»ç±DEBUGå¯ç¨çæ¶æ¯ä¹ä¸ã + + + 第ååç« ï¼åé å å + +å æ ¸æä¾äºä¸é¢çä¸è¬ç¨éçå ååé å½æ°ï¼kmalloc()ï¼kzalloc()ï¼kcalloc()å +vmalloc()ã请åèAPIæ档以è·åæå ³å®ä»¬ç详ç»ä¿¡æ¯ã + +ä¼ éç»æä½å¤§å°çé¦éå½¢å¼æ¯è¿æ ·çï¼ + + p = kmalloc(sizeof(*p), ...); + +å¦å¤ä¸ç§ä¼ éæ¹å¼ä¸ï¼sizeofçæä½æ°æ¯ç»æä½çååï¼è¿æ ·ä¼éä½å¯è¯»æ§ï¼å¹¶ä¸å¯è½ä¼å¼ +å ¥bugãæå¯è½æéåéç±»å被æ¹åæ¶ï¼è对åºçä¼ éç»å ååé å½æ°çsizeofçç»æä¸åã + +强å¶è½¬æ¢ä¸ä¸ªvoidæéè¿åå¼æ¯å¤ä½çãCè¯è¨æ¬èº«ä¿è¯äºä»voidæéå°å ¶ä»ä»»ä½æéç±»å +ç转æ¢æ¯æ²¡æé®é¢çã + + + 第åäºç« ï¼å èå¼ç + +æä¸ä¸ªå¸¸è§ç误解æ¯å èå½æ°æ¯gccæä¾çå¯ä»¥è®©ä»£ç è¿è¡æ´å¿«çä¸ä¸ªé项ãè½ç¶ä½¿ç¨å è +å½æ°ææ¶åæ¯æ°å½çï¼æ¯å¦ä½ä¸ºä¸ç§æ¿ä»£å®çæ¹å¼ï¼è¯·ç第åäºç« ï¼ï¼ä¸è¿å¾å¤æ åµä¸ä¸æ¯ +è¿æ ·ãinlineå ³é®åçè¿åº¦ä½¿ç¨ä¼ä½¿å æ ¸å大ï¼ä»è使æ´ä¸ªç³»ç»è¿è¡é度åæ ¢ãå 为大å æ ¸ +ä¼å ç¨æ´å¤çæ令é«éç¼åï¼è¯æ³¨ï¼ä¸çº§ç¼åé常æ¯æ令ç¼ååæ°æ®ç¼ååå¼çï¼èä¸ä¼å¯¼ +è´pagecacheçå¯ç¨å ååå°ãæ³è±¡ä¸ä¸ï¼ä¸æ¬¡pagecacheæªå½ä¸å°±ä¼å¯¼è´ä¸æ¬¡ç£ç寻åï¼å° +èæ¶5毫ç§ã5毫ç§çæ¶é´å CPUè½æ§è¡å¾å¤å¾å¤æ令ã + +ä¸ä¸ªåºæ¬çååæ¯å¦æä¸ä¸ªå½æ°æ3è¡ä»¥ä¸ï¼å°±ä¸è¦æå®åæå èå½æ°ãè¿ä¸ªååçä¸ä¸ªä¾ +å¤æ¯ï¼å¦æä½ ç¥éæ个åæ°æ¯ä¸ä¸ªç¼è¯æ¶å¸¸éï¼èä¸å 为è¿ä¸ªå¸¸éä½ ç¡®å®ç¼è¯å¨å¨ç¼è¯æ¶è½ +ä¼åæä½ çå½æ°ç大é¨å代ç ï¼é£ä»ç¶å¯ä»¥ç»å®å ä¸inlineå ³é®åãkmalloc()å èå½æ°å°± +æ¯ä¸ä¸ªå¾å¥½çä¾åã + +人们ç»å¸¸ä¸»å¼ ç»staticçèä¸åªç¨äºä¸æ¬¡çå½æ°å ä¸inlineï¼å¦æ¤ä¸ä¼æä»»ä½æ失ï¼å 为没 +æä»ä¹å¥½æè¡¡çãè½ç¶ä»ææ¯ä¸è¯´è¿æ¯æ£ç¡®çï¼ä½æ¯å®é ä¸è¿ç§æ åµä¸å³ä½¿ä¸å inline gcc +ä¹å¯ä»¥èªå¨ä½¿å ¶å èãèä¸å ¶ä»ç¨æ·å¯è½ä¼è¦æ±ç§»é¤inlineï¼ç±æ¤èæ¥çäºè®ºä¼æµæ¶inline +èªèº«çæ½å¨ä»·å¼ï¼å¾ä¸å¿å¤±ã + + + 第åå ç« ï¼å½æ°è¿åå¼åå½å + +å½æ°å¯ä»¥è¿åå¾å¤ç§ä¸åç±»åçå¼ï¼æ常è§çä¸ç§æ¯è¡¨æå½æ°æ§è¡æåæè 失败çå¼ãè¿æ · +çä¸ä¸ªå¼å¯ä»¥è¡¨ç¤ºä¸ºä¸ä¸ªé误代ç æ´æ°ï¼-Exxxï¼å¤±è´¥ï¼0ï¼æåï¼æè ä¸ä¸ªâæåâå¸å°å¼ï¼ +0ï¼å¤±è´¥ï¼é0ï¼æåï¼ã + +æ··å使ç¨è¿ä¸¤ç§è¡¨è¾¾æ¹å¼æ¯é¾äºåç°çbugçæ¥æºãå¦æCè¯è¨æ¬èº«ä¸¥æ ¼åºåæ´å½¢åå¸å°åå +éï¼é£ä¹ç¼è¯å¨å°±è½å¤å¸®æ们åç°è¿äºé误â¦â¦ä¸è¿Cè¯è¨ä¸åºåã为äºé¿å 产çè¿ç§bugï¼è¯· +éµå¾ªä¸é¢çæ¯ä¾ï¼ + + å¦æå½æ°çååæ¯ä¸ä¸ªå¨ä½æè 强å¶æ§çå½ä»¤ï¼é£ä¹è¿ä¸ªå½æ°åºè¯¥è¿åé误代ç æ´ + æ°ãå¦ææ¯ä¸ä¸ªå¤æï¼é£ä¹å½æ°åºè¯¥è¿åä¸ä¸ªâæåâå¸å°å¼ã + +æ¯å¦ï¼âadd workâæ¯ä¸ä¸ªå½ä»¤ï¼æ以add_work()å½æ°å¨æåæ¶è¿å0ï¼å¨å¤±è´¥æ¶è¿å-EBUSYã +类似çï¼å 为âPCI device presentâæ¯ä¸ä¸ªå¤æï¼æ以pci_dev_present()å½æ°å¨æåæ¾å° +ä¸ä¸ªå¹é ç设å¤æ¶åºè¯¥è¿å1ï¼å¦ææ¾ä¸å°æ¶åºè¯¥è¿å0ã + +ææ导åºï¼è¯æ³¨ï¼EXPORTï¼çå½æ°é½å¿ é¡»éµå®è¿ä¸ªæ¯ä¾ï¼ææçå ¬å ±å½æ°ä¹é½åºè¯¥å¦æ¤ãç§ +æï¼staticï¼å½æ°ä¸éè¦å¦æ¤ï¼ä½æ¯æ们ä¹æ¨èè¿æ ·åã + +è¿åå¼æ¯å®é 计ç®ç»æèä¸æ¯è®¡ç®æ¯å¦æåçæ å¿çå½æ°ä¸åæ¤æ¯ä¾çéå¶ãä¸è¬çï¼ä»ä»¬ +éè¿è¿åä¸äºæ£å¸¸å¼èå´ä¹å¤çç»ææ¥è¡¨ç¤ºåºéãå ¸åçä¾åæ¯è¿åæéçå½æ°ï¼ä»ä»¬ä½¿ç¨ +NULLæè ERR_PTRæºå¶æ¥æ¥åé误ã + + + 第åä¸ç« ï¼ä¸è¦éæ°åæå æ ¸å® + +头æ件include/linux/kernel.hå å«äºä¸äºå®ï¼ä½ åºè¯¥ä½¿ç¨å®ä»¬ï¼èä¸è¦èªå·±åä¸äºå®ä»¬ç +åç§ãæ¯å¦ï¼å¦æä½ éè¦è®¡ç®ä¸ä¸ªæ°ç»çé¿åº¦ï¼ä½¿ç¨è¿ä¸ªå® + + #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) + +类似çï¼å¦æä½ è¦è®¡ç®æç»æä½æåç大å°ï¼ä½¿ç¨ + + #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f)) + +è¿æå¯ä»¥åä¸¥æ ¼çç±»åæ£æ¥çmin()åmax()å®ï¼å¦æä½ éè¦å¯ä»¥ä½¿ç¨å®ä»¬ãä½ å¯ä»¥èªå·±çç +é£ä¸ªå¤´æ件éè¿å®ä¹äºä»ä¹ä½ å¯ä»¥æ¿æ¥ç¨çä¸è¥¿ï¼å¦ææå®ä¹çè¯ï¼ä½ å°±ä¸åºå¨ä½ ç代ç é +èªå·±éæ°å®ä¹ã + + + 第åå «ç« ï¼ç¼è¾å¨æ¨¡å¼è¡åå ¶ä»éè¦ç½å¦çäºæ + +æä¸äºç¼è¾å¨å¯ä»¥è§£éåµå ¥å¨æºæ件éçç±ä¸äºç¹æ®æ è®°æ æçé 置信æ¯ãæ¯å¦ï¼emacs +è½å¤è§£é被æ è®°æè¿æ ·çè¡ï¼ + +-*- mode: c -*- + +æè è¿æ ·çï¼ + +/* +Local Variables: +compile-command: "gcc -DMAGIC_DEBUG_FLAG foo.c" +End: +*/ + +Vimè½å¤è§£éè¿æ ·çæ è®°ï¼ + +/* vim:set sw=8 noet */ + +ä¸è¦å¨æºä»£ç ä¸å å«ä»»ä½è¿æ ·çå 容ãæ¯ä¸ªäººé½æä»èªå·±çç¼è¾å¨é ç½®ï¼ä½ çæºæ件ä¸åº +该è¦çå«äººçé ç½®ãè¿å æ¬æå ³ç¼©è¿å模å¼é ç½®çæ è®°ã人们å¯ä»¥ä½¿ç¨ä»ä»¬èªå·±å®å¶ç模 +å¼ï¼æè 使ç¨å ¶ä»å¯ä»¥äº§çæ£ç¡®ç缩è¿çå·§å¦æ¹æ³ã + + + + éå½ Iï¼åè + +The C Programming Language, 第äºç, ä½è Brian W. KernighanåDenni +M. Ritchie. Prentice Hall, Inc., 1988. ISBN 0-13-110362-8 (软ç®), +0-13-110370-9 (硬ç®). URL: http://cm.bell-labs.com/cm/cs/cbook/ + +The Practice of Programming ä½è Brian W. KernighanåRob Pike. Addison-Wesley, +Inc., 1999. ISBN 0-201-61586-X. URL: http://cm.bell-labs.com/cm/cs/tpop/ + +cppï¼gccï¼gcc internalsåindentçGNUæåââåK&Råæ¬æç¸ç¬¦åçé¨åï¼å ¨é¨å¯ä»¥å¨ +http://www.gnu.org/manual/æ¾å° + +WG14æ¯Cè¯è¨çå½é æ ååå·¥ä½ç»ï¼URL: http://www.open-std.org/JTC1/SC22/WG14/ + +Kernel [EMAIL PROTECTED] 2002ï¼ +http://www.kroah.com/linux/talks/ols_2002_kernel_codingstyle_talk/html/ + +-- +æåæ´æ°äº2007å¹´7æ13æ¥ã -- 1.5.3.8 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [EMAIL PROTECTED] More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/