Subversion Repositories shark

Rev

Rev 86 | Rev 118 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
87 giacomo 1
/* $Id: imports.c,v 1.4 2003-03-18 15:33:01 giacomo Exp $ */
55 pj 2
 
3
/*
4
 * Mesa 3-D graphics library
5
 * Version:  5.0
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 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,
32
 * etc.
33
 *
34
 * Some drivers will want to override/replace this file with something
35
 * specialized, but that'll be rare.
36
 *
37
 * Eventually, I want to move roll the glheader.h file into this.
38
 *
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.
42
 *
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.
46
 */
47
 
48
 
49
#include "glheader.h"
50
#include "mtypes.h"
51
#include "context.h"
52
#include "imports.h"
53
 
54
 
55
#define MAXSTRING 4000  /* for vsnprintf() */
56
 
57
#ifdef WIN32
58
#define vsnprintf _vsnprintf
59
#endif
60
 
61
 
62
/**********************************************************************/
63
/* Wrappers for standard C library functions                          */
64
/**********************************************************************/
65
 
66
/*
67
 * Functions still needed:
68
 * scanf
69
 * qsort
70
 * bsearch
71
 * rand and RAND_MAX
72
 */
73
 
74
void *
75
_mesa_malloc(size_t bytes)
76
{
77
#if defined(XFree86LOADER) && defined(IN_MODULE)
78
   return xf86malloc(bytes);
79
#else
80
   return malloc(bytes);
81
#endif
82
}
83
 
84
 
85
void *
86
_mesa_calloc(size_t bytes)
87
{
88
#if defined(XFree86LOADER) && defined(IN_MODULE)
89
   return xf86calloc(1, bytes);
90
#else
91
   return calloc(1, bytes);
92
#endif
93
}
94
 
95
 
96
void
97
_mesa_free(void *ptr)
98
{
99
#if defined(XFree86LOADER) && defined(IN_MODULE)
100
   xf86free(ptr);
101
#else
102
   free(ptr);
103
#endif
104
}
105
 
106
 
107
void *
108
_mesa_align_malloc(size_t bytes, unsigned long alignment)
109
{
110
   unsigned long ptr, buf;
111
 
112
   ASSERT( alignment > 0 );
113
 
114
   /* Allocate extra memory to accomodate rounding up the address for
115
    * alignment and to record the real malloc address.
116
    */
117
   ptr = (unsigned long) _mesa_malloc(bytes + alignment + sizeof(void *));
118
   if (!ptr)
119
      return NULL;
120
 
121
   buf = (ptr + alignment + sizeof(void *)) & ~(unsigned long)(alignment - 1);
122
   *(unsigned long *)(buf - sizeof(void *)) = ptr;
123
 
124
#ifdef DEBUG
125
   /* mark the non-aligned area */
126
   while ( ptr < buf - sizeof(void *) ) {
127
      *(unsigned long *)ptr = 0xcdcdcdcd;
128
      ptr += sizeof(unsigned long);
129
   }
130
#endif
131
 
132
   return (void *) buf;
133
}
134
 
135
 
136
void *
137
_mesa_align_calloc(size_t bytes, unsigned long alignment)
138
{
139
   unsigned long ptr, buf;
140
 
141
   ASSERT( alignment > 0 );
142
 
143
   ptr = (unsigned long) _mesa_calloc(bytes + alignment + sizeof(void *));
144
   if (!ptr)
145
      return NULL;
146
 
147
   buf = (ptr + alignment + sizeof(void *)) & ~(unsigned long)(alignment - 1);
148
   *(unsigned long *)(buf - sizeof(void *)) = ptr;
149
 
150
#ifdef DEBUG
151
   /* mark the non-aligned area */
152
   while ( ptr < buf - sizeof(void *) ) {
153
      *(unsigned long *)ptr = 0xcdcdcdcd;
154
      ptr += sizeof(unsigned long);
155
   }
156
#endif
157
 
158
   return (void *)buf;
159
}
160
 
161
 
162
void
163
_mesa_align_free(void *ptr)
164
{
165
#if 0
166
   _mesa_free( (void *)(*(unsigned long *)((unsigned long)ptr - sizeof(void *))) );
167
#else
168
   /* The actuall address to free is stuffed in the word immediately
169
    * before the address the client sees.
170
    */
171
   void **cubbyHole = (void **) ((char *) ptr - sizeof(void *));
172
   void *realAddr = *cubbyHole;
173
   _mesa_free(realAddr);
174
#endif
175
}
176
 
177
 
178
void *
179
_mesa_memcpy(void *dest, const void *src, size_t n)
180
{
181
#if defined(XFree86LOADER) && defined(IN_MODULE)
182
   return xf86memcpy(dest, src, n);
183
#elif defined(SUNOS4)
184
   return memcpy((char *) dest, (char *) src, (int) n);
185
#else
186
   return memcpy(dest, src, n);
187
#endif
188
}
189
 
190
 
191
void
192
_mesa_memset( void *dst, int val, size_t n )
193
{
194
#if defined(XFree86LOADER) && defined(IN_MODULE)
195
   xf86memset( dst, val, n );
196
#elif defined(SUNOS4)
197
   memset( (char *) dst, (int) val, (int) n );
198
#else
199
   memset(dst, val, n);
200
#endif
201
}
202
 
203
 
204
void
205
_mesa_memset16( unsigned short *dst, unsigned short val, size_t n )
206
{
207
   while (n-- > 0)
208
      *dst++ = val;
209
}
210
 
211
 
212
void
213
_mesa_bzero( void *dst, size_t n )
214
{
215
#if defined(XFree86LOADER) && defined(IN_MODULE)
216
   xf86memset( dst, 0, n );
217
#elif defined(__FreeBSD__)
218
   bzero( dst, n );
219
#else
220
   memset( dst, 0, n );
221
#endif
222
}
223
 
224
 
225
double
226
_mesa_sin(double a)
227
{
228
#if defined(XFree86LOADER) && defined(IN_MODULE)
229
   return xf86sin(a);
230
#else
231
   return sin(a);
232
#endif
233
}
234
 
235
 
236
double
237
_mesa_cos(double a)
238
{
239
#if defined(XFree86LOADER) && defined(IN_MODULE)
240
   return xf86cos(a);
241
#else
242
   return cos(a);
243
#endif
244
}
245
 
246
 
247
double
248
_mesa_sqrt(double x)
249
{
250
#if defined(XFree86LOADER) && defined(IN_MODULE)
251
   return xf86sqrt(x);
252
#else
253
   return sqrt(x);
254
#endif
255
}
256
 
257
 
258
double
259
_mesa_pow(double x, double y)
260
{
261
#if defined(XFree86LOADER) && defined(IN_MODULE)
262
   return xf86pow(x, y);
263
#else
264
   return pow(x, y);
265
#endif
266
}
267
 
268
 
269
char *
270
_mesa_getenv( const char *var )
271
{
272
#if defined(XFree86LOADER) && defined(IN_MODULE)
273
   return xf86getenv(var);
274
#else
275
   return getenv(var);
276
#endif
277
}
278
 
279
 
280
char *
281
_mesa_strstr( const char *haystack, const char *needle )
282
{
283
#if defined(XFree86LOADER) && defined(IN_MODULE)
284
   return xf86strstr(haystack, needle);
285
#else
286
   return strstr(haystack, needle);
287
#endif
288
}
289
 
290
 
291
char *
292
_mesa_strncat( char *dest, const char *src, size_t n )
293
{
294
#if defined(XFree86LOADER) && defined(IN_MODULE)
295
   return xf86strncat(dest, src, n);
296
#else
297
   return strncat(dest, src, n);
298
#endif
299
}
300
 
301
 
302
char *
303
_mesa_strcpy( char *dest, const char *src )
304
{
305
#if defined(XFree86LOADER) && defined(IN_MODULE)
306
   return xf86strcpy(dest, src);
307
#else
308
   return strcpy(dest, src);
309
#endif
310
}
311
 
312
 
313
char *
314
_mesa_strncpy( char *dest, const char *src, size_t n )
315
{
316
#if defined(XFree86LOADER) && defined(IN_MODULE)
317
   return xf86strncpy(dest, src, n);
318
#else
319
   return strncpy(dest, src, n);
320
#endif
321
}
322
 
323
 
324
size_t
325
_mesa_strlen( const char *s )
326
{
327
#if defined(XFree86LOADER) && defined(IN_MODULE)
328
   return xf86strlen(s);
329
#else
330
   return strlen(s);
331
#endif
332
}
333
 
334
 
335
int
336
_mesa_strcmp( const char *s1, const char *s2 )
337
{
338
#if defined(XFree86LOADER) && defined(IN_MODULE)
339
   return xf86strcmp(s1, s2);
340
#else
341
   return strcmp(s1, s2);
342
#endif
343
}
344
 
345
 
346
int
347
_mesa_strncmp( const char *s1, const char *s2, size_t n )
348
{
349
#if defined(XFree86LOADER) && defined(IN_MODULE)
350
   return xf86strncmp(s1, s2, n);
351
#else
352
   return strncmp(s1, s2, n);
353
#endif
354
}
355
 
356
 
357
int
358
_mesa_atoi(const char *s)
359
{
360
#if defined(XFree86LOADER) && defined(IN_MODULE)
361
   return xf86atoi(s);
362
#else
87 giacomo 363
   return (int)atoi((char *)s);
55 pj 364
#endif
365
}
366
 
86 giacomo 367
 
55 pj 368
int
369
_mesa_sprintf( char *str, const char *fmt, ... )
370
{
371
   int r;
372
   va_list args;
373
   va_start( args, fmt );  
374
   va_end( args );
375
#if defined(XFree86LOADER) && defined(IN_MODULE)
376
   r = xf86vsprintf( str, fmt, args );
377
#else
87 giacomo 378
   //r = vsnprintf( str, MAXSTRING, fmt, args );
379
   sprintf(str, "");
380
   r = 0;
55 pj 381
#endif
382
   return r;
383
}
384
 
86 giacomo 385
 
55 pj 386
void
387
_mesa_printf( const char *fmtString, ... )
388
{
389
   char s[MAXSTRING];
86 giacomo 390
   va_list args;
391
   va_start( args, fmtString );  
87 giacomo 392
   //vsnprintf(s, MAXSTRING, fmtString, args);
393
   sprintf(s, "");
86 giacomo 394
   va_end( args );
55 pj 395
#if defined(XFree86LOADER) && defined(IN_MODULE)
396
   xf86printf("%s", s);
397
#else
86 giacomo 398
   cprintf("%s", s);
55 pj 399
#endif
400
}
401
 
402
 
403
void
404
_mesa_warning( GLcontext *ctx, const char *fmtString, ... )
405
{
406
   GLboolean debug;
407
   char str[MAXSTRING];
86 giacomo 408
   va_list args;
55 pj 409
   (void) ctx;
86 giacomo 410
   va_start( args, fmtString );  
87 giacomo 411
   //(void) vsnprintf( str, MAXSTRING, fmtString, args );
412
   sprintf(str, "");
86 giacomo 413
   va_end( args );
55 pj 414
#ifdef DEBUG
415
   debug = GL_TRUE; /* always print warning */
416
#else
417
   debug = _mesa_getenv("MESA_DEBUG") ? GL_TRUE : GL_FALSE;
418
#endif
419
   if (debug) {
420
#if defined(XFree86LOADER) && defined(IN_MODULE)
421
      xf86fprintf(stderr, "Mesa warning: %s\n", str);
422
#else
86 giacomo 423
      cprintf("Mesa warning: %s\n", str);
55 pj 424
#endif
425
   }
426
}
427
 
428
 
429
/*
430
 * This function is called when the Mesa user has stumbled into a code
431
 * path which may not be implemented fully or correctly.
432
 */
433
void
434
_mesa_problem( const GLcontext *ctx, const char *s )
435
{
436
   (void) ctx;
437
#if defined(XFree86LOADER) && defined(IN_MODULE)
438
   xf86fprintf(stderr, "Mesa implementation error: %s\n", s);
439
   xf86fprintf(stderr, "Please report to the DRI project at dri.sourceforge.net\n");
440
#else
72 giacomo 441
   cprintf("Mesa implementation error: %s\n", s);
442
   cprintf("Please report to the Mesa bug database at www.mesa3d.org\n" );
55 pj 443
#endif
444
}
445
 
446
 
447
/*
448
 * If in debug mode, print error message to stdout.
449
 * Also, record the error code by calling _mesa_record_error().
450
 * Input:  ctx - the GL context
451
 *         error - the error value
452
 *         fmtString - printf-style format string, followed by optional args
453
 */
454
void
455
_mesa_error( GLcontext *ctx, GLenum error, const char *fmtString, ... )
456
{
457
   const char *debugEnv;
458
   GLboolean debug;
459
 
460
   debugEnv = _mesa_getenv("MESA_DEBUG");
461
 
462
#ifdef DEBUG
86 giacomo 463
   if (debugEnv && _mesa_strstr(debugEnv, "silent"))
464
      debug = GL_FALSE;
465
   else
55 pj 466
      debug = GL_TRUE;
467
#else
468
   if (debugEnv)
469
      debug = GL_TRUE;
470
   else
471
      debug = GL_FALSE;
472
#endif
473
 
474
   if (debug) {
475
      va_list args;
86 giacomo 476
      char where[MAXSTRING];
55 pj 477
      const char *errstr;
86 giacomo 478
 
55 pj 479
      va_start( args, fmtString );  
87 giacomo 480
      //vsnprintf( where, MAXSTRING, fmtString, args );
481
      sprintf(where, "");
55 pj 482
      va_end( args );
86 giacomo 483
 
55 pj 484
      switch (error) {
485
         case GL_NO_ERROR:
486
            errstr = "GL_NO_ERROR";
487
            break;
488
         case GL_INVALID_VALUE:
489
            errstr = "GL_INVALID_VALUE";
490
            break;
491
         case GL_INVALID_ENUM:
492
            errstr = "GL_INVALID_ENUM";
493
            break;
494
         case GL_INVALID_OPERATION:
495
            errstr = "GL_INVALID_OPERATION";
496
            break;
497
         case GL_STACK_OVERFLOW:
498
            errstr = "GL_STACK_OVERFLOW";
499
            break;
500
         case GL_STACK_UNDERFLOW:
501
            errstr = "GL_STACK_UNDERFLOW";
502
            break;
503
         case GL_OUT_OF_MEMORY:
504
            errstr = "GL_OUT_OF_MEMORY";
505
            break;
506
         case GL_TABLE_TOO_LARGE:
507
            errstr = "GL_TABLE_TOO_LARGE";
508
            break;
509
         default:
510
            errstr = "unknown";
511
            break;
512
      }
86 giacomo 513
      _mesa_debug(ctx, "Mesa user error: %s in %s\n", errstr, where);
55 pj 514
   }
515
 
516
   _mesa_record_error(ctx, error);
517
}  
518
 
519
 
520
/*
521
 * Call this to report debug information.  Uses stderr.
522
 */
523
void
524
_mesa_debug( const GLcontext *ctx, const char *fmtString, ... )
525
{
526
   char s[MAXSTRING];
527
   va_list args;
528
   va_start(args, fmtString);
87 giacomo 529
   //vsnprintf(s, MAXSTRING, fmtString, args);
530
   sprintf(s, "");
86 giacomo 531
   va_end(args);
55 pj 532
#if defined(XFree86LOADER) && defined(IN_MODULE)
533
   xf86fprintf(stderr, "Mesa: %s", s);
534
#else
86 giacomo 535
   cprintf("Mesa: %s", s);
55 pj 536
#endif
537
}
538
 
539
 
540
 
541
/**********************************************************************/
542
/* Default Imports Wrapper                                            */
543
/**********************************************************************/
544
 
545
static void *
546
default_malloc(__GLcontext *gc, size_t size)
547
{
548
   (void) gc;
549
   return _mesa_malloc(size);
550
}
551
 
552
static void *
553
default_calloc(__GLcontext *gc, size_t numElem, size_t elemSize)
554
{
555
   (void) gc;
556
   return _mesa_calloc(numElem * elemSize);
557
}
558
 
559
static void *
560
default_realloc(__GLcontext *gc, void *oldAddr, size_t newSize)
561
{
562
   (void) gc;
563
#if defined(XFree86LOADER) && defined(IN_MODULE)
564
   return xf86realloc(oldAddr, newSize);
565
#else
566
   return realloc(oldAddr, newSize);
567
#endif
568
}
569
 
570
static void
571
default_free(__GLcontext *gc, void *addr)
572
{
573
   (void) gc;
574
   _mesa_free(addr);
575
}
576
 
577
static char * CAPI
578
default_getenv( __GLcontext *gc, const char *var )
579
{
580
   (void) gc;
581
   return _mesa_getenv(var);
582
}
583
 
584
static void
585
default_warning(__GLcontext *gc, char *str)
586
{
587
   _mesa_warning(gc, str);
588
}
589
 
590
static void
591
default_fatal(__GLcontext *gc, char *str)
592
{
593
   _mesa_problem(gc, str);
594
   abort();
595
}
596
 
597
static int CAPI
598
default_atoi(__GLcontext *gc, const char *str)
599
{
600
   (void) gc;
87 giacomo 601
   return (int)atoi((char *)str);
55 pj 602
}
603
 
604
static int CAPI
605
default_sprintf(__GLcontext *gc, char *str, const char *fmt, ...)
606
{
607
   int r;
86 giacomo 608
   va_list args;
609
   va_start( args, fmt );  
87 giacomo 610
   //r = vsnprintf( str, MAXSTRING, fmt, args );
611
   sprintf(str, "");
612
   r = 0;
86 giacomo 613
   va_end( args );
55 pj 614
   return r;
615
}
616
 
617
static void * CAPI
618
default_fopen(__GLcontext *gc, const char *path, const char *mode)
619
{
72 giacomo 620
   return NULL;//fopen(path, mode);
55 pj 621
}
622
 
623
static int CAPI
624
default_fclose(__GLcontext *gc, void *stream)
625
{
72 giacomo 626
   return 0;//fclose((FILE *) stream);
55 pj 627
}
628
 
629
static int CAPI
630
default_fprintf(__GLcontext *gc, void *stream, const char *fmt, ...)
631
{
632
   int r;
633
   va_list args;
634
   va_start( args, fmt );  
87 giacomo 635
   //r = vsnprintf(tmp, MAXSTRING, fmt, args );
636
   r = 0;
55 pj 637
   va_end( args );
638
   return r;
639
}
640
 
641
/* XXX this really is driver-specific and can't be here */
642
static __GLdrawablePrivate *
643
default_GetDrawablePrivate(__GLcontext *gc)
644
{
645
   return NULL;
646
}
647
 
648
 
649
 
650
 
651
/*
652
 * Initialize a __GLimports object to point to the functions in
653
 * this file.  This is to be called from device drivers.
654
 * Input:  imports - the object to init
655
 *         driverCtx - pointer to device driver-specific data
656
 */
657
void
658
_mesa_init_default_imports(__GLimports *imports, void *driverCtx)
659
{
660
   imports->malloc = default_malloc;
661
   imports->calloc = default_calloc;
662
   imports->realloc = default_realloc;
663
   imports->free = default_free;
664
   imports->warning = default_warning;
665
   imports->fatal = default_fatal;
666
   imports->getenv = default_getenv; /* not used for now */
667
   imports->atoi = default_atoi;
668
   imports->sprintf = default_sprintf;
669
   imports->fopen = default_fopen;
670
   imports->fclose = default_fclose;
671
   imports->fprintf = default_fprintf;
672
   imports->getDrawablePrivate = default_GetDrawablePrivate;
673
   imports->other = driverCtx;
674
}