1/*
2 * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25#ifndef SHARE_UTILITIES_FORMATBUFFER_HPP
26#define SHARE_UTILITIES_FORMATBUFFER_HPP
27
28#include "jvm.h"
29#include "utilities/globalDefinitions.hpp"
30#include <stdarg.h>
31
32// Simple class to format the ctor arguments into a fixed-sized buffer.
33class FormatBufferBase {
34 protected:
35 char* _buf;
36 inline FormatBufferBase(char* buf) : _buf(buf) {}
37 public:
38 static const int BufferSize = 256;
39 operator const char *() const { return _buf; }
40};
41
42// Use resource area for buffer
43class FormatBufferResource : public FormatBufferBase {
44 public:
45 FormatBufferResource(const char * format, ...) ATTRIBUTE_PRINTF(2, 3);
46};
47
48class FormatBufferDummy {};
49
50// Use stack for buffer
51template <size_t bufsz = FormatBufferBase::BufferSize>
52class FormatBuffer : public FormatBufferBase {
53 public:
54 inline FormatBuffer(const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
55 // since va_list is unspecified type (can be char*), we use FormatBufferDummy to disambiguate these constructors
56 inline FormatBuffer(FormatBufferDummy dummy, const char* format, va_list ap) ATTRIBUTE_PRINTF(3, 0);
57 inline void append(const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
58 inline void print(const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
59 inline void printv(const char* format, va_list ap) ATTRIBUTE_PRINTF(2, 0);
60
61 char* buffer() { return _buf; }
62 int size() { return bufsz; }
63
64 private:
65 FormatBuffer(const FormatBuffer &); // prevent copies
66 char _buffer[bufsz];
67
68 protected:
69 inline FormatBuffer();
70};
71
72template <size_t bufsz>
73FormatBuffer<bufsz>::FormatBuffer(const char * format, ...) : FormatBufferBase(_buffer) {
74 va_list argp;
75 va_start(argp, format);
76 jio_vsnprintf(_buf, bufsz, format, argp);
77 va_end(argp);
78}
79
80template <size_t bufsz>
81FormatBuffer<bufsz>::FormatBuffer(FormatBufferDummy dummy, const char * format, va_list ap) : FormatBufferBase(_buffer) {
82 jio_vsnprintf(_buf, bufsz, format, ap);
83}
84
85template <size_t bufsz>
86FormatBuffer<bufsz>::FormatBuffer() : FormatBufferBase(_buffer) {
87 _buf[0] = '\0';
88}
89
90template <size_t bufsz>
91void FormatBuffer<bufsz>::print(const char * format, ...) {
92 va_list argp;
93 va_start(argp, format);
94 jio_vsnprintf(_buf, bufsz, format, argp);
95 va_end(argp);
96}
97
98template <size_t bufsz>
99void FormatBuffer<bufsz>::printv(const char * format, va_list argp) {
100 jio_vsnprintf(_buf, bufsz, format, argp);
101}
102
103template <size_t bufsz>
104void FormatBuffer<bufsz>::append(const char* format, ...) {
105 // Given that the constructor does a vsnprintf we can assume that
106 // _buf is already initialized.
107 size_t len = strlen(_buf);
108 char* buf_end = _buf + len;
109
110 va_list argp;
111 va_start(argp, format);
112 jio_vsnprintf(buf_end, bufsz - len, format, argp);
113 va_end(argp);
114}
115
116// Used to format messages.
117typedef FormatBuffer<> err_msg;
118
119#endif // SHARE_UTILITIES_FORMATBUFFER_HPP
120