forked from metin-server/m2dev-client-src
- Reuses vector<uint8_t> buffers to reduce allocations - Thread-safe with mutex protection - Max 64 buffers, 64MB buffer size limit - Tracks allocation statistics and pooled memory
51 lines
1002 B
C++
51 lines
1002 B
C++
#ifndef __INC_ETERLIB_BUFFERPOOL_H__
|
|
#define __INC_ETERLIB_BUFFERPOOL_H__
|
|
|
|
#include <vector>
|
|
#include <mutex>
|
|
#include <cstdint>
|
|
|
|
// Buffer pool for file I/O operations
|
|
class CBufferPool
|
|
{
|
|
public:
|
|
CBufferPool();
|
|
~CBufferPool();
|
|
|
|
// Get buffer with minimum size
|
|
std::vector<uint8_t> Acquire(size_t minSize);
|
|
|
|
// Return buffer to pool
|
|
void Release(std::vector<uint8_t>&& buffer);
|
|
|
|
// Get statistics
|
|
size_t GetPoolSize() const;
|
|
size_t GetTotalAllocated() const;
|
|
size_t GetTotalMemoryPooled() const; // Total bytes held in pool
|
|
|
|
// Clear pool
|
|
void Clear();
|
|
|
|
private:
|
|
struct TPooledBuffer
|
|
{
|
|
std::vector<uint8_t> buffer;
|
|
size_t capacity;
|
|
|
|
TPooledBuffer(std::vector<uint8_t>&& buf)
|
|
: buffer(std::move(buf))
|
|
, capacity(buffer.capacity())
|
|
{
|
|
}
|
|
};
|
|
|
|
std::vector<TPooledBuffer> m_pool;
|
|
mutable std::mutex m_mutex;
|
|
size_t m_totalAllocated;
|
|
|
|
static const size_t MAX_POOL_SIZE = 64;
|
|
static const size_t MAX_BUFFER_SIZE = 64 * 1024 * 1024;
|
|
};
|
|
|
|
#endif // __INC_ETERLIB_BUFFERPOOL_H__
|