1 /* $Id: imports.c,v 1.23 2002/10/30 19:40:20 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 * Imports are services which the device driver or window system or
30 * operating system provides to the core renderer. The core renderer (Mesa)
31 * will call these functions in order to do memory allocation, simple I/O,
34 * Some drivers will want to override/replace this file with something
35 * specialized, but that'll be rare.
37 * Eventually, I want to move roll the glheader.h file into this.
39 * The OpenGL SI's __GLimports structure allows per-context specification of
40 * replacements for the standard C lib functions. In practice that's probably
41 * never needed; compile-time replacements are far more likely.
43 * The _mesa_foo() functions defined here don't in general take a context
44 * parameter. I guess we can change that someday, if need be.
45 * So for now, the __GLimports stuff really isn't used.
55 #define MAXSTRING 4000 /* for vsnprintf() */
58 #define vsnprintf _vsnprintf
62 /**********************************************************************/
63 /* Wrappers for standard C library functions */
64 /**********************************************************************/
68 _mesa_malloc(size_t bytes
)
71 return xf86malloc(bytes
);
79 _mesa_calloc(size_t bytes
)
82 return xf86calloc(1, bytes
);
84 return calloc(1, bytes
);
101 _mesa_align_malloc(size_t bytes
, unsigned long alignment
)
103 unsigned long ptr
, buf
;
105 ASSERT( alignment
> 0 );
107 /* Allocate extra memory to accomodate rounding up the address for
108 * alignment and to record the real malloc address.
110 ptr
= (unsigned long) _mesa_malloc(bytes
+ alignment
+ sizeof(void *));
114 buf
= (ptr
+ alignment
+ sizeof(void *)) & ~(unsigned long)(alignment
- 1);
115 *(unsigned long *)(buf
- sizeof(void *)) = ptr
;
118 /* mark the non-aligned area */
119 while ( ptr
< buf
- sizeof(void *) ) {
120 *(unsigned long *)ptr
= 0xcdcdcdcd;
121 ptr
+= sizeof(unsigned long);
130 _mesa_align_calloc(size_t bytes
, unsigned long alignment
)
132 unsigned long ptr
, buf
;
134 ASSERT( alignment
> 0 );
136 ptr
= (unsigned long) _mesa_calloc(bytes
+ alignment
+ sizeof(void *));
140 buf
= (ptr
+ alignment
+ sizeof(void *)) & ~(unsigned long)(alignment
- 1);
141 *(unsigned long *)(buf
- sizeof(void *)) = ptr
;
144 /* mark the non-aligned area */
145 while ( ptr
< buf
- sizeof(void *) ) {
146 *(unsigned long *)ptr
= 0xcdcdcdcd;
147 ptr
+= sizeof(unsigned long);
156 _mesa_align_free(void *ptr
)
159 _mesa_free( (void *)(*(unsigned long *)((unsigned long)ptr
- sizeof(void *))) );
161 /* The actuall address to free is stuffed in the word immediately
162 * before the address the client sees.
164 void **cubbyHole
= (void **) ((char *) ptr
- sizeof(void *));
165 void *realAddr
= *cubbyHole
;
166 _mesa_free(realAddr
);
172 _mesa_memcpy(void *dest
, const void *src
, size_t n
)
175 return xf86memcpy(dest
, src
, n
);
176 #elif defined(SUNOS4)
177 return memcpy((char *) dest
, (char *) src
, (int) n
);
179 return memcpy(dest
, src
, n
);
185 _mesa_memset( void *dst
, int val
, size_t n
)
188 xf86memset( dst
, val
, n
);
189 #elif defined(SUNOS4)
190 memset( (char *) dst
, (int) val
, (int) n
);
198 _mesa_memset16( unsigned short *dst
, unsigned short val
, size_t n
)
206 _mesa_bzero( void *dst
, size_t n
)
209 xf86memset( dst
, 0, n
);
210 #elif defined(__FreeBSD__)
252 _mesa_pow(double x
, double y
)
255 return xf86pow(x
, y
);
263 _mesa_getenv( const char *var
)
266 return xf86getenv(var
);
274 _mesa_strstr( const char *haystack
, const char *needle
)
277 return xf86strstr(haystack
, needle
);
279 return strstr(haystack
, needle
);
285 _mesa_strncat( char *dest
, const char *src
, size_t n
)
288 return xf86strncat(dest
, src
, n
);
290 return strncat(dest
, src
, n
);
296 _mesa_strcpy( char *dest
, const char *src
)
299 return xf86strcpy(dest
, src
);
301 return strcpy(dest
, src
);
307 _mesa_strncpy( char *dest
, const char *src
, size_t n
)
310 return xf86strncpy(dest
, src
, n
);
312 return strncpy(dest
, src
, n
);
318 _mesa_strlen( const char *s
)
321 return xf86strlen(s
);
329 _mesa_strcmp( const char *s1
, const char *s2
)
332 return xf86strcmp(s1
, s2
);
334 return strcmp(s1
, s2
);
340 _mesa_strncmp( const char *s1
, const char *s2
, size_t n
)
343 return xf86strncmp(s1
, s2
, n
);
345 return strncmp(s1
, s2
, n
);
351 _mesa_atoi(const char *s
)
362 _mesa_sprintf( char *str
, const char *fmt
, ... )
366 va_start( args
, fmt
);
369 r
= xf86vsprintf( str
, fmt
, args
);
371 r
= vsprintf( str
, fmt
, args
);
378 _mesa_printf( const char *fmtString
, ... )
382 va_start( args
, fmtString
);
383 vsnprintf(s
, MAXSTRING
, fmtString
, args
);
394 _mesa_warning( GLcontext
*ctx
, const char *fmtString
, ... )
400 va_start( args
, fmtString
);
401 (void) vsnprintf( str
, MAXSTRING
, fmtString
, args
);
404 debug
= GL_TRUE
; /* always print warning */
406 debug
= _mesa_getenv("MESA_DEBUG") ? GL_TRUE
: GL_FALSE
;
410 xf86fprintf(stderr
, "Mesa warning: %s\n", str
);
412 fprintf(stderr
, "Mesa warning: %s\n", str
);
419 * This function is called when the Mesa user has stumbled into a code
420 * path which may not be implemented fully or correctly.
423 _mesa_problem( const GLcontext
*ctx
, const char *s
)
427 xf86fprintf(stderr
, "Mesa implementation error: %s\n", s
);
428 xf86fprintf(stderr
, "Please report to the DRI project at dri.sourceforge.net\n");
430 fprintf(stderr
, "Mesa implementation error: %s\n", s
);
431 fprintf(stderr
, "Please report to the Mesa bug database at www.mesa3d.org\n" );
437 * If in debug mode, print error message to stdout.
438 * Also, record the error code by calling _mesa_record_error().
439 * Input: ctx - the GL context
440 * error - the error value
441 * fmtString - printf-style format string, followed by optional args
444 _mesa_error( GLcontext
*ctx
, GLenum error
, const char *fmtString
, ... )
446 const char *debugEnv
;
449 debugEnv
= _mesa_getenv("MESA_DEBUG");
452 if (debugEnv
&& _mesa_strstr(debugEnv
, "silent"))
465 char where
[MAXSTRING
];
468 va_start( args
, fmtString
);
469 vsnprintf( where
, MAXSTRING
, fmtString
, args
);
474 errstr
= "GL_NO_ERROR";
476 case GL_INVALID_VALUE
:
477 errstr
= "GL_INVALID_VALUE";
479 case GL_INVALID_ENUM
:
480 errstr
= "GL_INVALID_ENUM";
482 case GL_INVALID_OPERATION
:
483 errstr
= "GL_INVALID_OPERATION";
485 case GL_STACK_OVERFLOW
:
486 errstr
= "GL_STACK_OVERFLOW";
488 case GL_STACK_UNDERFLOW
:
489 errstr
= "GL_STACK_UNDERFLOW";
491 case GL_OUT_OF_MEMORY
:
492 errstr
= "GL_OUT_OF_MEMORY";
494 case GL_TABLE_TOO_LARGE
:
495 errstr
= "GL_TABLE_TOO_LARGE";
501 _mesa_debug(ctx
, "Mesa user error: %s in %s\n", errstr
, where
);
504 _mesa_record_error(ctx
, error
);
509 * Call this to report debug information. Uses stderr.
512 _mesa_debug( const GLcontext
*ctx
, const char *fmtString
, ... )
516 va_start(args
, fmtString
);
517 vsnprintf(s
, MAXSTRING
, fmtString
, args
);
520 xf86fprintf(stderr
, "Mesa: %s", s
);
522 fprintf(stderr
, "Mesa: %s", s
);
528 /**********************************************************************/
529 /* Default Imports Wrapper */
530 /**********************************************************************/
533 default_malloc(__GLcontext
*gc
, size_t size
)
536 return _mesa_malloc(size
);
540 default_calloc(__GLcontext
*gc
, size_t numElem
, size_t elemSize
)
543 return _mesa_calloc(numElem
* elemSize
);
547 default_realloc(__GLcontext
*gc
, void *oldAddr
, size_t newSize
)
551 return xf86realloc(oldAddr
, newSize
);
553 return realloc(oldAddr
, newSize
);
558 default_free(__GLcontext
*gc
, void *addr
)
565 default_getenv( __GLcontext
*gc
, const char *var
)
568 return _mesa_getenv(var
);
572 default_warning(__GLcontext
*gc
, char *str
)
574 _mesa_warning(gc
, str
);
578 default_fatal(__GLcontext
*gc
, char *str
)
580 _mesa_problem(gc
, str
);
585 default_atoi(__GLcontext
*gc
, const char *str
)
592 default_sprintf(__GLcontext
*gc
, char *str
, const char *fmt
, ...)
596 va_start( args
, fmt
);
597 r
= vsprintf( str
, fmt
, args
);
603 default_fopen(__GLcontext
*gc
, const char *path
, const char *mode
)
605 return fopen(path
, mode
);
609 default_fclose(__GLcontext
*gc
, void *stream
)
611 return fclose((FILE *) stream
);
615 default_fprintf(__GLcontext
*gc
, void *stream
, const char *fmt
, ...)
619 va_start( args
, fmt
);
620 r
= vfprintf( (FILE *) stream
, fmt
, args
);
625 /* XXX this really is driver-specific and can't be here */
626 static __GLdrawablePrivate
*
627 default_GetDrawablePrivate(__GLcontext
*gc
)
636 * Initialize a __GLimports object to point to the functions in
637 * this file. This is to be called from device drivers.
638 * Input: imports - the object to init
639 * driverCtx - pointer to device driver-specific data
642 _mesa_init_default_imports(__GLimports
*imports
, void *driverCtx
)
644 imports
->malloc
= default_malloc
;
645 imports
->calloc
= default_calloc
;
646 imports
->realloc
= default_realloc
;
647 imports
->free
= default_free
;
648 imports
->warning
= default_warning
;
649 imports
->fatal
= default_fatal
;
650 imports
->getenv
= default_getenv
; /* not used for now */
651 imports
->atoi
= default_atoi
;
652 imports
->sprintf
= default_sprintf
;
653 imports
->fopen
= default_fopen
;
654 imports
->fclose
= default_fclose
;
655 imports
->fprintf
= default_fprintf
;
656 imports
->getDrawablePrivate
= default_GetDrawablePrivate
;
657 imports
->other
= driverCtx
;