From: Krzysztof Opasiak Date: Tue, 14 Apr 2015 09:00:45 +0000 (+0200) Subject: libusbgx: Add offsetof() and container_of() macros X-Git-Tag: libusbgx-v0.1.0~76 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=1ef2546b35105ee8b36ecf2f64daf961a67564c9;p=platform%2Fupstream%2Flibusbg.git libusbgx: Add offsetof() and container_of() macros Add some macros which are widely used in linux kernel and may be useful also in this library. Signed-off-by: Krzysztof Opasiak Reviewed-by: Pawel Szewczyk --- diff --git a/include/usbg/usbg_internal.h b/include/usbg/usbg_internal.h index dbbd0dd..793319c 100644 --- a/include/usbg/usbg_internal.h +++ b/include/usbg/usbg_internal.h @@ -28,6 +28,16 @@ * @file include/usbg/usbg_internal.h */ +#ifndef offsetof +#define offsetof(type, member) __builtin_offsetof (type, member) +#endif /* offsetof */ + +#ifndef container_of +#define container_of(ptr, type, field) ({ \ + const typeof(((type *)0)->field) *member = (ptr); \ + (type *)( (char *)member - offsetof(type, field) ); \ + }) +#endif /* container_of */ struct usbg_state {