From: <arn...@us...> - 2008-03-01 23:17:01
|
Revision: 1022 http://dcplusplus.svn.sourceforge.net/dcplusplus/?rev=1022&view=rev Author: arnetheduck Date: 2008-03-01 15:16:58 -0800 (Sat, 01 Mar 2008) Log Message: ----------- boostify some mem handling Modified Paths: -------------- dcplusplus/trunk/dcpp/AdcHub.cpp dcplusplus/trunk/dcpp/BufferedSocket.cpp dcplusplus/trunk/dcpp/CryptoManager.cpp dcplusplus/trunk/dcpp/DirectoryListing.cpp dcplusplus/trunk/dcpp/DownloadManager.cpp dcplusplus/trunk/dcpp/File.cpp dcplusplus/trunk/dcpp/FilteredFile.h dcplusplus/trunk/dcpp/HashManager.cpp dcplusplus/trunk/dcpp/NmdcHub.cpp dcplusplus/trunk/dcpp/SearchManager.cpp dcplusplus/trunk/dcpp/ShareManager.cpp dcplusplus/trunk/dcpp/Streams.h dcplusplus/trunk/dcpp/Util.cpp dcplusplus/trunk/dcpp/Util.h dcplusplus/trunk/dcpp/stdinc.h dcplusplus/trunk/win32/PropPage.cpp dcplusplus/trunk/win32/WinUtil.cpp Modified: dcplusplus/trunk/dcpp/AdcHub.cpp =================================================================== --- dcplusplus/trunk/dcpp/AdcHub.cpp 2008-03-01 22:39:24 UTC (rev 1021) +++ dcplusplus/trunk/dcpp/AdcHub.cpp 2008-03-01 23:16:58 UTC (rev 1022) @@ -643,15 +643,15 @@ return; if(!salt.empty()) { size_t saltBytes = salt.size() * 5 / 8; - AutoArray<uint8_t> buf(saltBytes); - Encoder::fromBase32(salt.c_str(), buf, saltBytes); + boost::scoped_array<uint8_t> buf(new uint8_t[saltBytes]); + Encoder::fromBase32(salt.c_str(), &buf[0], saltBytes); TigerHash th; if(oldPassword) { CID cid = getMyIdentity().getUser()->getCID(); th.update(cid.data(), CID::SIZE); } th.update(pwd.data(), pwd.length()); - th.update(buf, saltBytes); + th.update(&buf[0], saltBytes); send(AdcCommand(AdcCommand::CMD_PAS, AdcCommand::TYPE_HUB).addParam(Encoder::toBase32(th.finalize(), TigerHash::BYTES))); salt.clear(); } Modified: dcplusplus/trunk/dcpp/BufferedSocket.cpp =================================================================== --- dcplusplus/trunk/dcpp/BufferedSocket.cpp 2008-03-01 22:39:24 UTC (rev 1021) +++ dcplusplus/trunk/dcpp/BufferedSocket.cpp 2008-03-01 23:16:58 UTC (rev 1022) @@ -183,7 +183,7 @@ const int BufSize = 1024; // Special to autodetect nmdc connections... string::size_type pos = 0; - AutoArray<uint8_t> buffer (BufSize); + boost::scoped_array<uint8_t> buffer (new uint8_t[BufSize]); size_t in; l = line; // decompress all input data and store in l. Modified: dcplusplus/trunk/dcpp/CryptoManager.cpp =================================================================== --- dcplusplus/trunk/dcpp/CryptoManager.cpp 2008-03-01 22:39:24 UTC (rev 1021) +++ dcplusplus/trunk/dcpp/CryptoManager.cpp 2008-03-01 23:16:58 UTC (rev 1022) @@ -346,12 +346,12 @@ // We assume that the files aren't compressed more than 2:1...if they are it'll work anyway, // but we'll have to do multiple passes... size_t bufsize = 2*sz; - AutoArray<char> buf(bufsize); + boost::scoped_array<char> buf(new char[bufsize]); bs.avail_in = sz; bs.avail_out = bufsize; bs.next_in = (char*)(const_cast<uint8_t*>(is)); - bs.next_out = buf; + bs.next_out = &buf[0]; int err; @@ -362,13 +362,13 @@ BZ2_bzDecompressEnd(&bs); throw CryptoException(_("Error during decompression")); } - os.append(buf, bufsize-bs.avail_out); + os.append(&buf[0], bufsize-bs.avail_out); bs.avail_out = bufsize; - bs.next_out = buf; + bs.next_out = &buf[0]; } if(err == BZ_STREAM_END) - os.append(buf, bufsize-bs.avail_out); + os.append(&buf[0], bufsize-bs.avail_out); BZ2_bzDecompressEnd(&bs); @@ -379,7 +379,7 @@ } string CryptoManager::keySubst(const uint8_t* aKey, size_t len, size_t n) { - AutoArray<uint8_t> temp(len + n * 10); + boost::scoped_array<uint8_t> temp(new uint8_t[len + n * 10]); size_t j=0; @@ -400,14 +400,14 @@ temp[j++] = aKey[i]; } } - return string((char*)(uint8_t*)temp, j); + return string((const char*)&temp[0], j); } string CryptoManager::makeKey(const string& aLock) { if(aLock.size() < 3) return Util::emptyString; - AutoArray<uint8_t> temp(aLock.length()); + boost::scoped_array<uint8_t> temp(new uint8_t[aLock.length()]); uint8_t v1; size_t extra=0; @@ -431,7 +431,7 @@ extra++; } - return keySubst(temp, aLock.length(), extra); + return keySubst(&temp[0], aLock.length(), extra); } } // namespace dcpp Modified: dcplusplus/trunk/dcpp/DirectoryListing.cpp =================================================================== --- dcplusplus/trunk/dcpp/DirectoryListing.cpp 2008-03-01 22:39:24 UTC (rev 1021) +++ dcplusplus/trunk/dcpp/DirectoryListing.cpp 2008-03-01 23:16:58 UTC (rev 1022) @@ -83,13 +83,13 @@ dcpp::File ff(name, dcpp::File::READ, dcpp::File::OPEN); FilteredInputStream<UnBZFilter, false> f(&ff); const size_t BUF_SIZE = 64*1024; - AutoArray<char> buf(BUF_SIZE); + boost::scoped_array<char> buf(new char[BUF_SIZE]); size_t len; size_t bytesRead = 0; for(;;) { size_t n = BUF_SIZE; - len = f.read(buf, n); - txt.append(buf, len); + len = f.read(&buf[0], n); + txt.append(&buf[0], len); bytesRead += len; if(SETTING(MAX_FILELIST_SIZE) && bytesRead > (size_t)SETTING(MAX_FILELIST_SIZE)*1024*1024) break; Modified: dcplusplus/trunk/dcpp/DownloadManager.cpp =================================================================== --- dcplusplus/trunk/dcpp/DownloadManager.cpp 2008-03-01 22:39:24 UTC (rev 1021) +++ dcplusplus/trunk/dcpp/DownloadManager.cpp 2008-03-01 23:16:58 UTC (rev 1022) @@ -350,9 +350,9 @@ CalcInputStream<CRC32Filter, false> f(&ff); const size_t BUF_SIZE = 1024*1024; - AutoArray<uint8_t> b(BUF_SIZE); + boost::scoped_array<uint8_t> b(new uint8_t[BUF_SIZE]); size_t n = BUF_SIZE; - while(f.read((uint8_t*)b, n) > 0) + while(f.read(&b[0], n) > 0) ; // Keep on looping... return f.getFilter().getValue(); Modified: dcplusplus/trunk/dcpp/File.cpp =================================================================== --- dcplusplus/trunk/dcpp/File.cpp 2008-03-01 22:39:24 UTC (rev 1021) +++ dcplusplus/trunk/dcpp/File.cpp 2008-03-01 23:16:58 UTC (rev 1022) @@ -350,13 +350,13 @@ // This doesn't assume all bytes are written in one write call, it is a bit safer void File::copyFile(const string& source, const string& target) throw(FileException) { const size_t BUF_SIZE = 64 * 1024; - AutoArray<char> buffer(BUF_SIZE); + boost::scoped_array<char> buffer(BUF_SIZE); size_t count = BUF_SIZE; File src(source, File::READ, 0); File dst(target, File::WRITE, File::CREATE | File::TRUNCATE); - while(src.read((char*)buffer, count) > 0) { - char* p = (char*)buffer; + while(src.read(&buffer[0], count) > 0) { + char* p = &buffer[0]; while(count > 0) { size_t ret = dst.write(p, count); p += ret; Modified: dcplusplus/trunk/dcpp/FilteredFile.h =================================================================== --- dcplusplus/trunk/dcpp/FilteredFile.h 2008-03-01 22:39:24 UTC (rev 1021) +++ dcplusplus/trunk/dcpp/FilteredFile.h 2008-03-01 23:16:58 UTC (rev 1022) @@ -95,7 +95,7 @@ public: using OutputStream::write; - FilteredOutputStream(OutputStream* aFile) : f(aFile), buf(BUF_SIZE), flushed(false) { } + FilteredOutputStream(OutputStream* aFile) : f(aFile), buf(new uint8_t[BUF_SIZE]), flushed(false) { } ~FilteredOutputStream() throw() { if(manage) delete f; } size_t flush() throw(Exception) { @@ -108,9 +108,9 @@ for(;;) { size_t n = BUF_SIZE; size_t zero = 0; - bool more = filter(NULL, zero, buf, n); + bool more = filter(NULL, zero, &buf[0], n); - written += f->write(buf, n); + written += f->write(&buf[0], n); if(!more) break; @@ -128,11 +128,11 @@ size_t n = BUF_SIZE; size_t m = len; - bool more = filter(wb, m, buf, n); + bool more = filter(wb, m, &buf[0], n); wb += m; len -= m; - written += f->write(buf, n); + written += f->write(&buf[0], n); if(!more) { if(len > 0) { @@ -151,14 +151,14 @@ OutputStream* f; Filter filter; - AutoArray<uint8_t> buf; + boost::scoped_array<uint8_t> buf; bool flushed; }; template<class Filter, bool managed> class FilteredInputStream : public InputStream { public: - FilteredInputStream(InputStream* aFile) : f(aFile), buf(BUF_SIZE), pos(0), valid(0), more(true) { } + FilteredInputStream(InputStream* aFile) : f(aFile), buf(new uint8_t[BUF_SIZE]), pos(0), valid(0), more(true) { } virtual ~FilteredInputStream() throw() { if(managed) delete f; } /** @@ -177,13 +177,13 @@ size_t curRead = BUF_SIZE; if(valid == 0) { dcassert(pos == 0); - valid = f->read(buf, curRead); + valid = f->read(&buf[0], curRead); totalRead += curRead; } size_t n = len - totalProduced; size_t m = valid - pos; - more = filter(buf + pos, m, rb, n); + more = filter(&buf[pos], m, rb, n); pos += m; if(pos == valid) { valid = pos = 0; @@ -200,7 +200,7 @@ InputStream* f; Filter filter; - AutoArray<uint8_t> buf; + boost::scoped_array<uint8_t> buf; size_t pos; size_t valid; bool more; Modified: dcplusplus/trunk/dcpp/HashManager.cpp =================================================================== --- dcplusplus/trunk/dcpp/HashManager.cpp 2008-03-01 22:39:24 UTC (rev 1021) +++ dcplusplus/trunk/dcpp/HashManager.cpp 2008-03-01 23:16:58 UTC (rev 1022) @@ -153,9 +153,9 @@ try { f.setPos(ti.getIndex()); size_t datalen = TigerTree::calcBlocks(ti.getSize(), ti.getBlockSize()) * TTHValue::BYTES; - AutoArray<uint8_t> buf(datalen); - f.read((uint8_t*)buf, datalen); - tt = TigerTree(ti.getSize(), ti.getBlockSize(), buf); + boost::scoped_array<uint8_t> buf(new uint8_t[datalen]); + f.read(&buf[0], datalen); + tt = TigerTree(ti.getSize(), ti.getBlockSize(), &buf[0]); if(!(tt.getRoot() == root)) return false; } catch(const Exception&) { Modified: dcplusplus/trunk/dcpp/NmdcHub.cpp =================================================================== --- dcplusplus/trunk/dcpp/NmdcHub.cpp 2008-03-01 22:39:24 UTC (rev 1021) +++ dcplusplus/trunk/dcpp/NmdcHub.cpp 2008-03-01 23:16:58 UTC (rev 1022) @@ -807,7 +807,6 @@ void NmdcHub::search(int aSizeType, int64_t aSize, int aFileType, const string& aString, const string&) { checkstate(); - AutoArray<char> buf((char*)NULL); char c1 = (aSizeType == SearchManager::SIZE_DONTCARE) ? 'F' : 'T'; char c2 = (aSizeType == SearchManager::SIZE_ATLEAST) ? 'F' : 'T'; string tmp = ((aFileType == SearchManager::TYPE_TTH) ? "TTH:" + aString : fromUtf8(escape(aString))); @@ -815,19 +814,19 @@ while((i = tmp.find(' ')) != string::npos) { tmp[i] = '$'; } - int chars = 0; size_t BUF_SIZE; + string tmp2; if(ClientManager::getInstance()->isActive()) { string x = getLocalIp(); BUF_SIZE = x.length() + aString.length() + 64; - buf = new char[BUF_SIZE]; - chars = snprintf(buf, BUF_SIZE, "$Search %s:%d %c?%c?%s?%d?%s|", x.c_str(), (int)SearchManager::getInstance()->getPort(), c1, c2, Util::toString(aSize).c_str(), aFileType+1, tmp.c_str()); + tmp2.resize(BUF_SIZE); + tmp2.resize(snprintf(&tmp2[0], tmp2.size(), "$Search %s:%d %c?%c?%s?%d?%s|", x.c_str(), (int)SearchManager::getInstance()->getPort(), c1, c2, Util::toString(aSize).c_str(), aFileType+1, tmp.c_str())); } else { BUF_SIZE = getMyNick().length() + aString.length() + 64; - buf = new char[BUF_SIZE]; - chars = snprintf(buf, BUF_SIZE, "$Search Hub:%s %c?%c?%s?%d?%s|", fromUtf8(getMyNick()).c_str(), c1, c2, Util::toString(aSize).c_str(), aFileType+1, tmp.c_str()); + tmp2.resize(BUF_SIZE); + tmp2.resize(snprintf(&tmp2[0], tmp2.size(), "$Search Hub:%s %c?%c?%s?%d?%s|", fromUtf8(getMyNick()).c_str(), c1, c2, Util::toString(aSize).c_str(), aFileType+1, tmp.c_str())); } - send(buf, chars); + send(tmp2); } string NmdcHub::validateMessage(string tmp, bool reverse) { Modified: dcplusplus/trunk/dcpp/SearchManager.cpp =================================================================== --- dcplusplus/trunk/dcpp/SearchManager.cpp 2008-03-01 22:39:24 UTC (rev 1021) +++ dcplusplus/trunk/dcpp/SearchManager.cpp 2008-03-01 23:16:58 UTC (rev 1022) @@ -145,15 +145,15 @@ #define BUFSIZE 8192 int SearchManager::run() { - AutoArray<uint8_t> buf(BUFSIZE); + boost::scoped_array<uint8_t> buf(new uint8_t[BUFSIZE]); int len; while(true) { string remoteAddr; try { - while( (len = socket->read((uint8_t*)buf, BUFSIZE, remoteAddr)) != 0) { - onData(buf, len, remoteAddr); + while( (len = socket->read(&buf[0], BUFSIZE, remoteAddr)) != 0) { + onData(&buf[0], len, remoteAddr); } } catch(const SocketException& e) { dcdebug("SearchManager::run Error: %s\n", e.getError().c_str()); Modified: dcplusplus/trunk/dcpp/ShareManager.cpp =================================================================== --- dcplusplus/trunk/dcpp/ShareManager.cpp 2008-03-01 22:39:24 UTC (rev 1021) +++ dcplusplus/trunk/dcpp/ShareManager.cpp 2008-03-01 23:16:58 UTC (rev 1022) @@ -363,12 +363,12 @@ dcpp::File ff(Util::getConfigPath() + "files.xml.bz2", dcpp::File::READ, dcpp::File::OPEN); FilteredInputStream<UnBZFilter, false> f(&ff); const size_t BUF_SIZE = 64*1024; - AutoArray<char> buf(BUF_SIZE); + boost::scoped_array<char> buf(new char[BUF_SIZE]); size_t len; for(;;) { size_t n = BUF_SIZE; - len = f.read(buf, n); - txt.append(buf, len); + len = f.read(&buf[0], n); + txt.append(&buf[0], len); if(len < BUF_SIZE) break; } Modified: dcplusplus/trunk/dcpp/Streams.h =================================================================== --- dcplusplus/trunk/dcpp/Streams.h 2008-03-01 22:39:24 UTC (rev 1021) +++ dcplusplus/trunk/dcpp/Streams.h 2008-03-01 23:16:58 UTC (rev 1022) @@ -127,7 +127,7 @@ public: using OutputStream::write; - BufferedOutputStream(OutputStream* aStream, size_t aBufSize = SETTING(BUFFER_SIZE) * 1024) : s(aStream), pos(0), bufSize(aBufSize), buf(aBufSize) { } + BufferedOutputStream(OutputStream* aStream, size_t aBufSize = SETTING(BUFFER_SIZE) * 1024) : s(aStream), pos(0), buf(aBufSize) { } virtual ~BufferedOutputStream() throw() { try { // We must do this in order not to lose bytes when a download @@ -140,7 +140,7 @@ virtual size_t flush() throw(Exception) { if(pos > 0) - s->write(buf, pos); + s->write(&buf[0], pos); pos = 0; s->flush(); return 0; @@ -149,18 +149,19 @@ virtual size_t write(const void* wbuf, size_t len) throw(Exception) { uint8_t* b = (uint8_t*)wbuf; size_t l2 = len; + size_t bufSize = buf.size(); while(len > 0) { if(pos == 0 && len >= bufSize) { s->write(b, len); break; } else { size_t n = min(bufSize - pos, len); - memcpy(buf + pos, b, n); + memcpy(&buf[pos], b, n); b += n; pos += n; len -= n; if(pos == bufSize) { - s->write(buf, bufSize); + s->write(&buf[0], bufSize); pos = 0; } } @@ -170,8 +171,7 @@ private: OutputStream* s; size_t pos; - size_t bufSize; - AutoArray<uint8_t> buf; + ByteVector buf; }; class StringOutputStream : public OutputStream { Modified: dcplusplus/trunk/dcpp/Util.cpp =================================================================== --- dcplusplus/trunk/dcpp/Util.cpp 2008-03-01 22:39:24 UTC (rev 1021) +++ dcplusplus/trunk/dcpp/Util.cpp 2008-03-01 23:16:58 UTC (rev 1022) @@ -669,17 +669,18 @@ tmp[1] = tmp[2] = tmp[3] = 0; StringMap sm; - AutoArray<char> buf(1024); + static const size_t BUF_SIZE = 1024; + boost::scoped_array<char> buf(new char[BUF_SIZE]); for(size_t i = 0; i < strlen(codes); ++i) { tmp[1] = codes[i]; tmp[2] = 0; - strftime(buf, 1024-1, tmp, t); - sm[tmp] = buf; + strftime(&buf[0], BUF_SIZE-1, tmp, t); + sm[tmp] = &buf[0]; tmp[1] = '#'; tmp[2] = codes[i]; - strftime(buf, 1024-1, tmp, t); - sm[tmp] = buf; + strftime(&buf[0], BUF_SIZE-1, tmp, t); + sm[tmp] = &buf[0]; } for(StringMapIter i = sm.begin(); i != sm.end(); ++i) { @@ -701,13 +702,15 @@ return Util::emptyString; } #if _WIN32 - AutoArray<TCHAR> buf(bufsize); + tstring buf(bufsize, 0); - if(!_tcsftime(buf, bufsize-1, Text::toT(msg).c_str(), loc)) { + buf.resize(_tcsftime(&buf[0], buf.size()-1, Text::toT(msg).c_str(), loc)); + + if(buf.empty()) { return fixedftime(msg, loc); } - return Text::fromT(tstring(buf)); + return Text::fromT(buf); #else // will this give wide representations for %a and %A? // surely win32 can't have a leg up on linux/unixen in this area. - Todd Modified: dcplusplus/trunk/dcpp/Util.h =================================================================== --- dcplusplus/trunk/dcpp/Util.h 2008-03-01 22:39:24 UTC (rev 1021) +++ dcplusplus/trunk/dcpp/Util.h 2008-03-01 23:16:58 UTC (rev 1022) @@ -87,22 +87,6 @@ template<typename T1> inline int compare(const T1& v1, const T1& v2) { return (v1 < v2) ? -1 : ((v1 == v2) ? 0 : 1); } -template<typename T> -class AutoArray { - typedef T* TPtr; -public: - explicit AutoArray(TPtr t) : p(t) { } - explicit AutoArray(size_t size) : p(new T[size]) { } - ~AutoArray() { delete[] p; } - operator TPtr() { return p; } - AutoArray& operator=(TPtr t) { delete[] p; p = t; return *this; } -private: - AutoArray(const AutoArray&); - AutoArray& operator=(const AutoArray&); - - TPtr p; -}; - class Util { public: Modified: dcplusplus/trunk/dcpp/stdinc.h =================================================================== --- dcplusplus/trunk/dcpp/stdinc.h 2008-03-01 22:39:24 UTC (rev 1021) +++ dcplusplus/trunk/dcpp/stdinc.h 2008-03-01 23:16:58 UTC (rev 1022) @@ -120,6 +120,7 @@ #include <libintl.h> #include <boost/format.hpp> +#include <boost/scoped_array.hpp> #ifdef _STLPORT_VERSION Modified: dcplusplus/trunk/win32/PropPage.cpp =================================================================== --- dcplusplus/trunk/win32/PropPage.cpp 2008-03-01 22:39:24 UTC (rev 1021) +++ dcplusplus/trunk/win32/PropPage.cpp 2008-03-01 23:16:58 UTC (rev 1022) @@ -92,22 +92,24 @@ SettingsManager* settings = SettingsManager::getInstance(); - AutoArray<TCHAR> buf(SETTINGS_BUF_LEN); + tstring buf; for(Item const* i = items; i->type != T_END; i++) { switch(i->type) { case T_STR: { - ::GetDlgItemText(page, i->itemID, buf, SETTINGS_BUF_LEN); - settings->set((SettingsManager::StrSetting)i->setting, Text::fromT(tstring(buf))); + buf.resize(SETTINGS_BUF_LEN); + buf.resize(::GetDlgItemText(page, i->itemID, &buf[0], buf.size())); + settings->set((SettingsManager::StrSetting)i->setting, Text::fromT(buf)); break; } case T_INT: { - ::GetDlgItemText(page, i->itemID, buf, SETTINGS_BUF_LEN); - settings->set((SettingsManager::IntSetting)i->setting, Text::fromT(tstring(buf))); + buf.resize(SETTINGS_BUF_LEN); + buf.resize(::GetDlgItemText(page, i->itemID, &buf[0], buf.size())); + settings->set((SettingsManager::IntSetting)i->setting, Text::fromT(buf)); break; } case T_BOOL: @@ -120,7 +122,7 @@ } } - if(listItems != NULL) { + if(listItems) { int i; for(i = 0; listItems[i].setting != 0; i++) { settings->set(SettingsManager::IntSetting(listItems[i].setting), ListView_GetCheckState(list, i) > 0); Modified: dcplusplus/trunk/win32/WinUtil.cpp =================================================================== --- dcplusplus/trunk/win32/WinUtil.cpp 2008-03-01 22:39:24 UTC (rev 1021) +++ dcplusplus/trunk/win32/WinUtil.cpp 2008-03-01 23:16:58 UTC (rev 1022) @@ -904,9 +904,9 @@ STARTUPINFO si = { sizeof(si), 0 }; PROCESS_INFORMATION pi = { 0 }; - AutoArray<TCHAR> buf(cmdLine.length() + 1); - _tcscpy(buf, cmdLine.c_str()); - if(::CreateProcess(cmd.c_str(), buf, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) { + boost::scoped_ptr<TCHAR> buf(new TCHAR[cmdLine.length() + 1]); + _tcscpy(&buf[0], cmdLine.c_str()); + if(::CreateProcess(cmd.c_str(), &buf[0], NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) { ::CloseHandle(pi.hThread); ::CloseHandle(pi.hProcess); return; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |