Buffer.C

Go to the documentation of this file.
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 #include "Buffer.h"
00031 #include <stdlib.h>
00032 
00033 using namespace Dyninst;
00034 
00035 const int Buffer::ALLOCATION_UNIT = 256;
00036 
00037 Buffer::Buffer() : buffer_(NULL), size_(0), max_(0), start_(0) {}
00038 
00039 Buffer::Buffer(Address addr, unsigned initial_size) : buffer_(NULL), size_(0), max_(0), start_(0) {
00040    initialize(addr, initial_size);
00041 };
00042 
00043 Buffer::~Buffer() {
00044    assert(buffer_);
00045    free(buffer_);
00046 };
00047 
00048 void Buffer::initialize(Address a, unsigned s) {
00049    assert(buffer_ == NULL);
00050    start_ = a;
00051    increase_allocation(s);
00052 }
00053    
00054 
00055 unsigned Buffer::size() const {
00056    return size_;
00057 };
00058 unsigned Buffer::max_size() const {
00059    return max_;
00060 };
00061 bool Buffer::empty() const {
00062    return (size_ == 0);
00063 };
00064 
00065 void Buffer::increase_allocation(int size) {
00066    if (size <= 0) return;
00067    // Round size up to the next allocation unit
00068    size = ((size / ALLOCATION_UNIT) + 1) * ALLOCATION_UNIT;
00069    max_ += size;
00070    buffer_ = (unsigned char *)::realloc(buffer_, max_);
00071    assert(buffer_);
00072 }
00073 
00074 unsigned char *Buffer::cur_ptr() const {
00075    return &(buffer_[size_]);
00076 }
00077 
00078 Buffer::byte_iterator Buffer::begin() const {
00079    return byte_iterator(buffer_);
00080 }
00081 
00082 Buffer::byte_iterator Buffer::end() const {
00083    return byte_iterator(cur_ptr());
00084 }
00085 
00086 namespace Dyninst {
00087    template<>
00088    void Buffer::copy(unsigned char *begin, unsigned char *end) {
00089       unsigned added_size = (long)end - (long)begin;
00090       if ((size_ + added_size) > max_) 
00091          increase_allocation(added_size);
00092       memcpy(cur_ptr(), begin, added_size);
00093       size_ += added_size;
00094    }
00095 }   
00096 
00097 void Buffer::copy(void *buf, unsigned size) {
00098    unsigned char *begin = (unsigned char *) buf;
00099    unsigned char *end = begin + size;
00100 
00101    copy(begin, end);
00102 }
00103    
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 12 Jul 2013 for SymtabAPI by  doxygen 1.6.1