1f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// Copyright (c) 2011, Google Inc. 2f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// All rights reserved. 3f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// 4f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// Redistribution and use in source and binary forms, with or without 5f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// modification, are permitted provided that the following conditions are 6f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// met: 7f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// 8f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// * Redistributions of source code must retain the above copyright 9f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// notice, this list of conditions and the following disclaimer. 10f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// * Redistributions in binary form must reproduce the above 11f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// copyright notice, this list of conditions and the following disclaimer 12f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// in the documentation and/or other materials provided with the 13f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// distribution. 14f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// * Neither the name of Google Inc. nor the names of its 15f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// contributors may be used to endorse or promote products derived from 16f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// this software without specific prior written permission. 17f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// 18f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org 30f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// memory_range.h: Define the google_breakpad::MemoryRange class, which 31f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// is a lightweight wrapper with a pointer and a length to encapsulate 32f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// a contiguous range of memory. 33f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org 34f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org#ifndef COMMON_MEMORY_RANGE_H_ 35f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org#define COMMON_MEMORY_RANGE_H_ 36f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org 37f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org#include <stddef.h> 38f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org 39f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org#include "google_breakpad/common/breakpad_types.h" 40f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org 41f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.orgnamespace google_breakpad { 42f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org 43f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// A lightweight wrapper with a pointer and a length to encapsulate a 44f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// contiguous range of memory. It provides helper methods for checked 45f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// access of a subrange of the memory. Its implemementation does not 46f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// allocate memory or call into libc functions, and is thus safer to use 47f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org// in a crashed environment. 48f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.orgclass MemoryRange { 49f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org public: 50f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org MemoryRange() : data_(NULL), length_(0) {} 51f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org 52f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org MemoryRange(const void* data, size_t length) { 53f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org Set(data, length); 54f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org } 55f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org 56f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org // Returns true if this memory range contains no data. 57f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org bool IsEmpty() const { 58f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org // Set() guarantees that |length_| is zero if |data_| is NULL. 59f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org return length_ == 0; 60f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org } 61f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org 62f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org // Resets to an empty range. 63f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org void Reset() { 64f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org data_ = NULL; 65f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org length_ = 0; 66f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org } 67f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org 68f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org // Sets this memory range to point to |data| and its length to |length|. 69f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org void Set(const void* data, size_t length) { 706162aed3c3fcfc53373c963ac375d39a5dfa5a25ted.mielczarek@gmail.com data_ = reinterpret_cast<const uint8_t*>(data); 71f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org // Always set |length_| to zero if |data_| is NULL. 72f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org length_ = data ? length : 0; 73f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org } 74f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org 75f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org // Returns true if this range covers a subrange of |sub_length| bytes 76f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org // at |sub_offset| bytes of this memory range, or false otherwise. 77f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org bool Covers(size_t sub_offset, size_t sub_length) const { 78f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org // The following checks verify that: 79f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org // 1. sub_offset is within [ 0 .. length_ - 1 ] 80f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org // 2. sub_offset + sub_length is within 81f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org // [ sub_offset .. length_ ] 82f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org return sub_offset < length_ && 83f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org sub_offset + sub_length >= sub_offset && 84f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org sub_offset + sub_length <= length_; 85f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org } 86f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org 87f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org // Returns a raw data pointer to a subrange of |sub_length| bytes at 88f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org // |sub_offset| bytes of this memory range, or NULL if the subrange 89f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org // is out of bounds. 90f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org const void* GetData(size_t sub_offset, size_t sub_length) const { 91f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org return Covers(sub_offset, sub_length) ? (data_ + sub_offset) : NULL; 92f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org } 93f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org 94f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org // Same as the two-argument version of GetData() but uses sizeof(DataType) 95f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org // as the subrange length and returns an |DataType| pointer for convenience. 96f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org template <typename DataType> 97f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org const DataType* GetData(size_t sub_offset) const { 98f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org return reinterpret_cast<const DataType*>( 99f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org GetData(sub_offset, sizeof(DataType))); 100f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org } 101f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org 102f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org // Returns a raw pointer to the |element_index|-th element of an array 103f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org // of elements of length |element_size| starting at |sub_offset| bytes 104f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org // of this memory range, or NULL if the element is out of bounds. 105f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org const void* GetArrayElement(size_t element_offset, 106f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org size_t element_size, 107f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org unsigned element_index) const { 108f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org size_t sub_offset = element_offset + element_index * element_size; 109f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org return GetData(sub_offset, element_size); 110f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org } 111f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org 112f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org // Same as the three-argument version of GetArrayElement() but deduces 113f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org // the element size using sizeof(ElementType) and returns an |ElementType| 114f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org // pointer for convenience. 115f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org template <typename ElementType> 116f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org const ElementType* GetArrayElement(size_t element_offset, 117f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org unsigned element_index) const { 118f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org return reinterpret_cast<const ElementType*>( 119f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org GetArrayElement(element_offset, sizeof(ElementType), element_index)); 120f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org } 121f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org 122f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org // Returns a subrange of |sub_length| bytes at |sub_offset| bytes of 123f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org // this memory range, or an empty range if the subrange is out of bounds. 124f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org MemoryRange Subrange(size_t sub_offset, size_t sub_length) const { 125f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org return Covers(sub_offset, sub_length) ? 126f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org MemoryRange(data_ + sub_offset, sub_length) : MemoryRange(); 127f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org } 128f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org 129f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org // Returns a pointer to the beginning of this memory range. 1306162aed3c3fcfc53373c963ac375d39a5dfa5a25ted.mielczarek@gmail.com const uint8_t* data() const { return data_; } 131f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org 132f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org // Returns the length, in bytes, of this memory range. 133f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org size_t length() const { return length_; } 134f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org 135f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org private: 136f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org // Pointer to the beginning of this memory range. 1376162aed3c3fcfc53373c963ac375d39a5dfa5a25ted.mielczarek@gmail.com const uint8_t* data_; 138f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org 139f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org // Length, in bytes, of this memory range. 140f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org size_t length_; 141f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org}; 142f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org 143f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org} // namespace google_breakpad 144f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org 145f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org#endif // COMMON_MEMORY_RANGE_H_ 146