Buffers.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. //
  2. // libtgvoip is free and unencumbered public domain software.
  3. // For more information, see http://unlicense.org or the UNLICENSE file
  4. // you should have received with this source code distribution.
  5. //
  6. #ifndef LIBTGVOIP_BUFFERINPUTSTREAM_H
  7. #define LIBTGVOIP_BUFFERINPUTSTREAM_H
  8. #include <stdio.h>
  9. #include <stdint.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <assert.h>
  13. #include <stdexcept>
  14. #include <array>
  15. #include <limits>
  16. #include <stddef.h>
  17. #include "threading.h"
  18. #include "utils.h"
  19. namespace tgvoip{
  20. class Buffer;
  21. class BufferInputStream{
  22. public:
  23. BufferInputStream(const unsigned char* data, size_t length);
  24. BufferInputStream(const Buffer& buffer);
  25. ~BufferInputStream();
  26. void Seek(size_t offset);
  27. size_t GetLength();
  28. size_t GetOffset();
  29. size_t Remaining();
  30. unsigned char ReadByte();
  31. int64_t ReadInt64();
  32. int32_t ReadInt32();
  33. int16_t ReadInt16();
  34. int32_t ReadTlLength();
  35. void ReadBytes(unsigned char* to, size_t count);
  36. void ReadBytes(Buffer& to);
  37. BufferInputStream GetPartBuffer(size_t length, bool advance);
  38. private:
  39. void EnsureEnoughRemaining(size_t need);
  40. const unsigned char* buffer;
  41. size_t length;
  42. size_t offset;
  43. };
  44. class BufferOutputStream{
  45. friend class Buffer;
  46. public:
  47. TGVOIP_DISALLOW_COPY_AND_ASSIGN(BufferOutputStream);
  48. BufferOutputStream(size_t size);
  49. BufferOutputStream(unsigned char* buffer, size_t size);
  50. ~BufferOutputStream();
  51. void WriteByte(unsigned char byte);
  52. void WriteInt64(int64_t i);
  53. void WriteInt32(int32_t i);
  54. void WriteInt16(int16_t i);
  55. void WriteBytes(const unsigned char* bytes, size_t count);
  56. void WriteBytes(const Buffer& buffer);
  57. void WriteBytes(const Buffer& buffer, size_t offset, size_t count);
  58. unsigned char* GetBuffer();
  59. size_t GetLength();
  60. void Reset();
  61. void Rewind(size_t numBytes);
  62. BufferOutputStream& operator=(BufferOutputStream&& other){
  63. if(this!=&other){
  64. if(!bufferProvided && buffer)
  65. free(buffer);
  66. buffer=other.buffer;
  67. offset=other.offset;
  68. size=other.size;
  69. bufferProvided=other.bufferProvided;
  70. other.buffer=NULL;
  71. }
  72. return *this;
  73. }
  74. private:
  75. void ExpandBufferIfNeeded(size_t need);
  76. unsigned char* buffer=NULL;
  77. size_t size;
  78. size_t offset;
  79. bool bufferProvided;
  80. };
  81. class BufferPool{
  82. public:
  83. TGVOIP_DISALLOW_COPY_AND_ASSIGN(BufferPool);
  84. BufferPool(unsigned int size, unsigned int count);
  85. ~BufferPool();
  86. unsigned char* Get();
  87. void Reuse(unsigned char* buffer);
  88. size_t GetSingleBufferSize();
  89. size_t GetBufferCount();
  90. private:
  91. uint64_t usedBuffers;
  92. int bufferCount;
  93. size_t size;
  94. unsigned char* buffers[64];
  95. Mutex mutex;
  96. };
  97. class Buffer{
  98. public:
  99. Buffer(size_t capacity){
  100. if(capacity>0)
  101. data=(unsigned char *) malloc(capacity);
  102. else
  103. data=NULL;
  104. length=capacity;
  105. };
  106. TGVOIP_DISALLOW_COPY_AND_ASSIGN(Buffer); // use Buffer::CopyOf to copy contents explicitly
  107. Buffer(Buffer&& other) noexcept {
  108. data=other.data;
  109. length=other.length;
  110. other.data=NULL;
  111. };
  112. Buffer(BufferOutputStream&& stream){
  113. data=stream.buffer;
  114. length=stream.offset;
  115. stream.buffer=NULL;
  116. }
  117. Buffer(){
  118. data=NULL;
  119. length=0;
  120. }
  121. ~Buffer(){
  122. if(data)
  123. free(data);
  124. data=NULL;
  125. };
  126. Buffer& operator=(Buffer&& other){
  127. if(this!=&other){
  128. if(data)
  129. free(data);
  130. data=other.data;
  131. length=other.length;
  132. other.data=NULL;
  133. }
  134. return *this;
  135. }
  136. unsigned char& operator[](size_t i){
  137. if(i>=length)
  138. throw std::out_of_range("");
  139. return data[i];
  140. }
  141. const unsigned char& operator[](size_t i) const{
  142. if(i>=length)
  143. throw std::out_of_range("");
  144. return data[i];
  145. }
  146. unsigned char* operator*(){
  147. return data;
  148. }
  149. const unsigned char* operator*() const{
  150. return data;
  151. }
  152. void CopyFrom(const Buffer& other, size_t count, size_t srcOffset=0, size_t dstOffset=0){
  153. if(!other.data)
  154. throw std::invalid_argument("CopyFrom can't copy from NULL");
  155. if(other.length<srcOffset+count || length<dstOffset+count)
  156. throw std::out_of_range("Out of offset+count bounds of either buffer");
  157. memcpy(data+dstOffset, other.data+srcOffset, count);
  158. }
  159. void CopyFrom(const void* ptr, size_t dstOffset, size_t count){
  160. if(length<dstOffset+count)
  161. throw std::out_of_range("Offset+count is out of bounds");
  162. memcpy(data+dstOffset, ptr, count);
  163. }
  164. void Resize(size_t newSize){
  165. data=(unsigned char *) realloc(data, newSize);
  166. length=newSize;
  167. }
  168. size_t Length() const{
  169. return length;
  170. }
  171. bool IsEmpty() const{
  172. return length==0;
  173. }
  174. static Buffer CopyOf(const Buffer& other){
  175. Buffer buf(other.length);
  176. buf.CopyFrom(other, other.length);
  177. return buf;
  178. }
  179. private:
  180. unsigned char* data;
  181. size_t length;
  182. };
  183. template <typename T, size_t size, typename AVG_T=T> class HistoricBuffer{
  184. public:
  185. HistoricBuffer(){
  186. std::fill(data.begin(), data.end(), (T)0);
  187. }
  188. AVG_T Average(){
  189. AVG_T avg=(AVG_T)0;
  190. for(T& i:data){
  191. avg+=i;
  192. }
  193. return avg/(AVG_T)size;
  194. }
  195. AVG_T Average(size_t firstN){
  196. AVG_T avg=(AVG_T)0;
  197. for(size_t i=0;i<firstN;i++){
  198. avg+=(*this)[i];
  199. }
  200. return avg/(AVG_T)firstN;
  201. }
  202. AVG_T NonZeroAverage(){
  203. AVG_T avg=(AVG_T)0;
  204. int nonZeroCount=0;
  205. for(T& i:data){
  206. if(i!=0){
  207. nonZeroCount++;
  208. avg+=i;
  209. }
  210. }
  211. if(nonZeroCount==0)
  212. return (AVG_T)0;
  213. return avg/(AVG_T)nonZeroCount;
  214. }
  215. void Add(T el){
  216. data[offset]=el;
  217. offset=(offset+1)%size;
  218. }
  219. T Min(){
  220. T min=std::numeric_limits<T>::max();
  221. for(T& i:data){
  222. if(i<min)
  223. min=i;
  224. }
  225. return min;
  226. }
  227. T Max(){
  228. T max=std::numeric_limits<T>::min();
  229. for(T& i:data){
  230. if(i>max)
  231. max=i;
  232. }
  233. return max;
  234. }
  235. void Reset(){
  236. std::fill(data.begin(), data.end(), (T)0);
  237. offset=0;
  238. }
  239. T& operator[](size_t i){
  240. assert(i<size);
  241. // [0] should return the most recent entry, [1] the one before it, and so on
  242. ptrdiff_t _i=offset-i-1;
  243. if(_i<0)
  244. _i=size+_i;
  245. return data[_i];
  246. }
  247. size_t Size(){
  248. return size;
  249. }
  250. private:
  251. std::array<T, size> data;
  252. ptrdiff_t offset=0;
  253. };
  254. }
  255. #endif //LIBTGVOIP_BUFFERINPUTSTREAM_H