Subversion Repositories shark

Rev

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

Rev Author Line No. Line
72 giacomo 1
/* $Id: imports.c,v 1.2 2003-03-13 12:20:29 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
72 giacomo 275
   cprintf("Called _mesa_getenv\n");
55 pj 276
   return getenv(var);
277
#endif
278
}
279
 
280
 
281
char *
282
_mesa_strstr( const char *haystack, const char *needle )
283
{
284
#if defined(XFree86LOADER) && defined(IN_MODULE)
285
   return xf86strstr(haystack, needle);
286
#else
287
   return strstr(haystack, needle);
288
#endif
289
}
290
 
291
 
292
char *
293
_mesa_strncat( char *dest, const char *src, size_t n )
294
{
295
#if defined(XFree86LOADER) && defined(IN_MODULE)
296
   return xf86strncat(dest, src, n);
297
#else
298
   return strncat(dest, src, n);
299
#endif
300
}
301
 
302
 
303
char *
304
_mesa_strcpy( char *dest, const char *src )
305
{
306
#if defined(XFree86LOADER) && defined(IN_MODULE)
307
   return xf86strcpy(dest, src);
308
#else
309
   return strcpy(dest, src);
310
#endif
311
}
312
 
313
 
314
char *
315
_mesa_strncpy( char *dest, const char *src, size_t n )
316
{
317
#if defined(XFree86LOADER) && defined(IN_MODULE)
318
   return xf86strncpy(dest, src, n);
319
#else
320
   return strncpy(dest, src, n);
321
#endif
322
}
323
 
324
 
325
size_t
326
_mesa_strlen( const char *s )
327
{
328
#if defined(XFree86LOADER) && defined(IN_MODULE)
329
   return xf86strlen(s);
330
#else
331
   return strlen(s);
332
#endif
333
}
334
 
335
 
336
int
337
_mesa_strcmp( const char *s1, const char *s2 )
338
{
339
#if defined(XFree86LOADER) && defined(IN_MODULE)
340
   return xf86strcmp(s1, s2);
341
#else
342
   return strcmp(s1, s2);
343
#endif
344
}
345
 
346
 
347
int
348
_mesa_strncmp( const char *s1, const char *s2, size_t n )
349
{
350
#if defined(XFree86LOADER) && defined(IN_MODULE)
351
   return xf86strncmp(s1, s2, n);
352
#else
353
   return strncmp(s1, s2, n);
354
#endif
355
}
356
 
357
 
358
int
359
_mesa_atoi(const char *s)
360
{
361
#if defined(XFree86LOADER) && defined(IN_MODULE)
362
   return xf86atoi(s);
363
#else
72 giacomo 364
   return (int)atoi((char *)s);
55 pj 365
#endif
366
}
367
 
368
int
369
_mesa_sprintf( char *str, const char *fmt, ... )
370
{
371
   int r;
372
   va_list args;
72 giacomo 373
 
55 pj 374
   va_start( args, fmt );  
375
   va_end( args );
376
#if defined(XFree86LOADER) && defined(IN_MODULE)
377
   r = xf86vsprintf( str, fmt, args );
378
#else
72 giacomo 379
   cprintf("Called _mesa_sprintf\n");
380
   sprintf(str,"ERROR");
381
   r = 0;
55 pj 382
#endif
383
   return r;
384
}
385
 
386
void
387
_mesa_printf( const char *fmtString, ... )
388
{
389
   char s[MAXSTRING];
72 giacomo 390
   //va_list args;
391
 
392
   //va_start( args, fmtString );  
393
   //vsnprintf(s, MAXSTRING, fmtString, args);
394
   //va_end( args );
55 pj 395
#if defined(XFree86LOADER) && defined(IN_MODULE)
396
   xf86printf("%s", s);
397
#else
72 giacomo 398
   cprintf("Called _mesa_printf\n");
399
   cprintf("%s\n",fmtString);
55 pj 400
#endif
401
}
402
 
403
 
404
void
405
_mesa_warning( GLcontext *ctx, const char *fmtString, ... )
406
{
407
   GLboolean debug;
408
   char str[MAXSTRING];
72 giacomo 409
   //va_list args;
55 pj 410
   (void) ctx;
72 giacomo 411
 
412
   //va_start( args, fmtString );  
413
   //(void) vsnprintf( str, MAXSTRING, fmtString, args );
414
   //va_end( args );
55 pj 415
#ifdef DEBUG
416
   debug = GL_TRUE; /* always print warning */
417
#else
418
   debug = _mesa_getenv("MESA_DEBUG") ? GL_TRUE : GL_FALSE;
419
#endif
420
   if (debug) {
421
#if defined(XFree86LOADER) && defined(IN_MODULE)
422
      xf86fprintf(stderr, "Mesa warning: %s\n", str);
423
#else
72 giacomo 424
      cprintf("Called _mesa_warning\n");
425
      cprintf("ERROR: %s\n", fmtString);
55 pj 426
#endif
427
   }
428
}
429
 
430
 
431
/*
432
 * This function is called when the Mesa user has stumbled into a code
433
 * path which may not be implemented fully or correctly.
434
 */
435
void
436
_mesa_problem( const GLcontext *ctx, const char *s )
437
{
438
   (void) ctx;
439
#if defined(XFree86LOADER) && defined(IN_MODULE)
440
   xf86fprintf(stderr, "Mesa implementation error: %s\n", s);
441
   xf86fprintf(stderr, "Please report to the DRI project at dri.sourceforge.net\n");
442
#else
72 giacomo 443
   cprintf("Mesa implementation error: %s\n", s);
444
   cprintf("Please report to the Mesa bug database at www.mesa3d.org\n" );
55 pj 445
#endif
446
}
447
 
448
 
449
/*
450
 * If in debug mode, print error message to stdout.
451
 * Also, record the error code by calling _mesa_record_error().
452
 * Input:  ctx - the GL context
453
 *         error - the error value
454
 *         fmtString - printf-style format string, followed by optional args
455
 */
456
void
457
_mesa_error( GLcontext *ctx, GLenum error, const char *fmtString, ... )
458
{
459
   const char *debugEnv;
460
   GLboolean debug;
461
 
462
   debugEnv = _mesa_getenv("MESA_DEBUG");
463
 
464
#ifdef DEBUG
465
      debug = GL_TRUE;
466
#else
467
   if (debugEnv)
468
      debug = GL_TRUE;
469
   else
470
      debug = GL_FALSE;
471
#endif
472
 
473
   if (debug) {
72 giacomo 474
      /*
55 pj 475
      va_list args;
72 giacomo 476
      char where[MAXSTRING];*/
55 pj 477
      const char *errstr;
72 giacomo 478
      /*
55 pj 479
      va_start( args, fmtString );  
480
      vsnprintf( where, MAXSTRING, fmtString, args );
481
      va_end( args );
72 giacomo 482
      */
55 pj 483
      switch (error) {
484
         case GL_NO_ERROR:
485
            errstr = "GL_NO_ERROR";
486
            break;
487
         case GL_INVALID_VALUE:
488
            errstr = "GL_INVALID_VALUE";
489
            break;
490
         case GL_INVALID_ENUM:
491
            errstr = "GL_INVALID_ENUM";
492
            break;
493
         case GL_INVALID_OPERATION:
494
            errstr = "GL_INVALID_OPERATION";
495
            break;
496
         case GL_STACK_OVERFLOW:
497
            errstr = "GL_STACK_OVERFLOW";
498
            break;
499
         case GL_STACK_UNDERFLOW:
500
            errstr = "GL_STACK_UNDERFLOW";
501
            break;
502
         case GL_OUT_OF_MEMORY:
503
            errstr = "GL_OUT_OF_MEMORY";
504
            break;
505
         case GL_TABLE_TOO_LARGE:
506
            errstr = "GL_TABLE_TOO_LARGE";
507
            break;
508
         default:
509
            errstr = "unknown";
510
            break;
511
      }
72 giacomo 512
      _mesa_debug(ctx, "Mesa user error: %s in %s\n", errstr);
55 pj 513
   }
514
 
515
   _mesa_record_error(ctx, error);
516
}  
517
 
518
 
519
/*
520
 * Call this to report debug information.  Uses stderr.
521
 */
522
void
523
_mesa_debug( const GLcontext *ctx, const char *fmtString, ... )
524
{
525
   char s[MAXSTRING];
526
   va_list args;
72 giacomo 527
 
528
   /*
55 pj 529
   va_start(args, fmtString);
530
   vsnprintf(s, MAXSTRING, fmtString, args);
72 giacomo 531
   va_end(args);*/
55 pj 532
#if defined(XFree86LOADER) && defined(IN_MODULE)
533
   xf86fprintf(stderr, "Mesa: %s", s);
534
#else
72 giacomo 535
   cprintf("Called _mesa_debug\n");
536
   cprintf("ERROR: %s\n", fmtString);
55 pj 537
#endif
538
}
539
 
540
 
541
 
542
/**********************************************************************/
543
/* Default Imports Wrapper                                            */
544
/**********************************************************************/
545
 
546
static void *
547
default_malloc(__GLcontext *gc, size_t size)
548
{
549
   (void) gc;
550
   return _mesa_malloc(size);
551
}
552
 
553
static void *
554
default_calloc(__GLcontext *gc, size_t numElem, size_t elemSize)
555
{
556
   (void) gc;
557
   return _mesa_calloc(numElem * elemSize);
558
}
559
 
560
static void *
561
default_realloc(__GLcontext *gc, void *oldAddr, size_t newSize)
562
{
563
   (void) gc;
564
#if defined(XFree86LOADER) && defined(IN_MODULE)
565
   return xf86realloc(oldAddr, newSize);
566
#else
567
   return realloc(oldAddr, newSize);
568
#endif
569
}
570
 
571
static void
572
default_free(__GLcontext *gc, void *addr)
573
{
574
   (void) gc;
575
   _mesa_free(addr);
576
}
577
 
578
static char * CAPI
579
default_getenv( __GLcontext *gc, const char *var )
580
{
581
   (void) gc;
582
   return _mesa_getenv(var);
583
}
584
 
585
static void
586
default_warning(__GLcontext *gc, char *str)
587
{
588
   _mesa_warning(gc, str);
589
}
590
 
591
static void
592
default_fatal(__GLcontext *gc, char *str)
593
{
594
   _mesa_problem(gc, str);
595
   abort();
596
}
597
 
598
static int CAPI
599
default_atoi(__GLcontext *gc, const char *str)
600
{
601
   (void) gc;
72 giacomo 602
   return (int)atoi((char *)str);
55 pj 603
}
604
 
605
static int CAPI
606
default_sprintf(__GLcontext *gc, char *str, const char *fmt, ...)
607
{
608
   int r;
72 giacomo 609
   //va_list args;
610
   //va_start( args, fmt );  
611
   //r = vsprintf( str, fmt, args );
612
   //va_end( args );
613
   cprintf("Called default_sprintf\n");
614
   sprintf(str, "ERROR");
615
   r = 0;
55 pj 616
   return r;
617
}
618
 
619
static void * CAPI
620
default_fopen(__GLcontext *gc, const char *path, const char *mode)
621
{
72 giacomo 622
   return NULL;//fopen(path, mode);
55 pj 623
}
624
 
625
static int CAPI
626
default_fclose(__GLcontext *gc, void *stream)
627
{
72 giacomo 628
   return 0;//fclose((FILE *) stream);
55 pj 629
}
630
 
631
static int CAPI
632
default_fprintf(__GLcontext *gc, void *stream, const char *fmt, ...)
633
{
634
   int r;
72 giacomo 635
   char s[200];
636
   /*
55 pj 637
   va_list args;
638
   va_start( args, fmt );  
72 giacomo 639
   r = vsprintf(s, fmt, args );
55 pj 640
   va_end( args );
72 giacomo 641
   */
642
   cprintf("Called default_fprintf\n");
643
   cprintf("ERROR: %s\n",fmt);
644
   r = 0;
55 pj 645
   return r;
646
}
647
 
648
/* XXX this really is driver-specific and can't be here */
649
static __GLdrawablePrivate *
650
default_GetDrawablePrivate(__GLcontext *gc)
651
{
652
   return NULL;
653
}
654
 
655
 
656
 
657
 
658
/*
659
 * Initialize a __GLimports object to point to the functions in
660
 * this file.  This is to be called from device drivers.
661
 * Input:  imports - the object to init
662
 *         driverCtx - pointer to device driver-specific data
663
 */
664
void
665
_mesa_init_default_imports(__GLimports *imports, void *driverCtx)
666
{
667
   imports->malloc = default_malloc;
668
   imports->calloc = default_calloc;
669
   imports->realloc = default_realloc;
670
   imports->free = default_free;
671
   imports->warning = default_warning;
672
   imports->fatal = default_fatal;
673
   imports->getenv = default_getenv; /* not used for now */
674
   imports->atoi = default_atoi;
675
   imports->sprintf = default_sprintf;
676
   imports->fopen = default_fopen;
677
   imports->fclose = default_fclose;
678
   imports->fprintf = default_fprintf;
679
   imports->getDrawablePrivate = default_GetDrawablePrivate;
680
   imports->other = driverCtx;
681
}