Commit c3a378dd authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[cleanup] Move memory-related functions to v8memory.h

This moves the {ReadUnalignedValue} and {WriteUnalignedValue} methods
from utils.h to v8memory.h.

R=titzer@chromium.org

Bug: v8:7754, v8:7490
Cq-Include-Trybots: luci.chromium.try:linux_chromium_headless_rel;master.tryserver.blink:linux_trusty_blink_rel
Change-Id: I8becfc3ac169427968c11e24b035a90856f51e8e
Reviewed-on: https://chromium-review.googlesource.com/1158405Reviewed-by: 's avatarBen Titzer <titzer@chromium.org>
Reviewed-by: 's avatarYang Guo <yangguo@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#54837}
parent d0658079
......@@ -8,7 +8,7 @@ include_rules = [
"+src/base/platform/mutex.h",
"+src/conversions.h",
"+src/flags.h",
"+src/utils.h",
"+src/v8memory.h",
"+src/unicode-cache.h",
"+src/inspector",
"+src/tracing",
......
......@@ -7,7 +7,7 @@
#include "src/inspector/inspected-context.h"
#include "src/inspector/string-util.h"
#include "src/inspector/wasm-translation.h"
#include "src/utils.h"
#include "src/v8memory.h"
namespace v8_inspector {
......
......@@ -14,6 +14,8 @@
// for fields that can be written to and read from multiple threads at the same
// time. See comments in src/base/atomicops.h for the memory ordering sematics.
#include <src/v8memory.h>
#define DECL_PRIMITIVE_ACCESSORS(name, type) \
inline type name() const; \
inline void set_##name(type value);
......
......@@ -10,7 +10,7 @@
#include "src/external-reference-table.h"
#include "src/globals.h"
#include "src/snapshot/references.h"
#include "src/utils.h"
#include "src/v8memory.h"
#include "src/visitors.h"
namespace v8 {
......
......@@ -1571,84 +1571,6 @@ inline uintptr_t GetCurrentStackPosition() {
return limit;
}
template <typename V>
static inline V ReadUnalignedValue(Address p) {
ASSERT_TRIVIALLY_COPYABLE(V);
#if !(V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64 || V8_TARGET_ARCH_ARM)
return *reinterpret_cast<const V*>(p);
#else // V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64 || V8_TARGET_ARCH_ARM
V r;
memmove(&r, reinterpret_cast<void*>(p), sizeof(V));
return r;
#endif // V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64 || V8_TARGET_ARCH_ARM
}
template <typename V>
static inline void WriteUnalignedValue(Address p, V value) {
ASSERT_TRIVIALLY_COPYABLE(V);
#if !(V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64 || V8_TARGET_ARCH_ARM)
*(reinterpret_cast<V*>(p)) = value;
#else // V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64 || V8_TARGET_ARCH_ARM
memmove(reinterpret_cast<void*>(p), &value, sizeof(V));
#endif // V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64 || V8_TARGET_ARCH_ARM
}
static inline double ReadFloatValue(Address p) {
return ReadUnalignedValue<float>(p);
}
static inline double ReadDoubleValue(Address p) {
return ReadUnalignedValue<double>(p);
}
static inline void WriteDoubleValue(Address p, double value) {
WriteUnalignedValue(p, value);
}
static inline uint16_t ReadUnalignedUInt16(Address p) {
return ReadUnalignedValue<uint16_t>(p);
}
static inline void WriteUnalignedUInt16(Address p, uint16_t value) {
WriteUnalignedValue(p, value);
}
static inline uint32_t ReadUnalignedUInt32(Address p) {
return ReadUnalignedValue<uint32_t>(p);
}
static inline void WriteUnalignedUInt32(Address p, uint32_t value) {
WriteUnalignedValue(p, value);
}
template <typename V>
static inline V ReadLittleEndianValue(Address p) {
#if defined(V8_TARGET_LITTLE_ENDIAN)
return ReadUnalignedValue<V>(p);
#elif defined(V8_TARGET_BIG_ENDIAN)
V ret{};
const byte* src = reinterpret_cast<const byte*>(p);
byte* dst = reinterpret_cast<byte*>(&ret);
for (size_t i = 0; i < sizeof(V); i++) {
dst[i] = src[sizeof(V) - i - 1];
}
return ret;
#endif // V8_TARGET_LITTLE_ENDIAN
}
template <typename V>
static inline void WriteLittleEndianValue(Address p, V value) {
#if defined(V8_TARGET_LITTLE_ENDIAN)
WriteUnalignedValue<V>(p, value);
#elif defined(V8_TARGET_BIG_ENDIAN)
byte* src = reinterpret_cast<byte*>(&value);
byte* dst = reinterpret_cast<byte*>(p);
for (size_t i = 0; i < sizeof(V); i++) {
dst[i] = src[sizeof(V) - i - 1];
}
#endif // V8_TARGET_LITTLE_ENDIAN
}
template <typename V>
static inline V ByteReverse(V value) {
size_t size_of_v = sizeof(value);
......
......@@ -12,7 +12,8 @@ namespace internal {
// Memory provides an interface to 'raw' memory. It encapsulates the casts
// that typically are needed when incompatible pointer types are used.
// TODO(all): Avoid using this class, it's undefined behaviour in C++. Use
// {ReadUnalignedValue} and {WriteUnalignedValue} instead.
class Memory {
public:
static uint8_t& uint8_at(Address addr) {
......@@ -80,6 +81,84 @@ class Memory {
}
};
template <typename V>
static inline V ReadUnalignedValue(Address p) {
ASSERT_TRIVIALLY_COPYABLE(V);
#if !(V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64 || V8_TARGET_ARCH_ARM)
return *reinterpret_cast<const V*>(p);
#else // V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64 || V8_TARGET_ARCH_ARM
V r;
memmove(&r, reinterpret_cast<void*>(p), sizeof(V));
return r;
#endif // V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64 || V8_TARGET_ARCH_ARM
}
template <typename V>
static inline void WriteUnalignedValue(Address p, V value) {
ASSERT_TRIVIALLY_COPYABLE(V);
#if !(V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64 || V8_TARGET_ARCH_ARM)
*(reinterpret_cast<V*>(p)) = value;
#else // V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64 || V8_TARGET_ARCH_ARM
memmove(reinterpret_cast<void*>(p), &value, sizeof(V));
#endif // V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64 || V8_TARGET_ARCH_ARM
}
static inline double ReadFloatValue(Address p) {
return ReadUnalignedValue<float>(p);
}
static inline double ReadDoubleValue(Address p) {
return ReadUnalignedValue<double>(p);
}
static inline void WriteDoubleValue(Address p, double value) {
WriteUnalignedValue(p, value);
}
static inline uint16_t ReadUnalignedUInt16(Address p) {
return ReadUnalignedValue<uint16_t>(p);
}
static inline void WriteUnalignedUInt16(Address p, uint16_t value) {
WriteUnalignedValue(p, value);
}
static inline uint32_t ReadUnalignedUInt32(Address p) {
return ReadUnalignedValue<uint32_t>(p);
}
static inline void WriteUnalignedUInt32(Address p, uint32_t value) {
WriteUnalignedValue(p, value);
}
template <typename V>
static inline V ReadLittleEndianValue(Address p) {
#if defined(V8_TARGET_LITTLE_ENDIAN)
return ReadUnalignedValue<V>(p);
#elif defined(V8_TARGET_BIG_ENDIAN)
V ret{};
const byte* src = reinterpret_cast<const byte*>(p);
byte* dst = reinterpret_cast<byte*>(&ret);
for (size_t i = 0; i < sizeof(V); i++) {
dst[i] = src[sizeof(V) - i - 1];
}
return ret;
#endif // V8_TARGET_LITTLE_ENDIAN
}
template <typename V>
static inline void WriteLittleEndianValue(Address p, V value) {
#if defined(V8_TARGET_LITTLE_ENDIAN)
WriteUnalignedValue<V>(p, value);
#elif defined(V8_TARGET_BIG_ENDIAN)
byte* src = reinterpret_cast<byte*>(&value);
byte* dst = reinterpret_cast<byte*>(p);
for (size_t i = 0; i < sizeof(V); i++) {
dst[i] = src[sizeof(V) - i - 1];
}
#endif // V8_TARGET_LITTLE_ENDIAN
}
} // namespace internal
} // namespace v8
......
......@@ -11,7 +11,7 @@
#include "src/base/compiler-specific.h"
#include "src/flags.h"
#include "src/signature.h"
#include "src/utils.h"
#include "src/v8memory.h"
#include "src/wasm/wasm-result.h"
#include "src/zone/zone-containers.h"
......
......@@ -5,6 +5,7 @@
#include "src/wasm/memory-tracing.h"
#include "src/utils.h"
#include "src/v8memory.h"
namespace v8 {
namespace internal {
......
......@@ -10,8 +10,8 @@
#include "include/v8config.h"
#include "src/base/bits.h"
#include "src/trap-handler/trap-handler.h"
#include "src/utils.h"
#include "src/v8memory.h"
#include "src/wasm/wasm-external-refs.h"
namespace v8 {
......
......@@ -8,6 +8,7 @@
#include "src/signature.h"
#include "src/zone/zone-containers.h"
#include "src/v8memory.h"
#include "src/wasm/leb-helper.h"
#include "src/wasm/local-decl-encoder.h"
#include "src/wasm/wasm-opcodes.h"
......
......@@ -6,7 +6,7 @@
#define V8_WASM_WASM_VALUE_H_
#include "src/boxed-float.h"
#include "src/utils.h"
#include "src/v8memory.h"
#include "src/wasm/wasm-opcodes.h"
#include "src/zone/zone-containers.h"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment