Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/jfr/utilities/jfrBigEndian.hpp
38920 views
/*1* Copyright (c) 2012, 2018, 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_VM_JFR_UTILITIES_JFRBIGENDIAN_HPP25#define SHARE_VM_JFR_UTILITIES_JFRBIGENDIAN_HPP2627#include "memory/allocation.hpp"28#include "utilities/macros.hpp"29#ifdef TARGET_ARCH_x8630# include "bytes_x86.hpp"31#endif32#ifdef TARGET_ARCH_sparc33# include "bytes_sparc.hpp"34#endif35#ifdef TARGET_ARCH_zero36# include "bytes_zero.hpp"37#endif38#ifdef TARGET_ARCH_arm39# include "bytes_arm.hpp"40#endif41#ifdef TARGET_ARCH_ppc42# include "bytes_ppc.hpp"43#endif44#ifdef TARGET_ARCH_aarch3245# include "bytes_aarch32.hpp"46#endif4748#ifndef VM_LITTLE_ENDIAN49# define bigendian_16(x) (x)50# define bigendian_32(x) (x)51# define bigendian_64(x) (x)52#else53# define bigendian_16(x) Bytes::swap_u2(x)54# define bigendian_32(x) Bytes::swap_u4(x)55# define bigendian_64(x) Bytes::swap_u8(x)56#endif5758class JfrBigEndian : AllStatic {59private:60template <typename T>61static T read_bytes(const address location);62template <typename T>63static T read_unaligned(const address location);64public:65static bool platform_supports_unaligned_reads(void);66static bool is_aligned(const void* location, size_t size);67template <typename T>68static T read(const void* location);69};7071inline bool JfrBigEndian::is_aligned(const void* location, size_t size) {72assert(size <= sizeof(u8), "just checking");73if (size == sizeof(u1)) {74return true;75}76// check address alignment for datum access77return (((uintptr_t)location & (size -1)) == 0);78}7980template <>81inline u1 JfrBigEndian::read_bytes(const address location) {82return (*location & 0xFF);83}8485template <>86inline u2 JfrBigEndian::read_bytes(const address location) {87return Bytes::get_Java_u2(location);88}8990template <>91inline u4 JfrBigEndian::read_bytes(const address location) {92return Bytes::get_Java_u4(location);93}9495template <>96inline u8 JfrBigEndian::read_bytes(const address location) {97return Bytes::get_Java_u8(location);98}99100template <typename T>101inline T JfrBigEndian::read_unaligned(const address location) {102assert(location != NULL, "just checking");103switch (sizeof(T)) {104case sizeof(u1) :105return read_bytes<u1>(location);106case sizeof(u2):107return read_bytes<u2>(location);108case sizeof(u4):109return read_bytes<u4>(location);110case sizeof(u8):111return read_bytes<u8>(location);112default:113assert(false, "not reach");114}115return 0;116}117118inline bool JfrBigEndian::platform_supports_unaligned_reads(void) {119#if defined(IA32) || defined(AMD64) || defined(PPC) || defined(S390)120return true;121#elif defined(SPARC) || defined(ARM) || defined(AARCH64) || defined(AARCH32)122return false;123#else124#warning "Unconfigured platform"125return false;126#endif127}128129template<typename T>130inline T JfrBigEndian::read(const void* location) {131assert(location != NULL, "just checking");132assert(sizeof(T) <= sizeof(u8), "no support for arbitrary sizes");133if (sizeof(T) == sizeof(u1)) {134return *(T*)location;135}136if (is_aligned(location, sizeof(T)) || platform_supports_unaligned_reads()) {137// fastest case138switch (sizeof(T)) {139case sizeof(u1):140return *(T*)location;141case sizeof(u2):142return bigendian_16(*(T*)(location));143case sizeof(u4):144return bigendian_32(*(T*)(location));145case sizeof(u8):146return bigendian_64(*(T*)(location));147}148}149return read_unaligned<T>((const address)location);150}151152#endif // SHARE_VM_JFR_UTILITIES_JFRBIGENDIAN_HPP153154155