Here's an updated patch to address Windows portability concerns.
-Matt
On Tue, Oct 5, 2010 at 13:54, Chris Lattner <[email protected]> wrote:
> LGTM, please commit.
>
> -Chris
>
> On Oct 5, 2010, at 10:01 AM, Matt Beaumont-Gay wrote:
>
>> See attached. This removes a dependency on errno.h and improves
>> compatibility with GCC's implementation.
>>
>> -Matt
>> <mm_malloc.patch>_______________________________________________
>> cfe-commits mailing list
>> [email protected]
>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>
>
--- /tmp/llvm/trunk/tools/clang/lib/Headers/mm_malloc.h 2010-03-26 13:20:42.000000000 -0700
+++ tools/clang/lib/Headers/mm_malloc.h 2010-10-11 16:23:46.931775000 -0700
@@ -24,39 +24,45 @@
#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
+extern "C" int posix_memalign(void **memptr, size_t alignment, size_t size)
+ throw ();
+#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 */
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits