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

Generated for Crystal Space by doxygen 1.4.6