Used for aligned_alloc and other C11 functions missing from the header. Signed-off-by: Emil Velikov <emil.l.veli...@gmail.com> --- include/c11_stdlib.h | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 include/c11_stdlib.h
diff --git a/include/c11_stdlib.h b/include/c11_stdlib.h new file mode 100644 index 0000000..04e494f --- /dev/null +++ b/include/c11_stdlib.h @@ -0,0 +1,118 @@ +/* + * Mesa 3-D graphics library + * + * Copyright (C) 1999-2007 Brian Paul All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +/** + * Wrapper for stdlib.h which makes sure we have definitions of all the c11 + * functions. + */ + +#ifndef _C11_STDLIB_H_ +#define _C11_STDLIB_H_ + +#include <stdint.h> +#include <stdlib.h> +#include "c99_compat.h" + + +#if !defined(_ISOC11_SOURCE) && __STDC_VERSION__ < 201112L + +#if defined(_WIN32) && !defined(__CYGWIN__) +#include <malloc.h> +#endif + +/** + * Allocate aligned memory. + * + * \param alignment alignment (must be greater than zero). + * \param size number of bytes to allocate. + * + * Allocates extra memory to accommodate rounding up the address for + * alignment and to record the real malloc address. + * + * \sa aligned_free(). + */ +static inline void * +aligned_alloc(size_t alignment, size_t size) +{ +#if defined(HAVE_POSIX_MEMALIGN) + void *mem; + int err = posix_memalign(&mem, alignment, size); + if (err) + return NULL; + return mem; +#elif defined(_WIN32) && !defined(__CYGWIN__) + return _aligned_malloc(size, alignment); +#else + uintptr_t ptr, buf; + + assert( alignment > 0 ); + + ptr = (uintptr_t)malloc(size + alignment + sizeof(void *)); + if (!ptr) + return NULL; + + buf = (ptr + alignment + sizeof(void *)) & ~(uintptr_t)(alignment - 1); + *(uintptr_t *)(buf - sizeof(void *)) = ptr; + +#ifdef DEBUG + /* mark the non-aligned area */ + while ( ptr < buf - sizeof(void *) ) { + *(unsigned long *)ptr = 0xcdcdcdcd; + ptr += sizeof(unsigned long); + } +#endif + + return (void *) buf; +#endif /* defined(HAVE_POSIX_MEMALIGN) */ +} + +#endif /* C11 */ + +/** + * Free memory which was allocated with aligned_alloc(). + * + * \param ptr pointer to the memory to be freed. + * + * The actual address to free is stored in the word immediately before the + * address the client sees. + * Note that it is legal to pass NULL pointer to this function and will be + * handled accordingly. + */ +static inline void +aligned_free(void *ptr) +{ +#if defined(HAVE_POSIX_MEMALIGN) + free(ptr); +#elif defined(_WIN32) && !defined(__CYGWIN__) + _aligned_free(ptr); +#else + if (ptr) { + void **cubbyHole = (void **) ((char *) ptr - sizeof(void *)); + void *realAddr = *cubbyHole; + free(realAddr); + } +#endif /* defined(HAVE_POSIX_MEMALIGN) */ +} + +#endif /* #define _C11_STDLIB_H_ */ -- 2.1.3 _______________________________________________ mesa-dev mailing list mesa-dev@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/mesa-dev