Remove useless checks for NULL before freeing
[mesa.git] / src / mesa / main / imports.c
1 /**
2 * \file imports.c
3 * Standard C library function wrappers.
4 *
5 * Imports are services which the device driver or window system or
6 * operating system provides to the core renderer. The core renderer (Mesa)
7 * will call these functions in order to do memory allocation, simple I/O,
8 * etc.
9 *
10 * Some drivers will want to override/replace this file with something
11 * specialized, but that'll be rare.
12 *
13 * Eventually, I want to move roll the glheader.h file into this.
14 *
15 * \todo Functions still needed:
16 * - scanf
17 * - qsort
18 * - rand and RAND_MAX
19 */
20
21 /*
22 * Mesa 3-D graphics library
23 * Version: 7.1
24 *
25 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
26 *
27 * Permission is hereby granted, free of charge, to any person obtaining a
28 * copy of this software and associated documentation files (the "Software"),
29 * to deal in the Software without restriction, including without limitation
30 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
31 * and/or sell copies of the Software, and to permit persons to whom the
32 * Software is furnished to do so, subject to the following conditions:
33 *
34 * The above copyright notice and this permission notice shall be included
35 * in all copies or substantial portions of the Software.
36 *
37 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
38 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
39 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
40 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
41 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
42 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
43 */
44
45
46
47 #include "imports.h"
48 #include "context.h"
49 #include "mtypes.h"
50 #include "version.h"
51
52 #ifdef _GNU_SOURCE
53 #include <locale.h>
54 #ifdef __APPLE__
55 #include <xlocale.h>
56 #endif
57 #endif
58
59
60 #ifdef _WIN32
61 #define vsnprintf _vsnprintf
62 #elif defined(__IBMC__) || defined(__IBMCPP__) || ( defined(__VMS) && __CRTL_VER < 70312000 )
63 extern int vsnprintf(char *str, size_t count, const char *fmt, va_list arg);
64 #ifdef __VMS
65 #include "vsnprintf.c"
66 #endif
67 #endif
68
69 /**********************************************************************/
70 /** \name Memory */
71 /*@{*/
72
73 /**
74 * Allocate aligned memory.
75 *
76 * \param bytes number of bytes to allocate.
77 * \param alignment alignment (must be greater than zero).
78 *
79 * Allocates extra memory to accommodate rounding up the address for
80 * alignment and to record the real malloc address.
81 *
82 * \sa _mesa_align_free().
83 */
84 void *
85 _mesa_align_malloc(size_t bytes, unsigned long alignment)
86 {
87 #if defined(HAVE_POSIX_MEMALIGN)
88 void *mem;
89 int err = posix_memalign(& mem, alignment, bytes);
90 if (err)
91 return NULL;
92 return mem;
93 #elif defined(_WIN32) && defined(_MSC_VER)
94 return _aligned_malloc(bytes, alignment);
95 #else
96 uintptr_t ptr, buf;
97
98 ASSERT( alignment > 0 );
99
100 ptr = malloc(bytes + alignment + sizeof(void *));
101 if (!ptr)
102 return NULL;
103
104 buf = (ptr + alignment + sizeof(void *)) & ~(uintptr_t)(alignment - 1);
105 *(uintptr_t *)(buf - sizeof(void *)) = ptr;
106
107 #ifdef DEBUG
108 /* mark the non-aligned area */
109 while ( ptr < buf - sizeof(void *) ) {
110 *(unsigned long *)ptr = 0xcdcdcdcd;
111 ptr += sizeof(unsigned long);
112 }
113 #endif
114
115 return (void *) buf;
116 #endif /* defined(HAVE_POSIX_MEMALIGN) */
117 }
118
119 /**
120 * Same as _mesa_align_malloc(), but using calloc(1, ) instead of
121 * malloc()
122 */
123 void *
124 _mesa_align_calloc(size_t bytes, unsigned long alignment)
125 {
126 #if defined(HAVE_POSIX_MEMALIGN)
127 void *mem;
128
129 mem = _mesa_align_malloc(bytes, alignment);
130 if (mem != NULL) {
131 (void) memset(mem, 0, bytes);
132 }
133
134 return mem;
135 #elif defined(_WIN32) && defined(_MSC_VER)
136 void *mem;
137
138 mem = _aligned_malloc(bytes, alignment);
139 if (mem != NULL) {
140 (void) memset(mem, 0, bytes);
141 }
142
143 return mem;
144 #else
145 uintptr_t ptr, buf;
146
147 ASSERT( alignment > 0 );
148
149 ptr = calloc(1, bytes + alignment + sizeof(void *));
150 if (!ptr)
151 return NULL;
152
153 buf = (ptr + alignment + sizeof(void *)) & ~(uintptr_t)(alignment - 1);
154 *(uintptr_t *)(buf - sizeof(void *)) = ptr;
155
156 #ifdef DEBUG
157 /* mark the non-aligned area */
158 while ( ptr < buf - sizeof(void *) ) {
159 *(unsigned long *)ptr = 0xcdcdcdcd;
160 ptr += sizeof(unsigned long);
161 }
162 #endif
163
164 return (void *)buf;
165 #endif /* defined(HAVE_POSIX_MEMALIGN) */
166 }
167
168 /**
169 * Free memory which was allocated with either _mesa_align_malloc()
170 * or _mesa_align_calloc().
171 * \param ptr pointer to the memory to be freed.
172 * The actual address to free is stored in the word immediately before the
173 * address the client sees.
174 */
175 void
176 _mesa_align_free(void *ptr)
177 {
178 #if defined(HAVE_POSIX_MEMALIGN)
179 free(ptr);
180 #elif defined(_WIN32) && defined(_MSC_VER)
181 _aligned_free(ptr);
182 #else
183 void **cubbyHole = (void **) ((char *) ptr - sizeof(void *));
184 void *realAddr = *cubbyHole;
185 free(realAddr);
186 #endif /* defined(HAVE_POSIX_MEMALIGN) */
187 }
188
189 /**
190 * Reallocate memory, with alignment.
191 */
192 void *
193 _mesa_align_realloc(void *oldBuffer, size_t oldSize, size_t newSize,
194 unsigned long alignment)
195 {
196 #if defined(_WIN32) && defined(_MSC_VER)
197 (void) oldSize;
198 return _aligned_realloc(oldBuffer, newSize, alignment);
199 #else
200 const size_t copySize = (oldSize < newSize) ? oldSize : newSize;
201 void *newBuf = _mesa_align_malloc(newSize, alignment);
202 if (newBuf && oldBuffer && copySize > 0) {
203 memcpy(newBuf, oldBuffer, copySize);
204 }
205 if (oldBuffer)
206 _mesa_align_free(oldBuffer);
207 return newBuf;
208 #endif
209 }
210
211
212
213 /** Reallocate memory */
214 void *
215 _mesa_realloc(void *oldBuffer, size_t oldSize, size_t newSize)
216 {
217 const size_t copySize = (oldSize < newSize) ? oldSize : newSize;
218 void *newBuffer = malloc(newSize);
219 if (newBuffer && oldBuffer && copySize > 0)
220 memcpy(newBuffer, oldBuffer, copySize);
221 free(oldBuffer);
222 return newBuffer;
223 }
224
225 /*@}*/
226
227
228 /**********************************************************************/
229 /** \name Math */
230 /*@{*/
231
232
233 #ifndef __GNUC__
234 /**
235 * Find the first bit set in a word.
236 */
237 int
238 ffs(int i)
239 {
240 register int bit = 0;
241 if (i != 0) {
242 if ((i & 0xffff) == 0) {
243 bit += 16;
244 i >>= 16;
245 }
246 if ((i & 0xff) == 0) {
247 bit += 8;
248 i >>= 8;
249 }
250 if ((i & 0xf) == 0) {
251 bit += 4;
252 i >>= 4;
253 }
254 while ((i & 1) == 0) {
255 bit++;
256 i >>= 1;
257 }
258 bit++;
259 }
260 return bit;
261 }
262
263
264 /**
265 * Find position of first bit set in given value.
266 * XXX Warning: this function can only be used on 64-bit systems!
267 * \return position of least-significant bit set, starting at 1, return zero
268 * if no bits set.
269 */
270 int
271 ffsll(long long int val)
272 {
273 int bit;
274
275 assert(sizeof(val) == 8);
276
277 bit = ffs((int) val);
278 if (bit != 0)
279 return bit;
280
281 bit = ffs((int) (val >> 32));
282 if (bit != 0)
283 return 32 + bit;
284
285 return 0;
286 }
287 #endif /* __GNUC__ */
288
289
290 #if !defined(__GNUC__) ||\
291 ((__GNUC__ * 100 + __GNUC_MINOR__) < 304) /* Not gcc 3.4 or later */
292 /**
293 * Return number of bits set in given GLuint.
294 */
295 unsigned int
296 _mesa_bitcount(unsigned int n)
297 {
298 unsigned int bits;
299 for (bits = 0; n > 0; n = n >> 1) {
300 bits += (n & 1);
301 }
302 return bits;
303 }
304
305 /**
306 * Return number of bits set in given 64-bit uint.
307 */
308 unsigned int
309 _mesa_bitcount_64(uint64_t n)
310 {
311 unsigned int bits;
312 for (bits = 0; n > 0; n = n >> 1) {
313 bits += (n & 1);
314 }
315 return bits;
316 }
317 #endif
318
319
320 /**
321 * Convert a 4-byte float to a 2-byte half float.
322 * Based on code from:
323 * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
324 */
325 GLhalfARB
326 _mesa_float_to_half(float val)
327 {
328 const fi_type fi = {val};
329 const int flt_m = fi.i & 0x7fffff;
330 const int flt_e = (fi.i >> 23) & 0xff;
331 const int flt_s = (fi.i >> 31) & 0x1;
332 int s, e, m = 0;
333 GLhalfARB result;
334
335 /* sign bit */
336 s = flt_s;
337
338 /* handle special cases */
339 if ((flt_e == 0) && (flt_m == 0)) {
340 /* zero */
341 /* m = 0; - already set */
342 e = 0;
343 }
344 else if ((flt_e == 0) && (flt_m != 0)) {
345 /* denorm -- denorm float maps to 0 half */
346 /* m = 0; - already set */
347 e = 0;
348 }
349 else if ((flt_e == 0xff) && (flt_m == 0)) {
350 /* infinity */
351 /* m = 0; - already set */
352 e = 31;
353 }
354 else if ((flt_e == 0xff) && (flt_m != 0)) {
355 /* NaN */
356 m = 1;
357 e = 31;
358 }
359 else {
360 /* regular number */
361 const int new_exp = flt_e - 127;
362 if (new_exp < -24) {
363 /* this maps to 0 */
364 /* m = 0; - already set */
365 e = 0;
366 }
367 else if (new_exp < -14) {
368 /* this maps to a denorm */
369 unsigned int exp_val = (unsigned int) (-14 - new_exp); /* 2^-exp_val*/
370 e = 0;
371 switch (exp_val) {
372 case 0:
373 _mesa_warning(NULL,
374 "float_to_half: logical error in denorm creation!\n");
375 /* m = 0; - already set */
376 break;
377 case 1: m = 512 + (flt_m >> 14); break;
378 case 2: m = 256 + (flt_m >> 15); break;
379 case 3: m = 128 + (flt_m >> 16); break;
380 case 4: m = 64 + (flt_m >> 17); break;
381 case 5: m = 32 + (flt_m >> 18); break;
382 case 6: m = 16 + (flt_m >> 19); break;
383 case 7: m = 8 + (flt_m >> 20); break;
384 case 8: m = 4 + (flt_m >> 21); break;
385 case 9: m = 2 + (flt_m >> 22); break;
386 case 10: m = 1; break;
387 }
388 }
389 else if (new_exp > 15) {
390 /* map this value to infinity */
391 /* m = 0; - already set */
392 e = 31;
393 }
394 else {
395 /* regular */
396 e = new_exp + 15;
397 m = flt_m >> 13;
398 }
399 }
400
401 result = (s << 15) | (e << 10) | m;
402 return result;
403 }
404
405
406 /**
407 * Convert a 2-byte half float to a 4-byte float.
408 * Based on code from:
409 * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
410 */
411 float
412 _mesa_half_to_float(GLhalfARB val)
413 {
414 /* XXX could also use a 64K-entry lookup table */
415 const int m = val & 0x3ff;
416 const int e = (val >> 10) & 0x1f;
417 const int s = (val >> 15) & 0x1;
418 int flt_m, flt_e, flt_s;
419 fi_type fi;
420 float result;
421
422 /* sign bit */
423 flt_s = s;
424
425 /* handle special cases */
426 if ((e == 0) && (m == 0)) {
427 /* zero */
428 flt_m = 0;
429 flt_e = 0;
430 }
431 else if ((e == 0) && (m != 0)) {
432 /* denorm -- denorm half will fit in non-denorm single */
433 const float half_denorm = 1.0f / 16384.0f; /* 2^-14 */
434 float mantissa = ((float) (m)) / 1024.0f;
435 float sign = s ? -1.0f : 1.0f;
436 return sign * mantissa * half_denorm;
437 }
438 else if ((e == 31) && (m == 0)) {
439 /* infinity */
440 flt_e = 0xff;
441 flt_m = 0;
442 }
443 else if ((e == 31) && (m != 0)) {
444 /* NaN */
445 flt_e = 0xff;
446 flt_m = 1;
447 }
448 else {
449 /* regular */
450 flt_e = e + 112;
451 flt_m = m << 13;
452 }
453
454 fi.i = (flt_s << 31) | (flt_e << 23) | flt_m;
455 result = fi.f;
456 return result;
457 }
458
459 /*@}*/
460
461
462 /**********************************************************************/
463 /** \name Sort & Search */
464 /*@{*/
465
466 /**
467 * Wrapper for bsearch().
468 */
469 void *
470 _mesa_bsearch( const void *key, const void *base, size_t nmemb, size_t size,
471 int (*compar)(const void *, const void *) )
472 {
473 #if defined(_WIN32_WCE)
474 void *mid;
475 int cmp;
476 while (nmemb) {
477 nmemb >>= 1;
478 mid = (char *)base + nmemb * size;
479 cmp = (*compar)(key, mid);
480 if (cmp == 0)
481 return mid;
482 if (cmp > 0) {
483 base = (char *)mid + size;
484 --nmemb;
485 }
486 }
487 return NULL;
488 #else
489 return bsearch(key, base, nmemb, size, compar);
490 #endif
491 }
492
493 /*@}*/
494
495
496 /**********************************************************************/
497 /** \name Environment vars */
498 /*@{*/
499
500 /**
501 * Wrapper for getenv().
502 */
503 char *
504 _mesa_getenv( const char *var )
505 {
506 #if defined(_XBOX) || defined(_WIN32_WCE)
507 return NULL;
508 #else
509 return getenv(var);
510 #endif
511 }
512
513 /*@}*/
514
515
516 /**********************************************************************/
517 /** \name String */
518 /*@{*/
519
520 /**
521 * Implemented using malloc() and strcpy.
522 * Note that NULL is handled accordingly.
523 */
524 char *
525 _mesa_strdup( const char *s )
526 {
527 if (s) {
528 size_t l = strlen(s);
529 char *s2 = malloc(l + 1);
530 if (s2)
531 strcpy(s2, s);
532 return s2;
533 }
534 else {
535 return NULL;
536 }
537 }
538
539 /** Wrapper around strtof() */
540 float
541 _mesa_strtof( const char *s, char **end )
542 {
543 #if defined(_GNU_SOURCE) && !defined(__CYGWIN__) && !defined(__FreeBSD__) && \
544 !defined(ANDROID) && !defined(__HAIKU__) && !defined(__UCLIBC__)
545 static locale_t loc = NULL;
546 if (!loc) {
547 loc = newlocale(LC_CTYPE_MASK, "C", NULL);
548 }
549 return strtof_l(s, end, loc);
550 #elif defined(_ISOC99_SOURCE) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
551 return strtof(s, end);
552 #else
553 return (float)strtod(s, end);
554 #endif
555 }
556
557 /** Compute simple checksum/hash for a string */
558 unsigned int
559 _mesa_str_checksum(const char *str)
560 {
561 /* This could probably be much better */
562 unsigned int sum, i;
563 const char *c;
564 sum = i = 1;
565 for (c = str; *c; c++, i++)
566 sum += *c * (i % 100);
567 return sum + i;
568 }
569
570
571 /*@}*/
572
573
574 /** Needed due to #ifdef's, above. */
575 int
576 _mesa_vsnprintf(char *str, size_t size, const char *fmt, va_list args)
577 {
578 return vsnprintf( str, size, fmt, args);
579 }
580
581 /** Wrapper around vsnprintf() */
582 int
583 _mesa_snprintf( char *str, size_t size, const char *fmt, ... )
584 {
585 int r;
586 va_list args;
587 va_start( args, fmt );
588 r = vsnprintf( str, size, fmt, args );
589 va_end( args );
590 return r;
591 }
592
593