From 9daafc224a211ffd37e720002031b8569168bd9c Mon Sep 17 00:00:00 2001 From: honkstar1 Date: Mon, 21 Jun 2021 16:23:00 -0700 Subject: [PATCH] * Added min and max functions after not being able to figure out how to include things to work on all visual studio versions *sigh* just not worth spending more time on things --- include/EACopyShared.h | 2 ++ source/EACopy.cpp | 4 ++-- source/EACopyNetwork.cpp | 16 ++++++++-------- source/EACopyShared.cpp | 6 +++--- test/EACopyTest.cpp | 4 ++-- 5 files changed, 17 insertions(+), 15 deletions(-) diff --git a/include/EACopyShared.h b/include/EACopyShared.h index 01d4fad..1cd9f3b 100644 --- a/include/EACopyShared.h +++ b/include/EACopyShared.h @@ -172,6 +172,8 @@ int stringEquals(const char* a, const char* b); bool stringCopy(wchar_t* dest, uint destCapacity, const wchar_t* source); #define eacopy_sizeof_array(array) int(sizeof(array)/sizeof(array[0])) WString getVersionString(uint major, uint minor, bool isDebug); +template T min(T a, T b) { return a < b ? a : b; } +template T max(T a, T b) { return a > b ? a : b; } struct TimerScope { diff --git a/source/EACopy.cpp b/source/EACopy.cpp index 678dd31..152d2ba 100644 --- a/source/EACopy.cpp +++ b/source/EACopy.cpp @@ -184,7 +184,7 @@ bool readSettings(Settings& outSettings, int argc, wchar_t* argv[]) { outSettings.threadCount = 7; if (arg[3] == ':') - outSettings.threadCount = std::max(0, wtoi(arg + 4) - 1); + outSettings.threadCount = max(0, wtoi(arg + 4) - 1); } else if (equalsIgnoreCase(arg, L"/NOSERVER")) { @@ -528,7 +528,7 @@ int main(int argc, char* argv_[]) if (stats.destServerUsed || stats.sourceServerUsed) logInfoLinef(L" Server found and used! (%ls)", stats.info.c_str()); else if (stats.serverAttempt && !stats.destServerUsed) - logInfoLinef(L" Server not found (Spent ~%ls trying to connect. Use /NOSERVER to disable attempt)", toHourMinSec(stats.connectTime/std::max(1, (int)settings.threadCount)).c_str()); + logInfoLinef(L" Server not found (Spent ~%ls trying to connect. Use /NOSERVER to disable attempt)", toHourMinSec(stats.connectTime/max(1, (int)settings.threadCount)).c_str()); else logInfoLinef(L" No server used!"); } diff --git a/source/EACopyNetwork.cpp b/source/EACopyNetwork.cpp index 1ea9782..6c50382 100644 --- a/source/EACopyNetwork.cpp +++ b/source/EACopyNetwork.cpp @@ -299,7 +299,7 @@ bool sendFile(Socket& socket, const wchar_t* src, size_t fileSize, WriteFileType while (pos != fileSize) { u64 left = fileSize - pos; - uint toWrite = (uint)std::min(left, u64(INT_MAX-1)); + uint toWrite = (uint)min(left, u64(INT_MAX-1)); u64 startSendTime = getTime(); if (!TransmitFile(socket.socket, sourceFile, toWrite, 0, &overlapped, NULL, TF_USE_KERNEL_APC)) @@ -337,7 +337,7 @@ bool sendFile(Socket& socket, const wchar_t* src, size_t fileSize, WriteFileType u64 left = fileSize; while (left) { - uint toRead = (uint)std::min(left, u64(NetworkTransferChunkSize)); + uint toRead = (uint)min(left, u64(NetworkTransferChunkSize)); uint toReadAligned = useBufferedIO ? toRead : (((toRead + 4095) / 4096) * 4096); u64 read; @@ -370,7 +370,7 @@ bool sendFile(Socket& socket, const wchar_t* src, size_t fileSize, WriteFileType // Make sure the amount of data we've read fit in the destination compressed buffer static_assert(ZSTD_COMPRESSBOUND(CompressedNetworkTransferChunkSize - CompressBoundReservation) <= CompressedNetworkTransferChunkSize - 4, ""); - uint toRead = std::min(left, u64(CompressedNetworkTransferChunkSize - CompressBoundReservation)); + uint toRead = min(left, u64(CompressedNetworkTransferChunkSize - CompressBoundReservation)); uint toReadAligned = useBufferedIO ? toRead : (((toRead + 4095) / 4096) * 4096); u64 read; if (!readFile(src, sourceFile, copyContext.buffers[0], toReadAligned, read, ioStats)) @@ -428,9 +428,9 @@ bool sendFile(Socket& socket, const wchar_t* src, size_t fileSize, WriteFileType u64 timeUnitsPerBytes = (cs.currentSendTime * 1000000) / cs.currentSendBytes; if (timeUnitsPerBytes < cs.lastTimeUnitPerBytes) - cs.currentLevel = std::min(14, cs.currentLevel + 1); + cs.currentLevel = min(14, cs.currentLevel + 1); else - cs.currentLevel = std::max(1, cs.currentLevel - 1); + cs.currentLevel = max(1, cs.currentLevel - 1); cs.lastTimeUnitPerBytes = timeUnitsPerBytes; } } @@ -475,7 +475,7 @@ bool receiveFile(bool& outSuccess, Socket& socket, const wchar_t* fullPath, size // Copy the stuff already in the buffer if (recvPos > commandSize) { - u64 toCopy = std::min(u64(recvPos - commandSize), u64(fileSize)); + u64 toCopy = min(u64(recvPos - commandSize), u64(fileSize)); outSuccess = outSuccess & writeFile(fullPath, file, recvBuffer + commandSize, toCopy, ioStats, &osWrite); read = toCopy; commandSize += (uint)toCopy; @@ -487,7 +487,7 @@ bool receiveFile(bool& outSuccess, Socket& socket, const wchar_t* fullPath, size { u64 startRecvTime = getTime(); u64 left = fileSize - read; - uint toRead = (uint)std::min(left, u64(NetworkTransferChunkSize)); + uint toRead = (uint)min(left, u64(NetworkTransferChunkSize)); WSABUF wsabuf; wsabuf.len = toRead; wsabuf.buf = (char*)copyContext.buffers[fileBufIndex]; @@ -539,7 +539,7 @@ bool receiveFile(bool& outSuccess, Socket& socket, const wchar_t* fullPath, size // Copy the stuff already in the buffer if (recvPos > commandSize) { - u64 toCopy = std::min(u64(recvPos - commandSize), u64(fileSize)); + u64 toCopy = min(u64(recvPos - commandSize), u64(fileSize)); outSuccess = outSuccess & writeFile(fullPath, file, recvBuffer + commandSize, toCopy, ioStats, &osWrite); read = toCopy; commandSize += (uint)toCopy; diff --git a/source/EACopyShared.cpp b/source/EACopyShared.cpp index 66cc704..b833b02 100644 --- a/source/EACopyShared.cpp +++ b/source/EACopyShared.cpp @@ -1425,7 +1425,7 @@ bool writeFile(const wchar_t* fullPath, FileHandle& file, const void* data, u64 while (left) { uint written; - uint toWrite = (uint)std::min(left, u64(INT_MAX-1)); + uint toWrite = (uint)min(left, u64(INT_MAX-1)); if (!WriteFile(file, data, toWrite, &written, nullptr)) { logErrorf(L"Trying to write data to %ls: %ls", fullPath, getErrorText(fullPath, GetLastError()).c_str()); @@ -1443,7 +1443,7 @@ bool writeFile(const wchar_t* fullPath, FileHandle& file, const void* data, u64 u64 left = dataSize; while (left) { - size_t toWrite = (uint)std::min(left, u64(INT_MAX-1)); + size_t toWrite = (uint)min(left, u64(INT_MAX-1)); size_t written = write(fileHandle, data, toWrite); if (written == -1) { @@ -1774,7 +1774,7 @@ bool copyFile(const wchar_t* source, const FileInfo& sourceInfo, const wchar_t* ++ioStats.readCount; TimerScope _(ioStats.readTime); - uint toRead = (uint)std::min(left, u64(ReadChunkSize)); + uint toRead = (uint)min(left, u64(ReadChunkSize)); activeBufferIndex = (activeBufferIndex + 1) % 3; uint toReadAligned = nobufferingFlag ? (((toRead + 4095) / 4096) * 4096) : toRead; diff --git a/test/EACopyTest.cpp b/test/EACopyTest.cpp index feeeb5f..b2728c5 100644 --- a/test/EACopyTest.cpp +++ b/test/EACopyTest.cpp @@ -233,7 +233,7 @@ struct TestBase WString fullFilename = dir + name; const wchar_t* fullFileName = convertToShortPath(fullFilename.c_str(), tempBuffer); - u64 dataSize = std::min(size, 1024*1024*256ull); + u64 dataSize = min(size, 1024*1024*256ull); char* data = new char[size]; for (u64 i=0; i!=dataSize; ++i) data[i] = 'a' + i % 26; @@ -323,7 +323,7 @@ struct TestBase { char buffer[128]; memset(buffer, 0, sizeof(buffer)); - uint writePos = std::max((uint)0, (uint)(((uint(rand()) << 16) + uint(rand())) % (fileSize - 128))); + uint writePos = max((uint)0, (uint)(((uint(rand()) << 16) + uint(rand())) % (fileSize - 128))); memcpy(data + writePos, buffer, sizeof(buffer)); }