# bpcp **Repository Path**: undulators/bpcp ## Basic Information - **Project Name**: bpcp - **Description**: No description available - **Primary Language**: C - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-01-22 - **Last Updated**: 2024-01-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # bpcp ## linux kernel coding style https://www.kernel.org/doc/html/latest/process/coding-style.html ### 一.缩进 1. 使用TAB进行缩进,不要使用空格。一个TAB = 8个字符 2. 一行放置一行语句,不要把多行语句放在一行。 3. 将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; fallthrough; default: break; } ``` ### 二、长行 1. 单行长度不要超过80列,超过80列的保持和左括号对齐。 ### 三、大括号 1. 大括号左边放置最后,右边放置行首。(适用于if, switch, for, while, do) ``` if (x is true) { we do y } ``` 2. 函数的左、右大括号都放置下一个行首 ``` int function(int x) { body of function } ``` 3. do-while 和 if-else-if如下所示: ``` do { body of do-loop } while (condition); ``` ``` if (x == y) { .. } else if (x > y) { ... } else { .... } ``` 4. 只有一行语句,可以省略大括号。 ``` if (condition) do_this(); else do_that(); ``` 5. 多行一句话简单语句,以及if控制语句多行,else一行,不可以省略大括号。 ``` if (condition) { do_this(); do_that(); } else { otherwise(); } ``` ``` while (condition) { if (test) do_something(); } ``` ### 四、空格 1. if, switch, case, for, do, while 关键字后需要使用一个空格。 2. sizeof, typeof, alignof, or __attribute__后不需要使用空格。 ``` s = sizeof(struct file); ``` 3. 指针*的放置位置是和名称靠近,不和类型靠近。 ``` char *linux_banner; unsigned long long memparse(char *ptr, char **retptr); char *match_strdup(substring_t *s); ``` 4. = + - < > * / % | & ^ <= >= == != ? : 前后使用空格 5. 后无空格:& * + - ~ ! sizeof typeof alignof __attribute__ defined 6. p++; p--; ++p; --p; 7. 行尾不能留空格。 ### 五、命名 1. 遵循K & R 规则 : this_is_an_integer 2. 一般性原则: - 使用正确的英文术语及其简称 node, child, sibling, parent, root, first, last, next, previous, - 避免使用如下名称: item、data - 正确使用时态 linked_list - 正确使用单数、复数形式 nodes, children - 使用喜闻乐见的缩写 nr, sz, dbl, tri, len, max, min, buf, ver, id, prev, tmp, param, arg, argc, argv, conn, ctxt, err, … 3. 局部变量 - 简洁 循环:i, j, k;数量:n;长度:len; 尺寸:sz;指针:p; 临时变量:tmp/temp;临时缓冲区:buf; 4. 全局变量:可以使用匈牙利命名法thisIsAnInteger 5. 仅针对 extern 变量或者函数使用下划线前缀(主要用于 防止命名污染) ``` extern size_t __total_mem_use; ``` ### 六、typedef 1. 尽量不使用typedef 2. 仅在函数库的接口定义中使用 typedef 3. 仅对结构的指针使用 typedef,并使用 _t 后缀 4. 对枚举或者结构的类型定义名称使用驼峰命名法 ### 七、goto 1. goto 仅在处理函数的错误返回时使用,用于将函数的返 回统一在函数末尾处理。 ### 八、注释 1. 尽量不要在函数体内部添加注释 2. 防止注释使用过度 3. 多行注释的格式如下: ``` /* * 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. */ ``` ### 九、宏 1. 在枚举中定义常量和标签的宏的名称要大写。 ``` #define CONSTANT 0x12345 ``` 2. 功能宏可以小写定义,具有多个语句的宏应包含在do - while块中。 ``` #define macrofun(a, b, c) \ do { \ if (a == 5) \ do_this(b, c); \ } while (0) ``` ### 十、inline 1. 一个合理的经验法则是,不要将超过3行代码的函数内联。 2. 尽量inline函数前面添加static修饰。