日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

結構體通過成員變量獲取主結構體地址(struct)

作者:泰勒朗斯 更新時間: 2022-07-16 編程語言

今天在看代碼的時候看到一個巧妙的用法:
兩個結構體,一個是V4L2Context,另外一個是V4L2m2mContext;

typedef struct V4L2m2mContext {
    char devname[PATH_MAX];
    int fd;

    /* the codec context queues */
    V4L2Context capture;
    V4L2Context output;

    /* dynamic stream reconfig */
    AVCodecContext *avctx;
    sem_t refsync;
    atomic_uint refcount;
    int reinit;
...
}

如果我們知道其中capture或者output的地址能不能推出來V4L2m2mContext的地址呢,這樣就可以V4L2Context少一個指針變量了。
答案是可以的:

#define container_of(ptr, type, member) ({ \
        const __typeof__(((type *)0)->member ) *__mptr = (ptr); \
        (type *)((char *)__mptr - offsetof(type,member) );})

關于其分析:分析

實際使用:

static inline V4L2m2mContext *buf_to_m2mctx(V4L2Buffer *buf)
{
    return V4L2_TYPE_IS_OUTPUT(buf->context->type) ?
        container_of(buf->context, V4L2m2mContext, output) :
        container_of(buf->context, V4L2m2mContext, capture);
}

原文鏈接:https://blog.csdn.net/weixin_43360707/article/details/125780922

欄目分類
最近更新