3a73275ffd62748fd259113ac60b017d31cc79cd
[mesa.git] / src / util / imports.h
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /**
27 * \file imports.h
28 * Standard C library function wrappers.
29 *
30 * This file provides wrappers for all the standard C library functions
31 * like malloc(), free(), printf(), getenv(), etc.
32 */
33
34
35 #ifndef IMPORTS_H
36 #define IMPORTS_H
37
38
39 #include <stdlib.h>
40 #include <stdarg.h>
41 #include <string.h>
42 #include "util/compiler.h"
43 #include "util/bitscan.h"
44 #include "util/u_memory.h"
45
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49
50
51 /*
52 * For GL_ARB_vertex_buffer_object we need to treat vertex array pointers
53 * as offsets into buffer stores. Since the vertex array pointer and
54 * buffer store pointer are both pointers and we need to add them, we use
55 * this macro.
56 * Both pointers/offsets are expressed in bytes.
57 */
58 #define ADD_POINTERS(A, B) ( (uint8_t *) (A) + (uintptr_t) (B) )
59
60
61 /**
62 * Sometimes we treat floats as ints. On x86 systems, moving a float
63 * as an int (thereby using integer registers instead of FP registers) is
64 * a performance win. Typically, this can be done with ordinary casts.
65 * But with gcc's -fstrict-aliasing flag (which defaults to on in gcc 3.0)
66 * these casts generate warnings.
67 * The following union typedef is used to solve that.
68 */
69 typedef union { float f; int i; unsigned u; } fi_type;
70
71
72
73 /*@}*/
74
75
76 /**
77 * finite macro.
78 */
79 #if defined(_MSC_VER)
80 # define finite _finite
81 #endif
82
83
84 /***
85 *** IS_INF_OR_NAN: test if float is infinite or NaN
86 ***/
87 #if defined(isfinite)
88 #define IS_INF_OR_NAN(x) (!isfinite(x))
89 #elif defined(finite)
90 #define IS_INF_OR_NAN(x) (!finite(x))
91 #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
92 #define IS_INF_OR_NAN(x) (!isfinite(x))
93 #else
94 #define IS_INF_OR_NAN(x) (!finite(x))
95 #endif
96
97
98 /**
99 * Convert float to int by rounding to nearest integer, away from zero.
100 */
101 static inline int IROUND(float f)
102 {
103 return (int) ((f >= 0.0F) ? (f + 0.5F) : (f - 0.5F));
104 }
105
106 /**
107 * Convert float to int64 by rounding to nearest integer.
108 */
109 static inline int64_t IROUND64(float f)
110 {
111 return (int64_t) ((f >= 0.0F) ? (f + 0.5F) : (f - 0.5F));
112 }
113
114
115 /**********************************************************************
116 * Functions
117 */
118
119 extern void *
120 _mesa_align_malloc( size_t bytes, unsigned long alignment );
121
122 extern void *
123 _mesa_align_calloc( size_t bytes, unsigned long alignment );
124
125 extern void
126 _mesa_align_free( void *ptr );
127
128 extern void *
129 _mesa_align_realloc(void *oldBuffer, size_t oldSize, size_t newSize,
130 unsigned long alignment);
131
132 extern int
133 _mesa_snprintf( char *str, size_t size, const char *fmt, ... ) PRINTFLIKE(3, 4);
134
135 extern int
136 _mesa_vsnprintf(char *str, size_t size, const char *fmt, va_list arg);
137
138
139 #if defined(_WIN32) && !defined(HAVE_STRTOK_R)
140 #define strtok_r strtok_s
141 #endif
142
143 #ifdef __cplusplus
144 }
145 #endif
146
147
148 #endif /* IMPORTS_H */