Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blittable interop with "fixed" arrays and pointers #47105

Closed
MichalStrehovsky opened this issue Jan 18, 2021 · 4 comments
Closed

Blittable interop with "fixed" arrays and pointers #47105

MichalStrehovsky opened this issue Jan 18, 2021 · 4 comments
Labels
area-Interop-coreclr untriaged New issue has not been triaged by the area owner

Comments

@MichalStrehovsky
Copy link
Member

I was doing interop with a C library that defines data structures along these lines:

typedef struct sg_subimage_content {
    const void* ptr;    /* pointer to subimage data */
    int size;           /* size in bytes of pointed-to subimage data */
} sg_subimage_content;

typedef struct sg_image_content {
    sg_subimage_content subimage[SG_CUBEFACE_NUM][SG_MAX_MIPMAPS];
} sg_image_content;

typedef struct sg_image_desc {
    // <Snip>
    sg_image_content content;
    // <Snip>
}

I wanted to define a blittable equivalent in C#.

The usual trick is to define sg_image_content as StructLayout(Explicit, Size = XXX) and make an indexer that exposes individual elements (similar to how the C# fixed array does it). But the problem is that the individual elements have pointers so the size depends on the architecture.

Failing that, one can just manually unroll the array into SG_CUBEFACE_NUM * SG_MAX_MIPMAPS fields. In my case, that came down to 96 fields.

Just wondering whether people have a feel for how common is this situation and whether we need better primitives to enable such scenarios. (E.g. being able to specify the size as two numbers that would be used as const1 + PTR_SIZE * const2).

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Jan 18, 2021
@EgorBo
Copy link
Member

EgorBo commented Jan 18, 2021

Isn't it better to just wait for dotnet/csharplang#1314
so you will be able to define fixed sg_subimage_content subimage[SG_CUBEFACE_NUM * SG_MAX_MIPMAPS]; ?

@MichalStrehovsky
Copy link
Member Author

Oh neat. Didn't realize C# is considering emitting the bag of fields with a sequential layout instead of the single explicit layout struct with an explicit size. Switching back and forth between Explicit and Sequential layout has impact on calling conventions though so this will have to be done carefully. But it would solve the scenario I hit, albeit with a size impact (all those fields are not free).

@jkotas
Copy link
Member

jkotas commented Jan 18, 2021

We do not want to be emitting hundreds of dummy fields just to inflate the struct to the right size. The plan is to have runtime feature to solve that problem #12320

@MichalStrehovsky
Copy link
Member Author

The plan is to have runtime feature to solve that problem #12320

Missed that. We can close as dupe of that then.

@ghost ghost locked as resolved and limited conversation to collaborators Feb 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Interop-coreclr untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

3 participants