Move compiler.h and imports.h/c from src/mesa/main into src/util
[mesa.git] / src / util / 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 *
24 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
25 *
26 * Permission is hereby granted, free of charge, to any person obtaining a
27 * copy of this software and associated documentation files (the "Software"),
28 * to deal in the Software without restriction, including without limitation
29 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
30 * and/or sell copies of the Software, and to permit persons to whom the
31 * Software is furnished to do so, subject to the following conditions:
32 *
33 * The above copyright notice and this permission notice shall be included
34 * in all copies or substantial portions of the Software.
35 *
36 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
37 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
39 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
40 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
41 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
42 * OTHER DEALINGS IN THE SOFTWARE.
43 */
44
45 #include <stdio.h>
46 #include <stdarg.h>
47 #include "c99_math.h"
48 #include "imports.h"
49
50 #ifdef _GNU_SOURCE
51 #include <locale.h>
52 #ifdef __APPLE__
53 #include <xlocale.h>
54 #endif
55 #endif
56
57
58 #ifdef _WIN32
59 #define vsnprintf _vsnprintf
60 #elif defined(__IBMC__) || defined(__IBMCPP__)
61 extern int vsnprintf(char *str, size_t count, const char *fmt, va_list arg);
62 #endif
63
64 /**********************************************************************/
65 /** \name Memory */
66 /*@{*/
67
68 /**
69 * Allocate aligned memory.
70 *
71 * \param bytes number of bytes to allocate.
72 * \param alignment alignment (must be greater than zero).
73 *
74 * Allocates extra memory to accommodate rounding up the address for
75 * alignment and to record the real malloc address.
76 *
77 * \sa _mesa_align_free().
78 */
79 void *
80 _mesa_align_malloc(size_t bytes, unsigned long alignment)
81 {
82 #if defined(HAVE_POSIX_MEMALIGN)
83 void *mem;
84 int err = posix_memalign(& mem, alignment, bytes);
85 if (err)
86 return NULL;
87 return mem;
88 #elif defined(_WIN32)
89 return _aligned_malloc(bytes, alignment);
90 #else
91 uintptr_t ptr, buf;
92
93 assert( alignment > 0 );
94
95 ptr = (uintptr_t)malloc(bytes + alignment + sizeof(void *));
96 if (!ptr)
97 return NULL;
98
99 buf = (ptr + alignment + sizeof(void *)) & ~(uintptr_t)(alignment - 1);
100 *(uintptr_t *)(buf - sizeof(void *)) = ptr;
101
102 #ifndef NDEBUG
103 /* mark the non-aligned area */
104 while ( ptr < buf - sizeof(void *) ) {
105 *(unsigned long *)ptr = 0xcdcdcdcd;
106 ptr += sizeof(unsigned long);
107 }
108 #endif
109
110 return (void *) buf;
111 #endif /* defined(HAVE_POSIX_MEMALIGN) */
112 }
113
114 /**
115 * Same as _mesa_align_malloc(), but using calloc(1, ) instead of
116 * malloc()
117 */
118 void *
119 _mesa_align_calloc(size_t bytes, unsigned long alignment)
120 {
121 #if defined(HAVE_POSIX_MEMALIGN)
122 void *mem;
123
124 mem = _mesa_align_malloc(bytes, alignment);
125 if (mem != NULL) {
126 (void) memset(mem, 0, bytes);
127 }
128
129 return mem;
130 #elif defined(_WIN32)
131 void *mem;
132
133 mem = _aligned_malloc(bytes, alignment);
134 if (mem != NULL) {
135 (void) memset(mem, 0, bytes);
136 }
137
138 return mem;
139 #else
140 uintptr_t ptr, buf;
141
142 assert( alignment > 0 );
143
144 ptr = (uintptr_t)calloc(1, bytes + alignment + sizeof(void *));
145 if (!ptr)
146 return NULL;
147
148 buf = (ptr + alignment + sizeof(void *)) & ~(uintptr_t)(alignment - 1);
149 *(uintptr_t *)(buf - sizeof(void *)) = ptr;
150
151 #ifndef NDEBUG
152 /* mark the non-aligned area */
153 while ( ptr < buf - sizeof(void *) ) {
154 *(unsigned long *)ptr = 0xcdcdcdcd;
155 ptr += sizeof(unsigned long);
156 }
157 #endif
158
159 return (void *)buf;
160 #endif /* defined(HAVE_POSIX_MEMALIGN) */
161 }
162
163 /**
164 * Free memory which was allocated with either _mesa_align_malloc()
165 * or _mesa_align_calloc().
166 * \param ptr pointer to the memory to be freed.
167 * The actual address to free is stored in the word immediately before the
168 * address the client sees.
169 * Note that it is legal to pass NULL pointer to this function and will be
170 * handled accordingly.
171 */
172 void
173 _mesa_align_free(void *ptr)
174 {
175 #if defined(HAVE_POSIX_MEMALIGN)
176 free(ptr);
177 #elif defined(_WIN32)
178 _aligned_free(ptr);
179 #else
180 if (ptr) {
181 void **cubbyHole = (void **) ((char *) ptr - sizeof(void *));
182 void *realAddr = *cubbyHole;
183 free(realAddr);
184 }
185 #endif /* defined(HAVE_POSIX_MEMALIGN) */
186 }
187
188 /**
189 * Reallocate memory, with alignment.
190 */
191 void *
192 _mesa_align_realloc(void *oldBuffer, size_t oldSize, size_t newSize,
193 unsigned long alignment)
194 {
195 #if defined(_WIN32)
196 (void) oldSize;
197 return _aligned_realloc(oldBuffer, newSize, alignment);
198 #else
199 const size_t copySize = (oldSize < newSize) ? oldSize : newSize;
200 void *newBuf = _mesa_align_malloc(newSize, alignment);
201 if (newBuf && oldBuffer && copySize > 0) {
202 memcpy(newBuf, oldBuffer, copySize);
203 }
204
205 _mesa_align_free(oldBuffer);
206 return newBuf;
207 #endif
208 }
209
210 /*@}*/
211
212
213 /** Needed due to #ifdef's, above. */
214 int
215 _mesa_vsnprintf(char *str, size_t size, const char *fmt, va_list args)
216 {
217 return vsnprintf( str, size, fmt, args);
218 }
219
220 /** Wrapper around vsnprintf() */
221 int
222 _mesa_snprintf( char *str, size_t size, const char *fmt, ... )
223 {
224 int r;
225 va_list args;
226 va_start( args, fmt );
227 r = vsnprintf( str, size, fmt, args );
228 va_end( args );
229 return r;
230 }
231
232