stuw
stuw
CC#
Created by stuw on 1/8/2024 in #help
Parallel.For but that doesn't allocate.
No description
4 replies
CC#
Created by stuw on 5/25/2023 in #help
✅ Marshal.SizeOf is not what I'm thinking
[StructLayout(LayoutKind.Explicit)]
internal unsafe struct ChunkData
{
public const int CELL_SIZE = 33;
public const int CELL_LENGTH = CELL_SIZE * CELL_SIZE * CELL_SIZE;
public const int CELL_BIT_LENGTH = (CELL_SIZE * CELL_SIZE * CELL_SIZE) / 8 + 1;

public const int SIZE = CELL_LENGTH + CELL_BIT_LENGTH;

[FieldOffset(0)]
public fixed byte volumeData[CELL_LENGTH];

[FieldOffset(CELL_LENGTH)]
public fixed byte maxCellCache[CELL_BIT_LENGTH];
}

[StructLayout(LayoutKind.Explicit)]
internal unsafe struct ChunkDataBackupWrapper
{
public const int SIZE = ChunkData.SIZE + 1;

[FieldOffset(0)]
public bool hasBeenBacked;

[FieldOffset(1)]
public ChunkData chunkData;
}
[StructLayout(LayoutKind.Explicit)]
internal unsafe struct ChunkData
{
public const int CELL_SIZE = 33;
public const int CELL_LENGTH = CELL_SIZE * CELL_SIZE * CELL_SIZE;
public const int CELL_BIT_LENGTH = (CELL_SIZE * CELL_SIZE * CELL_SIZE) / 8 + 1;

public const int SIZE = CELL_LENGTH + CELL_BIT_LENGTH;

[FieldOffset(0)]
public fixed byte volumeData[CELL_LENGTH];

[FieldOffset(CELL_LENGTH)]
public fixed byte maxCellCache[CELL_BIT_LENGTH];
}

[StructLayout(LayoutKind.Explicit)]
internal unsafe struct ChunkDataBackupWrapper
{
public const int SIZE = ChunkData.SIZE + 1;

[FieldOffset(0)]
public bool hasBeenBacked;

[FieldOffset(1)]
public ChunkData chunkData;
}
ChunkDataBackupWrapper.SIZE is one byte smaller than Marshal.SizeOf() ChunkData: 40430 Marshal.SizeOf, 40430 SIZE ChunkDataBackupWrapper: 40432 Marshal.SizeOf, 40432 SIZE I'm trying to read https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.structlayoutattribute.pack?redirectedfrom=MSDN&view=net-7.0 but there's a lot of stuff that I can't wrap my head around. "The alignment of the type is the size of its largest element (1, 2, 4, 8, etc., bytes) or the specified packing size, whichever is smaller." "Byte fields align on 1-byte boundaries, Int16 fields align on 2-byte boundaries, and Int32 fields align on 4-byte boundaries." 1. I'm using bytes/bool everywhere, why didn't the second struct align to 1s? 2. Also is "volumeData" considered as an element of size "1" or size "35937"? 3. What's the rules for the size of the struct? I'm not sure I understand
9 replies