Lua UTString 如何确保字符串的最大对齐?

我正在阅读 Lua (5.3.0) 的源代码,在 lobject.h 中发现它使用了一种奇怪的方法来操作字符串,如下所示:

/*
 ** Header for string value; string bytes follow the end of this structure
 ** (aligned according to 'UTString'; see next).
 */
typedef struct TString {
    CommonHeader;
    lu_byte extra;  /* reserved words for short strings; "has hash" for longs */
    unsigned int hash;
    size_t len;  /* number of characters in string */
    struct TString *hnext;  /* linked list for hash table */
} TString;

/*
** Ensures that address after this type is always fully aligned.
*/
typedef union UTString {
    L_Umaxalign dummy;  /* ensures maximum alignment for strings */
    TString tsv;
} UTString;

/*
 ** Get the actual string (array of bytes) from a 'TString'.
 ** (Access to 'extra' ensures that value is really a 'TString'.)
 */
#define getaddrstr(ts)  (cast(char *, (ts)) + sizeof(UTString))
#define getstr(ts)  \
check_exp(sizeof((ts)->extra), cast(const char*, getaddrstr(ts)))

我在 这里 找到了一个很好的关于使用这种方法的原因的答案。但是我对 ensures maximum alignment for strings 意思是什么感到困惑?为什么我们需要 maximum alignment?如何保证?

点赞