mm_malloc.h revision 226633
1193326Sed/*===---- mm_malloc.h - Allocating and Freeing Aligned Memory Blocks -------===
2193326Sed *
3193326Sed * Permission is hereby granted, free of charge, to any person obtaining a copy
4193326Sed * of this software and associated documentation files (the "Software"), to deal
5193326Sed * in the Software without restriction, including without limitation the rights
6193326Sed * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7193326Sed * copies of the Software, and to permit persons to whom the Software is
8193326Sed * furnished to do so, subject to the following conditions:
9193326Sed *
10193326Sed * The above copyright notice and this permission notice shall be included in
11193326Sed * all copies or substantial portions of the Software.
12193326Sed *
13193326Sed * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14193326Sed * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15193326Sed * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16193326Sed * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17193326Sed * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18193326Sed * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19193326Sed * THE SOFTWARE.
20193326Sed *
21193326Sed *===-----------------------------------------------------------------------===
22193326Sed */
23193326Sed
24193326Sed#ifndef __MM_MALLOC_H
25193326Sed#define __MM_MALLOC_H
26193326Sed
27193326Sed#include <stdlib.h>
28193326Sed
29218893Sdim#ifdef _WIN32
30218893Sdim#include <malloc.h>
31218893Sdim#else
32218893Sdim#ifndef __cplusplus
33218893Sdimextern int posix_memalign(void **memptr, size_t alignment, size_t size);
34218893Sdim#else
35218893Sdim// Some systems (e.g. those with GNU libc) declare posix_memalign with an
36218893Sdim// exception specifier. Via an "egregious workaround" in
37218893Sdim// Sema::CheckEquivalentExceptionSpec, Clang accepts the following as a valid
38218893Sdim// redeclaration of glibc's declaration.
39218893Sdimextern "C" int posix_memalign(void **memptr, size_t alignment, size_t size);
40218893Sdim#endif
41218893Sdim#endif
42218893Sdim
43221345Sdim#if !(defined(_WIN32) && defined(_mm_malloc))
44218893Sdimstatic __inline__ void *__attribute__((__always_inline__, __nodebug__,
45218893Sdim                                       __malloc__))
46206084Srdivacky_mm_malloc(size_t size, size_t align)
47193326Sed{
48218893Sdim  if (align == 1) {
49218893Sdim    return malloc(size);
50193326Sed  }
51193326Sed
52218893Sdim  if (!(align & (align - 1)) && align < sizeof(void *))
53218893Sdim    align = sizeof(void *);
54193326Sed
55218893Sdim  void *mallocedMemory;
56226633Sdim#if defined(__MINGW32__)
57226633Sdim  mallocedMemory = __mingw_aligned_malloc(size, align);
58226633Sdim#elif defined(_WIN32)
59218893Sdim  mallocedMemory = _aligned_malloc(size, align);
60218893Sdim#else
61218893Sdim  if (posix_memalign(&mallocedMemory, align, size))
62193326Sed    return 0;
63218893Sdim#endif
64193326Sed
65218893Sdim  return mallocedMemory;
66193326Sed}
67193326Sed
68206084Srdivackystatic __inline__ void __attribute__((__always_inline__, __nodebug__))
69206084Srdivacky_mm_free(void *p)
70193326Sed{
71218893Sdim  free(p);
72193326Sed}
73221345Sdim#endif
74193326Sed
75193326Sed#endif /* __MM_MALLOC_H */
76