Common Coding Patterns

Memory Caches

事先要申请,事后要销毁

kmem_cache_create

 


kmem_cache_destroy

再分配和退还


kmem_cache_alloc

 


kmem_cache_free

 

 

Reference Counts

 

每个模块到处 xxx_hold xxx_release函数用于增加和减少引用计数

 

 

Garbage Collection

 

两种方式, 同步 异步

 

Function Pointers and Virtual Function Tables (VFTs)

 

Vector Definitions

a placeholder

 

struct abc

{

  int a;

  char placeholder[0];

};

 

sizeof(struct abc) 是4

 

 

Compile-Time Optimization for Condition Checks

使用 likely和unlikely两个宏 可以优化代码

likely表示 经常会发生

unlikely表示 很少发生

 

Mutual Exclusion

 

Spin locks

This is a lock that can be held by only one thread of execution at a time. An attempt to acquire the lock by another thread of execution makes the latter loop until the lock is released. Because of the waste cause by looping, spin locks are used only on multiprocessor systems, and generally are used only when the developer expects the lock to be held for short intervals. Also because of the waste caused to other threads, a thread of execution must not sleep while holding a spin lock.


Read-write spin locks

When the uses of a given lock can be clearly classified as read-only and read-write, the use of read-write spin locks is preferred. The difference between spin locks and read-write spin locks is that in the latter, multiple readers can hold the lock at the same time. However, only one writer at a time can hold the lock, and no reader can acquire it when it is already held by a writer. Because readers are given higher priority over writers, this type of lock performs well when the number of readers (or the number of read-only lock acquisitions) is a good deal bigger than the number of writers (or the number or read-write lock acquisitions).

When the lock is acquired in read-only mode, it cannot be promoted to read-write mode directly: the lock must be released and reacquired in read-write mode.


Read-Copy-Update (RCU)

RCU is one of the latest mechanisms made available in Linux to provide mutual exclusion. It performs quite well under the following specific conditions:

  • Read-write lock requests are rare compared to read-only lock requests.

  • The code that holds the lock is executed atomically and does not sleep.

  • The data structures protected by the lock are accessed via pointers.

Catching Bugs

使用宏 BUG_ON 和 BUG_TRAP来捕捉bug

 

Statistics

 

做好 数据统计

 

Measuring Time

 

使用jiffies 全局变量

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值