Blame examples/memkind_allocated.hpp

Packit Service 7f3b24
// SPDX-License-Identifier: BSD-2-Clause
Packit Service 7f3b24
/* Copyright (C) 2014 - 2020 Intel Corporation. */
Packit 345191
Packit 345191
///////////////////////////////////////////////////////////////////////////////////////////////////////////
Packit 345191
Packit 345191
#include <memkind.h>
Packit 345191
Packit 345191
#include <cstdlib>
Packit 345191
#include <new>
Packit 345191
Packit 345191
template <class deriving_class>
Packit 345191
class memkind_allocated
Packit 345191
{
Packit 345191
public:
Packit 345191
    static memkind_t getClassKind()
Packit 345191
    {
Packit 345191
        return MEMKIND_DEFAULT;
Packit 345191
    }
Packit 345191
Packit 345191
    void *operator new(std::size_t size)
Packit 345191
    {
Packit 345191
        return deriving_class::operator new(size, deriving_class::getClassKind());
Packit 345191
    }
Packit 345191
Packit 345191
    void *operator new[](std::size_t size)
Packit 345191
    {
Packit 345191
        return deriving_class::operator new(size, deriving_class::getClassKind());
Packit 345191
    }
Packit 345191
Packit 345191
    void *operator new(std::size_t size, memkind_t memory_kind)
Packit 345191
    {
Packit 345191
        void *result_ptr = NULL;
Packit 345191
        int allocation_result = 0;
Packit 345191
Packit 345191
        //This check if deriving_class has specified alignment, which is suitable
Packit 345191
        //to be used with posix_memalign()
Packit 345191
        if(alignof(deriving_class) <  sizeof(void *)) {
Packit 345191
            result_ptr = memkind_malloc(memory_kind, size);
Packit 345191
            allocation_result = result_ptr ? 1 : 0;
Packit 345191
        } else {
Packit 345191
            allocation_result = memkind_posix_memalign(memory_kind, &result_ptr,
Packit 345191
                                                       alignof(deriving_class), size);
Packit 345191
        }
Packit 345191
Packit 345191
        if(allocation_result) {
Packit 345191
            throw std::bad_alloc();
Packit 345191
        }
Packit 345191
Packit 345191
        return result_ptr;
Packit 345191
    }
Packit 345191
Packit 345191
    void *operator new[](std::size_t size, memkind_t memory_kind)
Packit 345191
    {
Packit 345191
        return deriving_class::operator new(size, memory_kind);
Packit 345191
    }
Packit 345191
Packit 345191
    void operator delete(void *ptr, memkind_t memory_kind)
Packit 345191
    {
Packit 345191
        memkind_free(memory_kind, ptr);
Packit 345191
    }
Packit 345191
Packit 345191
    void operator delete(void *ptr)
Packit 345191
    {
Packit 345191
        memkind_free(0, ptr);
Packit 345191
    }
Packit 345191
Packit 345191
    void operator delete[](void *ptr)
Packit 345191
    {
Packit 345191
        deriving_class::operator delete(ptr);
Packit 345191
    }
Packit 345191
Packit 345191
    void operator delete[](void *ptr, memkind_t memory_kind)
Packit 345191
    {
Packit 345191
        deriving_class::operator delete(ptr, memory_kind);
Packit 345191
    }
Packit 345191
Packit 345191
protected:
Packit 345191
    memkind_allocated()
Packit 345191
    {
Packit 345191
    }
Packit 345191
Packit 345191
    ~memkind_allocated()
Packit 345191
    {
Packit 345191
    }
Packit 345191
Packit 345191
};