clang API Documentation

mm_malloc.h
Go to the documentation of this file.
00001 /*===---- mm_malloc.h - Allocating and Freeing Aligned Memory Blocks -------===
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy
00004  * of this software and associated documentation files (the "Software"), to deal
00005  * in the Software without restriction, including without limitation the rights
00006  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00007  * copies of the Software, and to permit persons to whom the Software is
00008  * furnished to do so, subject to the following conditions:
00009  *
00010  * The above copyright notice and this permission notice shall be included in
00011  * all copies or substantial portions of the Software.
00012  *
00013  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00014  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00015  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00016  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00017  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00018  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00019  * THE SOFTWARE.
00020  *
00021  *===-----------------------------------------------------------------------===
00022  */
00023 
00024 #ifndef __MM_MALLOC_H
00025 #define __MM_MALLOC_H
00026 
00027 #include <stdlib.h>
00028 
00029 #ifdef _WIN32
00030 #include <malloc.h>
00031 #else
00032 #ifndef __cplusplus
00033 extern int posix_memalign(void **__memptr, size_t __alignment, size_t __size);
00034 #else
00035 // Some systems (e.g. those with GNU libc) declare posix_memalign with an
00036 // exception specifier. Via an "egregious workaround" in
00037 // Sema::CheckEquivalentExceptionSpec, Clang accepts the following as a valid
00038 // redeclaration of glibc's declaration.
00039 extern "C" int posix_memalign(void **__memptr, size_t __alignment, size_t __size);
00040 #endif
00041 #endif
00042 
00043 #if !(defined(_WIN32) && defined(_mm_malloc))
00044 static __inline__ void *__attribute__((__always_inline__, __nodebug__,
00045                                        __malloc__))
00046 _mm_malloc(size_t __size, size_t __align)
00047 {
00048   if (__align == 1) {
00049     return malloc(__size);
00050   }
00051 
00052   if (!(__align & (__align - 1)) && __align < sizeof(void *))
00053     __align = sizeof(void *);
00054 
00055   void *__mallocedMemory;
00056 #if defined(__MINGW32__)
00057   __mallocedMemory = __mingw_aligned_malloc(__size, __align);
00058 #elif defined(_WIN32)
00059   __mallocedMemory = _aligned_malloc(__size, __align);
00060 #else
00061   if (posix_memalign(&__mallocedMemory, __align, __size))
00062     return 0;
00063 #endif
00064 
00065   return __mallocedMemory;
00066 }
00067 
00068 static __inline__ void __attribute__((__always_inline__, __nodebug__))
00069 _mm_free(void *__p)
00070 {
00071   free(__p);
00072 }
00073 #endif
00074 
00075 #endif /* __MM_MALLOC_H */