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

View File

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