1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/crashreporter/google-breakpad/src/common/memory.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,218 @@ 1.4 +// Copyright (c) 2009, 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 +#ifndef GOOGLE_BREAKPAD_COMMON_MEMORY_H_ 1.34 +#define GOOGLE_BREAKPAD_COMMON_MEMORY_H_ 1.35 + 1.36 +#include <stdint.h> 1.37 +#include <stdlib.h> 1.38 +#include <unistd.h> 1.39 +#include <sys/mman.h> 1.40 + 1.41 +#ifdef __APPLE__ 1.42 +#define sys_mmap mmap 1.43 +#define sys_mmap2 mmap 1.44 +#define sys_munmap munmap 1.45 +#define MAP_ANONYMOUS MAP_ANON 1.46 +#else 1.47 +#include "third_party/lss/linux_syscall_support.h" 1.48 +#endif 1.49 + 1.50 +namespace google_breakpad { 1.51 + 1.52 +// This is very simple allocator which fetches pages from the kernel directly. 1.53 +// Thus, it can be used even when the heap may be corrupted. 1.54 +// 1.55 +// There is no free operation. The pages are only freed when the object is 1.56 +// destroyed. 1.57 +class PageAllocator { 1.58 + public: 1.59 + PageAllocator() 1.60 + : page_size_(getpagesize()), 1.61 + last_(NULL), 1.62 + current_page_(NULL), 1.63 + page_offset_(0) { 1.64 + } 1.65 + 1.66 + ~PageAllocator() { 1.67 + FreeAll(); 1.68 + } 1.69 + 1.70 + void *Alloc(unsigned bytes) { 1.71 + if (!bytes) 1.72 + return NULL; 1.73 + 1.74 + if (current_page_ && page_size_ - page_offset_ >= bytes) { 1.75 + uint8_t *const ret = current_page_ + page_offset_; 1.76 + page_offset_ += bytes; 1.77 + if (page_offset_ == page_size_) { 1.78 + page_offset_ = 0; 1.79 + current_page_ = NULL; 1.80 + } 1.81 + 1.82 + return ret; 1.83 + } 1.84 + 1.85 + const unsigned pages = 1.86 + (bytes + sizeof(PageHeader) + page_size_ - 1) / page_size_; 1.87 + uint8_t *const ret = GetNPages(pages); 1.88 + if (!ret) 1.89 + return NULL; 1.90 + 1.91 + page_offset_ = (page_size_ - (page_size_ * pages - (bytes + sizeof(PageHeader)))) % page_size_; 1.92 + current_page_ = page_offset_ ? ret + page_size_ * (pages - 1) : NULL; 1.93 + 1.94 + return ret + sizeof(PageHeader); 1.95 + } 1.96 + 1.97 + private: 1.98 + uint8_t *GetNPages(unsigned num_pages) { 1.99 +#ifdef __x86_64 1.100 + void *a = sys_mmap(NULL, page_size_ * num_pages, PROT_READ | PROT_WRITE, 1.101 + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 1.102 +#else 1.103 + void *a = sys_mmap2(NULL, page_size_ * num_pages, PROT_READ | PROT_WRITE, 1.104 + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 1.105 +#endif 1.106 + if (a == MAP_FAILED) 1.107 + return NULL; 1.108 + 1.109 + struct PageHeader *header = reinterpret_cast<PageHeader*>(a); 1.110 + header->next = last_; 1.111 + header->num_pages = num_pages; 1.112 + last_ = header; 1.113 + 1.114 + return reinterpret_cast<uint8_t*>(a); 1.115 + } 1.116 + 1.117 + void FreeAll() { 1.118 + PageHeader *next; 1.119 + 1.120 + for (PageHeader *cur = last_; cur; cur = next) { 1.121 + next = cur->next; 1.122 + sys_munmap(cur, cur->num_pages * page_size_); 1.123 + } 1.124 + } 1.125 + 1.126 + struct PageHeader { 1.127 + PageHeader *next; // pointer to the start of the next set of pages. 1.128 + unsigned num_pages; // the number of pages in this set. 1.129 + }; 1.130 + 1.131 + const unsigned page_size_; 1.132 + PageHeader *last_; 1.133 + uint8_t *current_page_; 1.134 + unsigned page_offset_; 1.135 +}; 1.136 + 1.137 +// A wasteful vector is like a normal std::vector, except that it's very much 1.138 +// simplier and it allocates memory from a PageAllocator. It's wasteful 1.139 +// because, when resizing, it always allocates a whole new array since the 1.140 +// PageAllocator doesn't support realloc. 1.141 +template<class T> 1.142 +class wasteful_vector { 1.143 + public: 1.144 + wasteful_vector(PageAllocator *allocator, unsigned size_hint = 16) 1.145 + : allocator_(allocator), 1.146 + a_((T*) allocator->Alloc(sizeof(T) * size_hint)), 1.147 + allocated_(size_hint), 1.148 + used_(0) { 1.149 + } 1.150 + 1.151 + T& back() { 1.152 + return a_[used_ - 1]; 1.153 + } 1.154 + 1.155 + const T& back() const { 1.156 + return a_[used_ - 1]; 1.157 + } 1.158 + 1.159 + bool empty() const { 1.160 + return used_ == 0; 1.161 + } 1.162 + 1.163 + void push_back(const T& new_element) { 1.164 + if (used_ == allocated_) 1.165 + Realloc(allocated_ * 2); 1.166 + a_[used_++] = new_element; 1.167 + } 1.168 + 1.169 + size_t size() const { 1.170 + return used_; 1.171 + } 1.172 + 1.173 + void resize(unsigned sz, T c = T()) { 1.174 + // No need to test "sz >= 0", as "sz" is unsigned. 1.175 + if (sz <= used_) { 1.176 + used_ = sz; 1.177 + } else { 1.178 + unsigned a = allocated_; 1.179 + if (sz > a) { 1.180 + while (sz > a) { 1.181 + a *= 2; 1.182 + } 1.183 + Realloc(a); 1.184 + } 1.185 + while (sz > used_) { 1.186 + a_[used_++] = c; 1.187 + } 1.188 + } 1.189 + } 1.190 + 1.191 + T& operator[](size_t index) { 1.192 + return a_[index]; 1.193 + } 1.194 + 1.195 + const T& operator[](size_t index) const { 1.196 + return a_[index]; 1.197 + } 1.198 + 1.199 + private: 1.200 + void Realloc(unsigned new_size) { 1.201 + T *new_array = 1.202 + reinterpret_cast<T*>(allocator_->Alloc(sizeof(T) * new_size)); 1.203 + memcpy(new_array, a_, used_ * sizeof(T)); 1.204 + a_ = new_array; 1.205 + allocated_ = new_size; 1.206 + } 1.207 + 1.208 + PageAllocator *const allocator_; 1.209 + T *a_; // pointer to an array of |allocated_| elements. 1.210 + unsigned allocated_; // size of |a_|, in elements. 1.211 + unsigned used_; // number of used slots in |a_|. 1.212 +}; 1.213 + 1.214 +} // namespace google_breakpad 1.215 + 1.216 +inline void* operator new(size_t nbytes, 1.217 + google_breakpad::PageAllocator& allocator) { 1.218 + return allocator.Alloc(nbytes); 1.219 +} 1.220 + 1.221 +#endif // GOOGLE_BREAKPAD_COMMON_MEMORY_H_