diff options
author | Harald Fernengel <harald@trolltech.com> | 2009-08-07 13:08:09 (GMT) |
---|---|---|
committer | Harald Fernengel <harald@trolltech.com> | 2009-08-07 13:10:36 (GMT) |
commit | dfa284220498a1e32ab3133f203bcb41cfa136b7 (patch) | |
tree | e6e8a6cae4b2195313258c5001100e2557c6463e /src/testlib/qabstracttestlogger_p.h | |
parent | 658c30c214070e8ff05ddaf1cb7b161c1b73f5ce (diff) | |
download | Qt-dfa284220498a1e32ab3133f203bcb41cfa136b7.zip Qt-dfa284220498a1e32ab3133f203bcb41cfa136b7.tar.gz Qt-dfa284220498a1e32ab3133f203bcb41cfa136b7.tar.bz2 |
Refactor QTestCharBuffer a bit
Use a static buffer for small strings, and making it oom safe.
We can now see messages up to 512 bytes even if we run out of memory
(important for OOM tests). Also, testlogging (< 512 bytes per line)
should again work without a single allocation.
Reviewed-By: Rohan McGovern <rohan.mcgovern@nokia.com>
Diffstat (limited to 'src/testlib/qabstracttestlogger_p.h')
-rw-r--r-- | src/testlib/qabstracttestlogger_p.h | 52 |
1 files changed, 42 insertions, 10 deletions
diff --git a/src/testlib/qabstracttestlogger_p.h b/src/testlib/qabstracttestlogger_p.h index 588184e..1834086 100644 --- a/src/testlib/qabstracttestlogger_p.h +++ b/src/testlib/qabstracttestlogger_p.h @@ -101,27 +101,26 @@ public: struct QTestCharBuffer { - inline QTestCharBuffer() - : buf(0) - {} + enum { InitialSize = 512 }; - inline ~QTestCharBuffer() + inline QTestCharBuffer() + : _size(InitialSize), buf(staticBuf) { - delete[] buf; - buf = 0; + staticBuf[0] = '\0'; } - inline operator void*() + inline ~QTestCharBuffer() { - return buf; + if (buf != staticBuf) + qFree(buf); } - inline operator char*() + inline char *data() { return buf; } - inline operator char**() + inline char **buffer() { return &buf; } @@ -131,10 +130,43 @@ struct QTestCharBuffer return buf; } + inline int size() const + { + return _size; + } + + inline bool reset(int newSize) + { + char *newBuf = 0; + if (buf == staticBuf) { + // if we point to our internal buffer, we need to malloc first + newBuf = reinterpret_cast<char *>(qMalloc(newSize)); + } else { + // if we already malloc'ed, just realloc + newBuf = reinterpret_cast<char *>(qRealloc(buf, newSize)); + } + + // if the allocation went wrong (newBuf == 0), we leave the object as is + if (!newBuf) + return false; + + _size = newSize; + buf = newBuf; + return true; + } + private: + int _size; char* buf; + char staticBuf[InitialSize]; }; +namespace QTest +{ + int qt_asprintf(QTestCharBuffer *buf, const char *format, ...); +} + + QT_END_NAMESPACE #endif |