added a comment
[mesa.git] / src / mesa / main / imports.c
1 /* $Id: imports.c,v 1.13 2002/06/15 02:38:15 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 4.1
6 *
7 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
8 *
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:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
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.
25 */
26
27
28 /*
29 * Imports are functions 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,
32 * etc.
33 *
34 * Some drivers will want to provide a specialed __GLimport object, but
35 * most Mesa drivers will be able to call _mesa_init_default_imports()
36 * and go with that.
37 *
38 * A server-side GL renderer will likely not use these functions since
39 * the renderer should use the XFree86-wrapped system calls.
40 */
41
42 /*
43 * XXX when we fully implement the __GLimports mechanism in Mesa, that
44 * should mean that we can remove <stdio.h>, <stdlib.h>, etc, from
45 * glheader.h. Strictly speaking, all system includes should be done
46 * from this file, and not glheader to ensure that core Mesa has no
47 * dependencies on external libraries. Someday...
48 */
49
50
51
52 #include "glheader.h"
53 #include "imports.h"
54 #include "mem.h"
55
56
57 static void *
58 _mesa_Malloc(__GLcontext *gc, size_t size)
59 {
60 return MALLOC(size);
61 }
62
63 static void *
64 _mesa_Calloc(__GLcontext *gc, size_t numElem, size_t elemSize)
65 {
66 return CALLOC(numElem * elemSize);
67 }
68
69 static void *
70 _mesa_Realloc(__GLcontext *gc, void *oldAddr, size_t newSize)
71 {
72 return realloc(oldAddr, newSize);
73 }
74
75 static void
76 _mesa_Free(__GLcontext *gc, void *addr)
77 {
78 FREE(addr);
79 }
80
81 /* Must be before '#undef getenv' for inclusion in XFree86.
82 */
83 static char * CAPI
84 _mesa_getenv(__GLcontext *gc, const char *var)
85 {
86 (void) gc;
87 return getenv(var);
88 }
89
90 static void
91 _mesa_warning(__GLcontext *gc, char *str)
92 {
93 GLboolean debug;
94 #ifdef DEBUG
95 debug = GL_TRUE;
96 #else
97 /* Whacko XFree86 macro:
98 */
99 #ifdef getenv
100 #undef getenv
101 #endif
102 if (gc->imports.getenv(gc, "MESA_DEBUG")) {
103 debug = GL_TRUE;
104 }
105 else {
106 debug = GL_FALSE;
107 }
108 #endif
109 if (debug) {
110 fprintf(stderr, "Mesa warning: %s\n", str);
111 }
112 }
113
114 static void
115 _mesa_fatal(__GLcontext *gc, char *str)
116 {
117 fprintf(stderr, "%s\n", str);
118 abort();
119 }
120
121 static int CAPI
122 _mesa_atoi(__GLcontext *gc, const char *str)
123 {
124 (void) gc;
125 return atoi(str);
126 }
127
128 static int CAPI
129 _mesa_sprintf(__GLcontext *gc, char *str, const char *fmt, ...)
130 {
131 int r;
132 va_list args;
133 va_start( args, fmt );
134 r = vsprintf( str, fmt, args );
135 va_end( args );
136 return r;
137 }
138
139 static void * CAPI
140 _mesa_fopen(__GLcontext *gc, const char *path, const char *mode)
141 {
142 return fopen(path, mode);
143 }
144
145 static int CAPI
146 _mesa_fclose(__GLcontext *gc, void *stream)
147 {
148 return fclose((FILE *) stream);
149 }
150
151 static int CAPI
152 _mesa_fprintf(__GLcontext *gc, void *stream, const char *fmt, ...)
153 {
154 int r;
155 va_list args;
156 va_start( args, fmt );
157 r = vfprintf( (FILE *) stream, fmt, args );
158 va_end( args );
159 return r;
160 }
161
162 /* XXX this really is driver-specific and can't be here */
163 static __GLdrawablePrivate *
164 _mesa_GetDrawablePrivate(__GLcontext *gc)
165 {
166 return NULL;
167 }
168
169
170 void
171 _mesa_init_default_imports(__GLimports *imports, void *driverCtx)
172 {
173 imports->malloc = _mesa_Malloc;
174 imports->calloc = _mesa_Calloc;
175 imports->realloc = _mesa_Realloc;
176 imports->free = _mesa_Free;
177 imports->warning = _mesa_warning;
178 imports->fatal = _mesa_fatal;
179 imports->getenv = _mesa_getenv;
180 imports->atoi = _mesa_atoi;
181 imports->sprintf = _mesa_sprintf;
182 imports->fopen = _mesa_fopen;
183 imports->fclose = _mesa_fclose;
184 imports->fprintf = _mesa_fprintf;
185 imports->getDrawablePrivate = _mesa_GetDrawablePrivate;
186 imports->other = driverCtx;
187 }