8224193: stringStream should not use Resouce Area

Reviewed-by: goetz, coleenp, dholmes
This commit is contained in:
Thomas Stuefe 2019-05-22 09:33:22 +02:00
parent 2257bae7a2
commit 7a0ac25b37
3 changed files with 84 additions and 14 deletions
src/hotspot/share/utilities
test/hotspot/gtest/utilities

@ -309,10 +309,9 @@ void outputStream::print_data(void* data, size_t len, bool with_ascii) {
stringStream::stringStream(size_t initial_size) : outputStream() {
buffer_length = initial_size;
buffer = NEW_RESOURCE_ARRAY(char, buffer_length);
buffer = NEW_C_HEAP_ARRAY(char, buffer_length, mtInternal);
buffer_pos = 0;
buffer_fixed = false;
DEBUG_ONLY(rm = Thread::current()->current_resource_mark();)
}
// useful for output to fixed chunks of memory, such as performance counters
@ -337,15 +336,7 @@ void stringStream::write(const char* s, size_t len) {
if (end < buffer_length * 2) {
end = buffer_length * 2;
}
char* oldbuf = buffer;
assert(rm == NULL || Thread::current()->current_resource_mark() == rm,
"StringStream is re-allocated with a different ResourceMark. Current: "
PTR_FORMAT " original: " PTR_FORMAT,
p2i(Thread::current()->current_resource_mark()), p2i(rm));
buffer = NEW_RESOURCE_ARRAY(char, end);
if (buffer_pos > 0) {
memcpy(buffer, oldbuf, buffer_pos);
}
buffer = REALLOC_C_HEAP_ARRAY(char, buffer, end, mtInternal);
buffer_length = end;
}
}
@ -370,7 +361,11 @@ char* stringStream::as_string() {
return copy;
}
stringStream::~stringStream() {}
stringStream::~stringStream() {
if (buffer_fixed == false && buffer != NULL) {
FREE_C_HEAP_ARRAY(char, buffer);
}
}
xmlStream* xtty;
outputStream* tty;

@ -190,19 +190,25 @@ class ttyUnlocker: StackObj {
}
};
// for writing to strings; buffer will expand automatically
// for writing to strings; buffer will expand automatically.
// Buffer will always be zero-terminated.
class stringStream : public outputStream {
protected:
char* buffer;
size_t buffer_pos;
size_t buffer_length;
bool buffer_fixed;
DEBUG_ONLY(ResourceMark* rm;)
public:
// Create a stringStream using an internal buffer of initially initial_bufsize size;
// will be enlarged on demand. There is no maximum cap.
stringStream(size_t initial_bufsize = 256);
// Creates a stringStream using a caller-provided buffer. Will truncate silently if
// it overflows.
stringStream(char* fixed_buffer, size_t fixed_buffer_size);
~stringStream();
virtual void write(const char* c, size_t len);
// Return number of characters written into buffer, excluding terminating zero and
// subject to truncation in static buffer mode.
size_t size() { return buffer_pos; }
const char* base() { return buffer; }
void reset() { buffer_pos = 0; _precount = 0; _position = 0; }

@ -0,0 +1,69 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019 SAP SE. 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.
*
*/
#include "precompiled.hpp"
#include "memory/resourceArea.hpp"
#include "runtime/os.hpp"
#include "utilities/globalDefinitions.hpp"
#include "utilities/ostream.hpp"
#include "unittest.hpp"
static size_t print_lorem(outputStream* st, bool short_len) {
// Create a ResourceMark just to make sure the stream does not use ResourceArea
ResourceMark rm;
static const char* const lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, "
"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Lacinia at quis "
"risus sed vulputate odio ut enim blandit. Amet risus nullam eget felis eget. Viverra "
"orci sagittis eu volutpat odio facilisis mauris sit. Erat velit scelerisque in dictum non.";
static const size_t len_lorem = strlen(lorem);
size_t len;
if (short_len) {
len = os::random() % 10;
} else {
len = MAX2(1, (int)(os::random() % len_lorem));
}
st->write(lorem, len);
return len;
}
static void do_test_stringStream_dynamic_realloc(bool short_len) {
stringStream ss(2); // small buffer to force lots of reallocations.
size_t written = 0;
for (int i = 0; i < 1000; i ++) {
written += print_lorem(&ss, short_len);
ASSERT_EQ(ss.size(), written);
// Internal buffer should always be zero-terminated.
ASSERT_EQ(ss.base()[ss.size()], '\0');
}
}
TEST_VM(ostream, stringStream_dynamic_realloc_1) {
do_test_stringStream_dynamic_realloc(false);
}
TEST_VM(ostream, stringStream_dynamic_realloc_2) {
do_test_stringStream_dynamic_realloc(true);
}