< prev index next >

src/share/vm/logging/logStream.cpp

Print this page
rev 13265 : imported patch 8181917-refactor-ul-logstream
rev 13266 : [mq]: 8181917-refactor-ul-logstream-delta-to-next


  29 LogStream::LineBuffer::LineBuffer()
  30  : _buf(_smallbuf), _cap(sizeof(_smallbuf)), _pos(0)
  31 {
  32   _buf[0] = '\0';
  33 }
  34 
  35 LogStream::LineBuffer::~LineBuffer() {
  36   assert(_pos == 0, "still outstanding bytes in the line buffer");
  37   if (_buf != _smallbuf) {
  38     os::free(_buf);
  39   }
  40 }
  41 
  42 // try_ensure_cap tries to enlarge the capacity of the internal buffer
  43 // to the given atleast value. May fail if either OOM happens or atleast
  44 // is larger than a reasonable max of 1 M. Caller must not assume
  45 // capacity without checking.
  46 void LogStream::LineBuffer::try_ensure_cap(size_t atleast) {
  47   assert(_cap >= sizeof(_smallbuf), "sanity");
  48   if (_cap < atleast) {

  49     const size_t reasonable_max = 1 * M;
  50     size_t newcap = align_size_up(atleast + 64, 64);
  51     assert(_cap <= reasonable_max, "sanity");
  52     // Cap out at a reasonable max to prevent runaway leaks.




  53     if (newcap > reasonable_max) {


  54       newcap = reasonable_max;
  55     }
  56     
  57     char* const newbuf = (char*) os::malloc(newcap, mtLogging);
  58     if (newbuf == NULL) { // OOM. Leave object unchanged.
  59       return;
  60     }
  61     if (_pos > 0) { // preserve old content
  62       memcpy(newbuf, _buf, _pos + 1); // ..including trailing zero
  63     }
  64     if (_buf != _smallbuf) {
  65       os::free(_buf);
  66     }
  67     _buf = newbuf;
  68     _cap = newcap;
  69   }
  70   assert(_cap >= atleast, "sanity");
  71 }
  72 
  73 void LogStream::LineBuffer::append(const char* s, size_t len) {




  29 LogStream::LineBuffer::LineBuffer()
  30  : _buf(_smallbuf), _cap(sizeof(_smallbuf)), _pos(0)
  31 {
  32   _buf[0] = '\0';
  33 }
  34 
  35 LogStream::LineBuffer::~LineBuffer() {
  36   assert(_pos == 0, "still outstanding bytes in the line buffer");
  37   if (_buf != _smallbuf) {
  38     os::free(_buf);
  39   }
  40 }
  41 
  42 // try_ensure_cap tries to enlarge the capacity of the internal buffer
  43 // to the given atleast value. May fail if either OOM happens or atleast
  44 // is larger than a reasonable max of 1 M. Caller must not assume
  45 // capacity without checking.
  46 void LogStream::LineBuffer::try_ensure_cap(size_t atleast) {
  47   assert(_cap >= sizeof(_smallbuf), "sanity");
  48   if (_cap < atleast) {
  49     // Cap out at a reasonable max to prevent runaway leaks.
  50     const size_t reasonable_max = 1 * M;

  51     assert(_cap <= reasonable_max, "sanity");
  52     if (_cap == reasonable_max) {
  53       return;
  54     }
  55 
  56     size_t newcap = align_up(atleast + 64, 64);
  57     if (newcap > reasonable_max) {
  58       log_info(logging)("Suspiciously long log line: \"%.100s%s",
  59               _buf, (_pos >= 100 ? "..." : ""));
  60       newcap = reasonable_max;
  61     }
  62 
  63     char* const newbuf = (char*) os::malloc(newcap, mtLogging);
  64     if (newbuf == NULL) { // OOM. Leave object unchanged.
  65       return;
  66     }
  67     if (_pos > 0) { // preserve old content
  68       memcpy(newbuf, _buf, _pos + 1); // ..including trailing zero
  69     }
  70     if (_buf != _smallbuf) {
  71       os::free(_buf);
  72     }
  73     _buf = newbuf;
  74     _cap = newcap;
  75   }
  76   assert(_cap >= atleast, "sanity");
  77 }
  78 
  79 void LogStream::LineBuffer::append(const char* s, size_t len) {


< prev index next >