Linux内核编程:防御性编程学习
来源:互联网 发布:超级淘宝店txt下载落秋 编辑:程序博客网 时间:2024/04/19 15:40
/* *Kernel : Linux2.6.32.63 *File : \scripts\mod\modpost.h \scripts\mod\modpost.c *Author : DavidLin *Date : 2014-12-25pm *Email : linpeng1577@163.com or linpeng1577@gmail.com *world : the city of SZ, in China *Ver : 000.000.001 *history : editor time do * 1)LinPeng 2014-12-25 created this file! * 2) */
/* Linux kernel code : modpost.h & modpost.c , author is someone, not me *//* modpost.h */#define NOFAIL(ptr) do_nofail((ptr), #ptr) void* do_nofail (void* ptr, const char* expr);/* end of modpost.h *//* modpost.c */void* do_nofail(void* ptr, const char* expr){ if(!ptr) fatal("modpost: Memory allocation failure:%s.\n", expr); return ptr;}static struct module* new_module(char* modname){ struct module* mod; char *p, *s; mod = NOFAIL(malloc(sizeof(*mod))); memset(mod, 0, sizeof(*mod)); p = NOFAIL(strdup(modname)); /* strip trailing .o */ s = strrchr(p, '.'); if(s != NULL) if(strcmp(s, ".o") == 0) *s = '\0'; /* add to list */ mod->name = p; mod->gpl_compatible = -1; mod->next = modules; modules = mod; return mod;}/* end of modpost.c */
每个个体都需要重复演化,每个工程师也不例外。一般入门的时候,我们可能如下编写代码:
开始如同这步:
struct module* mod;char *p, *s;mod = malloc(sizeof(struct module));memset(mod, 0, sizeof(struct module));//未考虑mod == NULL
于是更进一步:
struct module* mod;char *p, *s;mod = malloc(sizeof(struct module));if(NULL == mod) return NULL;memset(mod, 0, sizeof(struct module));
如何再进一步?
是否每次使用malloc函数,都需要增加一句 if(NULL == ptr) return NULL;
是否可以优化? 是的,可以!
#define NOFAIL(ptr) do_nofail((ptr), #ptr) void* do_nofail (void* ptr, const char* expr); mod = NOFAIL(malloc(sizeof(*mod))); memset(mod, 0, sizeof(*mod));1.非空封装已经封装在NOFAIL中,不仅编码工作减少,通过宏命名的自注释,可以很清楚知道该malloc期望的是只许成功,不许失败(当然只是期待),而且可以防止if(NULL == ptr)的偶然遗漏,更可更好的调试及维护代码;
2.使用sizeof(*mod)代替sizeof(struct module),使得mod类型变更时,并不会影响到具体函数,达到代码自适应效果。
还可以更进一步?
可以,消化吸收,根据项目具体应用情景,具体事物具体优化。
0 0
- Linux内核编程:防御性编程学习
- 防御性编程
- 防御性编程
- 防御性编程
- 防御性编程
- 防御性编程
- 防御性编程
- 什么是防御性编程,如何进行防御性编程
- 防御性编程技巧
- 防御性编程简介
- 【ThinkingInC++】70、防御性编程
- Linux 内核编程学习
- 《代码大全》之《防御性编程》
- 开始学习linux内核编程
- linux 内核编程学习资源
- C++编程思想(卷二):防御性编程
- 《Windows核心编程》の防御性编程--断言
- C++编程思想(卷二):防御性编程
- 用java界面实现根据当前系统时间定时更新二维码
- 黑马程序员学习_反射
- 用例图
- HDU OJ 1016 Prime Ring Problem
- HDU 3335 Divisibility(DLX可重复覆盖)
- Linux内核编程:防御性编程学习
- 专心与勤奋的差别
- 黑马程序员——JAVA核心:面向对象
- 爬取chaoshi.tmall商品详情
- 如何让你手上的debian 使用更方便更容易
- java线程中信号量Semaphore类的应用
- vim 自动提示、自动补齐插件 YouCompleteMe for windows Gvim 安装及使用效果
- SpringMVC 中整合JSON、XML视图
- web中缓存的几种方式