Skip to content

Commit 9b58527

Browse files
committed
Parts of JDK-8293251
1 parent 33436c8 commit 9b58527

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/hotspot/share/utilities/ostream.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ void stringStream::grow(size_t new_capacity) {
354354
}
355355

356356
void stringStream::write(const char* s, size_t len) {
357+
assert(_is_frozen == false, "Modification forbidden");
357358
assert(_capacity >= _written + 1, "Sanity");
358359
if (len == 0) {
359360
return;
@@ -393,6 +394,7 @@ void stringStream::zero_terminate() {
393394
}
394395

395396
void stringStream::reset() {
397+
assert(_is_frozen == false, "Modification forbidden");
396398
_written = 0; _precount = 0; _position = 0;
397399
_newlines = 0;
398400
zero_terminate();

src/hotspot/share/utilities/ostream.hpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "memory/allocation.hpp"
2929
#include "runtime/timer.hpp"
3030
#include "utilities/globalDefinitions.hpp"
31+
#include "utilities/macros.hpp"
3132

3233
DEBUG_ONLY(class ResourceMark;)
3334

@@ -191,6 +192,7 @@ class ttyUnlocker: StackObj {
191192
// for writing to strings; buffer will expand automatically.
192193
// Buffer will always be zero-terminated.
193194
class stringStream : public outputStream {
195+
DEBUG_ONLY(bool _is_frozen = false);
194196
char* _buffer;
195197
size_t _written; // Number of characters written, excluding termin. zero
196198
size_t _capacity;
@@ -215,9 +217,18 @@ class stringStream : public outputStream {
215217
// Return number of characters written into buffer, excluding terminating zero and
216218
// subject to truncation in static buffer mode.
217219
size_t size() const { return _written; }
220+
// Returns internal buffer containing the accumulated string.
221+
// Returned buffer is only guaranteed to be valid as long as stream is not modified
218222
const char* base() const { return _buffer; }
223+
// Freezes stringStream (no further modifications possible) and returns pointer to it.
224+
// No-op if stream is frozen already.
225+
// Returns the internal buffer containing the accumulated string.
226+
const char* freeze() NOT_DEBUG(const) {
227+
DEBUG_ONLY(_is_frozen = true);
228+
return _buffer;
229+
};
219230
void reset();
220-
// copy to a resource, or C-heap, array as requested
231+
// Copy to a resource, or C-heap, array as requested
221232
char* as_string(bool c_heap = false) const;
222233
};
223234

0 commit comments

Comments
 (0)