Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I prefer "default" calling convention over __fastcall when I don't really care of the calling convention?

We have a huge C++ codebase with lots of COM objects. Each function exposed to COM must have __stdcall calling convention (usually STDMETHODCALLTYPE macro) and so we have lots of functions marked 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 STDMETHODCALLTYPE macro in its signature. I'm completely sure that macro is useless there - no calls through COM to that function ever happen.

Should I drop the __stdcall so that it becomes a "default" calling convention function? How do I make such decisions?

like image 722
sharptooth Avatar asked May 03 '11 13:05

sharptooth


People also ask

Why do we need a calling convention?

Calling conventions specify how arguments are passed to a function, how return values are passed back out of a function, how the function is called, and how the function manages the stack and its stack frame. In short, the calling convention specifies how a function call in C or C++ is converted into assembly language.

Is Fastcall faster?

Since it typically saves at least four memory accesses, yes it is generally faster.

When should I use Fastcall?

Asking for help, clarification, or responding to other answers. Making statements based on opinion; back them up with references or personal experience.

What is the default calling convention for a compiler in C?

__cdecl is the default calling convention for C and C++ programs. Because the stack is cleaned up by the caller, it can do vararg functions.


1 Answers

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

The default calling convention for most compilers makes good use of registers for performance reasons so there are advantages to using it where appropriate. It also makes your code easier on the eye since you don't need to specify the convention to get the default.

For exported functions you clearly need to specify the convention. If you are making a library that you anticipate will be called from languages other than C or C++ it would be conventional to use stdcall. If you only expect C or C++ clients then cdecl is probably the most common convention.

like image 198
David Heffernan Avatar answered Oct 14 '22 07:10

David Heffernan