From 104d9c64d5bf82891b9aeaaa17fb762f2cf89972 Mon Sep 17 00:00:00 2001
From: Christian Engwer <christi@dune-project.org>
Date: Fri, 5 Oct 2012 09:11:38 +0000
Subject: [PATCH] add new allocator using malloc/free

[[Imported from SVN: r7009]]
---
 dune/common/Makefile.am        |  1 +
 dune/common/mallocallocator.hh | 91 ++++++++++++++++++++++++++++++++++
 2 files changed, 92 insertions(+)
 create mode 100644 dune/common/mallocallocator.hh

diff --git a/dune/common/Makefile.am b/dune/common/Makefile.am
index 986eb6f8c..3688decd4 100644
--- a/dune/common/Makefile.am
+++ b/dune/common/Makefile.am
@@ -57,6 +57,7 @@ commoninclude_HEADERS = 			\
 	iteratorfacades.hh			\
 	lcm.hh					\
 	lru.hh					\
+	mallocallocator.hh					\
 	math.hh					\
 	matvectraits.hh \
 	misc.hh					\
diff --git a/dune/common/mallocallocator.hh b/dune/common/mallocallocator.hh
new file mode 100644
index 000000000..41c2a6ef7
--- /dev/null
+++ b/dune/common/mallocallocator.hh
@@ -0,0 +1,91 @@
+// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
+// vi: set et ts=4 sw=2 sts=2:
+#ifndef DUNE_MALLOC_ALLOCATOR_HH
+#define DUNE_MALLOC_ALLOCATOR_HH
+
+#include <exception>
+#include <cstdlib>
+#include <new>
+
+namespace Dune
+{
+  //! allocator which simply calls malloc/free
+  template <class T>
+  class MallocAllocator {
+  public:
+    typedef std::size_t size_type;
+    typedef std::ptrdiff_t difference_type;
+    typedef T* pointer;
+    typedef const T* const_pointer;
+    typedef T& reference;
+    typedef const T& const_reference;
+    typedef T value_type;
+    template <class U> struct rebind {
+      typedef MallocAllocator<U> other;
+    };
+
+    //! create a new MallocAllocator
+    MallocAllocator() throw() {}
+    //! copy construct from an other MallocAllocator, possibly for a different result type
+    template <class U>
+    MallocAllocator(const MallocAllocator<U>&) throw() {}
+    //! cleanup this allocator
+    ~MallocAllocator() throw() {}
+
+    pointer address(reference x) const
+    {
+      return &x;
+    }
+    const_pointer address(const_reference x) const
+    {
+      return &x;
+    }
+
+    //! allocate n objects of type T
+    pointer allocate(size_type n,
+                     const void* hint = 0)
+    {
+      if (n > this->max_size())
+        throw std::bad_alloc();
+
+      pointer ret = static_cast<pointer>(std::malloc(n * sizeof(T)));
+      if (!ret)
+        throw std::bad_alloc();
+      return ret;
+    }
+
+    //! deallocate n objects of type T at address p
+    void deallocate(pointer p, size_type n)
+    {
+      std::free(p);
+    }
+
+    //! max size for allocate
+    size_type max_size() const throw()
+    {
+      return size_type(-1) / sizeof(T);
+    }
+
+    //! copy-construct an object of type T (i.e. make a placement new on p)
+    void construct(pointer p, const T& val)
+    {
+      ::new((void*)p)T(val);
+    }
+#if HAVE_VARIADIC_TEMPLATES || DOXYGEN
+    //! construct an object of type T from variadic parameters
+    //! \note works only with newer C++ compilers
+    template<typename ... _Args>
+    void construct(pointer p, _Args&&... __args)
+    {
+      ::new((void *)p)_Tp(std::forward<_Args>(__args) ...);
+    }
+#endif
+    //! destroy an object of type T (i.e. call the Destructor)
+    void destroy(pointer p)
+    {
+      p->~T();
+    }
+  };
+}
+
+#endif // DUNE_MALLOC_ALLOCATOR_HH
-- 
GitLab