< Linux Kernel > Reference Counts
来源:互联网 发布:淘宝主图尺寸高度 编辑:程序博客网 时间:2024/05/01 15:03
When a piece of code tries to access a data structure that has already been freed, the kernel is
not very happy, and the user is rarely happy with the kernel's reaction. To avoid those nasty
problems, and to make garbage collection mechanisms easier and more effective (see the
section "Garbage Collection" later in this chapter), most data structures keep a reference
count. Good kernel citizens increment and decrement the reference count of every data
structure every time they save and release a reference, respectively, to the structure. For any
data structure type that requires a reference count, the kernel component that owns the
structure usually exports two functions that can be used to increment and decrement the
reference count. Such functions are usually called xxx_hold and xxx_release,
respectively. Sometimes the release function is called xxx_put instead (e.g., dev_put for
such they do not always write bug-free code. The use of the reference count is a simple but
effective mechanism to avoid freeing still-referenced data structures. However, it does not
always solve the problem completely. This is the consequence of forgetting to balance
increments and decrements:
• If you release a reference to a data structure but forget to call the xxx_release
function, the kernel will never allow the data structure to be freed (unless another
buggy piece of code happens to call the release function an extra time by mistake!).
later point you happen to be the only reference holder, the structure will be
prematurely freed because you are not accounted for. This case definitely can be
more catastrophic than the previous one; your next attempt to access the structure
can corrupt other data or cause a kernel panic that brings down the whole system
instantly.
not very happy, and the user is rarely happy with the kernel's reaction. To avoid those nasty
problems, and to make garbage collection mechanisms easier and more effective (see the
section "Garbage Collection" later in this chapter), most data structures keep a reference
count. Good kernel citizens increment and decrement the reference count of every data
structure every time they save and release a reference, respectively, to the structure. For any
data structure type that requires a reference count, the kernel component that owns the
structure usually exports two functions that can be used to increment and decrement the
reference count. Such functions are usually called xxx_hold and xxx_release,
respectively. Sometimes the release function is called xxx_put instead (e.g., dev_put for
net_device structures).
such they do not always write bug-free code. The use of the reference count is a simple but
effective mechanism to avoid freeing still-referenced data structures. However, it does not
always solve the problem completely. This is the consequence of forgetting to balance
increments and decrements:
• If you release a reference to a data structure but forget to call the xxx_release
function, the kernel will never allow the data structure to be freed (unless another
buggy piece of code happens to call the release function an extra time by mistake!).
This leads to gradual memory exhaustion.
later point you happen to be the only reference holder, the structure will be
prematurely freed because you are not accounted for. This case definitely can be
more catastrophic than the previous one; your next attempt to access the structure
can corrupt other data or cause a kernel panic that brings down the whole system
instantly.
When a data structure is to be removed for some reason, the reference holders can be
explicitly notified about its going away so that they can politely release their references. This
is done through notification chains. See the section "Reference Counts" in Chapter 8 for an
interesting example.
- < Linux Kernel > Reference Counts
- Linux内核设计模式–Reference Counts, kcref
- 1.2.3 Reference Counts(引用计数)
- Basic Data Structures and Algorithms in the Linux Kernel--reference
- Video4Linux Kernel API Reference
- andoid4.0编译 reference ‘counts’ cannot be declared ‘mutable’ [-fpermissive]
- 線上 Android/Linux Kernel Source Code瀏覽 - Android/Linux Source Code Cross Reference
- Video4Linux Kernel API Reference v0_119990430
- Video4Linux Kernel API Reference v0_119990430
- LINUX reference
- Android源码编译时遇到的问题:error:reference 'counts' cannot be declared 'mutable'
- linux kernel
- Linux Kernel
- Linux kernel
- Linux kernel
- linux kernel
- linux kernel
- Linux Kernel
- 挑战编程:抛硬币赌博游戏【转】
- iBatis调用返回两个游标的存储过程处理方法
- TwoSat
- apple相关网址
- SQLite 第三版中的数据类型
- < Linux Kernel > Reference Counts
- 如何在 ESRI ArcMap 中打开谷歌卫星地图
- C++小结1
- CentOS 6 修改yum源为本地光盘
- SQLite3 C/C++ 开发接口简介(API函数)
- Valabind
- JavaBean 之Hello World(入门实例)
- 内存映射nopage()
- 图形界面库Windows Forms、 MFC、WTL、WxWidgets、Qt、GTK 综合比较