Buffer.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
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
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