Path: blob/master/src/hotspot/share/jfr/utilities/jfrBigEndian.hpp
41149 views
/*1* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_JFR_UTILITIES_JFRBIGENDIAN_HPP25#define SHARE_JFR_UTILITIES_JFRBIGENDIAN_HPP2627#include "memory/allocation.hpp"28#include "utilities/bytes.hpp"29#include "utilities/macros.hpp"3031#ifndef VM_LITTLE_ENDIAN32# define bigendian_16(x) (x)33# define bigendian_32(x) (x)34# define bigendian_64(x) (x)35#else36# define bigendian_16(x) Bytes::swap_u2(x)37# define bigendian_32(x) Bytes::swap_u4(x)38# define bigendian_64(x) Bytes::swap_u8(x)39#endif4041class JfrBigEndian : AllStatic {42private:43template <typename T>44static T read_bytes(const address location);45template <typename T>46static T read_unaligned(const address location);47public:48static bool platform_supports_unaligned_reads(void);49static bool is_aligned(const void* location, size_t size);50template <typename T>51static T read(const void* location);52};5354inline bool JfrBigEndian::is_aligned(const void* location, size_t size) {55assert(size <= sizeof(u8), "just checking");56if (size == sizeof(u1)) {57return true;58}59// check address alignment for datum access60return (((uintptr_t)location & (size -1)) == 0);61}6263template <>64inline u1 JfrBigEndian::read_bytes(const address location) {65return (*location & 0xFF);66}6768template <>69inline u2 JfrBigEndian::read_bytes(const address location) {70return Bytes::get_Java_u2(location);71}7273template <>74inline u4 JfrBigEndian::read_bytes(const address location) {75return Bytes::get_Java_u4(location);76}7778template <>79inline u8 JfrBigEndian::read_bytes(const address location) {80return Bytes::get_Java_u8(location);81}8283template <typename T>84inline T JfrBigEndian::read_unaligned(const address location) {85assert(location != NULL, "just checking");86switch (sizeof(T)) {87case sizeof(u1) :88return read_bytes<u1>(location);89case sizeof(u2):90return read_bytes<u2>(location);91case sizeof(u4):92return read_bytes<u4>(location);93case sizeof(u8):94return read_bytes<u8>(location);95default:96assert(false, "not reach");97}98return 0;99}100101inline bool JfrBigEndian::platform_supports_unaligned_reads(void) {102#if defined(IA32) || defined(AMD64) || defined(PPC) || defined(S390)103return true;104#elif defined(ARM) || defined(AARCH64)105return false;106#else107#warning "Unconfigured platform"108return false;109#endif110}111112template<typename T>113inline T JfrBigEndian::read(const void* location) {114assert(location != NULL, "just checking");115assert(sizeof(T) <= sizeof(u8), "no support for arbitrary sizes");116if (sizeof(T) == sizeof(u1)) {117return *(T*)location;118}119if (is_aligned(location, sizeof(T)) || platform_supports_unaligned_reads()) {120// fastest case121switch (sizeof(T)) {122case sizeof(u1):123return *(T*)location;124case sizeof(u2):125return bigendian_16(*(T*)(location));126case sizeof(u4):127return bigendian_32(*(T*)(location));128case sizeof(u8):129return bigendian_64(*(T*)(location));130}131}132return read_unaligned<T>((const address)location);133}134135#endif // SHARE_JFR_UTILITIES_JFRBIGENDIAN_HPP136137138