Giter Club home page Giter Club logo

Comments (4)

mitchcapper avatar mitchcapper commented on July 21, 2024 1

Me not to be an idiot, but not sure you can assist with that:) Quietly closing.

Too used to looking at libraries thinking about internal being internal not genned in project so when
itm.Anonymous.SymbolicLinkReparseBuffer.SizeOf didn't work was thinking that was why. Instead I just missed the static so should have been doing
REPARSE_DATA_BUFFER._Anonymous_e__Union._SymbolicLinkReparseBuffer_e__Struct.SizeOf.

So yes sorry for the waste of time.

from cswin32.

AArnott avatar AArnott commented on July 21, 2024

buffer.Clear();

You don't have to clear newly allocated buffers that are allocated on the GC heap unless you specifically call this API or one like it.

Assuming Marshal.SizeOf can't be made to work, a method on the struct that returns its size.

There's no way we can predict at code-gen time what its runtime size will be. We'd just use Marshal.SizeOf<T> in the generated code. So I'm not sure what more we can do than we're already doing. Creating a method that calls an existing public method doesn't sound valuable.
As for variable length arrays, for which some of these structs serve as a header, I'm not sure what more we can do there. The struct itself isn't inclusive of the array that may follow after it, and such structs don't have a well-defined pattern for naming the member that may be present to count the size or length of the array either.

from cswin32.

mitchcapper avatar mitchcapper commented on July 21, 2024

Understood about code-gen time. I think there is value in exposing your own methods even if they are only 3 lines of code. Union structs are already going to be somewhat confusing to people and estimating the wrong amount of memory can lead to bad outcomes. I feel a good number of people looking at
image

won't know how much memory will be alloced by hand. Hopefully they realize they need to Mashal.sizeof (struct) + however many elements they want to alloc * sizeof element. If that struct had multiple variable length arrays at different offsets that would be ever more complicated.

I think if there were helpers so one could do
MyStructType.myVarLengthArray<T>.SizeInBytes(int T Count, bool includeBaseStructSize=true)

for most situations a user could just call that to get the size they have to alloc. If there were multiple arrays that didnt overlap then you are calling SIzeInBytes on each with only the first one including the struct size.

I will give you though its not exactly pretty but probably butter than a user trying on their own.

Having an argument to AsSpan be TotalstructAllocSize would be a good helper. It would allso the variable length array to calculate its offset in the struct and how many bytes the new span request would take and make sure it is still under the total allocation.

I tried to figure out how to write a SafeVarArrayStructHandler class so one could alloc the memory for the user as well (and thus also know its total size) but saw no way of doing it well without code duplication and being type specific, or using Expressions/reflection.

They are small nits but with csWin32 making even some kernel level apis a piece of cake to use having an option safeguard on these arrays may help. Would also be nice if there was a c# attribute that would throw a warning if you ever tried to create an instance of a struct (rather than having a ptr/span acess to it) as clearly any of these var length structs should never be directly declared in c# space.

from cswin32.

AArnott avatar AArnott commented on July 21, 2024

I'm confused. Your screenshot includes a SizeOf method that CsWin32 emits. What more do you want?

from cswin32.

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.