3 * Standard C library function wrappers.
5 * Imports are services which the device driver or window system or
6 * operating system provides to the core renderer. The core renderer (Mesa)
7 * will call these functions in order to do memory allocation, simple I/O,
10 * Some drivers will want to override/replace this file with something
11 * specialized, but that'll be rare.
13 * Eventually, I want to move roll the glheader.h file into this.
15 * The OpenGL SI's __GLimports structure allows per-context specification of
16 * replacements for the standard C lib functions. In practice that's probably
17 * never needed; compile-time replacements are far more likely.
19 * The _mesa_*() functions defined here don't in general take a context
20 * parameter. I guess we can change that someday, if need be.
21 * So for now, the __GLimports stuff really isn't used.
23 * \todo Functions still needed:
28 * \note When compiled into a XFree86 module these functions wrap around
29 * XFree86 own wrappers.
33 * Mesa 3-D graphics library
36 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
38 * Permission is hereby granted, free of charge, to any person obtaining a
39 * copy of this software and associated documentation files (the "Software"),
40 * to deal in the Software without restriction, including without limitation
41 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
42 * and/or sell copies of the Software, and to permit persons to whom the
43 * Software is furnished to do so, subject to the following conditions:
45 * The above copyright notice and this permission notice shall be included
46 * in all copies or substantial portions of the Software.
48 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
49 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
50 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
51 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
52 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
53 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
63 #define MAXSTRING 4000 /* for vsnprintf() */
66 #define vsnprintf _vsnprintf
67 #elif defined(__IBMC__) || defined(__IBMCPP__) || ( defined(__VMS) && __CRTL_VER < 70312000 )
68 extern int vsnprintf(char *str
, size_t count
, const char *fmt
, va_list arg
);
70 #include "vsnprintf.c"
74 /* If we don't actually want to use the libcwrapper junk (even though we're
75 * building an Xorg server module), then just undef IN_MODULE to signal that to
76 * the following code. It's left around for now to allow compiling of newish
77 * Mesa with older servers, but this whole mess should go away at some point.
83 /**********************************************************************/
87 /** Wrapper around either malloc() or xf86malloc() */
89 _mesa_malloc(size_t bytes
)
91 #if defined(XFree86LOADER) && defined(IN_MODULE)
92 return xf86malloc(bytes
);
98 /** Wrapper around either calloc() or xf86calloc() */
100 _mesa_calloc(size_t bytes
)
102 #if defined(XFree86LOADER) && defined(IN_MODULE)
103 return xf86calloc(1, bytes
);
105 return calloc(1, bytes
);
109 /** Wrapper around either free() or xf86free() */
111 _mesa_free(void *ptr
)
113 #if defined(XFree86LOADER) && defined(IN_MODULE)
121 * Allocate aligned memory.
123 * \param bytes number of bytes to allocate.
124 * \param alignment alignment (must be greater than zero).
126 * Allocates extra memory to accommodate rounding up the address for
127 * alignment and to record the real malloc address.
129 * \sa _mesa_align_free().
132 _mesa_align_malloc(size_t bytes
, unsigned long alignment
)
134 #if defined(HAVE_POSIX_MEMALIGN) && !(defined(XFree86LOADER) && defined(IN_MODULE))
137 (void) posix_memalign(& mem
, alignment
, bytes
);
142 ASSERT( alignment
> 0 );
144 ptr
= (uintptr_t) _mesa_malloc(bytes
+ alignment
+ sizeof(void *));
148 buf
= (ptr
+ alignment
+ sizeof(void *)) & ~(uintptr_t)(alignment
- 1);
149 *(uintptr_t *)(buf
- sizeof(void *)) = ptr
;
152 /* mark the non-aligned area */
153 while ( ptr
< buf
- sizeof(void *) ) {
154 *(unsigned long *)ptr
= 0xcdcdcdcd;
155 ptr
+= sizeof(unsigned long);
160 #endif /* defined(HAVE_POSIX_MEMALIGN) && !(defined(XFree86LOADER) && defined(IN_MODULE)) */
164 * Same as _mesa_align_malloc(), but using _mesa_calloc() instead of
168 _mesa_align_calloc(size_t bytes
, unsigned long alignment
)
170 #if defined(HAVE_POSIX_MEMALIGN) && !(defined(XFree86LOADER) && defined(IN_MODULE))
173 mem
= _mesa_align_malloc(bytes
, alignment
);
175 (void) memset(mem
, 0, bytes
);
182 ASSERT( alignment
> 0 );
184 ptr
= (uintptr_t) _mesa_calloc(bytes
+ alignment
+ sizeof(void *));
188 buf
= (ptr
+ alignment
+ sizeof(void *)) & ~(uintptr_t)(alignment
- 1);
189 *(uintptr_t *)(buf
- sizeof(void *)) = ptr
;
192 /* mark the non-aligned area */
193 while ( ptr
< buf
- sizeof(void *) ) {
194 *(unsigned long *)ptr
= 0xcdcdcdcd;
195 ptr
+= sizeof(unsigned long);
200 #endif /* defined(HAVE_POSIX_MEMALIGN) && !(defined(XFree86LOADER) && defined(IN_MODULE)) */
204 * Free memory which was allocated with either _mesa_align_malloc()
205 * or _mesa_align_calloc().
206 * \param ptr pointer to the memory to be freed.
207 * The actual address to free is stored in the word immediately before the
208 * address the client sees.
211 _mesa_align_free(void *ptr
)
213 #if defined(HAVE_POSIX_MEMALIGN) && !(defined(XFree86LOADER) && defined(IN_MODULE))
216 void **cubbyHole
= (void **) ((char *) ptr
- sizeof(void *));
217 void *realAddr
= *cubbyHole
;
218 _mesa_free(realAddr
);
219 #endif /* defined(HAVE_POSIX_MEMALIGN) && !(defined(XFree86LOADER) && defined(IN_MODULE)) */
223 * Reallocate memory, with alignment.
226 _mesa_align_realloc(void *oldBuffer
, size_t oldSize
, size_t newSize
,
227 unsigned long alignment
)
229 const size_t copySize
= (oldSize
< newSize
) ? oldSize
: newSize
;
230 void *newBuf
= _mesa_align_malloc(newSize
, alignment
);
231 if (newBuf
&& oldBuffer
&& copySize
> 0) {
232 _mesa_memcpy(newBuf
, oldBuffer
, copySize
);
235 _mesa_align_free(oldBuffer
);
241 /** Reallocate memory */
243 _mesa_realloc(void *oldBuffer
, size_t oldSize
, size_t newSize
)
245 const size_t copySize
= (oldSize
< newSize
) ? oldSize
: newSize
;
246 void *newBuffer
= _mesa_malloc(newSize
);
247 if (newBuffer
&& oldBuffer
&& copySize
> 0)
248 _mesa_memcpy(newBuffer
, oldBuffer
, copySize
);
250 _mesa_free(oldBuffer
);
254 /** memcpy wrapper */
256 _mesa_memcpy(void *dest
, const void *src
, size_t n
)
258 #if defined(XFree86LOADER) && defined(IN_MODULE)
259 return xf86memcpy(dest
, src
, n
);
260 #elif defined(SUNOS4)
261 return memcpy((char *) dest
, (char *) src
, (int) n
);
263 return memcpy(dest
, src
, n
);
267 /** Wrapper around either memset() or xf86memset() */
269 _mesa_memset( void *dst
, int val
, size_t n
)
271 #if defined(XFree86LOADER) && defined(IN_MODULE)
272 xf86memset( dst
, val
, n
);
273 #elif defined(SUNOS4)
274 memset( (char *) dst
, (int) val
, (int) n
);
281 * Fill memory with a constant 16bit word.
282 * \param dst destination pointer.
284 * \param n number of words.
287 _mesa_memset16( unsigned short *dst
, unsigned short val
, size_t n
)
293 /** Wrapper around either memcpy() or xf86memcpy() or bzero() */
295 _mesa_bzero( void *dst
, size_t n
)
297 #if defined(XFree86LOADER) && defined(IN_MODULE)
298 xf86memset( dst
, 0, n
);
299 #elif defined(__FreeBSD__)
306 /** Wrapper around either memcmp() or xf86memcmp() */
308 _mesa_memcmp( const void *s1
, const void *s2
, size_t n
)
310 #if defined(XFree86LOADER) && defined(IN_MODULE)
311 return xf86memcmp( s1
, s2
, n
);
312 #elif defined(SUNOS4)
313 return memcmp( (char *) s1
, (char *) s2
, (int) n
);
315 return memcmp(s1
, s2
, n
);
322 /**********************************************************************/
326 /** Wrapper around either sin() or xf86sin() */
330 #if defined(XFree86LOADER) && defined(IN_MODULE)
337 /** Single precision wrapper around either sin() or xf86sin() */
341 #if defined(XFree86LOADER) && defined(IN_MODULE)
342 return (float) xf86sin((double) a
);
344 return (float) sin((double) a
);
348 /** Wrapper around either cos() or xf86cos() */
352 #if defined(XFree86LOADER) && defined(IN_MODULE)
359 /** Single precision wrapper around either asin() or xf86asin() */
363 #if defined(XFree86LOADER) && defined(IN_MODULE)
364 return (float) xf86asin((double) x
);
366 return (float) asin((double) x
);
370 /** Single precision wrapper around either atan() or xf86atan() */
374 #if defined(XFree86LOADER) && defined(IN_MODULE)
375 return (float) xf86atan((double) x
);
377 return (float) atan((double) x
);
381 /** Wrapper around either sqrt() or xf86sqrt() */
383 _mesa_sqrtd(double x
)
385 #if defined(XFree86LOADER) && defined(IN_MODULE)
394 * A High Speed, Low Precision Square Root
395 * by Paul Lalonde and Robert Dawson
396 * from "Graphics Gems", Academic Press, 1990
398 * SPARC implementation of a fast square root by table
400 * SPARC floating point format is as follows:
403 * sign exponent mantissa
405 static short sqrttab
[0x100]; /* declare table of square roots */
407 static void init_sqrt_table(void)
409 #if defined(USE_IEEE) && !defined(DEBUG)
411 fi_type fi
; /* to access the bits of a float in C quickly */
412 /* we use a union defined in glheader.h */
414 for(i
=0; i
<= 0x7f; i
++) {
418 * Build a float with the bit pattern i as mantissa
419 * and an exponent of 0, stored as 127
422 fi
.i
= (i
<< 16) | (127 << 23);
423 fi
.f
= _mesa_sqrtd(fi
.f
);
426 * Take the square root then strip the first 7 bits of
427 * the mantissa into the table
430 sqrttab
[i
] = (fi
.i
& 0x7fffff) >> 16;
433 * Repeat the process, this time with an exponent of
438 fi
.i
= (i
<< 16) | (128 << 23);
440 sqrttab
[i
+0x80] = (fi
.i
& 0x7fffff) >> 16;
443 (void) sqrttab
; /* silence compiler warnings */
444 #endif /*HAVE_FAST_MATH*/
449 * Single precision square root.
452 _mesa_sqrtf( float x
)
454 #if defined(USE_IEEE) && !defined(DEBUG)
456 /* to access the bits of a float in C
457 * we use a union from glheader.h */
459 short e
; /* the exponent */
460 if (x
== 0.0F
) return 0.0F
; /* check for square root of 0 */
462 e
= (num
.i
>> 23) - 127; /* get the exponent - on a SPARC the */
463 /* exponent is stored with 127 added */
464 num
.i
&= 0x7fffff; /* leave only the mantissa */
465 if (e
& 0x01) num
.i
|= 0x800000;
466 /* the exponent is odd so we have to */
467 /* look it up in the second half of */
468 /* the lookup table, so we set the */
470 e
>>= 1; /* divide the exponent by two */
471 /* note that in C the shift */
472 /* operators are sign preserving */
473 /* for signed operands */
474 /* Do the table lookup, based on the quaternary mantissa,
475 * then reconstruct the result back into a float
477 num
.i
= ((sqrttab
[num
.i
>> 16]) << 16) | ((e
+ 127) << 23);
481 return (float) _mesa_sqrtd((double) x
);
487 inv_sqrt - A single precision 1/sqrt routine for IEEE format floats.
488 written by Josh Vanderhoof, based on newsgroup posts by James Van Buskirk
492 _mesa_inv_sqrtf(float n
)
494 #if defined(USE_IEEE) && !defined(DEBUG)
498 #if 0 /* not used, see below -BP */
501 union { float f
; unsigned int i
; } u
;
505 Exponent part of the magic number -
508 1. subtract the bias from the exponent,
510 3. divide by two (rounding towards -inf)
513 Which is the same as subtracting the exponent from 381 and dividing
516 floor(-(x - 127) / 2) + 127 = floor((381 - x) / 2)
522 Significand part of magic number -
524 With the current magic number, "(magic - u.i) >> 1" will give you:
526 for 1 <= u.f <= 2: 1.25 - u.f / 4
527 for 2 <= u.f <= 4: 1.00 - u.f / 8
529 This isn't a bad approximation of 1/sqrt. The maximum difference from
530 1/sqrt will be around .06. After three Newton-Raphson iterations, the
531 maximum difference is less than 4.5e-8. (Which is actually close
532 enough to make the following bias academic...)
534 To get a better approximation you can add a bias to the magic
535 number. For example, if you subtract 1/2 of the maximum difference in
536 the first approximation (.03), you will get the following function:
538 for 1 <= u.f <= 2: 1.22 - u.f / 4
539 for 2 <= u.f <= 3.76: 0.97 - u.f / 8
540 for 3.76 <= u.f <= 4: 0.72 - u.f / 16
541 (The 3.76 to 4 range is where the result is < .5.)
543 This is the closest possible initial approximation, but with a maximum
544 error of 8e-11 after three NR iterations, it is still not perfect. If
545 you subtract 0.0332281 instead of .03, the maximum error will be
546 2.5e-11 after three NR iterations, which should be about as close as
549 for 1 <= u.f <= 2: 1.2167719 - u.f / 4
550 for 2 <= u.f <= 3.73: 0.9667719 - u.f / 8
551 for 3.73 <= u.f <= 4: 0.7167719 - u.f / 16
555 magic
-= (int)(0.0332281 * (1 << 25));
558 u
.i
= (magic
- u
.i
) >> 1;
561 Instead of Newton-Raphson, we use Goldschmidt's algorithm, which
562 allows more parallelism. From what I understand, the parallelism
563 comes at the cost of less precision, because it lets error
564 accumulate across iterations.
579 return x2
* r2
; /* we can stop here, and be conformant -BP */
587 #elif defined(XFree86LOADER) && defined(IN_MODULE)
588 return 1.0F
/ xf86sqrt(n
);
590 return (float) (1.0 / sqrt(n
));
596 * Wrapper around either pow() or xf86pow().
599 _mesa_pow(double x
, double y
)
601 #if defined(XFree86LOADER) && defined(IN_MODULE)
602 return xf86pow(x
, y
);
610 * Find the first bit set in a word.
615 #if (defined(_WIN32) && !defined(__MINGW32__) ) || defined(__IBMC__) || defined(__IBMCPP__)
616 register int bit
= 0;
618 if ((i
& 0xffff) == 0) {
622 if ((i
& 0xff) == 0) {
626 if ((i
& 0xf) == 0) {
630 while ((i
& 1) == 0) {
636 #elif defined(XFree86LOADER) && defined(IN_MODULE)
645 * Return number of bits set in given GLuint.
648 _mesa_bitcount(unsigned int n
)
651 for (bits
= 0; n
> 0; n
= n
>> 1) {
659 * Convert a 4-byte float to a 2-byte half float.
660 * Based on code from:
661 * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
664 _mesa_float_to_half(float val
)
666 const int flt
= *((int *) (void *) &val
);
667 const int flt_m
= flt
& 0x7fffff;
668 const int flt_e
= (flt
>> 23) & 0xff;
669 const int flt_s
= (flt
>> 31) & 0x1;
676 /* handle special cases */
677 if ((flt_e
== 0) && (flt_m
== 0)) {
679 /* m = 0; - already set */
682 else if ((flt_e
== 0) && (flt_m
!= 0)) {
683 /* denorm -- denorm float maps to 0 half */
684 /* m = 0; - already set */
687 else if ((flt_e
== 0xff) && (flt_m
== 0)) {
689 /* m = 0; - already set */
692 else if ((flt_e
== 0xff) && (flt_m
!= 0)) {
699 const int new_exp
= flt_e
- 127;
702 /* m = 0; - already set */
705 else if (new_exp
< -14) {
706 /* this maps to a denorm */
707 unsigned int exp_val
= (unsigned int) (-14 - new_exp
); /* 2^-exp_val*/
712 "float_to_half: logical error in denorm creation!\n");
713 /* m = 0; - already set */
715 case 1: m
= 512 + (flt_m
>> 14); break;
716 case 2: m
= 256 + (flt_m
>> 15); break;
717 case 3: m
= 128 + (flt_m
>> 16); break;
718 case 4: m
= 64 + (flt_m
>> 17); break;
719 case 5: m
= 32 + (flt_m
>> 18); break;
720 case 6: m
= 16 + (flt_m
>> 19); break;
721 case 7: m
= 8 + (flt_m
>> 20); break;
722 case 8: m
= 4 + (flt_m
>> 21); break;
723 case 9: m
= 2 + (flt_m
>> 22); break;
724 case 10: m
= 1; break;
727 else if (new_exp
> 15) {
728 /* map this value to infinity */
729 /* m = 0; - already set */
739 result
= (s
<< 15) | (e
<< 10) | m
;
745 * Convert a 2-byte half float to a 4-byte float.
746 * Based on code from:
747 * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
750 _mesa_half_to_float(GLhalfARB val
)
752 /* XXX could also use a 64K-entry lookup table */
753 const int m
= val
& 0x3ff;
754 const int e
= (val
>> 10) & 0x1f;
755 const int s
= (val
>> 15) & 0x1;
756 int flt_m
, flt_e
, flt_s
, flt
;
762 /* handle special cases */
763 if ((e
== 0) && (m
== 0)) {
768 else if ((e
== 0) && (m
!= 0)) {
769 /* denorm -- denorm half will fit in non-denorm single */
770 const float half_denorm
= 1.0f
/ 16384.0f
; /* 2^-14 */
771 float mantissa
= ((float) (m
)) / 1024.0f
;
772 float sign
= s
? -1.0f
: 1.0f
;
773 return sign
* mantissa
* half_denorm
;
775 else if ((e
== 31) && (m
== 0)) {
780 else if ((e
== 31) && (m
!= 0)) {
791 flt
= (flt_s
<< 31) | (flt_e
<< 23) | flt_m
;
792 result
= *((float *) (void *) &flt
);
799 /**********************************************************************/
800 /** \name Sort & Search */
804 * Wrapper for bsearch().
807 _mesa_bsearch( const void *key
, const void *base
, size_t nmemb
, size_t size
,
808 int (*compar
)(const void *, const void *) )
810 #if defined(XFree86LOADER) && defined(IN_MODULE)
811 return xf86bsearch(key
, base
, nmemb
, size
, compar
);
813 return bsearch(key
, base
, nmemb
, size
, compar
);
820 /**********************************************************************/
821 /** \name Environment vars */
825 * Wrapper for getenv().
828 _mesa_getenv( const char *var
)
830 #if defined(XFree86LOADER) && defined(IN_MODULE)
831 return xf86getenv(var
);
842 /**********************************************************************/
846 /** Wrapper around either strstr() or xf86strstr() */
848 _mesa_strstr( const char *haystack
, const char *needle
)
850 #if defined(XFree86LOADER) && defined(IN_MODULE)
851 return xf86strstr(haystack
, needle
);
853 return strstr(haystack
, needle
);
857 /** Wrapper around either strncat() or xf86strncat() */
859 _mesa_strncat( char *dest
, const char *src
, size_t n
)
861 #if defined(XFree86LOADER) && defined(IN_MODULE)
862 return xf86strncat(dest
, src
, n
);
864 return strncat(dest
, src
, n
);
868 /** Wrapper around either strcpy() or xf86strcpy() */
870 _mesa_strcpy( char *dest
, const char *src
)
872 #if defined(XFree86LOADER) && defined(IN_MODULE)
873 return xf86strcpy(dest
, src
);
875 return strcpy(dest
, src
);
879 /** Wrapper around either strncpy() or xf86strncpy() */
881 _mesa_strncpy( char *dest
, const char *src
, size_t n
)
883 #if defined(XFree86LOADER) && defined(IN_MODULE)
884 return xf86strncpy(dest
, src
, n
);
886 return strncpy(dest
, src
, n
);
890 /** Wrapper around either strlen() or xf86strlen() */
892 _mesa_strlen( const char *s
)
894 #if defined(XFree86LOADER) && defined(IN_MODULE)
895 return xf86strlen(s
);
901 /** Wrapper around either strcmp() or xf86strcmp() */
903 _mesa_strcmp( const char *s1
, const char *s2
)
905 #if defined(XFree86LOADER) && defined(IN_MODULE)
906 return xf86strcmp(s1
, s2
);
908 return strcmp(s1
, s2
);
912 /** Wrapper around either strncmp() or xf86strncmp() */
914 _mesa_strncmp( const char *s1
, const char *s2
, size_t n
)
916 #if defined(XFree86LOADER) && defined(IN_MODULE)
917 return xf86strncmp(s1
, s2
, n
);
919 return strncmp(s1
, s2
, n
);
923 /** Implemented using _mesa_malloc() and _mesa_strcpy */
925 _mesa_strdup( const char *s
)
927 size_t l
= _mesa_strlen(s
);
928 char *s2
= (char *) _mesa_malloc(l
+ 1);
934 /** Wrapper around either atoi() or xf86atoi() */
936 _mesa_atoi(const char *s
)
938 #if defined(XFree86LOADER) && defined(IN_MODULE)
945 /** Wrapper around either strtod() or xf86strtod() */
947 _mesa_strtod( const char *s
, char **end
)
949 #if defined(XFree86LOADER) && defined(IN_MODULE)
950 return xf86strtod(s
, end
);
952 return strtod(s
, end
);
959 /**********************************************************************/
963 /** Wrapper around either vsprintf() or xf86vsprintf() */
965 _mesa_sprintf( char *str
, const char *fmt
, ... )
969 va_start( args
, fmt
);
970 #if defined(XFree86LOADER) && defined(IN_MODULE)
971 r
= xf86vsprintf( str
, fmt
, args
);
973 r
= vsprintf( str
, fmt
, args
);
979 /** Wrapper around either printf() or xf86printf(), using vsprintf() for
982 _mesa_printf( const char *fmtString
, ... )
986 va_start( args
, fmtString
);
987 vsnprintf(s
, MAXSTRING
, fmtString
, args
);
989 #if defined(XFree86LOADER) && defined(IN_MODULE)
992 fprintf(stderr
,"%s", s
);
996 /** Wrapper around either vsprintf() or xf86vsprintf() */
998 _mesa_vsprintf( char *str
, const char *fmt
, va_list args
)
1000 #if defined(XFree86LOADER) && defined(IN_MODULE)
1001 return xf86vsprintf( str
, fmt
, args
);
1003 return vsprintf( str
, fmt
, args
);
1010 /**********************************************************************/
1011 /** \name Diagnostics */
1015 * Display a warning.
1017 * \param ctx GL context.
1018 * \param fmtString printf() alike format string.
1020 * If debugging is enabled (either at compile-time via the DEBUG macro, or
1021 * run-time via the MESA_DEBUG environment variable), prints the warning to
1022 * stderr, either via fprintf() or xf86printf().
1025 _mesa_warning( GLcontext
*ctx
, const char *fmtString
, ... )
1028 char str
[MAXSTRING
];
1031 va_start( args
, fmtString
);
1032 (void) vsnprintf( str
, MAXSTRING
, fmtString
, args
);
1035 debug
= GL_TRUE
; /* always print warning */
1037 debug
= _mesa_getenv("MESA_DEBUG") ? GL_TRUE
: GL_FALSE
;
1040 #if defined(XFree86LOADER) && defined(IN_MODULE)
1041 xf86fprintf(stderr
, "Mesa warning: %s\n", str
);
1043 fprintf(stderr
, "Mesa warning: %s\n", str
);
1049 * This function is called when the Mesa user has stumbled into a code
1050 * path which may not be implemented fully or correctly.
1052 * \param ctx GL context.
1053 * \param s problem description string.
1055 * Prints the message to stderr, either via fprintf() or xf86fprintf().
1058 _mesa_problem( const GLcontext
*ctx
, const char *fmtString
, ... )
1061 char str
[MAXSTRING
];
1064 va_start( args
, fmtString
);
1065 vsnprintf( str
, MAXSTRING
, fmtString
, args
);
1068 #if defined(XFree86LOADER) && defined(IN_MODULE)
1069 xf86fprintf(stderr
, "Mesa %s implementation error: %s\n", MESA_VERSION_STRING
, str
);
1070 xf86fprintf(stderr
, "Please report at bugzilla.freedesktop.org\n");
1072 fprintf(stderr
, "Mesa %s implementation error: %s\n", MESA_VERSION_STRING
, str
);
1073 fprintf(stderr
, "Please report at bugzilla.freedesktop.org\n");
1078 * Display an error message.
1080 * If in debug mode, print error message.
1081 * Also, record the error code by calling _mesa_record_error().
1083 * \param ctx the GL context.
1084 * \param error the error value.
1085 * \param fmtString printf() style format string, followed by optional args
1087 * If debugging is enabled (either at compile-time via the DEBUG macro, or
1088 * run-time via the MESA_DEBUG environment variable), interperts the error code and
1089 * prints the error message via _mesa_debug().
1092 _mesa_error( GLcontext
*ctx
, GLenum error
, const char *fmtString
, ... )
1094 const char *debugEnv
;
1097 debugEnv
= _mesa_getenv("MESA_DEBUG");
1100 if (debugEnv
&& _mesa_strstr(debugEnv
, "silent"))
1113 char where
[MAXSTRING
];
1116 va_start( args
, fmtString
);
1117 vsnprintf( where
, MAXSTRING
, fmtString
, args
);
1122 errstr
= "GL_NO_ERROR";
1124 case GL_INVALID_VALUE
:
1125 errstr
= "GL_INVALID_VALUE";
1127 case GL_INVALID_ENUM
:
1128 errstr
= "GL_INVALID_ENUM";
1130 case GL_INVALID_OPERATION
:
1131 errstr
= "GL_INVALID_OPERATION";
1133 case GL_STACK_OVERFLOW
:
1134 errstr
= "GL_STACK_OVERFLOW";
1136 case GL_STACK_UNDERFLOW
:
1137 errstr
= "GL_STACK_UNDERFLOW";
1139 case GL_OUT_OF_MEMORY
:
1140 errstr
= "GL_OUT_OF_MEMORY";
1142 case GL_TABLE_TOO_LARGE
:
1143 errstr
= "GL_TABLE_TOO_LARGE";
1145 case GL_INVALID_FRAMEBUFFER_OPERATION_EXT
:
1146 errstr
= "GL_INVALID_FRAMEBUFFER_OPERATION";
1152 _mesa_debug(ctx
, "User error: %s in %s\n", errstr
, where
);
1155 _mesa_record_error(ctx
, error
);
1159 * Report debug information.
1161 * \param ctx GL context.
1162 * \param fmtString printf() alike format string.
1164 * Prints the message to stderr, either via fprintf() or xf86printf().
1167 _mesa_debug( const GLcontext
*ctx
, const char *fmtString
, ... )
1172 va_start(args
, fmtString
);
1173 vsnprintf(s
, MAXSTRING
, fmtString
, args
);
1175 #if defined(XFree86LOADER) && defined(IN_MODULE)
1176 xf86fprintf(stderr
, "Mesa: %s", s
);
1178 fprintf(stderr
, "Mesa: %s", s
);
1188 /**********************************************************************/
1193 * Wrapper for exit().
1196 _mesa_exit( int status
)
1198 #if defined(XFree86LOADER) && defined(IN_MODULE)
1208 /**********************************************************************/
1209 /** \name Default Imports Wrapper */
1212 /** Wrapper around _mesa_malloc() */
1214 default_malloc(__GLcontext
*gc
, size_t size
)
1217 return _mesa_malloc(size
);
1220 /** Wrapper around _mesa_malloc() */
1222 default_calloc(__GLcontext
*gc
, size_t numElem
, size_t elemSize
)
1225 return _mesa_calloc(numElem
* elemSize
);
1228 /** Wrapper around either realloc() or xf86realloc() */
1230 default_realloc(__GLcontext
*gc
, void *oldAddr
, size_t newSize
)
1233 #if defined(XFree86LOADER) && defined(IN_MODULE)
1234 return xf86realloc(oldAddr
, newSize
);
1236 return realloc(oldAddr
, newSize
);
1240 /** Wrapper around _mesa_free() */
1242 default_free(__GLcontext
*gc
, void *addr
)
1248 /** Wrapper around _mesa_getenv() */
1250 default_getenv( __GLcontext
*gc
, const char *var
)
1253 return _mesa_getenv(var
);
1256 /** Wrapper around _mesa_warning() */
1258 default_warning(__GLcontext
*gc
, char *str
)
1260 _mesa_warning(gc
, str
);
1263 /** Wrapper around _mesa_problem() */
1265 default_fatal(__GLcontext
*gc
, char *str
)
1267 _mesa_problem(gc
, str
);
1271 /** Wrapper around atoi() */
1273 default_atoi(__GLcontext
*gc
, const char *str
)
1279 /** Wrapper around vsprintf() */
1281 default_sprintf(__GLcontext
*gc
, char *str
, const char *fmt
, ...)
1286 va_start( args
, fmt
);
1287 r
= vsprintf( str
, fmt
, args
);
1292 /** Wrapper around fopen() */
1294 default_fopen(__GLcontext
*gc
, const char *path
, const char *mode
)
1297 return fopen(path
, mode
);
1300 /** Wrapper around fclose() */
1302 default_fclose(__GLcontext
*gc
, void *stream
)
1305 return fclose((FILE *) stream
);
1308 /** Wrapper around vfprintf() */
1310 default_fprintf(__GLcontext
*gc
, void *stream
, const char *fmt
, ...)
1315 va_start( args
, fmt
);
1316 r
= vfprintf( (FILE *) stream
, fmt
, args
);
1322 * \todo this really is driver-specific and can't be here
1324 static __GLdrawablePrivate
*
1325 default_GetDrawablePrivate(__GLcontext
*gc
)
1335 * Initialize a __GLimports object to point to the functions in this
1338 * This is to be called from device drivers.
1340 * Also, do some one-time initializations.
1342 * \param imports the object to initialize.
1343 * \param driverCtx pointer to device driver-specific data.
1346 _mesa_init_default_imports(__GLimports
*imports
, void *driverCtx
)
1348 /* XXX maybe move this one-time init stuff into context.c */
1349 static GLboolean initialized
= GL_FALSE
;
1352 initialized
= GL_TRUE
;
1355 imports
->malloc
= default_malloc
;
1356 imports
->calloc
= default_calloc
;
1357 imports
->realloc
= default_realloc
;
1358 imports
->free
= default_free
;
1359 imports
->warning
= default_warning
;
1360 imports
->fatal
= default_fatal
;
1361 imports
->getenv
= default_getenv
; /* not used for now */
1362 imports
->atoi
= default_atoi
;
1363 imports
->sprintf
= default_sprintf
;
1364 imports
->fopen
= default_fopen
;
1365 imports
->fclose
= default_fclose
;
1366 imports
->fprintf
= default_fprintf
;
1367 imports
->getDrawablePrivate
= default_GetDrawablePrivate
;
1368 imports
->other
= driverCtx
;