| 1 | /* |
| 2 | * Copyright (c) 2016, 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_JFR_WRITERS_JFRENCODING_HPP |
| 26 | #define SHARE_JFR_WRITERS_JFRENCODING_HPP |
| 27 | |
| 28 | #include "jfr/writers/jfrEncoders.hpp" |
| 29 | #include "memory/allocation.hpp" |
| 30 | #include "utilities/globalDefinitions.hpp" |
| 31 | |
| 32 | enum JfrStringEncoding { |
| 33 | NULL_STRING = 0, |
| 34 | EMPTY_STRING, |
| 35 | STRING_CONSTANT, |
| 36 | UTF8, |
| 37 | UTF16, |
| 38 | LATIN1, |
| 39 | NOF_STRING_ENCODINGS |
| 40 | }; |
| 41 | |
| 42 | template <typename IntegerEncoder, typename BaseEncoder> |
| 43 | class EncoderHost : public AllStatic { |
| 44 | public: |
| 45 | template <typename T> |
| 46 | static u1* be_write(T value, u1* pos) { |
| 47 | return be_write(&value, 1, pos); |
| 48 | } |
| 49 | |
| 50 | template <typename T> |
| 51 | static u1* be_write(const T* value, size_t len, u1* pos) { |
| 52 | assert(value != NULL, "invariant" ); |
| 53 | assert(pos != NULL, "invariant" ); |
| 54 | assert(len > 0, "invariant" ); |
| 55 | return pos + BaseEncoder::encode(value, len, pos); |
| 56 | } |
| 57 | |
| 58 | template <typename T> |
| 59 | static u1* write_padded(T value, u1* pos) { |
| 60 | assert(pos != NULL, "invariant" ); |
| 61 | return write_padded(&value, 1, pos); |
| 62 | } |
| 63 | |
| 64 | template <typename T> |
| 65 | static u1* write_padded(const T* value, size_t len, u1* pos) { |
| 66 | assert(value != NULL, "invariant" ); |
| 67 | assert(pos != NULL, "invariant" ); |
| 68 | assert(len > 0, "invariant" ); |
| 69 | return pos + IntegerEncoder::encode_padded(value, len, pos); |
| 70 | } |
| 71 | |
| 72 | template <typename T> |
| 73 | static u1* write(T value, u1* pos) { |
| 74 | return write(&value, 1, pos); |
| 75 | } |
| 76 | |
| 77 | template <typename T> |
| 78 | static u1* write(const T* value, size_t len, u1* pos) { |
| 79 | assert(value != NULL, "invariant" ); |
| 80 | assert(pos != NULL, "invariant" ); |
| 81 | assert(len > 0, "invariant" ); |
| 82 | return pos + IntegerEncoder::encode(value, len, pos); |
| 83 | } |
| 84 | |
| 85 | static u1* write(bool value, u1* pos) { |
| 86 | return be_write((u1)value, pos); |
| 87 | } |
| 88 | |
| 89 | static u1* write(float value, u1* pos) { |
| 90 | return be_write(*(u4*)&(value), pos); |
| 91 | } |
| 92 | |
| 93 | static u1* write(double value, u1* pos) { |
| 94 | return be_write(*(u8*)&(value), pos); |
| 95 | } |
| 96 | |
| 97 | static u1* write(const char* value, u1* pos) { |
| 98 | u2 len = 0; |
| 99 | if (value != NULL) { |
| 100 | len = MIN2<u2>(max_jushort, (jushort)strlen(value)); |
| 101 | } |
| 102 | pos = write(len, pos); |
| 103 | if (len > 0) { |
| 104 | pos = be_write(value, len, pos); |
| 105 | } |
| 106 | return pos; |
| 107 | } |
| 108 | |
| 109 | static u1* write(char* value, u1* pos) { |
| 110 | return write(const_cast<const char*>(value), pos); |
| 111 | } |
| 112 | }; |
| 113 | |
| 114 | typedef EncoderHost<BigEndianEncoderImpl, BigEndianEncoderImpl> BigEndianEncoder; |
| 115 | typedef EncoderHost<Varint128EncoderImpl, BigEndianEncoderImpl> CompressedIntegerEncoder; |
| 116 | |
| 117 | #endif // SHARE_JFR_WRITERS_JFRENCODING_HPP |
| 118 | |