<commit_before>//
// Created by <NAME> on 24.11.18.
//

#include "SimpleMemoryAllocator.h"

void SimpleMemoryAllocator::deallocate(MemoryLocation &location) {
    LOG(INFO) << "Simple Allocator: Deallocating" << location.memory << std::endl;

    auto foundAllocation = std::find(m_allocations.begin(), m_allocations.end(), location);
    if (foundAllocation == m_allocations.end()) {
        LOG(ERROR) << "Allocation was not made with this allocator";
        return;
    }
    foundAllocation->destroy(m_device);
    m_allocations.erase(foundAllocation);
}

void SimpleMemoryAllocator::destroy() {
    for(auto& location : m_allocations) {
        if (location.buffer != static_cast<vk::Buffer>(nullptr)) {
            location.destroy(m_device);
        }
    }
    m_allocations.clear();
}

MemoryLocation SimpleMemoryAllocator::allocate(vk::DeviceSize size) {
    LOG(INFO) << "Simple Alloc " << vk::to_string(usageFlags) << " " << std::to_string(size);
    m_bufferCreateInfo.size = size;
    auto buffer = m_device.createBuffer(m_bufferCreateInfo);

    auto memReqs = m_device.getBufferMemoryRequirements(buffer);

    vk::MemoryAllocateInfo info;
    info.allocationSize = memReqs.size;
    info.memoryTypeIndex = findMemoryType(m_physicalDevice, memReqs.memoryTypeBits, flags);
    auto memory = SafeAllocator::instance()->allocateMemory(m_device, info);

    void* mappedPtr = nullptr;
    if (mapped) {
        mappedPtr = m_device.mapMemory(memory,0, memReqs.size);
    }
    m_device.bindBufferMemory(buffer, memory,0);

    mutex.lock();
    m_allocations.emplace_back(buffer, memory, 0,size, mappedPtr);
    auto retVal = m_allocations.back();
    mutex.unlock();
    return retVal;
}

void SimpleMemoryAllocator::init(vk::Device _device, vk::PhysicalDevice _physicalDevice,
                                 const vk::MemoryPropertyFlags &_flags, const vk::BufferUsageFlags &usage, bool _mapped) {
    m_device = _device;
    m_physicalDevice = _physicalDevice;
    flags = _flags;
    mapped = _mapped;
    usageFlags = usage;
    m_bufferCreateInfo.sharingMode = vk::SharingMode::eExclusive;
    m_bufferCreateInfo.usage = usage;
    m_bufferCreateInfo.size = 0;
}
<commit_msg>Use mutex when deallocating in simple memory allocator<commit_after>//
// Created by <NAME> on 24.11.18.
//

#include "SimpleMemoryAllocator.h"

void SimpleMemoryAllocator::deallocate(MemoryLocation &location) {
    mutex.lock();
    LOG(INFO) << "Simple Allocator: Deallocating" << location.memory << std::endl;

    auto foundAllocation = std::find(m_allocations.begin(), m_allocations.end(), location);
    if (foundAllocation == m_allocations.end()) {
        LOG(ERROR) << "Allocation was not made with this allocator";
        return;
    }
    foundAllocation->destroy(m_device);
    m_allocations.erase(foundAllocation);
    mutex.unlock();
}

void SimpleMemoryAllocator::destroy() {
    for(auto& location : m_allocations) {
        if (location.buffer != static_cast<vk::Buffer>(nullptr)) {
            location.destroy(m_device);
        }
    }
    m_allocations.clear();
}

MemoryLocation SimpleMemoryAllocator::allocate(vk::DeviceSize size) {
    LOG(INFO) << "Simple Alloc " << vk::to_string(usageFlags) << " " << std::to_string(size);
    m_bufferCreateInfo.size = size;
    auto buffer = m_device.createBuffer(m_bufferCreateInfo);

    auto memReqs = m_device.getBufferMemoryRequirements(buffer);

    vk::MemoryAllocateInfo info;
    info.allocationSize = memReqs.size;
    info.memoryTypeIndex = findMemoryType(m_physicalDevice, memReqs.memoryTypeBits, flags);
    auto memory = SafeAllocator::instance()->allocateMemory(m_device, info);

    void* mappedPtr = nullptr;
    if (mapped) {
        mappedPtr = m_device.mapMemory(memory,0, memReqs.size);
    }
    m_device.bindBufferMemory(buffer, memory,0);

    mutex.lock();
    m_allocations.emplace_back(buffer, memory, 0,size, mappedPtr);
    auto retVal = m_allocations.back();
    mutex.unlock();
    return retVal;
}

void SimpleMemoryAllocator::init(vk::Device _device, vk::PhysicalDevice _physicalDevice,
                                 const vk::MemoryPropertyFlags &_flags, const vk::BufferUsageFlags &usage, bool _mapped) {
    m_device = _device;
    m_physicalDevice = _physicalDevice;
    flags = _flags;
    mapped = _mapped;
    usageFlags = usage;
    m_bufferCreateInfo.sharingMode = vk::SharingMode::eExclusive;
    m_bufferCreateInfo.usage = usage;
    m_bufferCreateInfo.size = 0;
}

{'fraction_non_alphanumeric': 0.1221203310221427, 'fraction_numerical': 0.004473272198613286, 'mean_word_length': 3.9469026548672566, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4036695', 'n_tokens_mistral': 1436, 'n_tokens_neox': 1389, 'n_words': 257}