Writing complex structure to memory-mapped-file

2019-09-19 03:32发布

I try to write following struct to a memory mapped file, but I still have problem with the array (writing throws exception that the struct can not contain reference)

[StructLayout(LayoutKind.Explicit)]
struct IndexEntry {
    [FieldOffset(0)]
    public byte key;

    [FieldOffset(4)]
    public int lastValueIdx;

    [FieldOffset(8)]
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = Constants.PART_ENTRY_SIZE)]
    public long[] values;
}

I use this calling for writing:

UnmanagedMemoryAccessor.Write<IndexEntry>(0, ref entry);

Can you please tell me, what am I doing wrong? Thx

1条回答
老娘就宠你
2楼-- · 2019-09-19 03:49

The solution of this is using the fixed size array and unsafe code. So the struct should look like this:

[StructLayout(LayoutKind.Explicit)]
unsafe struct IndexEntry {
    [FieldOffset(0)]
    public byte key;

    [FieldOffset(1)]
    public int lastValueIdx;

    [FieldOffset(5)]
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = Constants.PART_ENTRY_SIZE)]
    public fixed long values[Constants.PART_ENTRY_SIZE];
}

Note that the program (or a single project containing that struct) must be compiled with the "/unasfe" option and the array must be then accessed like this:

fixed(long* arr = this.values) {
    // Setting value
    arr[index] = value;
}
unsafe {
    // Getting value
    x = obj.values[index];
}

Then the UnmanagedMemoryAccessor.Write<T>(...) and UnmanagedMemoryAccessor.Read<T>(...) functions work perfectly.

查看更多
登录 后发表回答