hotspot/src/share/vm/code/compressedStream.hpp
author trims
Thu, 27 May 2010 19:08:38 -0700
changeset 5547 f4b087cbb361
parent 1 489c9b5090e2
child 7397 5b173b4ca846
permissions -rw-r--r--
6941466: Oracle rebranding changes for Hotspot repositories Summary: Change all the Sun copyrights to Oracle copyright Reviewed-by: ohair
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
     2
 * Copyright (c) 1997, 2005, 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
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
// Simple interface for filing out and filing in basic types
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
// Used for writing out and reading in debugging information.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
class CompressedStream : public ResourceObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
  friend class VMStructs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
 protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
  u_char* _buffer;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
  int     _position;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
  enum {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
    // Constants for UNSIGNED5 coding of Pack200
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
    lg_H = 6, H = 1<<lg_H,    // number of high codes (64)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
    L = (1<<BitsPerByte)-H,   // number of low codes (192)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
    MAX_i = 4                 // bytes are numbered in (0..4), max 5 bytes
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  // these inlines are defined only in compressedStream.cpp
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
  static inline juint encode_sign(jint  value);  // for Pack200 SIGNED5
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  static inline jint  decode_sign(juint value);  // for Pack200 SIGNED5
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  static inline juint reverse_int(juint bits);   // to trim trailing float 0's
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  CompressedStream(u_char* buffer, int position = 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
    _buffer   = buffer;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
    _position = position;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  u_char* buffer() const               { return _buffer; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  // Positioning
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  int position() const                 { return _position; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
  void set_position(int position)      { _position = position; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
class CompressedReadStream : public CompressedStream {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  inline u_char read()                 { return _buffer[_position++]; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  jint     read_int_mb(jint b0);  // UNSIGNED5 coding, 2-5 byte cases
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  CompressedReadStream(u_char* buffer, int position = 0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  : CompressedStream(buffer, position) {}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  jboolean read_bool()                 { return (jboolean) read();      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  jbyte    read_byte()                 { return (jbyte   ) read();      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  jchar    read_char()                 { return (jchar   ) read_int();  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  jshort   read_short()                { return (jshort  ) read_signed_int(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  jint     read_int()                  { jint   b0 = read();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
                                         if (b0 < L)  return b0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
                                         else         return read_int_mb(b0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
                                       }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  jint     read_signed_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  jfloat   read_float();               // jfloat_cast(reverse_int(read_int()))
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  jdouble  read_double();              // jdouble_cast(2*reverse_int(read_int))
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  jlong    read_long();                // jlong_from(2*read_signed_int())
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
class CompressedWriteStream : public CompressedStream {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  bool full() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
    return _position >= _size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  void store(u_char b) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
    _buffer[_position++] = b;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  void write(u_char b) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
    if (full()) grow();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
    store(b);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  void grow();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  void write_int_mb(jint value);  // UNSIGNED5 coding, 1-5 byte cases
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
 protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  int _size;
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
  CompressedWriteStream(int initial_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  CompressedWriteStream(u_char* buffer, int initial_size, int position = 0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  : CompressedStream(buffer, position) { _size = initial_size; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  void write_bool(jboolean value)      { write(value);      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  void write_byte(jbyte value)         { write(value);      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  void write_char(jchar value)         { write_int(value); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  void write_short(jshort value)       { write_signed_int(value);  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  void write_int(jint value)           { if ((juint)value < L && !full())
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
                                               store((u_char)value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
                                         else  write_int_mb(value);  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  void write_signed_int(jint value);   // write_int(encode_sign(value))
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  void write_float(jfloat value);      // write_int(reverse_int(jint_cast(v)))
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  void write_double(jdouble value);    // write_int(reverse_int(<low,high>))
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  void write_long(jlong value);        // write_signed_int(<low,high>)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
};