hotspot/src/share/vm/code/compressedStream.hpp
author redestad
Thu, 29 Jun 2017 14:22:42 +0200
changeset 46600 fdde0f192cde
parent 7397 5b173b4ca846
permissions -rw-r--r--
8183001: Various inlining improvements Reviewed-by: iklam, mikael, ehelin, stefank
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
     2
 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    25
#ifndef SHARE_VM_CODE_COMPRESSEDSTREAM_HPP
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    26
#define SHARE_VM_CODE_COMPRESSEDSTREAM_HPP
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    27
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    28
#include "memory/allocation.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    29
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
// Simple interface for filing out and filing in basic types
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
// Used for writing out and reading in debugging information.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
class CompressedStream : public ResourceObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
  friend class VMStructs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
 protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
  u_char* _buffer;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
  int     _position;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  enum {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
    // Constants for UNSIGNED5 coding of Pack200
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
    lg_H = 6, H = 1<<lg_H,    // number of high codes (64)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
    L = (1<<BitsPerByte)-H,   // number of low codes (192)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
    MAX_i = 4                 // bytes are numbered in (0..4), max 5 bytes
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  // these inlines are defined only in compressedStream.cpp
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  static inline juint encode_sign(jint  value);  // for Pack200 SIGNED5
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  static inline jint  decode_sign(juint value);  // for Pack200 SIGNED5
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  static inline juint reverse_int(juint bits);   // to trim trailing float 0's
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  CompressedStream(u_char* buffer, int position = 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
    _buffer   = buffer;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
    _position = position;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  u_char* buffer() const               { return _buffer; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  // Positioning
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  int position() const                 { return _position; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  void set_position(int position)      { _position = position; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
class CompressedReadStream : public CompressedStream {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  inline u_char read()                 { return _buffer[_position++]; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
46600
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
    69
  // This encoding, called UNSIGNED5, is taken from J2SE Pack200.
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
    70
  // It assumes that most values have lots of leading zeroes.
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
    71
  // Very small values, in the range [0..191], code in one byte.
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
    72
  // Any 32-bit value (including negatives) can be coded, in
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
    73
  // up to five bytes.  The grammar is:
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
    74
  //    low_byte  = [0..191]
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
    75
  //    high_byte = [192..255]
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
    76
  //    any_byte  = low_byte | high_byte
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
    77
  //    coding = low_byte
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
    78
  //           | high_byte low_byte
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
    79
  //           | high_byte high_byte low_byte
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
    80
  //           | high_byte high_byte high_byte low_byte
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
    81
  //           | high_byte high_byte high_byte high_byte any_byte
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
    82
  // Each high_byte contributes six bits of payload.
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
    83
  // The encoding is one-to-one (except for integer overflow)
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
    84
  // and easy to parse and unparse.
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
    85
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
    86
  jint read_int_mb(jint b0) {
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
    87
    int     pos = position() - 1;
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
    88
    u_char* buf = buffer() + pos;
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
    89
    assert(buf[0] == b0 && b0 >= L, "correctly called");
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
    90
    jint    sum = b0;
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
    91
    // must collect more bytes:  b[1]...b[4]
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
    92
    int lg_H_i = lg_H;
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
    93
    for (int i = 0; ; ) {
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
    94
      jint b_i = buf[++i]; // b_i = read(); ++i;
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
    95
      sum += b_i << lg_H_i;  // sum += b[i]*(64**i)
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
    96
      if (b_i < L || i == MAX_i) {
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
    97
        set_position(pos+i+1);
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
    98
        return sum;
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
    99
      }
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
   100
      lg_H_i += lg_H;
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
   101
    }
fdde0f192cde 8183001: Various inlining improvements
redestad
parents: 7397
diff changeset
   102
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  CompressedReadStream(u_char* buffer, int position = 0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  : CompressedStream(buffer, position) {}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  jboolean read_bool()                 { return (jboolean) read();      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  jbyte    read_byte()                 { return (jbyte   ) read();      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  jchar    read_char()                 { return (jchar   ) read_int();  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  jshort   read_short()                { return (jshort  ) read_signed_int(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  jint     read_int()                  { jint   b0 = read();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
                                         if (b0 < L)  return b0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
                                         else         return read_int_mb(b0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
                                       }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  jint     read_signed_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  jfloat   read_float();               // jfloat_cast(reverse_int(read_int()))
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  jdouble  read_double();              // jdouble_cast(2*reverse_int(read_int))
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  jlong    read_long();                // jlong_from(2*read_signed_int())
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
class CompressedWriteStream : public CompressedStream {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  bool full() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
    return _position >= _size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
  void store(u_char b) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
    _buffer[_position++] = b;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
  void write(u_char b) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
    if (full()) grow();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
    store(b);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
  void grow();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
  void write_int_mb(jint value);  // UNSIGNED5 coding, 1-5 byte cases
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
 protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
  int _size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
  CompressedWriteStream(int initial_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  CompressedWriteStream(u_char* buffer, int initial_size, int position = 0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
  : CompressedStream(buffer, position) { _size = initial_size; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
  void write_bool(jboolean value)      { write(value);      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
  void write_byte(jbyte value)         { write(value);      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
  void write_char(jchar value)         { write_int(value); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
  void write_short(jshort value)       { write_signed_int(value);  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
  void write_int(jint value)           { if ((juint)value < L && !full())
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
                                               store((u_char)value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
                                         else  write_int_mb(value);  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
  void write_signed_int(jint value);   // write_int(encode_sign(value))
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
  void write_float(jfloat value);      // write_int(reverse_int(jint_cast(v)))
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
  void write_double(jdouble value);    // write_int(reverse_int(<low,high>))
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
  void write_long(jlong value);        // write_signed_int(<low,high>)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
};
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
   159
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
   160
#endif // SHARE_VM_CODE_COMPRESSEDSTREAM_HPP