From: Thomas Weißschuh Date: Sun, 1 Oct 2023 11:57:12 +0000 (+0200) Subject: include/c.h: add helpers for unaligned structure access X-Git-Tag: v2.40-rc1~221^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=68459714838c8c0c2d34b6d658638f5d59298bc7;p=thirdparty%2Futil-linux.git include/c.h: add helpers for unaligned structure access Signed-off-by: Thomas Weißschuh --- diff --git a/include/c.h b/include/c.h index 752f568c50..224a8e54fe 100644 --- a/include/c.h +++ b/include/c.h @@ -213,6 +213,14 @@ (type *)( (char *)__mptr - offsetof(type,member) );}) #endif +#define read_unaligned_member(p, m) __extension__ ({ \ + size_t offset = offsetof(__typeof__(* p), m); \ + __typeof__(p->m) v; \ + memcpy(&v, ((unsigned char *)p) + offset, sizeof(v)); \ + v; }) + +#define member_ptr(p, m) (((unsigned char *)p) + offsetof(__typeof__(*p), m)) + #ifndef HAVE_PROGRAM_INVOCATION_SHORT_NAME # ifdef HAVE___PROGNAME extern char *__progname;