Should I prefer the default assignment convention for __fastcall when I really don't like the calling convention?

We have a huge C ++ code base with a lot of COM objects. Each function open to COM must have a __stdcall calling convention (usually a STDMETHODCALLTYPE macro), and therefore we have many functions marked with STDMETHODCALLTYPE .

Now I see a function that is not directly called through COM, but rather called only from within our C ++ code, and this function also has a STDMETHODCALLTYPE macro in its signature. I am completely sure that the macro is useless there - there are never any calls through COM to this function.

Should I abandon __stdcall so that it becomes the default call assignment function? How can I make such decisions?

+6
source share
5 answers

The only reason COM material explicitly sets up a calling convention is because it is used across DLL boundaries.
Therefore, I would advise you to refuse to explicitly configure the calling convention and set it in the compiler settings.
Generally:
If functions are exported as DLLs, install a macro that defines the calling convention in the headers. This prevents users from the DLL from using the wrong calling convention when connecting to your DLL. Explicit overrides compiler settings.
Do not use call convection for local functions. The convention can be set by the compiler switch. If you decide to install it explicitly, do it for all functions. Then you still have a central place to change the calling agreement.
Of course, if that makes sense or you need a special calling convention, like fastcall for optimization, you also need to explicitly specify.

+3
source

My approach is to use the default compiler calling convention for internal code and using a well-defined explicitly stated calling convention for any methods that are exported across the module border.

The standard calling convention for most compilers allows registers to be used for performance reasons, so there are advantages to using it when necessary. It also makes your code easier on the eyes, since you do not need to specify an agreement to get the default value.

For exported functions, you explicitly need to specify an agreement. If you create the library that you expect, is called in languages ​​other than C or C ++, it would be conditional to use stdcall. If you expect only C or C ++ clients, then cdecl is probably the most common convention.

+9
source

When Windows switched from __cdecl to __stdcall as the default calling convention, the size of the product dropped by about 10%. This saving was entirely due to the removal of stack adjustments after calling stdcall methods (__cdecl is the “caller setting up the stacks to delete the parameters”), __stdcall is the “caller” that is coordinating the stacks to delete the parameters “since there are more calling than calling switching reduces the size of your binaries).

The disadvantage of using __stdcall is that you do not have #s variables from the expressions (since the caller sets up the stack, they cannot know how many parameters are specified by the caller).

Bottom line: Switching to __stdcall from the default assignment agreement may reduce the size of your binary. This may or may not be important to you.

However, as mentioned above, if your code is EVER accessed by another compiler (for example, if you are delivering a .lib file to someone else), it is absolutely important that you declare the calling convention used.

+5
source

Do you have optimization of the entire program and time code generation is turned on? If this is the case, and you are not exporting the function from your DLL or passing it to pointers, then the compiler can create custom calling conventions for this function or embed them (even if they are not defined in the header file).

+2
source

You can browse your maps to see if they are referenced by looking for the ODL files associated with the solution. If it is not there, it has no interface, and you can change the calling convention. There is a risk that someone else assumes that all functions are configured using this calling convention and that they may add an interface at a later date.

+1
source

Source: https://habr.com/ru/post/887201/


All Articles