5 ms·
Any chance you have an example of this pattern you can point to? Also, would the use of Span<T> make it possible to do this without unsafe code?
by konamicode 7y ago
Any chance you have an example of this pattern you can point to? Also, would the use of Span<T> make it possible to do this without unsafe code?
- gwbas1c 7y agoI use it with DeviceIOControl for interoperating with a driver. I didn't learn by following someone's example, in my case, I have to work with so many different structs in a high performance use case that I took the time to re-learn pointers. (Haven't used them in years.) 1: Memory is allocated using new byte[bufferSize] 2: That byte[] is pinned to a byte* or void* via fixed 3: The pointer is passed to DeviceIOControl 4: Traditional c-style typecasting inside of fixed 5: Traditional GC cleans up your byte[]. This avoids problems with managed programs that allocate lots of native memory. I don't know how to use pointers with Span<T>... BUT: I do similar things with ArraySegment<byte[]>. All that's needed is pointer arithmetic: ArraySegment<byte> arraySegment = ... fixed (byte* dataArrayPtr = arraySegment.Array) { var dataPtr = dataArrayPtr + arraySegment.Offset; var ptr = (SomeStruct*)dataPtr; }
- moron4hire 7y agoI recently learned about MemoryMappedFiles in .NET. https://docs.microsoft.com/en-us/dotnet/standard/io/memory-mapped-files https://docs.microsoft.com/en-us/dotnet/standard/io/memory-m... I only mention it because I'm not entirely clear on what is going on or how it works (largely because I tend to only be able to learn things by doing them, which is my own issue, not your failure to describe it), but after reading the docs on MemoryMappedFiles, I wonder if it's something that could be useful to you.