Blame include/pmem_allocator.h

Packit Service 724aca
/*
Packit Service 724aca
 * Copyright (C) 2018 - 2020 Intel Corporation.
Packit Service 724aca
 * All rights reserved.
Packit Service 724aca
 *
Packit Service 724aca
 * Redistribution and use in source and binary forms, with or without
Packit Service 724aca
 * modification, are permitted provided that the following conditions are met:
Packit Service 724aca
 * 1. Redistributions of source code must retain the above copyright notice(s),
Packit Service 724aca
 *    this list of conditions and the following disclaimer.
Packit Service 724aca
 * 2. Redistributions in binary form must reproduce the above copyright notice(s),
Packit Service 724aca
 *    this list of conditions and the following disclaimer in the documentation
Packit Service 724aca
 *    and/or other materials provided with the distribution.
Packit Service 724aca
 *
Packit Service 724aca
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS
Packit Service 724aca
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
Packit Service 724aca
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
Packit Service 724aca
 * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit Service 724aca
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit Service 724aca
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
Packit Service 724aca
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
Packit Service 724aca
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
Packit Service 724aca
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
Packit Service 724aca
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service 724aca
 */
Packit Service 724aca
Packit Service 724aca
#pragma once
Packit Service 724aca
Packit Service 724aca
#include <memory>
Packit Service 724aca
#include <string>
Packit Service 724aca
#include <exception>
Packit Service 724aca
#include <type_traits>
Packit Service 724aca
#include <atomic>
Packit Service 724aca
#include <cstddef>
Packit Service 724aca
#include <stdexcept>
Packit Service 724aca
Packit Service 724aca
#include "memkind.h"
Packit Service 724aca
Packit Service 724aca
/*
Packit Service 724aca
 * Header file for the C++ allocator compatible with the C++ standard library allocator concepts.
Packit Service 724aca
 * More details in pmemallocator(3) man page.
Packit Service 724aca
 * Note: memory heap management is based on memkind_malloc, refer to the memkind(3) man page for more
Packit Service 724aca
 * information.
Packit Service 724aca
 *
Packit Service 724aca
 * Functionality defined in this header is considered as stable API (STANDARD API).
Packit Service 724aca
 * API standards are described in memkind(3) man page.
Packit Service 724aca
 */
Packit Service 724aca
namespace libmemkind
Packit Service 724aca
{
Packit Service 724aca
    enum class allocation_policy {
Packit Service 724aca
        DEFAULT = MEMKIND_MEM_USAGE_POLICY_DEFAULT,
Packit Service 724aca
        CONSERVATIVE = MEMKIND_MEM_USAGE_POLICY_CONSERVATIVE,
Packit Service 724aca
        MAX
Packit Service 724aca
    };
Packit Service 724aca
Packit Service 724aca
    namespace pmem
Packit Service 724aca
    {
Packit Service 724aca
        namespace internal
Packit Service 724aca
        {
Packit Service 724aca
            class kind_wrapper_t
Packit Service 724aca
            {
Packit Service 724aca
            public:
Packit Service 724aca
                kind_wrapper_t(const char *dir, std::size_t max_size,
Packit Service 724aca
                               libmemkind::allocation_policy alloc_policy=
Packit Service 724aca
                                   libmemkind::allocation_policy::DEFAULT)
Packit Service 724aca
                {
Packit Service 724aca
                    cfg = memkind_config_new();
Packit Service 724aca
                    if (!cfg) {
Packit Service 724aca
                        throw std::runtime_error(
Packit Service 724aca
                            std::string("An error occurred while creating pmem config"));
Packit Service 724aca
                    }
Packit Service 724aca
                    memkind_config_set_path(cfg, dir);
Packit Service 724aca
                    memkind_config_set_size(cfg, max_size);
Packit Service 724aca
                    memkind_config_set_memory_usage_policy(cfg,
Packit Service 724aca
                                                           static_cast<memkind_mem_usage_policy>(alloc_policy));
Packit Service 724aca
                    int err_c  = memkind_create_pmem_with_config(cfg, &kind);
Packit Service 724aca
                    memkind_config_delete(cfg);
Packit Service 724aca
                    if (err_c) {
Packit Service 724aca
                        throw std::runtime_error(
Packit Service 724aca
                            std::string("An error occurred while creating pmem kind; error code: ") +
Packit Service 724aca
                            std::to_string(err_c));
Packit Service 724aca
                    }
Packit Service 724aca
                }
Packit Service 724aca
Packit Service 724aca
                kind_wrapper_t(const kind_wrapper_t &) = delete;
Packit Service 724aca
                void operator=(const kind_wrapper_t &) = delete;
Packit Service 724aca
Packit Service 724aca
                ~kind_wrapper_t()
Packit Service 724aca
                {
Packit Service 724aca
                    memkind_destroy_kind(kind);
Packit Service 724aca
                }
Packit Service 724aca
Packit Service 724aca
                memkind_t get() const
Packit Service 724aca
                {
Packit Service 724aca
                    return kind;
Packit Service 724aca
                }
Packit Service 724aca
Packit Service 724aca
            private:
Packit Service 724aca
                memkind_t kind;
Packit Service 724aca
                struct memkind_config *cfg;
Packit Service 724aca
            };
Packit Service 724aca
        }
Packit Service 724aca
Packit Service 724aca
        template<typename T>
Packit Service 724aca
        class allocator
Packit Service 724aca
        {
Packit Service 724aca
            using kind_wrapper_t = internal::kind_wrapper_t;
Packit Service 724aca
            std::shared_ptr<kind_wrapper_t> kind_wrapper_ptr;
Packit Service 724aca
        public:
Packit Service 724aca
            using value_type = T;
Packit Service 724aca
            using pointer = value_type*;
Packit Service 724aca
            using const_pointer = const value_type*;
Packit Service 724aca
            using reference = value_type&;
Packit Service 724aca
            using const_reference = const value_type&;
Packit Service 724aca
            using size_type = size_t;
Packit Service 724aca
            using difference_type = ptrdiff_t;
Packit Service 724aca
Packit Service 724aca
            template<class U>
Packit Service 724aca
            struct rebind {
Packit Service 724aca
                using other = allocator<U>;
Packit Service 724aca
            };
Packit Service 724aca
Packit Service 724aca
            template<typename U>
Packit Service 724aca
            friend class allocator;
Packit Service 724aca
Packit Service 724aca
#if !_GLIBCXX_USE_CXX11_ABI
Packit Service 724aca
            /* This is a workaround for compilers (e.g GCC 4.8) that uses C++11 standard,
Packit Service 724aca
             * but use old - non C++11 ABI */
Packit Service 724aca
            template<typename V = void>
Packit Service 724aca
            explicit allocator()
Packit Service 724aca
            {
Packit Service 724aca
                static_assert(std::is_same<V, void>::value,
Packit Service 724aca
                              "libmemkind::pmem::allocator cannot be compiled without CXX11 ABI");
Packit Service 724aca
            }
Packit Service 724aca
#endif
Packit Service 724aca
Packit Service 724aca
            explicit allocator(const char *dir, size_t max_size) :
Packit Service 724aca
                kind_wrapper_ptr(std::make_shared<kind_wrapper_t>(dir, max_size))
Packit Service 724aca
            {
Packit Service 724aca
            }
Packit Service 724aca
Packit Service 724aca
            explicit allocator(const std::string &dir, size_t max_size) :
Packit Service 724aca
                allocator(dir.c_str(), max_size)
Packit Service 724aca
            {
Packit Service 724aca
            }
Packit Service 724aca
Packit Service 724aca
            explicit allocator(const char *dir, size_t max_size,
Packit Service 724aca
                               libmemkind::allocation_policy alloc_policy) :
Packit Service 724aca
                kind_wrapper_ptr(std::make_shared<kind_wrapper_t>(dir, max_size, alloc_policy))
Packit Service 724aca
            {
Packit Service 724aca
            }
Packit Service 724aca
Packit Service 724aca
            explicit allocator(const std::string &dir, size_t max_size,
Packit Service 724aca
                               libmemkind::allocation_policy alloc_policy) :
Packit Service 724aca
                allocator(dir.c_str(), max_size, alloc_policy)
Packit Service 724aca
            {
Packit Service 724aca
            }
Packit Service 724aca
Packit Service 724aca
            allocator(const allocator &other) = default;
Packit Service 724aca
Packit Service 724aca
            template <typename U>
Packit Service 724aca
            allocator(const allocator<U> &other) noexcept : kind_wrapper_ptr(
Packit Service 724aca
                    other.kind_wrapper_ptr)
Packit Service 724aca
            {
Packit Service 724aca
            }
Packit Service 724aca
Packit Service 724aca
            allocator(allocator &&other) = default;
Packit Service 724aca
Packit Service 724aca
            template <typename U>
Packit Service 724aca
            allocator(allocator<U> &&other) noexcept :
Packit Service 724aca
                kind_wrapper_ptr(std::move(other.kind_wrapper_ptr))
Packit Service 724aca
            {
Packit Service 724aca
            }
Packit Service 724aca
Packit Service 724aca
            allocator<T> &operator = (const allocator &other) = default;
Packit Service 724aca
Packit Service 724aca
            template <typename U>
Packit Service 724aca
            allocator<T> &operator = (const allocator<U> &other) noexcept
Packit Service 724aca
            {
Packit Service 724aca
                kind_wrapper_ptr = other.kind_wrapper_ptr;
Packit Service 724aca
                return *this;
Packit Service 724aca
            }
Packit Service 724aca
Packit Service 724aca
            allocator<T> &operator = (allocator &&other) = default;
Packit Service 724aca
Packit Service 724aca
            template <typename U>
Packit Service 724aca
            allocator<T> &operator = (allocator<U> &&other) noexcept
Packit Service 724aca
            {
Packit Service 724aca
                kind_wrapper_ptr = std::move(other.kind_wrapper_ptr);
Packit Service 724aca
                return *this;
Packit Service 724aca
            }
Packit Service 724aca
Packit Service 724aca
            pointer allocate(size_type n) const
Packit Service 724aca
            {
Packit Service 724aca
                pointer result = static_cast<pointer>(memkind_malloc(kind_wrapper_ptr->get(),
Packit Service 724aca
                                                                     n*sizeof(T)));
Packit Service 724aca
                if (!result) {
Packit Service 724aca
                    throw std::bad_alloc();
Packit Service 724aca
                }
Packit Service 724aca
                return result;
Packit Service 724aca
            }
Packit Service 724aca
Packit Service 724aca
            void deallocate(pointer p, size_type n) const
Packit Service 724aca
            {
Packit Service 724aca
                memkind_free(kind_wrapper_ptr->get(), static_cast<void *>(p));
Packit Service 724aca
            }
Packit Service 724aca
Packit Service 724aca
            template <class U, class... Args>
Packit Service 724aca
            void construct(U *p, Args &&... args) const
Packit Service 724aca
            {
Packit Service 724aca
                ::new((void *)p) U(std::forward<Args>(args)...);
Packit Service 724aca
            }
Packit Service 724aca
Packit Service 724aca
            void destroy(pointer p) const
Packit Service 724aca
            {
Packit Service 724aca
                p->~value_type();
Packit Service 724aca
            }
Packit Service 724aca
Packit Service 724aca
            template <typename U, typename V>
Packit Service 724aca
            friend bool operator ==(const allocator<U> &lhs, const allocator<V> &rhs;;
Packit Service 724aca
Packit Service 724aca
            template <typename U, typename V>
Packit Service 724aca
            friend bool operator !=(const allocator<U> &lhs, const allocator<V> &rhs;;
Packit Service 724aca
        };
Packit Service 724aca
Packit Service 724aca
        template <typename U, typename V>
Packit Service 724aca
        bool operator ==(const allocator<U> &lhs, const allocator<V> &rhs)
Packit Service 724aca
        {
Packit Service 724aca
            return lhs.kind_wrapper_ptr->get() == rhs.kind_wrapper_ptr->get();
Packit Service 724aca
        }
Packit Service 724aca
Packit Service 724aca
        template <typename U, typename V>
Packit Service 724aca
        bool operator !=(const allocator<U> &lhs, const allocator<V> &rhs)
Packit Service 724aca
        {
Packit Service 724aca
            return !(lhs == rhs);
Packit Service 724aca
        }
Packit Service 724aca
    }  // namespace pmem
Packit Service 724aca
}  // namespace libmemkind