void KMemoryRemoveTrack(void* addr) { if (!KAllocMap::allocMapValid) return; KAutoLock aCrit(allocMap.crit); KAllocMap::iterator anItr = allocMap.find(addr); if (anItr != allocMap.end()) allocMap.erase(anItr); };
bool Session::TestSetSendSeq(unsigned short theSeq, bool isReliable) const { AutoCrit aCrit(mRefP->mCrit); // Lock bool aRet = (isReliable ? (mRefP->mSendSeq + 1) == theSeq : mRefP->mSendSeq < theSeq); if (aRet) mRefP->mSendSeq = theSeq; return aRet; }
size_t KMemoryAllocated() { if (!KAllocMap::allocMapValid) return 0; KAutoLock aCrit(allocMap.crit); size_t size = 0; for(auto i = allocMap.begin(); i != allocMap.end(); ++i) { KANJI_ALLOC_INFO& info = i->second; size += info.size; } return size; }
extern void KMemoryAddTrack( void* addr, size_t asize, const char* fname, int lnum ) { if (!KAllocMap::allocMapValid || asize == 0) return; KAutoLock aCrit(allocMap.crit); showLeaks = true; KANJI_ALLOC_INFO& info = allocMap[addr]; info.file = fname; info.line = lnum; info.size = asize; #ifdef KANJI_TRACK_MEM_USAGE if (KAllocStat::allocMapValid) allocStat.addTrack(fname, lnum, asize); #endif };
void Session::Init(const EncryptAttributes& theAttr, SymmetricKey* theKeyP, AuthCertificateBase* theCertP, unsigned short theId) { // If current session is shared, break reference link and init a new session // NOTE: Must still check for a 0 ref count in case a thread switch happened // between if and decrement. if (mRefP->mRefCt > 1) { if (InterlockedDecrement(&(mRefP->mRefCt)) <= 0) delete mRefP; mRefP = new SessionData; } // Init session as remote session (theId specified) or new session AutoCrit aCrit(mRefP->mCrit); // Lock if (theId > 0) { mRefP->mId = theId; mRefP->mIsRemote = true; } else { mRefP->mId = GetNextSessionId(); mRefP->mIsRemote = false; } // Init session attributes mRefP->mEncryptAttr = theAttr; mRefP->mKeyP = theKeyP; mRefP->mCertP = theCertP; // Init seq numbers and update lastAction mRefP->mRecvSeq = mRefP->mSendSeq = 0; Touch(); }
void KMemoryDumpUnfreed() { if (!KAllocMap::allocMapValid) return; KAutoLock aCrit(allocMap.crit); // prevent modification of the map while iterating size_t totalSize = 0; char buf[8192]; FILE* f = fopen("mem_leaks.txt", "wt"); if (!f) return; time_t aTime = time(NULL); sprintf(buf, "Memory Leak Report for %s\n", asctime(localtime(&aTime))); fprintf(f, "%s", buf); KOutputDebug(DEBUGLVL, "\n"); KOutputDebug(INFOLVL, buf); for(KAllocMap::iterator i = allocMap.begin(); i != allocMap.end(); ++i) { sprintf(buf, "%s(%d) : Leak %u byte%s @0x%08X\n", i->second.file.c_str(), i->second.line, i->second.size, i->second.size > 1 ? "s" : "", (size_t)i->first); KOutputDebug(ERRORLVL, buf); fprintf(f, "%s", buf); #ifdef KANJI_DUMP_LEAKED_MEM unsigned char* data = (unsigned char*)i->first; int count = 0; char hex_dump[1024]; char ascii_dump[1024]; for (int index = 0; index < i->second.size; index++) { unsigned char _c = *data; if (count == 0) sprintf(hex_dump, "\t%02X ", _c); else sprintf(hex_dump, "%s%02X ", hex_dump, _c); // technically, this is undefined behavior if ((_c < 32) || (_c > 126)) _c = '.'; if (count == 7) sprintf(ascii_dump, "%s%c ", ascii_dump, _c); else sprintf(ascii_dump, "%s%c", count == 0 ? "\t" : ascii_dump, _c); // technically, this is undefined behavior if (++count == 16) { count = 0; sprintf(buf, "%s\t%s\n", hex_dump, ascii_dump); fprintf(f, buf); memset((void*)hex_dump, 0, 1024); memset((void*)ascii_dump, 0, 1024); } data++; } if (count != 0) { fprintf(f, hex_dump); for (int index = 0; index < 16 - count; index++) fprintf(f, "\t"); fprintf(f, ascii_dump); for (int index = 0; index < 16 - count; index++) fprintf(f, "."); } count = 0; fprintf(f, "\n\n"); memset((void*)hex_dump, 0, 1024); memset((void*)ascii_dump, 0, 1024); #endif // KANJI_DUMP_LEAKED_MEM totalSize += i->second.size; } ErrorLevel lvl = (totalSize > 0) ? ERRORLVL : INFOLVL; sprintf(buf, "-----------------------------------------------------------\n"); fprintf(f, "%s", buf); KOutputDebug(lvl, buf); sprintf(buf, "Total Unfreed: %u bytes (%luKB)\n\n", totalSize, totalSize / 1024); KOutputDebug(lvl, buf); fprintf(f, "%s", buf); fclose(f); }