aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Input/in_wmvdrm/BufferPool.h
blob: b4c1ec2acbc02135b3de7d8514dfc9faca844894 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#ifndef NULLSOFT_BUFFERPOOLH
#define NULLSOFT_BUFFERPOOLH

#include <wmsdk.h>
#include "../nu/AutoLock.h"
#include <deque>
#include <iostream>
#include <cassert>
using namespace Nullsoft::Utility;
class Buffer;
class Pool
{
public:
	virtual void ReturnBuffer(Buffer *buffer) = 0;
};

class Buffer : public INSSBuffer
{
public:
	Buffer(size_t _size, Pool *_pool)
			: size(_size),
			length(0),
			pool(_pool),
			refCount(1)
	{
		buffer = new unsigned char[size];
	}

	~Buffer()
	{
		delete[] buffer;
	}

	ULONG STDMETHODCALLTYPE AddRef()
	{
		return ++refCount;
	}

	ULONG STDMETHODCALLTYPE Release()
	{
		assert(refCount > 0);

		if (--refCount == 0)
		{
			length = 0;
			pool->ReturnBuffer(this);
		}
		return refCount;
	}

	HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void **ppvObject)
	{
		if (IID_INSSBuffer == iid)
		{
			*ppvObject = static_cast<INSSBuffer *>(this);
			AddRef();
			return S_OK;
		}
		else
		{
			*ppvObject = 0;
			return E_NOINTERFACE;
		}
	}

	HRESULT STDMETHODCALLTYPE GetBuffer(BYTE **ppdwBuffer)
	{
		*ppdwBuffer = (BYTE *)buffer;
		return S_OK;
	}

	HRESULT STDMETHODCALLTYPE GetBufferAndLength(BYTE **ppdwBuffer, DWORD *pdwLength)
	{
		*ppdwBuffer = (BYTE *)buffer;
		*pdwLength = length;
		return S_OK;
	}

	HRESULT STDMETHODCALLTYPE GetLength(DWORD *pdwLength)
	{
		*pdwLength = length;
		return S_OK;
	}

	HRESULT STDMETHODCALLTYPE GetMaxLength(DWORD *pdwLength)
	{
		*pdwLength = size;
		return S_OK;
	}

	HRESULT STDMETHODCALLTYPE SetLength(DWORD dwLength)
	{
		length = dwLength;
		return S_OK;
	}

	bool CanFit(size_t sizeCompare)
	{
		return (sizeCompare <= size);
	}
	size_t size, length;
	Pool *pool;
	int refCount;
	unsigned char *buffer;
};

class BufferPool : public Pool
{
	typedef std::deque<Buffer *> PoolList;
public:
	long limit;
	BufferPool() : allocSize(0),
			poolSize(0),
			limit(0)
	{}

	~BufferPool()
	{
		FreeBuffers();
	}
	void FreeBuffers()
	{

		AutoLock lock (bufferGuard);
		while (!pool.empty())
		{
			Buffer *buff = pool.front();
			delete buff;
			pool.pop_front();
			poolSize = 0;
		}
	}
	void ReturnBuffer(Buffer *buffer)
	{
		AutoLock lock (bufferGuard);
		pool.push_back(buffer);
	}
	Buffer *SearchForBuffer(size_t size)
	{
		PoolList::iterator itr;
		AutoLock lock (bufferGuard);
		for (itr = pool.begin();itr != pool.end();itr++)
		{
			if ((*itr)->CanFit(size))
			{
				Buffer *buff = *itr;
				pool.erase(itr);
				buff->AddRef();
				return buff;
			}
		}
		return 0;
	}

	void PreAllocate(size_t count)
	{
		if (!allocSize)
			return ;
		for (size_t i = 0;i != count;i++)
		{
			AutoLock lock (bufferGuard);
			pool.push_back( new Buffer(allocSize , this));
		}
	}
	INSSBuffer *GetBuffer(size_t size)
	{
		Buffer *buff = SearchForBuffer(size);

		/*
		while (!buff && poolSize>=limit && limit)
		{
			Sleep(1);
			buff = SearchForBuffer(size);
		}*/

		if (!buff)
		{
			poolSize++;
			std::cerr << "poolsize = " << poolSize << std::endl;
			buff = new Buffer(allocSize ? allocSize : size, this);
		}
		return buff;
	}

	void test()
	{
		std::cerr << "pool.size() == " << pool.size();
		std::cerr << " and poolsize == " << poolSize << std::endl;
	}

	void SetAllocSize(long size)
	{
		allocSize = size;
	}

	PoolList pool;
	LockGuard bufferGuard;
	size_t allocSize;
	size_t poolSize;

};

#endif