Blame examples/memkind_allocated_example.cpp

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
#include "memkind_allocated.hpp"
Packit 345191
Packit 345191
// This code is example usage of C++11 features with custom allocator,
Packit 345191
// support for C++11 is required.
Packit 345191
#include <iostream>
Packit 345191
Packit 345191
#if __cplusplus > 199711L
Packit 345191
Packit 345191
#include <cstdlib>
Packit 345191
#include <new>
Packit 345191
#include <string>
Packit 345191
Packit 345191
//example class definition, which derive from  memkind_allocated template to
Packit 345191
//have objects allocated with memkind, and have alignment specified by alignas()
Packit 345191
class alignas(128) memkind_allocated_example : public
Packit 345191
    memkind_allocated<memkind_allocated_example>
Packit 345191
{
Packit 345191
    std::string message;
Packit 345191
Packit 345191
public:
Packit 345191
    //Override method for returning class default kind to make objects be by default allocated on High-Bandwidth Memory
Packit 345191
    static memkind_t getClassKind()
Packit 345191
    {
Packit 345191
        return MEMKIND_HBW;
Packit 345191
    }
Packit 345191
Packit 345191
    memkind_allocated_example(std::string my_message)
Packit 345191
    {
Packit 345191
        this->message = my_message;
Packit 345191
    }
Packit 345191
Packit 345191
    memkind_allocated_example()
Packit 345191
    {
Packit 345191
    }
Packit 345191
Packit 345191
    void print_message()
Packit 345191
    {
Packit 345191
        std::cout << message << std::endl;
Packit 345191
        std::cout << "Memory address of this object is: " <<  (void *)this << std::endl
Packit 345191
                  << std::endl;
Packit 345191
    }
Packit 345191
};
Packit 345191
Packit 345191
int main()
Packit 345191
{
Packit 345191
    memkind_t specified_kind = MEMKIND_HBW_HUGETLB;
Packit 345191
Packit 345191
    memkind_allocated_example *default_kind_example = new memkind_allocated_example(
Packit 345191
        std::string("This object has been allocated using class default kind, which is: MEMKIND_DEFAULT") );
Packit 345191
    default_kind_example->print_message();
Packit 345191
    delete default_kind_example;
Packit 345191
Packit 345191
    memkind_allocated_example *specified_kind_example = new(
Packit 345191
        specified_kind) memkind_allocated_example(
Packit 345191
        std::string("This object has been allocated using specified kind, which is: MEMKIND_HBW_HUGETLB") );
Packit 345191
    specified_kind_example->print_message();
Packit 345191
    delete specified_kind_example;
Packit 345191
Packit 345191
    //examples for using same approach for allocating arrays of objects, note that objects created that way can be initialized only with default (unparameterized) constructor
Packit 345191
    memkind_allocated_example *default_kind_array_example = new
Packit 345191
    memkind_allocated_example[5]();
Packit 345191
    delete[] default_kind_array_example;
Packit 345191
Packit 345191
    memkind_allocated_example *specified_kind_array_example = new(
Packit 345191
        specified_kind) memkind_allocated_example[5]();
Packit 345191
    delete[] specified_kind_array_example;
Packit 345191
Packit 345191
    return 0;
Packit 345191
}
Packit 345191
#else //If C++11 is not available - do nothing.
Packit 345191
int main()
Packit 345191
{
Packit 345191
    std::cout << "WARNING: because your compiler does not support C++11 standard,"
Packit 345191
              << std::endl;
Packit 345191
    std::cout << "this example is only as a dummy placeholder." << std::endl;
Packit 345191
    return 0;
Packit 345191
}
Packit 345191
#endif