From 4707b610fca45c0762e25994594fc58557945a02 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Thu, 18 Nov 2010 16:08:56 +0000 Subject: COMMON: Fix incorrect use of assert() macro The assert() macro may be compiled to be empty. In that case, its arguments are *NOT* evaluated. Hence, things like assert(doSomething()) must not be used whenever doSomething() has important side effects. Also document BufferedWriteStream::flushBuffer() and explain why it exists parallel to BufferedWriteStream::flush(). svn-id: r54322 --- common/stream.cpp | 17 +++++++---------- common/stream.h | 14 +++++++++++--- 2 files changed, 18 insertions(+), 13 deletions(-) (limited to 'common') diff --git a/common/stream.cpp b/common/stream.cpp index 9f8f6127f1..0e8e578e86 100644 --- a/common/stream.cpp +++ b/common/stream.cpp @@ -331,7 +331,8 @@ BufferedWriteStream::BufferedWriteStream(WriteStream *parentStream, uint32 bufSi } BufferedWriteStream::~BufferedWriteStream() { - assert(flush()); + const bool flushResult = flushBuffer(); + assert(flushResult); if (_disposeParentStream) delete _parentStream; @@ -345,20 +346,20 @@ uint32 BufferedWriteStream::write(const void *dataPtr, uint32 dataSize) { memcpy(_buf + _pos, dataPtr, dataSize); _pos += dataSize; } else if (_bufSize >= dataSize) { // check if we can flush the buffer and load the data - // flush the buffer - assert(flushBuffer()); + const bool flushResult = flushBuffer(); + assert(flushResult); memcpy(_buf, dataPtr, dataSize); _pos += dataSize; } else { // too big for our buffer - // flush the buffer - assert(flushBuffer()); + const bool flushResult = flushBuffer(); + assert(flushResult); return _parentStream->write(dataPtr, dataSize); } return dataSize; } bool BufferedWriteStream::flushBuffer() { - uint32 bytesToWrite = _pos; + const uint32 bytesToWrite = _pos; if (bytesToWrite) { _pos = 0; @@ -368,10 +369,6 @@ bool BufferedWriteStream::flushBuffer() { return true; } -bool BufferedWriteStream::flush() { - return flushBuffer(); -} - bool MemoryWriteStreamDynamic::seek(int32 offs, int whence) { // Pre-Condition assert(_pos <= _size); diff --git a/common/stream.h b/common/stream.h index c6605cb42d..d497cabe2b 100644 --- a/common/stream.h +++ b/common/stream.h @@ -534,15 +534,23 @@ protected: DisposeAfterUse::Flag _disposeParentStream; byte *_buf; uint32 _pos; - uint32 _bufSize; - bool flushBuffer(); // write out the data in the buffer + const uint32 _bufSize; + + /** + * Write out the data in the buffer. + * + * @note This method is identical to flush() (which actually is + * implemented by calling this method), except that it is not + * virtual, hence there is less overhead calling it. + */ + bool flushBuffer(); public: BufferedWriteStream(WriteStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO); virtual ~BufferedWriteStream(); virtual uint32 write(const void *dataPtr, uint32 dataSize); - virtual bool flush(); + virtual bool flush() { return flushBuffer(); } }; /** -- cgit v1.2.3