]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
include/c.h: add helpers for unaligned structure access
authorThomas Weißschuh <thomas@t-8ch.de>
Sun, 1 Oct 2023 11:57:12 +0000 (13:57 +0200)
committerThomas Weißschuh <thomas@t-8ch.de>
Sun, 1 Oct 2023 12:00:57 +0000 (14:00 +0200)
Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
include/c.h

index 752f568c50aaccbe17938f2e7834bb693f96853e..224a8e54feae8ad8aadf2460e6281ac656d62b4a 100644 (file)
        (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;