Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Removed the inline functions from SDL_stdinc.h
Browse files Browse the repository at this point in the history
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
  • Loading branch information
slouken committed Jul 6, 2013
1 parent 4d179f3 commit 21e334d
Show file tree
Hide file tree
Showing 9 changed files with 460 additions and 779 deletions.
457 changes: 13 additions & 444 deletions include/SDL_stdinc.h

Large diffs are not rendered by default.

77 changes: 11 additions & 66 deletions src/libm/math_libm.h
Expand Up @@ -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: */
12 changes: 12 additions & 0 deletions src/libm/math_private.h
Expand Up @@ -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 *
Expand Down
18 changes: 12 additions & 6 deletions src/stdlib/SDL_getenv.c
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
31 changes: 21 additions & 10 deletions src/stdlib/SDL_malloc.c
Expand Up @@ -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. :) */

Expand Down
5 changes: 2 additions & 3 deletions src/stdlib/SDL_qsort.c
Expand Up @@ -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

Expand Down
179 changes: 162 additions & 17 deletions src/stdlib/SDL_stdlib.c
Expand Up @@ -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
Expand Down

0 comments on commit 21e334d

Please sign in to comment.