From 46e0a5a0c6a546ca15228d3cc98969cca0fe3a50 Mon Sep 17 00:00:00 2001 From: superp00t Date: Thu, 27 Mar 2025 15:24:19 -0400 Subject: [PATCH] feat(memory): add helper macros to simplify allocating/deallocating objects SMemAlloc and SMemFree can now be used indirectly by using the 'new' and 'delete' operators. This also means that any project linked against bc will import these operators. The following helper macros are now available for use by including 'bc/Memory.hpp' ALLOC(n) allocate n random bytes of memory ALLOC_ZERO(n) allocate n zeroed bytes of memory FREE(ptr) deallocate memory at ptr NEW(T) allocate an object of type T and call the object's constructor NEW_ZERO(T) allocate an object of type T with zeroed bytes and call the object's constructor DEL(ptr) call the object at ptr's destructor, and deallocate its memory --- bc/Memory.cpp | 18 ----- bc/Memory.hpp | 14 +--- bc/memory/Memory.cpp | 24 ++++++ bc/memory/Memory.hpp | 19 +++++ bc/memory/Storm.cpp | 14 ++++ bc/memory/Storm.hpp | 39 ++++++++++ bc/system/file/posix/Stacked.cpp | 45 ++++++------ bc/system/file/win/Stacked.cpp | 122 ++++++++++++++----------------- 8 files changed, 173 insertions(+), 122 deletions(-) delete mode 100644 bc/Memory.cpp create mode 100644 bc/memory/Memory.cpp create mode 100644 bc/memory/Memory.hpp diff --git a/bc/Memory.cpp b/bc/Memory.cpp deleted file mode 100644 index 3e2e416..0000000 --- a/bc/Memory.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include "bc/Memory.hpp" -#include "bc/memory/Storm.hpp" - -void* Blizzard::Memory::Allocate(uint32_t bytes) { - return SMemAlloc(bytes, __FILE__, __LINE__, 0x0); -} - -void* Blizzard::Memory::Allocate(uint32_t bytes, uint32_t flags, const char* filename, uint32_t linenumber, const char* a5) { - // TODO - // - determine purpose of a5 - // - flags manipulation - - return SMemAlloc(bytes, filename, linenumber, flags); -} - -void Blizzard::Memory::Free(void* ptr) { - SMemFree(ptr); -} diff --git a/bc/Memory.hpp b/bc/Memory.hpp index 923d5bd..80f5894 100644 --- a/bc/Memory.hpp +++ b/bc/Memory.hpp @@ -1,17 +1,7 @@ #ifndef BC_MEMORY_HPP #define BC_MEMORY_HPP -#include - -namespace Blizzard { -namespace Memory { - -// Functions -void* Allocate(uint32_t bytes); -void* Allocate(uint32_t bytes, uint32_t flags, const char* filename, uint32_t linenumber, const char* a5); -void Free(void* ptr); - -} // namespace Memory -} // namespace Blizzard +#include "bc/memory/Memory.hpp" +#include "bc/memory/Storm.hpp" #endif diff --git a/bc/memory/Memory.cpp b/bc/memory/Memory.cpp new file mode 100644 index 0000000..663ea97 --- /dev/null +++ b/bc/memory/Memory.cpp @@ -0,0 +1,24 @@ +#include "bc/memory/Memory.hpp" +#include "bc/memory/Storm.hpp" + +namespace Blizzard { +namespace Memory { + +void* Allocate(uint32_t bytes) { + return SMemAlloc(bytes, __FILE__, __LINE__, 0x0); +} + +void* Allocate(uint32_t bytes, uint32_t flags, const char* filename, uint32_t linenumber, const char* a5) { + // TODO + // - determine purpose of a5 + // - flags manipulation + + return SMemAlloc(bytes, filename, linenumber, flags); +} + +void Free(void* ptr) { + SMemFree(ptr); +} + +} // namespace Memory +} // namespace Blizzard diff --git a/bc/memory/Memory.hpp b/bc/memory/Memory.hpp new file mode 100644 index 0000000..7761d1d --- /dev/null +++ b/bc/memory/Memory.hpp @@ -0,0 +1,19 @@ +#ifndef BC_MEMORY_MEMORY_HPP +#define BC_MEMORY_MEMORY_HPP + +#include + +namespace Blizzard { +namespace Memory { + +// Functions +void* Allocate(uint32_t bytes); + +void* Allocate(uint32_t bytes, uint32_t flags, const char* filename, uint32_t linenumber, const char* a5); + +void Free(void* ptr); + +} // namespace Memory +} // namespace Blizzard + +#endif diff --git a/bc/memory/Storm.cpp b/bc/memory/Storm.cpp index a6e318b..53bb31c 100644 --- a/bc/memory/Storm.cpp +++ b/bc/memory/Storm.cpp @@ -2,6 +2,20 @@ constexpr size_t ALIGNMENT = 8; +void* operator new(size_t bytes) { + return SMemAlloc(bytes, "new", -1, 0x0); +} + +void* operator new[](size_t bytes) { + return SMemAlloc(bytes, "new", -1, 0x0); +} + +void operator delete(void* ptr) { + if (ptr) { + SMemFree(ptr, "delete", -1, 0x0); + } +} + void* SMemAlloc(size_t bytes, const char* filename, int32_t linenumber, uint32_t flags) { size_t alignedBytes = (bytes + (ALIGNMENT - 1)) & ~(ALIGNMENT - 1); diff --git a/bc/memory/Storm.hpp b/bc/memory/Storm.hpp index ceb3473..4919aa5 100644 --- a/bc/memory/Storm.hpp +++ b/bc/memory/Storm.hpp @@ -3,6 +3,25 @@ #include #include +#include +#include + +#define ALLOC(n) SMemAlloc(static_cast(n), __FILE__, __LINE__, 0x0) +#define ALLOC_ZERO(n) SMemAlloc(static_cast(n), __FILE__, __LINE__, 0x8) +#define FREE(ptr) SMemFree(ptr, __FILE__, __LINE__, 0x0) + +#define NEW(T, ...) (new (SMemAlloc(sizeof(T), __FILE__, __LINE__, 0)) T(__VA_ARGS__)) +#define NEW_ZERO(T, ...) (new (SMemAlloc(sizeof(T), __FILE__, __LINE__, 0x8) T(__VA_ARGS__)) +#define DEL(ptr) \ + do { \ + if (ptr) { \ + using __storm_object = std::remove_pointer::type>::type; \ + (ptr)->~__storm_object(); \ + ::operator delete(ptr, __FILE__, __LINE__); \ + } \ + } while (0) + +// allocation functions void* SMemAlloc(size_t bytes, const char* filename, int32_t linenumber, uint32_t flags); @@ -12,4 +31,24 @@ void SMemFree(void* ptr, const char* filename, int32_t linenumber, uint32_t flag void* SMemReAlloc(void* ptr, size_t bytes, const char* filename, int32_t linenumber, uint32_t flags); +// global operators + +void* operator new(size_t bytes); + +void* operator new[](size_t bytes); + +void operator delete(void* ptr); + +inline void operator delete(void* ptr, const char* filename, int32_t linenumber) { + if (ptr) { + SMemFree(ptr, "delete", -1, 0); + } +} + +inline void operator delete[](void* ptr, const char* filename, int32_t linenumber) { + if (ptr) { + SMemFree(ptr, "delete[]", -1, 0); + } +} + #endif diff --git a/bc/system/file/posix/Stacked.cpp b/bc/system/file/posix/Stacked.cpp index 9cd9870..fad966a 100644 --- a/bc/system/file/posix/Stacked.cpp +++ b/bc/system/file/posix/Stacked.cpp @@ -2,13 +2,12 @@ #include "bc/file/Defines.hpp" #if defined(WHOA_SYSTEM_LINUX) || defined(WHOA_SYSTEM_MAC) -#include "bc/system/file/System_File.hpp" -#include "bc/system/file/Stacked.hpp" #include "bc/Debug.hpp" #include "bc/File.hpp" #include "bc/Memory.hpp" -#include "bc/memory/Storm.hpp" #include "bc/String.hpp" +#include "bc/system/file/Stacked.hpp" +#include "bc/system/file/System_File.hpp" #include "bc/system/file/posix/Support.hpp" #include #include @@ -25,8 +24,8 @@ #endif #if defined(WHOA_SYSTEM_MAC) -#include #include +#include #endif #include @@ -55,7 +54,7 @@ bool Close(FileParms* parms) { BLIZZARD_ASSERT(file != nullptr); ::close(file->filefd); - SMemFree(file); + FREE(file); return true; } @@ -327,7 +326,7 @@ bool IsReadOnly(FileParms* parms) { bool MakeAbsolutePath(FileParms* parms) { char namebuffer[BC_FILE_MAX_PATH]; auto name = parms->buffersize > BC_FILE_MAX_PATH - ? static_cast(Blizzard::Memory::Allocate(parms->buffersize)) + ? static_cast(ALLOC(parms->buffersize)) : namebuffer; *name = '\0'; @@ -338,7 +337,7 @@ bool MakeAbsolutePath(FileParms* parms) { Blizzard::String::Append(name, parms->name, parms->buffersize); char resultbuffer[BC_FILE_MAX_PATH]; auto result = parms->buffersize > BC_FILE_MAX_PATH - ? static_cast(Blizzard::Memory::Allocate(parms->buffersize)) + ? static_cast(ALLOC(parms->buffersize)) : resultbuffer; Blizzard::String::MakeUnivPath(name, result, parms->buffersize); @@ -391,7 +390,7 @@ bool MakeAbsolutePath(FileParms* parms) { char canonicalbuffer[BC_FILE_MAX_PATH]; char resolvedbuffer[BC_FILE_MAX_PATH]; auto canonical = parms->buffersize > BC_FILE_MAX_PATH - ? static_cast(Blizzard::Memory::Allocate(parms->buffersize)) + ? static_cast(ALLOC(parms->buffersize)) : canonicalbuffer; auto cur = canonical; @@ -405,7 +404,7 @@ bool MakeAbsolutePath(FileParms* parms) { auto v15 = (src + 1) - begin; Blizzard::String::Copy(cur, begin, v15 + 1); - auto resolved = parms->buffersize > BC_FILE_MAX_PATH ? static_cast(Blizzard::Memory::Allocate(parms->buffersize)) : resolvedbuffer; + auto resolved = parms->buffersize > BC_FILE_MAX_PATH ? static_cast(ALLOC(parms->buffersize)) : resolvedbuffer; if (::realpath(canonical, resolved)) { Blizzard::String::Copy(canonical, resolved, parms->buffersize); if (src[0] == '/' || src[0] == '\0' && src[-1] == '/') { @@ -536,9 +535,8 @@ bool Copy(FileParms* parms) { return false; } - auto size = Blizzard::File::GetFileInfo(src)->size; - auto copybuffersize = size > BC_SYSTEM_FILE_COPYBUFFER_SIZE ? BC_SYSTEM_FILE_COPYBUFFER_SIZE : size; - auto copybuffer = Blizzard::Memory::Allocate(copybuffersize); + auto size = Blizzard::File::GetFileInfo(src)->size; + auto buffer = ALLOC(size > BC_SYSTEM_FILE_COPYBUFFER_SIZE ? BC_SYSTEM_FILE_COPYBUFFER_SIZE : size); int64_t offset = 0; @@ -546,12 +544,12 @@ bool Copy(FileParms* parms) { while (offset < size) { int32_t count = size - offset >= BC_SYSTEM_FILE_COPYBUFFER_SIZE ? BC_SYSTEM_FILE_COPYBUFFER_SIZE : size - offset; - if (!Blizzard::File::Read(src, copybuffer, offset, &count)) { + if (!Blizzard::File::Read(src, buffer, offset, &count)) { result = false; break; } - if (!Blizzard::File::Write(dst, copybuffer, offset, &count)) { + if (!Blizzard::File::Write(dst, buffer, offset, &count)) { result = false; break; } @@ -559,7 +557,7 @@ bool Copy(FileParms* parms) { offset += static_cast(count); } - Blizzard::Memory::Free(copybuffer); + FREE(buffer); Blizzard::File::Close(src); Blizzard::File::Close(dst); if (result == false) { @@ -622,10 +620,10 @@ bool Open(FileParms* parms) { if (System_File::s_EnableFileLocks) { if ((parms->mode & Blizzard::File::Mode::shareread) == 0 && (parms->mode & Blizzard::File::Mode::write) != 0) { - if (!LockFileDescriptorForWrite(filefd)) { - return false; - } + if (!LockFileDescriptorForWrite(filefd)) { + return false; } + } } if (parms->mode & Blizzard::File::Mode::append) { @@ -652,12 +650,11 @@ bool Open(FileParms* parms) { auto file = parms->file; if (!file) { - auto namelength = Blizzard::String::Length(name.ToString()) + 1; - file = reinterpret_cast(SMemAlloc(sizeof(Blizzard::File::StreamRecord) + namelength, __FILE__, __LINE__, 0x8)); + auto namesize = Blizzard::String::Length(name.ToString()) + 1; + file = reinterpret_cast(ALLOC_ZERO(sizeof(Blizzard::File::StreamRecord) + namesize)); auto filename = reinterpret_cast(file) + sizeof(Blizzard::File::StreamRecord); file->name = filename; - Blizzard::String::Copy(filename, name.ToString(), namelength); - + Blizzard::String::Copy(filename, name.ToString(), namesize); } file->filefd = filefd; @@ -763,8 +760,8 @@ bool SetAttributes(FileParms* parms) { } auto mode = parms->info->attributes & BC_FILE_ATTRIBUTE_READONLY - ? info.st_mode & (S_IFSOCK | S_IFLNK | S_IFIFO | S_ISUID | S_ISGID | S_ISVTX | 0555) - : info.st_mode | 0222; + ? info.st_mode & (S_IFSOCK | S_IFLNK | S_IFIFO | S_ISUID | S_ISGID | S_ISVTX | 0555) + : info.st_mode | 0222; if (::chmod(PATH(parms->name), mode) != 0) { BC_FILE_SET_ERROR(8); diff --git a/bc/system/file/win/Stacked.cpp b/bc/system/file/win/Stacked.cpp index 0a8011b..b6b9050 100644 --- a/bc/system/file/win/Stacked.cpp +++ b/bc/system/file/win/Stacked.cpp @@ -1,22 +1,18 @@ -#include "bc/file/Defines.hpp" #if defined(WHOA_SYSTEM_WIN) -#include "bc/system/file/Stacked.hpp" -#include "bc/string/QuickFormat.hpp" -#include "bc/File.hpp" -#include "bc/Debug.hpp" #include "bc/Memory.hpp" -#include "bc/String.hpp" -#include "bc/Unicode.hpp" -#include "bc/File.hpp" -#include "bc/time/Time.hpp" -#include "bc/memory/Storm.hpp" - -#include - #include #include +#include "bc/File.hpp" +#include "bc/system/file/Stacked.hpp" +#include "bc/Debug.hpp" +#include "bc/String.hpp" +#include "bc/Unicode.hpp" +#include "bc/time/Time.hpp" + +#include + #include "bc/system/file/win/Support.hpp" #define PATH(name) Blizzard::String::QuickNativePath<300UL>(name).ToString() @@ -27,9 +23,9 @@ high = static_cast(qw >> 32); \ low = static_cast(qw) -#define BREAKFILEPOINTER(qw, high, low) \ - high = static_cast(qw >> 32); \ - low = static_cast(qw) +#define BREAKFILEPOINTER(qw, high, low) \ + high = static_cast(qw >> 32); \ + low = static_cast(qw) namespace System_File { @@ -38,7 +34,7 @@ void FromNativeName(char* buffer, int32_t buffersize, const char* name) { uint16_t widenamebuffer[300]; auto len = name ? Blizzard::String::Length(name) : 0; auto widename = len + 1 > 300 - ? static_cast(Blizzard::Memory::Allocate(2 * (len + 1))) + ? new uint16_t[2 * (len + 1)] : widenamebuffer; widename[MultiByteToWideChar(CP_ACP, 0, name, len, reinterpret_cast(widename), len + 1)] = 0; @@ -46,7 +42,7 @@ void FromNativeName(char* buffer, int32_t buffersize, const char* name) { Blizzard::Unicode::ConvertUTF16to8(reinterpret_cast(buffer), buffersize, widenamebuffer, 0xFFFFFFFF, nullptr, nullptr); if (widenamebuffer != widename) { - Blizzard::Memory::Free(widename); + delete widename; } } @@ -152,13 +148,13 @@ void GetFileInfoByFile(Blizzard::File::StreamRecord* file) { WIN32_FILE_ATTRIBUTE_DATA fileinfo; if (::GetFileAttributesEx(PATH(file->name), GetFileExInfoStandard, &fileinfo)) { - info->createtime = Blizzard::Time::FromFileTime(MakeFileTime(fileinfo.ftCreationTime)); + info->createtime = Blizzard::Time::FromFileTime(MakeFileTime(fileinfo.ftCreationTime)); info->writetime = Blizzard::Time::FromFileTime(MakeFileTime(fileinfo.ftLastWriteTime)); info->accesstime = Blizzard::Time::FromFileTime(MakeFileTime(fileinfo.ftLastAccessTime)); info->attributes = FromNativeAttributes(fileinfo.dwFileAttributes); - info->normal = info->attributes & BC_FILE_ATTRIBUTE_NORMAL != 0; + info->normal = (info->attributes & BC_FILE_ATTRIBUTE_NORMAL) != 0; if (fileinfo.dwFileAttributes == 0xFFFFFFFF) { info->filetype = 0; @@ -209,7 +205,7 @@ bool Close(FileParms* parms) { ::CloseHandle(file->filehandle); } - SMemFree(file); + FREE(file); return true; } @@ -314,7 +310,7 @@ bool GetFileInfo(FileParms* parms) { info->size = MAKEU64(fileinfo.nFileSizeHigh, fileinfo.nFileSizeLow); - info->createtime = Blizzard::Time::FromFileTime(MakeFileTime(fileinfo.ftCreationTime)); + info->createtime = Blizzard::Time::FromFileTime(MakeFileTime(fileinfo.ftCreationTime)); info->writetime = Blizzard::Time::FromFileTime(MakeFileTime(fileinfo.ftLastWriteTime)); info->accesstime = Blizzard::Time::FromFileTime(MakeFileTime(fileinfo.ftLastAccessTime)); @@ -322,7 +318,7 @@ bool GetFileInfo(FileParms* parms) { info->attributes = FromNativeAttributes(fileinfo.dwFileAttributes); - info->normal = info->attributes & BC_FILE_ATTRIBUTE_NORMAL != 0; + info->normal = (info->attributes & BC_FILE_ATTRIBUTE_NORMAL) != 0; if (fileinfo.dwFileAttributes == 0xFFFFFFFF) { info->filetype = 0; @@ -351,20 +347,15 @@ bool GetFreeSpace(FileParms* parms) { return false; } - auto pathchars = (Blizzard::String::FindFilename(name) + 1) - name; - if (pathchars > 260) { - pathchars = 260; - } - - char path[260]; - char shortpath[260]; - Blizzard::String::Copy(path, name, pathchars); + char path[PATH_MAX]; + char shortpath[PATH_MAX]; + Blizzard::String::Copy(path, name, std::min(static_cast((Blizzard::String::FindFilename(name) + 1) - name), PATH_MAX)); ULARGE_INTEGER freebytesavailable; ULARGE_INTEGER totalbytesavailable; - auto shortpathchars = ::GetShortPathName(PATH(path), shortpath, 260); - if (shortpathchars && shortpathchars < 260) { + auto shortpathchars = ::GetShortPathName(PATH(path), shortpath, PATH_MAX); + if (shortpathchars && shortpathchars < PATH_MAX) { if (!GetDiskFreeSpaceEx(shortpath, &freebytesavailable, &totalbytesavailable, nullptr)) { BC_FILE_SET_ERROR(8); return false; @@ -403,7 +394,7 @@ bool GetPos(FileParms* parms) { bool GetRootChars(FileParms* parms) { char pathbuffer[256]; auto pathsize = Blizzard::String::Length(parms->name) + 1; - auto path = pathsize > 256 ? reinterpret_cast(Blizzard::Memory::Allocate(pathsize)) : pathbuffer; + auto path = pathsize > 256 ? new char[pathsize] : pathbuffer; if (pathsize > 2) { Blizzard::String::MakeUnivPath(parms->name, path, pathsize); @@ -417,7 +408,7 @@ bool GetRootChars(FileParms* parms) { if (!cur) { parms->offset = pathsize; if (path != pathbuffer) { - Blizzard::Memory::Free(path); + delete path; } return true; } @@ -429,7 +420,7 @@ bool GetRootChars(FileParms* parms) { } if (path != pathbuffer) { - Blizzard::Memory::Free(path); + delete path; } return true; } @@ -487,9 +478,9 @@ bool CreateDirectory(FileParms* parms) { return false; } - char pathbuffer[260]; + char pathbuffer[PATH_MAX]; auto pathsize = Blizzard::String::Length(parms->name) + 1; - auto path = pathsize > 260 ? reinterpret_cast(Blizzard::Memory::Allocate(pathsize)) : pathbuffer; + auto path = pathsize > PATH_MAX ? new char[pathsize] : pathbuffer; Blizzard::String::MakeBackslashPath(parms->name, path, pathsize); @@ -502,21 +493,17 @@ bool CreateDirectory(FileParms* parms) { } if (parms->recurse) { - char leadingpath[260]; + char leadingpath[PATH_MAX]; for (auto s = path; *s && s < (s + Blizzard::String::Length(s)); s++) { while (*s && *s != '\\') { s++; } - auto leadingpathsize = (s - path) + 2; - if (leadingpathsize > 260) { - leadingpathsize = 260; - } - if (Blizzard::String::Copy(leadingpath, path, leadingpathsize)) { + if (Blizzard::String::Copy(leadingpath, path, std::min(static_cast(s - path) + 2, PATH_MAX))) { BC_FILE_SET_ERROR(8); if (path != pathbuffer) { - Blizzard::Memory::Free(path); + delete path; } return false; } @@ -532,7 +519,7 @@ bool CreateDirectory(FileParms* parms) { if ((last_err != ERROR_ALREADY_EXISTS && last_err != ERROR_ACCESS_DENIED)) { if (path != pathbuffer) { - Blizzard::Memory::Free(path); + delete path; } BC_FILE_SET_ERROR(8); return false; @@ -541,7 +528,7 @@ bool CreateDirectory(FileParms* parms) { auto directoryattributes = GetFileAttributes(directorypath.ToString()); if (directoryattributes == INVALID_FILE_ATTRIBUTES || (directoryattributes & FILE_ATTRIBUTE_DIRECTORY) == 0) { if (path != pathbuffer) { - Blizzard::Memory::Free(path); + delete path; } BC_FILE_SET_ERROR(8); return false; @@ -553,14 +540,14 @@ bool CreateDirectory(FileParms* parms) { auto last_err = ::GetLastError(); bool result = last_err == ERROR_ALREADY_EXISTS || last_err == ERROR_ACCESS_DENIED; if (path != pathbuffer) { - Blizzard::Memory::Free(path); + delete path; } return result; } } if (path != pathbuffer) { - Blizzard::Memory::Free(path); + delete path; } return true; } @@ -608,9 +595,8 @@ bool Copy(FileParms* parms) { return false; } - auto size = Blizzard::File::GetFileInfo(src)->size; - auto copybuffersize = size > BC_SYSTEM_FILE_COPYBUFFER_SIZE ? BC_SYSTEM_FILE_COPYBUFFER_SIZE : size; - auto copybuffer = Blizzard::Memory::Allocate(copybuffersize); + auto size = Blizzard::File::GetFileInfo(src)->size; + auto buffer = ALLOC(size > BC_SYSTEM_FILE_COPYBUFFER_SIZE ? BC_SYSTEM_FILE_COPYBUFFER_SIZE : size); int64_t offset = 0; @@ -618,12 +604,12 @@ bool Copy(FileParms* parms) { while (offset < size) { auto count = static_cast(size - offset >= BC_SYSTEM_FILE_COPYBUFFER_SIZE ? BC_SYSTEM_FILE_COPYBUFFER_SIZE : size - offset); - if (!Blizzard::File::Read(src, copybuffer, offset, &count)) { + if (!Blizzard::File::Read(src, buffer, offset, &count)) { result = false; break; } - if (!Blizzard::File::Write(dst, copybuffer, offset, &count)) { + if (!Blizzard::File::Write(dst, buffer, offset, &count)) { result = false; break; } @@ -631,7 +617,7 @@ bool Copy(FileParms* parms) { offset += static_cast(count); } - Blizzard::Memory::Free(copybuffer); + FREE(buffer); Blizzard::File::Close(src); Blizzard::File::Close(dst); if (!result) { @@ -675,13 +661,13 @@ bool Open(FileParms* parms) { if (file == nullptr) { // alloc(sizeof(StreamRecord) + len(name) + 1) // block of memory holds file as well as C string of the filename - auto namesize = Blizzard::String::Length(name); - file = reinterpret_cast(SMemAlloc(sizeof(Blizzard::File::StreamRecord) + 1 + namesize, __FILE__, __LINE__, 0x8)); + auto namesize = Blizzard::String::Length(name) + 1; + file = reinterpret_cast(ALLOC_ZERO(sizeof(Blizzard::File::StreamRecord) + namesize)); // set the name pointer inside of StreamRecord to point to the end of StreamRecord (i.e. the start of the name cstring in the memory block) auto filename = reinterpret_cast(file) + sizeof(Blizzard::File::StreamRecord); file->name = filename; // copy name into memory block - Blizzard::String::Copy(filename, name, namesize + 1); + Blizzard::String::Copy(filename, name, namesize); } file->filehandle = filehandle; @@ -707,15 +693,15 @@ bool RemoveDirectory(FileParms* parms) { return false; } - char namebuffer[260]; - auto namesize = Blizzard::String::Length(parms->name); - auto name = namesize > 260 ? reinterpret_cast(Blizzard::Memory::Allocate(namesize)) : namebuffer; - Blizzard::String::MakeBackslashPath(parms->name, name, 260); + char namebuffer[PATH_MAX]; + auto namesize = Blizzard::String::Length(parms->name) + 1; + auto name = namesize > PATH_MAX ? new char[namesize] : namebuffer; + Blizzard::String::MakeBackslashPath(parms->name, name, PATH_MAX); auto removed = ::RemoveDirectoryA(PATH(name)); if (name != namebuffer) { - Blizzard::Memory::Free(name); + delete name; } return removed != 0; @@ -762,6 +748,8 @@ bool SetEOF(FileParms* parms) { } bool SetAttributes(FileParms* parms) { + auto file = parms->file; + if (parms->setinfo & BC_SYSTEM_FILE_INFO_TIMES) { auto info = parms->info; @@ -769,7 +757,7 @@ bool SetAttributes(FileParms* parms) { auto lastaccesstime = BreakFileTime(Blizzard::Time::ToFileTime(info->accesstime)); auto lastwritetime = BreakFileTime(Blizzard::Time::ToFileTime(info->writetime)); - if (!::SetFileTime(parms->file->filehandle, &createtime, &lastaccesstime, &lastwritetime)) { + if (!::SetFileTime(file->filehandle, &createtime, &lastaccesstime, &lastwritetime)) { BC_FILE_SET_ERROR(8); return false; } @@ -778,7 +766,7 @@ bool SetAttributes(FileParms* parms) { } if (parms->setinfo & BC_SYSTEM_FILE_INFO_ATTRIBUTES) { - if (!::SetFileAttributes(PATH(parms->file->name), ToNativeAttributes(parms->info->attributes))) { + if (!::SetFileAttributes(PATH(file->name), ToNativeAttributes(parms->info->attributes))) { BC_FILE_SET_ERROR(8); return false; } @@ -802,9 +790,7 @@ bool SetPos(FileParms* parms) { BREAKFILEPOINTER(parms->offset, distancetomovehigh, distancetomovelow); auto movemethod = static_cast(parms->whence); - if (movemethod > FILE_END) { - movemethod = FILE_END; - } + movemethod = std::min(movemethod, static_cast(FILE_END)); if (::SetFilePointer(file->filehandle, distancetomovelow, &distancetomovehigh, movemethod) == INVALID_SET_FILE_POINTER) { if (GetLastError()) {