Planeshift
|
00001 // Copyright (c) 2011, Google Inc. 00002 // All rights reserved. 00003 // 00004 // Redistribution and use in source and binary forms, with or without 00005 // modification, are permitted provided that the following conditions are 00006 // met: 00007 // 00008 // * Redistributions of source code must retain the above copyright 00009 // notice, this list of conditions and the following disclaimer. 00010 // * Redistributions in binary form must reproduce the above 00011 // copyright notice, this list of conditions and the following disclaimer 00012 // in the documentation and/or other materials provided with the 00013 // distribution. 00014 // * Neither the name of Google Inc. nor the names of its 00015 // contributors may be used to endorse or promote products derived from 00016 // this software without specific prior written permission. 00017 // 00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 00030 // memory_range.h: Define the google_breakpad::MemoryRange class, which 00031 // is a lightweight wrapper with a pointer and a length to encapsulate 00032 // a contiguous range of memory. 00033 00034 #ifndef COMMON_MEMORY_RANGE_H_ 00035 #define COMMON_MEMORY_RANGE_H_ 00036 00037 #include <stddef.h> 00038 00039 #include "google_breakpad/common/breakpad_types.h" 00040 00041 namespace google_breakpad { 00042 00043 // A lightweight wrapper with a pointer and a length to encapsulate a 00044 // contiguous range of memory. It provides helper methods for checked 00045 // access of a subrange of the memory. Its implemementation does not 00046 // allocate memory or call into libc functions, and is thus safer to use 00047 // in a crashed environment. 00048 class MemoryRange { 00049 public: 00050 MemoryRange() : data_(NULL), length_(0) {} 00051 00052 MemoryRange(const void* data, size_t length) { 00053 Set(data, length); 00054 } 00055 00056 // Returns true if this memory range contains no data. 00057 bool IsEmpty() const { 00058 // Set() guarantees that |length_| is zero if |data_| is NULL. 00059 return length_ == 0; 00060 } 00061 00062 // Resets to an empty range. 00063 void Reset() { 00064 data_ = NULL; 00065 length_ = 0; 00066 } 00067 00068 // Sets this memory range to point to |data| and its length to |length|. 00069 void Set(const void* data, size_t length) { 00070 data_ = reinterpret_cast<const uint8_t*>(data); 00071 // Always set |length_| to zero if |data_| is NULL. 00072 length_ = data ? length : 0; 00073 } 00074 00075 // Returns true if this range covers a subrange of |sub_length| bytes 00076 // at |sub_offset| bytes of this memory range, or false otherwise. 00077 bool Covers(size_t sub_offset, size_t sub_length) const { 00078 // The following checks verify that: 00079 // 1. sub_offset is within [ 0 .. length_ - 1 ] 00080 // 2. sub_offset + sub_length is within 00081 // [ sub_offset .. length_ ] 00082 return sub_offset < length_ && 00083 sub_offset + sub_length >= sub_offset && 00084 sub_offset + sub_length <= length_; 00085 } 00086 00087 // Returns a raw data pointer to a subrange of |sub_length| bytes at 00088 // |sub_offset| bytes of this memory range, or NULL if the subrange 00089 // is out of bounds. 00090 const void* GetData(size_t sub_offset, size_t sub_length) const { 00091 return Covers(sub_offset, sub_length) ? (data_ + sub_offset) : NULL; 00092 } 00093 00094 // Same as the two-argument version of GetData() but uses sizeof(DataType) 00095 // as the subrange length and returns an |DataType| pointer for convenience. 00096 template <typename DataType> 00097 const DataType* GetData(size_t sub_offset) const { 00098 return reinterpret_cast<const DataType*>( 00099 GetData(sub_offset, sizeof(DataType))); 00100 } 00101 00102 // Returns a raw pointer to the |element_index|-th element of an array 00103 // of elements of length |element_size| starting at |sub_offset| bytes 00104 // of this memory range, or NULL if the element is out of bounds. 00105 const void* GetArrayElement(size_t element_offset, 00106 size_t element_size, 00107 unsigned element_index) const { 00108 size_t sub_offset = element_offset + element_index * element_size; 00109 return GetData(sub_offset, element_size); 00110 } 00111 00112 // Same as the three-argument version of GetArrayElement() but deduces 00113 // the element size using sizeof(ElementType) and returns an |ElementType| 00114 // pointer for convenience. 00115 template <typename ElementType> 00116 const ElementType* GetArrayElement(size_t element_offset, 00117 unsigned element_index) const { 00118 return reinterpret_cast<const ElementType*>( 00119 GetArrayElement(element_offset, sizeof(ElementType), element_index)); 00120 } 00121 00122 // Returns a subrange of |sub_length| bytes at |sub_offset| bytes of 00123 // this memory range, or an empty range if the subrange is out of bounds. 00124 MemoryRange Subrange(size_t sub_offset, size_t sub_length) const { 00125 return Covers(sub_offset, sub_length) ? 00126 MemoryRange(data_ + sub_offset, sub_length) : MemoryRange(); 00127 } 00128 00129 // Returns a pointer to the beginning of this memory range. 00130 const uint8_t* data() const { return data_; } 00131 00132 // Returns the length, in bytes, of this memory range. 00133 size_t length() const { return length_; } 00134 00135 private: 00136 // Pointer to the beginning of this memory range. 00137 const uint8_t* data_; 00138 00139 // Length, in bytes, of this memory range. 00140 size_t length_; 00141 }; 00142 00143 } // namespace google_breakpad 00144 00145 #endif // COMMON_MEMORY_RANGE_H_