From a4aad1115c7b1f660aef122b4dc4397a87c7fbbb Mon Sep 17 00:00:00 2001
From: Nehal J Wani <nehaljw.kkd1@gmail.com>
Date: Sun, 18 Jun 2023 15:40:42 +0100
Subject: [PATCH 23/23] Revert "BUG#34849343 Aligned_atomic not working as
 intended - aligned_alloc"

This reverts commit be8348a7c3e8510b998a063065b626a459631b32.
---
 mysys/CMakeLists.txt                      |  1 -
 sql/memory/aligned_atomic.h               | 71 ++++++-----------------
 unittest/gunit/memory/aligned_atomic-t.cc |  2 -
 3 files changed, 18 insertions(+), 56 deletions(-)

diff --git a/mysys/CMakeLists.txt b/mysys/CMakeLists.txt
index 7b2f108c4d..e83d1642fc 100644
--- a/mysys/CMakeLists.txt
+++ b/mysys/CMakeLists.txt
@@ -55,7 +55,6 @@ SET(MYSYS_SOURCES
   mf_wcomp.cc
   mulalloc.cc
   my_access.cc
-  my_aligned_malloc.cc
   my_alloc.cc
   my_bit.cc
   my_bitmap.cc
diff --git a/sql/memory/aligned_atomic.h b/sql/memory/aligned_atomic.h
index 423366dab3..985efb758b 100644
--- a/sql/memory/aligned_atomic.h
+++ b/sql/memory/aligned_atomic.h
@@ -38,9 +38,8 @@
 #include <unistd.h>
 #endif
 
-#include "my_aligned_malloc.h"
-
 namespace memory {
+
 /**
  Calculates and returns the size of the CPU cache line.
 
@@ -259,28 +258,14 @@ class Aligned_atomic {
 
     @return The pointer to the underlying `std::atomic<T>` object.
    */
-  std::atomic<T> *operator->();
-  /*
-    Pointer operator that allows the access to the underlying `std::atomic<T>`
-    object.
-
-    @return The const pointer to the underlying `std::atomic<T>` object.
-   */
-  const std::atomic<T> *operator->() const;
+  std::atomic<T> *operator->() const;
   /*
     Dereference operator that allows the access to the underlying
     `std::atomic<T>` object.
 
     @return The reference to the underlying `std::atomic<T>` object.
    */
-  std::atomic<T> &operator*();
-  /*
-    Dereference operator that allows the access to the underlying
-    `std::atomic<T>` object.
-
-    @return The const reference to the underlying `std::atomic<T>` object.
-   */
-  const std::atomic<T> &operator*() const;
+  std::atomic<T> &operator*() const;
   /*
     The size of `std::atomic<T>`, as returned by `sizeof std::atomic<T>`.
 
@@ -298,7 +283,7 @@ class Aligned_atomic {
   /** The size of the byte buffer. */
   size_t m_storage_size{0};
   /** The byte buffer to use as underlying storage. */
-  void *m_storage{nullptr};
+  alignas(std::max_align_t) unsigned char *m_storage{nullptr};
   /** The pointer to the underlying `std::atomic<T>` object. */
   std::atomic<T> *m_underlying{nullptr};
 };
@@ -306,10 +291,9 @@ class Aligned_atomic {
 
 template <typename T>
 memory::Aligned_atomic<T>::Aligned_atomic()
-    : m_storage_size{memory::minimum_cacheline_for<std::atomic<T>>()} {
-  m_storage = my_aligned_malloc(m_storage_size, cache_line_size());
-  m_underlying = new (this->m_storage) std::atomic<T>();
-}
+    : m_storage_size{memory::minimum_cacheline_for<std::atomic<T>>()},
+      m_storage{new unsigned char[m_storage_size]},
+      m_underlying{new (this->m_storage) std::atomic<T>()} {}
 
 template <typename T>
 memory::Aligned_atomic<T>::Aligned_atomic(T value)
@@ -318,16 +302,12 @@ memory::Aligned_atomic<T>::Aligned_atomic(T value)
 }
 
 template <typename T>
-memory::Aligned_atomic<T>::Aligned_atomic(Aligned_atomic<T> &&rhs) {
-  if (this->m_underlying != nullptr) {
-    this->m_underlying->~atomic();
-  }
-  my_aligned_free(this->m_storage);
+memory::Aligned_atomic<T>::Aligned_atomic(Aligned_atomic<T> &&rhs)
+    : m_storage_size{rhs.m_storage_size}, m_underlying{rhs.m_underlying} {
+  delete[] this->m_storage;
   this->m_storage = rhs.m_storage;
-  this->m_storage_size = rhs.m_storage_size;
-  this->m_underlying = rhs.m_underlying;
-  rhs.m_storage = nullptr;
   rhs.m_storage_size = 0;
+  rhs.m_storage = nullptr;
   rhs.m_underlying = nullptr;
 }
 
@@ -335,25 +315,22 @@ template <typename T>
 memory::Aligned_atomic<T>::~Aligned_atomic() {
   if (this->m_underlying != nullptr) {
     this->m_underlying->~atomic();
+    this->m_underlying = nullptr;
   }
-  my_aligned_free(this->m_storage);
+  delete[] this->m_storage;
   this->m_storage = nullptr;
   this->m_storage_size = 0;
-  this->m_underlying = nullptr;
 }
 
 template <typename T>
 memory::Aligned_atomic<T> &memory::Aligned_atomic<T>::operator=(
     Aligned_atomic<T> &&rhs) {
-  if (this->m_underlying != nullptr) {
-    this->m_underlying->~atomic();
-  }
-  my_aligned_free(this->m_storage);
-  this->m_storage = rhs.m_storage;
+  delete[] this->m_storage;
   this->m_storage_size = rhs.m_storage_size;
+  this->m_storage = rhs.m_storage;
   this->m_underlying = rhs.m_underlying;
-  rhs.m_storage = nullptr;
   rhs.m_storage_size = 0;
+  rhs.m_storage = nullptr;
   rhs.m_underlying = nullptr;
   return (*this);
 }
@@ -393,25 +370,13 @@ bool memory::Aligned_atomic<T>::operator!=(T rhs) const {
 }
 
 template <typename T>
-std::atomic<T> *memory::Aligned_atomic<T>::operator->() {
-  assert(this->m_underlying != nullptr);
-  return this->m_underlying;
-}
-
-template <typename T>
-const std::atomic<T> *memory::Aligned_atomic<T>::operator->() const {
+std::atomic<T> *memory::Aligned_atomic<T>::operator->() const {
   assert(this->m_underlying != nullptr);
   return this->m_underlying;
 }
 
 template <typename T>
-std::atomic<T> &memory::Aligned_atomic<T>::operator*() {
-  assert(this->m_underlying != nullptr);
-  return *this->m_underlying;
-}
-
-template <typename T>
-const std::atomic<T> &memory::Aligned_atomic<T>::operator*() const {
+std::atomic<T> &memory::Aligned_atomic<T>::operator*() const {
   assert(this->m_underlying != nullptr);
   return *this->m_underlying;
 }
diff --git a/unittest/gunit/memory/aligned_atomic-t.cc b/unittest/gunit/memory/aligned_atomic-t.cc
index 39ac072402..b4bc336a1a 100644
--- a/unittest/gunit/memory/aligned_atomic-t.cc
+++ b/unittest/gunit/memory/aligned_atomic-t.cc
@@ -25,9 +25,7 @@
 #include <chrono>
 #include <vector>
 
-#define private public
 #include "sql/memory/aligned_atomic.h"
-#undef private
 
 #include <gmock/gmock.h>
 #include <gtest/gtest.h>
-- 
2.40.1

