< prev index next >

src/share/vm/logging/logStream.cpp

Print this page
rev 13106 : imported patch 8181917-refactor-ul-logstream-alt1-api-changes
rev 13107 : imported patch 8181917-refactor-ul-logstream-alt1-logstream-optimization

@@ -24,17 +24,60 @@
 
 #include "precompiled.hpp"
 #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);
 }
 
< prev index next >