Allocate memory on cache line if requested
authorH.J. Lu <hongjiu.lu@intel.com>
Tue, 19 Apr 2016 17:01:11 +0000 (17:01 +0000)
committerH.J. Lu <hjl@gcc.gnu.org>
Tue, 19 Apr 2016 17:01:11 +0000 (10:01 -0700)
Since GTM::gtm_thread has

gtm_thread *next_thread __attribute__((__aligned__(HW_CACHELINE_SIZE)));

GTM::gtm_thread::operator new () calls xmalloc with separate_cl == true.
xmalloc must return memory on cache line in this case.

PR libitm/70456
* util.cc (xmalloc): Use posix_memalign to allocate memory on
on cache line if requested.

From-SVN: r235211

libitm/ChangeLog
libitm/util.cc

index 42b4776..68b5069 100644 (file)
@@ -1,3 +1,9 @@
+2016-04-19  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR libitm/70456
+       * util.cc (xmalloc): Use posix_memalign to allocate memory on
+       on cache line if requested.
+
 2016-03-03  Dominik Vogt  <vogt@linux.vnet.ibm.com>
 
        * config/s390/target.h (TARGET_BEGIN_TRANSACTION_ATTRIBUTE): Define
index 16e5d03..f89b2e5 100644 (file)
@@ -61,12 +61,22 @@ GTM_fatal (const char *fmt, ...)
 void *
 xmalloc (size_t size, bool separate_cl)
 {
-  // TODO Use posix_memalign if separate_cl is true, or some other allocation
-  // method that will avoid sharing cache lines with data used by other
-  // threads.
-  void *r = malloc (size);
-  if (r == 0)
-    GTM_fatal ("Out of memory allocating %lu bytes", (unsigned long) size);
+  void *r;
+#ifdef HAVE_POSIX_MEMALIGN
+  if (separate_cl)
+    {
+      if (posix_memalign (&r, HW_CACHELINE_SIZE, size))
+       GTM_fatal ("Out of memory allocating %lu bytes aligned on cache line",
+                  (unsigned long) size);
+    }
+  else
+#endif
+    {
+      r = malloc (size);
+      if (r == 0)
+       GTM_fatal ("Out of memory allocating %lu bytes",
+                  (unsigned long) size);
+    }
   return r;
 }