| 1 | /* |
| 2 | * This is the source code of tgnet library v. 1.1 |
| 3 | * It is licensed under GNU GPL v. 2 or later. |
| 4 | * You should have received a copy of the license in this archive (see LICENSE). |
| 5 | * |
| 6 | * Copyright Nikolai Kudashov, 2015-2018. |
| 7 | */ |
| 8 | |
| 9 | #ifndef BUFFERSSTORAGE_H |
| 10 | #define BUFFERSSTORAGE_H |
| 11 | |
| 12 | #include <vector> |
| 13 | #include <pthread.h> |
| 14 | #include <stdint.h> |
| 15 | |
| 16 | class NativeByteBuffer; |
| 17 | |
| 18 | class BuffersStorage { |
| 19 | |
| 20 | public: |
| 21 | BuffersStorage(bool threadSafe); |
| 22 | NativeByteBuffer *getFreeBuffer(uint32_t size); |
| 23 | void reuseFreeBuffer(NativeByteBuffer *buffer); |
| 24 | static BuffersStorage &getInstance(); |
| 25 | |
| 26 | private: |
| 27 | std::vector<NativeByteBuffer *> freeBuffers8; |
| 28 | std::vector<NativeByteBuffer *> freeBuffers128; |
| 29 | std::vector<NativeByteBuffer *> freeBuffers1024; |
| 30 | std::vector<NativeByteBuffer *> freeBuffers4096; |
| 31 | std::vector<NativeByteBuffer *> freeBuffers16384; |
| 32 | std::vector<NativeByteBuffer *> freeBuffers32768; |
| 33 | std::vector<NativeByteBuffer *> freeBuffersBig; |
| 34 | bool isThreadSafe = true; |
| 35 | pthread_mutex_t mutex; |
| 36 | }; |
| 37 | |
| 38 | #endif |
| 39 | |