C ++ x86 / x64 structure alignment + .NET AnyCPU Using the C ++ library (calls / callbacks)

Since my previous question was unsuccessful ("C # AnyCPU library using x86 / x64 C API - packaging structures, calls and callbacks"), I will write a more concise and abstract.

PICTURE:

The company I work for has 64 bit porting software. The software consists of a BASE library (C ++ with the C API), two shells over the C API: the C ++ shell and the .NET shell.

The C ++ BASE library and C ++ WRAPPER must have an x86 / x64 build configuration. .NET WRAPPER should only have AnyCPU build configuration.

Choosing the right library from .NET WRAPPER was successfully completed (both C ++ BASE libraries (x86 / x64) are loaded into two separate namespaces and, depending on the size of IntPtr, are called at runtime, it is not necessary that both libraries be there, but only the one to be used).

PROBLEM:

All structures of the BASE library are aligned by 4 bytes:

#pragma pack(push,4) 

In order to compile the code with NO warnings on 64-bit, I added selective alignment (C ++ BASE and C ++ WRAPPER work like charms):

 #ifdef WIN64 #pragma pack(push,8) #else #pragma pack(push,4) #endif 

The problem is with .NET structures:

 [StructLayout(LayoutKind.Sequential, Pack = 4, CharSet = CharSet.Unicode)] internal struct DBConnectionSettings{...} 

This structure cannot have selective packaging, since AnyCPU is the only necessary WRAPPER configuration.

It is very difficult (LOTS AND LOTS OF PEFRORMANCE LOSS) to create separate structures aligned by 8 bytes in a different .NET namespace ... Nevertheless, this may be the only solution that we can fully find.

Return to 4-byte alignment for x86 and x64 issues. MANY warnings, since 100% of the structures are packaging sensitive, especially for unary operators (problems only appear in the x64 solution configuration with 4-byte alignment). Many failures, structures cannot be aligned unless we add dummy members. It should be noted that the structure is grouped by value or is associated with a license / target OS (ifdefs). Adding complementary (fictitious) members to all features will require several months.

Alignment of only 8 bytes is not a solution since we again get glitches in this place - only for x86, x64 works well - alignment of the structure requires again dummy (additions) elements and groupings based on size.

CONCLUSION (QUESTIONS):

1) Is it possible for C ++ code to have alignment 4 for x86 and x64 solution configurations? How can I do that?

2) What is the best alternative to align x86 and x64? The size of the pointer should definitely govern the alignment (in my opinion). We strive to have FAST software that is not necessarily memory efficient (so far, fingerprint memory is somewhere between 15 MB and 2 GB on x86 systems).

3) What special needs do function-bearing structures have in relation to the interaction between .NET and C?

4) Any recommended alternative is much appreciated.

We searched for more than one month on this Internet (the one on our planet), but we did not come anywhere. Some architects beg to use custom packaging (4/8), while some argue that the software is not straightforward and that it needs to be reorganized. It is not my decision what to do, but I can give ideas with good arguments. The software was launched in the late 2000s, when the STL was buggy, so BASE has its own containers optimized for knowledge of God. I should not express my opinion here, since this is not a Christian.

+6
source share
1 answer

Just fix the alignment problem on the C ++ side. There should be no reason for x86 to fail when aligning to 8 bytes, or to fail x64 when aligning to 4 bytes. #pragma pack(push,4) would be a suitable method. You may need a different #pragma pack for the internal SSE code, but this code should not be open .Net AnyCPU.

If you get hundreds of warnings (not counting the primary warnings, of course), then the compiler agrees with my assessment that your code is suspicious.

+1
source

All Articles