Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
| 5 | #ifndef V8_MEMORY_H_ |
| 6 | #define V8_MEMORY_H_ |
| 7 | |
| 8 | namespace v8 { |
| 9 | namespace 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 | |
| 14 | class Memory { |
| 15 | public: |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 16 | static uint8_t& uint8_at(Address addr) { |
| 17 | return *reinterpret_cast<uint8_t*>(addr); |
| 18 | } |
| 19 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 20 | 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 Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 40 | static unsigned& unsigned_at(Address addr) { |
| 41 | return *reinterpret_cast<unsigned*>(addr); |
| 42 | } |
| 43 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 44 | 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 Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 52 | static double& double_at(Address addr) { |
| 53 | return *reinterpret_cast<double*>(addr); |
| 54 | } |
| 55 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 56 | 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 Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 63 | |
| 64 | static Handle<Object>& Object_Handle_at(Address addr) { |
| 65 | return *reinterpret_cast<Handle<Object>*>(addr); |
| 66 | } |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 67 | |
| 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 76 | } // namespace internal |
| 77 | } // namespace v8 |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 78 | |
| 79 | #endif // V8_MEMORY_H_ |