00001 /* 00002 * See the dyninst/COPYRIGHT file for copyright information. 00003 * 00004 * We provide the Paradyn Tools (below described as "Paradyn") 00005 * on an AS IS basis, and do not warrant its validity or performance. 00006 * We reserve the right to update, modify, or discontinue this 00007 * software at any time. We shall have no obligation to supply such 00008 * updates or modifications or any other form of support to you. 00009 * 00010 * By your use of Paradyn, you understand and agree that we (or any 00011 * other person or entity with proprietary rights in Paradyn) are 00012 * under no obligation to provide either maintenance services, 00013 * update services, notices of latent defects, or correction of 00014 * defects for Paradyn. 00015 * 00016 * This library is free software; you can redistribute it and/or 00017 * modify it under the terms of the GNU Lesser General Public 00018 * License as published by the Free Software Foundation; either 00019 * version 2.1 of the License, or (at your option) any later version. 00020 * 00021 * This library is distributed in the hope that it will be useful, 00022 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00024 * Lesser General Public License for more details. 00025 * 00026 * You should have received a copy of the GNU Lesser General Public 00027 * License along with this library; if not, write to the Free Software 00028 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00029 */ 00030 #if !defined(_BUFFER_H_) 00031 #define _BUFFER_H_ 00032 00033 #include <assert.h> 00034 #include <string.h> 00035 #include "dyntypes.h" 00036 00037 namespace Dyninst { 00038 00039 // A class to support multiple forms of code generation. The design of this class is as 00040 // a tiered model: 00041 00042 // Tier 1: A buffer of bytes that represent position-dependent executable code 00043 // Tier 2: A buffer that supports linker-style relocations of raw instructions 00044 // Tier 3: A buffer that supports relocatable and optimizer-friendly instruction objects 00045 00046 // The current implementation supports tier 1. In that, it is a cut-down version of the 00047 // Dyninst internal codeGen structure that aims to be more user-friendly. Tiers 2 and 3 00048 // are TODO. 00049 00050 class COMMON_EXPORT Buffer { 00051 public: 00052 Buffer(Address addr, unsigned initial_size); 00053 Buffer(); 00054 void initialize(Address addr, unsigned initial_size); 00055 ~Buffer(); 00056 00057 static const int ALLOCATION_UNIT; 00058 00059 template <class InputIterator> 00060 void copy(InputIterator begin, InputIterator end); 00061 void copy(void *buffer, unsigned size); 00062 00063 unsigned size() const; 00064 unsigned max_size() const; 00065 bool empty() const; 00066 00067 template <class Input> 00068 void push_back(const Input &); 00069 00070 template <class storage> 00071 class iterator { 00072 friend class Buffer; 00073 00074 public: 00075 iterator() : pos((storage *)-1) {}; 00076 ~iterator() {}; 00077 storage operator*() const { 00078 assert(valid); 00079 return *pos; 00080 } 00081 00082 bool operator==(const iterator<storage> &rhs) const { 00083 return rhs.pos == pos; 00084 } 00085 bool operator!=(const iterator<storage> &rhs) const { 00086 return rhs.pos != pos; 00087 } 00088 iterator<storage> operator++() { 00089 assert(valid); 00090 iterator<storage> i = *this; 00091 ++pos; 00092 return i; 00093 } 00094 00095 iterator<storage> operator++(int) { 00096 assert(valid); 00097 ++pos; 00098 return *this; 00099 } 00100 00101 private: 00102 bool valid() const { return pos != (storage *)-1; }; 00103 iterator(storage *start) : pos(start) {}; 00104 storage *pos; 00105 }; 00106 00107 typedef iterator<unsigned char> byte_iterator; 00108 typedef iterator<unsigned int> word_iterator; 00109 typedef iterator<unsigned long> long_iterator; 00110 00111 byte_iterator begin() const; 00112 byte_iterator end() const; 00113 00114 word_iterator w_begin() const; 00115 word_iterator w_end() const; 00116 00117 long_iterator l_begin() const; 00118 long_iterator l_end() const; 00119 00120 unsigned char *start_ptr() const { return buffer_; } 00121 00122 Address startAddr() const { return start_; } 00123 Address curAddr() const { return start_ + size_; } 00124 00125 private: 00126 // May call realloc(); 00127 void increase_allocation(int added); 00128 unsigned char * cur_ptr() const; 00129 00130 unsigned char * buffer_; 00131 unsigned size_; 00132 unsigned max_; 00133 00134 Address start_; 00135 }; 00136 00137 template <class InputIterator> 00138 void Buffer::copy(InputIterator begin, InputIterator end) { 00139 while (begin != end) { 00140 push_back(*begin); 00141 ++begin; 00142 } 00143 }; 00144 00145 00146 00147 template <class Input> 00148 void Buffer::push_back(const Input &i) { 00149 if (size_ + sizeof(i) >= max_) { 00150 increase_allocation(sizeof(i)); 00151 } 00152 Input *ptr = (Input *)cur_ptr(); 00153 *ptr = i; 00154 size_ += sizeof(i); 00155 }; 00156 00157 } 00158 00159 #endif
1.6.1