Kernel开发 - SubmittingPatches,有关ifdef和static inline & macro
2013-01-14 16:32
417 查看
From: Documentation/SubmittingPatches
2) #ifdefs are ugly
Code cluttered with ifdefs is difficult to read and maintain. Don't doit. Instead, put your ifdefs in a header, and conditionally define'static inline' functions, or macros, which are used in the code.Let the compiler optimize away the "no-op" case.Simple example, of poor code: dev = alloc_etherdev (sizeof(struct funky_private)); if (!dev) return -ENODEV; #ifdef CONFIG_NET_FUNKINESS init_funky_net(dev); #endifCleaned-up example:(in header) #ifndef CONFIG_NET_FUNKINESS static inline void init_funky_net (struct net_device *d) {} #endif(in the code itself) dev = alloc_etherdev (sizeof(struct funky_private)); if (!dev) return -ENODEV; init_funky_net(dev);3) 'static inline' is better than a macroStatic inline functions are greatly preferred over macros.They provide type safety, have no length limitations, no formattinglimitations, and under gcc they are as cheap as macros.Macros should only be used for cases where a static inline is clearlysuboptimal [there are a few, isolated cases of this in fast paths],or where it is impossible to use a static inline function [such asstring-izing].'static inline' is preferred over 'static __inline__', 'extern inline',and 'extern __inline__'.
2) #ifdefs are ugly
Code cluttered with ifdefs is difficult to read and maintain. Don't doit. Instead, put your ifdefs in a header, and conditionally define'static inline' functions, or macros, which are used in the code.Let the compiler optimize away the "no-op" case.Simple example, of poor code: dev = alloc_etherdev (sizeof(struct funky_private)); if (!dev) return -ENODEV; #ifdef CONFIG_NET_FUNKINESS init_funky_net(dev); #endifCleaned-up example:(in header) #ifndef CONFIG_NET_FUNKINESS static inline void init_funky_net (struct net_device *d) {} #endif(in the code itself) dev = alloc_etherdev (sizeof(struct funky_private)); if (!dev) return -ENODEV; init_funky_net(dev);3) 'static inline' is better than a macroStatic inline functions are greatly preferred over macros.They provide type safety, have no length limitations, no formattinglimitations, and under gcc they are as cheap as macros.Macros should only be used for cases where a static inline is clearlysuboptimal [there are a few, isolated cases of this in fast paths],or where it is impossible to use a static inline function [such asstring-izing].'static inline' is preferred over 'static __inline__', 'extern inline',and 'extern __inline__'.
相关文章推荐
- how to enable #ifdef macro in the command line of make?
- There are inconsistent line endings in the 'xxx' script. Some are Mac OS X (UNIX) and some are Windows
- HDU5095 Linearization of the kernel functions in SVM && 2014上海全国邀请赛
- 【cocos2d-x 报错异常集】运行安卓虚拟机的时候出现了emulator: ERROR: Can't find 'Linux version ' string in kernel image fi
- 例解gcc内嵌汇编(inline assembly)中&修饰符(modifier)的作用
- static inline int register_chrdev(unsigned int major, const char *name, const stru
- PM2 - Documentation - (PM2 API & Using PM2 in Cloud Providers & Expose static files)
- static inline
- extern inline和static inline
- ASSERT: "width > 0.0" in file painting\qrasterizer.cpp, line 710
- C++中三大修饰符 static const inline
- macro Vs inline
- static inline
- 有关TypeError: invalid 'in' operand obj的错误
- static inline与内部、外部链接对象
- 实用程序软件包(关于使用静态函数报错问题“Static function declared but not defined in C+++ "eh.h is only for C++!"报错问题)
- The error may involve XXX(包)-Inline && The error occurred while setting parameters
- static inline
- 高级Linux Kernel Inline Hook技术分析与实现
- static inline和extern inline辨析