From 21e334d145ae080adc462b600de11205d255c18b Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Fri, 5 Jul 2013 23:57:19 -0700 Subject: [PATCH] Removed the inline functions from SDL_stdinc.h Having the SDL functions inline is causing build issues, and in the case of malloc(), etc. causing malloc/free mismatches, if the application build environment differs from the SDL build environment. In the interest of safety and consistency, the functions will always be in the SDL library and will only be redirected to the C library there, if they are available. See the following threads on the SDL mailing list for the gruesome details: * SDL_stdinc.h inlines problematic when application not compiled in exact same feature environment * Error compiling program against SDL2 with -std=c++11 g++ flag --- include/SDL_stdinc.h | 457 ++------------------------------------- src/libm/math_libm.h | 77 +------ src/libm/math_private.h | 12 + src/stdlib/SDL_getenv.c | 18 +- src/stdlib/SDL_malloc.c | 31 ++- src/stdlib/SDL_qsort.c | 5 +- src/stdlib/SDL_stdlib.c | 179 +++++++++++++-- src/stdlib/SDL_string.c | 452 +++++++++++++++++++------------------- src/video/SDL_RLEaccel.c | 8 +- 9 files changed, 460 insertions(+), 779 deletions(-) diff --git a/include/SDL_stdinc.h b/include/SDL_stdinc.h index f52bd0310..f8b6bd417 100644 --- a/include/SDL_stdinc.h +++ b/include/SDL_stdinc.h @@ -227,76 +227,17 @@ char *alloca(); #define SDL_stack_free(data) SDL_free(data) #endif - -/* SDL stdinc inline functions: - - The theory here is that by default we forcibly inline what we can, and your - app will use the inline version by default. However we expose a non-inline - version too, so the symbol is always available in the library even if your app - bypassed the inline version. The SDL_*_inline versions aren't guaranteed to - exist, so never call them directly; use SDL_* instead, and trust the system - to give you the right thing. - - The benefit here is that you can dlsym() these functions, which you - couldn't if you had macros, you can link against a foreign build of SDL - even if you configured differently, and you can drop the unconfigured SDL - headers into a project without #defining HAVE_MALLOC (etc) and still link. - - If you want to disable the inline functions and just use SDL's functions, - you can define SDL_STDINC_NO_INLINES before including this file. -*/ - extern DECLSPEC void *SDLCALL SDL_malloc(size_t size); -#if defined(HAVE_MALLOC) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE void *SDL_malloc_inline(size_t size) { return malloc(size); } -#define SDL_malloc SDL_malloc_inline -#endif - extern DECLSPEC void *SDLCALL SDL_calloc(size_t nmemb, size_t size); -#if defined(HAVE_CALLOC) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE void *SDL_calloc_inline(size_t nmemb, size_t size) { return calloc(nmemb, size); } -#define SDL_calloc SDL_calloc_inline -#endif - extern DECLSPEC void *SDLCALL SDL_realloc(void *mem, size_t size); -#if defined(HAVE_REALLOC) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE void *SDL_realloc_inline(void *mem, size_t size) { return realloc(mem, size); } -#define SDL_realloc SDL_realloc_inline -#endif - extern DECLSPEC void SDLCALL SDL_free(void *mem); -#if defined(HAVE_FREE) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE void SDL_free_inline(void *mem) { free(mem); } -#define SDL_free SDL_free_inline -#endif extern DECLSPEC char *SDLCALL SDL_getenv(const char *name); -#if defined(HAVE_GETENV) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE char *SDL_getenv_inline(const char *name) { return getenv(name); } -#define SDL_getenv SDL_getenv_inline -#endif - extern DECLSPEC int SDLCALL SDL_setenv(const char *name, const char *value, int overwrite); -#if defined(HAVE_SETENV) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE int SDL_setenv_inline(const char *name, const char *value, int overwrite) { return setenv(name, value, overwrite); } -#define SDL_setenv SDL_setenv_inline -#endif extern DECLSPEC void SDLCALL SDL_qsort(void *base, size_t nmemb, size_t size, int (*compare) (const void *, const void *)); -#if defined(HAVE_QSORT) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE void SDL_qsort_inline(void *base, size_t nmemb, size_t size, int (*compare) (const void *, const void *)) { qsort(base, nmemb, size, compare); } -#define SDL_qsort SDL_qsort_inline -#endif extern DECLSPEC int SDLCALL SDL_abs(int x); -#ifndef SDL_STDINC_NO_INLINES -#ifdef HAVE_ABS -SDL_FORCE_INLINE int SDL_abs_inline(int x) { return abs(x); } -#else -SDL_FORCE_INLINE int SDL_abs_inline(int x) { return ((x) < 0 ? -(x) : (x)); } -#endif -#define SDL_abs SDL_abs_inline -#endif /* !SDL_STDINC_NO_INLINES */ /* !!! FIXME: these have side effects. You probably shouldn't use them. */ /* !!! FIXME: Maybe we do forceinline functions of SDL_mini, SDL_minf, etc? */ @@ -307,35 +248,14 @@ extern DECLSPEC int SDLCALL SDL_isdigit(int x); extern DECLSPEC int SDLCALL SDL_isspace(int x); extern DECLSPEC int SDLCALL SDL_toupper(int x); extern DECLSPEC int SDLCALL SDL_tolower(int x); -#ifndef SDL_STDINC_NO_INLINES -#ifdef HAVE_CTYPE_H -SDL_FORCE_INLINE int SDL_isdigit_inline(int x) { return isdigit(x); } -SDL_FORCE_INLINE int SDL_isspace_inline(int x) { return isspace(x); } -SDL_FORCE_INLINE int SDL_toupper_inline(int x) { return toupper(x); } -SDL_FORCE_INLINE int SDL_tolower_inline(int x) { return tolower(x); } -#else -SDL_FORCE_INLINE int SDL_isdigit_inline(int x) { return ((x) >= '0') && ((x) <= '9'); } -SDL_FORCE_INLINE int SDL_isspace_inline(int x) { return ((x) == ' ') || ((x) == '\t') || ((x) == '\r') || ((x) == '\n'); } -SDL_FORCE_INLINE int SDL_toupper_inline(int x) { return ((x) >= 'a') && ((x) <= 'z') ? ('A'+((x)-'a')) : (x); } -SDL_FORCE_INLINE int SDL_tolower_inline(int x) { return ((x) >= 'A') && ((x) <= 'Z') ? ('a'+((x)-'A')) : (x); } -#endif -#define SDL_isdigit SDL_isdigit_inline -#define SDL_isspace SDL_isspace_inline -#define SDL_toupper SDL_toupper_inline -#define SDL_tolower SDL_tolower_inline -#endif /* !SDL_STDINC_NO_INLINES */ extern DECLSPEC void *SDLCALL SDL_memset(void *dst, int c, size_t len); -#if defined(HAVE_MEMSET) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE void *SDL_memset_inline(void *dst, int c, size_t len) { return memset(dst, c, len); } -#define SDL_memset SDL_memset_inline -#endif #define SDL_zero(x) SDL_memset(&(x), 0, sizeof((x))) #define SDL_zerop(x) SDL_memset((x), 0, sizeof(*(x))) -/* !!! FIXME: does this _really_ beat memset() on any modern platform? */ -SDL_FORCE_INLINE void SDL_memset4(void *dst, int val, size_t len) +/* Note that the semantics are different from memset() in that this is a 32-bit assignment */ +SDL_FORCE_INLINE void SDL_memset4(void *dst, int val, size_t dwords) { #if defined(__GNUC__) && defined(i386) int u0, u1, u2; @@ -343,17 +263,16 @@ SDL_FORCE_INLINE void SDL_memset4(void *dst, int val, size_t len) "cld \n\t" "rep ; stosl \n\t" : "=&D" (u0), "=&a" (u1), "=&c" (u2) - : "0" (dst), "1" (val), "2" (SDL_static_cast(Uint32, len)) + : "0" (dst), "1" (val), "2" (SDL_static_cast(Uint32, dwords)) : "memory" ); -/* !!! FIXME: amd64? */ #else - size_t _n = (len + 3) / 4; + size_t _n = (dwords + 3) / 4; Uint32 *_p = SDL_static_cast(Uint32 *, dst); Uint32 _val = (val); - if (len == 0) + if (dwords == 0) return; - switch (len % 4) + switch (dwords % 4) { case 0: do { *_p++ = _val; case 3: *_p++ = _val; @@ -366,323 +285,54 @@ SDL_FORCE_INLINE void SDL_memset4(void *dst, int val, size_t len) extern DECLSPEC void *SDLCALL SDL_memcpy(void *dst, const void *src, size_t len); -#if !defined(SDL_STDINC_NO_INLINES) -#if defined(__MACOSX__) && defined(HAVE_MEMCPY) -SDL_FORCE_INLINE void *SDL_memcpy_inline(void *dst, const void *src, size_t len) -{ - /* We can count on memcpy existing on Mac OS X and being well-tuned. */ - return memcpy(dst, src, len); -} -#define SDL_memcpy SDL_memcpy_inline -#elif defined(__GNUC__) && defined(i386) && !defined(__WIN32__) -SDL_FORCE_INLINE void *SDL_memcpy_inline(void *dst, const void *src, size_t len) -{ - /* !!! FIXME: does this _really_ beat memcpy() on any modern platform? */ - /* !!! FIXME: shouldn't we just force the inputs to ecx/edi/esi instead of this tapdance with outputs? */ - /* !!! FIXME: amd64? */ - int u0, u1, u2; - __asm__ __volatile__ ( - "cld \n\t" - "rep ; movsl \n\t" - "testb $2,%b4 \n\t" - "je 1f \n\t" - "movsw \n" - "1:\ttestb $1,%b4 \n\t" - "je 2f \n\t" - "movsb \n" - "2:" - : "=&c" (u0), "=&D" (u1), "=&S" (u2) - : "0" (SDL_static_cast(unsigned, len)/4), "q" (len), "1" (dst), "2" (src) - : "memory" - ); - return dst; -} -#define SDL_memcpy SDL_memcpy_inline -#elif defined(HAVE_MEMCPY) -SDL_FORCE_INLINE void *SDL_memcpy_inline(void *dst, const void *src, size_t len) -{ - return memcpy(dst, src, len); -} -#define SDL_memcpy SDL_memcpy_inline -#elif defined(HAVE_BCOPY) /* !!! FIXME: is there _really_ ever a time where you have bcopy and not memcpy? */ -SDL_FORCE_INLINE void *SDL_memcpy_inline(void *dst, const void *src, size_t len) -{ - bcopy(src, dst, len); - return dst; -} -#define SDL_memcpy SDL_memcpy_inline -#endif -#endif /* !SDL_STDINC_NO_INLINES */ - SDL_FORCE_INLINE void *SDL_memcpy4(void *dst, const void *src, size_t dwords) { -#if defined(__GNUC__) && defined(i386) - /* !!! FIXME: does this _really_ beat memcpy() on any modern platform? */ - /* !!! FIXME: shouldn't we just force the inputs to ecx/edi/esi instead of this tapdance with outputs? */ - int ecx, edi, esi; - __asm__ __volatile__ ( - "cld \n\t" - "rep ; movsl \n\t" - : "=&c" (ecx), "=&D" (edi), "=&S" (esi) - : "0" (SDL_static_cast(unsigned, dwords)), "1" (dst), "2" (src) - : "memory" - ); - return dst; -#else return SDL_memcpy(dst, src, dwords * 4); -#endif } extern DECLSPEC void *SDLCALL SDL_memmove(void *dst, const void *src, size_t len); -#if defined(HAVE_MEMMOVE) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE void *SDL_memmove_inline(void *dst, const void *src, size_t len) { return memmove(dst, src, len); } -#define SDL_memmove SDL_memmove_inline -#endif - extern DECLSPEC int SDLCALL SDL_memcmp(const void *s1, const void *s2, size_t len); -#if defined(HAVE_MEMCMP) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE int SDL_memcmp_inline(const void *s1, const void *s2, size_t len) { return memcmp(s1, s2, len); } -#define SDL_memcmp SDL_memcmp_inline -#endif - -extern DECLSPEC size_t SDLCALL SDL_strlen(const char *str); -#if defined(HAVE_STRLEN) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE size_t SDL_strlen_inline(const char *str) { return strlen(str); } -#define SDL_strlen SDL_strlen_inline -#endif extern DECLSPEC size_t SDLCALL SDL_wcslen(const wchar_t *wstr); -#if defined(HAVE_WCSLEN) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE size_t SDL_wcslen_inline(const wchar_t *wstr) { return wcslen(wstr); } -#define SDL_wcslen SDL_wcslen_inline -#endif - extern DECLSPEC size_t SDLCALL SDL_wcslcpy(wchar_t *dst, const wchar_t *src, size_t maxlen); -#if defined(HAVE_WCSLCPY) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE size_t SDL_wcslcpy_inline(wchar_t *dst, const wchar_t *src, size_t maxlen) { return wcslcpy(dst, src, maxlen); } -#define SDL_wcslcpy SDL_wcslcpy_inline -#endif - extern DECLSPEC size_t SDLCALL SDL_wcslcat(wchar_t *dst, const wchar_t *src, size_t maxlen); -#if defined(HAVE_WCSLCAT) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE size_t SDL_wcslcat_inline(wchar_t *dst, const wchar_t *src, size_t maxlen) { return wcslcat(dst, src, maxlen); } -#define SDL_wcslcat SDL_wcslcat_inline -#endif +extern DECLSPEC size_t SDLCALL SDL_strlen(const char *str); extern DECLSPEC size_t SDLCALL SDL_strlcpy(char *dst, const char *src, size_t maxlen); -#if defined(HAVE_STRLCPY) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE size_t SDL_strlcpy_inline(char *dst, const char *src, size_t maxlen) { return strlcpy(dst, src, maxlen); } -#define SDL_strlcpy SDL_strlcpy_inline -#else -#endif - extern DECLSPEC size_t SDLCALL SDL_utf8strlcpy(char *dst, const char *src, size_t dst_bytes); - extern DECLSPEC size_t SDLCALL SDL_strlcat(char *dst, const char *src, size_t maxlen); -#if defined(HAVE_STRLCAT) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE size_t SDL_strlcat_inline(char *dst, const char *src, size_t maxlen) { return strlcat(dst, src, maxlen); } -#define SDL_strlcat SDL_strlcat_inline -#endif - extern DECLSPEC char *SDLCALL SDL_strdup(const char *str); -#if defined(HAVE_STRDUP) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE char *SDL_strdup_inline(const char *str) { return strdup(str); } -#define SDL_strdup SDL_strdup_inline -#endif - extern DECLSPEC char *SDLCALL SDL_strrev(char *str); -#if defined(HAVE__STRREV) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE char *SDL_strrev_inline(char *str) { return _strrev(str); } -#define SDL_strrev SDL_strrev_inline -#endif - extern DECLSPEC char *SDLCALL SDL_strupr(char *str); -#if defined(HAVE__STRUPR) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE char *SDL_strupr_inline(char *str) { return _strupr(str); } -#define SDL_strupr SDL_strupr_inline -#endif - extern DECLSPEC char *SDLCALL SDL_strlwr(char *str); -#if defined(HAVE__STRLWR) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE char *SDL_strlwr_inline(char *str) { return _strlwr(str); } -#define SDL_strlwr SDL_strlwr_inline -#endif - extern DECLSPEC char *SDLCALL SDL_strchr(const char *str, int c); -#ifndef SDL_STDINC_NO_INLINES -#ifdef HAVE_STRCHR -SDL_FORCE_INLINE char *SDL_strchr_inline(const char *str, int c) { return SDL_const_cast(char*,strchr(str, c)); } -#define SDL_strchr SDL_strchr_inline -#elif defined(HAVE_INDEX) /* !!! FIXME: is there anywhere that has this but not strchr? */ -SDL_FORCE_INLINE char *SDL_strchr_inline(const char *str, int c) { return SDL_const_cast(char*,index(str, c)); } -#define SDL_strchr SDL_strchr_inline -#endif -#endif /* !SDL_STDINC_NO_INLINES */ - extern DECLSPEC char *SDLCALL SDL_strrchr(const char *str, int c); -#ifndef SDL_STDINC_NO_INLINES -#ifdef HAVE_STRRCHR -SDL_FORCE_INLINE char *SDL_strrchr_inline(const char *str, int c) { return SDL_const_cast(char*,strrchr(str, c)); } -#define SDL_strrchr SDL_strrchr_inline -#elif defined(HAVE_RINDEX) /* !!! FIXME: is there anywhere that has this but not strrchr? */ -SDL_FORCE_INLINE char *SDL_strrchr_inline(const char *str, int c) { return SDL_const_cast(char*,rindex(str, c)); } -#define SDL_strrchr SDL_strrchr_inline -#endif -#endif /* !SDL_STDINC_NO_INLINES */ - extern DECLSPEC char *SDLCALL SDL_strstr(const char *haystack, const char *needle); -#if defined(HAVE_STRSTR) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE char *SDL_strstr_inline(const char *haystack, const char *needle) { return SDL_const_cast(char*,strstr(haystack, needle)); } -#define SDL_strstr SDL_strstr_inline -#endif - -extern DECLSPEC char *SDLCALL SDL_ltoa(long value, char *str, int radix); -#if defined(HAVE__LTOA) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE char *SDL_ltoa_inline(long value, char *str, int radix) { return _ltoa(value, str, radix); } -#define SDL_ltoa SDL_ltoa_inline -#endif extern DECLSPEC char *SDLCALL SDL_itoa(int value, char *str, int radix); -#ifndef SDL_STDINC_NO_INLINES -#ifdef HAVE_ITOA -SDL_FORCE_INLINE char *SDL_itoa_inline(int value, char *str, int radix) { return itoa(value, str, radix); } -#else -SDL_FORCE_INLINE char *SDL_itoa_inline(int value, char *str, int radix) { return SDL_ltoa((long)value, str, radix); } -#endif -#define SDL_itoa SDL_itoa_inline -#endif /* !SDL_STDINC_NO_INLINES */ - -extern DECLSPEC char *SDLCALL SDL_ultoa(unsigned long value, char *str, int radix); -#if defined(HAVE__ULTOA) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE char *SDL_ultoa_inline(unsigned long value, char *str, int radix) { return _ultoa(value, str, radix); } -#define SDL_ultoa SDL_ultoa_inline -#endif - extern DECLSPEC char *SDLCALL SDL_uitoa(unsigned int value, char *str, int radix); -#ifndef SDL_STDINC_NO_INLINES -#ifdef HAVE__UITOA -SDL_FORCE_INLINE char *SDL_uitoa_inline(unsigned int value, char *str, int radix) { return _uitoa(value, str, radix); } -#else -SDL_FORCE_INLINE char *SDL_uitoa_inline(unsigned int value, char *str, int radix) { return SDL_ultoa((unsigned long)value, str, radix); } -#endif -#define SDL_uitoa SDL_uitoa_inline -#endif /* !SDL_STDINC_NO_INLINES */ - - -extern DECLSPEC long SDLCALL SDL_strtol(const char *str, char **endp, int base); -#if defined(HAVE_STRTOL) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE long SDL_strtol_inline(const char *str, char **endp, int base) { return strtol(str, endp, base); } -#define SDL_strtol SDL_strtol_inline -#endif - -extern DECLSPEC unsigned long SDLCALL SDL_strtoul(const char *str, char **endp, int base); -#if defined(HAVE_STRTOUL) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE unsigned long SDLCALL SDL_strtoul_inline(const char *str, char **endp, int base) { return strtoul(str, endp, base); } -#define SDL_strtoul SDL_strtoul_inline -#endif - +extern DECLSPEC char *SDLCALL SDL_ltoa(long value, char *str, int radix); +extern DECLSPEC char *SDLCALL SDL_ultoa(unsigned long value, char *str, int radix); extern DECLSPEC char *SDLCALL SDL_lltoa(Sint64 value, char *str, int radix); -#if defined(HAVE__I64TOA) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE char *SDL_lltoa_inline(Sint64 value, char *str, int radix) { return _i64toa(value, str, radix); } -#define SDL_lltoa SDL_lltoa_inline -#endif - extern DECLSPEC char *SDLCALL SDL_ulltoa(Uint64 value, char *str, int radix); -#if defined(HAVE__UI64TOA) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE char *SDL_ulltoa_inline(Uint64 value, char *str, int radix) { return _ui64toa(value, str, radix); } -#define SDL_ulltoa SDL_ulltoa_inline -#endif +extern DECLSPEC int SDLCALL SDL_atoi(const char *str); +extern DECLSPEC double SDLCALL SDL_atof(const char *str); +extern DECLSPEC long SDLCALL SDL_strtol(const char *str, char **endp, int base); +extern DECLSPEC unsigned long SDLCALL SDL_strtoul(const char *str, char **endp, int base); extern DECLSPEC Sint64 SDLCALL SDL_strtoll(const char *str, char **endp, int base); -#if defined(HAVE_STRTOLL) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE Sint64 SDL_strtoll_inline(const char *str, char **endp, int base) { return strtoll(str, endp, base); } -#define SDL_strtoll SDL_strtoll_inline -#endif - extern DECLSPEC Uint64 SDLCALL SDL_strtoull(const char *str, char **endp, int base); -#if defined(HAVE_STRTOULL) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE Uint64 SDL_strtoull_inline(const char *str, char **endp, int base) { return strtoull(str, endp, base); } -#define SDL_strtoull SDL_strtoull_inline -#endif - extern DECLSPEC double SDLCALL SDL_strtod(const char *str, char **endp); -#if defined(HAVE_STRTOD) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE double SDL_strtod_inline(const char *str, char **endp) { return strtod(str, endp); } -#define SDL_strtod SDL_strtod_inline -#endif - -extern DECLSPEC int SDLCALL SDL_atoi(const char *str); -#ifndef SDL_STDINC_NO_INLINES -#ifdef HAVE_ATOI -SDL_FORCE_INLINE int SDL_atoi_inline(const char *str) { return atoi(str); } -#else -SDL_FORCE_INLINE int SDL_atoi_inline(const char *str) { return SDL_strtol(str, NULL, 0); } -#endif -#define SDL_atoi SDL_atoi_inline -#endif /* !SDL_STDINC_NO_INLINES */ - -extern DECLSPEC double SDLCALL SDL_atof(const char *str); -#ifndef SDL_STDINC_NO_INLINES -#ifdef HAVE_ATOF -SDL_FORCE_INLINE double SDL_atof_inline(const char *str) { return (double) atof(str); } -#else -SDL_FORCE_INLINE double SDL_atof_inline(const char *str) { return SDL_strtod(str, NULL); } -#endif -#define SDL_atof SDL_atof_inline -#endif /* !SDL_STDINC_NO_INLINES */ - extern DECLSPEC int SDLCALL SDL_strcmp(const char *str1, const char *str2); -#if defined(HAVE_STRCMP) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE int SDL_strcmp_inline(const char *str1, const char *str2) { return strcmp(str1, str2); } -#define SDL_strcmp SDL_strcmp_inline -#endif - extern DECLSPEC int SDLCALL SDL_strncmp(const char *str1, const char *str2, size_t maxlen); -#if defined(HAVE_STRNCMP) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE int SDL_strncmp_inline(const char *str1, const char *str2, size_t maxlen) { return strncmp(str1, str2, maxlen); } -#define SDL_strncmp SDL_strncmp_inline -#endif - extern DECLSPEC int SDLCALL SDL_strcasecmp(const char *str1, const char *str2); -#ifndef SDL_STDINC_NO_INLINES -#ifdef HAVE_STRCASECMP -SDL_FORCE_INLINE int SDL_strcasecmp_inline(const char *str1, const char *str2) { return strcasecmp(str1, str2); } -#define SDL_strcasecmp SDL_strcasecmp_inline -#elif defined(HAVE__STRICMP) -SDL_FORCE_INLINE int SDL_strcasecmp_inline(const char *str1, const char *str2) { return _stricmp(str1, str2); } -#define SDL_strcasecmp SDL_strcasecmp_inline -#endif -#endif /* !SDL_STDINC_NO_INLINES */ - extern DECLSPEC int SDLCALL SDL_strncasecmp(const char *str1, const char *str2, size_t len); -#ifndef SDL_STDINC_NO_INLINES -#ifdef HAVE_STRNCASECMP -SDL_FORCE_INLINE int SDL_strncasecmp_inline(const char *str1, const char *str2, size_t len) { return strncasecmp(str1, str2, len); } -#define SDL_strncasecmp SDL_strncasecmp_inline -#elif defined(HAVE__STRNICMP) -SDL_FORCE_INLINE int SDL_strncasecmp_inline(const char *str1, const char *str2, size_t len) { return _strnicmp(str1, str2, len); } -#define SDL_strncasecmp SDL_strncasecmp_inline -#endif -#endif /* !SDL_STDINC_NO_INLINES */ -/* Not doing SDL_*_inline functions for these, because of the varargs. */ extern DECLSPEC int SDLCALL SDL_sscanf(const char *text, const char *fmt, ...); -#ifdef HAVE_SSCANF -#define SDL_sscanf sscanf -#endif - extern DECLSPEC int SDLCALL SDL_snprintf(char *text, size_t maxlen, const char *fmt, ...); -#ifdef HAVE_SNPRINTF -#define SDL_snprintf snprintf -#endif - extern DECLSPEC int SDLCALL SDL_vsnprintf(char *text, size_t maxlen, const char *fmt, va_list ap); -#if defined(HAVE_VSNPRINTF) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE int SDL_vsnprintf_inline(char *text, size_t maxlen, const char *fmt, va_list ap) { return vsnprintf(text, maxlen, fmt, ap); } -#define SDL_vsnprintf SDL_vsnprintf_inline -#endif #ifndef HAVE_M_PI #ifndef M_PI @@ -691,100 +341,19 @@ SDL_FORCE_INLINE int SDL_vsnprintf_inline(char *text, size_t maxlen, const char #endif extern DECLSPEC double SDLCALL SDL_atan(double x); -#if defined(HAVE_ATAN) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE double SDL_atan_inline(double x) { return atan(x); } -#define SDL_atan SDL_atan_inline -#endif - extern DECLSPEC double SDLCALL SDL_atan2(double x, double y); -#if defined(HAVE_ATAN2) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE double SDL_atan2_inline(double x, double y) { return atan2(x, y); } -#define SDL_atan2 SDL_atan2_inline -#endif - extern DECLSPEC double SDLCALL SDL_ceil(double x); -#ifndef SDL_STDINC_NO_INLINES -#ifdef HAVE_CEIL -SDL_FORCE_INLINE double SDL_ceil_inline(double x) { return ceil(x); } -#else -SDL_FORCE_INLINE double SDL_ceil_inline(double x) { return (double)(int)((x)+0.5); } -#endif -#define SDL_ceil SDL_ceil_inline -#endif /* !SDL_STDINC_NO_INLINES */ - extern DECLSPEC double SDLCALL SDL_copysign(double x, double y); -#if defined(HAVE_COPYSIGN) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE double SDL_copysign_inline(double x, double y) { return copysign(x, y); } -#define SDL_copysign SDL_copysign_inline -#endif - extern DECLSPEC double SDLCALL SDL_cos(double x); -#if defined(HAVE_COS) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE double SDL_cos_inline(double x) { return cos(x); } -#define SDL_cos SDL_cos_inline -#endif - extern DECLSPEC float SDLCALL SDL_cosf(float x); -#ifndef SDL_STDINC_NO_INLINES -#ifdef HAVE_COSF -SDL_FORCE_INLINE float SDL_cosf_inline(float x) { return cosf(x); } -#else -SDL_FORCE_INLINE float SDL_cosf_inline(float x) { return (float)SDL_cos((double)x); } -#endif -#define SDL_cosf SDL_cosf_inline -#endif /* !SDL_STDINC_NO_INLINES */ - extern DECLSPEC double SDLCALL SDL_fabs(double x); -#if defined(HAVE_FABS) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE double SDL_fabs_inline(double x) { return fabs(x); } -#define SDL_fabs SDL_fabs_inline -#endif - extern DECLSPEC double SDLCALL SDL_floor(double x); -#if defined(HAVE_FLOOR) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE double SDL_floor_inline(double x) { return floor(x); } -#define SDL_floor SDL_floor_inline -#endif - extern DECLSPEC double SDLCALL SDL_log(double x); -#if defined(HAVE_LOG) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE double SDL_log_inline(double x) { return log(x); } -#define SDL_log SDL_log_inline -#endif - extern DECLSPEC double SDLCALL SDL_pow(double x, double y); -#if defined(HAVE_POW) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE double SDL_pow_inline(double x, double y) { return pow(x, y); } -#define SDL_pow SDL_pow_inline -#endif - extern DECLSPEC double SDLCALL SDL_scalbn(double x, int n); -#if defined(HAVE_SCALBN) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE double SDL_scalbn_inline(double x, int n) { return scalbn(x, n); } -#define SDL_scalbn SDL_scalbn_inline -#endif - extern DECLSPEC double SDLCALL SDL_sin(double x); -#if defined(HAVE_SIN) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE double SDL_sin_inline(double x) { return sin(x); } -#define SDL_sin SDL_sin_inline -#endif - extern DECLSPEC float SDLCALL SDL_sinf(float x); -#ifndef SDL_STDINC_NO_INLINES -#ifdef HAVE_SINF -SDL_FORCE_INLINE float SDL_sinf_inline(float x) { return sinf(x); } -#else -SDL_FORCE_INLINE float SDL_sinf_inline(float x) { return (float)SDL_sin((double)x); } -#endif -#define SDL_sinf SDL_sinf_inline -#endif /* !SDL_STDINC_NO_INLINES */ - extern DECLSPEC double SDLCALL SDL_sqrt(double x); -#if defined(HAVE_SQRT) && !defined(SDL_STDINC_NO_INLINES) -SDL_FORCE_INLINE double SDL_sqrt_inline(double x) { return sqrt(x); } -#define SDL_sqrt SDL_sqrt_inline -#endif /* The SDL implementation of iconv() returns these error codes */ #define SDL_ICONV_ERROR (size_t)-1 diff --git a/src/libm/math_libm.h b/src/libm/math_libm.h index 6a6788255..8d7ac30c2 100644 --- a/src/libm/math_libm.h +++ b/src/libm/math_libm.h @@ -18,74 +18,19 @@ misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. */ -#include "SDL_config.h" -#include "SDL_stdinc.h" /* Math routines from uClibc: http://www.uclibc.org */ -#ifdef HAVE_ATAN -#define atan SDL_uclibc_atan -#else -#define atan SDL_atan -#endif -double atan(double x); - -#ifndef HAVE_ATAN2 -#define __ieee754_atan2 SDL_atan2 -#endif - -#ifdef HAVE_COPYSIGN -#define copysign SDL_uclibc_copysign -#else -#define copysign SDL_copysign -#endif -double copysign(double x, double y); - -#ifdef HAVE_COS -#define cos SDL_uclibc_cos -#else -#define cos SDL_cos -#endif -double cos(double x); - -#ifdef HAVE_FABS -#define fabs SDL_uclibc_fabs -#else -#define fabs SDL_fabs -#endif -double fabs(double x); - -#ifdef HAVE_FLOOR -#define floor SDL_uclibc_floor -#else -#define floor SDL_floor -#endif -double floor(double x); - -#ifndef HAVE_LOG -#define __ieee754_log SDL_log -#endif - -#ifndef HAVE_POW -#define __ieee754_pow SDL_pow -#endif - -#ifdef HAVE_SCALBN -#define scalbn SDL_uclibc_scalbn -#else -#define scalbn SDL_scalbn -#endif -double scalbn(double x, int n); - -#ifdef HAVE_SIN -#define sin SDL_uclibc_sin -#else -#define sin SDL_sin -#endif -double sin(double x); - -#ifndef HAVE_SQRT -#define __ieee754_sqrt SDL_sqrt -#endif +double SDL_uclibc_atan(double x); +double SDL_uclibc_atan2(double y, double x); +double SDL_uclibc_copysign(double x, double y); +double SDL_uclibc_cos(double x); +double SDL_uclibc_fabs(double x); +double SDL_uclibc_floor(double x); +double SDL_uclibc_log(double x); +double SDL_uclibc_pow(double x, double y); +double SDL_uclibc_scalbn(double x, int n); +double SDL_uclibc_sin(double x); +double SDL_uclibc_sqrt(double x); /* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/libm/math_private.h b/src/libm/math_private.h index 500e7da96..d61dc2e07 100644 --- a/src/libm/math_private.h +++ b/src/libm/math_private.h @@ -27,6 +27,18 @@ typedef unsigned int u_int32_t; +#define atan SDL_uclibc_atan +#define __ieee754_atan2 SDL_uclibc_atan2 +#define copysign SDL_uclibc_copysign +#define cos SDL_uclibc_cos +#define fabs SDL_uclibc_fabs +#define floor SDL_uclibc_floor +#define __ieee754_log SDL_uclibc_log +#define __ieee754_pow SDL_uclibc_pow +#define scalbn SDL_uclibc_scalbn +#define sin SDL_uclibc_sin +#define __ieee754_sqrt SDL_uclibc_sqrt + /* The original fdlibm code used statements like: n0 = ((*(int*)&one)>>29)^1; * index of high word * ix0 = *(n0+(int*)&x); * high word of x * diff --git a/src/stdlib/SDL_getenv.c b/src/stdlib/SDL_getenv.c index 9079387cd..0cd440549 100644 --- a/src/stdlib/SDL_getenv.c +++ b/src/stdlib/SDL_getenv.c @@ -31,9 +31,12 @@ static size_t SDL_envmemlen = 0; /* Put a variable into the environment */ -#ifdef SDL_setenv -#undef SDL_setenv -int SDL_setenv(const char *name, const char *value, int overwrite) { return SDL_setenv_inline(name, value, overwrite); } +#if defined(HAVE_SETENV) +int +SDL_setenv(const char *name, const char *value, int overwrite) +{ + return setenv(name, value, overwrite); +} #elif defined(__WIN32__) int SDL_setenv(const char *name, const char *value, int overwrite) @@ -143,9 +146,12 @@ SDL_setenv(const char *name, const char *value, int overwrite) #endif /* Retrieve a variable named "name" from the environment */ -#ifdef SDL_getenv -#undef SDL_getenv -char *SDL_getenv(const char *name) { return SDL_getenv_inline(name); } +#if defined(HAVE_GETENV) +char * +SDL_getenv(const char *name) +{ + return getenv(name); +} #elif defined(__WIN32__) char * SDL_getenv(const char *name) diff --git a/src/stdlib/SDL_malloc.c b/src/stdlib/SDL_malloc.c index 5f7882139..7a85a3b49 100644 --- a/src/stdlib/SDL_malloc.c +++ b/src/stdlib/SDL_malloc.c @@ -24,16 +24,27 @@ #include "SDL_stdinc.h" -#ifdef SDL_malloc -/* expose the symbol, but use what we figured out elsewhere. */ -#undef SDL_malloc -#undef SDL_calloc -#undef SDL_realloc -#undef SDL_free -void *SDL_malloc(size_t size) { return SDL_malloc_inline(size); } -void *SDL_calloc(size_t nmemb, size_t size) { return SDL_calloc_inline(nmemb, size); } -void *SDL_realloc(void *ptr, size_t size) { return SDL_realloc_inline(ptr, size); } -void SDL_free(void *ptr) { SDL_free_inline(ptr); } +#if defined(HAVE_MALLOC) + +void *SDL_malloc(size_t size) +{ + return malloc(size); +} + +void *SDL_calloc(size_t nmemb, size_t size) +{ + return calloc(nmemb, size); +} + +void *SDL_realloc(void *ptr, size_t size) +{ + return realloc(ptr, size); +} + +void SDL_free(void *ptr) +{ + free(ptr); +} #else /* the rest of this is a LOT of tapdancing to implement malloc. :) */ diff --git a/src/stdlib/SDL_qsort.c b/src/stdlib/SDL_qsort.c index 8c73419c9..0130d5a3b 100644 --- a/src/stdlib/SDL_qsort.c +++ b/src/stdlib/SDL_qsort.c @@ -51,12 +51,11 @@ #include "SDL_stdinc.h" #include "SDL_assert.h" -#ifdef SDL_qsort -#undef SDL_qsort +#if defined(HAVE_QSORT) void SDL_qsort(void *base, size_t nmemb, size_t size, int (*compare) (const void *, const void *)) { - SDL_qsort_inline(base, nmemb, size, compare); + qsort(base, nmemb, size, compare); } #else diff --git a/src/stdlib/SDL_stdlib.c b/src/stdlib/SDL_stdlib.c index 176aa240c..bff34ef63 100644 --- a/src/stdlib/SDL_stdlib.c +++ b/src/stdlib/SDL_stdlib.c @@ -23,24 +23,169 @@ /* This file contains portable stdlib functions for SDL */ #include "SDL_stdinc.h" +#include "../libm/math_libm.h" -/* these are always #defined, make them real symbol in the library, too... */ -#undef SDL_ceil -#undef SDL_abs -#undef SDL_sinf -#undef SDL_cosf -#undef SDL_isdigit -#undef SDL_isspace -#undef SDL_toupper -#undef SDL_tolower -double SDL_ceil(double x) { return SDL_ceil_inline(x); } -float SDL_cosf(float x) { return SDL_cosf_inline(x); } -float SDL_sinf(float x) { return SDL_sinf_inline(x); } -int SDL_abs(int x) { return SDL_abs_inline(x); } -int SDL_isdigit(int x) { return SDL_isdigit_inline(x); } -int SDL_isspace(int x) { return SDL_isspace_inline(x); } -int SDL_toupper(int x) { return SDL_toupper_inline(x); } -int SDL_tolower(int x) { return SDL_tolower_inline(x); } + +double +SDL_atan(double x) +{ +#ifdef HAVE_ATAN + return atan(x); +#else + return SDL_uclibc_atan(x); +#endif /* HAVE_ATAN */ +} + +double +SDL_atan2(double x, double y) +{ +#if defined(HAVE_ATAN2) + return atan2(x, y); +#else + return SDL_uclibc_atan2(x, y); +#endif /* HAVE_ATAN2 */ +} + +double +SDL_ceil(double x) +{ +#ifdef HAVE_CEIL + return ceil(x); +#else + return (double)(int)((x)+0.5); +#endif /* HAVE_CEIL */ +} + +double +SDL_copysign(double x, double y) +{ +#if defined(HAVE_COPYSIGN) + return copysign(x, y); +#else + return SDL_uclibc_copysign(x, y); +#endif /* HAVE_COPYSIGN */ +} + +double +SDL_cos(double x) +{ +#if defined(HAVE_COS) + return cos(x); +#else + return SDL_uclibc_cos(x); +#endif /* HAVE_COS */ +} + +float +SDL_cosf(float x) +{ +#ifdef HAVE_COSF + return cosf(x); +#else + return (float)SDL_cos((double)x); +#endif +} + +double +SDL_fabs(double x) +{ +#if defined(HAVE_FABS) + return fabs(x); +#else + return SDL_uclibc_fabs(x); +#endif /* HAVE_FABS */ +} + +double +SDL_floor(double x) +{ +#if defined(HAVE_FLOOR) + return floor(x); +#else + return SDL_uclibc_floor(x); +#endif /* HAVE_FLOOR */ +} + +double +SDL_log(double x) +{ +#if defined(HAVE_LOG) + return log(x); +#else + return SDL_uclibc_log(x); +#endif /* HAVE_LOG */ +} + +double +SDL_pow(double x, double y) +{ +#if defined(HAVE_POW) + return pow(x, y); +#else + return SDL_uclibc_pow(x, y); +#endif /* HAVE_POW */ +} + +double +SDL_scalbn(double x, int n) +{ +#if defined(HAVE_SCALBN) + return scalbn(x, n); +#else + return SDL_uclibc_scalbn(x, n); +#endif /* HAVE_SCALBN */ +} + +double +SDL_sin(double x) +{ +#if defined(HAVE_SIN) + return sin(x); +#else + return SDL_uclibc_sin(x); +#endif /* HAVE_SIN */ +} + +float +SDL_sinf(float x) +{ +#ifdef HAVE_SINF + return sinf(x); +#else + return (float)SDL_sin((double)x); +#endif /* HAVE_SINF */ +} + +double +SDL_sqrt(double x) +{ +#if defined(HAVE_SQRT) + return sqrt(x); +#else + return SDL_uclibc_sqrt(x); +#endif +} + +int SDL_abs(int x) +{ +#ifdef HAVE_ABS + return abs(x); +#else + return ((x) < 0 ? -(x) : (x)); +#endif +} + +#ifdef HAVE_CTYPE_H +int SDL_isdigit(int x) { return isdigit(x); } +int SDL_isspace(int x) { return isspace(x); } +int SDL_toupper(int x) { return toupper(x); } +int SDL_tolower(int x) { return tolower(x); } +#else +int SDL_isdigit(int x) { return ((x) >= '0') && ((x) <= '9'); } +int SDL_isspace(int x) { return ((x) == ' ') || ((x) == '\t') || ((x) == '\r') || ((x) == '\n'); } +int SDL_toupper(int x) { return ((x) >= 'a') && ((x) <= 'z') ? ('A'+((x)-'a')) : (x); } +int SDL_tolower(int x) { return ((x) >= 'A') && ((x) <= 'Z') ? ('a'+((x)-'A')) : (x); } +#endif #ifndef HAVE_LIBC diff --git a/src/stdlib/SDL_string.c b/src/stdlib/SDL_string.c index f2b2f31f8..7d117169b 100644 --- a/src/stdlib/SDL_string.c +++ b/src/stdlib/SDL_string.c @@ -24,17 +24,6 @@ #include "SDL_stdinc.h" -/* these are always #defined, make them real symbol in the library, too... */ -#undef SDL_itoa -#undef SDL_uitoa -#undef SDL_atoi -#undef SDL_atof -char *SDL_itoa(int value, char *str, int radix) { return SDL_itoa_inline(value, str, radix); } -char *SDL_uitoa(unsigned int value, char *str, int radix) { return SDL_uitoa_inline(value, str, radix); } -int SDL_atoi(const char *str) { return SDL_atoi_inline(str); } -double SDL_atof(const char *str) { return SDL_atof_inline(str); } - - #define SDL_isupperhex(X) (((X) >= 'A') && ((X) <= 'F')) #define SDL_islowerhex(X) (((X) >= 'a') && ((X) <= 'f')) @@ -54,7 +43,7 @@ static int UTF8_TrailingBytes(unsigned char c) return 0; } -#if !defined(SDL_sscanf) || !defined(SDL_strtol) +#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOL) static size_t SDL_ScanLong(const char *text, int radix, long *valuep) { @@ -95,7 +84,7 @@ SDL_ScanLong(const char *text, int radix, long *valuep) } #endif -#if !defined(SDL_sscanf) || !defined(SDL_strtoul) || !defined(SDL_strtod) +#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD) static size_t SDL_ScanUnsignedLong(const char *text, int radix, unsigned long *valuep) { @@ -127,7 +116,7 @@ SDL_ScanUnsignedLong(const char *text, int radix, unsigned long *valuep) } #endif -#ifndef SDL_sscanf +#ifndef HAVE_SSCANF static size_t SDL_ScanUintPtrT(const char *text, int radix, uintptr_t * valuep) { @@ -159,7 +148,7 @@ SDL_ScanUintPtrT(const char *text, int radix, uintptr_t * valuep) } #endif -#if !defined(SDL_sscanf) || !defined(SDL_strtoll) +#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOLL) static size_t SDL_ScanLongLong(const char *text, int radix, Sint64 * valuep) { @@ -200,7 +189,7 @@ SDL_ScanLongLong(const char *text, int radix, Sint64 * valuep) } #endif -#if !defined(SDL_sscanf) || !defined(SDL_strtoull) +#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOULL) static size_t SDL_ScanUnsignedLongLong(const char *text, int radix, Uint64 * valuep) { @@ -232,7 +221,7 @@ SDL_ScanUnsignedLongLong(const char *text, int radix, Uint64 * valuep) } #endif -#if !defined(SDL_sscanf) || !defined(SDL_strtod) +#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOD) static size_t SDL_ScanFloat(const char *text, double *valuep) { @@ -268,14 +257,12 @@ SDL_ScanFloat(const char *text, double *valuep) } #endif - -#ifdef SDL_memset -#undef SDL_memset -void *SDL_memset(void *dst, int c, size_t len) { return SDL_memset_inline(dst, c, len); } -#else void * SDL_memset(void *dst, int c, size_t len) { +#if defined(HAVE_MEMSET) + return memset(dst, c, len); +#else size_t left = (len % 4); Uint32 *dstp4; Uint8 *dstp1; @@ -299,14 +286,9 @@ SDL_memset(void *dst, int c, size_t len) } return dst; +#endif /* HAVE_MEMSET */ } -#endif - -#ifdef SDL_memcpy -#undef SDL_memcpy -void *SDL_memcpy(void *dst, const void *src, size_t len) { return SDL_memcpy_inline(dst, src, len); } -#else void * SDL_memcpy(void *dst, const void *src, size_t len) { @@ -315,6 +297,11 @@ SDL_memcpy(void *dst, const void *src, size_t len) On my machine this is twice as fast as the C code below. */ return __builtin_memcpy(dst, src, len); +#elif defined(HAVE_MEMCPY) + return memcpy(dst, src, len); +#elif defined(HAVE_BCOPY) + bcopy(src, dst, len); + return dst; #else /* GCC 4.9.0 with -O3 will generate movaps instructions with the loop using Uint32* pointers, so we need to make sure the pointers are @@ -354,16 +341,13 @@ SDL_memcpy(void *dst, const void *src, size_t len) return dst; #endif /* __GNUC__ */ } -#endif - -#ifdef SDL_memmove -#undef SDL_memmove -void *SDL_memmove(void *dst, const void *src, size_t len) { return SDL_memmove_inline(dst, src, len); } -#else void * SDL_memmove(void *dst, const void *src, size_t len) { +#if defined(HAVE_MEMMOVE) + return memmove(dst, src, len); +#else char *srcp = (char *) src; char *dstp = (char *) dst; @@ -379,16 +363,15 @@ SDL_memmove(void *dst, const void *src, size_t len) } } return dst; +#endif /* HAVE_MEMMOVE */ } -#endif -#ifdef SDL_memcmp -#undef SDL_memcmp -int SDL_memcmp(const void *s1, const void *s2, size_t len) { return SDL_memcmp_inline(s1, s2, len); } -#else int SDL_memcmp(const void *s1, const void *s2, size_t len) { +#if defined(HAVE_MEMCMP) + return memcmp(s1, s2, len); +#else char *s1p = (char *) s1; char *s2p = (char *) s2; while (len--) { @@ -399,46 +382,43 @@ SDL_memcmp(const void *s1, const void *s2, size_t len) ++s2p; } return 0; +#endif /* HAVE_MEMCMP */ } -#endif -#ifdef SDL_strlen -#undef SDL_strlen -size_t SDL_strlen(const char *string) { return SDL_strlen_inline(string); } -#else size_t SDL_strlen(const char *string) { +#if defined(HAVE_STRLEN) + return strlen(str); +#else size_t len = 0; while (*string++) { ++len; } return len; +#endif /* HAVE_STRLEN */ } -#endif -#ifdef SDL_wcslen -#undef SDL_wcslen -size_t SDL_wcslen(const wchar_t * string) { return SDL_wcslen_inline(string); } -#else size_t SDL_wcslen(const wchar_t * string) { +#if defined(HAVE_WCSLEN) + return wcslen(wstr); +#else size_t len = 0; while (*string++) { ++len; } return len; +#endif /* HAVE_WCSLEN */ } -#endif -#ifdef SDL_wcslcpy -#undef SDL_wcslcpy -size_t SDL_wcslcpy(wchar_t *dst, const wchar_t *src, size_t maxlen) { return SDL_wcslcpy_inline(dst, src, maxlen); } -#else size_t SDL_wcslcpy(wchar_t *dst, const wchar_t *src, size_t maxlen) { +#if defined(HAVE_WCSLCPY) + return wcslcpy(dst, src, maxlen); +#else size_t srclen = SDL_wcslen(src); if (maxlen > 0) { size_t len = SDL_min(srclen, maxlen - 1); @@ -446,32 +426,30 @@ SDL_wcslcpy(wchar_t *dst, const wchar_t *src, size_t maxlen) dst[len] = '\0'; } return srclen; +#endif /* HAVE_WCSLCPY */ } -#endif -#ifdef SDL_wcslcat -#undef SDL_wcslcat -size_t SDL_wcslcat(wchar_t *dst, const wchar_t *src, size_t maxlen) { return SDL_wcslcat_inline(dst, src, maxlen); } -#else size_t SDL_wcslcat(wchar_t *dst, const wchar_t *src, size_t maxlen) { +#if defined(HAVE_WCSLCAT) + return wcslcat(dst, src, maxlen); +#else size_t dstlen = SDL_wcslen(dst); size_t srclen = SDL_wcslen(src); if (dstlen < maxlen) { SDL_wcslcpy(dst + dstlen, src, maxlen - dstlen); } return dstlen + srclen; +#endif /* HAVE_WCSLCAT */ } -#endif -#ifdef SDL_strlcpy -#undef SDL_strlcpy -size_t SDL_strlcpy(char *dst, const char *src, size_t maxlen) { return SDL_strlcpy_inline(dst, src, maxlen); } -#else size_t SDL_strlcpy(char *dst, const char *src, size_t maxlen) { +#if defined(HAVE_STRLCPY) + return strlcpy(dst, src, maxlen); +#else size_t srclen = SDL_strlen(src); if (maxlen > 0) { size_t len = SDL_min(srclen, maxlen - 1); @@ -479,8 +457,8 @@ SDL_strlcpy(char *dst, const char *src, size_t maxlen) dst[len] = '\0'; } return srclen; +#endif /* HAVE_STRLCPY */ } -#endif size_t SDL_utf8strlcpy(char *dst, const char *src, size_t dst_bytes) { @@ -514,45 +492,42 @@ size_t SDL_utf8strlcpy(char *dst, const char *src, size_t dst_bytes) return bytes; } -#ifdef SDL_strlcat -#undef SDL_strlcat -size_t SDL_strlcat(char *dst, const char *src, size_t maxlen) { return SDL_strlcat_inline(dst, src, maxlen); } -#else size_t SDL_strlcat(char *dst, const char *src, size_t maxlen) { +#if defined(HAVE_STRLCAT) + return strlcat(dst, src, maxlen); +#else size_t dstlen = SDL_strlen(dst); size_t srclen = SDL_strlen(src); if (dstlen < maxlen) { SDL_strlcpy(dst + dstlen, src, maxlen - dstlen); } return dstlen + srclen; +#endif /* HAVE_STRLCAT */ } -#endif -#ifdef SDL_strdup -#undef SDL_strdup -char *SDL_strdup(const char *string) { return SDL_strdup_inline(string); } -#else char * SDL_strdup(const char *string) { +#if defined(HAVE_STRDUP) + return strdup(str); +#else size_t len = SDL_strlen(string) + 1; char *newstr = SDL_malloc(len); if (newstr) { SDL_strlcpy(newstr, string, len); } return newstr; +#endif /* HAVE_STRDUP */ } -#endif -#ifdef SDL_strrev -#undef SDL_strrev -char *SDL_strrev(char *string) { return SDL_strrev_inline(string); } -#else char * SDL_strrev(char *string) { +#if defined(HAVE__STRREV) + return _strrev(str); +#else size_t len = SDL_strlen(string); char *a = &string[0]; char *b = &string[len - 1]; @@ -563,48 +538,47 @@ SDL_strrev(char *string) *b-- = c; } return string; +#endif /* HAVE__STRREV */ } -#endif -#ifdef SDL_strupr -#undef SDL_strupr -char *SDL_strupr(char *string) { return SDL_strupr_inline(string); } -#else char * SDL_strupr(char *string) { +#if defined(HAVE__STRUPR) + return _strupr(str); +#else char *bufp = string; while (*bufp) { *bufp = SDL_toupper((unsigned char) *bufp); ++bufp; } return string; +#endif /* HAVE__STRUPR */ } -#endif -#ifdef SDL_strlwr -#undef SDL_strlwr -char *SDL_strlwr(char *string) { return SDL_strlwr_inline(string); } -#else char * SDL_strlwr(char *string) { +#if defined(HAVE__STRLWR) + return _strlwr(str); +#else char *bufp = string; while (*bufp) { *bufp = SDL_tolower((unsigned char) *bufp); ++bufp; } return string; +#endif /* HAVE__STRLWR */ } -#endif -#ifdef SDL_strchr -#undef SDL_strchr -char *SDL_strchr(const char *string, int c) { return SDL_strchr_inline(string, c); } -#else char * SDL_strchr(const char *string, int c) { +#ifdef HAVE_STRCHR + return SDL_const_cast(char*,strchr(str, c)); +#elif defined(HAVE_INDEX) + return SDL_const_cast(char*,index(str, c)); +#else while (*string) { if (*string == c) { return (char *) string; @@ -612,16 +586,17 @@ SDL_strchr(const char *string, int c) ++string; } return NULL; +#endif /* HAVE_STRCHR */ } -#endif -#ifdef SDL_strrchr -#undef SDL_strrchr -char *SDL_strrchr(const char *string, int c) { return SDL_strrchr_inline(string, c); } -#else char * SDL_strrchr(const char *string, int c) { +#ifdef HAVE_STRRCHR + return SDL_const_cast(char*,strrchr(str, c)); +#elif defined(HAVE_RINDEX) + return SDL_const_cast(char*,rindex(str, c)); +#else const char *bufp = string + SDL_strlen(string) - 1; while (bufp >= string) { if (*bufp == c) { @@ -630,16 +605,15 @@ SDL_strrchr(const char *string, int c) --bufp; } return NULL; +#endif /* HAVE_STRRCHR */ } -#endif -#ifdef SDL_strstr -#undef SDL_strstr -char *SDL_strstr(const char *haystack, const char *needle) { return SDL_strstr_inline(haystack, needle); } -#else char * SDL_strstr(const char *haystack, const char *needle) { +#if defined(HAVE_STRSTR) + return SDL_const_cast(char*,strstr(haystack, needle)); +#else size_t length = SDL_strlen(needle); while (*haystack) { if (SDL_strncmp(haystack, needle, length) == 0) { @@ -648,10 +622,10 @@ SDL_strstr(const char *haystack, const char *needle) ++haystack; } return NULL; +#endif /* HAVE_STRSTR */ } -#endif -#if !defined(SDL_ltoa) || !defined(SDL_lltoa) || \ +#if !defined(HAVE__LTOA) || !defined(SDL_lltoa) || \ !defined(SDL_ultoa) || !defined(SDL_ulltoa) static const char ntoa_table[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', @@ -661,13 +635,32 @@ static const char ntoa_table[] = { }; #endif /* ntoa() conversion table */ -#ifdef SDL_ltoa -#undef SDL_ltoa -char *SDL_ltoa(long value, char *string, int radix) { return SDL_ltoa_inline(value, string, radix); } +char * +SDL_itoa(int value, char *str, int radix) +{ +#ifdef HAVE_ITOA + return itoa(value, str, radix); #else + return SDL_ltoa((long)value, str, radix); +#endif /* HAVE_ITOA */ +} + +char * +SDL_uitoa(unsigned int value, char *str, int radix) +{ +#ifdef HAVE__UITOA + return _uitoa(value, str, radix); +#else + return SDL_ultoa((unsigned long)value, str, radix); +#endif /* HAVE__UITOA */ +} + char * SDL_ltoa(long value, char *string, int radix) { +#if defined(HAVE__LTOA) + return _ltoa(value, str, radix); +#else char *bufp = string; if (value < 0) { @@ -692,16 +685,15 @@ SDL_ltoa(long value, char *string, int radix) } return string; +#endif /* HAVE__LTOA */ } -#endif -#ifdef SDL_ultoa -#undef SDL_ultoa -char *SDL_ultoa(unsigned long value, char *string, int radix) { return SDL_ultoa_inline(value, string, radix); } -#else char * SDL_ultoa(unsigned long value, char *string, int radix) { +#if defined(HAVE__ULTOA) + return _ultoa(value, str, radix); +#else char *bufp = string; if (value) { @@ -718,68 +710,15 @@ SDL_ultoa(unsigned long value, char *string, int radix) SDL_strrev(string); return string; +#endif /* HAVE__ULTOA */ } -#endif - -#ifdef SDL_strtol -#undef SDL_strtol -long SDL_strtol(const char *string, char **endp, int base) { return SDL_strtol_inline(string, endp, base); } -#else -long -SDL_strtol(const char *string, char **endp, int base) -{ - size_t len; - long value; - - if (!base) { - if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) { - base = 16; - } else { - base = 10; - } - } - - len = SDL_ScanLong(string, base, &value); - if (endp) { - *endp = (char *) string + len; - } - return value; -} -#endif - -#ifdef SDL_strtoul -#undef SDL_strtoul -unsigned long SDL_strtoul(const char *string, char **endp, int base) { return SDL_strtoul_inline(string, endp, base); } -#else -unsigned long -SDL_strtoul(const char *string, char **endp, int base) -{ - size_t len; - unsigned long value; - - if (!base) { - if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) { - base = 16; - } else { - base = 10; - } - } - - len = SDL_ScanUnsignedLong(string, base, &value); - if (endp) { - *endp = (char *) string + len; - } - return value; -} -#endif -#ifdef SDL_lltoa -#undef SDL_lltoa -char *SDL_lltoa(Sint64 value, char *string, int radix) { return SDL_lltoa_inline(value, string, radix); } -#else char * SDL_lltoa(Sint64 value, char *string, int radix) { +#if defined(HAVE__I64TOA) + return _i64toa(value, str, radix); +#else char *bufp = string; if (value < 0) { @@ -804,16 +743,15 @@ SDL_lltoa(Sint64 value, char *string, int radix) } return string; +#endif /* HAVE__I64TOA */ } -#endif -#ifdef SDL_ulltoa -#undef SDL_ulltoa -char *SDL_ulltoa(Uint64 value, char *string, int radix) { return SDL_ulltoa_inline(value, string, radix); } -#else char * SDL_ulltoa(Uint64 value, char *string, int radix) { +#if defined(HAVE__UI64TOA) + return _ui64toa(value, str, radix); +#else char *bufp = string; if (value) { @@ -830,16 +768,83 @@ SDL_ulltoa(Uint64 value, char *string, int radix) SDL_strrev(string); return string; +#endif /* HAVE__UI64TOA */ +} + +int SDL_atoi(const char *str) +{ +#ifdef HAVE_ATOI + return atoi(str); +#else + return SDL_strtol(str, NULL, 0); +#endif /* HAVE_ATOI */ +} + +double SDL_atof(const char *str) +{ +#ifdef HAVE_ATOF + return (double) atof(str); +#else + return SDL_strtod(str, NULL); +#endif /* HAVE_ATOF */ +} + +long +SDL_strtol(const char *string, char **endp, int base) +{ +#if defined(HAVE_STRTOL) + return strtol(str, endp, base); +#else + size_t len; + long value; + + if (!base) { + if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) { + base = 16; + } else { + base = 10; + } + } + + len = SDL_ScanLong(string, base, &value); + if (endp) { + *endp = (char *) string + len; + } + return value; +#endif /* HAVE_STRTOL */ } -#endif -#ifdef SDL_strtoll -#undef SDL_strtoll -Sint64 SDL_strtoll(const char *string, char **endp, int base) { return SDL_strtoll_inline(string, endp, base); } +unsigned long +SDL_strtoul(const char *string, char **endp, int base) +{ +#if defined(HAVE_STRTOUL) + return strtoul(str, endp, base); #else + size_t len; + unsigned long value; + + if (!base) { + if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) { + base = 16; + } else { + base = 10; + } + } + + len = SDL_ScanUnsignedLong(string, base, &value); + if (endp) { + *endp = (char *) string + len; + } + return value; +#endif /* HAVE_STRTOUL */ +} + Sint64 SDL_strtoll(const char *string, char **endp, int base) { +#if defined(HAVE_STRTOLL) + return strtoll(str, endp, base); +#else size_t len; Sint64 value; @@ -856,16 +861,15 @@ SDL_strtoll(const char *string, char **endp, int base) *endp = (char *) string + len; } return value; +#endif /* HAVE_STRTOLL */ } -#endif -#ifdef SDL_strtoull -#undef SDL_strtoull -Uint64 SDL_strtoull(const char *string, char **endp, int base) { return SDL_strtoull_inline(string, endp, base); } -#else Uint64 SDL_strtoull(const char *string, char **endp, int base) { +#if defined(HAVE_STRTOULL) + return strtoull(str, endp, base); +#else size_t len; Uint64 value; @@ -882,16 +886,15 @@ SDL_strtoull(const char *string, char **endp, int base) *endp = (char *) string + len; } return value; +#endif /* HAVE_STRTOULL */ } -#endif -#ifdef SDL_strtod -#undef SDL_strtod -double SDL_strtod(const char *string, char **endp) { return SDL_strtod_inline(string, endp); } -#else double SDL_strtod(const char *string, char **endp) { +#if defined(HAVE_STRTOD) + return strtod(str, endp); +#else size_t len; double value; @@ -900,16 +903,15 @@ SDL_strtod(const char *string, char **endp) *endp = (char *) string + len; } return value; +#endif /* HAVE_STRTOD */ } -#endif -#ifdef SDL_strcmp -#undef SDL_strcmp -int SDL_strcmp(const char *str1, const char *str2) { return SDL_strcmp_inline(str1, str2); } -#else int SDL_strcmp(const char *str1, const char *str2) { +#if defined(HAVE_STRCMP) + return strcmp(str1, str2); +#else while (*str1 && *str2) { if (*str1 != *str2) break; @@ -917,16 +919,15 @@ SDL_strcmp(const char *str1, const char *str2) ++str2; } return (int) ((unsigned char) *str1 - (unsigned char) *str2); +#endif /* HAVE_STRCMP */ } -#endif -#ifdef SDL_strncmp -#undef SDL_strncmp -int SDL_strncmp(const char *str1, const char *str2, size_t maxlen) { return SDL_strncmp_inline(str1, str2, maxlen); } -#else int SDL_strncmp(const char *str1, const char *str2, size_t maxlen) { +#if defined(HAVE_STRNCMP) + return strncmp(str1, str2, maxlen); +#else while (*str1 && *str2 && maxlen) { if (*str1 != *str2) break; @@ -938,39 +939,41 @@ SDL_strncmp(const char *str1, const char *str2, size_t maxlen) return 0; } return (int) ((unsigned char) *str1 - (unsigned char) *str2); +#endif /* HAVE_STRNCMP */ } -#endif -#ifdef SDL_strcasecmp -#undef SDL_strcasecmp -int SDL_strcasecmp(const char *str1, const char *str2) { return SDL_strcasecmp_inline(str1, str2); } -#else int SDL_strcasecmp(const char *str1, const char *str2) { +#ifdef HAVE_STRCASECMP + return strcasecmp(str1, str2); +#elif defined(HAVE__STRICMP) + return _stricmp(str1, str2); +#else char a = 0; char b = 0; while (*str1 && *str2) { - a = SDL_tolower((unsigned char) *str1); - b = SDL_tolower((unsigned char) *str2); + a = SDL_toupper((unsigned char) *str1); + b = SDL_toupper((unsigned char) *str2); if (a != b) break; ++str1; ++str2; } - a = SDL_tolower(*str1); - b = SDL_tolower(*str2); + a = SDL_toupper(*str1); + b = SDL_toupper(*str2); return (int) ((unsigned char) a - (unsigned char) b); +#endif /* HAVE_STRCASECMP */ } -#endif -#ifdef SDL_strncasecmp -#undef SDL_strncasecmp -int SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen) { return SDL_strncasecmp_inline(str1, str2, maxlen); } -#else int SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen) { +#ifdef HAVE_STRNCASECMP + return strncasecmp(str1, str2, len); +#elif defined(HAVE__STRNICMP) + return _strnicmp(str1, str2, len); +#else char a = 0; char b = 0; while (*str1 && *str2 && maxlen) { @@ -989,11 +992,10 @@ SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen) b = SDL_tolower((unsigned char) *str2); return (int) ((unsigned char) a - (unsigned char) b); } +#endif /* HAVE_STRNCASECMP */ } -#endif -#ifdef SDL_sscanf -#undef SDL_sscanf +#ifdef HAVE_SSCANF int SDL_sscanf(const char *text, const char *fmt, ...) { @@ -1269,12 +1271,8 @@ SDL_sscanf(const char *text, const char *fmt, ...) return retval; } -#endif +#endif /* HAVE_SSCANF */ -/* just undef; the headers only define this to snprintf because of varargs. */ -#ifdef SDL_snprintf -#undef SDL_snprintf -#endif int SDL_snprintf(char *text, size_t maxlen, const char *fmt, ...) { @@ -1288,9 +1286,11 @@ SDL_snprintf(char *text, size_t maxlen, const char *fmt, ...) return retval; } -#ifdef SDL_vsnprintf -#undef SDL_vsnprintf -int SDL_vsnprintf(char *text, size_t maxlen, const char *fmt, va_list ap) { return SDL_vsnprintf_inline(text, maxlen, fmt, ap); } +#ifdef HAVE_VSNPRINTF +int SDL_vsnprintf(char *text, size_t maxlen, const char *fmt, va_list ap) +{ + return vsnprintf(text, maxlen, fmt, ap); +} #else /* FIXME: implement more of the format specifiers */ typedef struct @@ -1633,6 +1633,6 @@ SDL_vsnprintf(char *text, size_t maxlen, const char *fmt, va_list ap) } return (int)(text - textstart); } -#endif +#endif /* HAVE_VSNPRINTF */ /* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/video/SDL_RLEaccel.c b/src/video/SDL_RLEaccel.c index e1f12d13b..25ecd4556 100644 --- a/src/video/SDL_RLEaccel.c +++ b/src/video/SDL_RLEaccel.c @@ -98,13 +98,7 @@ #endif #define PIXEL_COPY(to, from, len, bpp) \ -do { \ - if(bpp == 4) { \ - SDL_memcpy4(to, from, (size_t)(len)); \ - } else { \ - SDL_memcpy(to, from, (size_t)(len) * (bpp)); \ - } \ -} while(0) + SDL_memcpy(to, from, (size_t)(len) * (bpp)) /* * Various colorkey blit methods, for opaque and per-surface alpha