简体   繁体   中英

Marshalling pointer to array P/Invoke

I am wrapping a shared object library (FFTW) in .NET Core using P/Invoke . FFTW needs to allocate memory potentially aligned on particular boundaries, so I need to use its memory allocation routine. Ideally, I would like to avoid creating a separate chunk of memory in a managed array and copying the data across on every use. Ideally, the array is created to point to the already allocated memory. Is this possible, or should I give up and take the performance hit of a copy?

No, you can't create an array that points to unmanaged memory that has been allocated by an external memory manager. You can, however, create a Span<T> or a Memory<T> that does, which give you a very similar API and means you don't need unsafe at any point afterwards. Note that to be able to store it anywhere, it needs to be a Memory<T> ( Span<T> is a ref struct ) - but a Memory<T> is essentially just an on-demand span provider - when you want the span, call .Span on your Memory<T> .

Now; there isn't a Memory<T> for dealing with raw pointers that ships out of the box, but it is simple to write one. Or you can just use this one I wrote earlier (a MemoryManager<T> is an abstraction that can be used to implement custom Memory<T> instances, which can then provide a Span<T> when needed).

Usage:

int* ptr = ...
int len = ...

var memory = new UnmanagedMemoryManager<int>(ptr, len).Memory;
// (I should probably add a helper method for that!)

which gives you a Memory<int> that you can store in fields etc; then to work with it, you want the span:

var span = _memory.Span;
span[42] = 119; // etc

You can also do things like coerced type conversions over spans, allowing them to do most of the same things pointers can do (ie in the same way that you can force an int* to be a byte* , you can convert a Span<int> into a Span<byte> over the same memory - not a copy).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM