Browse Source

Get rid of most _alloca use and remove ALLOCA().

Darryl Pogue 13 years ago committed by Adam Johnson
parent
commit
6cdcf6a95e
  1. 2
      Sources/Plasma/CoreLib/hsMalloc.h
  2. 8
      Sources/Plasma/FeatureLib/pfGameMgr/pfGameMgr.cpp
  3. 15
      Sources/Plasma/NucleusLib/pnAsyncCoreExe/Private/Win32/pnAceW32Dns.cpp
  4. 4
      Sources/Plasma/NucleusLib/pnIni/Private/pnIniCore.cpp
  5. 12
      Sources/Plasma/NucleusLib/pnMail/pnMail.cpp
  6. 27
      Sources/Plasma/NucleusLib/pnNetCli/pnNcCli.cpp
  7. 3
      Sources/Plasma/NucleusLib/pnNetProtocol/Private/pnNpCommon.cpp
  8. 4
      Sources/Plasma/NucleusLib/pnOraLib/pnOraLib.cpp
  9. 2
      Sources/Plasma/NucleusLib/pnUtils/Private/Win32/pnUtW32Addr.cpp
  10. 14
      Sources/Plasma/NucleusLib/pnUtils/Private/Win32/pnUtW32Misc.cpp
  11. 8
      Sources/Plasma/NucleusLib/pnUtils/Private/pnUtCrypt.cpp
  12. 8
      Sources/Plasma/NucleusLib/pnUtils/Private/pnUtPath.cpp
  13. 4
      Sources/Plasma/NucleusLib/pnUtils/Private/pnUtUuid.cpp
  14. 4
      Sources/Plasma/PubUtilLib/plNetClientComm/plNetClientComm.cpp
  15. 9
      Sources/Plasma/PubUtilLib/plVault/plVaultNodeAccess.cpp

2
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 MEMDUP(s, b) MemDup(s, b, 0, __FILE__, __LINE__)
#define ZERO(s) MemSet(&s, 0, sizeof(s)) #define ZERO(s) MemSet(&s, 0, sizeof(s))
#define ZEROPTR(p) MemSet(p, 0, sizeof(*p)) #define ZEROPTR(p) MemSet(p, 0, sizeof(*p))
// Client must #include <malloc.h>
#define ALLOCA(t, n) (t *)_alloca((n) * sizeof(t))
#ifdef __cplusplus #ifdef __cplusplus

8
Sources/Plasma/FeatureLib/pfGameMgr/pfGameMgr.cpp

@ -415,7 +415,7 @@ void pfGameMgr::CreateGame (
- sizeof(msg->createData) - sizeof(msg->createData)
+ initBytes; + initBytes;
msg = (Cli2Srv_GameMgr_CreateGame *)_alloca(msgBytes); msg = (Cli2Srv_GameMgr_CreateGame *)malloc(msgBytes);
msg->messageId = kCli2Srv_GameMgr_CreateGame; msg->messageId = kCli2Srv_GameMgr_CreateGame;
msg->recvGameId = 0; // send to GameMgr on server msg->recvGameId = 0; // send to GameMgr on server
@ -426,6 +426,8 @@ void pfGameMgr::CreateGame (
MemCopy(msg->createData, initData, initBytes); MemCopy(msg->createData, initData, initBytes);
GameMgrSend(msg, NEWZERO(JoinTransState)(receiver)); GameMgrSend(msg, NEWZERO(JoinTransState)(receiver));
free(msg);
} }
//============================================================================ //============================================================================
@ -443,7 +445,7 @@ void pfGameMgr::JoinCommonGame (
- sizeof(msg->createData) - sizeof(msg->createData)
+ initBytes; + initBytes;
msg = (Cli2Srv_GameMgr_JoinGame *)_alloca(msgBytes); msg = (Cli2Srv_GameMgr_JoinGame *)malloc(msgBytes);
msg->messageId = kCli2Srv_GameMgr_JoinGame; msg->messageId = kCli2Srv_GameMgr_JoinGame;
msg->recvGameId = 0; // send to GameMgr on server msg->recvGameId = 0; // send to GameMgr on server
@ -455,6 +457,8 @@ void pfGameMgr::JoinCommonGame (
MemCopy(msg->createData, initData, initBytes); MemCopy(msg->createData, initData, initBytes);
GameMgrSend(msg, NEWZERO(JoinTransState)(receiver)); GameMgrSend(msg, NEWZERO(JoinTransState)(receiver));
free(msg);
} }

15
Sources/Plasma/NucleusLib/pnAsyncCoreExe/Private/Win32/pnAceW32Dns.cpp

@ -90,17 +90,17 @@ static unsigned s_nextLookupCancelId = 1;
static void LookupProcess (Lookup * lookup, unsigned error) { static void LookupProcess (Lookup * lookup, unsigned error) {
unsigned count = 0; unsigned count = 0;
NetAddress * addrs = nil; NetAddress * addrs = nil;
for (ONCE) {
if (error) if (error)
break; return;
const HOSTENT & host = * (HOSTENT *) lookup->buffer; const HOSTENT & host = * (HOSTENT *) lookup->buffer;
if (host.h_addrtype != AF_INET) if (host.h_addrtype != AF_INET)
break; return;
if (host.h_length != sizeof(in_addr)) if (host.h_length != sizeof(in_addr))
break; return;
if (!host.h_addr_list) if (!host.h_addr_list)
break; return;
in_addr const * const * const inAddr = (in_addr **) host.h_addr_list; in_addr const * const * const inAddr = (in_addr **) host.h_addr_list;
@ -109,7 +109,7 @@ static void LookupProcess (Lookup * lookup, unsigned error) {
++count; ++count;
// allocate a buffer large enough to hold all the addresses // allocate a buffer large enough to hold all the addresses
addrs = (NetAddress *) _alloca(sizeof(*addrs) * count); addrs = (NetAddress *)malloc(sizeof(*addrs) * count);
MemZero(addrs, sizeof(*addrs) * count); MemZero(addrs, sizeof(*addrs) * count);
// fill in address data // fill in address data
@ -123,7 +123,6 @@ static void LookupProcess (Lookup * lookup, unsigned error) {
if (host.h_name && host.h_name[0]) if (host.h_name && host.h_name[0])
StrToUnicode(lookup->name, host.h_name, arrsize(lookup->name)); StrToUnicode(lookup->name, host.h_name, arrsize(lookup->name));
}
if (lookup->lookupProc) if (lookup->lookupProc)
lookup->lookupProc(lookup->param, lookup->name, count, addrs); lookup->lookupProc(lookup->param, lookup->name, count, addrs);
@ -134,6 +133,8 @@ static void LookupProcess (Lookup * lookup, unsigned error) {
ASSERT(!lookup->link.IsLinked()); ASSERT(!lookup->link.IsLinked());
delete lookup; delete lookup;
PerfSubCounter(kAsyncPerfNameLookupAttemptsCurr, 1); PerfSubCounter(kAsyncPerfNameLookupAttemptsCurr, 1);
free(addrs);
} }
//=========================================================================== //===========================================================================

4
Sources/Plasma/NucleusLib/pnIni/Private/pnIniCore.cpp

@ -99,9 +99,11 @@ static void AddValueString (
const wchar_t src[] const wchar_t src[]
) { ) {
unsigned chars = StrLen(src) + 1; 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\""); StrTokenize(&src, dst, chars, L" \t\r\n\"");
value->fArgs.Add(StrDup(dst)); value->fArgs.Add(StrDup(dst));
free(dst);
} }

12
Sources/Plasma/NucleusLib/pnMail/pnMail.cpp

@ -212,10 +212,12 @@ static bool AdvanceStep (
++start; ++start;
const char * term = StrChr(start, ';'); const char * term = StrChr(start, ';');
if (term) { if (term) {
char * buffer = ALLOCA(char, term + 1 - start); char * buffer = (char*)malloc(term + 1 - start);
StrCopy(buffer, start, term + 1 - start); StrCopy(buffer, start, term + 1 - start);
Send(sock, "rcpt to:<", buffer, ">\r\n", nil); Send(sock, "rcpt to:<", buffer, ">\r\n", nil);
transaction->subStep = term + 1 - transaction->recipient; transaction->subStep = term + 1 - transaction->recipient;
free(buffer);
} }
else { else {
Send(sock, "rcpt to:<", start, ">\r\n", nil); Send(sock, "rcpt to:<", start, ">\r\n", nil);
@ -445,12 +447,7 @@ static void __cdecl Send (
} }
// Allocate string buffer // Allocate string buffer
char * packed; char* packed = (char *) malloc(bytes);
const unsigned kStackBufSize = 8 * 1024;
if (bytes > kStackBufSize)
packed = (char *) malloc(bytes);
else
packed = (char *) _alloca(bytes);
// Pack the string // Pack the string
{ {
@ -466,7 +463,6 @@ static void __cdecl Send (
AsyncSocketSend(sock, packed, bytes - 1); AsyncSocketSend(sock, packed, bytes - 1);
// Free the string // Free the string
if (bytes > kStackBufSize)
free(packed); free(packed);
} }

27
Sources/Plasma/NucleusLib/pnNetCli/pnNcCli.cpp

@ -169,7 +169,7 @@ namespace pnNetCli {
//============================================================================ //============================================================================
static void PutBufferOnWire (NetCli * cli, void * data, unsigned bytes) { static void PutBufferOnWire (NetCli * cli, void * data, unsigned bytes) {
uint8_t * temp, * heap = NULL; uint8_t * temp = NULL;
#ifndef PLASMA_EXTERNAL_RELEASE #ifndef PLASMA_EXTERNAL_RELEASE
// Write to the netlog // Write to the netlog
@ -189,13 +189,7 @@ static void PutBufferOnWire (NetCli * cli, void * data, unsigned bytes) {
#endif // PLASMA_EXTERNAL_RELEASE #endif // PLASMA_EXTERNAL_RELEASE
if (cli->mode == kNetCliModeEncrypted && cli->cryptOut) { if (cli->mode == kNetCliModeEncrypted && cli->cryptOut) {
// Encrypt data... temp = (uint8_t *)malloc(bytes);
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);
MemCopy(temp, data, bytes); MemCopy(temp, data, bytes);
CryptEncrypt(cli->cryptOut, bytes, temp); CryptEncrypt(cli->cryptOut, bytes, temp);
@ -205,7 +199,7 @@ static void PutBufferOnWire (NetCli * cli, void * data, unsigned bytes) {
AsyncSocketSend(cli->sock, data, bytes); AsyncSocketSend(cli->sock, data, bytes);
// free heap buffer (if any) // free heap buffer (if any)
free(heap); free(temp);
} }
//============================================================================ //============================================================================
@ -295,7 +289,7 @@ static void BufferedSendData (
case kNetMsgFieldInteger: { case kNetMsgFieldInteger: {
const unsigned count = cmd->count ? cmd->count : 1; const unsigned count = cmd->count ? cmd->count : 1;
const unsigned bytes = cmd->size * count; const unsigned bytes = cmd->size * count;
void * temp = ALLOCA(uint8_t, bytes); void * temp = malloc(bytes);
if (count == 1) if (count == 1)
{ {
@ -328,6 +322,8 @@ static void BufferedSendData (
// Write values to send buffer // Write values to send buffer
AddToSendBuffer(cli, bytes, temp); AddToSendBuffer(cli, bytes, temp);
free(temp);
} }
break; break;
@ -1112,15 +1108,10 @@ bool NetCliDispatch (
do { do {
if (cli->mode == kNetCliModeEncrypted) { if (cli->mode == kNetCliModeEncrypted) {
// Decrypt data... // Decrypt data...
uint8_t * temp, * heap = NULL; uint8_t * temp = NULL;
if (cli->cryptIn) { if (cli->cryptIn) {
if (bytes <= 2048) temp = (uint8_t *)malloc(bytes);
// 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);
MemCopy(temp, data, bytes); MemCopy(temp, data, bytes);
CryptDecrypt(cli->cryptIn, bytes, temp); CryptDecrypt(cli->cryptIn, bytes, temp);
@ -1153,7 +1144,7 @@ bool NetCliDispatch (
#endif #endif
// free heap buffer (if any) // free heap buffer (if any)
free(heap); free(temp);
cli->input.Compact(); cli->input.Compact();
return cli->recvDispatch; return cli->recvDispatch;

3
Sources/Plasma/NucleusLib/pnNetProtocol/Private/pnNpCommon.cpp

@ -991,9 +991,10 @@ void NetVaultNodeFieldArray::GetFieldValueString_LCS (
case NetVaultNode::kString64_6: case NetVaultNode::kString64_6:
case NetVaultNode::kIString64_1: case NetVaultNode::kIString64_1:
case NetVaultNode::kIString64_2: { 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); IStrSqlEscape(*(wchar_t **)fieldAddr, tmp, dstChars);
StrPrintf(dst, dstChars, L"'%s'", tmp); StrPrintf(dst, dstChars, L"'%s'", tmp);
free(tmp);
} }
break; break;

4
Sources/Plasma/NucleusLib/pnOraLib/pnOraLib.cpp

@ -458,9 +458,11 @@ void OraGetUuid (
occi::Bytes bytes = oraStmt->getBytes(index); occi::Bytes bytes = oraStmt->getBytes(index);
if (const unsigned length = bytes.length()) { if (const unsigned length = bytes.length()) {
ASSERT(length == msizeof(Uuid, data)); ASSERT(length == msizeof(Uuid, data));
uint8_t * buf = ALLOCA(uint8_t, length); uint8_t * buf = malloc(length);
bytes.getBytes(buf, length); bytes.getBytes(buf, length);
GuidFromHex(buf, length, uuid); GuidFromHex(buf, length, uuid);
free(buf)
} }
else { else {
GuidClear(uuid); GuidClear(uuid);

2
Sources/Plasma/NucleusLib/pnUtils/Private/Win32/pnUtW32Addr.cpp

@ -355,7 +355,7 @@ unsigned NetAddressGetLocal (
// Create a buffer to sort the addresses // Create a buffer to sort the addresses
NetAddressNode * dst; NetAddressNode * dst;
if (found > count) if (found > count)
dst = ALLOCA(NetAddressNode, found); dst = (NetAddressNode*)_alloca(found * sizeof(NetAddressNode));
else else
dst = addresses; dst = addresses;

14
Sources/Plasma/NucleusLib/pnUtils/Private/Win32/pnUtW32Misc.cpp

@ -96,14 +96,17 @@ void MemoryGetStatus (MemoryStatus * status) {
//============================================================================ //============================================================================
void DiskGetStatus (ARRAY(DiskStatus) * disks) { void DiskGetStatus (ARRAY(DiskStatus) * disks) {
for (;;) {
DWORD length = GetLogicalDriveStrings(0, NULL); DWORD length = GetLogicalDriveStrings(0, NULL);
if (!length || length > 2048) if (!length || length > 2048)
break; return;
wchar_t * buffer = ALLOCA(wchar_t, length + 1); wchar_t* buffer = (wchar_t*)malloc((length + 1) * sizeof(wchar_t));
wchar_t* origbuf = buffer;
if (!GetLogicalDriveStringsW(length, buffer)) if (!GetLogicalDriveStringsW(length, buffer))
break; {
free(buffer);
return;
}
for (; *buffer; buffer += StrLen(buffer) + 1) { for (; *buffer; buffer += StrLen(buffer) + 1) {
UINT driveType = GetDriveTypeW(buffer); UINT driveType = GetDriveTypeW(buffer);
@ -124,8 +127,7 @@ void DiskGetStatus (ARRAY(DiskStatus) * disks) {
disks->Add(status); disks->Add(status);
} }
break; free(origbuf);
}
} }
//============================================================================ //============================================================================

8
Sources/Plasma/NucleusLib/pnUtils/Private/pnUtCrypt.cpp

@ -166,9 +166,11 @@ static void Rc4Codec (
void * data void * data
) { ) {
// RC4 uses the same algorithm to both encrypt and decrypt // 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); RC4((RC4_KEY *)key->handle, bytes, (const unsigned char *)data, temp);
MemCopy(data, temp, bytes); MemCopy(data, temp, bytes);
free(temp);
} }
} using namespace Crypt; } using namespace Crypt;
@ -337,7 +339,7 @@ void CryptHashPassword (
unsigned passlen = StrLen(password); unsigned passlen = StrLen(password);
unsigned userlen = StrLen(username); 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, password, passlen);
StrCopy(buffer + passlen, username, userlen); StrCopy(buffer + passlen, username, userlen);
StrLower(buffer + passlen); // lowercase the username StrLower(buffer + passlen); // lowercase the username
@ -348,6 +350,8 @@ void CryptHashPassword (
(userlen + passlen) * sizeof(buffer[0]), (userlen + passlen) * sizeof(buffer[0]),
buffer buffer
); );
free(buffer);
} }
//============================================================================ //============================================================================

8
Sources/Plasma/NucleusLib/pnUtils/Private/pnUtPath.cpp

@ -234,7 +234,7 @@ void PathSplitEmail (
return; return;
// copy email address so we can tokenize it // 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); StrCopy(tmp, emailAddr, len + 1);
const wchar_t * work = tmp; const wchar_t * work = tmp;
@ -255,9 +255,11 @@ void PathSplitEmail (
ARRAY(wchar_t *) arr; ARRAY(wchar_t *) arr;
while (StrTokenize(&work, token, arrsize(token), L".")) { while (StrTokenize(&work, token, arrsize(token), L".")) {
unsigned toklen = StrLen(token); 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); StrCopy(str, token, toklen + 1);
arr.Add(str); arr.Add(str);
free(str);
} }
// copy domains to output parameters // copy domains to output parameters
@ -286,5 +288,7 @@ void PathSplitEmail (
StrCopy(&SUB_DOMAIN(index), arr[index], subDomainChars); StrCopy(&SUB_DOMAIN(index), arr[index], subDomainChars);
} }
free(tmp);
#undef SUB_DOMAIN #undef SUB_DOMAIN
} }

4
Sources/Plasma/NucleusLib/pnUtils/Private/pnUtUuid.cpp

@ -81,7 +81,7 @@ unsigned GuidHash (const Uuid & uuid) {
static const wchar_t s_hexChars[] = L"0123456789ABCDEF"; static const wchar_t s_hexChars[] = L"0123456789ABCDEF";
const wchar_t * GuidToHex (const Uuid & uuid, wchar_t * dst, unsigned chars) { 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; wchar_t * cur = str;
for (unsigned i = 0; i < sizeof(uuid.data); ++i) { 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; *cur = 0;
StrCopy(dst, str, chars); StrCopy(dst, str, chars);
free(str);
return dst; return dst;
} }

4
Sources/Plasma/PubUtilLib/plNetClientComm/plNetClientComm.cpp

@ -1084,7 +1084,7 @@ void NetCommSendMsg (
msg->SetPlayerID(NetCommGetPlayer()->playerInt); msg->SetPlayerID(NetCommGetPlayer()->playerInt);
unsigned msgSize = msg->GetPackSize(); unsigned msgSize = msg->GetPackSize();
uint8_t * buf = ALLOCA(uint8_t, msgSize); uint8_t * buf = (uint8_t *)malloc(msgSize);
msg->PokeBuffer((char *)buf, msgSize); msg->PokeBuffer((char *)buf, msgSize);
switch (msg->GetNetProtocol()) { switch (msg->GetNetProtocol()) {
@ -1106,6 +1106,8 @@ void NetCommSendMsg (
DEFAULT_FATAL(msg->GetNetProtocol()); DEFAULT_FATAL(msg->GetNetProtocol());
} }
free(buf);
} }
//============================================================================ //============================================================================

9
Sources/Plasma/PubUtilLib/plVault/plVaultNodeAccess.cpp

@ -566,17 +566,14 @@ void VaultSDLNode::SetStateDataRecord (const plStateDataRecord * rec, unsigned w
ram.Rewind(); ram.Rewind();
unsigned bytes = ram.GetEOF(); unsigned bytes = ram.GetEOF();
uint8_t * buf, * heap = nil; uint8_t * buf = nil;
if (bytes <= 2048) buf = (uint8_t *)malloc(bytes);
buf = ALLOCA(uint8_t, bytes);
else
buf = (uint8_t *)ALLOC(bytes);
ram.CopyToMem(buf); ram.CopyToMem(buf);
IVaultNodeSetBlob(kSDLData, base, &sdlData, &sdlDataLen, buf, bytes); IVaultNodeSetBlob(kSDLData, base, &sdlData, &sdlDataLen, buf, bytes);
free(heap); free(buf);
} }
#endif // def CLIENT #endif // def CLIENT

Loading…
Cancel
Save