本文共 3045 字,大约阅读时间需要 10 分钟。
lwn.net网站有两篇文章, 学习一下...
面向对象相关知识有 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 . The object then has a single pointer to this table rather than a separate pointer for each method, and consequently uses less memory.
由于方法(与状态不同)通常不会再每个对象的基础上进行更改, 因此一种更常见的方法是将特定类对象的所有方法收集到一个单独的结构体中, 有时称为"虚函数表"或"vtable"。 然后, 该对象只有一个指向该表的指针, 而不是每种方法都有一个单独的指针, 因此使用的内存更少。
未完待续....
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
通过联合扩展
......
待续...
转载地址:http://zpcrj.baihongyu.com/