Giter Club home page Giter Club logo

Comments (7)

jmelosegui avatar jmelosegui commented on July 19, 2024

Well, in this case why not having only byte[]?
Is there any reason to actually need the IntPtr and byte* overloads?

I thought we introduced pointers to have more meaningful parameters, and code generation to solve the gap for those languages that cannot call pointer exposing methods, but on those scenarios where we can use an array I don't see the need of the pointer at all (since the parameter is meaningful enough and it can be used by all languages)

from pinvoke.

AArnott avatar AArnott commented on July 19, 2024

@jmelosegui: that's a fair question.
There are two reasons for using byte* instead of byte[]. These reasons aren't usually compelling enough to warrant using byte* so most methods that can should probably use byte[]. But for a few methods or libraries, they are quite compelling. They are:

  1. byte* can point anywhere, including (for example) the middle of a byte[]. So if you already have an array with the content you want to pass in, but that content isn't at the start of the array, byte* lets you point directly at it, whereas byte[] requires that you copy the whole content into a new array just so it's at the beginning of it.
  2. byte* enables you to be certain that the memory doesn't get copied or moved around by the CLR. byte[] as a reference type can be moved during a GC's defragmentation of memory. If your data is sensitive (say, as part of a cryptographic operation) best practices suggest you scrub the buffer when you're done so that memory dumps don't capture the data. But if the buffer gets copied around in memory by the GC, you can't effectively scrub all the old locations. Locking the buffer location mitigates this, and a very convenient way to do this (if it's all local variables within a method) is using C# fixed with pointers and not messing with multiple levels of try/finally and GC pinning a byte[].

So I expect to use this enhancement primarily in the BCrypt and NCrypt libraries (where I'm already writing these overloads by hand) and probably nowhere else.

from pinvoke.

vbfox avatar vbfox commented on July 19, 2024

I'm also using pointer inside arrays (Wrapped in an ArraySegment) for IO access to USB devices without allocating memory for each call.

It's not necessary for most API but if the codegen can do them automatically, why not have them ?

from pinvoke.

jmelosegui avatar jmelosegui commented on July 19, 2024

Thank you @AArnott and @vbfox for pointing out those use cases. Now I get the reasons behind this issue.

from pinvoke.

AArnott avatar AArnott commented on July 19, 2024

I've edited the proposal to include the addition of the MarshalAsAttribute in the generated code.

from pinvoke.

AArnott avatar AArnott commented on July 19, 2024

I've removed [MarshalAs], since the generated methods are never P/Invoke methods themselves so it's not applicable.
Also: I've added a 3rd overload for folks who want to pass in byte[] and native pointers for other parameters. I discovered the need for this combination during the migration to the code generated second overload -- a lot of my own extra overloads had this combination.

from pinvoke.

jmelosegui avatar jmelosegui commented on July 19, 2024

Also: I've added a 3rd overload for folks who want to pass in byte[] and native pointers for other parameters.

That makes a lot sense.

from pinvoke.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.