Home | History | Annotate | Download | only in src
      1 // Copyright 2011 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      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:
     16   static uint8_t& uint8_at(Address addr) {
     17     return *reinterpret_cast<uint8_t*>(addr);
     18   }
     19 
     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 
     40   static unsigned& unsigned_at(Address addr) {
     41     return *reinterpret_cast<unsigned*>(addr);
     42   }
     43 
     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 
     52   static double& double_at(Address addr)  {
     53     return *reinterpret_cast<double*>(addr);
     54   }
     55 
     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   }
     63 
     64   static Handle<Object>& Object_Handle_at(Address addr)  {
     65     return *reinterpret_cast<Handle<Object>*>(addr);
     66   }
     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   }
     74 };
     75 
     76 }  // namespace internal
     77 }  // namespace v8
     78 
     79 #endif  // V8_MEMORY_H_
     80