00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "bdbuffer.h"
00021
00022 using namespace YAPET;
00023
00033 uint8_t*
00034 BDBuffer::alloc_mem(uint32_t s) throw(YAPETException) {
00035 uint8_t* tmp = (uint8_t*) malloc(s);
00036 if (tmp == NULL)
00037 throw YAPETException("Memory exhausted");
00038
00039 return tmp;
00040 }
00041
00051 void
00052 BDBuffer::free_mem(uint8_t* d, uint32_t s) {
00053 memset(d, 0, s);
00054 free(d);
00055 }
00056
00062 BDBuffer::BDBuffer(uint32_t is) throw(YAPETException) : _size(is) {
00063 data = alloc_mem(_size);
00064 }
00065
00072 BDBuffer::BDBuffer() : _size(0), data(NULL) { }
00073
00074 BDBuffer::BDBuffer(const BDBuffer& ed) throw(YAPETException) {
00075 if (ed.data == NULL) {
00076 data = NULL;
00077 _size = 0;
00078 return;
00079 }
00080
00081 data = alloc_mem(ed._size);
00082 memcpy(data, ed.data, ed._size);
00083 _size = ed._size;
00084 }
00085
00091 BDBuffer::~BDBuffer() {
00092 if (data == NULL) return;
00093 free_mem(data, _size);
00094 }
00095
00110 void
00111 BDBuffer::resize(uint32_t ns) throw(YAPETException) {
00112 if (data == NULL) {
00113 data = alloc_mem(ns);
00114 _size = ns;
00115 return;
00116 }
00117
00118 uint8_t* newbuf = alloc_mem(ns);
00119
00120 if (ns > _size)
00121 memcpy(newbuf, data, _size);
00122 else
00123 memcpy(newbuf, data, ns);
00124
00125 free_mem(data, _size);
00126
00127 _size = ns;
00128 data = newbuf;
00129 }
00130
00146 uint8_t*
00147 BDBuffer::at(uint32_t pos) throw(std::out_of_range) {
00148 if (pos > (_size - 1))
00149 throw std::out_of_range("Position out of range");
00150
00151 return data + pos;
00152 }
00153
00168 const uint8_t*
00169 BDBuffer::at(uint32_t pos) const throw(std::out_of_range) {
00170 if (pos > (_size - 1))
00171 throw std::out_of_range("Position out of range");
00172
00173 return data + pos;
00174 }
00175
00176 const BDBuffer&
00177 BDBuffer::operator=(const BDBuffer& ed) {
00178 if (this == &ed) return *this;
00179
00180 if (data != NULL)
00181 free_mem(data, _size);
00182
00183 if (ed.data != NULL) {
00184 data = alloc_mem(ed._size);
00185 memcpy(data, ed.data, ed._size);
00186 } else {
00187 data = NULL;
00188 }
00189
00190 _size = ed._size;
00191 return *this;
00192 }