News for hackers. Stuff that matters.
Re:ProjectPSX – A C# coded emulator of the original Playstation by gwbas1c on Monday September 09, 2019 @12:38PM (#20919726)
A rather useful pattern in C# is to load a blob into a byte array, then inside unsafe code, typecast it to a pointer to well-defined struct.

There's also managed ways to do that, but they are slower and involve copying memory.

Re:ProjectPSX – A C# coded emulator of the original Playstation by konamicode on Monday September 09, 2019 @04:20PM (#20922016)
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?
Re:ProjectPSX – A C# coded emulator of the original Playstation by gwbas1c on Monday September 09, 2019 @06:00PM (#20922948)
I 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;

}
Re:ProjectPSX – A C# coded emulator of the original Playstation by moron4hire on Friday September 13, 2019 @10:33AM (#20962514)
I recently learned about MemoryMappedFiles in .NET. 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.