[cfe-commits] r116771 - /cfe/trunk/lib/Headers/mm_malloc.h
Eric Christopher
echristo at apple.com
Mon Oct 18 16:38:51 PDT 2010
Author: echristo
Date: Mon Oct 18 18:38:51 2010
New Revision: 116771
URL: http://llvm.org/viewvc/llvm-project?rev=116771&view=rev
Log:
>From scratch rewrite of mm_malloc.h.
Patch by Matthew Beaumont-Gay!
Modified:
cfe/trunk/lib/Headers/mm_malloc.h
Modified: cfe/trunk/lib/Headers/mm_malloc.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Headers/mm_malloc.h?rev=116771&r1=116770&r2=116771&view=diff
==============================================================================
--- cfe/trunk/lib/Headers/mm_malloc.h (original)
+++ cfe/trunk/lib/Headers/mm_malloc.h Mon Oct 18 18:38:51 2010
@@ -24,39 +24,48 @@
#ifndef __MM_MALLOC_H
#define __MM_MALLOC_H
-#include <errno.h>
#include <stdlib.h>
-static __inline__ void *__attribute__((__always_inline__, __nodebug__))
+#ifdef _WIN32
+#include <malloc.h>
+#else
+#ifndef __cplusplus
+extern int posix_memalign(void **memptr, size_t alignment, size_t size);
+#else
+// Some systems (e.g. those with GNU libc) declare posix_memalign with an
+// exception specifier. Via an "egregious workaround" in
+// Sema::CheckEquivalentExceptionSpec, Clang accepts the following as a valid
+// redeclaration of glibc's declaration.
+extern "C" int posix_memalign(void **memptr, size_t alignment, size_t size);
+#endif
+#endif
+
+static __inline__ void *__attribute__((__always_inline__, __nodebug__,
+ __malloc__))
_mm_malloc(size_t size, size_t align)
{
- if (align & (align - 1)) {
- errno = EINVAL;
- return 0;
+ if (align == 1) {
+ return malloc(size);
}
- if (!size)
- return 0;
+ if (!(align & (align - 1)) && align < sizeof(void *))
+ align = sizeof(void *);
- if (align < 2 * sizeof(void *))
- align = 2 * sizeof(void *);
-
- void *mallocedMemory = malloc(size + align);
- if (!mallocedMemory)
+ void *mallocedMemory;
+#ifdef _WIN32
+ mallocedMemory = _aligned_malloc(size, align);
+#else
+ if (posix_memalign(&mallocedMemory, align, size))
return 0;
+#endif
- void *alignedMemory =
- (void *)(((size_t)mallocedMemory + align) & ~((size_t)align - 1));
- ((void **)alignedMemory)[-1] = mallocedMemory;
-
- return alignedMemory;
+ return mallocedMemory;
}
static __inline__ void __attribute__((__always_inline__, __nodebug__))
_mm_free(void *p)
{
- if (p)
- free(((void **)p)[-1]);
+ free(p);
}
#endif /* __MM_MALLOC_H */
More information about the cfe-commits
mailing list