[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Xen-devel] [PATCH 12/10] libxl: New convenience macro CONTAINING_STRUCT



On Mon, 2012-01-09 at 17:34 +0000, Ian Jackson wrote:
> Provide a convenient and type-safe wrapper which does the correct
> dance to subtract offsetof.
> 
> Signed-off-by: Ian Jackson <ian.jackson@xxxxxxxxxxxxx>
> ---
>  tools/libxl/libxl_internal.h |   35 +++++++++++++++++++++++++++++++++++
>  1 files changed, 35 insertions(+), 0 deletions(-)
> 
> diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
> index e0ff15c..e0c2ad6 100644
> --- a/tools/libxl/libxl_internal.h
> +++ b/tools/libxl/libxl_internal.h
> @@ -1224,6 +1224,41 @@ _hidden void libxl__ao__destroy(libxl_ctx*, libxl__ao 
> *ao);
>   * Convenience macros.
>   */
>  
> +/*
> + * [GET_]CONTAINING_STRUCT work like this.  Given:
> + *    typedef struct {
> + *      ...
> + *      member_type member_name;
> + *      ...
> + *    } outer_type;
> + * Then:
> + *    void GET_CONTAINING_STRUCT(outer_type *outer_var [NB:UPDATED],
> + *                               some_type *inner_ptr,
> + *                               member_name);
> + *    outer_type *CONTAINING_STRUCT(outer_type,
> + *                                  some_type *inner_ptr,
> + *                                  member_name);
> + * The semantics are that after:
> + *    outer_type outer, *outer_var;
> + *    member_type *inner_ptr = &outer->member_name;
> + *    GET_CONTAINING_STRUCT(outer_var, &outer_ptr->member_name, member_name)
> + * The following hold:
> + *    CONTAINING_STRUCT(inner_ptr, outer_type, member_name) == outer_ptr

There is no outer_ptr in the givens, did you mean outer_var or something
else?

It's not entirely clear to me what the distinction between the GET_ and
non GET_ variant is (just that one returns the thing and the other
updates a variable?) and/or why we would need both. The common operation
is to go from inner_ptr to outer_ptr I think and CONTAINING_STRUCT seems
to fill that niche just fine.

BTW, in Linux this is called container_of, which is maybe more familiar
to people?

> + *    outer_var == &outer
> + */
> +#define GET_CONTAINING_STRUCT(outer_var, inner_ptr, member_name)        \
> +    ((outer_var) = (void*)((char*)(inner_ptr) -                         \
> +                           offsetof(typeof(*(outer_var)), member_name)), \
> +     (void)(&(outer_var)->member_name ==                                \
> +           (typeof(inner_ptr))0) /* type check */,                      \
> +     (void)0)
> +#define CONTAINING_STRUCT(outer_type, inner_ptr, member_name)           \
> +    ({                                                                  \
> +        typeof(outer_type) *containing_struct;                          \
> +        GET_CONTAINING_STRUCT(containing_struct, inner_ptr, member_name); \
> +        containing_struct;                                              \
> +    })
> +
>  
>  /*
>   * All of these assume (or define)



_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.