blob: d34bce77464ae7b09a971875c9398ad3d3893feb [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
5#ifndef V8_MEMORY_H_
6#define V8_MEMORY_H_
7
8namespace v8 {
9namespace internal {
10
11// Memory provides an interface to 'raw' memory. It encapsulates the casts
12// that typically are needed when incompatible pointer types are used.
13
14class Memory {
15 public:
Kristian Monsen80d68ea2010-09-08 11:05:35 +010016 static uint8_t& uint8_at(Address addr) {
17 return *reinterpret_cast<uint8_t*>(addr);
18 }
19
Steve Blocka7e24c12009-10-30 11:49:00 +000020 static uint16_t& uint16_at(Address addr) {
21 return *reinterpret_cast<uint16_t*>(addr);
22 }
23
24 static uint32_t& uint32_at(Address addr) {
25 return *reinterpret_cast<uint32_t*>(addr);
26 }
27
28 static int32_t& int32_at(Address addr) {
29 return *reinterpret_cast<int32_t*>(addr);
30 }
31
32 static uint64_t& uint64_at(Address addr) {
33 return *reinterpret_cast<uint64_t*>(addr);
34 }
35
36 static int& int_at(Address addr) {
37 return *reinterpret_cast<int*>(addr);
38 }
39
Ben Murdoch3ef787d2012-04-12 10:51:47 +010040 static unsigned& unsigned_at(Address addr) {
41 return *reinterpret_cast<unsigned*>(addr);
42 }
43
Ben Murdochb8a8cc12014-11-26 15:28:44 +000044 static intptr_t& intptr_at(Address addr) {
45 return *reinterpret_cast<intptr_t*>(addr);
46 }
47
48 static uintptr_t& uintptr_at(Address addr) {
49 return *reinterpret_cast<uintptr_t*>(addr);
50 }
51
Ben Murdochb0fe1622011-05-05 13:52:32 +010052 static double& double_at(Address addr) {
53 return *reinterpret_cast<double*>(addr);
54 }
55
Steve Blocka7e24c12009-10-30 11:49:00 +000056 static Address& Address_at(Address addr) {
57 return *reinterpret_cast<Address*>(addr);
58 }
59
60 static Object*& Object_at(Address addr) {
61 return *reinterpret_cast<Object**>(addr);
62 }
Steve Block3ce2e202009-11-05 08:53:23 +000063
64 static Handle<Object>& Object_Handle_at(Address addr) {
65 return *reinterpret_cast<Handle<Object>*>(addr);
66 }
Ben Murdoch61f157c2016-09-16 13:49:30 +010067
68 static bool IsAddressInRange(Address base, Address address, uint32_t size) {
69 uintptr_t numeric_base = reinterpret_cast<uintptr_t>(base);
70 uintptr_t numeric_address = reinterpret_cast<uintptr_t>(address);
71 return numeric_base <= numeric_address &&
72 numeric_address < numeric_base + size;
73 }
Steve Blocka7e24c12009-10-30 11:49:00 +000074};
75
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000076} // namespace internal
77} // namespace v8
Steve Blocka7e24c12009-10-30 11:49:00 +000078
79#endif // V8_MEMORY_H_