2626#define SHARE_CODE_COMPRESSEDSTREAM_HPP
2727
2828#include " memory/allocation.hpp"
29+ #include " utilities/unsigned5.hpp"
2930
3031// Simple interface for filing out and filing in basic types
3132// Used for writing out and reading in debugging information.
@@ -36,18 +37,6 @@ class CompressedStream : public ResourceObj {
3637 u_char* _buffer;
3738 int _position;
3839
39- enum {
40- // Constants for UNSIGNED5 coding of Pack200
41- lg_H = 6 , H = 1 <<lg_H, // number of high codes (64)
42- L = (1 <<BitsPerByte)-H, // number of low codes (192)
43- MAX_i = 4 // bytes are numbered in (0..4), max 5 bytes
44- };
45-
46- // 32-bit one-to-one sign encoding taken from Pack200
47- // converts leading sign bits into leading zeroes with trailing sign bit
48- static juint encode_sign (jint value) { return (value << 1 ) ^ (value >> 31 ); }
49- static jint decode_sign (juint value) { return (value >> 1 ) ^ -(jint)(value & 1 ); }
50- static juint reverse_int (juint i); // to trim trailing float 0's
5140 public:
5241 CompressedStream (u_char* buffer, int position = 0 ) {
5342 _buffer = buffer;
@@ -66,41 +55,6 @@ class CompressedReadStream : public CompressedStream {
6655 private:
6756 inline u_char read () { return _buffer[_position++]; }
6857
69- // This encoding, called UNSIGNED5, is taken from J2SE Pack200.
70- // It assumes that most values have lots of leading zeroes.
71- // Very small values, in the range [0..191], code in one byte.
72- // Any 32-bit value (including negatives) can be coded, in
73- // up to five bytes. The grammar is:
74- // low_byte = [0..191]
75- // high_byte = [192..255]
76- // any_byte = low_byte | high_byte
77- // coding = low_byte
78- // | high_byte low_byte
79- // | high_byte high_byte low_byte
80- // | high_byte high_byte high_byte low_byte
81- // | high_byte high_byte high_byte high_byte any_byte
82- // Each high_byte contributes six bits of payload.
83- // The encoding is one-to-one (except for integer overflow)
84- // and easy to parse and unparse.
85-
86- jint read_int_mb (jint b0) {
87- int pos = position () - 1 ;
88- u_char* buf = buffer () + pos;
89- assert (buf[0 ] == b0 && b0 >= L, " correctly called" );
90- jint sum = b0;
91- // must collect more bytes: b[1]...b[4]
92- int lg_H_i = lg_H;
93- for (int i = 0 ; ; ) {
94- jint b_i = buf[++i]; // b_i = read(); ++i;
95- sum += b_i << lg_H_i; // sum += b[i]*(64**i)
96- if (b_i < L || i == MAX_i) {
97- set_position (pos+i+1 );
98- return sum;
99- }
100- lg_H_i += lg_H;
101- }
102- }
103-
10458 public:
10559 CompressedReadStream (u_char* buffer, int position = 0 )
10660 : CompressedStream(buffer, position) {}
@@ -109,14 +63,14 @@ class CompressedReadStream : public CompressedStream {
10963 jbyte read_byte () { return (jbyte ) read (); }
11064 jchar read_char () { return (jchar ) read_int (); }
11165 jshort read_short () { return (jshort ) read_signed_int (); }
112- jint read_int () { jint b0 = read ();
113- if (b0 < L) return b0;
114- else return read_int_mb (b0);
115- }
11666 jint read_signed_int ();
117- jfloat read_float (); // jfloat_cast(reverse_int (read_int()))
118- jdouble read_double (); // jdouble_cast(2*reverse_int (read_int))
67+ jfloat read_float (); // jfloat_cast(reverse_bits (read_int()))
68+ jdouble read_double (); // jdouble_cast(2*reverse_bits (read_int))
11969 jlong read_long (); // jlong_from(2*read_signed_int())
70+
71+ jint read_int () {
72+ return UNSIGNED5::read_uint (_buffer, _position, 0 );
73+ }
12074};
12175
12276
@@ -134,23 +88,6 @@ class CompressedWriteStream : public CompressedStream {
13488 }
13589 void grow ();
13690
137- // UNSIGNED5 coding, 1-5 byte cases
138- void write_int_mb (jint value) {
139- juint sum = value;
140- for (int i = 0 ; ; ) {
141- if (sum < L || i == MAX_i) {
142- // remainder is either a "low code" or the 5th byte
143- assert (sum == (u_char)sum, " valid byte" );
144- write ((u_char)sum);
145- break ;
146- }
147- sum -= L;
148- int b_i = L + (sum % H); // this is a "high code"
149- sum >>= lg_H; // extracted 6 bits
150- write (b_i); ++i;
151- }
152- }
153-
15491 protected:
15592 int _size;
15693
@@ -163,13 +100,15 @@ class CompressedWriteStream : public CompressedStream {
163100 void write_byte (jbyte value) { write (value); }
164101 void write_char (jchar value) { write_int (value); }
165102 void write_short (jshort value) { write_signed_int (value); }
166- void write_int (jint value) { if ((juint)value < L && !full ())
167- store ((u_char)value);
168- else write_int_mb (value); }
169- void write_signed_int (jint value) { write_int (encode_sign (value)); }
170- void write_float (jfloat value); // write_int(reverse_int(jint_cast(v)))
171- void write_double (jdouble value); // write_int(reverse_int(<low,high>))
103+ void write_signed_int (jint value) { write_int (UNSIGNED5::encode_sign (value)); }
104+ void write_float (jfloat value); // write_int(reverse_bits(jint_cast(v)))
105+ void write_double (jdouble value); // write_int(reverse_bits(<low,high>))
172106 void write_long (jlong value); // write_signed_int(<low,high>)
107+
108+ void write_int (juint value) {
109+ UNSIGNED5::write_uint_grow (value, _buffer, _position, _size,
110+ [&](int ){ grow (); });
111+ }
173112};
174113
175114#endif // SHARE_CODE_COMPRESSEDSTREAM_HPP
0 commit comments