Blame include/pmem_allocator.h

Packit Service 7f3b24
// SPDX-License-Identifier: BSD-2-Clause
Packit Service 7f3b24
/* Copyright (C) 2018 - 2020 Intel Corporation. */
Packit 345191
Packit 345191
#pragma once
Packit 345191
Packit 345191
#include <memory>
Packit 345191
#include <string>
Packit 345191
#include <exception>
Packit 345191
#include <type_traits>
Packit 345191
#include <atomic>
Packit 345191
#include <cstddef>
Packit 345191
#include <stdexcept>
Packit 345191
Packit 345191
#include "memkind.h"
Packit 345191
Packit 345191
/*
Packit 345191
 * Header file for the C++ allocator compatible with the C++ standard library allocator concepts.
Packit 345191
 * More details in pmemallocator(3) man page.
Packit 345191
 * Note: memory heap management is based on memkind_malloc, refer to the memkind(3) man page for more
Packit 345191
 * information.
Packit 345191
 *
Packit 345191
 * Functionality defined in this header is considered as stable API (STANDARD API).
Packit 345191
 * API standards are described in memkind(3) man page.
Packit 345191
 */
Packit 345191
namespace libmemkind
Packit 345191
{
Packit 345191
    enum class allocation_policy {
Packit 345191
        DEFAULT = MEMKIND_MEM_USAGE_POLICY_DEFAULT,
Packit 345191
        CONSERVATIVE = MEMKIND_MEM_USAGE_POLICY_CONSERVATIVE,
Packit 345191
        MAX
Packit 345191
    };
Packit 345191
Packit 345191
    namespace pmem
Packit 345191
    {
Packit 345191
        namespace internal
Packit 345191
        {
Packit 345191
            class kind_wrapper_t
Packit 345191
            {
Packit 345191
            public:
Packit 345191
                kind_wrapper_t(const char *dir, std::size_t max_size,
Packit 345191
                               libmemkind::allocation_policy alloc_policy=
Packit 345191
                                   libmemkind::allocation_policy::DEFAULT)
Packit 345191
                {
Packit 345191
                    cfg = memkind_config_new();
Packit 345191
                    if (!cfg) {
Packit 345191
                        throw std::runtime_error(
Packit 345191
                            std::string("An error occurred while creating pmem config"));
Packit 345191
                    }
Packit 345191
                    memkind_config_set_path(cfg, dir);
Packit 345191
                    memkind_config_set_size(cfg, max_size);
Packit 345191
                    memkind_config_set_memory_usage_policy(cfg,
Packit 345191
                                                           static_cast<memkind_mem_usage_policy>(alloc_policy));
Packit 345191
                    int err_c  = memkind_create_pmem_with_config(cfg, &kind);
Packit 345191
                    memkind_config_delete(cfg);
Packit 345191
                    if (err_c) {
Packit 345191
                        throw std::runtime_error(
Packit 345191
                            std::string("An error occurred while creating pmem kind; error code: ") +
Packit 345191
                            std::to_string(err_c));
Packit 345191
                    }
Packit 345191
                }
Packit 345191
Packit 345191
                kind_wrapper_t(const kind_wrapper_t &) = delete;
Packit 345191
                void operator=(const kind_wrapper_t &) = delete;
Packit 345191
Packit 345191
                ~kind_wrapper_t()
Packit 345191
                {
Packit 345191
                    memkind_destroy_kind(kind);
Packit 345191
                }
Packit 345191
Packit 345191
                memkind_t get() const
Packit 345191
                {
Packit 345191
                    return kind;
Packit 345191
                }
Packit 345191
Packit 345191
            private:
Packit 345191
                memkind_t kind;
Packit 345191
                struct memkind_config *cfg;
Packit 345191
            };
Packit 345191
        }
Packit 345191
Packit 345191
        template<typename T>
Packit 345191
        class allocator
Packit 345191
        {
Packit 345191
            using kind_wrapper_t = internal::kind_wrapper_t;
Packit 345191
            std::shared_ptr<kind_wrapper_t> kind_wrapper_ptr;
Packit 345191
        public:
Packit 345191
            using value_type = T;
Packit 345191
            using pointer = value_type*;
Packit 345191
            using const_pointer = const value_type*;
Packit 345191
            using reference = value_type&;
Packit 345191
            using const_reference = const value_type&;
Packit 345191
            using size_type = size_t;
Packit 345191
            using difference_type = ptrdiff_t;
Packit 345191
Packit 345191
            template<class U>
Packit 345191
            struct rebind {
Packit 345191
                using other = allocator<U>;
Packit 345191
            };
Packit 345191
Packit 345191
            template<typename U>
Packit 345191
            friend class allocator;
Packit 345191
Packit 345191
#if !_GLIBCXX_USE_CXX11_ABI
Packit 345191
            /* This is a workaround for compilers (e.g GCC 4.8) that uses C++11 standard,
Packit 345191
             * but use old - non C++11 ABI */
Packit 345191
            template<typename V = void>
Packit 345191
            explicit allocator()
Packit 345191
            {
Packit 345191
                static_assert(std::is_same<V, void>::value,
Packit 345191
                              "libmemkind::pmem::allocator cannot be compiled without CXX11 ABI");
Packit 345191
            }
Packit 345191
#endif
Packit 345191
Packit 345191
            explicit allocator(const char *dir, size_t max_size) :
Packit 345191
                kind_wrapper_ptr(std::make_shared<kind_wrapper_t>(dir, max_size))
Packit 345191
            {
Packit 345191
            }
Packit 345191
Packit 345191
            explicit allocator(const std::string &dir, size_t max_size) :
Packit 345191
                allocator(dir.c_str(), max_size)
Packit 345191
            {
Packit 345191
            }
Packit 345191
Packit 345191
            explicit allocator(const char *dir, size_t max_size,
Packit 345191
                               libmemkind::allocation_policy alloc_policy) :
Packit 345191
                kind_wrapper_ptr(std::make_shared<kind_wrapper_t>(dir, max_size, alloc_policy))
Packit 345191
            {
Packit 345191
            }
Packit 345191
Packit 345191
            explicit allocator(const std::string &dir, size_t max_size,
Packit 345191
                               libmemkind::allocation_policy alloc_policy) :
Packit 345191
                allocator(dir.c_str(), max_size, alloc_policy)
Packit 345191
            {
Packit 345191
            }
Packit 345191
Packit 345191
            allocator(const allocator &other) = default;
Packit 345191
Packit 345191
            template <typename U>
Packit 345191
            allocator(const allocator<U> &other) noexcept : kind_wrapper_ptr(
Packit 345191
                    other.kind_wrapper_ptr)
Packit 345191
            {
Packit 345191
            }
Packit 345191
Packit 345191
            allocator(allocator &&other) = default;
Packit 345191
Packit 345191
            template <typename U>
Packit 345191
            allocator(allocator<U> &&other) noexcept :
Packit 345191
                kind_wrapper_ptr(std::move(other.kind_wrapper_ptr))
Packit 345191
            {
Packit 345191
            }
Packit 345191
Packit 345191
            allocator<T> &operator = (const allocator &other) = default;
Packit 345191
Packit 345191
            template <typename U>
Packit 345191
            allocator<T> &operator = (const allocator<U> &other) noexcept
Packit 345191
            {
Packit 345191
                kind_wrapper_ptr = other.kind_wrapper_ptr;
Packit 345191
                return *this;
Packit 345191
            }
Packit 345191
Packit 345191
            allocator<T> &operator = (allocator &&other) = default;
Packit 345191
Packit 345191
            template <typename U>
Packit 345191
            allocator<T> &operator = (allocator<U> &&other) noexcept
Packit 345191
            {
Packit 345191
                kind_wrapper_ptr = std::move(other.kind_wrapper_ptr);
Packit 345191
                return *this;
Packit 345191
            }
Packit 345191
Packit 345191
            pointer allocate(size_type n) const
Packit 345191
            {
Packit 345191
                pointer result = static_cast<pointer>(memkind_malloc(kind_wrapper_ptr->get(),
Packit 345191
                                                                     n*sizeof(T)));
Packit 345191
                if (!result) {
Packit 345191
                    throw std::bad_alloc();
Packit 345191
                }
Packit 345191
                return result;
Packit 345191
            }
Packit 345191
Packit 345191
            void deallocate(pointer p, size_type n) const
Packit 345191
            {
Packit 345191
                memkind_free(kind_wrapper_ptr->get(), static_cast<void *>(p));
Packit 345191
            }
Packit 345191
Packit 345191
            template <class U, class... Args>
Packit 345191
            void construct(U *p, Args &&... args) const
Packit 345191
            {
Packit 345191
                ::new((void *)p) U(std::forward<Args>(args)...);
Packit 345191
            }
Packit 345191
Packit 345191
            void destroy(pointer p) const
Packit 345191
            {
Packit 345191
                p->~value_type();
Packit 345191
            }
Packit 345191
Packit 345191
            template <typename U, typename V>
Packit 345191
            friend bool operator ==(const allocator<U> &lhs, const allocator<V> &rhs;;
Packit 345191
Packit 345191
            template <typename U, typename V>
Packit 345191
            friend bool operator !=(const allocator<U> &lhs, const allocator<V> &rhs;;
Packit 345191
        };
Packit 345191
Packit 345191
        template <typename U, typename V>
Packit 345191
        bool operator ==(const allocator<U> &lhs, const allocator<V> &rhs)
Packit 345191
        {
Packit 345191
            return lhs.kind_wrapper_ptr->get() == rhs.kind_wrapper_ptr->get();
Packit 345191
        }
Packit 345191
Packit 345191
        template <typename U, typename V>
Packit 345191
        bool operator !=(const allocator<U> &lhs, const allocator<V> &rhs)
Packit 345191
        {
Packit 345191
            return !(lhs == rhs);
Packit 345191
        }
Packit 345191
    }  // namespace pmem
Packit 345191
}  // namespace libmemkind