CrystalSpace

Public API Reference

csutil/win32/csosdefs.h

00001 /*
00002     Copyright (C) 1998 by Jorrit Tyberghein
00003     Written by Andrew Zabolotny <bit@eltech.ru>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public
00016     License along with this library; if not, write to the Free
00017     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00020 #ifndef __CS_CSOSDEFS_H__
00021 #define __CS_CSOSDEFS_H__
00022 
00023 #define CS_EXPORT_SYM_DLL       __declspec(dllexport)
00024 #define CS_IMPORT_SYM_DLL       __declspec(dllimport)
00025 
00026 #ifdef CS_BUILD_SHARED_LIBS
00027   #define CS_EXPORT_SYM CS_EXPORT_SYM_DLL
00028   #define CS_IMPORT_SYM CS_IMPORT_SYM_DLL
00029 #else
00030   #define CS_EXPORT_SYM
00031   #define CS_IMPORT_SYM
00032 #endif // CS_BUILD_SHARED_LIBS
00033 
00034 #if defined(CS_COMPILER_MSVC)
00035   //#pragma warning(disable:4097)   // use of xxx as synonym for a classname
00036   //#pragma warning(disable:4099)   // type seen as both 'struct' and `class'
00037   //#pragma warning(disable:4100)   // Use of void* as a formal function parameter
00038   //#pragma warning(disable:4102)   // 'label' : unreferenced label
00039   //#pragma warning(disable:4146)   /* unary minus operator applied to unsigned 
00040   //                                 * type, result still unsigned */
00041   //#pragma warning(disable:4201)   // VC6: structure/ union without name.
00042   #pragma warning(disable:4244)   // conversion from 'double' to 'float'
00043   #pragma warning(disable:4250)   // '...' inherits '..' via dominance
00044   //#pragma warning(disable:4251)   /* class needs to have dll-interface to be 
00045   //                               * used by clients */
00046   //#pragma warning(disable:4275)   /* non-DLL-interface used as base for
00047   //                                 * DLL-interface */
00048   #pragma warning(disable:4290)   // C++ exception specification ignored
00049   //#pragma warning(disable:4291)   // no matching operator delete found
00050   #pragma warning(disable:4312)   /* 'variable' : conversion from 'type' to 
00051                                    * 'type' of greater size */
00052   #pragma warning(disable:4345)   /* VC7.1: an object of POD type constructed 
00053                                    * with an initializer of the form () will 
00054                                    * be default-initialized */
00055   #pragma warning(disable:4355)   // 'this' used in base member initializer list
00056   //#pragma warning(disable:4390)   // Empty control statement
00057   //#pragma warning(disable:4505)   /* 'function' : unreferenced local function 
00058   //                                 * has been removed */
00059   //#pragma warning(disable:4611)   /* interaction between _setjmp and C++ 
00060   //                                 * destructors not portable */
00061   //#pragma warning(disable:4702)   // Unreachable Code
00062   //#pragma warning(disable:4706)   // Assignment in conditional expression
00063   //#pragma warning(disable:4710)   // function not inlined
00064 //  #pragma warning(disable:4711)   /* function 'function' selected for inline 
00065 //                                 * expansion */
00066 //  #pragma warning(disable:4786)   /* VC6: identifier was truncated to '255'
00067 //                                 * characters in the browser information */
00068 //  #pragma warning(disable:4800)   // Forcing value to bool
00069   //#pragma warning(disable:4805)   // unsafe mix of bool and int.
00070 
00071 //  #pragma warning(default:4265)   /* class has virtual functions, but 
00072 //                                 * destructor is not virtual */
00073 
00074   #pragma inline_depth (255)
00075   #pragma inline_recursion (on)
00076   #pragma auto_inline (on)
00077   
00078   #define CS_FORCEINLINE __forceinline
00079 
00080   #pragma intrinsic (memset, memcpy, memcmp)
00081   #pragma intrinsic (strcpy, strcmp, strlen, strcat)
00082   #pragma intrinsic (abs, fabs)
00083   #pragma intrinsic (_byteswap_ushort, _byteswap_ulong, _byteswap_uint64)
00084   
00085   #if _MSC_VER >= 1400
00086     #include <intrin.h>
00087   #else
00088     extern "C" long _InterlockedCompareExchange (long volatile *, long, long);
00089     extern "C" long _InterlockedDecrement (long volatile *);
00090     extern "C" long _InterlockedExchange (long volatile *, long);
00091     extern "C" long _InterlockedIncrement (long volatile *);
00092   #endif
00093   #pragma intrinsic (_InterlockedCompareExchange)
00094   #pragma intrinsic (_InterlockedDecrement)
00095   #pragma intrinsic (_InterlockedExchange)
00096   #pragma intrinsic (_InterlockedIncrement)
00097 
00098   #if defined(__CRYSTAL_SPACE__) && !defined(CS_DEBUG)
00099     #pragma code_seg("CSpace")    // Just for fun :)
00100     // However, doing this in debug builds prevents Edit & Continue from
00101     // functioning properly :/
00102   #endif
00103 
00104   // VC8 quirks
00105   #if (_MSC_VER >= 1400)
00106     // Also note quirk in csconfig.h
00107 
00108     // Nothing else atm.
00109   #endif
00110 #endif
00111 
00112 #ifndef WINVER
00113 #define WINVER 0x0400
00114 #endif
00115 
00116 // So many things require this. IF you have an issue with something defined
00117 // in it then undef that def here.
00118 
00119 #if defined(CS_COMPILER_GCC)
00120 
00121 // From the w32api header files:
00122 
00123 #if defined(__i686__) && !defined(_M_IX86)
00124 #define _M_IX86 600
00125 #elif defined(__i586__) && !defined(_M_IX86)
00126 #define _M_IX86 500
00127 #elif defined(__i486__) && !defined(_M_IX86)
00128 #define _M_IX86 400
00129 #elif defined(__i386__) && !defined(_M_IX86)
00130 #define _M_IX86 300
00131 #endif
00132 #if defined(_M_IX86) && !defined(_X86_)
00133 #define _X86_
00134 #endif
00135 
00136 #ifdef __GNUC__
00137 #ifndef NONAMELESSUNION
00138 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95) 
00139 #define _ANONYMOUS_UNION __extension__
00140 #define _ANONYMOUS_STRUCT __extension__
00141 #else
00142 #if defined(__cplusplus)
00143 #define _ANONYMOUS_UNION __extension__
00144 #endif /* __cplusplus */
00145 #endif /* __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95) */
00146 #endif /* NONAMELESSUNION */
00147 #endif /* __GNUC__ */
00148 
00149 #ifndef _ANONYMOUS_UNION
00150 #define _ANONYMOUS_UNION
00151 #define _UNION_NAME(x) x
00152 #define DUMMYUNIONNAME  u
00153 #define DUMMYUNIONNAME2 u2
00154 #define DUMMYUNIONNAME3 u3
00155 #define DUMMYUNIONNAME4 u4
00156 #define DUMMYUNIONNAME5 u5
00157 #define DUMMYUNIONNAME6 u6
00158 #define DUMMYUNIONNAME7 u7
00159 #define DUMMYUNIONNAME8 u8
00160 #else
00161 #define _UNION_NAME(x)
00162 #define DUMMYUNIONNAME
00163 #define DUMMYUNIONNAME2
00164 #define DUMMYUNIONNAME3
00165 #define DUMMYUNIONNAME4
00166 #define DUMMYUNIONNAME5
00167 #define DUMMYUNIONNAME6
00168 #define DUMMYUNIONNAME7
00169 #define DUMMYUNIONNAME8
00170 #endif
00171 #ifndef _ANONYMOUS_STRUCT
00172 #define _ANONYMOUS_STRUCT
00173 #define _STRUCT_NAME(x) x
00174 #define DUMMYSTRUCTNAME s
00175 #define DUMMYSTRUCTNAME2 s2
00176 #define DUMMYSTRUCTNAME3 s3
00177 #else
00178 #define _STRUCT_NAME(x)
00179 #define DUMMYSTRUCTNAME
00180 #define DUMMYSTRUCTNAME2
00181 #define DUMMYSTRUCTNAME3
00182 #endif
00183 
00184 #else
00185 
00186 #if !defined(_X86_) && !defined(_IA64_) && !defined(_AMD64_) && \
00187      defined(_M_IX86)
00188 #define _X86_
00189 #endif
00190 
00191 #if !defined(_X86_) && !defined(_IA64_) && !defined(_AMD64_) && \
00192      defined(_M_AMD64)
00193 #define _AMD64_
00194 #endif
00195 
00196 #if !defined(_X86_) && !defined(_M_IX86) && !defined(_AMD64_) && \
00197      defined(_M_IA64) && !defined(_IA64_)
00198 #define _IA64_
00199 #endif
00200 
00201 #endif
00202 
00203 #ifndef __CYGWIN32__
00204 #include <excpt.h>
00205 #endif
00206 #include <stdarg.h>
00207 #include <windef.h>
00208 #include <winbase.h>
00209 #include <malloc.h>
00210 #include <sys/types.h>
00211 #include <sys/stat.h>
00212 #ifdef CS_HAVE_SYS_PARAM_H
00213 #include <sys/param.h>
00214 #endif
00215 #ifndef __CYGWIN32__
00216 #include <direct.h>
00217 #endif
00218 
00219 
00220 #ifndef WINGDIAPI
00221 #define WINGDIAPI DECLSPEC_IMPORT
00222 #endif
00223 
00224 /*
00225   LONG_PTR is used in the Win32 canvases, but it's only defined in newer 
00226   Platform or DirectX SDKs (in BaseTsd.h).
00227   Ergo, on older SDKs, we have to define it ourselves. One indicator for the
00228   presence of LONG_PTR seems to be if the __int3264 macro is #defined.
00229   So, if it's not, we define LONG_PTR.
00230  */
00231 #ifndef __int3264
00232   typedef LONG LONG_PTR;
00233   typedef ULONG ULONG_PTR;
00234   typedef DWORD DWORD_PTR;
00235 #endif
00236 
00237 #if defined(_DEBUG) || defined(CS_DEBUG)
00238   #include <assert.h>
00239   #ifndef CS_DEBUG
00240     #define CS_DEBUG
00241   #endif
00242 
00243   #if defined(CS_COMPILER_MSVC) 
00244     #include <crtdbg.h>
00245 
00246     #if defined(CS_EXTENSIVE_MEMDEBUG)
00247       #define malloc(size) \
00248         _malloc_dbg ((size), _NORMAL_BLOCK, __FILE__, __LINE__)
00249       #define free(ptr) _free_dbg ((ptr), _NORMAL_BLOCK)
00250       #define realloc(ptr, size) \
00251         _realloc_dbg ((ptr), (size), _NORMAL_BLOCK, __FILE__, __LINE__)
00252       #define calloc(num, size) \
00253         _calloc_dbg ((num), (size), _NORMAL_BLOCK, __FILE__, __LINE__)
00254 
00255       // heap consistency check is on by default, leave it
00256       #define CS_WIN32_MSVC_DEBUG_GOOP \
00257         _CrtSetDbgFlag ( \
00258           _CrtSetDbgFlag (_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF)
00259     #else
00260       // turn heap consistency check off
00261       #define CS_WIN32_MSVC_DEBUG_GOOP \
00262         _CrtSetDbgFlag ( \
00263           (_CrtSetDbgFlag (_CRTDBG_REPORT_FLAG) & ~_CRTDBG_ALLOC_MEM_DF) | \
00264           _CRTDBG_LEAK_CHECK_DF)
00265     #endif
00266   #endif
00267 
00268 #endif
00269 
00270 #ifdef CS_WIN32_MSVC_DEBUG_GOOP
00271   #define CS_INITIALIZE_PLATFORM_APPLICATION CS_WIN32_MSVC_DEBUG_GOOP
00272 #endif
00273 
00274 // The 2D graphics driver used by software renderer on this platform
00275 #define CS_SOFTWARE_2D_DRIVER "crystalspace.graphics2d.directdraw"
00276 #define CS_OPENGL_2D_DRIVER "crystalspace.graphics2d.glwin32"
00277 
00278 // The sound driver
00279 #define CS_SOUND_DRIVER "crystalspace.sound.driver.waveout"
00280 #define CS_SNDSYS_DRIVER "crystalspace.sndsys.software.driver.directsound"
00281 
00282 // SCF symbol export facility.
00283 #ifndef CS_STATIC_LINKED
00284 // No need to export the symbols when statically linking into one big binary.
00285 # undef CS_EXPORTED_FUNCTION
00286 # define CS_EXPORTED_FUNCTION extern "C" __declspec(dllexport)
00287 #endif
00288 
00289 #if defined (CS_COMPILER_BCC)
00290 #  define strcasecmp stricmp
00291 #  define strncasecmp strnicmp
00292 #endif
00293 
00294 #if defined (CS_COMPILER_MSVC)
00295 #  define strcasecmp _stricmp
00296 #  define strncasecmp _strnicmp
00297 #endif
00298 
00299 #if defined (CS_COMPILER_MSVC)
00300 #  if defined(_MSC_VER) && (_MSC_VER < 1300)
00301 #    include <assert.h>
00302 static inline longlong strtoll(char const* s, char** sN, int base)
00303 {
00304   assert(sN == 0);
00305   assert(base == 10);
00306   return _atoi64(s);
00307 }
00308 #  else
00309 #   define strtoll _strtoi64
00310 #  endif
00311 #endif
00312 
00313 // Maximal path length
00314 #ifndef CS_MAXPATHLEN
00315 #  ifdef _MAX_FNAME
00316 #    define CS_MAXPATHLEN _MAX_FNAME
00317 #  else
00318 #    define CS_MAXPATHLEN 260 /* not 256 */
00319 #  endif
00320 #endif
00321 #define CS_PATH_DELIMITER ';'
00322 #define CS_PATH_SEPARATOR '\\'
00323 
00324 #if defined (__CYGWIN32__)
00325 #  define CS_MKDIR(path) mkdir(path, 0755)
00326 #else
00327 #  define CS_MKDIR(path) _mkdir(path)
00328 #endif
00329 
00330 // Directory read functions, file access, etc.
00331 #include <io.h>
00332 #ifndef F_OK
00333 #  define F_OK 0
00334 #endif
00335 #ifndef R_OK
00336 #  define R_OK 2
00337 #endif
00338 #ifndef W_OK
00339 #  define W_OK 4
00340 #endif
00341 
00342 #define CS_PROVIDES_EXPAND_PATH 1
00343 inline void csPlatformExpandPath(const char* /*path*/, char* /*buffer*/,
00344   int /*nbuf*/) {}
00345 
00346 // Although CS_COMPILER_GCC has opendir(), readdir(), etc., we prefer the CS
00347 // versions of these functions.
00348 #define CS_WIN32_USE_CUSTOM_OPENDIR
00349 
00350 #ifndef CS_WIN32_USE_CUSTOM_OPENDIR
00351 # include <dirent.h>
00352 #else
00353 struct dirent
00354 {
00355   char d_name [CS_MAXPATHLEN + 1]; // File name, 0 terminated
00356   size_t d_size; // File size (bytes)
00357   long dwFileAttributes; // File attributes (Windows-specific)
00358 };
00359 
00360 struct DIR;
00361 # ifdef CS_CRYSTALSPACE_LIB
00362   extern "C" CS_EXPORT_SYM DIR *opendir (const char *name);
00363   extern "C" CS_EXPORT_SYM dirent *readdir (DIR *dirp);
00364   extern "C" CS_EXPORT_SYM int closedir (DIR *dirp);
00365   extern "C" CS_EXPORT_SYM bool isdir (const char *path, dirent *de);
00366 # else
00367   extern "C" CS_IMPORT_SYM DIR *opendir (const char *name);
00368   extern "C" CS_IMPORT_SYM dirent *readdir (DIR *dirp);
00369   extern "C" CS_IMPORT_SYM int closedir (DIR *dirp);
00370   extern "C" CS_IMPORT_SYM bool isdir (const char *path, dirent *de);
00371 # endif // CS_BUILD_SHARED_LIBS
00372 #endif
00373 
00374 #if defined (CS_COMPILER_BCC) || defined (__CYGWIN32__)
00375 #  define GETPID() getpid()
00376 #else
00377 #  define GETPID() _getpid()
00378 #endif
00379 
00380 #ifdef __CYGWIN32__
00381 #  include <unistd.h>
00382 #  define CS_TEMP_FILE "cs%lu.tmp", (unsigned long)getpid()
00383 #  define CS_TEMP_DIR  "/tmp"
00384 #else
00385 #  include <process.h>
00386 #  define CS_TEMP_FILE "%x.cs", (int)GETPID()
00387 #  define CS_TEMP_DIR win32_tempdir()
00388    // This is the function called by CS_TEMP_DIR macro
00389    static inline char *win32_tempdir()
00390    {
00391      char *tmp;
00392      if ((tmp = getenv ("TMP")) != 0)
00393        return tmp;
00394      if ((tmp = getenv ("TEMP")) != 0)
00395        return tmp;
00396      return "";
00397    }
00398 #endif
00399 
00400 // Microsoft Visual C++ compiler includes a very in-efficient 'memcpy'.
00401 // This also replaces the older 'better_memcpy', which was also not as
00402 // efficient as it could be ergo... heres a better solution.
00403 #if defined(CS_COMPILER_MSVC) && (_MSC_VER < 1300)
00404 #include <memory.h>
00405 #define memcpy fast_mem_copy
00406 static inline void* fast_mem_copy (void *dest, const void *src, int count)
00407 {
00408     __asm
00409     {
00410       mov               eax, count
00411       mov               esi, src
00412       mov               edi, dest
00413       xor               ecx, ecx
00414 
00415       // Check for 'short' moves
00416       cmp               eax, 16
00417       jl                do_short
00418                 
00419       // Move enough bytes to align 'dest'
00420       sub               ecx, edi
00421       and               ecx, 3
00422       je                skip
00423       sub               eax, ecx
00424       rep               movsb
00425 
00426       skip:
00427         mov             ecx, eax
00428         and             eax, 3
00429         shr             ecx, 2
00430         rep             movsd
00431         test    eax, eax
00432         je              end
00433 
00434       do_short:
00435         mov             ecx, eax
00436         rep             movsb
00437 
00438       end:
00439     }
00440 
00441     return dest;
00442 }
00443 #endif
00444 
00445 #ifdef CS_COMPILER_BCC
00446 // Major hack due to pow failures in CS for Borland, removing this
00447 // causes millions of strings to print out -- Brandon Ehle
00448 #define pow(arga, argb) ( (!arga && !argb)?0:pow(arga, argb) )
00449 // Dunno why this is in CS -- Brandon Ehle
00450 #define DEBUG_BREAK
00451 #endif
00452 
00453 #if defined (CS_PROCESSOR_X86)
00454 #  define CS_LITTLE_ENDIAN
00455 #else
00456 #  error "Please define a suitable CS_XXX_ENDIAN macro in win32/csosdefs.h!"
00457 #endif
00458 
00459 #if defined(CS_COMPILER_BCC)
00460   // The Borland C++ compiler does not accept a 'main' routine
00461   // in a program which already contains WinMain. This is a work-around.
00462   #undef main
00463   #define main csMain
00464 #endif
00465 
00466 // cygwin has no _beginthread and _endthread functions
00467 #ifdef __CYGWIN32__
00468 #ifndef _beginthread
00469 #define _beginthread(func, stack, ptr)  CreateThread (0, 0, \
00470           LPTHREAD_START_ROUTINE(func), ptr, CREATE_SUSPENDED, 0)
00471 #endif
00472 #ifndef _endthread
00473 #define _endthread()  {}
00474 #endif
00475 #endif
00476 
00477 // Fake up setenv(), if necessary
00478 #ifndef CS_HAVE_SETENV
00479   #ifdef CS_CRYSTALSPACE_LIB
00480     CS_EXPORT_SYM int setenv (const char* name, const char* value, 
00481       bool overwrite);
00482   #else
00483     CS_IMPORT_SYM int setenv (const char* name, const char* value, 
00484       bool overwrite);
00485   #endif
00486 #endif
00487 
00488 // just to avoid windows.h inclusion
00489 #define csSW_SHOWNORMAL 1
00490 
00491 #if defined(CS_COMPILER_GCC) && defined(__STRICT_ANSI__)
00492 // Need those...
00493   extern int            _argc;
00494   extern char** _argv;
00495   #define CS_WIN32_ARGC _argc
00496   #define CS_WIN32_ARGV _argv
00497 #elif defined(CS_COMPILER_BCC) 
00498   #define CS_WIN32_ARGC _argc
00499   #define CS_WIN32_ARGV _argv
00500 #else
00501   #define CS_WIN32_ARGC __argc
00502   #define CS_WIN32_ARGV __argv
00503 #endif
00504 
00505 
00506 #ifdef __CYGWIN32__
00507 #if !defined(CS_IMPLEMENT_PLATFORM_APPLICATION)
00508 #define CS_IMPLEMENT_PLATFORM_APPLICATION
00509 #endif
00510 
00511 #else // __CYGWIN32__
00512 
00513 /*
00514  if the EXE is compiled as a GUI app,
00515  a WinMain is needed. But if compiled
00516  as a console app it's not used but main() is
00517  instead. 
00518  */
00519 
00520 #if !defined(CS_IMPLEMENT_PLATFORM_APPLICATION)
00521 #ifndef __STRICT_ANSI__
00522   #define csMain main
00523 #else
00524   /* Work around "error: ISO C++ forbids taking address of function `::main'"
00525    * when compiling -ansi -pedantic */
00526   #define csMain mainWithAnotherNameBecauseISOCPPForbidsIt
00527 #endif
00528 #define CS_IMPLEMENT_PLATFORM_APPLICATION                              \
00529 int csMain (int argc, char* argv[]);                            \
00530 int WINAPI WinMain (HINSTANCE hApp, HINSTANCE prev, LPSTR cmd, int show)\
00531 {                                                                      \
00532   (void)hApp;                                                          \
00533   (void)show;                                                          \
00534   (void)prev;                                                          \
00535   (void)cmd;                                                           \
00536   int ret = csMain (CS_WIN32_ARGC, CS_WIN32_ARGV);                     \
00537   return ret;                                                          \
00538 }
00539 #ifdef __STRICT_ANSI__
00540   #define main mainWithAnotherNameBecauseISOCPPForbidsIt
00541 #endif
00542 #endif // CS_IMPLEMENT_PLATFORM_APPLICATION
00543 
00544 #endif // __CYGWIN32__
00545 
00546 #if !defined(CS_STATIC_LINKED)
00547 
00548 #if !defined(CS_IMPLEMENT_PLATFORM_PLUGIN)
00549 #define CS_IMPLEMENT_PLATFORM_PLUGIN                                   \
00550 int _cs_main(int /*argc*/, char* /*argv*/[])                           \
00551 {                                                                      \
00552          return 0;                                                     \
00553 }                                                                      \
00554 extern "C" BOOL WINAPI                                                 \
00555 DllMain (HINSTANCE /*hinstDLL*/, DWORD /*fdwReason*/, LPVOID /*lpvReserved*/) \
00556 {                                                                      \
00557           return TRUE;                                                 \
00558 }                                                                      \
00559 CS_EXPORTED_FUNCTION const char* plugin_compiler()                     \
00560 {                                                                      \
00561          return CS_COMPILER_NAME;                                      \
00562 }
00563 #endif // CS_IMPLEMENT_PLATFORM_PLUGIN
00564 
00565 #endif // CS_STATIC_LINKED
00566 
00567 #include "sanity.inc"
00568 
00569 #endif // __CS_CSOSDEFS_H__

Generated for Crystal Space by doxygen 1.4.7