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.
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
);
64 /**********************************************************************/
69 * Allocate aligned memory.
71 * \param bytes number of bytes to allocate.
72 * \param alignment alignment (must be greater than zero).
74 * Allocates extra memory to accommodate rounding up the address for
75 * alignment and to record the real malloc address.
77 * \sa _mesa_align_free().
80 _mesa_align_malloc(size_t bytes
, unsigned long alignment
)
82 #if defined(HAVE_POSIX_MEMALIGN)
84 int err
= posix_memalign(& mem
, alignment
, bytes
);
89 return _aligned_malloc(bytes
, alignment
);
93 assert( alignment
> 0 );
95 ptr
= (uintptr_t)malloc(bytes
+ alignment
+ sizeof(void *));
99 buf
= (ptr
+ alignment
+ sizeof(void *)) & ~(uintptr_t)(alignment
- 1);
100 *(uintptr_t *)(buf
- sizeof(void *)) = ptr
;
103 /* mark the non-aligned area */
104 while ( ptr
< buf
- sizeof(void *) ) {
105 *(unsigned long *)ptr
= 0xcdcdcdcd;
106 ptr
+= sizeof(unsigned long);
111 #endif /* defined(HAVE_POSIX_MEMALIGN) */
115 * Same as _mesa_align_malloc(), but using calloc(1, ) instead of
119 _mesa_align_calloc(size_t bytes
, unsigned long alignment
)
121 #if defined(HAVE_POSIX_MEMALIGN)
124 mem
= _mesa_align_malloc(bytes
, alignment
);
126 (void) memset(mem
, 0, bytes
);
130 #elif defined(_WIN32)
133 mem
= _aligned_malloc(bytes
, alignment
);
135 (void) memset(mem
, 0, bytes
);
142 assert( alignment
> 0 );
144 ptr
= (uintptr_t)calloc(1, bytes
+ alignment
+ sizeof(void *));
148 buf
= (ptr
+ alignment
+ sizeof(void *)) & ~(uintptr_t)(alignment
- 1);
149 *(uintptr_t *)(buf
- sizeof(void *)) = ptr
;
152 /* mark the non-aligned area */
153 while ( ptr
< buf
- sizeof(void *) ) {
154 *(unsigned long *)ptr
= 0xcdcdcdcd;
155 ptr
+= sizeof(unsigned long);
160 #endif /* defined(HAVE_POSIX_MEMALIGN) */
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.
173 _mesa_align_free(void *ptr
)
175 #if defined(HAVE_POSIX_MEMALIGN)
177 #elif defined(_WIN32)
181 void **cubbyHole
= (void **) ((char *) ptr
- sizeof(void *));
182 void *realAddr
= *cubbyHole
;
185 #endif /* defined(HAVE_POSIX_MEMALIGN) */
189 * Reallocate memory, with alignment.
192 _mesa_align_realloc(void *oldBuffer
, size_t oldSize
, size_t newSize
,
193 unsigned long alignment
)
197 return _aligned_realloc(oldBuffer
, newSize
, alignment
);
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
);
205 _mesa_align_free(oldBuffer
);
213 /** Needed due to #ifdef's, above. */
215 _mesa_vsnprintf(char *str
, size_t size
, const char *fmt
, va_list args
)
217 return vsnprintf( str
, size
, fmt
, args
);
220 /** Wrapper around vsnprintf() */
222 _mesa_snprintf( char *str
, size_t size
, const char *fmt
, ... )
226 va_start( args
, fmt
);
227 r
= vsnprintf( str
, size
, fmt
, args
);