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