diff options
Diffstat (limited to 'container_of.c')
-rw-r--r-- | container_of.c | 67 |
1 files changed, 37 insertions, 30 deletions
diff --git a/container_of.c b/container_of.c index 5fa6134..13f8e69 100644 --- a/container_of.c +++ b/container_of.c @@ -1,7 +1,7 @@ /* * File : container_of.c * Author : Jérémy Zurcher <jeremy@asynk.ch> - * Date : 01/11/09 + * Date : 01/11/09 * License : * * Permission is hereby granted, free of charge, to any person obtaining @@ -11,10 +11,10 @@ * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: - * + * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. - * + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND @@ -29,40 +29,47 @@ #include "stdlib.h" #include "stddef.h" -#define container_of(ptr, type, member) ({ \ - const typeof( ((type *)0)->member ) *__mptr = (ptr); \ - (type *)( (char *)__mptr - offsetof(type,member) );}) - -struct node { - int data1; - int data2; - int data3; - int data4; - int data5; -}; +#define container_of(ptr, type, member) ({ \ + const typeof( ((type *)0)->member ) *__mptr = (ptr); \ + (type *)( (char *)__mptr - offsetof(type,member) );}) -#define my_offset(ptr, type, member) ( { (type*)(((char*)ptr)-offsetof(type,member)); } ) +#define containerof(ptr, type, member) ( { (type*)(((char*)ptr)-offsetof(type,member)); } ) -struct node n; +struct node +{ + int data1; + int data2; + int data3; + int data4; + int data5; +}; -struct node* use_container_of() { - return container_of( &n.data3, struct node, data3); +static void _check(int cond, const char *msg) +{ + if(!cond) + { + fprintf(stderr,"%s\n",msg); + exit(EXIT_FAILURE); + } } -struct node* use_mine() { - return my_offset( &n.data3, struct node, data3); -/* return (struct node*)(((char*)&n.daat3)-OFFSET); */ -} +int main(int argc, char *argv[]) +{ + struct node n; -struct node* use_struct(){ - return &n; -} + _check((&n==container_of( &n.data1, struct node, data1)),"1 container_of failed"); + _check((&n==container_of( &n.data2, struct node, data2)),"2 container_of failed"); + _check((&n==container_of( &n.data3, struct node, data3)),"3 container_of failed"); + _check((&n==container_of( &n.data4, struct node, data4)),"4 container_of failed"); + _check((&n==container_of( &n.data5, struct node, data5)),"5 container_of failed"); -int main(int argc, char *argv[]) { + _check((&n==containerof( &n.data1, struct node, data1)),"1 container_of failed"); + _check((&n==containerof( &n.data2, struct node, data2)),"2 container_of failed"); + _check((&n==containerof( &n.data3, struct node, data3)),"3 container_of failed"); + _check((&n==containerof( &n.data4, struct node, data4)),"4 container_of failed"); + _check((&n==containerof( &n.data5, struct node, data5)),"5 container_of failed"); - printf ("%X\n", (int)use_container_of()); - printf ("%X\n", (int)use_mine()); - printf ("%X\n", (int)use_struct()); + printf("success\n"); - return EXIT_SUCCESS; + return EXIT_SUCCESS; } |