Blame memkind-1.10.1/test/allocator_perf_tool/ScenarioWorkload.cpp

Packit Service 7f3b24
// SPDX-License-Identifier: BSD-2-Clause
Packit Service 7f3b24
/* Copyright (C) 2015 - 2020 Intel Corporation. */
Packit Service 7f3b24
#include "ScenarioWorkload.h"
Packit Service 7f3b24
Packit Service 7f3b24
ScenarioWorkload::ScenarioWorkload(VectorIterator<Allocator *> *a,
Packit Service 7f3b24
                                   VectorIterator<size_t> *as,  VectorIterator<int> *fc)
Packit Service 7f3b24
{
Packit Service 7f3b24
    allocators = a;
Packit Service 7f3b24
    func_calls = fc;
Packit Service 7f3b24
    alloc_sizes = as;
Packit Service 7f3b24
}
Packit Service 7f3b24
Packit Service 7f3b24
bool ScenarioWorkload::run()
Packit Service 7f3b24
{
Packit Service 7f3b24
    if(func_calls->has_next() && allocators->has_next() &&
Packit Service 7f3b24
       alloc_sizes->has_next()) {
Packit Service 7f3b24
        switch(func_calls->next()) {
Packit Service 7f3b24
            case FunctionCalls::MALLOC: {
Packit Service 7f3b24
                memory_operation data = allocators->next()->wrapped_malloc(alloc_sizes->next());
Packit Service 7f3b24
                post_allocation_check(data);
Packit Service 7f3b24
                break;
Packit Service 7f3b24
            }
Packit Service 7f3b24
            case FunctionCalls::CALLOC: {
Packit Service 7f3b24
                memory_operation data = allocators->next()->wrapped_calloc(1,
Packit Service 7f3b24
                                                                           alloc_sizes->next());
Packit Service 7f3b24
                post_allocation_check(data);
Packit Service 7f3b24
                break;
Packit Service 7f3b24
            }
Packit Service 7f3b24
            case FunctionCalls::REALLOC: {
Packit Service 7f3b24
                //Guarantee the memory for realloc.
Packit Service 7f3b24
                Allocator *allocator = allocators->next();
Packit Service 7f3b24
                memory_operation to_realloc = allocator->wrapped_malloc(512);
Packit Service 7f3b24
Packit Service 7f3b24
                memory_operation data = allocator->wrapped_realloc(to_realloc.ptr,
Packit Service 7f3b24
                                                                   alloc_sizes->next());
Packit Service 7f3b24
                post_allocation_check(data);
Packit Service 7f3b24
                break;
Packit Service 7f3b24
            }
Packit Service 7f3b24
            case FunctionCalls::FREE: {
Packit Service 7f3b24
                memory_operation *data = get_allocated_memory();
Packit Service 7f3b24
Packit Service 7f3b24
                if(!allocations.empty() && (data != NULL)) {
Packit Service 7f3b24
                    allocator_factory.get_existing(data->allocator_type)->wrapped_free(data->ptr);
Packit Service 7f3b24
                    data->is_allocated = false;
Packit Service 7f3b24
Packit Service 7f3b24
                    memory_operation free_op = *data;
Packit Service 7f3b24
                    free_op.allocation_method = FunctionCalls::FREE;
Packit Service 7f3b24
                    allocations.push_back(free_op);
Packit Service 7f3b24
                }
Packit Service 7f3b24
Packit Service 7f3b24
                break;
Packit Service 7f3b24
            }
Packit Service 7f3b24
            default:
Packit Service 7f3b24
                assert(!"Function call identifier out of range.");
Packit Service 7f3b24
                break;
Packit Service 7f3b24
        }
Packit Service 7f3b24
Packit Service 7f3b24
        return true;
Packit Service 7f3b24
    }
Packit Service 7f3b24
Packit Service 7f3b24
    return false;
Packit Service 7f3b24
}
Packit Service 7f3b24
Packit Service 7f3b24
ScenarioWorkload::~ScenarioWorkload(void)
Packit Service 7f3b24
{
Packit Service 7f3b24
    for (int i=0; i
Packit Service 7f3b24
        memory_operation data = allocations[i];
Packit Service 7f3b24
        if(data.is_allocated && (data.allocation_method != FunctionCalls::FREE))
Packit Service 7f3b24
            allocator_factory.get_existing(data.allocator_type)->wrapped_free(data.ptr);
Packit Service 7f3b24
    }
Packit Service 7f3b24
}
Packit Service 7f3b24
Packit Service 7f3b24
memory_operation *ScenarioWorkload::get_allocated_memory()
Packit Service 7f3b24
{
Packit Service 7f3b24
    for (int i=allocations.size()-1; i>=0; i--) {
Packit Service 7f3b24
        memory_operation *data = &allocations[i];
Packit Service 7f3b24
        if(data->is_allocated)
Packit Service 7f3b24
            return data;
Packit Service 7f3b24
    }
Packit Service 7f3b24
Packit Service 7f3b24
    return NULL;
Packit Service 7f3b24
}
Packit Service 7f3b24
Packit Service 7f3b24
void ScenarioWorkload::post_allocation_check(const memory_operation &data)
Packit Service 7f3b24
{
Packit Service 7f3b24
    allocations.push_back(data);
Packit Service 7f3b24
    if(touch_memory_on_allocation && (data.ptr != NULL) &&
Packit Service 7f3b24
       (data.error_code != ENOMEM)) {
Packit Service 7f3b24
        //Write memory to ensure physical allocation.
Packit Service 7f3b24
        memset(data.ptr, 1, data.size_of_allocation);
Packit Service 7f3b24
    }
Packit Service 7f3b24
}