Make sure zone memory can allocate blocks that are larger than its default maximum size and throw an error if allocation failed

This commit is contained in:
Jan 2021-03-18 11:48:41 +01:00
parent 50808bfad7
commit 0a4886e7ab
2 changed files with 18 additions and 5 deletions

View File

@ -1,6 +1,6 @@
#include "InMemoryZoneData.h"
#include <cassert>
#include <stdexcept>
InMemoryZoneData::InMemoryZoneData()
: m_total_size(0)
@ -12,15 +12,22 @@ InMemoryZoneData::MemoryBuffer::MemoryBuffer(const size_t size)
: m_data(std::make_unique<char[]>(size)),
m_size(0)
{
if (!m_data)
throw std::runtime_error("Failed to allocate memory for memory buffer.");
}
void* InMemoryZoneData::GetBufferOfSize(const size_t size)
{
assert(size <= BUFFER_SIZE);
if(m_buffers.back().m_size + size > BUFFER_SIZE)
if(size > BUFFER_SIZE)
{
m_buffers.emplace_back(BUFFER_SIZE);
m_buffers.emplace_back(size);
}
else
{
if (m_buffers.back().m_size + size > BUFFER_SIZE)
{
m_buffers.emplace_back(BUFFER_SIZE);
}
}
auto& backBuffer = m_buffers.back();

View File

@ -1,6 +1,7 @@
#include "ZoneWriter.h"
#include <iostream>
#include <stdexcept>
#include "WritingException.h"
#include "WritingFileStream.h"
@ -74,6 +75,11 @@ bool ZoneWriter::WriteZone(std::ostream& stream)
std::cout << "Writing fastfile failed: " << e.Message() << std::endl;
return false;
}
catch (std::runtime_error& e)
{
std::cout << "Writing fastfile failed: " << e.what() << std::endl;
return false;
}
endStream->Flush();