1 /* $Id: imports.c,v 1.20 2002/10/15 15:36:26 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 most Mesa drivers will be able to call
36 *_mesa_init_default_imports() and go with what's here.
38 * Eventually, I'd like to move most of the stuff in glheader.h and mem.[ch]
39 * into imports.[ch]. Then we'll really have one, single place where
40 * all OS-related dependencies are isolated.
50 #define MAXSTRING 4000 /* for vsnprintf() */
53 #define vsnprintf _vsnprintf
57 _mesa_Malloc(__GLcontext
*gc
, size_t size
)
63 _mesa_Calloc(__GLcontext
*gc
, size_t numElem
, size_t elemSize
)
65 return CALLOC(numElem
* elemSize
);
69 _mesa_Realloc(__GLcontext
*gc
, void *oldAddr
, size_t newSize
)
71 return realloc(oldAddr
, newSize
);
75 _mesa_Free(__GLcontext
*gc
, void *addr
)
81 /* Must be before '#undef getenv' for inclusion in XFree86.
84 _mesa_getenv(__GLcontext
*gc
, const char *var
)
88 return xf86getenv(var
);
96 warning(__GLcontext
*gc
, char *str
)
102 if (_mesa_getenv(gc
, "MESA_DEBUG"))
108 fprintf(stderr
, "Mesa warning: %s\n", str
);
114 _mesa_fatal(__GLcontext
*gc
, char *str
)
117 fprintf(stderr
, "%s\n", str
);
123 _mesa_atoi(__GLcontext
*gc
, const char *str
)
131 _mesa_sprintf(__GLcontext
*gc
, char *str
, const char *fmt
, ...)
135 va_start( args
, fmt
);
136 r
= vsprintf( str
, fmt
, args
);
143 _mesa_fopen(__GLcontext
*gc
, const char *path
, const char *mode
)
145 return fopen(path
, mode
);
150 _mesa_fclose(__GLcontext
*gc
, void *stream
)
152 return fclose((FILE *) stream
);
157 _mesa_fprintf(__GLcontext
*gc
, void *stream
, const char *fmt
, ...)
161 va_start( args
, fmt
);
162 r
= vfprintf( (FILE *) stream
, fmt
, args
);
168 /* XXX this really is driver-specific and can't be here */
169 static __GLdrawablePrivate
*
170 _mesa_GetDrawablePrivate(__GLcontext
*gc
)
178 _mesa_warning(__GLcontext
*gc
, const char *fmtString
, ...)
182 va_start( args
, fmtString
);
183 (void) vsnprintf( str
, MAXSTRING
, fmtString
, args
);
190 * This function is called when the Mesa user has stumbled into a code
191 * path which may not be implemented fully or correctly.
194 _mesa_problem( const GLcontext
*ctx
, const char *s
)
197 ctx
->imports
.fprintf((GLcontext
*) ctx
, stderr
, "Mesa implementation error: %s\n", s
);
199 ctx
->imports
.fprintf((GLcontext
*) ctx
, stderr
, "Please report to the DRI bug database at dri.sourceforge.net\n");
201 ctx
->imports
.fprintf((GLcontext
*) ctx
, stderr
, "Please report to the Mesa bug database at www.mesa3d.org\n" );
205 /* what can we do if we don't have a context???? */
206 fprintf( stderr
, "Mesa implementation error: %s\n", s
);
208 fprintf( stderr
, "Please report to the DRI bug database at dri.sourceforge.net\n");
210 fprintf( stderr
, "Please report to the Mesa bug database at www.mesa3d.org\n" );
217 * If in debug mode, print error message to stdout.
218 * Also, record the error code by calling _mesa_record_error().
219 * Input: ctx - the GL context
220 * error - the error value
221 * fmtString - printf-style format string, followed by optional args
224 _mesa_error( GLcontext
*ctx
, GLenum error
, const char *fmtString
, ... )
226 const char *debugEnv
;
229 debugEnv
= _mesa_getenv(ctx
, "MESA_DEBUG");
232 if (debugEnv
&& strstr(debugEnv
, "silent"))
245 char where
[MAXSTRING
];
248 va_start( args
, fmtString
);
249 vsnprintf( where
, MAXSTRING
, fmtString
, args
);
254 errstr
= "GL_NO_ERROR";
256 case GL_INVALID_VALUE
:
257 errstr
= "GL_INVALID_VALUE";
259 case GL_INVALID_ENUM
:
260 errstr
= "GL_INVALID_ENUM";
262 case GL_INVALID_OPERATION
:
263 errstr
= "GL_INVALID_OPERATION";
265 case GL_STACK_OVERFLOW
:
266 errstr
= "GL_STACK_OVERFLOW";
268 case GL_STACK_UNDERFLOW
:
269 errstr
= "GL_STACK_UNDERFLOW";
271 case GL_OUT_OF_MEMORY
:
272 errstr
= "GL_OUT_OF_MEMORY";
274 case GL_TABLE_TOO_LARGE
:
275 errstr
= "GL_TABLE_TOO_LARGE";
281 _mesa_debug(ctx
, "Mesa user error: %s in %s\n", errstr
, where
);
284 _mesa_record_error(ctx
, error
);
289 * Call this to report debug information. Uses stderr.
292 _mesa_debug( const GLcontext
*ctx
, const char *fmtString
, ... )
296 va_start(args
, fmtString
);
297 vsnprintf(s
, MAXSTRING
, fmtString
, args
);
299 (void) ctx
->imports
.fprintf( (__GLcontext
*) ctx
, stderr
, s
);
301 fprintf( stderr
, s
);
307 * A wrapper for printf. Uses stdout.
310 _mesa_printf( const GLcontext
*ctx
, const char *fmtString
, ... )
314 va_start( args
, fmtString
);
315 vsnprintf(s
, MAXSTRING
, fmtString
, args
);
317 (void) ctx
->imports
.fprintf( (__GLcontext
*) ctx
, stdout
, s
);
325 * Initialize a __GLimports object to point to the functions in
326 * this file. This is to be called from device drivers.
327 * Input: imports - the object to init
328 * driverCtx - pointer to device driver-specific data
331 _mesa_init_default_imports(__GLimports
*imports
, void *driverCtx
)
333 imports
->malloc
= _mesa_Malloc
;
334 imports
->calloc
= _mesa_Calloc
;
335 imports
->realloc
= _mesa_Realloc
;
336 imports
->free
= _mesa_Free
;
337 imports
->warning
= warning
;
338 imports
->fatal
= _mesa_fatal
;
339 imports
->getenv
= _mesa_getenv
;
340 imports
->atoi
= _mesa_atoi
;
341 imports
->sprintf
= _mesa_sprintf
;
342 imports
->fopen
= _mesa_fopen
;
343 imports
->fclose
= _mesa_fclose
;
344 imports
->fprintf
= _mesa_fprintf
;
345 imports
->getDrawablePrivate
= _mesa_GetDrawablePrivate
;
346 imports
->other
= driverCtx
;