2012/09/12

Get container pointer from a member

Tricks from Linux kernel source:

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

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)


An example

static void free_sched_group_rcu(struct rcu_head *rhp)
{
    /* now it should be safe to free those cfs_rqs */
    free_sched_group(container_of(rhp, struct task_group, rcu));
}

No comments:

Post a Comment

Post Code on Blogger

Simplest way to post code to blogger for me: <pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black;overflow-x:...