Fix a number of MINGW32 issues
[mesa.git] / src / mesa / main / imports.c
1 /**
2 * \file imports.c
3 * Standard C library function wrappers.
4 *
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,
8 * etc.
9 *
10 * Some drivers will want to override/replace this file with something
11 * specialized, but that'll be rare.
12 *
13 * Eventually, I want to move roll the glheader.h file into this.
14 *
15 * \todo Functions still needed:
16 * - scanf
17 * - qsort
18 * - rand and RAND_MAX
19 */
20
21 /*
22 * Mesa 3-D graphics library
23 * Version: 7.1
24 *
25 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
26 *
27 * Permission is hereby granted, free of charge, to any person obtaining a
28 * copy of this software and associated documentation files (the "Software"),
29 * to deal in the Software without restriction, including without limitation
30 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
31 * and/or sell copies of the Software, and to permit persons to whom the
32 * Software is furnished to do so, subject to the following conditions:
33 *
34 * The above copyright notice and this permission notice shall be included
35 * in all copies or substantial portions of the Software.
36 *
37 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
38 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
39 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
40 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
41 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
42 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
43 */
44
45
46
47 #include "imports.h"
48 #include "context.h"
49 #include "version.h"
50
51
52 #define MAXSTRING 4000 /* for vsnprintf() */
53
54 #ifdef WIN32
55 #define vsnprintf _vsnprintf
56 #elif defined(__IBMC__) || defined(__IBMCPP__) || ( defined(__VMS) && __CRTL_VER < 70312000 )
57 extern int vsnprintf(char *str, size_t count, const char *fmt, va_list arg);
58 #ifdef __VMS
59 #include "vsnprintf.c"
60 #endif
61 #endif
62
63 /**********************************************************************/
64 /** \name Memory */
65 /*@{*/
66
67 /** Wrapper around malloc() */
68 void *
69 _mesa_malloc(size_t bytes)
70 {
71 return malloc(bytes);
72 }
73
74 /** Wrapper around calloc() */
75 void *
76 _mesa_calloc(size_t bytes)
77 {
78 return calloc(1, bytes);
79 }
80
81 /** Wrapper around free() */
82 void
83 _mesa_free(void *ptr)
84 {
85 free(ptr);
86 }
87
88 /**
89 * Allocate aligned memory.
90 *
91 * \param bytes number of bytes to allocate.
92 * \param alignment alignment (must be greater than zero).
93 *
94 * Allocates extra memory to accommodate rounding up the address for
95 * alignment and to record the real malloc address.
96 *
97 * \sa _mesa_align_free().
98 */
99 void *
100 _mesa_align_malloc(size_t bytes, unsigned long alignment)
101 {
102 #if defined(HAVE_POSIX_MEMALIGN)
103 void *mem;
104
105 (void) posix_memalign(& mem, alignment, bytes);
106 return mem;
107 #else
108 uintptr_t ptr, buf;
109
110 ASSERT( alignment > 0 );
111
112 ptr = (uintptr_t) _mesa_malloc(bytes + alignment + sizeof(void *));
113 if (!ptr)
114 return NULL;
115
116 buf = (ptr + alignment + sizeof(void *)) & ~(uintptr_t)(alignment - 1);
117 *(uintptr_t *)(buf - sizeof(void *)) = ptr;
118
119 #ifdef DEBUG
120 /* mark the non-aligned area */
121 while ( ptr < buf - sizeof(void *) ) {
122 *(unsigned long *)ptr = 0xcdcdcdcd;
123 ptr += sizeof(unsigned long);
124 }
125 #endif
126
127 return (void *) buf;
128 #endif /* defined(HAVE_POSIX_MEMALIGN) */
129 }
130
131 /**
132 * Same as _mesa_align_malloc(), but using _mesa_calloc() instead of
133 * _mesa_malloc()
134 */
135 void *
136 _mesa_align_calloc(size_t bytes, unsigned long alignment)
137 {
138 #if defined(HAVE_POSIX_MEMALIGN)
139 void *mem;
140
141 mem = _mesa_align_malloc(bytes, alignment);
142 if (mem != NULL) {
143 (void) memset(mem, 0, bytes);
144 }
145
146 return mem;
147 #else
148 uintptr_t ptr, buf;
149
150 ASSERT( alignment > 0 );
151
152 ptr = (uintptr_t) _mesa_calloc(bytes + alignment + sizeof(void *));
153 if (!ptr)
154 return NULL;
155
156 buf = (ptr + alignment + sizeof(void *)) & ~(uintptr_t)(alignment - 1);
157 *(uintptr_t *)(buf - sizeof(void *)) = ptr;
158
159 #ifdef DEBUG
160 /* mark the non-aligned area */
161 while ( ptr < buf - sizeof(void *) ) {
162 *(unsigned long *)ptr = 0xcdcdcdcd;
163 ptr += sizeof(unsigned long);
164 }
165 #endif
166
167 return (void *)buf;
168 #endif /* defined(HAVE_POSIX_MEMALIGN) */
169 }
170
171 /**
172 * Free memory which was allocated with either _mesa_align_malloc()
173 * or _mesa_align_calloc().
174 * \param ptr pointer to the memory to be freed.
175 * The actual address to free is stored in the word immediately before the
176 * address the client sees.
177 */
178 void
179 _mesa_align_free(void *ptr)
180 {
181 #if defined(HAVE_POSIX_MEMALIGN)
182 free(ptr);
183 #else
184 void **cubbyHole = (void **) ((char *) ptr - sizeof(void *));
185 void *realAddr = *cubbyHole;
186 _mesa_free(realAddr);
187 #endif /* defined(HAVE_POSIX_MEMALIGN) */
188 }
189
190 /**
191 * Reallocate memory, with alignment.
192 */
193 void *
194 _mesa_align_realloc(void *oldBuffer, size_t oldSize, size_t newSize,
195 unsigned long alignment)
196 {
197 const size_t copySize = (oldSize < newSize) ? oldSize : newSize;
198 void *newBuf = _mesa_align_malloc(newSize, alignment);
199 if (newBuf && oldBuffer && copySize > 0) {
200 _mesa_memcpy(newBuf, oldBuffer, copySize);
201 }
202 if (oldBuffer)
203 _mesa_align_free(oldBuffer);
204 return newBuf;
205 }
206
207
208
209 /** Reallocate memory */
210 void *
211 _mesa_realloc(void *oldBuffer, size_t oldSize, size_t newSize)
212 {
213 const size_t copySize = (oldSize < newSize) ? oldSize : newSize;
214 void *newBuffer = _mesa_malloc(newSize);
215 if (newBuffer && oldBuffer && copySize > 0)
216 _mesa_memcpy(newBuffer, oldBuffer, copySize);
217 if (oldBuffer)
218 _mesa_free(oldBuffer);
219 return newBuffer;
220 }
221
222 /** memcpy wrapper */
223 void *
224 _mesa_memcpy(void *dest, const void *src, size_t n)
225 {
226 #if defined(SUNOS4)
227 return memcpy((char *) dest, (char *) src, (int) n);
228 #else
229 return memcpy(dest, src, n);
230 #endif
231 }
232
233 /** Wrapper around memset() */
234 void
235 _mesa_memset( void *dst, int val, size_t n )
236 {
237 #if defined(SUNOS4)
238 memset( (char *) dst, (int) val, (int) n );
239 #else
240 memset(dst, val, n);
241 #endif
242 }
243
244 /**
245 * Fill memory with a constant 16bit word.
246 * \param dst destination pointer.
247 * \param val value.
248 * \param n number of words.
249 */
250 void
251 _mesa_memset16( unsigned short *dst, unsigned short val, size_t n )
252 {
253 while (n-- > 0)
254 *dst++ = val;
255 }
256
257 /** Wrapper around either memset() or bzero() */
258 void
259 _mesa_bzero( void *dst, size_t n )
260 {
261 #if defined(__FreeBSD__)
262 bzero( dst, n );
263 #else
264 memset( dst, 0, n );
265 #endif
266 }
267
268 /** Wrapper around memcmp() */
269 int
270 _mesa_memcmp( const void *s1, const void *s2, size_t n )
271 {
272 #if defined(SUNOS4)
273 return memcmp( (char *) s1, (char *) s2, (int) n );
274 #else
275 return memcmp(s1, s2, n);
276 #endif
277 }
278
279 /*@}*/
280
281
282 /**********************************************************************/
283 /** \name Math */
284 /*@{*/
285
286 /** Wrapper around sin() */
287 double
288 _mesa_sin(double a)
289 {
290 return sin(a);
291 }
292
293 /** Single precision wrapper around sin() */
294 float
295 _mesa_sinf(float a)
296 {
297 return (float) sin((double) a);
298 }
299
300 /** Wrapper around cos() */
301 double
302 _mesa_cos(double a)
303 {
304 return cos(a);
305 }
306
307 /** Single precision wrapper around asin() */
308 float
309 _mesa_asinf(float x)
310 {
311 return (float) asin((double) x);
312 }
313
314 /** Single precision wrapper around atan() */
315 float
316 _mesa_atanf(float x)
317 {
318 return (float) atan((double) x);
319 }
320
321 /** Wrapper around sqrt() */
322 double
323 _mesa_sqrtd(double x)
324 {
325 return sqrt(x);
326 }
327
328
329 /*
330 * A High Speed, Low Precision Square Root
331 * by Paul Lalonde and Robert Dawson
332 * from "Graphics Gems", Academic Press, 1990
333 *
334 * SPARC implementation of a fast square root by table
335 * lookup.
336 * SPARC floating point format is as follows:
337 *
338 * BIT 31 30 23 22 0
339 * sign exponent mantissa
340 */
341 static short sqrttab[0x100]; /* declare table of square roots */
342
343 void
344 _mesa_init_sqrt_table(void)
345 {
346 #if defined(USE_IEEE) && !defined(DEBUG)
347 unsigned short i;
348 fi_type fi; /* to access the bits of a float in C quickly */
349 /* we use a union defined in glheader.h */
350
351 for(i=0; i<= 0x7f; i++) {
352 fi.i = 0;
353
354 /*
355 * Build a float with the bit pattern i as mantissa
356 * and an exponent of 0, stored as 127
357 */
358
359 fi.i = (i << 16) | (127 << 23);
360 fi.f = _mesa_sqrtd(fi.f);
361
362 /*
363 * Take the square root then strip the first 7 bits of
364 * the mantissa into the table
365 */
366
367 sqrttab[i] = (fi.i & 0x7fffff) >> 16;
368
369 /*
370 * Repeat the process, this time with an exponent of
371 * 1, stored as 128
372 */
373
374 fi.i = 0;
375 fi.i = (i << 16) | (128 << 23);
376 fi.f = sqrt(fi.f);
377 sqrttab[i+0x80] = (fi.i & 0x7fffff) >> 16;
378 }
379 #else
380 (void) sqrttab; /* silence compiler warnings */
381 #endif /*HAVE_FAST_MATH*/
382 }
383
384
385 /**
386 * Single precision square root.
387 */
388 float
389 _mesa_sqrtf( float x )
390 {
391 #if defined(USE_IEEE) && !defined(DEBUG)
392 fi_type num;
393 /* to access the bits of a float in C
394 * we use a union from glheader.h */
395
396 short e; /* the exponent */
397 if (x == 0.0F) return 0.0F; /* check for square root of 0 */
398 num.f = x;
399 e = (num.i >> 23) - 127; /* get the exponent - on a SPARC the */
400 /* exponent is stored with 127 added */
401 num.i &= 0x7fffff; /* leave only the mantissa */
402 if (e & 0x01) num.i |= 0x800000;
403 /* the exponent is odd so we have to */
404 /* look it up in the second half of */
405 /* the lookup table, so we set the */
406 /* high bit */
407 e >>= 1; /* divide the exponent by two */
408 /* note that in C the shift */
409 /* operators are sign preserving */
410 /* for signed operands */
411 /* Do the table lookup, based on the quaternary mantissa,
412 * then reconstruct the result back into a float
413 */
414 num.i = ((sqrttab[num.i >> 16]) << 16) | ((e + 127) << 23);
415
416 return num.f;
417 #else
418 return (float) _mesa_sqrtd((double) x);
419 #endif
420 }
421
422
423 /**
424 inv_sqrt - A single precision 1/sqrt routine for IEEE format floats.
425 written by Josh Vanderhoof, based on newsgroup posts by James Van Buskirk
426 and Vesa Karvonen.
427 */
428 float
429 _mesa_inv_sqrtf(float n)
430 {
431 #if defined(USE_IEEE) && !defined(DEBUG)
432 float r0, x0, y0;
433 float r1, x1, y1;
434 float r2, x2, y2;
435 #if 0 /* not used, see below -BP */
436 float r3, x3, y3;
437 #endif
438 union { float f; unsigned int i; } u;
439 unsigned int magic;
440
441 /*
442 Exponent part of the magic number -
443
444 We want to:
445 1. subtract the bias from the exponent,
446 2. negate it
447 3. divide by two (rounding towards -inf)
448 4. add the bias back
449
450 Which is the same as subtracting the exponent from 381 and dividing
451 by 2.
452
453 floor(-(x - 127) / 2) + 127 = floor((381 - x) / 2)
454 */
455
456 magic = 381 << 23;
457
458 /*
459 Significand part of magic number -
460
461 With the current magic number, "(magic - u.i) >> 1" will give you:
462
463 for 1 <= u.f <= 2: 1.25 - u.f / 4
464 for 2 <= u.f <= 4: 1.00 - u.f / 8
465
466 This isn't a bad approximation of 1/sqrt. The maximum difference from
467 1/sqrt will be around .06. After three Newton-Raphson iterations, the
468 maximum difference is less than 4.5e-8. (Which is actually close
469 enough to make the following bias academic...)
470
471 To get a better approximation you can add a bias to the magic
472 number. For example, if you subtract 1/2 of the maximum difference in
473 the first approximation (.03), you will get the following function:
474
475 for 1 <= u.f <= 2: 1.22 - u.f / 4
476 for 2 <= u.f <= 3.76: 0.97 - u.f / 8
477 for 3.76 <= u.f <= 4: 0.72 - u.f / 16
478 (The 3.76 to 4 range is where the result is < .5.)
479
480 This is the closest possible initial approximation, but with a maximum
481 error of 8e-11 after three NR iterations, it is still not perfect. If
482 you subtract 0.0332281 instead of .03, the maximum error will be
483 2.5e-11 after three NR iterations, which should be about as close as
484 is possible.
485
486 for 1 <= u.f <= 2: 1.2167719 - u.f / 4
487 for 2 <= u.f <= 3.73: 0.9667719 - u.f / 8
488 for 3.73 <= u.f <= 4: 0.7167719 - u.f / 16
489
490 */
491
492 magic -= (int)(0.0332281 * (1 << 25));
493
494 u.f = n;
495 u.i = (magic - u.i) >> 1;
496
497 /*
498 Instead of Newton-Raphson, we use Goldschmidt's algorithm, which
499 allows more parallelism. From what I understand, the parallelism
500 comes at the cost of less precision, because it lets error
501 accumulate across iterations.
502 */
503 x0 = 1.0f;
504 y0 = 0.5f * n;
505 r0 = u.f;
506
507 x1 = x0 * r0;
508 y1 = y0 * r0 * r0;
509 r1 = 1.5f - y1;
510
511 x2 = x1 * r1;
512 y2 = y1 * r1 * r1;
513 r2 = 1.5f - y2;
514
515 #if 1
516 return x2 * r2; /* we can stop here, and be conformant -BP */
517 #else
518 x3 = x2 * r2;
519 y3 = y2 * r2 * r2;
520 r3 = 1.5f - y3;
521
522 return x3 * r3;
523 #endif
524 #else
525 return (float) (1.0 / sqrt(n));
526 #endif
527 }
528
529
530 /** Wrapper around pow() */
531 double
532 _mesa_pow(double x, double y)
533 {
534 return pow(x, y);
535 }
536
537
538 /**
539 * Find the first bit set in a word.
540 */
541 int
542 _mesa_ffs(int i)
543 {
544 #if (defined(_WIN32) && !defined(__MINGW32__) ) || defined(__IBMC__) || defined(__IBMCPP__)
545 register int bit = 0;
546 if (i != 0) {
547 if ((i & 0xffff) == 0) {
548 bit += 16;
549 i >>= 16;
550 }
551 if ((i & 0xff) == 0) {
552 bit += 8;
553 i >>= 8;
554 }
555 if ((i & 0xf) == 0) {
556 bit += 4;
557 i >>= 4;
558 }
559 while ((i & 1) == 0) {
560 bit++;
561 i >>= 1;
562 }
563 }
564 return bit;
565 #else
566 return ffs(i);
567 #endif
568 }
569
570
571 /**
572 * Find position of first bit set in given value.
573 * XXX Warning: this function can only be used on 64-bit systems!
574 * \return position of least-significant bit set, starting at 1, return zero
575 * if no bits set.
576 */
577 int
578 #ifdef __MINGW32__
579 _mesa_ffsll(long val)
580 #else
581 _mesa_ffsll(long long val)
582 #endif
583 {
584 #ifdef ffsll
585 return ffsll(val);
586 #else
587 int bit;
588
589 assert(sizeof(val) == 8);
590
591 bit = _mesa_ffs(val);
592 if (bit != 0)
593 return bit;
594
595 bit = _mesa_ffs(val >> 32);
596 if (bit != 0)
597 return 32 + bit;
598
599 return 0;
600 #endif
601 }
602
603
604 /**
605 * Return number of bits set in given GLuint.
606 */
607 unsigned int
608 _mesa_bitcount(unsigned int n)
609 {
610 unsigned int bits;
611 for (bits = 0; n > 0; n = n >> 1) {
612 bits += (n & 1);
613 }
614 return bits;
615 }
616
617
618 /**
619 * Convert a 4-byte float to a 2-byte half float.
620 * Based on code from:
621 * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
622 */
623 GLhalfARB
624 _mesa_float_to_half(float val)
625 {
626 const int flt = *((int *) (void *) &val);
627 const int flt_m = flt & 0x7fffff;
628 const int flt_e = (flt >> 23) & 0xff;
629 const int flt_s = (flt >> 31) & 0x1;
630 int s, e, m = 0;
631 GLhalfARB result;
632
633 /* sign bit */
634 s = flt_s;
635
636 /* handle special cases */
637 if ((flt_e == 0) && (flt_m == 0)) {
638 /* zero */
639 /* m = 0; - already set */
640 e = 0;
641 }
642 else if ((flt_e == 0) && (flt_m != 0)) {
643 /* denorm -- denorm float maps to 0 half */
644 /* m = 0; - already set */
645 e = 0;
646 }
647 else if ((flt_e == 0xff) && (flt_m == 0)) {
648 /* infinity */
649 /* m = 0; - already set */
650 e = 31;
651 }
652 else if ((flt_e == 0xff) && (flt_m != 0)) {
653 /* NaN */
654 m = 1;
655 e = 31;
656 }
657 else {
658 /* regular number */
659 const int new_exp = flt_e - 127;
660 if (new_exp < -24) {
661 /* this maps to 0 */
662 /* m = 0; - already set */
663 e = 0;
664 }
665 else if (new_exp < -14) {
666 /* this maps to a denorm */
667 unsigned int exp_val = (unsigned int) (-14 - new_exp); /* 2^-exp_val*/
668 e = 0;
669 switch (exp_val) {
670 case 0:
671 _mesa_warning(NULL,
672 "float_to_half: logical error in denorm creation!\n");
673 /* m = 0; - already set */
674 break;
675 case 1: m = 512 + (flt_m >> 14); break;
676 case 2: m = 256 + (flt_m >> 15); break;
677 case 3: m = 128 + (flt_m >> 16); break;
678 case 4: m = 64 + (flt_m >> 17); break;
679 case 5: m = 32 + (flt_m >> 18); break;
680 case 6: m = 16 + (flt_m >> 19); break;
681 case 7: m = 8 + (flt_m >> 20); break;
682 case 8: m = 4 + (flt_m >> 21); break;
683 case 9: m = 2 + (flt_m >> 22); break;
684 case 10: m = 1; break;
685 }
686 }
687 else if (new_exp > 15) {
688 /* map this value to infinity */
689 /* m = 0; - already set */
690 e = 31;
691 }
692 else {
693 /* regular */
694 e = new_exp + 15;
695 m = flt_m >> 13;
696 }
697 }
698
699 result = (s << 15) | (e << 10) | m;
700 return result;
701 }
702
703
704 /**
705 * Convert a 2-byte half float to a 4-byte float.
706 * Based on code from:
707 * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
708 */
709 float
710 _mesa_half_to_float(GLhalfARB val)
711 {
712 /* XXX could also use a 64K-entry lookup table */
713 const int m = val & 0x3ff;
714 const int e = (val >> 10) & 0x1f;
715 const int s = (val >> 15) & 0x1;
716 int flt_m, flt_e, flt_s, flt;
717 float result;
718
719 /* sign bit */
720 flt_s = s;
721
722 /* handle special cases */
723 if ((e == 0) && (m == 0)) {
724 /* zero */
725 flt_m = 0;
726 flt_e = 0;
727 }
728 else if ((e == 0) && (m != 0)) {
729 /* denorm -- denorm half will fit in non-denorm single */
730 const float half_denorm = 1.0f / 16384.0f; /* 2^-14 */
731 float mantissa = ((float) (m)) / 1024.0f;
732 float sign = s ? -1.0f : 1.0f;
733 return sign * mantissa * half_denorm;
734 }
735 else if ((e == 31) && (m == 0)) {
736 /* infinity */
737 flt_e = 0xff;
738 flt_m = 0;
739 }
740 else if ((e == 31) && (m != 0)) {
741 /* NaN */
742 flt_e = 0xff;
743 flt_m = 1;
744 }
745 else {
746 /* regular */
747 flt_e = e + 112;
748 flt_m = m << 13;
749 }
750
751 flt = (flt_s << 31) | (flt_e << 23) | flt_m;
752 result = *((float *) (void *) &flt);
753 return result;
754 }
755
756 /*@}*/
757
758
759 /**********************************************************************/
760 /** \name Sort & Search */
761 /*@{*/
762
763 /**
764 * Wrapper for bsearch().
765 */
766 void *
767 _mesa_bsearch( const void *key, const void *base, size_t nmemb, size_t size,
768 int (*compar)(const void *, const void *) )
769 {
770 return bsearch(key, base, nmemb, size, compar);
771 }
772
773 /*@}*/
774
775
776 /**********************************************************************/
777 /** \name Environment vars */
778 /*@{*/
779
780 /**
781 * Wrapper for getenv().
782 */
783 char *
784 _mesa_getenv( const char *var )
785 {
786 #if defined(_XBOX)
787 return NULL;
788 #else
789 return getenv(var);
790 #endif
791 }
792
793 /*@}*/
794
795
796 /**********************************************************************/
797 /** \name String */
798 /*@{*/
799
800 /** Wrapper around strstr() */
801 char *
802 _mesa_strstr( const char *haystack, const char *needle )
803 {
804 return strstr(haystack, needle);
805 }
806
807 /** Wrapper around strncat() */
808 char *
809 _mesa_strncat( char *dest, const char *src, size_t n )
810 {
811 return strncat(dest, src, n);
812 }
813
814 /** Wrapper around strcpy() */
815 char *
816 _mesa_strcpy( char *dest, const char *src )
817 {
818 return strcpy(dest, src);
819 }
820
821 /** Wrapper around strncpy() */
822 char *
823 _mesa_strncpy( char *dest, const char *src, size_t n )
824 {
825 return strncpy(dest, src, n);
826 }
827
828 /** Wrapper around strlen() */
829 size_t
830 _mesa_strlen( const char *s )
831 {
832 return strlen(s);
833 }
834
835 /** Wrapper around strcmp() */
836 int
837 _mesa_strcmp( const char *s1, const char *s2 )
838 {
839 return strcmp(s1, s2);
840 }
841
842 /** Wrapper around strncmp() */
843 int
844 _mesa_strncmp( const char *s1, const char *s2, size_t n )
845 {
846 return strncmp(s1, s2, n);
847 }
848
849 /**
850 * Implemented using _mesa_malloc() and _mesa_strcpy.
851 * Note that NULL is handled accordingly.
852 */
853 char *
854 _mesa_strdup( const char *s )
855 {
856 if (s) {
857 size_t l = _mesa_strlen(s);
858 char *s2 = (char *) _mesa_malloc(l + 1);
859 if (s2)
860 _mesa_strcpy(s2, s);
861 return s2;
862 }
863 else {
864 return NULL;
865 }
866 }
867
868 /** Wrapper around atoi() */
869 int
870 _mesa_atoi(const char *s)
871 {
872 return atoi(s);
873 }
874
875 /** Wrapper around strtod() */
876 double
877 _mesa_strtod( const char *s, char **end )
878 {
879 return strtod(s, end);
880 }
881
882 /*@}*/
883
884
885 /**********************************************************************/
886 /** \name I/O */
887 /*@{*/
888
889 /** Wrapper around vsprintf() */
890 int
891 _mesa_sprintf( char *str, const char *fmt, ... )
892 {
893 int r;
894 va_list args;
895 va_start( args, fmt );
896 r = vsprintf( str, fmt, args );
897 va_end( args );
898 return r;
899 }
900
901 /** Wrapper around printf(), using vsprintf() for the formatting. */
902 void
903 _mesa_printf( const char *fmtString, ... )
904 {
905 char s[MAXSTRING];
906 va_list args;
907 va_start( args, fmtString );
908 vsnprintf(s, MAXSTRING, fmtString, args);
909 va_end( args );
910 fprintf(stderr,"%s", s);
911 }
912
913 /** Wrapper around vsprintf() */
914 int
915 _mesa_vsprintf( char *str, const char *fmt, va_list args )
916 {
917 return vsprintf( str, fmt, args );
918 }
919
920 /*@}*/
921
922
923 /**********************************************************************/
924 /** \name Diagnostics */
925 /*@{*/
926
927 /**
928 * Report a warning (a recoverable error condition) to stderr if
929 * either DEBUG is defined or the MESA_DEBUG env var is set.
930 *
931 * \param ctx GL context.
932 * \param fmtString printf() alike format string.
933 */
934 void
935 _mesa_warning( GLcontext *ctx, const char *fmtString, ... )
936 {
937 GLboolean debug;
938 char str[MAXSTRING];
939 va_list args;
940 (void) ctx;
941 va_start( args, fmtString );
942 (void) vsnprintf( str, MAXSTRING, fmtString, args );
943 va_end( args );
944 #ifdef DEBUG
945 debug = GL_TRUE; /* always print warning */
946 #else
947 debug = _mesa_getenv("MESA_DEBUG") ? GL_TRUE : GL_FALSE;
948 #endif
949 if (debug) {
950 fprintf(stderr, "Mesa warning: %s\n", str);
951 }
952 }
953
954 /**
955 * Report an internla implementation problem.
956 * Prints the message to stderr via fprintf().
957 *
958 * \param ctx GL context.
959 * \param s problem description string.
960 */
961 void
962 _mesa_problem( const GLcontext *ctx, const char *fmtString, ... )
963 {
964 va_list args;
965 char str[MAXSTRING];
966 (void) ctx;
967
968 va_start( args, fmtString );
969 vsnprintf( str, MAXSTRING, fmtString, args );
970 va_end( args );
971
972 fprintf(stderr, "Mesa %s implementation error: %s\n", MESA_VERSION_STRING, str);
973 fprintf(stderr, "Please report at bugzilla.freedesktop.org\n");
974 }
975
976 /**
977 * Record an OpenGL state error. These usually occur when the users
978 * passes invalid parameters to a GL function.
979 *
980 * If debugging is enabled (either at compile-time via the DEBUG macro, or
981 * run-time via the MESA_DEBUG environment variable), report the error with
982 * _mesa_debug().
983 *
984 * \param ctx the GL context.
985 * \param error the error value.
986 * \param fmtString printf() style format string, followed by optional args
987 */
988 void
989 _mesa_error( GLcontext *ctx, GLenum error, const char *fmtString, ... )
990 {
991 const char *debugEnv;
992 GLboolean debug;
993
994 debugEnv = _mesa_getenv("MESA_DEBUG");
995
996 #ifdef DEBUG
997 if (debugEnv && _mesa_strstr(debugEnv, "silent"))
998 debug = GL_FALSE;
999 else
1000 debug = GL_TRUE;
1001 #else
1002 if (debugEnv)
1003 debug = GL_TRUE;
1004 else
1005 debug = GL_FALSE;
1006 #endif
1007
1008 if (debug) {
1009 va_list args;
1010 char where[MAXSTRING];
1011 const char *errstr;
1012
1013 va_start( args, fmtString );
1014 vsnprintf( where, MAXSTRING, fmtString, args );
1015 va_end( args );
1016
1017 switch (error) {
1018 case GL_NO_ERROR:
1019 errstr = "GL_NO_ERROR";
1020 break;
1021 case GL_INVALID_VALUE:
1022 errstr = "GL_INVALID_VALUE";
1023 break;
1024 case GL_INVALID_ENUM:
1025 errstr = "GL_INVALID_ENUM";
1026 break;
1027 case GL_INVALID_OPERATION:
1028 errstr = "GL_INVALID_OPERATION";
1029 break;
1030 case GL_STACK_OVERFLOW:
1031 errstr = "GL_STACK_OVERFLOW";
1032 break;
1033 case GL_STACK_UNDERFLOW:
1034 errstr = "GL_STACK_UNDERFLOW";
1035 break;
1036 case GL_OUT_OF_MEMORY:
1037 errstr = "GL_OUT_OF_MEMORY";
1038 break;
1039 case GL_TABLE_TOO_LARGE:
1040 errstr = "GL_TABLE_TOO_LARGE";
1041 break;
1042 case GL_INVALID_FRAMEBUFFER_OPERATION_EXT:
1043 errstr = "GL_INVALID_FRAMEBUFFER_OPERATION";
1044 break;
1045 default:
1046 errstr = "unknown";
1047 break;
1048 }
1049 _mesa_debug(ctx, "User error: %s in %s\n", errstr, where);
1050 }
1051
1052 _mesa_record_error(ctx, error);
1053 }
1054
1055 /**
1056 * Report debug information. Print error message to stderr via fprintf().
1057 * No-op if DEBUG mode not enabled.
1058 *
1059 * \param ctx GL context.
1060 * \param fmtString printf()-style format string, followed by optional args.
1061 */
1062 void
1063 _mesa_debug( const GLcontext *ctx, const char *fmtString, ... )
1064 {
1065 #ifdef DEBUG
1066 char s[MAXSTRING];
1067 va_list args;
1068 va_start(args, fmtString);
1069 vsnprintf(s, MAXSTRING, fmtString, args);
1070 va_end(args);
1071 fprintf(stderr, "Mesa: %s", s);
1072 #endif /* DEBUG */
1073 (void) ctx;
1074 (void) fmtString;
1075 }
1076
1077 /*@}*/
1078
1079
1080 /**
1081 * Wrapper for exit().
1082 */
1083 void
1084 _mesa_exit( int status )
1085 {
1086 exit(status);
1087 }