util-linux/backport-include-c.h-add-helpers-for-unaligned-structure-acce.patch
zhangyao 28efad9cbd sync patches from upstream
(cherry picked from commit 3c35f41986548ed7577511fbce80652f353b4147)
2024-02-19 19:44:29 +08:00

36 lines
1.1 KiB
Diff

From 68459714838c8c0c2d34b6d658638f5d59298bc7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= <thomas@t-8ch.de>
Date: Sun, 1 Oct 2023 13:57:12 +0200
Subject: [PATCH] include/c.h: add helpers for unaligned structure access
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
---
include/c.h | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/include/c.h b/include/c.h
index 752f568c5..224a8e54f 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;
--
2.33.0