Skip to content

Commit e22fc12

Browse files
committedJun 10, 2024
8333775: Small improvement to outputStream auto-indentation mode
Reviewed-by: jsjolen, mbaesken
1 parent 7b43a8c commit e22fc12

File tree

5 files changed

+28
-15
lines changed

5 files changed

+28
-15
lines changed
 

‎src/hotspot/share/nmt/memReporter.cpp

+1-7
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,7 @@ static ssize_t counter_diff(size_t c1, size_t c2) {
5151
}
5252

5353
MemReporterBase::MemReporterBase(outputStream* out, size_t scale) :
54-
_scale(scale), _output(out) {
55-
_output->set_autoindent(true);
56-
}
57-
58-
MemReporterBase::~MemReporterBase() {
59-
_output->set_autoindent(false);
60-
}
54+
_scale(scale), _output(out), _auto_indentor(out) {}
6155

6256
size_t MemReporterBase::reserved_total(const MallocMemory* malloc, const VirtualMemory* vm) {
6357
return malloc->malloc_size() + malloc->arena_size() + vm->reserved();

‎src/hotspot/share/nmt/memReporter.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ class MemReporterBase : public StackObj {
3838
private:
3939
const size_t _scale; // report in this scale
4040
outputStream* const _output; // destination
41+
StreamAutoIndentor _auto_indentor;
4142

4243
public:
4344

4445
// Default scale to use if no scale given.
4546
static const size_t default_scale = K;
4647

4748
MemReporterBase(outputStream* out, size_t scale = default_scale);
48-
~MemReporterBase();
4949

5050
// Helper functions
5151
// Calculate total reserved and committed amount

‎src/hotspot/share/utilities/ostream.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,12 @@ outputStream& outputStream::indent() {
277277
return *this;
278278
}
279279

280+
bool outputStream::set_autoindent(bool value) {
281+
const bool old = _autoindent;
282+
_autoindent = value;
283+
return old;
284+
}
285+
280286
void outputStream::print_jlong(jlong value) {
281287
print(JLONG_FORMAT, value);
282288
}

‎src/hotspot/share/utilities/ostream.hpp

+16-6
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ class outputStream : public CHeapObjBase {
109109
// line starts depending on the current indentation level:
110110
// print(), print_cr(), print_raw(), print_raw_cr()
111111
// Other APIs are unaffected
112-
void set_autoindent(bool value) { _autoindent = value; }
112+
// Returns old autoindent state.
113+
bool set_autoindent(bool value);
113114

114115
// sizing
115116
int position() const { return _position; }
@@ -175,17 +176,26 @@ class outputStream : public CHeapObjBase {
175176
extern outputStream* tty; // tty output
176177

177178
class streamIndentor : public StackObj {
178-
private:
179-
outputStream* _str;
180-
int _amount;
181-
182-
public:
179+
outputStream* const _str;
180+
const int _amount;
181+
NONCOPYABLE(streamIndentor);
182+
public:
183183
streamIndentor(outputStream* str, int amt = 2) : _str(str), _amount(amt) {
184184
_str->inc(_amount);
185185
}
186186
~streamIndentor() { _str->dec(_amount); }
187187
};
188188

189+
class StreamAutoIndentor : public StackObj {
190+
outputStream* const _os;
191+
const bool _old;
192+
NONCOPYABLE(StreamAutoIndentor);
193+
public:
194+
StreamAutoIndentor(outputStream* os) :
195+
_os(os), _old(os->set_autoindent(true)) {}
196+
~StreamAutoIndentor() { _os->set_autoindent(_old); }
197+
};
198+
189199
// advisory locking for the shared tty stream:
190200
class ttyLocker: StackObj {
191201
friend class ttyUnlocker;

‎test/hotspot/gtest/utilities/test_ostream.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ TEST_VM(ostream, bufferedStream_dynamic_small) {
106106

107107
static void test_autoindent(bool on) {
108108
stringStream ss;
109-
ss.set_autoindent(on);
109+
const bool prior = ss.set_autoindent(on);
110+
EXPECT_FALSE(prior);
110111
{
111112
streamIndentor si(&ss, 5);
112113
ss.print("ABC");
@@ -146,6 +147,8 @@ static void test_autoindent(bool on) {
146147
"end"
147148
);
148149
}
150+
bool prior2 = ss.set_autoindent(prior);
151+
EXPECT_EQ(prior2, on);
149152
}
150153

151154
TEST_VM(ostream, autoindent_on) { test_autoindent(true); }

0 commit comments

Comments
 (0)
Please sign in to comment.