1 /* |
|
2 * Copyright (c) 2015, 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_UTILITIES_ENDIAN_HPP |
|
26 #define SHARE_VM_UTILITIES_ENDIAN_HPP |
|
27 |
|
28 #include "utilities/globalDefinitions.hpp" |
|
29 |
|
30 // Selectable endian handling. Endian handlers are used when accessing values |
|
31 // that are of unknown (until runtime) endian. The only requirement of the values |
|
32 // accessed are that they are aligned to proper size boundaries (no misalignment.) |
|
33 // To select an endian handler, one should call Endian::get_handler(big_endian); |
|
34 // Where big_endian is true if big endian is required and false otherwise. The |
|
35 // native endian handler can be fetched with Endian::get_native_handler(); |
|
36 // To retrieve a value using the approprate endian, use one of the overloaded |
|
37 // calls to get. To set a value, then use one of the overloaded set calls. |
|
38 // Ex. |
|
39 // s4 value; // Imported value; |
|
40 // ... |
|
41 // Endian* endian = Endian::get_handler(true); // Use big endian |
|
42 // s4 corrected = endian->get(value); |
|
43 // endian->set(value, 1); |
|
44 // |
|
45 class Endian { |
|
46 public: |
|
47 virtual u2 get(u2 x) = 0; |
|
48 virtual u4 get(u4 x) = 0; |
|
49 virtual u8 get(u8 x) = 0; |
|
50 virtual s2 get(s2 x) = 0; |
|
51 virtual s4 get(s4 x) = 0; |
|
52 virtual s8 get(s8 x) = 0; |
|
53 |
|
54 virtual void set(u2& x, u2 y) = 0; |
|
55 virtual void set(u4& x, u4 y) = 0; |
|
56 virtual void set(u8& x, u8 y) = 0; |
|
57 virtual void set(s2& x, s2 y) = 0; |
|
58 virtual void set(s4& x, s4 y) = 0; |
|
59 virtual void set(s8& x, s8 y) = 0; |
|
60 |
|
61 // Quick little endian test. |
|
62 static bool is_little_endian() { u4 x = 1; return *(u1 *)&x != 0; } |
|
63 |
|
64 // Quick big endian test. |
|
65 static bool is_big_endian() { return !is_little_endian(); } |
|
66 |
|
67 // Select an appropriate endian handler. |
|
68 static Endian* get_handler(bool big_endian); |
|
69 |
|
70 // Return the native endian handler. |
|
71 static Endian* get_native_handler(); |
|
72 }; |
|
73 |
|
74 // Normal endian handling. |
|
75 class NativeEndian : public Endian { |
|
76 private: |
|
77 static NativeEndian _native; |
|
78 |
|
79 public: |
|
80 u2 get(u2 x); |
|
81 u4 get(u4 x); |
|
82 u8 get(u8 x); |
|
83 s2 get(s2 x); |
|
84 s4 get(s4 x); |
|
85 s8 get(s8 x); |
|
86 |
|
87 void set(u2& x, u2 y); |
|
88 void set(u4& x, u4 y); |
|
89 void set(u8& x, u8 y); |
|
90 void set(s2& x, s2 y); |
|
91 void set(s4& x, s4 y); |
|
92 void set(s8& x, s8 y); |
|
93 |
|
94 static Endian* get_native() { return &_native; } |
|
95 }; |
|
96 |
|
97 // Swapping endian handling. |
|
98 class SwappingEndian : public Endian { |
|
99 private: |
|
100 static SwappingEndian _swapping; |
|
101 |
|
102 public: |
|
103 u2 get(u2 x); |
|
104 u4 get(u4 x); |
|
105 u8 get(u8 x); |
|
106 s2 get(s2 x); |
|
107 s4 get(s4 x); |
|
108 s8 get(s8 x); |
|
109 |
|
110 void set(u2& x, u2 y); |
|
111 void set(u4& x, u4 y); |
|
112 void set(u8& x, u8 y); |
|
113 void set(s2& x, s2 y); |
|
114 void set(s4& x, s4 y); |
|
115 void set(s8& x, s8 y); |
|
116 |
|
117 static Endian* get_swapping() { return &_swapping; } |
|
118 }; |
|
119 #endif // SHARE_VM_UTILITIES_ENDIAN_HPP |
|