misc: add type-safe "offsetof"

This uses the GCC extension "typeof" to implement a type-safe "offsetof"
similar to the implementation in the linux kernel.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
This commit is contained in:
David Herrmann 2012-04-22 15:40:39 +02:00
parent 68633aee06
commit 7a7f977d43

View File

@ -34,6 +34,14 @@
#include <stdbool.h>
#include <stdlib.h>
#include <stddef.h>
/* miscellaneous */
#define kmscon_offsetof(pointer, type, member) ({ \
const typeof(((type*)0)->member) *__ptr = (pointer); \
(type*)(((char*)__ptr) - offsetof(type, member)); \
})
/* ring buffer for arbitrary byte-streams */