diff options
author | dwight <dwight@10gen.com> | 2010-08-22 13:54:03 -0400 |
---|---|---|
committer | Eliot Horowitz <eliot@10gen.com> | 2010-09-15 14:40:43 -0400 |
commit | b6f400ef69dc683289edd03340f6794088b91507 (patch) | |
tree | fc3ed68c89d91dfcbaa3a87ae389fc5319985dd0 | |
parent | bb7759ebf8b8bf14757218981d08cdb0973b81b6 (diff) | |
download | mongo-b6f400ef69dc683289edd03340f6794088b91507.tar.gz |
dont heap allocate in rawOut function when avoidable
-rw-r--r-- | util/log.h | 68 | ||||
-rw-r--r-- | util/util.cpp | 11 |
2 files changed, 43 insertions, 36 deletions
diff --git a/util/log.h b/util/log.h index e0147a02761..87b7b7eb1db 100644 --- a/util/log.h +++ b/util/log.h @@ -329,40 +329,40 @@ namespace mongo { s << "errno:" << x << ' '; #if defined(_WIN32) - LPTSTR errorText = NULL;
- FormatMessage(
- FORMAT_MESSAGE_FROM_SYSTEM
- |FORMAT_MESSAGE_ALLOCATE_BUFFER
- |FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL,
- x, 0,
- (LPTSTR) &errorText, // output
- 0, // minimum size for output buffer
- NULL);
- if( errorText ) {
- string x = toUtf8String(errorText);
- for( string::iterator i = x.begin(); i != x.end(); i++ ) {
- if( *i == '\n' || *i == '\r' )
- break;
- s << *i;
- }
- LocalFree(errorText);
- }
- else
- s << strerror(x);
- /*
- DWORD n = FormatMessage(
- FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL, x,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
- (LPTSTR) &lpMsgBuf, 0, NULL);
- */
-#else
- s << strerror(x);
-#endif
- return s.str();
+ LPTSTR errorText = NULL; + FormatMessage( + FORMAT_MESSAGE_FROM_SYSTEM + |FORMAT_MESSAGE_ALLOCATE_BUFFER + |FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + x, 0, + (LPTSTR) &errorText, // output + 0, // minimum size for output buffer + NULL); + if( errorText ) { + string x = toUtf8String(errorText); + for( string::iterator i = x.begin(); i != x.end(); i++ ) { + if( *i == '\n' || *i == '\r' ) + break; + s << *i; + } + LocalFree(errorText); + } + else + s << strerror(x); + /* + DWORD n = FormatMessage( + FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, x, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPTSTR) &lpMsgBuf, 0, NULL); + */ +#else + s << strerror(x); +#endif + return s.str(); } /** output the error # and error message with prefix. diff --git a/util/util.cpp b/util/util.cpp index 02abfa902ea..60127b63e2b 100644 --- a/util/util.cpp +++ b/util/util.cpp @@ -160,8 +160,12 @@ namespace mongo { void rawOut( const string &s ) { if( s.empty() ) return; - boost::scoped_array<char> buf_holder(new char[32 + s.size()]); - char * buf = buf_holder.get(); + char smallbuf[512]; + char *buf = smallbuf; + if( s.size() + 20 + 10 > 512 ) { + // don't use the heap unless the output is really big. that way rawOut("out of memory") is ok. + buf = new char[32 + s.size()]; + } time_t_to_String( time(0) , buf ); buf[20] = ' '; @@ -170,6 +174,9 @@ namespace mongo { buf[21+s.size()+1] = 0; Logstream::logLockless( buf ); + + if( buf != smallbuf ) + delete[] buf; } ostream& operator<<( ostream &s, const ThreadSafeString &o ){ |