michael@0: // Copyright (c) 2011, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: // memory_range.h: Define the google_breakpad::MemoryRange class, which michael@0: // is a lightweight wrapper with a pointer and a length to encapsulate michael@0: // a contiguous range of memory. michael@0: michael@0: #ifndef COMMON_MEMORY_RANGE_H_ michael@0: #define COMMON_MEMORY_RANGE_H_ michael@0: michael@0: #include michael@0: michael@0: #include "google_breakpad/common/breakpad_types.h" michael@0: michael@0: namespace google_breakpad { michael@0: michael@0: // A lightweight wrapper with a pointer and a length to encapsulate a michael@0: // contiguous range of memory. It provides helper methods for checked michael@0: // access of a subrange of the memory. Its implemementation does not michael@0: // allocate memory or call into libc functions, and is thus safer to use michael@0: // in a crashed environment. michael@0: class MemoryRange { michael@0: public: michael@0: MemoryRange() : data_(NULL), length_(0) {} michael@0: michael@0: MemoryRange(const void* data, size_t length) { michael@0: Set(data, length); michael@0: } michael@0: michael@0: // Returns true if this memory range contains no data. michael@0: bool IsEmpty() const { michael@0: // Set() guarantees that |length_| is zero if |data_| is NULL. michael@0: return length_ == 0; michael@0: } michael@0: michael@0: // Resets to an empty range. michael@0: void Reset() { michael@0: data_ = NULL; michael@0: length_ = 0; michael@0: } michael@0: michael@0: // Sets this memory range to point to |data| and its length to |length|. michael@0: void Set(const void* data, size_t length) { michael@0: data_ = reinterpret_cast(data); michael@0: // Always set |length_| to zero if |data_| is NULL. michael@0: length_ = data ? length : 0; michael@0: } michael@0: michael@0: // Returns true if this range covers a subrange of |sub_length| bytes michael@0: // at |sub_offset| bytes of this memory range, or false otherwise. michael@0: bool Covers(size_t sub_offset, size_t sub_length) const { michael@0: // The following checks verify that: michael@0: // 1. sub_offset is within [ 0 .. length_ - 1 ] michael@0: // 2. sub_offset + sub_length is within michael@0: // [ sub_offset .. length_ ] michael@0: return sub_offset < length_ && michael@0: sub_offset + sub_length >= sub_offset && michael@0: sub_offset + sub_length <= length_; michael@0: } michael@0: michael@0: // Returns a raw data pointer to a subrange of |sub_length| bytes at michael@0: // |sub_offset| bytes of this memory range, or NULL if the subrange michael@0: // is out of bounds. michael@0: const void* GetData(size_t sub_offset, size_t sub_length) const { michael@0: return Covers(sub_offset, sub_length) ? (data_ + sub_offset) : NULL; michael@0: } michael@0: michael@0: // Same as the two-argument version of GetData() but uses sizeof(DataType) michael@0: // as the subrange length and returns an |DataType| pointer for convenience. michael@0: template michael@0: const DataType* GetData(size_t sub_offset) const { michael@0: return reinterpret_cast( michael@0: GetData(sub_offset, sizeof(DataType))); michael@0: } michael@0: michael@0: // Returns a raw pointer to the |element_index|-th element of an array michael@0: // of elements of length |element_size| starting at |sub_offset| bytes michael@0: // of this memory range, or NULL if the element is out of bounds. michael@0: const void* GetArrayElement(size_t element_offset, michael@0: size_t element_size, michael@0: unsigned element_index) const { michael@0: size_t sub_offset = element_offset + element_index * element_size; michael@0: return GetData(sub_offset, element_size); michael@0: } michael@0: michael@0: // Same as the three-argument version of GetArrayElement() but deduces michael@0: // the element size using sizeof(ElementType) and returns an |ElementType| michael@0: // pointer for convenience. michael@0: template michael@0: const ElementType* GetArrayElement(size_t element_offset, michael@0: unsigned element_index) const { michael@0: return reinterpret_cast( michael@0: GetArrayElement(element_offset, sizeof(ElementType), element_index)); michael@0: } michael@0: michael@0: // Returns a subrange of |sub_length| bytes at |sub_offset| bytes of michael@0: // this memory range, or an empty range if the subrange is out of bounds. michael@0: MemoryRange Subrange(size_t sub_offset, size_t sub_length) const { michael@0: return Covers(sub_offset, sub_length) ? michael@0: MemoryRange(data_ + sub_offset, sub_length) : MemoryRange(); michael@0: } michael@0: michael@0: // Returns a pointer to the beginning of this memory range. michael@0: const uint8_t* data() const { return data_; } michael@0: michael@0: // Returns the length, in bytes, of this memory range. michael@0: size_t length() const { return length_; } michael@0: michael@0: private: michael@0: // Pointer to the beginning of this memory range. michael@0: const uint8_t* data_; michael@0: michael@0: // Length, in bytes, of this memory range. michael@0: size_t length_; michael@0: }; michael@0: michael@0: } // namespace google_breakpad michael@0: michael@0: #endif // COMMON_MEMORY_RANGE_H_