1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/crashreporter/google-breakpad/src/common/memory_range.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,145 @@ 1.4 +// Copyright (c) 2011, Google Inc. 1.5 +// All rights reserved. 1.6 +// 1.7 +// Redistribution and use in source and binary forms, with or without 1.8 +// modification, are permitted provided that the following conditions are 1.9 +// met: 1.10 +// 1.11 +// * Redistributions of source code must retain the above copyright 1.12 +// notice, this list of conditions and the following disclaimer. 1.13 +// * Redistributions in binary form must reproduce the above 1.14 +// copyright notice, this list of conditions and the following disclaimer 1.15 +// in the documentation and/or other materials provided with the 1.16 +// distribution. 1.17 +// * Neither the name of Google Inc. nor the names of its 1.18 +// contributors may be used to endorse or promote products derived from 1.19 +// this software without specific prior written permission. 1.20 +// 1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.32 + 1.33 +// memory_range.h: Define the google_breakpad::MemoryRange class, which 1.34 +// is a lightweight wrapper with a pointer and a length to encapsulate 1.35 +// a contiguous range of memory. 1.36 + 1.37 +#ifndef COMMON_MEMORY_RANGE_H_ 1.38 +#define COMMON_MEMORY_RANGE_H_ 1.39 + 1.40 +#include <stddef.h> 1.41 + 1.42 +#include "google_breakpad/common/breakpad_types.h" 1.43 + 1.44 +namespace google_breakpad { 1.45 + 1.46 +// A lightweight wrapper with a pointer and a length to encapsulate a 1.47 +// contiguous range of memory. It provides helper methods for checked 1.48 +// access of a subrange of the memory. Its implemementation does not 1.49 +// allocate memory or call into libc functions, and is thus safer to use 1.50 +// in a crashed environment. 1.51 +class MemoryRange { 1.52 + public: 1.53 + MemoryRange() : data_(NULL), length_(0) {} 1.54 + 1.55 + MemoryRange(const void* data, size_t length) { 1.56 + Set(data, length); 1.57 + } 1.58 + 1.59 + // Returns true if this memory range contains no data. 1.60 + bool IsEmpty() const { 1.61 + // Set() guarantees that |length_| is zero if |data_| is NULL. 1.62 + return length_ == 0; 1.63 + } 1.64 + 1.65 + // Resets to an empty range. 1.66 + void Reset() { 1.67 + data_ = NULL; 1.68 + length_ = 0; 1.69 + } 1.70 + 1.71 + // Sets this memory range to point to |data| and its length to |length|. 1.72 + void Set(const void* data, size_t length) { 1.73 + data_ = reinterpret_cast<const uint8_t*>(data); 1.74 + // Always set |length_| to zero if |data_| is NULL. 1.75 + length_ = data ? length : 0; 1.76 + } 1.77 + 1.78 + // Returns true if this range covers a subrange of |sub_length| bytes 1.79 + // at |sub_offset| bytes of this memory range, or false otherwise. 1.80 + bool Covers(size_t sub_offset, size_t sub_length) const { 1.81 + // The following checks verify that: 1.82 + // 1. sub_offset is within [ 0 .. length_ - 1 ] 1.83 + // 2. sub_offset + sub_length is within 1.84 + // [ sub_offset .. length_ ] 1.85 + return sub_offset < length_ && 1.86 + sub_offset + sub_length >= sub_offset && 1.87 + sub_offset + sub_length <= length_; 1.88 + } 1.89 + 1.90 + // Returns a raw data pointer to a subrange of |sub_length| bytes at 1.91 + // |sub_offset| bytes of this memory range, or NULL if the subrange 1.92 + // is out of bounds. 1.93 + const void* GetData(size_t sub_offset, size_t sub_length) const { 1.94 + return Covers(sub_offset, sub_length) ? (data_ + sub_offset) : NULL; 1.95 + } 1.96 + 1.97 + // Same as the two-argument version of GetData() but uses sizeof(DataType) 1.98 + // as the subrange length and returns an |DataType| pointer for convenience. 1.99 + template <typename DataType> 1.100 + const DataType* GetData(size_t sub_offset) const { 1.101 + return reinterpret_cast<const DataType*>( 1.102 + GetData(sub_offset, sizeof(DataType))); 1.103 + } 1.104 + 1.105 + // Returns a raw pointer to the |element_index|-th element of an array 1.106 + // of elements of length |element_size| starting at |sub_offset| bytes 1.107 + // of this memory range, or NULL if the element is out of bounds. 1.108 + const void* GetArrayElement(size_t element_offset, 1.109 + size_t element_size, 1.110 + unsigned element_index) const { 1.111 + size_t sub_offset = element_offset + element_index * element_size; 1.112 + return GetData(sub_offset, element_size); 1.113 + } 1.114 + 1.115 + // Same as the three-argument version of GetArrayElement() but deduces 1.116 + // the element size using sizeof(ElementType) and returns an |ElementType| 1.117 + // pointer for convenience. 1.118 + template <typename ElementType> 1.119 + const ElementType* GetArrayElement(size_t element_offset, 1.120 + unsigned element_index) const { 1.121 + return reinterpret_cast<const ElementType*>( 1.122 + GetArrayElement(element_offset, sizeof(ElementType), element_index)); 1.123 + } 1.124 + 1.125 + // Returns a subrange of |sub_length| bytes at |sub_offset| bytes of 1.126 + // this memory range, or an empty range if the subrange is out of bounds. 1.127 + MemoryRange Subrange(size_t sub_offset, size_t sub_length) const { 1.128 + return Covers(sub_offset, sub_length) ? 1.129 + MemoryRange(data_ + sub_offset, sub_length) : MemoryRange(); 1.130 + } 1.131 + 1.132 + // Returns a pointer to the beginning of this memory range. 1.133 + const uint8_t* data() const { return data_; } 1.134 + 1.135 + // Returns the length, in bytes, of this memory range. 1.136 + size_t length() const { return length_; } 1.137 + 1.138 + private: 1.139 + // Pointer to the beginning of this memory range. 1.140 + const uint8_t* data_; 1.141 + 1.142 + // Length, in bytes, of this memory range. 1.143 + size_t length_; 1.144 +}; 1.145 + 1.146 +} // namespace google_breakpad 1.147 + 1.148 +#endif // COMMON_MEMORY_RANGE_H_