include/SDL_stdinc.h
author Sam Lantinga <slouken@libsdl.org>
Fri, 17 Feb 2006 08:43:23 +0000
changeset 1367 e440d5c488c1
parent 1361 19418e4422cb
child 1368 533567cbb576
permissions -rw-r--r--
Fixes for BeOS and Solaris builds
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 /* This is a general header that includes C language support */
    24 
    25 #ifndef _SDL_stdinc_h
    26 #define _SDL_stdinc_h
    27 
    28 #include "SDL_config.h"
    29 
    30 /* AIX requires this to be the first thing in the file.  */
    31 #ifdef __GNUC__
    32 # define alloca __builtin_alloca
    33 #else
    34 # ifdef _MSC_VER
    35 #  include <malloc.h>
    36 #  define alloca _alloca
    37 # else
    38 #  if HAVE_ALLOCA_H
    39 #   include <alloca.h>
    40 #  else
    41 #   ifdef _AIX
    42  #pragma alloca
    43 #   else
    44 #    ifndef alloca /* predefined by HP cc +Olibcalls */
    45 char *alloca ();
    46 #    endif
    47 #   endif
    48 #  endif
    49 # endif
    50 #endif
    51 
    52 #if HAVE_SYS_TYPES_H
    53 #include <sys/types.h>
    54 #endif
    55 #if HAVE_STDIO_H
    56 #include <stdio.h>
    57 #endif
    58 #if STDC_HEADERS
    59 # include <stdlib.h>
    60 # include <stddef.h>
    61 # include <stdarg.h>
    62 #else
    63 # if HAVE_STDLIB_H
    64 # include <stdlib.h>
    65 # elif HAVE_MALLOC_H
    66 # include <malloc.h>
    67 # endif
    68 # if HAVE_STDARG_H
    69 # include <stdarg.h>
    70 # endif
    71 #endif
    72 #if HAVE_STRING_H
    73 # if !STDC_HEADERS && HAVE_MEMORY_H
    74 # include <memory.h>
    75 # endif
    76 # include <string.h>
    77 #endif
    78 #if HAVE_STRINGS_H
    79 # include <strings.h>
    80 #endif
    81 #if HAVE_INTTYPES_H
    82 # include <inttypes.h>
    83 #else
    84 # if HAVE_STDINT_H
    85 # include <stdint.h>
    86 # endif
    87 #endif
    88 #if HAVE_CTYPE_H
    89 # include <ctype.h>
    90 #endif
    91 
    92 /* The number of elements in an array */
    93 #define SDL_arraysize(array)	(sizeof(array)/sizeof(array[0]))
    94 #define SDL_TABLESIZE(table)	SDL_arraysize(table)
    95 
    96 /* Basic data types */
    97 typedef enum SDL_bool {
    98 	SDL_FALSE = 0,
    99 	SDL_TRUE  = 1
   100 } SDL_bool;
   101 
   102 typedef int8_t		Sint8;
   103 typedef uint8_t		Uint8;
   104 typedef int16_t		Sint16;
   105 typedef uint16_t	Uint16;
   106 typedef int32_t		Sint32;
   107 typedef uint32_t	Uint32;
   108 
   109 #ifdef SDL_HAS_64BIT_TYPE
   110 typedef int64_t		Sint64;
   111 typedef uint64_t	Uint64;
   112 #else
   113 /* This is really just a hack to prevent the compiler from complaining */
   114 typedef struct {
   115 	Uint32 hi;
   116 	Uint32 lo;
   117 } Uint64, Sint64;
   118 #endif
   119 
   120 /* Make sure the types really have the right sizes */
   121 #define SDL_COMPILE_TIME_ASSERT(name, x)               \
   122        typedef int SDL_dummy_ ## name[(x) * 2 - 1]
   123 
   124 SDL_COMPILE_TIME_ASSERT(uint8, sizeof(Uint8) == 1);
   125 SDL_COMPILE_TIME_ASSERT(sint8, sizeof(Sint8) == 1);
   126 SDL_COMPILE_TIME_ASSERT(uint16, sizeof(Uint16) == 2);
   127 SDL_COMPILE_TIME_ASSERT(sint16, sizeof(Sint16) == 2);
   128 SDL_COMPILE_TIME_ASSERT(uint32, sizeof(Uint32) == 4);
   129 SDL_COMPILE_TIME_ASSERT(sint32, sizeof(Sint32) == 4);
   130 SDL_COMPILE_TIME_ASSERT(uint64, sizeof(Uint64) == 8);
   131 SDL_COMPILE_TIME_ASSERT(sint64, sizeof(Sint64) == 8);
   132 
   133 /* Check to make sure enums are the size of ints, for structure packing.
   134    For both Watcom C/C++ and Borland C/C++ the compiler option that makes
   135    enums having the size of an int must be enabled.
   136    This is "-b" for Borland C/C++ and "-ei" for Watcom C/C++ (v11).
   137 */
   138 /* Enable enums always int in CodeWarrior (for MPW use "-enum int") */
   139 #ifdef __MWERKS__
   140 #pragma enumsalwaysint on
   141 #endif
   142 
   143 typedef enum {
   144 	DUMMY_ENUM_VALUE
   145 } SDL_DUMMY_ENUM;
   146 
   147 SDL_COMPILE_TIME_ASSERT(enum, sizeof(SDL_DUMMY_ENUM) == sizeof(int));
   148 
   149 
   150 #include "begin_code.h"
   151 /* Set up for C function definitions, even when using C++ */
   152 #ifdef __cplusplus
   153 extern "C" {
   154 #endif
   155 
   156 #if HAVE_MALLOC
   157 #define SDL_malloc	malloc
   158 #else
   159 extern DECLSPEC void * SDLCALL SDL_malloc(size_t size);
   160 #endif
   161 
   162 #if HAVE_CALLOC
   163 #define SDL_calloc	calloc
   164 #else
   165 extern DECLSPEC void * SDLCALL SDL_calloc(size_t nmemb, size_t size);
   166 #endif
   167 
   168 #if HAVE_REALLOC
   169 #define SDL_realloc	realloc
   170 #else
   171 extern DECLSPEC void * SDLCALL SDL_realloc(void *mem, size_t size);
   172 #endif
   173 
   174 #if HAVE_FREE
   175 #define SDL_free	free
   176 #else
   177 extern DECLSPEC void SDLCALL SDL_free(void *mem);
   178 #endif
   179 
   180 #if HAVE_ALLOCA
   181 #define SDL_stack_alloc(type, count)    (type*)alloca(sizeof(type)*count)
   182 #define SDL_stack_free(data)
   183 #else
   184 #define SDL_stack_alloc(type, count)    (type*)SDL_malloc(sizeof(type)*count)
   185 #define SDL_stack_free(data)            SDL_free(data)
   186 #endif
   187 
   188 #if HAVE_GETENV
   189 #define SDL_getenv	getenv
   190 #else
   191 extern DECLSPEC char * SDLCALL SDL_getenv(const char *name);
   192 #endif
   193 
   194 #if HAVE_PUTENV
   195 #define SDL_putenv	putenv
   196 #else
   197 extern DECLSPEC int SDLCALL SDL_putenv(const char *variable);
   198 #endif
   199 
   200 #if HAVE_QSORT
   201 #define SDL_qsort	qsort
   202 #else
   203 extern DECLSPEC void SDLCALL SDL_qsort(void *base, size_t nmemb, size_t size,
   204            int (*compare)(const void *, const void *));
   205 #endif
   206 
   207 #if HAVE_ABS
   208 #define SDL_abs		abs
   209 #else
   210 #define SDL_abs(X)	((X) < 0 ? -(X) : (X))
   211 #endif
   212 
   213 #if HAVE_CTYPE_H
   214 #define SDL_isdigit(X)  isdigit(X)
   215 #define SDL_isspace(X)  isspace(X)
   216 #define SDL_toupper(X)  toupper(X)
   217 #define SDL_tolower(X)  tolower(X)
   218 #else
   219 #define SDL_isdigit(X)  (((X) >= '0') && ((X) <= '9'))
   220 #define SDL_isspace(X)  (((X) == ' ') || ((X) == '\t') || ((X) == '\r') || ((X) == '\n'))
   221 #define SDL_toupper(X)  (((X) >= 'a') && ((X) <= 'z') ? ('A'+((X)-'a')) : (X))
   222 #define SDL_tolower(X)  (((X) >= 'A') && ((X) <= 'Z') ? ('a'+((X)-'A')) : (X))
   223 #endif
   224 
   225 #if HAVE_MEMSET
   226 #define SDL_memset      memset
   227 #else
   228 extern DECLSPEC void * SDLCALL SDL_memset(void *dst, int c, size_t len);
   229 #endif
   230 
   231 #if defined(__GNUC__) && defined(i386)
   232 #define SDL_memset4(dst, val, len)				\
   233 do {								\
   234 	int u0, u1, u2;						\
   235 	__asm__ __volatile__ (					\
   236 		"cld\n\t"					\
   237 		"rep ; stosl\n\t"				\
   238 		: "=&D" (u0), "=&a" (u1), "=&c" (u2)		\
   239 		: "0" (dst), "1" (val), "2" ((Uint32)(len))	\
   240 		: "memory" );					\
   241 } while(0)
   242 #endif
   243 #ifndef SDL_memset4
   244 #define SDL_memset4(dst, val, len)		\
   245 do {						\
   246 	unsigned _count = (len);		\
   247 	unsigned _n = (_count + 3) / 4;		\
   248 	Uint32 *_p = (Uint32 *)(dst);		\
   249 	Uint32 _val = (val);			\
   250         switch (_count % 4) {			\
   251         case 0: do {    *_p++ = _val;		\
   252         case 3:         *_p++ = _val;		\
   253         case 2:         *_p++ = _val;		\
   254         case 1:         *_p++ = _val;		\
   255 		} while ( --_n );		\
   256 	}					\
   257 } while(0)
   258 #endif
   259 
   260 #if defined(__GNUC__) && defined(i386)
   261 #define SDL_memcpy(dst, src, len)					  \
   262 do {									  \
   263 	int u0, u1, u2;						  	  \
   264 	__asm__ __volatile__ (						  \
   265 		"cld\n\t"						  \
   266 		"rep ; movsl\n\t"					  \
   267 		"testb $2,%b4\n\t"					  \
   268 		"je 1f\n\t"						  \
   269 		"movsw\n"						  \
   270 		"1:\ttestb $1,%b4\n\t"					  \
   271 		"je 2f\n\t"						  \
   272 		"movsb\n"						  \
   273 		"2:"							  \
   274 		: "=&c" (u0), "=&D" (u1), "=&S" (u2)			  \
   275 		: "0" ((unsigned)(len)/4), "q" (len), "1" (dst),"2" (src) \
   276 		: "memory" );						  \
   277 } while(0)
   278 #endif
   279 #ifndef SDL_memcpy
   280 #if HAVE_MEMCPY
   281 #define SDL_memcpy      memcpy
   282 #elif HAVE_BCOPY
   283 #define SDL_memcpy(d, s, n)	bcopy((s), (d), (n))
   284 #else
   285 extern DECLSPEC void * SDLCALL SDL_memcpy(void *dst, const void *src, size_t len);
   286 #endif
   287 #endif
   288 
   289 #if defined(__GNUC__) && defined(i386)
   290 #define SDL_memcpy4(dst, src, len)				\
   291 do {								\
   292 	int ecx, edi, esi;					\
   293 	__asm__ __volatile__ (					\
   294 		"cld\n\t"					\
   295 		"rep ; movsl"					\
   296 		: "=&c" (ecx), "=&D" (edi), "=&S" (esi)		\
   297 		: "0" ((unsigned)(len)), "1" (dst), "2" (src)	\
   298 		: "memory" );					\
   299 } while(0)
   300 #endif
   301 #ifndef SDL_memcpy4
   302 #define SDL_memcpy4(dst, src, len)	SDL_memcpy(dst, src, (len) << 2)
   303 #endif
   304 
   305 #if defined(__GNUC__) && defined(i386)
   306 #define SDL_revcpy(dst, src, len)			\
   307 do {							\
   308 	int u0, u1, u2;					\
   309 	char *dstp = (char *)(dst);			\
   310 	char *srcp = (char *)(src);			\
   311 	int n = (len);					\
   312 	if ( n >= 4 ) {					\
   313 	__asm__ __volatile__ (				\
   314 		"std\n\t"				\
   315 		"rep ; movsl\n\t"			\
   316 		: "=&c" (u0), "=&D" (u1), "=&S" (u2)	\
   317 		: "0" (n >> 2),				\
   318 		  "1" (dstp+(n-4)), "2" (srcp+(n-4))	\
   319 		: "memory" );				\
   320 	}						\
   321 	switch (n & 3) {				\
   322 		case 3: dstp[2] = srcp[2];		\
   323 		case 2: dstp[1] = srcp[1];		\
   324 		case 1: dstp[0] = srcp[0];		\
   325 			break;				\
   326 		default:				\
   327 			break;				\
   328 	}						\
   329 } while(0)
   330 #endif
   331 #ifndef SDL_revcpy
   332 extern DECLSPEC void * SDLCALL SDL_revcpy(void *dst, const void *src, size_t len);
   333 #endif
   334 
   335 #if HAVE_MEMMOVE
   336 #define SDL_memmove     memmove
   337 #elif HAVE_BCOPY
   338 #define SDL_memmove(d, s, n)	bcopy((s), (d), (n))
   339 #else
   340 #define SDL_memmove(dst, src, len)			\
   341 do {							\
   342 	if ( dst < src ) {				\
   343 		SDL_memcpy(dst, src, len);		\
   344 	} else {					\
   345 		SDL_revcpy(dst, src, len);		\
   346 	}						\
   347 } while(0)
   348 #endif
   349 
   350 #if HAVE_MEMCMP
   351 #define SDL_memcmp      memcmp
   352 #else
   353 extern DECLSPEC int SDLCALL SDL_memcmp(const void *s1, const void *s2, size_t len);
   354 #endif
   355 
   356 #if HAVE_STRLEN
   357 #define SDL_strlen      strlen
   358 #else
   359 extern DECLSPEC size_t SDLCALL SDL_strlen(const char *string);
   360 #endif
   361 
   362 #if HAVE_STRCPY
   363 #define SDL_strcpy     strcpy
   364 #else
   365 extern DECLSPEC char * SDLCALL SDL_strcpy(char *dst, const char *src);
   366 #endif
   367 
   368 #if HAVE_STRNCPY
   369 #define SDL_strncpy     strncpy
   370 #else
   371 extern DECLSPEC char * SDLCALL SDL_strncpy(char *dst, const char *src, size_t maxlen);
   372 #endif
   373 
   374 #if HAVE_STRCAT
   375 #define SDL_strcat     strcat
   376 #else
   377 #define SDL_strcat(dst, src)    (SDL_strcpy(dst+SDL_strlen(dst), src), dst)
   378 #endif
   379 
   380 #if HAVE_STRNCAT
   381 #define SDL_strncat    strncat
   382 #else
   383 #define SDL_strncat(dst, src, n) (SDL_strncpy(dst+SDL_strlen(dst), src, n), dst)
   384 #endif
   385 
   386 #if HAVE_STRDUP
   387 #define SDL_strdup     strdup
   388 #else
   389 extern DECLSPEC char * SDLCALL SDL_strdup(const char *string);
   390 #endif
   391 
   392 #if HAVE__STRREV
   393 #define SDL_strrev      _strrev
   394 #else
   395 extern DECLSPEC char * SDLCALL SDL_strrev(char *string);
   396 #endif
   397 
   398 #if HAVE__STRUPR
   399 #define SDL_strupr      _strupr
   400 #else
   401 extern DECLSPEC char * SDLCALL SDL_strupr(char *string);
   402 #endif
   403 
   404 #if HAVE__STRLWR
   405 #define SDL_strlwr      _strlwr
   406 #else
   407 extern DECLSPEC char * SDLCALL SDL_strlwr(char *string);
   408 #endif
   409 
   410 #if HAVE_STRCHR
   411 #define SDL_strchr      strchr
   412 #elif HAVE_INDEX
   413 #define SDL_strchr      index
   414 #else
   415 extern DECLSPEC char * SDLCALL SDL_strchr(const char *string, int c);
   416 #endif
   417 
   418 #if HAVE_STRRCHR
   419 #define SDL_strrchr     strrchr
   420 #elif HAVE_RINDEX
   421 #define SDL_strrchr     rindex
   422 #else
   423 extern DECLSPEC char * SDLCALL SDL_strrchr(const char *string, int c);
   424 #endif
   425 
   426 #if HAVE_STRSTR
   427 #define SDL_strstr      strstr
   428 #else
   429 extern DECLSPEC char * SDLCALL SDL_strstr(const char *haystack, const char *needle);
   430 #endif
   431 
   432 #if HAVE_ITOA
   433 #define SDL_itoa        itoa
   434 #else
   435 #define SDL_itoa(value, string, radix)	SDL_ltoa((long)value, string, radix)
   436 #endif
   437 
   438 #if HAVE__LTOA
   439 #define SDL_ltoa        _ltoa
   440 #else
   441 extern DECLSPEC char * SDLCALL SDL_ltoa(long value, char *string, int radix);
   442 #endif
   443 
   444 #if HAVE__UITOA
   445 #define SDL_uitoa       _uitoa
   446 #else
   447 #define SDL_uitoa(value, string, radix)	SDL_ultoa((long)value, string, radix)
   448 #endif
   449 
   450 #if HAVE__ULTOA
   451 #define SDL_ultoa       _ultoa
   452 #else
   453 extern DECLSPEC char * SDLCALL SDL_ultoa(unsigned long value, char *string, int radix);
   454 #endif
   455 
   456 #if HAVE_STRTOL
   457 #define SDL_strtol      strtol
   458 #else
   459 extern DECLSPEC long SDLCALL SDL_strtol(const char *string, char **endp, int base);
   460 #endif
   461 
   462 #if SDL_HAS_64BIT_TYPE
   463 
   464 #if HAVE__I64TOA
   465 #define SDL_lltoa       _i64toa
   466 #else
   467 extern DECLSPEC char* SDLCALL SDL_lltoa(Sint64 value, char *string, int radix);
   468 #endif
   469 
   470 #if HAVE__UI64TOA
   471 #define SDL_ulltoa      _ui64toa
   472 #else
   473 extern DECLSPEC char* SDLCALL SDL_ulltoa(Uint64 value, char *string, int radix);
   474 #endif
   475 
   476 #if HAVE_STRTOLL
   477 #define SDL_strtoll     strtoll
   478 #else
   479 extern DECLSPEC Sint64 SDLCALL SDL_strtoll(const char *string, char **endp, int base);
   480 #endif
   481 
   482 #endif /* SDL_HAS_64BIT_TYPE */
   483 
   484 #if HAVE_STRTOD
   485 #define SDL_strtod      strtod
   486 #else
   487 extern DECLSPEC double SDLCALL SDL_strtod(const char *string, char **endp);
   488 #endif
   489 
   490 #if HAVE_ATOI
   491 #define SDL_atoi        atoi
   492 #else
   493 #define SDL_atoi(X)     SDL_strtol(X, NULL, 0)
   494 #endif
   495 
   496 #if HAVE_ATOF
   497 #define SDL_atof        atof
   498 #else
   499 #define SDL_atof(X)     SDL_strtod(X, NULL)
   500 #endif
   501 
   502 #if HAVE_STRCMP
   503 #define SDL_strcmp      strcmp
   504 #else
   505 extern DECLSPEC int SDLCALL SDL_strcmp(const char *str1, const char *str2);
   506 #endif
   507 
   508 #if HAVE_STRNCMP
   509 #define SDL_strncmp     strncmp
   510 #else
   511 extern DECLSPEC int SDLCALL SDL_strncmp(const char *str1, const char *str2, size_t maxlen);
   512 #endif
   513 
   514 #if HAVE_STRCASECMP
   515 #define SDL_strcasecmp  strcasecmp
   516 #elif HAVE_STRICMP
   517 #define SDL_strcasecmp  stricmp
   518 #else
   519 extern DECLSPEC int SDLCALL SDL_strcasecmp(const char *str1, const char *str2);
   520 #endif
   521 
   522 #if HAVE_SSCANF
   523 #define SDL_sscanf      sscanf
   524 #else
   525 extern DECLSPEC int SDLCALL SDL_sscanf(const char *text, const char *fmt, ...);
   526 #endif
   527 
   528 #if HAVE_SNPRINTF
   529 #define SDL_snprintf    snprintf
   530 #else
   531 extern DECLSPEC int SDLCALL SDL_snprintf(char *text, size_t maxlen, const char *fmt, ...);
   532 #endif
   533 
   534 #if HAVE_VSNPRINTF
   535 #define SDL_vsnprintf   vsnprintf
   536 #else
   537 extern DECLSPEC int SDLCALL SDL_vsnprintf(char *text, size_t maxlen, const char *fmt, va_list ap);
   538 #endif
   539 
   540 /* Ends C function definitions when using C++ */
   541 #ifdef __cplusplus
   542 }
   543 #endif
   544 #include "close_code.h"
   545 
   546 #endif /* _SDL_stdinc_h */