blob: defcc5ebea7ea1163fc041721a5b1eda36fdc443 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
//========= Copyright �, Valve LLC, All rights reserved. ======================
//
// Purpose: Defines a buffer pool used to group small allocations
//
//=============================================================================
#ifndef BUFFERPOOL_H
#define BUFFERPOOL_H
#ifdef _WIN32
#pragma once
#endif
#include "tier1/utlbuffer.h"
#include "tier1/utlvector.h"
namespace GCSDK
{
//----------------------------------------------------------------------------
// Purpose: Defines buffers that can be used to group lots of small allocs
// together to improve performance. The buffers will naturally grow over time
// to accommodate the largest consumers of the feature.
//----------------------------------------------------------------------------
class CBufferPool
{
public:
CBufferPool( const char *pchName, const GCConVar &cvMaxSizeMB, const GCConVar &cvInitBufferSize, int nFlags = 0 );
~CBufferPool();
CUtlBuffer *GetBuffer();
void ReturnBuffer( CUtlBuffer *pBuffer );
static void DumpPools();
private:
static CUtlVector<CBufferPool *> sm_vecBufferPools;
const GCConVar &m_cvMaxSizeMB;
const GCConVar &m_cvInitBufferSize;
int m_nFlags;
int32 m_nBuffersInUse;
int32 m_nHighWatermark;
int32 m_nBuffersTotal;
size_t m_cubFree;
CUtlVector<CUtlBuffer *> m_vecFreeBuffers;
CUtlConstString m_sName;
};
//thread safe version of the above which synchronizes access at the buffer allocate/release
class CBufferPoolMT
{
public:
CBufferPoolMT( const char *pchName, const GCConVar &cvMaxSizeMB, const GCConVar &cvInitBufferSize, int nFlags = 0 ) :
m_BufferPool( pchName, cvMaxSizeMB, cvInitBufferSize, nFlags )
{}
CUtlBuffer *GetBuffer() { AUTO_LOCK( m_mutex ); return m_BufferPool.GetBuffer(); }
void ReturnBuffer( CUtlBuffer *pBuffer ) { AUTO_LOCK( m_mutex ); m_BufferPool.ReturnBuffer( pBuffer ); }
private:
CBufferPool m_BufferPool;
CThreadFastMutex m_mutex;
};
} // namespace GCSDK
#endif // BUFFERPOOL_H
|