Blame examples/memkind_allocated_example.cpp

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