/* * Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. *
*/
LogStreamImplBase::LineBuffer::~LineBuffer() {
assert(_pos == 0, "still outstanding bytes in the line buffer"); if (_buf != _smallbuf) {
os::free(_buf);
}
}
// try_ensure_cap tries to enlarge the capacity of the internal buffer // to the given atleast value. May fail if either OOM happens or atleast // is larger than a reasonable max of 1 M. // Returns whether the capacity is at least atleast bytes. bool LogStreamImplBase::LineBuffer::try_ensure_cap(size_t atleast) { // Cap out at a reasonable max to prevent runaway leaks. const size_t reasonable_max = 1 * M;
char* const newbuf = (char*)os::malloc(newcap, mtLogging); if (newbuf == NULL) { // OOM. Leave object unchanged. returnfalse;
} if (_pos > 0) { // preserve old content
memcpy(newbuf, _buf, _pos + 1); // ..including trailing zero
} if (_buf != _smallbuf) {
os::free(_buf);
}
_buf = newbuf;
_cap = newcap;
return _cap >= atleast;
}
void LogStreamImplBase::LineBuffer::append(constchar* s, size_t len) {
assert(_buf[_pos] == '\0', "sanity");
assert(_pos < _cap, "sanity"); const size_t minimum_capacity_needed = _pos + len + 1; constbool has_capacity = try_ensure_cap(minimum_capacity_needed); // try_ensure_cap may not have enlarged the capacity to the full requested // extent or may have not worked at all. In that case, just gracefully work // with what we have already; just truncate if necessary. if (!has_capacity) {
len = _cap - _pos - 1; if (len == 0) { return;
}
}
memcpy(_buf + _pos, s, len);
_pos += len;
_buf[_pos] = '\0';
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.