summaryrefslogtreecommitdiffstats
path: root/clang/lib
diff options
context:
space:
mode:
authorAnders Carlsson <andersca@mac.com>2009-01-08 23:30:09 +0000
committerAnders Carlsson <andersca@mac.com>2009-01-08 23:30:09 +0000
commitbfd47a3c00bbceaa1bf55ce9be8f763a2a94e886 (patch)
tree627568463bfde5b97eae78d0eb7ba7c6c5d6cf19 /clang/lib
parentd11dc0b1d9e88c260c016793da0bbf8415f65368 (diff)
downloadbcm5719-llvm-bfd47a3c00bbceaa1bf55ce9be8f763a2a94e886.tar.gz
bcm5719-llvm-bfd47a3c00bbceaa1bf55ce9be8f763a2a94e886.zip
Add mm_malloc.h, patch by Sam Weinig.
llvm-svn: 61954
Diffstat (limited to 'clang/lib')
-rw-r--r--clang/lib/Headers/mm_malloc.h59
1 files changed, 59 insertions, 0 deletions
diff --git a/clang/lib/Headers/mm_malloc.h b/clang/lib/Headers/mm_malloc.h
new file mode 100644
index 00000000000..71e7b87ae8e
--- /dev/null
+++ b/clang/lib/Headers/mm_malloc.h
@@ -0,0 +1,59 @@
+/*===---- mm_malloc.h - Allocating and Freeing Aligned Memory Blocks -------===
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *===-----------------------------------------------------------------------===
+ */
+
+#ifndef __MM_MALLOC_H
+#define __MM_MALLOC_H
+
+#include <errno.h>
+#include <stdlib.h>
+
+static inline void *__attribute__((__always_inline__)) _mm_malloc(size_t size, size_t align)
+{
+ if (align & (align - 1)) {
+ errno = EINVAL;
+ return 0;
+ }
+
+ if (!size)
+ return 0;
+
+ if (align < 2 * sizeof(void *))
+ align = 2 * sizeof(void *);
+
+ void *mallocedMemory = malloc(size + align);
+ if (!mallocedMemory)
+ return 0;
+
+ void *alignedMemory = (void *)(((size_t)mallocedMemory + align) & ~((size_t)align - 1));
+ ((void **)alignedMemory)[-1] = mallocedMemory;
+
+ return alignedMemory;
+}
+
+static inline void __attribute__((__always_inline__)) _mm_free(void *p)
+{
+ if (p)
+ free(((void **)p)[-1]);
+}
+
+#endif /* __MM_MALLOC_H */
OpenPOWER on IntegriCloud