diff --git a/Sources/Plasma/CoreLib/hsMalloc.h b/Sources/Plasma/CoreLib/hsMalloc.h index 99df0dbb..9bffcbb8 100644 --- a/Sources/Plasma/CoreLib/hsMalloc.h +++ b/Sources/Plasma/CoreLib/hsMalloc.h @@ -174,8 +174,6 @@ inline void CDECL operator delete (void *, void *) {} #define MEMDUP(s, b) MemDup(s, b, 0, __FILE__, __LINE__) #define ZERO(s) MemSet(&s, 0, sizeof(s)) #define ZEROPTR(p) MemSet(p, 0, sizeof(*p)) -// Client must #include -#define ALLOCA(t, n) (t *)_alloca((n) * sizeof(t)) #ifdef __cplusplus diff --git a/Sources/Plasma/FeatureLib/pfGameMgr/pfGameMgr.cpp b/Sources/Plasma/FeatureLib/pfGameMgr/pfGameMgr.cpp index 3f67bff2..d6d18a45 100644 --- a/Sources/Plasma/FeatureLib/pfGameMgr/pfGameMgr.cpp +++ b/Sources/Plasma/FeatureLib/pfGameMgr/pfGameMgr.cpp @@ -415,7 +415,7 @@ void pfGameMgr::CreateGame ( - sizeof(msg->createData) + initBytes; - msg = (Cli2Srv_GameMgr_CreateGame *)_alloca(msgBytes); + msg = (Cli2Srv_GameMgr_CreateGame *)malloc(msgBytes); msg->messageId = kCli2Srv_GameMgr_CreateGame; msg->recvGameId = 0; // send to GameMgr on server @@ -426,6 +426,8 @@ void pfGameMgr::CreateGame ( MemCopy(msg->createData, initData, initBytes); GameMgrSend(msg, NEWZERO(JoinTransState)(receiver)); + + free(msg); } //============================================================================ @@ -443,7 +445,7 @@ void pfGameMgr::JoinCommonGame ( - sizeof(msg->createData) + initBytes; - msg = (Cli2Srv_GameMgr_JoinGame *)_alloca(msgBytes); + msg = (Cli2Srv_GameMgr_JoinGame *)malloc(msgBytes); msg->messageId = kCli2Srv_GameMgr_JoinGame; msg->recvGameId = 0; // send to GameMgr on server @@ -455,6 +457,8 @@ void pfGameMgr::JoinCommonGame ( MemCopy(msg->createData, initData, initBytes); GameMgrSend(msg, NEWZERO(JoinTransState)(receiver)); + + free(msg); } diff --git a/Sources/Plasma/NucleusLib/pnAsyncCoreExe/Private/Win32/pnAceW32Dns.cpp b/Sources/Plasma/NucleusLib/pnAsyncCoreExe/Private/Win32/pnAceW32Dns.cpp index 9bbcf829..4328aade 100644 --- a/Sources/Plasma/NucleusLib/pnAsyncCoreExe/Private/Win32/pnAceW32Dns.cpp +++ b/Sources/Plasma/NucleusLib/pnAsyncCoreExe/Private/Win32/pnAceW32Dns.cpp @@ -90,41 +90,40 @@ static unsigned s_nextLookupCancelId = 1; static void LookupProcess (Lookup * lookup, unsigned error) { unsigned count = 0; NetAddress * addrs = nil; - for (ONCE) { - if (error) - break; - const HOSTENT & host = * (HOSTENT *) lookup->buffer; - if (host.h_addrtype != AF_INET) - break; - if (host.h_length != sizeof(in_addr)) - break; - if (!host.h_addr_list) - break; + if (error) + return; - in_addr const * const * const inAddr = (in_addr **) host.h_addr_list; + const HOSTENT & host = * (HOSTENT *) lookup->buffer; + if (host.h_addrtype != AF_INET) + return; + if (host.h_length != sizeof(in_addr)) + return; + if (!host.h_addr_list) + return; - // count the number of addresses - while (inAddr[count]) - ++count; + in_addr const * const * const inAddr = (in_addr **) host.h_addr_list; - // allocate a buffer large enough to hold all the addresses - addrs = (NetAddress *) _alloca(sizeof(*addrs) * count); - MemZero(addrs, sizeof(*addrs) * count); + // count the number of addresses + while (inAddr[count]) + ++count; - // fill in address data - const uint16_t port = htons((uint16_t) lookup->port); - for (unsigned i = 0; i < count; ++i) { - sockaddr_in * inetaddr = (sockaddr_in *) &addrs[i]; - inetaddr->sin_family = AF_INET; - inetaddr->sin_addr = *inAddr[i]; - inetaddr->sin_port = port; - } + // allocate a buffer large enough to hold all the addresses + addrs = (NetAddress *)malloc(sizeof(*addrs) * count); + MemZero(addrs, sizeof(*addrs) * count); - if (host.h_name && host.h_name[0]) - StrToUnicode(lookup->name, host.h_name, arrsize(lookup->name)); + // fill in address data + const uint16_t port = htons((uint16_t) lookup->port); + for (unsigned i = 0; i < count; ++i) { + sockaddr_in * inetaddr = (sockaddr_in *) &addrs[i]; + inetaddr->sin_family = AF_INET; + inetaddr->sin_addr = *inAddr[i]; + inetaddr->sin_port = port; } + if (host.h_name && host.h_name[0]) + StrToUnicode(lookup->name, host.h_name, arrsize(lookup->name)); + if (lookup->lookupProc) lookup->lookupProc(lookup->param, lookup->name, count, addrs); @@ -134,6 +133,8 @@ static void LookupProcess (Lookup * lookup, unsigned error) { ASSERT(!lookup->link.IsLinked()); delete lookup; PerfSubCounter(kAsyncPerfNameLookupAttemptsCurr, 1); + + free(addrs); } //=========================================================================== diff --git a/Sources/Plasma/NucleusLib/pnIni/Private/pnIniCore.cpp b/Sources/Plasma/NucleusLib/pnIni/Private/pnIniCore.cpp index ea3ab967..017ce677 100644 --- a/Sources/Plasma/NucleusLib/pnIni/Private/pnIniCore.cpp +++ b/Sources/Plasma/NucleusLib/pnIni/Private/pnIniCore.cpp @@ -99,9 +99,11 @@ static void AddValueString ( const wchar_t src[] ) { unsigned chars = StrLen(src) + 1; - wchar_t * dst = ALLOCA(wchar_t, chars); + wchar_t * dst = (wchar_t*)malloc(sizeof(wchar_t) * chars); StrTokenize(&src, dst, chars, L" \t\r\n\""); value->fArgs.Add(StrDup(dst)); + + free(dst); } diff --git a/Sources/Plasma/NucleusLib/pnMail/pnMail.cpp b/Sources/Plasma/NucleusLib/pnMail/pnMail.cpp index 1c6f4866..2e93b43c 100644 --- a/Sources/Plasma/NucleusLib/pnMail/pnMail.cpp +++ b/Sources/Plasma/NucleusLib/pnMail/pnMail.cpp @@ -212,10 +212,12 @@ static bool AdvanceStep ( ++start; const char * term = StrChr(start, ';'); if (term) { - char * buffer = ALLOCA(char, term + 1 - start); + char * buffer = (char*)malloc(term + 1 - start); StrCopy(buffer, start, term + 1 - start); Send(sock, "rcpt to:<", buffer, ">\r\n", nil); transaction->subStep = term + 1 - transaction->recipient; + + free(buffer); } else { Send(sock, "rcpt to:<", start, ">\r\n", nil); @@ -445,12 +447,7 @@ static void __cdecl Send ( } // Allocate string buffer - char * packed; - const unsigned kStackBufSize = 8 * 1024; - if (bytes > kStackBufSize) - packed = (char *) malloc(bytes); - else - packed = (char *) _alloca(bytes); + char* packed = (char *) malloc(bytes); // Pack the string { @@ -466,8 +463,7 @@ static void __cdecl Send ( AsyncSocketSend(sock, packed, bytes - 1); // Free the string - if (bytes > kStackBufSize) - free(packed); + free(packed); } //=========================================================================== diff --git a/Sources/Plasma/NucleusLib/pnNetCli/pnNcCli.cpp b/Sources/Plasma/NucleusLib/pnNetCli/pnNcCli.cpp index 6b4bf263..f47606a5 100644 --- a/Sources/Plasma/NucleusLib/pnNetCli/pnNcCli.cpp +++ b/Sources/Plasma/NucleusLib/pnNetCli/pnNcCli.cpp @@ -169,7 +169,7 @@ namespace pnNetCli { //============================================================================ static void PutBufferOnWire (NetCli * cli, void * data, unsigned bytes) { - uint8_t * temp, * heap = NULL; + uint8_t * temp = NULL; #ifndef PLASMA_EXTERNAL_RELEASE // Write to the netlog @@ -189,13 +189,7 @@ static void PutBufferOnWire (NetCli * cli, void * data, unsigned bytes) { #endif // PLASMA_EXTERNAL_RELEASE if (cli->mode == kNetCliModeEncrypted && cli->cryptOut) { - // Encrypt data... - if (bytes <= 2048) - // uint8_t count is small, use stack-based buffer - temp = ALLOCA(uint8_t, bytes); - else - // uint8_t count is large, use heap-based buffer - temp = heap = (uint8_t *)malloc(bytes); + temp = (uint8_t *)malloc(bytes); MemCopy(temp, data, bytes); CryptEncrypt(cli->cryptOut, bytes, temp); @@ -205,7 +199,7 @@ static void PutBufferOnWire (NetCli * cli, void * data, unsigned bytes) { AsyncSocketSend(cli->sock, data, bytes); // free heap buffer (if any) - free(heap); + free(temp); } //============================================================================ @@ -295,7 +289,7 @@ static void BufferedSendData ( case kNetMsgFieldInteger: { const unsigned count = cmd->count ? cmd->count : 1; const unsigned bytes = cmd->size * count; - void * temp = ALLOCA(uint8_t, bytes); + void * temp = malloc(bytes); if (count == 1) { @@ -328,6 +322,8 @@ static void BufferedSendData ( // Write values to send buffer AddToSendBuffer(cli, bytes, temp); + + free(temp); } break; @@ -1112,15 +1108,10 @@ bool NetCliDispatch ( do { if (cli->mode == kNetCliModeEncrypted) { // Decrypt data... - uint8_t * temp, * heap = NULL; + uint8_t * temp = NULL; if (cli->cryptIn) { - if (bytes <= 2048) - // uint8_t count is small, use stack-based buffer - temp = ALLOCA(uint8_t, bytes); - else - // uint8_t count is large, use heap-based buffer - temp = heap = (uint8_t *)malloc(bytes); + temp = (uint8_t *)malloc(bytes); MemCopy(temp, data, bytes); CryptDecrypt(cli->cryptIn, bytes, temp); @@ -1153,7 +1144,7 @@ bool NetCliDispatch ( #endif // free heap buffer (if any) - free(heap); + free(temp); cli->input.Compact(); return cli->recvDispatch; diff --git a/Sources/Plasma/NucleusLib/pnNetProtocol/Private/pnNpCommon.cpp b/Sources/Plasma/NucleusLib/pnNetProtocol/Private/pnNpCommon.cpp index af371b59..d8831588 100644 --- a/Sources/Plasma/NucleusLib/pnNetProtocol/Private/pnNpCommon.cpp +++ b/Sources/Plasma/NucleusLib/pnNetProtocol/Private/pnNpCommon.cpp @@ -991,9 +991,10 @@ void NetVaultNodeFieldArray::GetFieldValueString_LCS ( case NetVaultNode::kString64_6: case NetVaultNode::kIString64_1: case NetVaultNode::kIString64_2: { - wchar_t * tmp = ALLOCA(wchar_t, dstChars); + wchar_t * tmp = (wchar_t*)malloc(sizeof(wchar_t) * dstChars); IStrSqlEscape(*(wchar_t **)fieldAddr, tmp, dstChars); StrPrintf(dst, dstChars, L"'%s'", tmp); + free(tmp); } break; diff --git a/Sources/Plasma/NucleusLib/pnOraLib/pnOraLib.cpp b/Sources/Plasma/NucleusLib/pnOraLib/pnOraLib.cpp index a9eb58b0..ff712593 100644 --- a/Sources/Plasma/NucleusLib/pnOraLib/pnOraLib.cpp +++ b/Sources/Plasma/NucleusLib/pnOraLib/pnOraLib.cpp @@ -458,9 +458,11 @@ void OraGetUuid ( occi::Bytes bytes = oraStmt->getBytes(index); if (const unsigned length = bytes.length()) { ASSERT(length == msizeof(Uuid, data)); - uint8_t * buf = ALLOCA(uint8_t, length); + uint8_t * buf = malloc(length); bytes.getBytes(buf, length); GuidFromHex(buf, length, uuid); + + free(buf) } else { GuidClear(uuid); diff --git a/Sources/Plasma/NucleusLib/pnUtils/Private/Win32/pnUtW32Addr.cpp b/Sources/Plasma/NucleusLib/pnUtils/Private/Win32/pnUtW32Addr.cpp index 5112cb50..5b00b896 100644 --- a/Sources/Plasma/NucleusLib/pnUtils/Private/Win32/pnUtW32Addr.cpp +++ b/Sources/Plasma/NucleusLib/pnUtils/Private/Win32/pnUtW32Addr.cpp @@ -355,7 +355,7 @@ unsigned NetAddressGetLocal ( // Create a buffer to sort the addresses NetAddressNode * dst; if (found > count) - dst = ALLOCA(NetAddressNode, found); + dst = (NetAddressNode*)_alloca(found * sizeof(NetAddressNode)); else dst = addresses; diff --git a/Sources/Plasma/NucleusLib/pnUtils/Private/Win32/pnUtW32Misc.cpp b/Sources/Plasma/NucleusLib/pnUtils/Private/Win32/pnUtW32Misc.cpp index 393ed4ac..4de26024 100644 --- a/Sources/Plasma/NucleusLib/pnUtils/Private/Win32/pnUtW32Misc.cpp +++ b/Sources/Plasma/NucleusLib/pnUtils/Private/Win32/pnUtW32Misc.cpp @@ -96,36 +96,38 @@ void MemoryGetStatus (MemoryStatus * status) { //============================================================================ void DiskGetStatus (ARRAY(DiskStatus) * disks) { - for (;;) { - DWORD length = GetLogicalDriveStrings(0, NULL); - if (!length || length > 2048) - break; - - wchar_t * buffer = ALLOCA(wchar_t, length + 1); - if (!GetLogicalDriveStringsW(length, buffer)) - break; - - for (; *buffer; buffer += StrLen(buffer) + 1) { - UINT driveType = GetDriveTypeW(buffer); - if (driveType != DRIVE_FIXED) - continue; - - ULARGE_INTEGER freeBytes; - ULARGE_INTEGER totalBytes; - if (!GetDiskFreeSpaceExW(buffer, &freeBytes, &totalBytes, NULL)) - continue; - - DiskStatus status; - StrCopy(status.name, buffer, arrsize(status.name)); - - const uint64_t BYTES_PER_MB = 1024 * 1024; - status.totalSpaceMB = unsigned(totalBytes.QuadPart / BYTES_PER_MB); - status.freeSpaceMB = unsigned(freeBytes.QuadPart / BYTES_PER_MB); - - disks->Add(status); - } - break; + DWORD length = GetLogicalDriveStrings(0, NULL); + if (!length || length > 2048) + return; + + wchar_t* buffer = (wchar_t*)malloc((length + 1) * sizeof(wchar_t)); + wchar_t* origbuf = buffer; + if (!GetLogicalDriveStringsW(length, buffer)) + { + free(buffer); + return; } + + for (; *buffer; buffer += StrLen(buffer) + 1) { + UINT driveType = GetDriveTypeW(buffer); + if (driveType != DRIVE_FIXED) + continue; + + ULARGE_INTEGER freeBytes; + ULARGE_INTEGER totalBytes; + if (!GetDiskFreeSpaceExW(buffer, &freeBytes, &totalBytes, NULL)) + continue; + + DiskStatus status; + StrCopy(status.name, buffer, arrsize(status.name)); + + const uint64_t BYTES_PER_MB = 1024 * 1024; + status.totalSpaceMB = unsigned(totalBytes.QuadPart / BYTES_PER_MB); + status.freeSpaceMB = unsigned(freeBytes.QuadPart / BYTES_PER_MB); + + disks->Add(status); + } + free(origbuf); } //============================================================================ diff --git a/Sources/Plasma/NucleusLib/pnUtils/Private/pnUtCrypt.cpp b/Sources/Plasma/NucleusLib/pnUtils/Private/pnUtCrypt.cpp index 41936e3c..707b4420 100644 --- a/Sources/Plasma/NucleusLib/pnUtils/Private/pnUtCrypt.cpp +++ b/Sources/Plasma/NucleusLib/pnUtils/Private/pnUtCrypt.cpp @@ -166,9 +166,11 @@ static void Rc4Codec ( void * data ) { // RC4 uses the same algorithm to both encrypt and decrypt - uint8_t * temp = ALLOCA(uint8_t, bytes); + uint8_t * temp = (uint8_t *)malloc(bytes); RC4((RC4_KEY *)key->handle, bytes, (const unsigned char *)data, temp); MemCopy(data, temp, bytes); + + free(temp); } } using namespace Crypt; @@ -337,7 +339,7 @@ void CryptHashPassword ( unsigned passlen = StrLen(password); unsigned userlen = StrLen(username); - wchar_t * buffer = ALLOCA(wchar_t, passlen + userlen); + wchar_t * buffer = (wchar_t*)malloc(sizeof(wchar_t) * (passlen + userlen)); StrCopy(buffer, password, passlen); StrCopy(buffer + passlen, username, userlen); StrLower(buffer + passlen); // lowercase the username @@ -348,6 +350,8 @@ void CryptHashPassword ( (userlen + passlen) * sizeof(buffer[0]), buffer ); + + free(buffer); } //============================================================================ diff --git a/Sources/Plasma/NucleusLib/pnUtils/Private/pnUtPath.cpp b/Sources/Plasma/NucleusLib/pnUtils/Private/pnUtPath.cpp index c419215c..c7d3b96a 100644 --- a/Sources/Plasma/NucleusLib/pnUtils/Private/pnUtPath.cpp +++ b/Sources/Plasma/NucleusLib/pnUtils/Private/pnUtPath.cpp @@ -234,7 +234,7 @@ void PathSplitEmail ( return; // copy email address so we can tokenize it - wchar_t * tmp = ALLOCA(wchar_t, len + 1); + wchar_t * tmp = (wchar_t*)malloc(sizeof(wchar_t) * (len + 1)); StrCopy(tmp, emailAddr, len + 1); const wchar_t * work = tmp; @@ -255,9 +255,11 @@ void PathSplitEmail ( ARRAY(wchar_t *) arr; while (StrTokenize(&work, token, arrsize(token), L".")) { unsigned toklen = StrLen(token); - wchar_t * str = ALLOCA(wchar_t, toklen + 1); + wchar_t * str = (wchar_t*)malloc(sizeof(wchar_t) * (toklen + 1)); StrCopy(str, token, toklen + 1); arr.Add(str); + + free(str); } // copy domains to output parameters @@ -285,6 +287,8 @@ void PathSplitEmail ( if (subDomains) StrCopy(&SUB_DOMAIN(index), arr[index], subDomainChars); } + + free(tmp); #undef SUB_DOMAIN } diff --git a/Sources/Plasma/NucleusLib/pnUtils/Private/pnUtUuid.cpp b/Sources/Plasma/NucleusLib/pnUtils/Private/pnUtUuid.cpp index d1aef0c4..d52e02f9 100644 --- a/Sources/Plasma/NucleusLib/pnUtils/Private/pnUtUuid.cpp +++ b/Sources/Plasma/NucleusLib/pnUtils/Private/pnUtUuid.cpp @@ -81,7 +81,7 @@ unsigned GuidHash (const Uuid & uuid) { static const wchar_t s_hexChars[] = L"0123456789ABCDEF"; const wchar_t * GuidToHex (const Uuid & uuid, wchar_t * dst, unsigned chars) { - wchar_t * str = ALLOCA(wchar_t, sizeof(uuid.data) * 2 + 1); + wchar_t * str = (wchar_t*)malloc((sizeof(uuid.data) * 2 + 1) * sizeof(wchar_t)); wchar_t * cur = str; for (unsigned i = 0; i < sizeof(uuid.data); ++i) { @@ -91,6 +91,8 @@ const wchar_t * GuidToHex (const Uuid & uuid, wchar_t * dst, unsigned chars) { *cur = 0; StrCopy(dst, str, chars); + + free(str); return dst; } @@ -100,4 +102,4 @@ bool GuidFromHex (const uint8_t buf[], unsigned length, Uuid * uuid) { ASSERT(length == msizeof(Uuid, data)); MemCopy(uuid->data, buf, msizeof(Uuid, data)); return true; -} \ No newline at end of file +} diff --git a/Sources/Plasma/PubUtilLib/plNetClientComm/plNetClientComm.cpp b/Sources/Plasma/PubUtilLib/plNetClientComm/plNetClientComm.cpp index 03aaf49b..7154116b 100644 --- a/Sources/Plasma/PubUtilLib/plNetClientComm/plNetClientComm.cpp +++ b/Sources/Plasma/PubUtilLib/plNetClientComm/plNetClientComm.cpp @@ -1084,7 +1084,7 @@ void NetCommSendMsg ( msg->SetPlayerID(NetCommGetPlayer()->playerInt); unsigned msgSize = msg->GetPackSize(); - uint8_t * buf = ALLOCA(uint8_t, msgSize); + uint8_t * buf = (uint8_t *)malloc(msgSize); msg->PokeBuffer((char *)buf, msgSize); switch (msg->GetNetProtocol()) { @@ -1106,6 +1106,8 @@ void NetCommSendMsg ( DEFAULT_FATAL(msg->GetNetProtocol()); } + + free(buf); } //============================================================================ diff --git a/Sources/Plasma/PubUtilLib/plVault/plVaultNodeAccess.cpp b/Sources/Plasma/PubUtilLib/plVault/plVaultNodeAccess.cpp index 99ce12b3..f9304217 100644 --- a/Sources/Plasma/PubUtilLib/plVault/plVaultNodeAccess.cpp +++ b/Sources/Plasma/PubUtilLib/plVault/plVaultNodeAccess.cpp @@ -566,17 +566,14 @@ void VaultSDLNode::SetStateDataRecord (const plStateDataRecord * rec, unsigned w ram.Rewind(); unsigned bytes = ram.GetEOF(); - uint8_t * buf, * heap = nil; - if (bytes <= 2048) - buf = ALLOCA(uint8_t, bytes); - else - buf = (uint8_t *)ALLOC(bytes); + uint8_t * buf = nil; + buf = (uint8_t *)malloc(bytes); ram.CopyToMem(buf); IVaultNodeSetBlob(kSDLData, base, &sdlData, &sdlDataLen, buf, bytes); - free(heap); + free(buf); } #endif // def CLIENT