/* * 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. * * 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. * */ #ifndef SHARE_VM_UTILITIES_FORMATBUFFER_HPP #define SHARE_VM_UTILITIES_FORMATBUFFER_HPP #include "jvm.h" #include "utilities/globalDefinitions.hpp" #include // Simple class to format the ctor arguments into a fixed-sized buffer. class FormatBufferBase { protected: char* _buf; inline FormatBufferBase(char* buf) : _buf(buf) {} public: static const int BufferSize = 256; operator const char *() const { return _buf; } }; // Use resource area for buffer 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 class FormatBuffer : public FormatBufferBase { public: inline FormatBuffer(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); // since va_list is unspecified type (can be char*), we use FormatBufferDummy to disambiguate these constructors inline FormatBuffer(FormatBufferDummy dummy, const char* format, va_list ap) ATTRIBUTE_PRINTF(3, 0); 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; } private: FormatBuffer(const FormatBuffer &); // prevent copies char _buffer[bufsz]; 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 FormatBuffer::FormatBuffer(const char * format, ...) : FormatBufferBase(_buffer) { va_list argp; va_start(argp, format); jio_vsnprintf(_buf, bufsz, format, argp); va_end(argp); } template FormatBuffer::FormatBuffer(FormatBufferDummy dummy, const char * format, va_list ap) : FormatBufferBase(_buffer) { jio_vsnprintf(_buf, bufsz, format, ap); } template FormatBuffer::FormatBuffer() : FormatBufferBase(_buffer) { _buf[0] = '\0'; } template void FormatBuffer::print(const char * format, ...) { va_list argp; va_start(argp, format); jio_vsnprintf(_buf, bufsz, format, argp); va_end(argp); } template void FormatBuffer::printv(const char * format, va_list argp) { jio_vsnprintf(_buf, bufsz, format, argp); } template void FormatBuffer::append(const char* format, ...) { // Given that the constructor does a vsnprintf we can assume that // _buf is already initialized. size_t len = strlen(_buf); char* buf_end = _buf + len; va_list argp; va_start(argp, format); jio_vsnprintf(buf_end, bufsz - len, format, argp); va_end(argp); } // Used to format messages. typedef FormatBuffer<> err_msg; #endif // SHARE_VM_UTILITIES_FORMATBUFFER_HPP