博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux内核中的面向对象设计
阅读量:3556 次
发布时间:2019-05-20

本文共 3045 字,大约阅读时间需要 10 分钟。

lwn.net网站有两篇文章, 学习一下...


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 . 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

通过联合扩展

......

 

待续...

转载地址:http://zpcrj.baihongyu.com/

你可能感兴趣的文章
BFC(Block Formatting Context)
查看>>
什么是作用域,什么是闭包,什么是作用域链
查看>>
惰性求值,面向对象
查看>>
数据结构之列表
查看>>
发布/订阅模式 vs 观察者模式
查看>>
es5中的arguments对象
查看>>
git本地仓库和远程仓库关联,分支重命名
查看>>
js对象的深拷贝,你真的觉得很简单吗?
查看>>
你真的了解map方法吗?手动实现数组map方法。
查看>>
带你手动实现call方法,让你收获满满
查看>>
前端知识体系
查看>>
查找入职员工时间排名倒数第三的员工所有信息
查看>>
使用join查询方式找出没有分类的电影id以及名称
查看>>
Qt教程(2) : Qt元对象系统
查看>>
驱动开发误用指针错误:Unable to handle kernel NULL pointer dereference at virtual address
查看>>
Linux部署DocSystem知识/文件管理系统
查看>>
Centos7开机自启动脚本无法使用备用方案
查看>>
jvm虚拟机内存详解
查看>>
线程的创建方式
查看>>
DNS是什么
查看>>