Merge branch 'lp-offset-twoside'
[mesa.git] / src / mesa / main / imports.c
index 6ffaddcde96e7f1996736847ac9eb49b4c48b958..bcca4edc1aa77dd3b898a1c5ea355f93672adba0 100644 (file)
 #include "context.h"
 #include "version.h"
 
+#ifdef _GNU_SOURCE
+#include <locale.h>
+#ifdef __APPLE__
+#include <xlocale.h>
+#endif
+#endif
+
 
 #define MAXSTRING 4000  /* for vsnprintf() */
 
@@ -64,27 +71,6 @@ extern int vsnprintf(char *str, size_t count, const char *fmt, va_list arg);
 /** \name Memory */
 /*@{*/
 
-/** Wrapper around malloc() */
-void *
-_mesa_malloc(size_t bytes)
-{
-   return malloc(bytes);
-}
-
-/** Wrapper around calloc() */
-void *
-_mesa_calloc(size_t bytes)
-{
-   return calloc(1, bytes);
-}
-
-/** Wrapper around free() */
-void
-_mesa_free(void *ptr)
-{
-   free(ptr);
-}
-
 /**
  * Allocate aligned memory.
  *
@@ -101,8 +87,8 @@ _mesa_align_malloc(size_t bytes, unsigned long alignment)
 {
 #if defined(HAVE_POSIX_MEMALIGN)
    void *mem;
-
-   (void) posix_memalign(& mem, alignment, bytes);
+   int err = posix_memalign(& mem, alignment, bytes);
+   (void) err;
    return mem;
 #elif defined(_WIN32) && defined(_MSC_VER)
    return _aligned_malloc(bytes, alignment);
@@ -111,7 +97,7 @@ _mesa_align_malloc(size_t bytes, unsigned long alignment)
 
    ASSERT( alignment > 0 );
 
-   ptr = (uintptr_t) _mesa_malloc(bytes + alignment + sizeof(void *));
+   ptr = (uintptr_t) malloc(bytes + alignment + sizeof(void *));
    if (!ptr)
       return NULL;
 
@@ -131,8 +117,8 @@ _mesa_align_malloc(size_t bytes, unsigned long alignment)
 }
 
 /**
- * Same as _mesa_align_malloc(), but using _mesa_calloc() instead of
- * _mesa_malloc()
+ * Same as _mesa_align_malloc(), but using calloc(1, ) instead of
+ * malloc()
  */
 void *
 _mesa_align_calloc(size_t bytes, unsigned long alignment)
@@ -160,7 +146,7 @@ _mesa_align_calloc(size_t bytes, unsigned long alignment)
 
    ASSERT( alignment > 0 );
 
-   ptr = (uintptr_t) _mesa_calloc(bytes + alignment + sizeof(void *));
+   ptr = (uintptr_t) calloc(1, bytes + alignment + sizeof(void *));
    if (!ptr)
       return NULL;
 
@@ -196,7 +182,7 @@ _mesa_align_free(void *ptr)
 #else
    void **cubbyHole = (void **) ((char *) ptr - sizeof(void *));
    void *realAddr = *cubbyHole;
-   _mesa_free(realAddr);
+   free(realAddr);
 #endif /* defined(HAVE_POSIX_MEMALIGN) */
 }
 
@@ -214,7 +200,7 @@ _mesa_align_realloc(void *oldBuffer, size_t oldSize, size_t newSize,
    const size_t copySize = (oldSize < newSize) ? oldSize : newSize;
    void *newBuf = _mesa_align_malloc(newSize, alignment);
    if (newBuf && oldBuffer && copySize > 0) {
-      _mesa_memcpy(newBuf, oldBuffer, copySize);
+      memcpy(newBuf, oldBuffer, copySize);
    }
    if (oldBuffer)
       _mesa_align_free(oldBuffer);
@@ -229,36 +215,14 @@ void *
 _mesa_realloc(void *oldBuffer, size_t oldSize, size_t newSize)
 {
    const size_t copySize = (oldSize < newSize) ? oldSize : newSize;
-   void *newBuffer = _mesa_malloc(newSize);
+   void *newBuffer = malloc(newSize);
    if (newBuffer && oldBuffer && copySize > 0)
-      _mesa_memcpy(newBuffer, oldBuffer, copySize);
+      memcpy(newBuffer, oldBuffer, copySize);
    if (oldBuffer)
-      _mesa_free(oldBuffer);
+      free(oldBuffer);
    return newBuffer;
 }
 
-/** memcpy wrapper */
-void *
-_mesa_memcpy(void *dest, const void *src, size_t n)
-{
-#if defined(SUNOS4)
-   return memcpy((char *) dest, (char *) src, (int) n);
-#else
-   return memcpy(dest, src, n);
-#endif
-}
-
-/** Wrapper around memset() */
-void
-_mesa_memset( void *dst, int val, size_t n )
-{
-#if defined(SUNOS4)
-   memset( (char *) dst, (int) val, (int) n );
-#else
-   memset(dst, val, n);
-#endif
-}
-
 /**
  * Fill memory with a constant 16bit word.
  * \param dst destination pointer.
@@ -272,28 +236,6 @@ _mesa_memset16( unsigned short *dst, unsigned short val, size_t n )
       *dst++ = val;
 }
 
-/** Wrapper around either memset() or bzero() */
-void
-_mesa_bzero( void *dst, size_t n )
-{
-#if defined(__FreeBSD__)
-   bzero( dst, n );
-#else
-   memset( dst, 0, n );
-#endif
-}
-
-/** Wrapper around memcmp() */
-int
-_mesa_memcmp( const void *s1, const void *s2, size_t n )
-{
-#if defined(SUNOS4)
-   return memcmp( (char *) s1, (char *) s2, (int) n );
-#else
-   return memcmp(s1, s2, n);
-#endif
-}
-
 /*@}*/
 
 
@@ -301,41 +243,6 @@ _mesa_memcmp( const void *s1, const void *s2, size_t n )
 /** \name Math */
 /*@{*/
 
-/** Wrapper around sin() */
-double
-_mesa_sin(double a)
-{
-   return sin(a);
-}
-
-/** Single precision wrapper around sin() */
-float
-_mesa_sinf(float a)
-{
-   return (float) sin((double) a);
-}
-
-/** Wrapper around cos() */
-double
-_mesa_cos(double a)
-{
-   return cos(a);
-}
-
-/** Single precision wrapper around asin() */
-float
-_mesa_asinf(float x)
-{
-   return (float) asin((double) x);
-}
-
-/** Single precision wrapper around atan() */
-float
-_mesa_atanf(float x)
-{
-   return (float) atan((double) x);
-}
-
 /** Wrapper around sqrt() */
 double
 _mesa_sqrtd(double x)
@@ -453,7 +360,7 @@ _mesa_inv_sqrtf(float n)
 #if 0 /* not used, see below -BP */
         float r3, x3, y3;
 #endif
-        union { float f; unsigned int i; } u;
+        fi_type u;
         unsigned int magic;
 
         /*
@@ -544,15 +451,6 @@ _mesa_inv_sqrtf(float n)
 #endif
 }
 
-
-/** Wrapper around pow() */
-double
-_mesa_pow(double x, double y)
-{
-   return pow(x, y);
-}
-
-
 /**
  * Find the first bit set in a word.
  */
@@ -622,11 +520,16 @@ _mesa_ffsll(int64_t val)
 unsigned int
 _mesa_bitcount(unsigned int n)
 {
+#if defined(__GNUC__) && \
+       ((_GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ >= 4)
+   return __builtin_popcount(n);
+#else
    unsigned int bits;
    for (bits = 0; n > 0; n = n >> 1) {
       bits += (n & 1);
    }
    return bits;
+#endif
 }
 
 
@@ -638,10 +541,10 @@ _mesa_bitcount(unsigned int n)
 GLhalfARB
 _mesa_float_to_half(float val)
 {
-   const int flt = *((int *) (void *) &val);
-   const int flt_m = flt & 0x7fffff;
-   const int flt_e = (flt >> 23) & 0xff;
-   const int flt_s = (flt >> 31) & 0x1;
+   const fi_type fi = {val};
+   const int flt_m = fi.i & 0x7fffff;
+   const int flt_e = (fi.i >> 23) & 0xff;
+   const int flt_s = (fi.i >> 31) & 0x1;
    int s, e, m = 0;
    GLhalfARB result;
    
@@ -728,7 +631,8 @@ _mesa_half_to_float(GLhalfARB val)
    const int m = val & 0x3ff;
    const int e = (val >> 10) & 0x1f;
    const int s = (val >> 15) & 0x1;
-   int flt_m, flt_e, flt_s, flt;
+   int flt_m, flt_e, flt_s;
+   fi_type fi;
    float result;
 
    /* sign bit */
@@ -763,8 +667,8 @@ _mesa_half_to_float(GLhalfARB val)
       flt_m = m << 13;
    }
 
-   flt = (flt_s << 31) | (flt_e << 23) | flt_m;
-   result = *((float *) (void *) &flt);
+   fi.i = (flt_s << 31) | (flt_e << 23) | flt_m;
+   result = fi.f;
    return result;
 }
 
@@ -829,67 +733,18 @@ _mesa_getenv( const char *var )
 /** \name String */
 /*@{*/
 
-/** Wrapper around strstr() */
-char *
-_mesa_strstr( const char *haystack, const char *needle )
-{
-   return strstr(haystack, needle);
-}
-
-/** Wrapper around strncat() */
-char *
-_mesa_strncat( char *dest, const char *src, size_t n )
-{
-   return strncat(dest, src, n);
-}
-
-/** Wrapper around strcpy() */
-char *
-_mesa_strcpy( char *dest, const char *src )
-{
-   return strcpy(dest, src);
-}
-
-/** Wrapper around strncpy() */
-char *
-_mesa_strncpy( char *dest, const char *src, size_t n )
-{
-   return strncpy(dest, src, n);
-}
-
-/** Wrapper around strlen() */
-size_t
-_mesa_strlen( const char *s )
-{
-   return strlen(s);
-}
-
-/** Wrapper around strcmp() */
-int
-_mesa_strcmp( const char *s1, const char *s2 )
-{
-   return strcmp(s1, s2);
-}
-
-/** Wrapper around strncmp() */
-int
-_mesa_strncmp( const char *s1, const char *s2, size_t n )
-{
-   return strncmp(s1, s2, n);
-}
-
 /**
- * Implemented using _mesa_malloc() and _mesa_strcpy.
+ * Implemented using malloc() and strcpy.
  * Note that NULL is handled accordingly.
  */
 char *
 _mesa_strdup( const char *s )
 {
    if (s) {
-      size_t l = _mesa_strlen(s);
-      char *s2 = (char *) _mesa_malloc(l + 1);
+      size_t l = strlen(s);
+      char *s2 = (char *) malloc(l + 1);
       if (s2)
-         _mesa_strcpy(s2, s);
+         strcpy(s2, s);
       return s2;
    }
    else {
@@ -897,18 +752,21 @@ _mesa_strdup( const char *s )
    }
 }
 
-/** Wrapper around atoi() */
-int
-_mesa_atoi(const char *s)
-{
-   return atoi(s);
-}
-
-/** Wrapper around strtod() */
-double
-_mesa_strtod( const char *s, char **end )
+/** Wrapper around strtof() */
+float
+_mesa_strtof( const char *s, char **end )
 {
-   return strtod(s, end);
+#if defined(_GNU_SOURCE) && !defined(__CYGWIN__) && !defined(__FreeBSD__)
+   static locale_t loc = NULL;
+   if (!loc) {
+      loc = newlocale(LC_CTYPE_MASK, "C", NULL);
+   }
+   return strtof_l(s, end, loc);
+#elif defined(_ISOC99_SOURCE) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
+   return strtof(s, end);
+#else
+   return (float)strtod(s, end);
+#endif
 }
 
 /** Compute simple checksum/hash for a string */
@@ -919,31 +777,15 @@ _mesa_str_checksum(const char *str)
    unsigned int sum, i;
    const char *c;
    sum = i = 1;
-   for (c = str; *c; c++)
+   for (c = str; *c; c++, i++)
       sum += *c * (i % 100);
-   return sum;
+   return sum + i;
 }
 
 
 /*@}*/
 
 
-/**********************************************************************/
-/** \name I/O */
-/*@{*/
-
-/** Wrapper around vsprintf() */
-int
-_mesa_sprintf( char *str, const char *fmt, ... )
-{
-   int r;
-   va_list args;
-   va_start( args, fmt );  
-   r = vsprintf( str, fmt, args );
-   va_end( args );
-   return r;
-}
-
 /** Wrapper around vsnprintf() */
 int
 _mesa_snprintf( char *str, size_t size, const char *fmt, ... )
@@ -956,38 +798,6 @@ _mesa_snprintf( char *str, size_t size, const char *fmt, ... )
    return r;
 }
 
-/** Wrapper around printf(), using vsprintf() for the formatting. */
-void
-_mesa_printf( const char *fmtString, ... )
-{
-   va_list args;
-   va_start( args, fmtString );  
-   vfprintf(stderr, fmtString, args);
-   va_end( args );
-}
-
-/** Wrapper around fprintf(), using vsprintf() for the formatting. */
-void
-_mesa_fprintf( FILE *f, const char *fmtString, ... )
-{
-   char s[MAXSTRING];
-   va_list args;
-   va_start( args, fmtString );  
-   vsnprintf(s, MAXSTRING, fmtString, args);
-   va_end( args );
-   fprintf(f, "%s", s);
-}
-
-
-/** Wrapper around vsprintf() */
-int
-_mesa_vsprintf( char *str, const char *fmt, va_list args )
-{
-   return vsprintf( str, fmt, args );
-}
-
-/*@}*/
-
 
 /**********************************************************************/
 /** \name Diagnostics */
@@ -1011,7 +821,7 @@ output_if_debug(const char *prefixString, const char *outputString,
        * set *to any value*.
        */
 #ifdef DEBUG
-      debug = (env != NULL && _mesa_atoi(env) == 0) ? 0 : 1;
+      debug = (env != NULL && atoi(env) == 0) ? 0 : 1;
 #else
       debug = (env != NULL) ? 1 : 0;
 #endif
@@ -1072,7 +882,7 @@ error_string( GLenum error )
  * previous errors which were accumulated.
  */
 static void
-flush_delayed_errors( GLcontext *ctx )
+flush_delayed_errors( struct gl_context *ctx )
 {
    char s[MAXSTRING];
 
@@ -1096,7 +906,7 @@ flush_delayed_errors( GLcontext *ctx )
  * \param fmtString printf()-like format string.
  */
 void
-_mesa_warning( GLcontext *ctx, const char *fmtString, ... )
+_mesa_warning( struct gl_context *ctx, const char *fmtString, ... )
 {
    char str[MAXSTRING];
    va_list args;
@@ -1119,7 +929,7 @@ _mesa_warning( GLcontext *ctx, const char *fmtString, ... )
  * \param fmtString problem description string.
  */
 void
-_mesa_problem( const GLcontext *ctx, const char *fmtString, ... )
+_mesa_problem( const struct gl_context *ctx, const char *fmtString, ... )
 {
    va_list args;
    char str[MAXSTRING];
@@ -1147,7 +957,7 @@ _mesa_problem( const GLcontext *ctx, const char *fmtString, ... )
  * \param fmtString printf() style format string, followed by optional args
  */
 void
-_mesa_error( GLcontext *ctx, GLenum error, const char *fmtString, ... )
+_mesa_error( struct gl_context *ctx, GLenum error, const char *fmtString, ... )
 {
    static GLint debug = -1;
 
@@ -1157,7 +967,7 @@ _mesa_error( GLcontext *ctx, GLenum error, const char *fmtString, ... )
       const char *debugEnv = _mesa_getenv("MESA_DEBUG");
 
 #ifdef DEBUG
-      if (debugEnv && _mesa_strstr(debugEnv, "silent"))
+      if (debugEnv && strstr(debugEnv, "silent"))
          debug = GL_FALSE;
       else
          debug = GL_TRUE;
@@ -1204,7 +1014,7 @@ _mesa_error( GLcontext *ctx, GLenum error, const char *fmtString, ... )
  * \param fmtString printf()-style format string, followed by optional args.
  */
 void
-_mesa_debug( const GLcontext *ctx, const char *fmtString, ... )
+_mesa_debug( const struct gl_context *ctx, const char *fmtString, ... )
 {
 #ifdef DEBUG
    char s[MAXSTRING];
@@ -1219,13 +1029,3 @@ _mesa_debug( const GLcontext *ctx, const char *fmtString, ... )
 }
 
 /*@}*/
-
-
-/**
- * Wrapper for exit().
- */
-void
-_mesa_exit( int status )
-{
-   exit(status);
-}