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