Loading...
Searching...
No Matches
mallocallocator.hh
Go to the documentation of this file.
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
#ifndef DUNE_MALLOC_ALLOCATOR_HH
#define DUNE_MALLOC_ALLOCATOR_HH
#include <exception>
#include <cstdlib>
#include <new>
#include <utility>
namespace Dune
{
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;
};
MallocAllocator() noexcept {}
template <class U>
MallocAllocator(const MallocAllocator<U>&) noexcept {}
~MallocAllocator() noexcept {}
pointer address(reference x) const
{
return &x;
}
const_pointer address(const_reference x) const
{
return &x;
}
pointer allocate(size_type n,
[[maybe_unused]] 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;
}
void deallocate(pointer p, [[maybe_unused]] size_type n)
{
std::free(p);
}
size_type max_size() const noexcept
{
return size_type(-1) / sizeof(T);
}
void construct(pointer p, const T& val)
{
::new((void*)p)T(val);
}
template<typename ... Args>
void construct(pointer p, Args&&... args)
{
::new((void *)p)T(std::forward<Args>(args) ...);
}
void destroy(pointer p)
{
p->~T();
}
};
template<class T>
constexpr bool
operator==(const MallocAllocator<T> &, const MallocAllocator<T> &)
{
return true;
}
template<class T>
constexpr bool
operator!=(const MallocAllocator<T> &, const MallocAllocator<T> &)
{
return false;
}
}
#endif // DUNE_MALLOC_ALLOCATOR_HH
Dune
Dune namespace
Definition alignedallocator.hh:13
Dune::operator!=
constexpr auto operator!=(const HybridMultiIndex< S... > &lhs, const HybridMultiIndex< T... > &rhs)
Compare two HybridMultiIndexs for inequality.
Definition hybridmultiindex.hh:441
Dune::operator==
constexpr bool operator==(const HybridMultiIndex< S... > &lhs, const HybridMultiIndex< T... > &rhs)
Compare two HybridMultiIndexs for value equality.
Definition hybridmultiindex.hh:404
Dune::MallocAllocator
Allocators implementation which simply calls malloc/free.
Definition mallocallocator.hh:24
Dune::MallocAllocator::~MallocAllocator
~MallocAllocator() noexcept
cleanup this allocator
Definition mallocallocator.hh:43
Dune::MallocAllocator::pointer
T * pointer
Definition mallocallocator.hh:28
Dune::MallocAllocator::construct
void construct(pointer p, const T &val)
copy-construct an object of type T (i.e. make a placement new on p)
Definition mallocallocator.hh:80
Dune::MallocAllocator::MallocAllocator
MallocAllocator() noexcept
create a new MallocAllocator
Definition mallocallocator.hh:38
Dune::MallocAllocator::MallocAllocator
MallocAllocator(const MallocAllocator< U > &) noexcept
copy construct from an other MallocAllocator, possibly for a different result type
Definition mallocallocator.hh:41
Dune::MallocAllocator::size_type
std::size_t size_type
Definition mallocallocator.hh:26
Dune::MallocAllocator::difference_type
std::ptrdiff_t difference_type
Definition mallocallocator.hh:27
Dune::MallocAllocator::deallocate
void deallocate(pointer p, size_type n)
deallocate n objects of type T at address p
Definition mallocallocator.hh:68
Dune::MallocAllocator::value_type
T value_type
Definition mallocallocator.hh:32
Dune::MallocAllocator::address
const_pointer address(const_reference x) const
Definition mallocallocator.hh:49
Dune::MallocAllocator::const_reference
const T & const_reference
Definition mallocallocator.hh:31
Dune::MallocAllocator::destroy
void destroy(pointer p)
destroy an object of type T (i.e. call the destructor)
Definition mallocallocator.hh:93
Dune::MallocAllocator::allocate
pointer allocate(size_type n, const void *hint=0)
allocate n objects of type T
Definition mallocallocator.hh:55
Dune::MallocAllocator::address
pointer address(reference x) const
Definition mallocallocator.hh:45
Dune::MallocAllocator::construct
void construct(pointer p, Args &&... args)
construct an object of type T from variadic parameters
Definition mallocallocator.hh:87
Dune::MallocAllocator::reference
T & reference
Definition mallocallocator.hh:30
Dune::MallocAllocator::max_size
size_type max_size() const noexcept
max size for allocate
Definition mallocallocator.hh:74
Dune::MallocAllocator::const_pointer
const T * const_pointer
Definition mallocallocator.hh:29
Dune::MallocAllocator::rebind
Definition mallocallocator.hh:33
Dune::MallocAllocator::rebind::other
MallocAllocator< U > other
Definition mallocallocator.hh:34