[Openmp-commits] [PATCH] D40296: Fix aligned memory allocation in the stub library

Jonathan Peyton via Phabricator via Openmp-commits openmp-commits at lists.llvm.org
Wed Nov 29 14:30:14 PST 2017


This revision was automatically updated to reflect the committed changes.
Closed by commit rOMP319375: Fix aligned memory allocation in the stub library (authored by jlpeyton).

Repository:
  rOMP OpenMP

https://reviews.llvm.org/D40296

Files:
  runtime/src/kmp_stub.cpp


Index: runtime/src/kmp_stub.cpp
===================================================================
--- runtime/src/kmp_stub.cpp
+++ runtime/src/kmp_stub.cpp
@@ -139,34 +139,59 @@
 /* KMP memory management functions. */
 void *kmp_malloc(size_t size) {
   i;
-  return malloc(size);
+  void *res;
+#if KMP_OS_WINDOWS
+  // If succesfull returns a pointer to the memory block, otherwise returns
+  // NULL.
+  // Sets errno to ENOMEM or EINVAL if memory allocation failed or parameter
+  // validation failed.
+  res = _aligned_malloc(size, 1);
+#else
+  res = malloc(size);
+#endif
+  return res;
 }
 void *kmp_aligned_malloc(size_t sz, size_t a) {
   i;
+  int err;
+  void *res;
 #if KMP_OS_WINDOWS
-  errno = ENOSYS; // not supported
-  return NULL; // no standard aligned allocator on Windows (pre - C11)
+  res = _aligned_malloc(sz, a);
 #else
-  void *res;
-  int err;
   if (err = posix_memalign(&res, a, sz)) {
     errno = err; // can be EINVAL or ENOMEM
-    return NULL;
+    res = NULL;
   }
-  return res;
 #endif
+  return res;
 }
 void *kmp_calloc(size_t nelem, size_t elsize) {
   i;
-  return calloc(nelem, elsize);
+  void *res;
+#if KMP_OS_WINDOWS
+  res = _aligned_recalloc(NULL, nelem, elsize, 1);
+#else
+  res = calloc(nelem, elsize);
+#endif
+  return res;
 }
 void *kmp_realloc(void *ptr, size_t size) {
   i;
-  return realloc(ptr, size);
+  void *res;
+#if KMP_OS_WINDOWS
+  res = _aligned_realloc(ptr, size, 1);
+#else
+  res = realloc(ptr, size);
+#endif
+  return res;
 }
 void kmp_free(void *ptr) {
   i;
+#if KMP_OS_WINDOWS
+  _aligned_free(ptr);
+#else
   free(ptr);
+#endif
 }
 
 static int __kmps_blocktime = INT_MAX;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D40296.124823.patch
Type: text/x-patch
Size: 1653 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/openmp-commits/attachments/20171129/d0441794/attachment-0001.bin>


More information about the Openmp-commits mailing list