--- old/src/share/vm/logging/logStream.cpp 2017-06-18 08:42:32.367799700 +0200 +++ new/src/share/vm/logging/logStream.cpp 2017-06-18 08:42:31.618018600 +0200 @@ -26,14 +26,57 @@ #include "logging/log.hpp" #include "logging/logStream.hpp" +LogStream::LineBuffer::LineBuffer() + : _buf(_smallbuf), _cap(sizeof(_smallbuf)), _pos(0) +{ + _buf[0] = '\0'; +} + +LogStream::LineBuffer::~LineBuffer() { + assert(_pos == 0, "still outstanding bytes in the line buffer"); + if (_buf != _smallbuf) { + os::free(_buf); + } +} + +void LogStream::LineBuffer::ensure_cap(size_t atleast) { + assert(_cap >= sizeof(_smallbuf), "sanity"); + if (_cap < atleast) { + const size_t newcap = MAX2(_cap * 2, atleast * 2); + char* const newbuf = (char*) os::malloc(newcap, mtLogging); + if (_pos > 0) { // preserve old content + memcpy(newbuf, _buf, _pos + 1); // ..including trailing zero + } + if (_buf != _smallbuf) { + os::free(_buf); + } + _buf = newbuf; + _cap = newcap; + } + assert(_cap >= atleast, "sanity"); +} + +void LogStream::LineBuffer::append(const char* s, size_t len) { + assert(_buf[_pos] == '\0', "sanity"); + const size_t remaining = _cap - _pos; + ensure_cap(remaining + len + 1); + memcpy(_buf + _pos, s, len); + _pos += len; + _buf[_pos] = '\0'; +} + +void LogStream::LineBuffer::reset() { + _pos = 0; + _buf[_pos] = '\0'; +} + void LogStream::write(const char* s, size_t len) { if (len > 0 && s[len - 1] == '\n') { - _current_line.write(s, len - 1); - _current_line.write("\0", 1); - _log_handle.print("%s", _current_line.base()); + _current_line.append(s, len - 1); // omit the newline. + _log_handle.print("%s", _current_line.ptr()); _current_line.reset(); } else { - _current_line.write(s, len); + _current_line.append(s, len); } update_position(s, len); } --- old/src/share/vm/logging/logStream.hpp 2017-06-18 08:42:39.220358000 +0200 +++ new/src/share/vm/logging/logStream.hpp 2017-06-18 08:42:38.344294200 +0200 @@ -32,9 +32,31 @@ class LogStream : public outputStream { - bufferedStream _current_line; + + // Helper class, maintains the line buffer. For small line lengths, + // we avoid malloc and use a fixed sized member char array. If LogStream + // is allocated on the stack, this means small lines are assembled + // directly on the stack. + class LineBuffer { + char _smallbuf[128]; + char* _buf; + size_t _cap; + size_t _pos; + void ensure_cap(size_t cap); + public: + LineBuffer(); + ~LineBuffer(); + const char* ptr() const { return _buf; } + void append(const char* s, size_t len); + void reset(); + }; + LineBuffer _current_line; LogTargetHandle _log_handle; + // Prevent operator new for LogStream. +// static void* operator new (size_t); +// static void* operator new[] (size_t); + public: // Constructor to support creation from a LogTarget instance. // @@ -65,10 +87,6 @@ // LogStreamBase(level, tageset); LogStream(LogLevelType level, LogTagSet* tagset) : _log_handle(level, tagset) {} - ~LogStream() { - guarantee(_current_line.size() == 0, "Buffer not flushed. Missing call to print_cr()?"); - } - public: void write(const char* s, size_t len); };