如何在内存中对齐 ByteArray# 的数组负载与 GHC Haskell

How to align in memory the array payload of a ByteArray# with GHC Haskell

我有几个问题关于如何对齐不是 ByteArray# 而是 ByteArray# 数组负载 (存储的非元数据数据的实际字节数在数组中),这可能会因为 RTS 在内存中存储数组元数据 就在 数组有效负载之前

而变得复杂:

If you have a value of type ByteArray#, it definitely points to a heap object with type ARR_WORDS (see below)…

ARR_WORDS, MUT_ARR_PTRS_CLEAN, MUT_ARR_PTRS_DIRTY, MUT_ARR_PTRS_FROZEN0, MUT_ARR_PTRS_FROZEN

Non-pointer arrays are straightforward:

| Header | Bytes | Array payload |

请确保即使复制垃圾收集器不关心对齐,您关于获得正确对齐的建议也不会被撤消。

来自执行比对的PrimOps.cmm中的GHC源代码:

/* Now we need to move p forward so that the payload is aligned
   to <alignment> bytes. Note that we are assuming that
   <alignment> is a power of 2, which is technically not guaranteed */
p = p + ((-p - SIZEOF_StgArrBytes) & (alignment - 1));

因此,对齐的是有效负载,而不是header。