From dc5ac443695f6a7f02aa8c8a1eb8a560793200eb Mon Sep 17 00:00:00 2001 From: "Steven R. Loomis" Date: Wed, 8 Nov 2023 13:17:51 -0600 Subject: [PATCH] =?UTF-8?q?fix(common):=20kmx=20struct=20alignment=20?= =?UTF-8?q?=F0=9F=99=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - move KMX struct alignment to km_types.h For: #9138 --- common/include/km_types.h | 20 ++++++++++++++++++++ common/include/kmx_file.h | 18 ------------------ 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/common/include/km_types.h b/common/include/km_types.h index aa7fb2e518..7b2bbfb416 100644 --- a/common/include/km_types.h +++ b/common/include/km_types.h @@ -67,3 +67,23 @@ typedef wchar_t KMX_UCHAR; #endif typedef KMX_UCHAR* KMX_PUCHAR; + +// Alignment + +/* + When we read .kmx files, they have no alignment guarantees, so we need to tell + the compiler to generate unaligned-safe code for accesses to COMP_ structure + members. Note we are assuming that COMP_KEYBOARD is aligned because it is + always the start of the file, so will be at the start of any buffer which will + automatically be aligned correctly. +*/ +#ifdef __EMSCRIPTEN__ +typedef KMX_DWORD __attribute__((aligned(1))) KMX_DWORD_unaligned; +typedef KMX_BOOL __attribute__((aligned(1))) KMX_BOOL_unaligned; +typedef KMX_WORD __attribute__((aligned(1))) KMX_WORD_unaligned; +#else +// TODO: consider other platforms +#define KMX_DWORD_unaligned KMX_DWORD +#define KMX_BOOL_unaligned KMX_BOOL +#define KMX_WORD_unaligned KMX_WORD +#endif diff --git a/common/include/kmx_file.h b/common/include/kmx_file.h index 7dbc2261d0..cd37948989 100644 --- a/common/include/kmx_file.h +++ b/common/include/kmx_file.h @@ -7,24 +7,6 @@ #include -/* - When we read .kmx files, they have no alignment guarantees, so we need to tell - the compiler to generate unaligned-safe code for accesses to COMP_ structure - members. Note we are assuming that COMP_KEYBOARD is aligned because it is - always the start of the file, so will be at the start of any buffer which will - automatically be aligned correctly. -*/ -#ifdef __EMSCRIPTEN__ -typedef KMX_DWORD __attribute__((aligned(1))) KMX_DWORD_unaligned; -typedef KMX_BOOL __attribute__((aligned(1))) KMX_BOOL_unaligned; -typedef KMX_WORD __attribute__((aligned(1))) KMX_WORD_unaligned; -#else -// TODO: consider other platforms -#define KMX_DWORD_unaligned KMX_DWORD -#define KMX_BOOL_unaligned KMX_BOOL -#define KMX_WORD_unaligned KMX_WORD -#endif - #ifdef KM_CORE_LIBRARY // TODO: move this to a common namespace keyman::common::kmx_file or similar in the future namespace km {