mesa: add ARB_vertex_attrib_64bit VertexArrayVertexAttribLOffsetEXT
[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 *
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 #include "context.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__)
63 extern int vsnprintf(char *str, size_t count, const char *fmt, va_list arg);
64 #endif
65
66 /**********************************************************************/
67 /** \name Memory */
68 /*@{*/
69
70 /**
71 * Allocate aligned memory.
72 *
73 * \param bytes number of bytes to allocate.
74 * \param alignment alignment (must be greater than zero).
75 *
76 * Allocates extra memory to accommodate rounding up the address for
77 * alignment and to record the real malloc address.
78 *
79 * \sa _mesa_align_free().
80 */
81 void *
82 _mesa_align_malloc(size_t bytes, unsigned long alignment)
83 {
84 #if defined(HAVE_POSIX_MEMALIGN)
85 void *mem;
86 int err = posix_memalign(& mem, alignment, bytes);
87 if (err)
88 return NULL;
89 return mem;
90 #elif defined(_WIN32)
91 return _aligned_malloc(bytes, alignment);
92 #else
93 uintptr_t ptr, buf;
94
95 assert( alignment > 0 );
96
97 ptr = (uintptr_t)malloc(bytes + alignment + sizeof(void *));
98 if (!ptr)
99 return NULL;
100
101 buf = (ptr + alignment + sizeof(void *)) & ~(uintptr_t)(alignment - 1);
102 *(uintptr_t *)(buf - sizeof(void *)) = ptr;
103
104 #ifndef NDEBUG
105 /* mark the non-aligned area */
106 while ( ptr < buf - sizeof(void *) ) {
107 *(unsigned long *)ptr = 0xcdcdcdcd;
108 ptr += sizeof(unsigned long);
109 }
110 #endif
111
112 return (void *) buf;
113 #endif /* defined(HAVE_POSIX_MEMALIGN) */
114 }
115
116 /**
117 * Same as _mesa_align_malloc(), but using calloc(1, ) instead of
118 * malloc()
119 */
120 void *
121 _mesa_align_calloc(size_t bytes, unsigned long alignment)
122 {
123 #if defined(HAVE_POSIX_MEMALIGN)
124 void *mem;
125
126 mem = _mesa_align_malloc(bytes, alignment);
127 if (mem != NULL) {
128 (void) memset(mem, 0, bytes);
129 }
130
131 return mem;
132 #elif defined(_WIN32)
133 void *mem;
134
135 mem = _aligned_malloc(bytes, alignment);
136 if (mem != NULL) {
137 (void) memset(mem, 0, bytes);
138 }
139
140 return mem;
141 #else
142 uintptr_t ptr, buf;
143
144 assert( alignment > 0 );
145
146 ptr = (uintptr_t)calloc(1, bytes + alignment + sizeof(void *));
147 if (!ptr)
148 return NULL;
149
150 buf = (ptr + alignment + sizeof(void *)) & ~(uintptr_t)(alignment - 1);
151 *(uintptr_t *)(buf - sizeof(void *)) = ptr;
152
153 #ifndef NDEBUG
154 /* mark the non-aligned area */
155 while ( ptr < buf - sizeof(void *) ) {
156 *(unsigned long *)ptr = 0xcdcdcdcd;
157 ptr += sizeof(unsigned long);
158 }
159 #endif
160
161 return (void *)buf;
162 #endif /* defined(HAVE_POSIX_MEMALIGN) */
163 }
164
165 /**
166 * Free memory which was allocated with either _mesa_align_malloc()
167 * or _mesa_align_calloc().
168 * \param ptr pointer to the memory to be freed.
169 * The actual address to free is stored in the word immediately before the
170 * address the client sees.
171 * Note that it is legal to pass NULL pointer to this function and will be
172 * handled accordingly.
173 */
174 void
175 _mesa_align_free(void *ptr)
176 {
177 #if defined(HAVE_POSIX_MEMALIGN)
178 free(ptr);
179 #elif defined(_WIN32)
180 _aligned_free(ptr);
181 #else
182 if (ptr) {
183 void **cubbyHole = (void **) ((char *) ptr - sizeof(void *));
184 void *realAddr = *cubbyHole;
185 free(realAddr);
186 }
187 #endif /* defined(HAVE_POSIX_MEMALIGN) */
188 }
189
190 /**
191 * Reallocate memory, with alignment.
192 */
193 void *
194 _mesa_align_realloc(void *oldBuffer, size_t oldSize, size_t newSize,
195 unsigned long alignment)
196 {
197 #if defined(_WIN32)
198 (void) oldSize;
199 return _aligned_realloc(oldBuffer, newSize, alignment);
200 #else
201 const size_t copySize = (oldSize < newSize) ? oldSize : newSize;
202 void *newBuf = _mesa_align_malloc(newSize, alignment);
203 if (newBuf && oldBuffer && copySize > 0) {
204 memcpy(newBuf, oldBuffer, copySize);
205 }
206
207 _mesa_align_free(oldBuffer);
208 return newBuf;
209 #endif
210 }
211
212 /*@}*/
213
214
215 /** Needed due to #ifdef's, above. */
216 int
217 _mesa_vsnprintf(char *str, size_t size, const char *fmt, va_list args)
218 {
219 return vsnprintf( str, size, fmt, args);
220 }
221
222 /** Wrapper around vsnprintf() */
223 int
224 _mesa_snprintf( char *str, size_t size, const char *fmt, ... )
225 {
226 int r;
227 va_list args;
228 va_start( args, fmt );
229 r = vsnprintf( str, size, fmt, args );
230 va_end( args );
231 return r;
232 }
233
234