Blame include/hbw_allocator.h

Packit 345191
/*
Packit 345191
 * Copyright (C) 2014 - 2018 Intel Corporation.
Packit 345191
 * All rights reserved.
Packit 345191
 *
Packit 345191
 * Redistribution and use in source and binary forms, with or without
Packit 345191
 * modification, are permitted provided that the following conditions are met:
Packit 345191
 * 1. Redistributions of source code must retain the above copyright notice(s),
Packit 345191
 *    this list of conditions and the following disclaimer.
Packit 345191
 * 2. Redistributions in binary form must reproduce the above copyright notice(s),
Packit 345191
 *    this list of conditions and the following disclaimer in the documentation
Packit 345191
 *    and/or other materials provided with the distribution.
Packit 345191
 *
Packit 345191
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS
Packit 345191
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
Packit 345191
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
Packit 345191
 * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit 345191
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit 345191
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
Packit 345191
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
Packit 345191
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
Packit 345191
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
Packit 345191
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 345191
 */
Packit 345191
Packit 345191
#pragma once
Packit 345191
Packit 345191
#include <hbwmalloc.h>
Packit 345191
Packit 345191
#include <stddef.h>
Packit 345191
#include <new>
Packit 345191
/*
Packit 345191
 * Header file for the C++ allocator compatible with the C++ standard library allocator concepts.
Packit 345191
 * More details in hbwallocator(3) man page.
Packit 345191
 * Note: memory heap management is based on hbwmalloc, refer to the hbwmalloc man page for more information.
Packit 345191
 *
Packit 345191
 * Functionality defined in this header is considered as EXPERIMENTAL API.
Packit 345191
 * API standards are described in memkind(3) man page.
Packit 345191
 */
Packit 345191
namespace hbw
Packit 345191
{
Packit 345191
Packit 345191
    template <class T>
Packit 345191
    class allocator
Packit 345191
    {
Packit 345191
    public:
Packit 345191
        /*
Packit 345191
         *  Public member types required and defined by the standard library allocator concepts.
Packit 345191
         */
Packit 345191
        typedef size_t size_type;
Packit 345191
        typedef ptrdiff_t difference_type;
Packit 345191
        typedef T *pointer;
Packit 345191
        typedef const T *const_pointer;
Packit 345191
        typedef T &reference;
Packit 345191
        typedef const T &const_reference;
Packit 345191
        typedef T value_type;
Packit 345191
Packit 345191
        template <class U>
Packit 345191
        struct rebind {
Packit 345191
            typedef hbw::allocator<U> other;
Packit 345191
        };
Packit 345191
Packit 345191
        /*
Packit 345191
         *  Public member functions required and defined by the standard library allocator concepts.
Packit 345191
         */
Packit 345191
        allocator() throw() { }
Packit 345191
Packit 345191
        template <class U>
Packit 345191
        allocator(const allocator<U> &) throw() { }
Packit 345191
Packit 345191
        ~allocator() throw() { }
Packit 345191
Packit 345191
        pointer address(reference x) const
Packit 345191
        {
Packit 345191
            return &x;
Packit 345191
        }
Packit 345191
Packit 345191
        const_pointer address(const_reference x) const
Packit 345191
        {
Packit 345191
            return &x;
Packit 345191
        }
Packit 345191
Packit 345191
        /*
Packit 345191
         *  Allocates n*sizeof(T) bytes of high bandwidth memory using hbw_malloc().
Packit 345191
         *  Throws std::bad_alloc when cannot allocate memory.
Packit 345191
         */
Packit 345191
        pointer allocate(size_type n, const void * = 0)
Packit 345191
        {
Packit 345191
            if (n > this->max_size()) {
Packit 345191
                throw std::bad_alloc();
Packit 345191
            }
Packit 345191
            pointer result = static_cast<pointer>(hbw_malloc(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
        /*
Packit 345191
         *  Deallocates memory associated with pointer returned by allocate() using hbw_free().
Packit 345191
         */
Packit 345191
        void deallocate(pointer p, size_type n)
Packit 345191
        {
Packit 345191
            hbw_free(static_cast<void *>(p));
Packit 345191
        }
Packit 345191
Packit 345191
        size_type max_size() const throw()
Packit 345191
        {
Packit 345191
            return size_t(-1) / sizeof(T);
Packit 345191
        }
Packit 345191
Packit 345191
        void construct(pointer p, const_reference val)
Packit 345191
        {
Packit 345191
            ::new(p) value_type(val);
Packit 345191
        }
Packit 345191
Packit 345191
        void destroy(pointer p)
Packit 345191
        {
Packit 345191
            p->~T();
Packit 345191
        }
Packit 345191
    };
Packit 345191
Packit 345191
    template <class T, class U>
Packit 345191
    bool operator==(const allocator<T> &, const allocator<U> &)
Packit 345191
    {
Packit 345191
        return true;
Packit 345191
    }
Packit 345191
Packit 345191
    template <class T, class U>
Packit 345191
    bool operator!=(const allocator<T> &, const allocator<U> &)
Packit 345191
    {
Packit 345191
        return false;
Packit 345191
    }
Packit 345191
Packit 345191
}