Linux內核中的面向對象設計

lwn.net網站有兩篇文章, 學習一下...

https://lwn.net/Articles/444910/

https://lwn.net/Articles/446317/


Object-oriented design patterns in the kernel, part 1

面向對象相關知識有 objects(對象), classes(類), method(方法), inheritance(繼承), polymorphism(多態)等等。

這邊直接進入正題:

1. Method Dispatch - 方法調度

The large variety of styles of inheritance and rules for its usage in languages today seems to suggest that there is no uniform understanding of what "object-oriented" really means. The term is a bit like "love": everyone thinks they know what it means but when you get down to details people can find they have very different ideas. While what it means to be "oriented" might not be clear, what we mean by an "object" does seem to be uniformly agreed upon. It is simply an abstraction comprising both state and behavior. An object is like a record (Pascal) or struct (C), except that some of the names of members refer to functions which act on the other fields in the object. These function members are sometimes referred to a "methods".

The most obvious way to implement objects in C is to declare a "struct" where some fields are pointers to functions which take a pointer to the struct itself as their first argument. The calling convention for method "foo" in object "bar" would simply be: bar->foo(bar, ...args); While this pattern is used in the Linux kernel it is not the dominant pattern so we will leave discussion of it until a little later.

 

即C語言中, 常見方法是將函數指針作爲struct數據結構的成員,  

可以使用bar->foo(bar, ...args); 作爲基於對象的方法調用。

 

As methods (unlike state) are not normally changed on a per-object basis, a more common and only slightly less obvious approach is to collect all the methods for a particular class of objects into a separate structure, sometimes known as a "virtual function table" or vtable. The object then has a single pointer to this table rather than a separate pointer for each method, and consequently uses less memory.

 

由於方法(與狀態不同)通常不會再每個對象的基礎上進行更改,  因此一種更常見的方法是將特定類對象的所有方法收集到一個單獨的結構體中, 有時稱爲"虛函數表"或"vtable"。 然後, 該對象只有一個指向該表的指針,  而不是每種方法都有一個單獨的指針, 因此使用的內存更少。


未完待續....



Object-oriented design patterns in the kernel, part 2

2. Data inheritance -  數據繼承

Inheritance is a core concept of object-oriented programming, though it comes in many forms, whether prototype inheritance, mixin inheritance, subtype inheritance, interface inheritance etc., some of which overlap. The form that is of interest when exploring the Linux kernel is most like subtype inheritance, where a concrete or "final" type inherits some data fields from a "virtual" parent type. We will call this "data inheritance" to emphasize the fact that it is the data rather than the behavior that is being inherited.

Put another way, a number of different implementations of a particular interface share, and separately extend, a common data structure. They can be said to inherit from that data structure. There are three different approaches to this sharing and extending that can be found in the Linux kernel, and all can be seen by exploring the struct inode structure and its history, though they are widely used elsewhere.

 

1). Extension through unions

通過聯合擴展

......

 

待續...

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章