Blame examples/memkind_allocated_example.cpp

Packit 345191
/*
Packit 345191
 * Copyright (C) 2014 - 2019 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
///////////////////////////////////////////////////////////////////////////////////////////////////////////
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