1 /* 2 * Copyright (c) 1997, 2016, 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 CPU_X86_VM_BYTES_X86_HPP 26 #define CPU_X86_VM_BYTES_X86_HPP 27 28 #include "memory/allocation.hpp" 29 #include "utilities/macros.hpp" 30 31 class Bytes: AllStatic { 32 private: 33 #ifndef AMD64 34 // Helper function for swap_u8 35 static inline u8 swap_u8_base(u4 x, u4 y); // compiler-dependent implementation 36 #endif // AMD64 37 38 public: 39 // Efficient reading and writing of unaligned unsigned data in platform-specific byte ordering 40 template <typename T> 41 static inline T get_native(const void* p) { 42 assert(p != NULL, "null pointer"); 43 44 T x; 45 46 if (is_aligned(p, sizeof(T))) { 47 x = *(T*)p; 48 } else { 49 memcpy(&x, p, sizeof(T)); 50 } 51 52 return x; 53 } 54 55 template <typename T> 56 static inline void put_native(void* p, T x) { 57 assert(p != NULL, "null pointer"); 58 59 if (is_aligned(p, sizeof(T))) { 60 *(T*)p = x; 61 } else { 62 memcpy(p, &x, sizeof(T)); 63 } 64 } 65 66 static inline u2 get_native_u2(address p) { return get_native<u2>((void*)p); } 67 static inline u4 get_native_u4(address p) { return get_native<u4>((void*)p); } 68 static inline u8 get_native_u8(address p) { return get_native<u8>((void*)p); } 69 static inline void put_native_u2(address p, u2 x) { put_native<u2>((void*)p, x); } 70 static inline void put_native_u4(address p, u4 x) { put_native<u4>((void*)p, x); } 71 static inline void put_native_u8(address p, u8 x) { put_native<u8>((void*)p, x); } 72 73 // Efficient reading and writing of unaligned unsigned data in Java 74 // byte ordering (i.e. big-endian ordering). Byte-order reversal is 75 // needed since x86 CPUs use little-endian format. 76 template <typename T> 77 static inline T get_Java(const address p) { 78 T x = get_native<T>(p); 79 80 if (Endian::is_Java_byte_ordering_different()) { 81 x = swap<T>(x); 82 } 83 84 return x; 85 } 86 87 template <typename T> 88 static inline void put_Java(address p, T x) { 89 if (Endian::is_Java_byte_ordering_different()) { 90 x = swap<T>(x); 91 } 92 93 put_native<T>(p, x); 94 } 95 96 static inline u2 get_Java_u2(address p) { return get_Java<u2>(p); } 97 static inline u4 get_Java_u4(address p) { return get_Java<u4>(p); } 98 static inline u8 get_Java_u8(address p) { return get_Java<u8>(p); } 99 100 static inline void put_Java_u2(address p, u2 x) { put_Java<u2>(p, x); } 101 static inline void put_Java_u4(address p, u4 x) { put_Java<u4>(p, x); } 102 static inline void put_Java_u8(address p, u8 x) { put_Java<u8>(p, x); } 103 104 // Efficient swapping of byte ordering 105 template <typename T> 106 static T swap(T x) { 107 switch (sizeof(T)) { 108 case sizeof(u1): return x; 109 case sizeof(u2): return swap_u2(x); 110 case sizeof(u4): return swap_u4(x); 111 case sizeof(u8): return swap_u8(x); 112 default: 113 guarantee(false, "invalid size: " SIZE_FORMAT "\n", sizeof(T)); 114 return 0; 115 } 116 } 117 118 static inline u2 swap_u2(u2 x); // compiler-dependent implementation 119 static inline u4 swap_u4(u4 x); // compiler-dependent implementation 120 static inline u8 swap_u8(u8 x); 121 }; 122 123 // The following header contains the implementations of swap_u2, swap_u4, and swap_u8[_base] 124 #include OS_CPU_HEADER_INLINE(bytes) 125 126 #endif // CPU_X86_VM_BYTES_X86_HPP