vnode 上的转换操作(在 NetBSD 中)

casting operations on vnode (in NetBSD)

NetBSD 系统文件usr/src/sys/sys/vnode.h 定义了一个vnode 的结构。 但是,我看到有时在执行操作时(假设 ufs_getattr),vnode* 作为 void* 传递给被调用的操作。

每个这样的操作都有其参数结构。例如,ufs_getattr() 结构如下:

struct vop_getattr_args /* {
                struct vnode    *a_vp;
                struct vattr    *a_vap;
                kauth_cred_t    a_cred;
};

通常,这些操作的第一行执行将vnode 指针(实际上是void*)赋值到此参数类型指针的指针中。例如,我们做类似的事情:

int
ufs_getattr(void *v)
{
        struct vop_getattr_args /* {
                struct vnode    *a_vp;
                struct vattr    *a_vap;
                kauth_cred_t    a_cred;
        } */ *ap = v;            //why this is okay to do ?
        struct vnode    *vp;
        struct inode    *ip;
        struct vattr    *vap;

        vp = ap->a_vp;           //wont this break ?

摘自usr/src/sys/ufs/ufs/ufs_vnops.c

由于 C 编程知识很少,我无法证明这种不匹配的赋值是正确的,因为类型并不真正匹配。

在 C 中,将 void* 分配给 T* 是合法的(不是 ,因此在 C++ 中)。因此代码完全有效。

K&R 引用 "The C Programming Language 2nd Edition":

Any pointer to an object may be converted to type void * without loss of information. If the result is converted back to the original pointer type, the original pointer is recovered. Unlike the pointer-to-pointer conversions discussed in Par.A.6.6, which generally require an explicit cast, pointers may be assigned to and from pointers of type void *, and may be compared with them.


作为一个有趣的旁注,关于 void* 的历史(来自同一本书):

This interpretation of void * pointers is new; previously, char * pointers played the role of generic pointer. The ANSI standard specifically blesses the meeting of void * pointers with object pointers in assignments and relationals, while requiring explicit casts for other pointer mixtures.