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