3 * Standard C library function wrappers.
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,
10 * Some drivers will want to override/replace this file with something
11 * specialized, but that'll be rare.
13 * Eventually, I want to move roll the glheader.h file into this.
15 * \todo Functions still needed:
22 * Mesa 3-D graphics library
24 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
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:
33 * The above copyright notice and this permission notice shall be included
34 * in all copies or substantial portions of the Software.
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.
48 #include "util/rounding.h" /* for _mesa_roundeven */
63 #define vsnprintf _vsnprintf
64 #elif defined(__IBMC__) || defined(__IBMCPP__)
65 extern int vsnprintf(char *str
, size_t count
, const char *fmt
, va_list arg
);
68 /**********************************************************************/
73 * Allocate aligned memory.
75 * \param bytes number of bytes to allocate.
76 * \param alignment alignment (must be greater than zero).
78 * Allocates extra memory to accommodate rounding up the address for
79 * alignment and to record the real malloc address.
81 * \sa _mesa_align_free().
84 _mesa_align_malloc(size_t bytes
, unsigned long alignment
)
86 #if defined(HAVE_POSIX_MEMALIGN)
88 int err
= posix_memalign(& mem
, alignment
, bytes
);
93 return _aligned_malloc(bytes
, alignment
);
97 assert( alignment
> 0 );
99 ptr
= (uintptr_t)malloc(bytes
+ alignment
+ sizeof(void *));
103 buf
= (ptr
+ alignment
+ sizeof(void *)) & ~(uintptr_t)(alignment
- 1);
104 *(uintptr_t *)(buf
- sizeof(void *)) = ptr
;
107 /* mark the non-aligned area */
108 while ( ptr
< buf
- sizeof(void *) ) {
109 *(unsigned long *)ptr
= 0xcdcdcdcd;
110 ptr
+= sizeof(unsigned long);
115 #endif /* defined(HAVE_POSIX_MEMALIGN) */
119 * Same as _mesa_align_malloc(), but using calloc(1, ) instead of
123 _mesa_align_calloc(size_t bytes
, unsigned long alignment
)
125 #if defined(HAVE_POSIX_MEMALIGN)
128 mem
= _mesa_align_malloc(bytes
, alignment
);
130 (void) memset(mem
, 0, bytes
);
134 #elif defined(_WIN32)
137 mem
= _aligned_malloc(bytes
, alignment
);
139 (void) memset(mem
, 0, bytes
);
146 assert( alignment
> 0 );
148 ptr
= (uintptr_t)calloc(1, bytes
+ alignment
+ sizeof(void *));
152 buf
= (ptr
+ alignment
+ sizeof(void *)) & ~(uintptr_t)(alignment
- 1);
153 *(uintptr_t *)(buf
- sizeof(void *)) = ptr
;
156 /* mark the non-aligned area */
157 while ( ptr
< buf
- sizeof(void *) ) {
158 *(unsigned long *)ptr
= 0xcdcdcdcd;
159 ptr
+= sizeof(unsigned long);
164 #endif /* defined(HAVE_POSIX_MEMALIGN) */
168 * Free memory which was allocated with either _mesa_align_malloc()
169 * or _mesa_align_calloc().
170 * \param ptr pointer to the memory to be freed.
171 * The actual address to free is stored in the word immediately before the
172 * address the client sees.
173 * Note that it is legal to pass NULL pointer to this function and will be
174 * handled accordingly.
177 _mesa_align_free(void *ptr
)
179 #if defined(HAVE_POSIX_MEMALIGN)
181 #elif defined(_WIN32)
185 void **cubbyHole
= (void **) ((char *) ptr
- sizeof(void *));
186 void *realAddr
= *cubbyHole
;
189 #endif /* defined(HAVE_POSIX_MEMALIGN) */
193 * Reallocate memory, with alignment.
196 _mesa_align_realloc(void *oldBuffer
, size_t oldSize
, size_t newSize
,
197 unsigned long alignment
)
201 return _aligned_realloc(oldBuffer
, newSize
, alignment
);
203 const size_t copySize
= (oldSize
< newSize
) ? oldSize
: newSize
;
204 void *newBuf
= _mesa_align_malloc(newSize
, alignment
);
205 if (newBuf
&& oldBuffer
&& copySize
> 0) {
206 memcpy(newBuf
, oldBuffer
, copySize
);
209 _mesa_align_free(oldBuffer
);
217 /**********************************************************************/
222 #ifndef HAVE___BUILTIN_POPCOUNT
224 * Return number of bits set in given GLuint.
227 _mesa_bitcount(unsigned int n
)
230 for (bits
= 0; n
> 0; n
= n
>> 1) {
237 #ifndef HAVE___BUILTIN_POPCOUNTLL
239 * Return number of bits set in given 64-bit uint.
242 _mesa_bitcount_64(uint64_t n
)
245 for (bits
= 0; n
> 0; n
= n
>> 1) {
255 /** Needed due to #ifdef's, above. */
257 _mesa_vsnprintf(char *str
, size_t size
, const char *fmt
, va_list args
)
259 return vsnprintf( str
, size
, fmt
, args
);
262 /** Wrapper around vsnprintf() */
264 _mesa_snprintf( char *str
, size_t size
, const char *fmt
, ... )
268 va_start( args
, fmt
);
269 r
= vsnprintf( str
, size
, fmt
, args
);