Thu, 24 May 2018 18:41:44 +0800
Merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | * |
aoqi@0 | 23 | */ |
aoqi@0 | 24 | |
aoqi@0 | 25 | #ifndef SHARE_VM_CODE_COMPRESSEDSTREAM_HPP |
aoqi@0 | 26 | #define SHARE_VM_CODE_COMPRESSEDSTREAM_HPP |
aoqi@0 | 27 | |
aoqi@0 | 28 | #include "memory/allocation.hpp" |
aoqi@0 | 29 | |
aoqi@0 | 30 | // Simple interface for filing out and filing in basic types |
aoqi@0 | 31 | // Used for writing out and reading in debugging information. |
aoqi@0 | 32 | |
aoqi@0 | 33 | class CompressedStream : public ResourceObj { |
aoqi@0 | 34 | friend class VMStructs; |
aoqi@0 | 35 | protected: |
aoqi@0 | 36 | u_char* _buffer; |
aoqi@0 | 37 | int _position; |
aoqi@0 | 38 | |
aoqi@0 | 39 | enum { |
aoqi@0 | 40 | // Constants for UNSIGNED5 coding of Pack200 |
aoqi@0 | 41 | lg_H = 6, H = 1<<lg_H, // number of high codes (64) |
aoqi@0 | 42 | L = (1<<BitsPerByte)-H, // number of low codes (192) |
aoqi@0 | 43 | MAX_i = 4 // bytes are numbered in (0..4), max 5 bytes |
aoqi@0 | 44 | }; |
aoqi@0 | 45 | |
aoqi@0 | 46 | // these inlines are defined only in compressedStream.cpp |
aoqi@0 | 47 | static inline juint encode_sign(jint value); // for Pack200 SIGNED5 |
aoqi@0 | 48 | static inline jint decode_sign(juint value); // for Pack200 SIGNED5 |
aoqi@0 | 49 | static inline juint reverse_int(juint bits); // to trim trailing float 0's |
aoqi@0 | 50 | |
aoqi@0 | 51 | public: |
aoqi@0 | 52 | CompressedStream(u_char* buffer, int position = 0) { |
aoqi@0 | 53 | _buffer = buffer; |
aoqi@0 | 54 | _position = position; |
aoqi@0 | 55 | } |
aoqi@0 | 56 | |
aoqi@0 | 57 | u_char* buffer() const { return _buffer; } |
aoqi@0 | 58 | |
aoqi@0 | 59 | // Positioning |
aoqi@0 | 60 | int position() const { return _position; } |
aoqi@0 | 61 | void set_position(int position) { _position = position; } |
aoqi@0 | 62 | }; |
aoqi@0 | 63 | |
aoqi@0 | 64 | |
aoqi@0 | 65 | class CompressedReadStream : public CompressedStream { |
aoqi@0 | 66 | private: |
aoqi@0 | 67 | inline u_char read() { return _buffer[_position++]; } |
aoqi@0 | 68 | |
aoqi@0 | 69 | jint read_int_mb(jint b0); // UNSIGNED5 coding, 2-5 byte cases |
aoqi@0 | 70 | |
aoqi@0 | 71 | public: |
aoqi@0 | 72 | CompressedReadStream(u_char* buffer, int position = 0) |
aoqi@0 | 73 | : CompressedStream(buffer, position) {} |
aoqi@0 | 74 | |
aoqi@0 | 75 | jboolean read_bool() { return (jboolean) read(); } |
aoqi@0 | 76 | jbyte read_byte() { return (jbyte ) read(); } |
aoqi@0 | 77 | jchar read_char() { return (jchar ) read_int(); } |
aoqi@0 | 78 | jshort read_short() { return (jshort ) read_signed_int(); } |
aoqi@0 | 79 | jint read_int() { jint b0 = read(); |
aoqi@0 | 80 | if (b0 < L) return b0; |
aoqi@0 | 81 | else return read_int_mb(b0); |
aoqi@0 | 82 | } |
aoqi@0 | 83 | jint read_signed_int(); |
aoqi@0 | 84 | jfloat read_float(); // jfloat_cast(reverse_int(read_int())) |
aoqi@0 | 85 | jdouble read_double(); // jdouble_cast(2*reverse_int(read_int)) |
aoqi@0 | 86 | jlong read_long(); // jlong_from(2*read_signed_int()) |
aoqi@0 | 87 | }; |
aoqi@0 | 88 | |
aoqi@0 | 89 | |
aoqi@0 | 90 | class CompressedWriteStream : public CompressedStream { |
aoqi@0 | 91 | private: |
aoqi@0 | 92 | bool full() { |
aoqi@0 | 93 | return _position >= _size; |
aoqi@0 | 94 | } |
aoqi@0 | 95 | void store(u_char b) { |
aoqi@0 | 96 | _buffer[_position++] = b; |
aoqi@0 | 97 | } |
aoqi@0 | 98 | void write(u_char b) { |
aoqi@0 | 99 | if (full()) grow(); |
aoqi@0 | 100 | store(b); |
aoqi@0 | 101 | } |
aoqi@0 | 102 | void grow(); |
aoqi@0 | 103 | |
aoqi@0 | 104 | void write_int_mb(jint value); // UNSIGNED5 coding, 1-5 byte cases |
aoqi@0 | 105 | |
aoqi@0 | 106 | protected: |
aoqi@0 | 107 | int _size; |
aoqi@0 | 108 | |
aoqi@0 | 109 | public: |
aoqi@0 | 110 | CompressedWriteStream(int initial_size); |
aoqi@0 | 111 | CompressedWriteStream(u_char* buffer, int initial_size, int position = 0) |
aoqi@0 | 112 | : CompressedStream(buffer, position) { _size = initial_size; } |
aoqi@0 | 113 | |
aoqi@0 | 114 | void write_bool(jboolean value) { write(value); } |
aoqi@0 | 115 | void write_byte(jbyte value) { write(value); } |
aoqi@0 | 116 | void write_char(jchar value) { write_int(value); } |
aoqi@0 | 117 | void write_short(jshort value) { write_signed_int(value); } |
aoqi@0 | 118 | void write_int(jint value) { if ((juint)value < L && !full()) |
aoqi@0 | 119 | store((u_char)value); |
aoqi@0 | 120 | else write_int_mb(value); } |
aoqi@0 | 121 | void write_signed_int(jint value); // write_int(encode_sign(value)) |
aoqi@0 | 122 | void write_float(jfloat value); // write_int(reverse_int(jint_cast(v))) |
aoqi@0 | 123 | void write_double(jdouble value); // write_int(reverse_int(<low,high>)) |
aoqi@0 | 124 | void write_long(jlong value); // write_signed_int(<low,high>) |
aoqi@0 | 125 | }; |
aoqi@0 | 126 | |
aoqi@0 | 127 | #endif // SHARE_VM_CODE_COMPRESSEDSTREAM_HPP |