Calling conventions on callbacks

Forum for technical discussions regarding development. If you have a general suggestion, problem or comment, please use one of the other forums.

Moderator: OpenTTD Developers

Post Reply
matthijs
Engineer
Engineer
Posts: 76
Joined: 25 Jun 2004 15:20
Location: Enschede, Netherlands
Contact:

Calling conventions on callbacks

Post by matthijs »

Hey,

I was playing around with Celestar's multistop patch, trying to compile it under msvc6. After some issues with variable declaration(*) I encountered an issue with calling conventions for callback functions.
Specifically, the vc project is now set to declare all functions as __fastcall. But, in Celestar's code, qsort is used, which expects it's callback to be of __cdecl convention.
I see two obvious solutions to this:
1. Change the project options to use __cdecl by default (I think this is msvc default). Downside to this would be speed, at least I assume __fastcall is faster?
2. Declare all callbacks with an explicit __cdecl. Since __cdecl is only understood by msvc (at least not by gcc), we would need a macro in the form of:

Code: Select all

#ifdef MSVC
    #define CALLBACK __cdecl
#else
    #define CALLBACK
#endif
(...)
int CALLBACK function(void*, void*) {
    (...)
}
I vote for the last one, which do you think is best?

Matthijs

(*) When you declare variables, you should only do it at the beginning of a scope (ie any {} block). If you declare a variable after a normal statement, msvc will barf.
matthijs
Engineer
Engineer
Posts: 76
Joined: 25 Jun 2004 15:20
Location: Enschede, Netherlands
Contact:

Post by matthijs »

How's this for detecting that MSVC is used? Is there a better way?

Code: Select all

#if defined(WIN32) && !defined(MINGW)
Matthijs
matthijs
Engineer
Engineer
Posts: 76
Joined: 25 Jun 2004 15:20
Location: Enschede, Netherlands
Contact:

Post by matthijs »

I just saw that there is already a macro called CDECL that would do the trick. Probably just use that one then.

Matthijs
msalters
Engineer
Engineer
Posts: 34
Joined: 16 Jun 2004 19:21

Post by msalters »

Too bad we're stuck with C and qsort, C++ std::sort is 6 times faster (due to inlined callbacks). Anyway, the classic trick to detect MSVC is to test _MSC_VER. IIRC, MSVC6 has _MSC_VER==1200, VC7.0 has 1300 and VC7.1 has 1301. I may be off by a few.
Post Reply

Return to “OpenTTD Development”

Who is online

Users browsing this forum: No registered users and 26 guests