Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
deathkiller committed Apr 25, 2024
1 parent 74718d4 commit 3e9f91b
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 74 deletions.
5 changes: 2 additions & 3 deletions Sources/Shared/Base/Unaligned.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ namespace Death {
class Unaligned
{
public:
Unaligned() = delete;

static std::uint16_t Load16(const void* p);
static std::uint32_t Load32(const void* p);
static std::uint64_t Load64(const void* p);

static void Store16(void* p, std::uint16_t v);
static void Store32(void* p, std::uint32_t v);
static void Store64(void* p, std::uint64_t v);

private:
Unaligned() = delete;
};

DEATH_ALWAYS_INLINE std::uint16_t Unaligned::Load16(const void* p)
Expand Down
4 changes: 2 additions & 2 deletions Sources/Shared/IO/AndroidAssetStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Death { namespace IO {
#endif
{
_path = path;
Open(mode);
OpenInternal(mode);
}

AndroidAssetStream::~AndroidAssetStream()
Expand Down Expand Up @@ -242,7 +242,7 @@ namespace Death { namespace IO {
return AAssetDir_getNextFileName(assetDir);
}

void AndroidAssetStream::Open(FileAccessMode mode)
void AndroidAssetStream::OpenInternal(FileAccessMode mode)
{
FileAccessMode maskedMode = mode & ~FileAccessMode::Exclusive;
if (maskedMode != FileAccessMode::Read) {
Expand Down
5 changes: 4 additions & 1 deletion Sources/Shared/IO/AndroidAssetStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ namespace Death { namespace IO {
AndroidAssetStream(const Containers::String& path, FileAccessMode mode);
~AndroidAssetStream() override;

AndroidAssetStream(const AndroidAssetStream&) = delete;
AndroidAssetStream& operator=(const AndroidAssetStream&) = delete;

void Close() override;
std::int64_t Seek(std::int64_t offset, SeekOrigin origin) override;
std::int64_t GetPosition() const override;
Expand Down Expand Up @@ -87,7 +90,7 @@ namespace Death { namespace IO {
#endif
bool _shouldCloseOnDestruction;

void Open(FileAccessMode mode);
void OpenInternal(FileAccessMode mode);
};
}}

Expand Down
46 changes: 40 additions & 6 deletions Sources/Shared/IO/DeflateStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,42 @@ namespace Death { namespace IO {
}

DeflateStream::~DeflateStream()
{
DeflateStream::Close();
}

DeflateStream::DeflateStream(DeflateStream&& other) noexcept
{
_inputStream = other._inputStream;
_strm = other._strm;
_inputSize = other._inputSize;
_state = other._state;
_rawInflate = other._rawInflate;
std::memcpy(_buffer, other._buffer, sizeof(_buffer));

// Original instance will be disabled
if (other._state == State::Created || other._state == State::Initialized) {
other._state = State::Failed;
}
}

DeflateStream& DeflateStream::operator=(DeflateStream&& other) noexcept
{
Close();

_inputStream = other._inputStream;
_strm = other._strm;
_inputSize = other._inputSize;
_state = other._state;
_rawInflate = other._rawInflate;
std::memcpy(_buffer, other._buffer, sizeof(_buffer));

// Original instance will be disabled
if (other._state == State::Created || other._state == State::Initialized) {
other._state = State::Failed;
}

return *this;
}

std::int64_t DeflateStream::Seek(std::int64_t offset, SeekOrigin origin)
Expand Down Expand Up @@ -67,7 +101,7 @@ namespace Death { namespace IO {

std::int32_t DeflateStream::Read(void* buffer, std::int32_t bytes)
{
std::uint8_t* typedBuffer = (std::uint8_t*)buffer;
std::uint8_t* typedBuffer = static_cast<std::uint8_t*>(buffer);
std::int32_t bytesRead = ReadInternal(typedBuffer, bytes);
std::int32_t bytesReadTotal = bytesRead;
while (bytesRead > 0 && bytesRead < bytes) {
Expand Down Expand Up @@ -163,7 +197,7 @@ namespace Death { namespace IO {
_strm.avail_in = static_cast<std::uint32_t>(bytesRead);
}

_strm.next_out = (unsigned char*)ptr;
_strm.next_out = static_cast<unsigned char*>(ptr);
_strm.avail_out = size;

std::int32_t res = inflate(&_strm, Z_SYNC_FLUSH);
Expand All @@ -188,7 +222,7 @@ namespace Death { namespace IO {
return true;
}

_inputStream->Seek(_inputSize >= 0 ? _inputSize : -std::int32_t(_strm.avail_in), SeekOrigin::Current);
_inputStream->Seek(_inputSize >= 0 ? _inputSize : -static_cast<std::int32_t>(_strm.avail_in), SeekOrigin::Current);

std::int32_t error = inflateEnd((z_stream*)&_strm);
if (error != Z_OK) {
Expand Down Expand Up @@ -220,7 +254,7 @@ namespace Death { namespace IO {

DeflateWriter::~DeflateWriter()
{
Close();
DeflateWriter::Close();
}

void DeflateWriter::Close()
Expand Down Expand Up @@ -280,11 +314,11 @@ namespace Death { namespace IO {
while (_strm.avail_in > 0 || finish) {
std::int32_t error;
do {
_strm.next_out = (unsigned char*)_buffer;
_strm.next_out = static_cast<unsigned char*>(_buffer);
_strm.avail_out = sizeof(_buffer);
error = deflate(&_strm, finish ? Z_FINISH : Z_NO_FLUSH);

std::int32_t bytesWritten = sizeof(_buffer) - (std::int32_t)_strm.avail_out;
std::int32_t bytesWritten = sizeof(_buffer) - static_cast<std::int32_t>(_strm.avail_out);
if (bytesWritten > 0) {
_outputStream->Write(_buffer, bytesWritten);
}
Expand Down
9 changes: 8 additions & 1 deletion Sources/Shared/IO/DeflateStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ namespace Death { namespace IO {
public:
DeflateStream();
DeflateStream(Stream& inputStream, std::int32_t inputSize = -1, bool rawInflate = true);

~DeflateStream();

DeflateStream(const DeflateStream&) = delete;
DeflateStream(DeflateStream&& other) noexcept;
DeflateStream& operator=(const DeflateStream&) = delete;
DeflateStream& operator=(DeflateStream&& other) noexcept;

void Open(Stream& inputStream, std::int32_t inputSize = -1, bool rawInflate = true);

void Close() override;
Expand Down Expand Up @@ -72,6 +76,9 @@ namespace Death { namespace IO {
DeflateWriter(Stream& outputStream, std::int32_t compressionLevel = 9, bool rawInflate = true);
~DeflateWriter();

DeflateWriter(const DeflateWriter&) = delete;
DeflateWriter& operator=(const DeflateWriter&) = delete;

void Close() override;
std::int64_t Seek(std::int64_t offset, SeekOrigin origin) override;
std::int64_t GetPosition() const override;
Expand Down
63 changes: 50 additions & 13 deletions Sources/Shared/IO/FileStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ namespace Death { namespace IO {
_handle(nullptr)
#endif
{
Open(mode);
OpenInternal(mode);
}

FileStream::~FileStream()
{
if (_shouldCloseOnDestruction) {
Close();
FileStream::Close();
}
}

Expand Down Expand Up @@ -78,12 +78,19 @@ namespace Death { namespace IO {
if (_handle != nullptr) {
// ::fseek return 0 on success
# if defined(DEATH_TARGET_WINDOWS)
# if defined(_LTL_Core_Version) && _LTL_Core_Version <= 4
// VC-LTL doesn't support _nolock functions
if (::_fseeki64(_handle, offset, static_cast<std::int32_t>(origin)) == 0) {
newPos = ::_ftelli64(_handle);
}
# else
if (::_fseeki64_nolock(_handle, offset, static_cast<std::int32_t>(origin)) == 0) {
newPos = ::_ftelli64_nolock(_handle);
}
# endif
# else
if (::fseek(_handle, offset, static_cast<std::int32_t>(origin)) == 0) {
newPos = ::ftell(_handle);
if (::fseeko(_handle, offset, static_cast<std::int32_t>(origin)) == 0) {
newPos = ::ftello(_handle);
}
# endif
else {
Expand All @@ -104,9 +111,14 @@ namespace Death { namespace IO {
#else
if (_handle != nullptr) {
# if defined(DEATH_TARGET_WINDOWS)
# if defined(_LTL_Core_Version) && _LTL_Core_Version <= 4
// VC-LTL doesn't support _nolock functions
pos = ::_ftelli64(_handle);
# else
pos = ::_ftelli64_nolock(_handle);
# endif
# else
pos = ::ftell(_handle);
pos = ::ftello(_handle);
# endif
}
#endif
Expand All @@ -124,7 +136,16 @@ namespace Death { namespace IO {
}
#else
if (_handle != nullptr) {
# if defined(DEATH_TARGET_WINDOWS)
# if defined(_LTL_Core_Version) && _LTL_Core_Version <= 4
// VC-LTL doesn't support _nolock functions
bytesRead = static_cast<std::int32_t>(::fread(buffer, 1, bytes, _handle));
# else
bytesRead = static_cast<std::int32_t>(::_fread_nolock(buffer, 1, bytes, _handle));
# endif
# else
bytesRead = static_cast<std::int32_t>(::fread_unlocked(buffer, 1, bytes, _handle));
# endif
}
#endif
return bytesRead;
Expand All @@ -141,7 +162,16 @@ namespace Death { namespace IO {
}
#else
if (_handle != nullptr) {
# if defined(DEATH_TARGET_WINDOWS)
# if defined(_LTL_Core_Version) && _LTL_Core_Version <= 4
// VC-LTL doesn't support _nolock functions
bytesWritten = static_cast<std::int32_t>(::fwrite(buffer, 1, bytes, _handle));
# else
bytesWritten = static_cast<std::int32_t>(::_fwrite_nolock(buffer, 1, bytes, _handle));
# endif
# else
bytesWritten = static_cast<std::int32_t>(::fwrite_unlocked(buffer, 1, bytes, _handle));
# endif
}
#endif
return bytesWritten;
Expand All @@ -161,7 +191,7 @@ namespace Death { namespace IO {
return _path;
}

void FileStream::Open(FileAccessMode mode)
void FileStream::OpenInternal(FileAccessMode mode)
{
#if defined(DEATH_USE_FILE_DESCRIPTORS)
std::int32_t openFlag;
Expand Down Expand Up @@ -193,8 +223,8 @@ namespace Death { namespace IO {
}

// Try to get file size
_size = ::lseek(_fileDescriptor, 0L, SEEK_END);
::lseek(_fileDescriptor, 0L, SEEK_SET);
_size = ::lseek(_fileDescriptor, 0, SEEK_END);
::lseek(_fileDescriptor, 0, SEEK_SET);
#else
# if defined(DEATH_TARGET_WINDOWS_RT)
DWORD desireAccess, creationDisposition;
Expand Down Expand Up @@ -285,13 +315,20 @@ namespace Death { namespace IO {

// Try to get file size
# if defined(DEATH_TARGET_WINDOWS)
::_fseeki64(_handle, 0L, SEEK_END);
# if defined(_LTL_Core_Version) && _LTL_Core_Version <= 4
// VC-LTL doesn't support _nolock functions
::_fseeki64(_handle, 0, SEEK_END);
_size = ::_ftelli64(_handle);
::_fseeki64(_handle, 0L, SEEK_SET);
::_fseeki64(_handle, 0, SEEK_SET);
# else
::_fseeki64_nolock(_handle, 0, SEEK_END);
_size = ::_ftelli64_nolock(_handle);
::_fseeki64_nolock(_handle, 0, SEEK_SET);
# endif
# else
::fseek(_handle, 0L, SEEK_END);
_size = ::ftell(_handle);
::fseek(_handle, 0L, SEEK_SET);
::fseeko(_handle, 0, SEEK_END);
_size = ::ftello(_handle);
::fseeko(_handle, 0, SEEK_SET);
# endif
#endif
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/Shared/IO/FileStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ namespace Death { namespace IO {
FileStream(Containers::String&& path, FileAccessMode mode);
~FileStream() override;

FileStream(const FileStream&) = delete;
FileStream& operator=(const FileStream&) = delete;

void Close() override;
std::int64_t Seek(std::int64_t offset, SeekOrigin origin) override;
std::int64_t GetPosition() const override;
Expand Down Expand Up @@ -44,9 +47,6 @@ namespace Death { namespace IO {
#endif

private:
FileStream(const FileStream&) = delete;
FileStream& operator=(const FileStream&) = delete;

Containers::String _path;
#if defined(DEATH_USE_FILE_DESCRIPTORS)
std::int32_t _fileDescriptor;
Expand All @@ -55,6 +55,6 @@ namespace Death { namespace IO {
#endif
bool _shouldCloseOnDestruction;

void Open(FileAccessMode mode);
void OpenInternal(FileAccessMode mode);
};
}}
15 changes: 9 additions & 6 deletions Sources/Shared/IO/FileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ namespace Death { namespace IO {
# if defined(DEATH_TARGET_UNIX)
static bool RedirectFileDescriptorToNull(std::int32_t fd)
{
if (fd == -1) {
if (fd < 0) {
return false;
}

Expand Down Expand Up @@ -411,6 +411,9 @@ namespace Death { namespace IO {
Close();
}

Impl(const Impl&) = delete;
Impl& operator=(const Impl&) = delete;

bool Open(const StringView path, EnumerationOptions options)
{
Close();
Expand Down Expand Up @@ -605,8 +608,6 @@ namespace Death { namespace IO {
}

private:
Impl(const Impl&) = delete;
Impl& operator=(const Impl&) = delete;

EnumerationOptions _options;
char _path[MaxPathLength];
Expand Down Expand Up @@ -1891,7 +1892,9 @@ namespace Death { namespace IO {
}

struct stat sb;
::fstat(sourceFd, &sb);
if (::fstat(sourceFd, &sb) != 0) {
return false;
}

mode_t sourceMode = sb.st_mode;
mode_t destMode = sourceMode;
Expand Down Expand Up @@ -1934,7 +1937,7 @@ namespace Death { namespace IO {
if (bytesLeft < static_cast<std::uint64_t>(MaxSendSize)) {
bytesToCopy = static_cast<std::size_t>(bytesLeft);
}
ssize_t sz = ::sendfile(destFd, sourceFd, NULL, bytesToCopy);
ssize_t sz = ::sendfile(destFd, sourceFd, nullptr, bytesToCopy);
if DEATH_UNLIKELY(sz < 0) {
std::int32_t err = errno;
if (errno == EINTR) {
Expand Down Expand Up @@ -2413,7 +2416,7 @@ namespace Death { namespace IO {
return { };
}

const int fd = ::open(String::nullTerminatedView(path).data(), flags);
const std::int32_t fd = ::open(String::nullTerminatedView(path).data(), flags);
if (fd == -1) {
LOGE("Cannot open file \"%s\"", String::nullTerminatedView(path).data());
return { };
Expand Down
Loading

0 comments on commit 3e9f91b

Please sign in to comment.