libfaster API Documentation  Development Version
Super fast distributted computing
fastCommBuffer.cpp
1 #include "fastCommBuffer.h"
2 
3 faster::fastCommBuffer::fastCommBuffer(){
4  _size = 0;
5  _allocatedSize = BUFFER_INITIAL_SIZE;
6  _data = new char [BUFFER_INITIAL_SIZE];
7  _ownData = true;
8 }
9 faster::fastCommBuffer::fastCommBuffer(size_t s){
10  _size = 0;
11  if (s > 0){
12  _allocatedSize = s;
13  _data = new char [_allocatedSize];
14  _ownData = true;
15  }else{
16  _allocatedSize = 0;
17  _data = NULL;
18  _ownData = false;
19  }
20 }
21 faster::fastCommBuffer::~fastCommBuffer(){
22  if ((_data != NULL) && (_ownData)){
23  //std::cerr << "DEL _DATA\n";
24  delete [] _data;
25  }
26 }
27 
28 void faster::fastCommBuffer::setBuffer(void * buffer, size_t s){
29  //if (_data)
30  // delete [] _data;
31  _size = 0;
32  _data = (char*) buffer;
33  _allocatedSize = s;
34 }
35 void faster::fastCommBuffer::reset(){
36  _size = 0;
37 }
38 
39 char * faster::fastCommBuffer::data(){
40  return _data;
41 }
42 char * faster::fastCommBuffer::pos(size_t pos){
43  return &(_data[pos]);
44 }
45 char * faster::fastCommBuffer::pos(){
46  return &(_data[_size]);
47 }
48 size_t faster::fastCommBuffer::size(){
49  return _size;
50 }
51 size_t faster::fastCommBuffer::free(){
52  return _allocatedSize - _size;
53 }
54 void faster::fastCommBuffer::advance(size_t pos){
55  _size += pos;
56 }
57 
58 void faster::fastCommBuffer::grow(size_t s){
59  if (_allocatedSize < s){
60  //std::cerr << "(GROW BUFFER: "<< _allocatedSize<< " > ";
61  _allocatedSize = std::max(size_t(1.5*_allocatedSize), s + _allocatedSize);
62 
63  char * newdata = new char[_allocatedSize];
64 
65  //memcpy(newdata, _data, _size );
66  std::copy(_data, _data+_size, newdata);
67 
68  delete [] _data;
69 
70  _data = newdata;
71  //std::cerr << _allocatedSize<< ")";
72  }
73 }
74 
75 void faster::fastCommBuffer::print(){
76  for (size_t i = 0; i < _size; ++i){
77  std::cout << (int) _data[i] << ' ';
78  }
79 }
80 
81 void faster::fastCommBuffer::read(procstat &s){
82  read(s.ram);
83  read(s.utime);
84  read(s.stime);
85 }
86 
87 void faster::fastCommBuffer::write(procstat &s){
88  write(s.ram);
89  write(s.utime);
90  write(s.stime);
91 }
92 
93 void faster::fastCommBuffer::writePos(procstat &s, size_t pos){
94  size_t save = _size;
95  _size = pos;
96  write(s);
97  _size = save;
98 }
99 
100 void faster::fastCommBuffer::advance(procstat &s){
101  advance (sizeof(s.ram));
102  advance (sizeof(s.utime));
103  advance (sizeof(s.stime));
104 }