See attached. This removes a dependency on errno.h and improves
compatibility with GCC's implementation.

-Matt
--- /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-05 09:54:34.800365000 -0700
@@ -24,39 +24,36 @@
 #ifndef __MM_MALLOC_H
 #define __MM_MALLOC_H
 
-#include <errno.h>
 #include <stdlib.h>
 
+#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
+
 static __inline__ void *__attribute__((__always_inline__, __nodebug__))
 _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;
+  if (posix_memalign(&mallocedMemory, align, size))
     return 0;
 
-  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

Reply via email to