Comments (4)
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.
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.
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
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.
I'm confused. Your screenshot includes a SizeOf
method that CsWin32 emits. What more do you want?
from cswin32.
Related Issues (20)
- APPBARDATA should compile on AnyCPU HOT 2
- CsWin32 should avoid Unsafe.SkipInit when it doesn't exist in the compilation HOT 1
- WlanOpenHandle doesn't support SafeHandle HOT 1
- The API "Gdi32" is ambiguous HOT 4
- Add `GET_WHEEL_DELTA_WPARAM` macro
- `TASKDIALOGCONFIG.pfCallback` should use associated enum for `msg` parameter
- Broken `VariableLengthInlineArray<T>` after upgrade from 0.3.49-beta to 0.3.106 HOT 8
- NetFramework - Unsafe.SkipInit(out T value) unavailable HOT 3
- Inconsistent const in parameters of RmRegisterResources HOT 9
- LocalExternFunctions should use `DefaultDllImportSearchPaths`
- Code Quality: Use `WinMdRoot` instead of `winmdroot` to supress CS8981 (type name is ASCII lowercase only) HOT 5
- `PCWSTR` is not marked with `[GeneratedCode]`
- Many Win32 structs (HDC, HMONITOR, etc.) lack the IntPtr conversion operator.
- "Pointers cannot reference marshaled structures. Use ByRef instead" caused by VariableLengthInlineArray
- Latest WDK metadata fails to create DEVPKEY field
- Daily nuget feed not updating HOT 1
- MFTEnumEx signature incorrect
- `VirtualFreeEx` missing HOT 1
- Missing NtCreateThread and NtCreateThreadEx functions HOT 1
- Add an analyzer to catch inadvertent ephemeral delegates HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cswin32.