Skip to content

Commit 8555e0f

Browse files
committed
8319318: bufferedStream fixed case can be removed
Reviewed-by: dholmes
1 parent 73c5f60 commit 8555e0f

File tree

3 files changed

+26
-64
lines changed

3 files changed

+26
-64
lines changed

src/hotspot/share/utilities/ostream.cpp

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -991,16 +991,6 @@ bufferedStream::bufferedStream(size_t initial_size, size_t bufmax) : outputStrea
991991
buffer_length = initial_size;
992992
buffer = NEW_C_HEAP_ARRAY(char, buffer_length, mtInternal);
993993
buffer_pos = 0;
994-
buffer_fixed = false;
995-
buffer_max = bufmax;
996-
truncated = false;
997-
}
998-
999-
bufferedStream::bufferedStream(char* fixed_buffer, size_t fixed_buffer_size, size_t bufmax) : outputStream() {
1000-
buffer_length = fixed_buffer_size;
1001-
buffer = fixed_buffer;
1002-
buffer_pos = 0;
1003-
buffer_fixed = true;
1004994
buffer_max = bufmax;
1005995
truncated = false;
1006996
}
@@ -1017,39 +1007,33 @@ void bufferedStream::write(const char* s, size_t len) {
10171007

10181008
size_t end = buffer_pos + len;
10191009
if (end >= buffer_length) {
1020-
if (buffer_fixed) {
1021-
// if buffer cannot resize, silently truncate
1022-
len = buffer_length - buffer_pos - 1;
1023-
truncated = true;
1024-
} else {
1025-
// For small overruns, double the buffer. For larger ones,
1026-
// increase to the requested size.
1027-
if (end < buffer_length * 2) {
1028-
end = buffer_length * 2;
1029-
}
1030-
// Impose a cap beyond which the buffer cannot grow - a size which
1031-
// in all probability indicates a real error, e.g. faulty printing
1032-
// code looping, while not affecting cases of just-very-large-but-its-normal
1033-
// output.
1034-
const size_t reasonable_cap = MAX2(100 * M, buffer_max * 2);
1035-
if (end > reasonable_cap) {
1036-
// In debug VM, assert right away.
1037-
assert(false, "Exceeded max buffer size for this string.");
1038-
// Release VM: silently truncate. We do this since these kind of errors
1039-
// are both difficult to predict with testing (depending on logging content)
1040-
// and usually not serious enough to kill a production VM for it.
1041-
end = reasonable_cap;
1042-
size_t remaining = end - buffer_pos;
1043-
if (len >= remaining) {
1044-
len = remaining - 1;
1045-
truncated = true;
1046-
}
1047-
}
1048-
if (buffer_length < end) {
1049-
buffer = REALLOC_C_HEAP_ARRAY(char, buffer, end, mtInternal);
1050-
buffer_length = end;
1010+
// For small overruns, double the buffer. For larger ones,
1011+
// increase to the requested size.
1012+
if (end < buffer_length * 2) {
1013+
end = buffer_length * 2;
1014+
}
1015+
// Impose a cap beyond which the buffer cannot grow - a size which
1016+
// in all probability indicates a real error, e.g. faulty printing
1017+
// code looping, while not affecting cases of just-very-large-but-its-normal
1018+
// output.
1019+
const size_t reasonable_cap = MAX2(100 * M, buffer_max * 2);
1020+
if (end > reasonable_cap) {
1021+
// In debug VM, assert right away.
1022+
assert(false, "Exceeded max buffer size for this string.");
1023+
// Release VM: silently truncate. We do this since these kind of errors
1024+
// are both difficult to predict with testing (depending on logging content)
1025+
// and usually not serious enough to kill a production VM for it.
1026+
end = reasonable_cap;
1027+
size_t remaining = end - buffer_pos;
1028+
if (len >= remaining) {
1029+
len = remaining - 1;
1030+
truncated = true;
10511031
}
10521032
}
1033+
if (buffer_length < end) {
1034+
buffer = REALLOC_C_HEAP_ARRAY(char, buffer, end, mtInternal);
1035+
buffer_length = end;
1036+
}
10531037
}
10541038
if (len > 0) {
10551039
memcpy(buffer + buffer_pos, s, len);
@@ -1066,9 +1050,7 @@ char* bufferedStream::as_string() {
10661050
}
10671051

10681052
bufferedStream::~bufferedStream() {
1069-
if (!buffer_fixed) {
1070-
FREE_C_HEAP_ARRAY(char, buffer);
1071-
}
1053+
FREE_C_HEAP_ARRAY(char, buffer);
10721054
}
10731055

10741056
#ifndef PRODUCT

src/hotspot/share/utilities/ostream.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,9 @@ class bufferedStream : public outputStream {
293293
size_t buffer_pos;
294294
size_t buffer_max;
295295
size_t buffer_length;
296-
bool buffer_fixed;
297296
bool truncated;
298297
public:
299298
bufferedStream(size_t initial_bufsize = 256, size_t bufmax = 1024*1024*10);
300-
bufferedStream(char* fixed_buffer, size_t fixed_buffer_size, size_t bufmax = 1024*1024*10);
301299
~bufferedStream();
302300
virtual void write(const char* c, size_t len);
303301
size_t size() { return buffer_pos; }

test/hotspot/gtest/utilities/test_ostream.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -93,24 +93,6 @@ TEST_VM(ostream, stringStream_static) {
9393
ASSERT_EQ(*canary_at, 'X'); // canary
9494
}
9595

96-
TEST_VM(ostream, bufferedStream_static) {
97-
char buf[100 + 1];
98-
char* canary_at = buf + sizeof(buf) - 1;
99-
*canary_at = 'X';
100-
size_t stream_buf_size = sizeof(buf) - 1;
101-
bufferedStream bs(buf, stream_buf_size);
102-
size_t written = 0;
103-
for (int i = 0; i < 100; i ++) {
104-
written += print_lorem(&bs);
105-
if (written < stream_buf_size) {
106-
ASSERT_EQ(bs.size(), written);
107-
} else {
108-
ASSERT_EQ(bs.size(), stream_buf_size - 1);
109-
}
110-
}
111-
ASSERT_EQ(*canary_at, 'X'); // canary
112-
}
113-
11496
TEST_VM(ostream, bufferedStream_dynamic_small) {
11597
bufferedStream bs(1); // small to excercise realloc.
11698
size_t written = 0;

0 commit comments

Comments
 (0)