1 /* $Id: imports.c,v 1.17 2002/08/01 15:10:23 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() */
54 _mesa_Malloc(__GLcontext
*gc
, size_t size
)
60 _mesa_Calloc(__GLcontext
*gc
, size_t numElem
, size_t elemSize
)
62 return CALLOC(numElem
* elemSize
);
66 _mesa_Realloc(__GLcontext
*gc
, void *oldAddr
, size_t newSize
)
68 return realloc(oldAddr
, newSize
);
72 _mesa_Free(__GLcontext
*gc
, void *addr
)
78 /* Must be before '#undef getenv' for inclusion in XFree86.
81 _mesa_getenv(__GLcontext
*gc
, const char *var
)
84 /* Whacko XFree86 macro:
94 warning(__GLcontext
*gc
, char *str
)
100 if (_mesa_getenv(gc
, "MESA_DEBUG"))
106 fprintf(stderr
, "Mesa warning: %s\n", str
);
112 _mesa_fatal(__GLcontext
*gc
, char *str
)
115 fprintf(stderr
, "%s\n", str
);
121 _mesa_atoi(__GLcontext
*gc
, const char *str
)
129 _mesa_sprintf(__GLcontext
*gc
, char *str
, const char *fmt
, ...)
133 va_start( args
, fmt
);
134 r
= vsprintf( str
, fmt
, args
);
141 _mesa_fopen(__GLcontext
*gc
, const char *path
, const char *mode
)
143 return fopen(path
, mode
);
148 _mesa_fclose(__GLcontext
*gc
, void *stream
)
150 return fclose((FILE *) stream
);
155 _mesa_fprintf(__GLcontext
*gc
, void *stream
, const char *fmt
, ...)
159 va_start( args
, fmt
);
160 r
= vfprintf( (FILE *) stream
, fmt
, args
);
166 /* XXX this really is driver-specific and can't be here */
167 static __GLdrawablePrivate
*
168 _mesa_GetDrawablePrivate(__GLcontext
*gc
)
176 _mesa_warning(__GLcontext
*gc
, const char *fmtString
, ...)
180 va_start( args
, fmtString
);
181 (void) vsnprintf( str
, MAXSTRING
, fmtString
, args
);
188 * This function is called when the Mesa user has stumbled into a code
189 * path which may not be implemented fully or correctly.
192 _mesa_problem( const GLcontext
*ctx
, const char *s
)
195 ctx
->imports
.fprintf((GLcontext
*) ctx
, stderr
, "Mesa implementation error: %s\n", s
);
197 ctx
->imports
.fprintf((GLcontext
*) ctx
, stderr
, "Please report to the DRI bug database at dri.sourceforge.net\n");
199 ctx
->imports
.fprintf((GLcontext
*) ctx
, stderr
, "Please report to the Mesa bug database at www.mesa3d.org\n" );
203 /* what can we do if we don't have a context???? */
204 fprintf( stderr
, "Mesa implementation error: %s\n", s
);
206 fprintf( stderr
, "Please report to the DRI bug database at dri.sourceforge.net\n");
208 fprintf( stderr
, "Please report to the Mesa bug database at www.mesa3d.org\n" );
215 * If in debug mode, print error message to stdout.
216 * Also, record the error code by calling _mesa_record_error().
217 * Input: ctx - the GL context
218 * error - the error value
219 * fmtString - printf-style format string, followed by optional args
222 _mesa_error( GLcontext
*ctx
, GLenum error
, const char *fmtString
, ... )
224 const char *debugEnv
;
227 debugEnv
= _mesa_getenv(ctx
, "MESA_DEBUG");
230 if (debugEnv
&& strstr(debugEnv
, "silent"))
243 char where
[MAXSTRING
];
246 va_start( args
, fmtString
);
247 vsnprintf( where
, MAXSTRING
, fmtString
, args
);
252 errstr
= "GL_NO_ERROR";
254 case GL_INVALID_VALUE
:
255 errstr
= "GL_INVALID_VALUE";
257 case GL_INVALID_ENUM
:
258 errstr
= "GL_INVALID_ENUM";
260 case GL_INVALID_OPERATION
:
261 errstr
= "GL_INVALID_OPERATION";
263 case GL_STACK_OVERFLOW
:
264 errstr
= "GL_STACK_OVERFLOW";
266 case GL_STACK_UNDERFLOW
:
267 errstr
= "GL_STACK_UNDERFLOW";
269 case GL_OUT_OF_MEMORY
:
270 errstr
= "GL_OUT_OF_MEMORY";
272 case GL_TABLE_TOO_LARGE
:
273 errstr
= "GL_TABLE_TOO_LARGE";
279 _mesa_debug(ctx
, "Mesa user error: %s in %s\n", errstr
, where
);
282 _mesa_record_error(ctx
, error
);
287 * Call this to report debug information. Uses stderr.
290 _mesa_debug( const GLcontext
*ctx
, const char *fmtString
, ... )
294 va_start(args
, fmtString
);
295 vsnprintf(s
, MAXSTRING
, fmtString
, args
);
297 (void) ctx
->imports
.fprintf( (__GLcontext
*) ctx
, stderr
, s
);
299 fprintf( stderr
, s
);
305 * A wrapper for printf. Uses stdout.
308 _mesa_printf( const GLcontext
*ctx
, const char *fmtString
, ... )
312 va_start( args
, fmtString
);
313 vsnprintf(s
, MAXSTRING
, fmtString
, args
);
315 (void) ctx
->imports
.fprintf( (__GLcontext
*) ctx
, stdout
, s
);
323 * Initialize a __GLimports object to point to the functions in
324 * this file. This is to be called from device drivers.
325 * Input: imports - the object to init
326 * driverCtx - pointer to device driver-specific data
329 _mesa_init_default_imports(__GLimports
*imports
, void *driverCtx
)
331 imports
->malloc
= _mesa_Malloc
;
332 imports
->calloc
= _mesa_Calloc
;
333 imports
->realloc
= _mesa_Realloc
;
334 imports
->free
= _mesa_Free
;
335 imports
->warning
= warning
;
336 imports
->fatal
= _mesa_fatal
;
337 imports
->getenv
= _mesa_getenv
;
338 imports
->atoi
= _mesa_atoi
;
339 imports
->sprintf
= _mesa_sprintf
;
340 imports
->fopen
= _mesa_fopen
;
341 imports
->fclose
= _mesa_fclose
;
342 imports
->fprintf
= _mesa_fprintf
;
343 imports
->getDrawablePrivate
= _mesa_GetDrawablePrivate
;
344 imports
->other
= driverCtx
;