zircon 内核使用C++的原则

新版内核改动非常大,感觉有人写的书要大改了[偷笑]。看上数百行代码似乎就能了解其结构了。

在内核里使用了C++,这么做不无代价,严格的原则很有意思。

C++ in Zircon

A subset of the C++14 language is used in the Zircon tree. This includes both the upper layers of the kernel (above the lk layer), as well as some userspace code. In particular, Zircon does not use the C++ standard library, and many language features are not used or allowed.

Language features

– Not allowed

– Exceptions

– RTTI and `dynamic_cast`

– Operator overloading

– Default parameters

– Virtual inheritance

– Statically constructed objects

– Trailing return type syntax

– Exception: when necessary for lambdas with otherwise unutterable return types

– Initializer lists

– `thread_local` in kernel code

– Allowed

– Pure interface inheritance

– Lambdas

– `constexpr`

– `nullptr`

– `enum class`es

– `template`s

– Plain old classes

– `auto`

– Multiple implementation inheritance

– But be judicious. This is used widely for e.g. intrusive container mixins.

– Needs more ruling TODO(cpu)

– Global constructors

– Currently we have these for global data structures.

Linux Kernel 尝试过C++,最后失败告终。有这些原则我看到了C++在内核发扬光大的曙光。

Exception, dynamic_cast 多少年了我都不愿意用,经历的血泪太多了。没有太多复杂对象,Operator overloading 也没有什么用武之地。借C++编译器的强大让代码组织方便一点没有坏处。

设备的支持,去掉了pixel,只剩下: acer12, hikey960, nuc, rpi3。下个月入手一台intel nuc看看内核,也能作为日常使用。

妄自瞎说开始:

感觉内核技术发展这么多年后,zircon的实践会很有意思。实用主义会让这个内核大放异彩,会又各种嵌入式系统改用它。

    原文作者:毒舌Ken
    原文地址: https://zhuanlan.zhihu.com/p/29781087
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞