From 6bfcf51a2227f1576007cb080c2b92e8ac96c116 Mon Sep 17 00:00:00 2001
From: Berenger Bramas <bbramas@mpcdf.mpg.de>
Date: Tue, 23 May 2017 15:16:20 +0200
Subject: [PATCH] Move new into a dedicated cpp file to respect the standard

---
 bfps/cpp/bfps_new.cpp                  | 51 ++++++++++++++++++++++++++
 bfps/cpp/bfps_new.hpp                  |  6 +++
 bfps/cpp/particles/particles_utils.hpp | 47 +-----------------------
 setup.py                               |  3 +-
 4 files changed, 60 insertions(+), 47 deletions(-)
 create mode 100644 bfps/cpp/bfps_new.cpp
 create mode 100644 bfps/cpp/bfps_new.hpp

diff --git a/bfps/cpp/bfps_new.cpp b/bfps/cpp/bfps_new.cpp
new file mode 100644
index 00000000..c169b4d7
--- /dev/null
+++ b/bfps/cpp/bfps_new.cpp
@@ -0,0 +1,51 @@
+#include <cstdlib>
+#include "bfps_new.hpp"
+#include "particles/particles_utils.hpp"
+
+// Do not make this function or in header.
+
+// Regular scalar new
+void* operator new(std::size_t n) {
+    void* const allocated = particles_utils::aligned_malloc::malloc<particles_utils::DefaultMemAlignement>(n);
+    if(allocated){
+        return allocated;
+    }
+    throw std::bad_alloc();
+    return allocated;
+}
+
+void* operator new[]( std::size_t n ) {
+    void* const allocated = particles_utils::aligned_malloc::malloc<particles_utils::DefaultMemAlignement>(n);
+    if(allocated){
+        return allocated;
+    }
+    throw std::bad_alloc();
+    return allocated;
+}
+
+void* operator new  ( std::size_t n, const std::nothrow_t& tag){
+    void* const allocated = particles_utils::aligned_malloc::malloc<particles_utils::DefaultMemAlignement>(n);
+    return allocated;
+}
+
+void* operator new[] ( std::size_t n, const std::nothrow_t& tag){
+    void* const allocated = particles_utils::aligned_malloc::malloc<particles_utils::DefaultMemAlignement>(n);
+    return allocated;
+}
+
+// Regular scalar delete
+void operator delete(void* p) {
+    particles_utils::aligned_malloc::free(p);
+}
+
+void operator delete[](void* p) {
+    particles_utils::aligned_malloc::free(p);
+}
+
+void operator delete  ( void* p, const std::nothrow_t& /*tag*/) {
+    particles_utils::aligned_malloc::free(p);
+}
+
+void operator delete[]( void* p, const std::nothrow_t& /*tag*/) {
+    particles_utils::aligned_malloc::free(p);
+}
diff --git a/bfps/cpp/bfps_new.hpp b/bfps/cpp/bfps_new.hpp
new file mode 100644
index 00000000..473a0a66
--- /dev/null
+++ b/bfps/cpp/bfps_new.hpp
@@ -0,0 +1,6 @@
+#ifndef BFPS_NEW_HPP
+#define BFPS_NEW_HPP
+
+#include <new>
+
+#endif
diff --git a/bfps/cpp/particles/particles_utils.hpp b/bfps/cpp/particles/particles_utils.hpp
index 09d6d7f3..1c327ea4 100644
--- a/bfps/cpp/particles/particles_utils.hpp
+++ b/bfps/cpp/particles/particles_utils.hpp
@@ -311,6 +311,7 @@ public:
     }
 };
 
+// Default alignement for the complete application by redirecting the new operator
 static const int DefaultMemAlignement = 64;
 
 namespace aligned_malloc {
@@ -348,51 +349,5 @@ inline void free(void* ptrToFree){
 }
 
 
-// Regular scalar new
-inline void* operator new(std::size_t n) {
-    void* const allocated = particles_utils::aligned_malloc::malloc<particles_utils::DefaultMemAlignement>(n);
-    if(allocated){
-        return allocated;
-    }
-    throw std::bad_alloc();
-    return allocated;
-}
-
-inline void* operator new[]( std::size_t n ) {
-    void* const allocated = particles_utils::aligned_malloc::malloc<particles_utils::DefaultMemAlignement>(n);
-    if(allocated){
-        return allocated;
-    }
-    throw std::bad_alloc();
-    return allocated;
-}
-
-inline void* operator new  ( std::size_t n, const std::nothrow_t& tag){
-    void* const allocated = particles_utils::aligned_malloc::malloc<particles_utils::DefaultMemAlignement>(n);
-    return allocated;
-}
-
-inline void* operator new[] ( std::size_t n, const std::nothrow_t& tag){
-    void* const allocated = particles_utils::aligned_malloc::malloc<particles_utils::DefaultMemAlignement>(n);
-    return allocated;
-}
-
-// Regular scalar delete
-inline void operator delete(void* p) noexcept{
-    particles_utils::aligned_malloc::free(p);
-}
-
-inline void operator delete[](void* p) noexcept{
-    particles_utils::aligned_malloc::free(p);
-}
-
-inline void operator delete  ( void* p, const std::nothrow_t& /*tag*/) {
-    particles_utils::aligned_malloc::free(p);
-}
-
-inline void operator delete[]( void* p, const std::nothrow_t& /*tag*/) {
-    particles_utils::aligned_malloc::free(p);
-}
-
 
 #endif
diff --git a/setup.py b/setup.py
index c531263a..7200cbc6 100644
--- a/setup.py
+++ b/setup.py
@@ -118,7 +118,8 @@ src_file_list = ['full_code/direct_numerical_simulation',
                  'spline_n9',
                  'spline_n10',
                  'Lagrange_polys',
-                 'scope_timer']
+                 'scope_timer',
+                 'bfps_new']
 
 particle_headers = [
         'cpp/particles/particles_distr_mpi.hpp',
-- 
GitLab