< prev index next >
src/hotspot/share/utilities/formatBuffer.hpp
Print this page
*** 1,7 ****
/*
! * Copyright (c) 2017, 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.
--- 1,7 ----
/*
! * Copyright (c) 2017, 2018, 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.
*** 43,52 ****
--- 43,68 ----
class FormatBufferResource : public FormatBufferBase {
public:
FormatBufferResource(const char * format, ...) ATTRIBUTE_PRINTF(2, 3);
};
+ // Use malloc() for buffer
+ class FormatBufferDynamic : public FormatBufferBase {
+ protected:
+ int bufsz;
+
+ public:
+ inline FormatBufferDynamic();
+ ~FormatBufferDynamic();
+ inline void append(const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
+ inline void print(const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
+ inline void printv(const char* format, va_list ap) ATTRIBUTE_PRINTF(2, 0);
+
+ char* buffer() { return _buf; }
+ int size() { return bufsz; }
+ };
+
class FormatBufferDummy {};
// Use stack for buffer
template <size_t bufsz = FormatBufferBase::BufferSize>
class FormatBuffer : public FormatBufferBase {
*** 67,76 ****
--- 83,141 ----
protected:
inline FormatBuffer();
};
+ FormatBufferDynamic::FormatBufferDynamic() : FormatBufferBase(NULL), bufsz(-1) {
+ // do nothing
+ }
+
+ void FormatBufferDynamic::append(const char* format, ...) {
+ va_list argp;
+
+ va_start(argp, format);
+ int append_len = jio_vsnprintf(NULL, 0, format, argp) + 1;
+ va_end(argp);
+
+ char *append_str = (char *)os::malloc(append_len, mtInternal);
+ va_start(argp, format);
+ jio_vsnprintf(append_str, append_len, format, argp);
+ va_end(argp);
+
+ if (_buf == NULL) {
+ bufsz = append_len;
+ _buf = append_str;
+ } else {
+ bufsz += append_len - 1;
+ _buf = (char *)os::realloc(_buf, bufsz, mtInternal);
+ strcat(_buf, append_str);
+ os::free(append_str);
+ }
+ }
+
+ void FormatBufferDynamic::print(const char * format, ...) {
+ va_list argp;
+ va_start(argp, format);
+ printv(format, argp);
+ va_end(argp);
+ }
+
+ void FormatBufferDynamic::printv(const char * format, va_list argp) {
+ if (_buf != NULL) {
+ os::free(_buf);
+ bufsz = -1;
+ }
+
+ va_list argp_local;
+ va_copy(argp_local, argp);
+ bufsz = jio_vsnprintf(NULL, 0, format, argp_local) + 1;
+ va_end(argp_local);
+
+ _buf = (char *)os::malloc(bufsz, mtInternal);
+ jio_vsnprintf(_buf, bufsz, format, argp);
+ }
+
template <size_t bufsz>
FormatBuffer<bufsz>::FormatBuffer(const char * format, ...) : FormatBufferBase(_buffer) {
va_list argp;
va_start(argp, format);
jio_vsnprintf(_buf, bufsz, format, argp);
< prev index next >