Code convention

  1. 函数和循环后的括号和花括号之间应该空一格int getListLen (ListNode *head)
  2. 用++i 而不是i++
  3. 每个变量定义都尽量占一行,不要将多个变量定义在一行
ListNode *head = nullptr;
ListNode *tail = nullptr;
ListNode *middle = nullptr;
modutils.c不一样 没问题的版本/* * Common modutils related functions for busybox * * Copyright (C) 2008 by Timo Teras <timo.teras@iki.fi> * * Licensed under GPLv2 or later, see file LICENSE in this source tree. */ #include "modutils.h" #ifdef __UCLIBC__ extern int init_module(void *module, unsigned long len, const char *options); extern int delete_module(const char *module, unsigned int flags); #else # include <sys/syscall.h> # define init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts) # define delete_module(mod, flags) syscall(__NR_delete_module, mod, flags) #endif void FAST_FUNC replace(char *s, char what, char with) { while (*s) { if (what == *s) *s = with; ++s; } } char* FAST_FUNC replace_underscores(char *s) { replace(s, '-', '_'); return s; } int FAST_FUNC string_to_llist(char *string, llist_t **llist, const char *delim) { char *tok; int len = 0; while ((tok = strsep(&string, delim)) != NULL) { if (tok[0] == '\0') continue; llist_add_to_end(llist, xstrdup(tok)); len += strlen(tok); } return len; } char* FAST_FUNC filename2modname(const char *filename, char *modname) { int i; char *from; if (filename == NULL) return NULL; if (modname == NULL) modname = xmalloc(MODULE_NAME_LEN); from = bb_get_last_path_component_nostrip(filename); for (i = 0; i < (MODULE_NAME_LEN-1) && from[i] != '\0' && from[i] != '.'; i++) modname[i] = (from[i] == '-') ? '_' : from[i]; modname[i] = '\0'; return modname; } char* FAST_FUNC parse_cmdline_module_options(char **argv, int quote_spaces) { char *options; int optlen; options = xzalloc(1); optlen = 0; while (*++argv) { const char *fmt; const char *var; const char *val; var = *argv; options = xrealloc(options, optlen + 2 + strlen(var) + 2); fmt = "%.*s%s "; val = strchrnul(var, '='); if (quote_spaces) { /* * modprobe (module-init-tools version 3.11.1) compat: * quote only value: * var="val with spaces", not "var=val with spaces" * (note: var *name* is not checked for spaces!) */ if (*val) { /* has var=val format. skip '=' */ val++; if (strchr(val, ' ')) fmt = "%.*s\"%s\" "; } } optlen += sprintf(options + optlen, fmt, (int)(val - var), var, val); } /* Remove trailing space. Disabled */ /* if (optlen != 0) options[optlen-1] = '\0'; */ return options; } #if ENABLE_FEATURE_INSMOD_TRY_MMAP void* FAST_FUNC try_to_mmap_module(const char *filename, size_t *image_size_p) { /* We have user reports of failure to load 3MB module * on a 16MB RAM machine. Apparently even a transient * memory spike to 6MB during module load * is too big for that system. */ void *image; struct stat st; int fd; fd = xopen(filename, O_RDONLY); fstat(fd, &st); image = NULL; /* st.st_size is off_t, we can't just pass it to mmap */ if (st.st_size <= *image_size_p) { size_t image_size = st.st_size; image = mmap(NULL, image_size, PROT_READ, MAP_PRIVATE, fd, 0); if (image == MAP_FAILED) { image = NULL; } else if (*(uint32_t*)image != SWAP_BE32(0x7f454C46)) { /* No ELF signature. Compressed module? */ munmap(image, image_size); image = NULL; } else { /* Success. Report the size */ *image_size_p = image_size; } } close(fd); return image; } #endif /* Return: * 0 on success, * -errno on open/read error, * errno on init_module() error */ int FAST_FUNC bb_init_module(const char *filename, const char *options) { size_t image_size; char *image; int rc; bool mmaped; if (!options) options = ""; //TODO: audit bb_init_module_24 to match error code convention #if ENABLE_FEATURE_2_4_MODULES if (get_linux_version_code() < KERNEL_VERSION(2,6,0)) return bb_init_module_24(filename, options); #endif image_size = INT_MAX - 4095; mmaped = 0; image = try_to_mmap_module(filename, &image_size); if (image) { mmaped = 1; } else { errno = ENOMEM; /* may be changed by e.g. open errors below */ image = xmalloc_open_zipped_read_close(filename, &image_size); if (!image) return -errno; } errno = 0; init_module(image, image_size, options); rc = errno; if (mmaped) munmap(image, image_size); else free(image); return rc; } int FAST_FUNC bb_delete_module(const char *module, unsigned int flags) { errno = 0; delete_module(module, flags); return errno; } const char* FAST_FUNC moderror(int err) { switch (err) { case -1: /* btw: it's -EPERM */ return "no such module"; case ENOEXEC: return "invalid module format"; case ENOENT: return "unknown symbol in module, or unknown parameter"; case ESRCH: return "module has wrong symbol version"; case ENOSYS: return "kernel does not support requested operation"; } if (err < 0) /* should always be */ err = -err; return strerror(err); } 有问题的版本/* * Common modutils related functions for busybox * * Copyright (C) 2008 by Timo Teras <timo.teras@iki.fi> * * Licensed under GPLv2 or later, see file LICENSE in this source tree. */ #include "modutils.h" #include <sys/syscall.h> #define init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts) #if defined(__NR_finit_module) # define finit_module(fd, uargs, flags) syscall(__NR_finit_module, fd, uargs, flags) #endif #define delete_module(mod, flags) syscall(__NR_delete_module, mod, flags) static module_entry *helper_get_module(module_db *db, const char *module, int create) { char modname[MODULE_NAME_LEN]; struct module_entry *e; unsigned i, hash; filename2modname(module, modname); hash = 0; for (i = 0; modname[i]; i++) hash = ((hash << 5) + hash) + modname[i]; hash %= MODULE_HASH_SIZE; for (e = db->buckets[hash]; e; e = e->next) if (strcmp(e->modname, modname) == 0) return e; if (!create) return NULL; e = xzalloc(sizeof(*e)); e->modname = xstrdup(modname); e->next = db->buckets[hash]; db->buckets[hash] = e; IF_DEPMOD(e->dnext = e->dprev = e;) return e; } module_entry* FAST_FUNC moddb_get(module_db *db, const char *module) { return helper_get_module(db, module, 0); } module_entry* FAST_FUNC moddb_get_or_create(module_db *db, const char *module) { return helper_get_module(db, module, 1); } void FAST_FUNC moddb_free(module_db *db) { module_entry *e, *n; unsigned i; for (i = 0; i < MODULE_HASH_SIZE; i++) { for (e = db->buckets[i]; e; e = n) { n = e->next; free(e->name); free(e->modname); free(e); } } } void FAST_FUNC replace(char *s, char what, char with) { while (*s) { if (what == *s) *s = with; ++s; } } int FAST_FUNC string_to_llist(char *string, llist_t **llist, const char *delim) { char *tok; int len = 0; while ((tok = strsep(&string, delim)) != NULL) { if (tok[0] == '\0') continue; llist_add_to_end(llist, xstrdup(tok)); len += strlen(tok); } return len; } char* FAST_FUNC filename2modname(const char *filename, char *modname) { char local_modname[MODULE_NAME_LEN]; int i; const char *from; if (filename == NULL) return NULL; if (modname == NULL) modname = local_modname; // Disabled since otherwise "modprobe dir/name" would work // as if it is "modprobe name". It is unclear why // 'basenamization' was here in the first place. //from = bb_get_last_path_component_nostrip(filename); from = filename; for (i = 0; i < (MODULE_NAME_LEN-1) && from[i] != '\0' && from[i] != '.'; i++) modname[i] = (from[i] == '-') ? '_' : from[i]; modname[i] = '\0'; if (modname == local_modname) return xstrdup(modname); return modname; } #if ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS char* FAST_FUNC parse_cmdline_module_options(char **argv, int quote_spaces) { char *options; int optlen; options = xzalloc(1); optlen = 0; while (*++argv) { const char *fmt; const char *var; const char *val; var = *argv; options = xrealloc(options, optlen + 2 + strlen(var) + 2); fmt = "%.*s%s "; val = strchrnul(var, '='); if (quote_spaces) { /* * modprobe (module-init-tools version 3.11.1) compat: * quote only value: * var="val with spaces", not "var=val with spaces" * (note: var *name* is not checked for spaces!) */ if (*val) { /* has var=val format. skip '=' */ val++; if (strchr(val, ' ')) fmt = "%.*s\"%s\" "; } } optlen += sprintf(options + optlen, fmt, (int)(val - var), var, val); } /* Remove trailing space. Disabled */ /* if (optlen != 0) options[optlen-1] = '\0'; */ return options; } #endif #if ENABLE_FEATURE_INSMOD_TRY_MMAP void* FAST_FUNC try_to_mmap_module(const char *filename, size_t *image_size_p) { /* We have user reports of failure to load 3MB module * on a 16MB RAM machine. Apparently even a transient * memory spike to 6MB during module load * is too big for that system. */ void *image; struct stat st; int fd; fd = xopen(filename, O_RDONLY); fstat(fd, &st); image = NULL; /* st.st_size is off_t, we can't just pass it to mmap */ if (st.st_size <= *image_size_p) { size_t image_size = st.st_size; image = mmap_read(fd, image_size); if (image == MAP_FAILED) { image = NULL; } else if (*(uint32_t*)image != SWAP_BE32(0x7f454C46)) { /* No ELF signature. Compressed module? */ munmap(image, image_size); image = NULL; } else { /* Success. Report the size */ *image_size_p = image_size; } } close(fd); return image; } #endif /* Return: * 0 on success, * -errno on open/read error, * errno on init_module() error */ int FAST_FUNC bb_init_module(const char *filename, const char *options) { size_t image_size; char *image; int rc; bool mmaped; if (!options) options = ""; //TODO: audit bb_init_module_24 to match error code convention #if ENABLE_FEATURE_2_4_MODULES if (get_linux_version_code() < KERNEL_VERSION(2,6,0)) return bb_init_module_24(filename, options); #endif /* * First we try finit_module if available. Some kernels are configured * to only allow loading of modules off of secure storage (like a read- * only rootfs) which needs the finit_module call. If it fails, we fall * back to normal module loading to support compressed modules. */ # ifdef __NR_finit_module { int fd = open(filename, O_RDONLY | O_CLOEXEC); if (fd >= 0) { rc = finit_module(fd, options, 0) != 0; bb_error_msg("can't insert111 '%s'", options); close(fd); if (rc == 0) return rc; } } # endif image_size = INT_MAX - 4095; mmaped = 0; image = try_to_mmap_module(filename, &image_size); if (image) { mmaped = 1; } else { errno = ENOMEM; /* may be changed by e.g. open errors below */ image = xmalloc_open_zipped_read_close(filename, &image_size); bb_error_msg("can't insert222 '%s''%s'", options, filename); if (!image) return -errno; } errno = 0; init_module(image, image_size, options); bb_error_msg("can't insert333 '%s''%s'", options, image); rc = errno; if (mmaped) munmap(image, image_size); else free(image); return rc; } int FAST_FUNC bb_delete_module(const char *module, unsigned int flags) { errno = 0; delete_module(module, flags); return errno; } /* Note: not suitable for delete_module() errnos. * For them, probably only EWOULDBLOCK needs explaining: * "Other modules depend on us". So far we don't do such * translation and don't use moderror() for removal errors. */ const char* FAST_FUNC moderror(int err) { switch (err) { case -1: /* btw: it's -EPERM */ return "no such module"; case ENOEXEC: return "invalid module format"; case ENOENT: return "unknown symbol in module, or unknown parameter"; case ESRCH: return "module has wrong symbol version"; case ENOSYS: return "kernel does not support requested operation"; } if (err < 0) /* should always be */ err = -err; return strerror(err); }
最新发布
12-05
在编程中,遵循良好的代码命名规范是编写高质量、可维护性强的代码的关键之一。合理的命名不仅有助于他人理解代码意图,也有助于开发者自身在未来回顾和修改代码时更加高效。以下是与代码命名规范相关的一些最佳实践: ### 命名应具有描述性 变量、函数、类以及其他代码元素的名称应清晰表达其用途或含义。避免使用模糊或过于宽泛的名称(如 `data`、`value`),而应选择更具体的名称(如 `userName`、`calculateTotalPrice`)[^2]。 ### 使用一致的命名风格 不同语言通常有推荐的命名约定,例如: - **Java/C#**:采用驼峰命名法(camelCase),如 `firstName`。 - **Python/Ruby**:采用蛇形命名法(snake_case),如 `first_name`。 - **C++**:根据项目或团队偏好,可能支持驼峰或蛇形命名。 - **常量**:通常使用全大写字母加下划线,如 `MAX_USERS`。 保持整个项目或团队内部的一致性比遵循语言惯例更重要。 ### 函数名应体现行为 函数名称应该是一个动词或动词短语,表示该函数执行的操作,例如 `saveUser()`、`validateInput()`。对于返回布尔值的函数,可以使用类似 `isReady()` 或 `hasPermission()` 的命名方式。 ### 类名应为名词 类名通常为名词或名词短语,代表某种实体或概念,如 `User`、`PaymentProcessor`。接口或抽象类也可以使用形容词形式,如 `Runnable` 或 `Serializable`。 ### 避免缩写和单字母变量 除非是循环计数器(如 `i`, `j`),否则应尽量避免使用缩写或单字母变量名。使用完整单词可以提高代码可读性,例如使用 `index` 而不是 `idx`。 ### 包/模块命名 包名通常为小写字母,结构上反映项目层级,例如 `com.example.project.utils`。模块或文件夹命名也应具有明确的功能指向,如 `auth`、`user_management`。 ### 布尔变量命名 布尔类型的变量命名建议以 `is`、`has`、`should` 等开头,使其含义一目了然,例如 `isLoggedIn`、`hasChildren`。 ### 常量命名 常量应使用全大写字母,并通过下划线分隔单词,例如 `DEFAULT_TIMEOUT`。这种格式有助于快速识别哪些是不可变值。 ### 命名重构的重要性 随着项目的发展,某些变量或函数的职责可能会发生变化。在这种情况下,及时重构名称以反映其新功能是非常重要的,这有助于维护代码的一致性和清晰度。 ### 示例代码 ```python # 不推荐 def calc(a, b): return a * b # 推荐 def calculate_total_price(quantity, unit_price): return quantity * unit_price ``` ```java // 不推荐 String d = "2023-10-01"; // 推荐 String dateOfBirth = "2023-10-01"; ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值