Files
OpenRCT2-Web/src/openrct2/core/MemoryStream.cpp
T

187 lines
4.7 KiB
C++
Raw Normal View History

2017-06-01 21:55:10 +02:00
#pragma region Copyright (c) 2014-2017 OpenRCT2 Developers
2016-06-25 12:04:12 +01:00
/*****************************************************************************
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
*
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* A full copy of the GNU General Public License can be found in licence.txt
*****************************************************************************/
#pragma endregion
2018-02-01 12:52:39 +00:00
#include <algorithm>
2016-06-25 12:04:12 +01:00
#include "Math.hpp"
#include "Memory.hpp"
#include "MemoryStream.h"
MemoryStream::MemoryStream(const MemoryStream &copy)
{
_access = copy._access;
_dataCapacity = copy._dataCapacity;
_dataSize = copy._dataSize;
2017-03-25 17:05:18 +00:00
if (_access & MEMORY_ACCESS::OWNER)
2016-06-25 12:04:12 +01:00
{
_data = Memory::Duplicate(copy._data, _dataCapacity);
_position = (void*)((uintptr_t)_data + copy.GetPosition());
}
}
MemoryStream::MemoryStream(size_t capacity)
{
_dataCapacity = capacity;
_data = Memory::Allocate<void>(capacity);
_position = _data;
}
2017-02-05 02:40:16 +00:00
MemoryStream::MemoryStream(void * data, size_t dataSize, uint8 access)
2016-06-25 12:04:12 +01:00
{
_access = access;
_dataCapacity = dataSize;
_dataSize = dataSize;
_data = data;
_position = _data;
}
MemoryStream::MemoryStream(const void * data, size_t dataSize)
2017-02-05 02:40:16 +00:00
: MemoryStream((void *)data, dataSize, MEMORY_ACCESS::READ)
2016-06-25 12:04:12 +01:00
{
}
MemoryStream::~MemoryStream()
{
2017-02-05 02:40:16 +00:00
if (_access & MEMORY_ACCESS::OWNER)
2016-06-25 12:04:12 +01:00
{
Memory::Free(_data);
}
_dataCapacity = 0;
_dataSize = 0;
_data = nullptr;
}
2017-02-08 18:16:33 +00:00
const void * MemoryStream::GetData() const
{
return _data;
}
void * MemoryStream::GetDataCopy() const
2016-06-25 12:04:12 +01:00
{
return Memory::Duplicate(_data, _dataSize);
}
void * MemoryStream::TakeData()
{
2017-02-05 02:40:16 +00:00
_access &= ~MEMORY_ACCESS::OWNER;
2016-06-25 12:04:12 +01:00
return _data;
}
bool MemoryStream::CanRead() const
{
2017-02-05 02:40:16 +00:00
return (_access & MEMORY_ACCESS::READ) != 0;
2016-06-25 12:04:12 +01:00
}
bool MemoryStream::CanWrite() const
{
2017-02-05 02:40:16 +00:00
return (_access & MEMORY_ACCESS::WRITE) != 0;
2016-06-25 12:04:12 +01:00
}
uint64 MemoryStream::GetLength() const
{
return _dataSize;
}
uint64 MemoryStream::GetPosition() const
{
return (uint64)((uintptr_t)_position - (uintptr_t)_data);
}
void MemoryStream::SetPosition(uint64 position)
{
Seek(position, STREAM_SEEK_BEGIN);
}
2017-01-04 22:17:08 +01:00
void MemoryStream::Seek(sint64 offset, sint32 origin)
2016-06-25 12:04:12 +01:00
{
uint64 newPosition;
switch (origin) {
default:
case STREAM_SEEK_BEGIN:
newPosition = offset;
break;
case STREAM_SEEK_CURRENT:
newPosition = GetPosition() + offset;
break;
case STREAM_SEEK_END:
newPosition = _dataSize + offset;
break;
}
if (newPosition > _dataSize)
{
throw IOException("New position out of bounds.");
}
_position = (void*)((uintptr_t)_data + (uintptr_t)newPosition);
}
void MemoryStream::Read(void * buffer, uint64 length)
{
uint64 position = GetPosition();
if (position + length > _dataSize)
{
throw IOException("Attempted to read past end of stream.");
}
2018-02-01 12:52:39 +00:00
std::copy_n((const uint8 *)_position, length, (uint8 *)buffer);
2016-06-25 12:04:12 +01:00
_position = (void*)((uintptr_t)_position + length);
}
2016-06-28 21:16:09 +01:00
uint64 MemoryStream::TryRead(void * buffer, uint64 length)
{
uint64 remainingBytes = GetLength() - GetPosition();
uint64 bytesToRead = Math::Min(length, remainingBytes);
Read(buffer, bytesToRead);
return bytesToRead;
}
2016-06-25 12:04:12 +01:00
void MemoryStream::Write(const void * buffer, uint64 length)
{
uint64 position = GetPosition();
uint64 nextPosition = position + length;
if (nextPosition > _dataCapacity)
{
2017-02-05 02:40:16 +00:00
if (_access & MEMORY_ACCESS::OWNER)
2016-06-25 12:04:12 +01:00
{
EnsureCapacity((size_t)nextPosition);
}
else
{
throw IOException("Attempted to write past end of stream.");
}
}
2018-02-01 12:52:39 +00:00
std::copy_n((const uint8 *)buffer, length, (uint8 *)_position);
2016-06-25 12:04:12 +01:00
_position = (void*)((uintptr_t)_position + length);
_dataSize = Math::Max<size_t>(_dataSize, (size_t)nextPosition);
}
void MemoryStream::EnsureCapacity(size_t capacity)
{
if (_dataCapacity < capacity)
{
size_t newCapacity = Math::Max<size_t>(8, _dataCapacity);
while (newCapacity < capacity)
{
newCapacity *= 2;
}
2017-02-08 21:53:37 +00:00
uint64 position = GetPosition();
2016-06-25 12:04:12 +01:00
_dataCapacity = newCapacity;
2017-02-08 21:53:37 +00:00
_data = Memory::Reallocate(_data, _dataCapacity);
_position = (void *)((uintptr_t)_data + (uintptr_t)position);
2016-06-25 12:04:12 +01:00
}
}