Buffer Pool problem...

Started by
0 comments, last by Telastyn 19 years, 1 month ago
Hello, I wrote myself a byte buffer pool for my multithreaded application. The pool has a queue of X byte arrays (X defined upon creation). And when you Reserve() a buffer, it will check to see if the queue is not empty, if it's not then it Dequeues a byte array and returns it for you to use. If it is empty, it'll make you a new one. When you're done with it, you're suppose to Release() it and it will requeue it. Making sure that you're not going over the capacity of the pool (otherwise you coudl end up with a memory leak in theory). Here's some code..

		public byte[] Reserve(int requiredSize)
		{
			if(requiredSize <= _maxSize)
			{
				lock(_bufferPool)
				{
					if(_bufferPool.Count > 0)
					{
						return (byte[])_bufferPool.Dequeue();
					}
				}
			}

			return new byte[requiredSize];
		}

		public void Release(byte[] buffer)
		{
			lock(_bufferPool)
			{
				if(_bufferPool.Count < _capacity)
				{
					_bufferPool.Enqueue(buffer);
				}
			}
		}


Ok, now my problem arises when I need to return my current buffer. I obviously can't release the byte buffere BEFORE I return, because then between the release and the return someone else could dequeue and change it (remember, it's multi threaded). I also obviously can't release it AFTER the return, because then I'm out of the function... What I would REALLY love is some sort of auto-release. So when the function which called it ends, the GC is goign to come around and destroy it. Instead of that happening it'd be nice if it auto-released itself back to the queue. Possible? Example:

public byte[] GetSocketData()
{
	byte[] buffer = BufferPool.Reserve();
	socket.Receive(buffer);

	// can't release here... still need to return it!
	return buffer;
	// can't release here... we left the function.
}

public main()
{
	byte[] buffer = BufferPool.Reserve();
	buffer = GetSocketData();

	Handle(buffer);

	BufferPool.Release(buffer);
}


Now this situation is twice as scary because the return is goign to set the references equal... and if you release both you'll end up with 2 of one, and another eaten by the GC right? But lets look past that for a moment... Obviously in that situation I could pass "ref buffer" to GetSocketData() and only ever use 1 buffer, but in some situations it's not so easy. Any other solutions? Anyone have a completly different much more friendly buffer pool system? Edited: Missed some info.
---------------------------------------------------------------"The problem with computers is they do what you tell them.""Computer programmers know how to use their hardware."- Geek#
Advertisement
How about this?

byte buffer[] = BufferPool.Reserve();GetSocketData(&buffer);  // or whatever this language's pass by reference is...// do stuff with buffer


Otherwise, require the programmer to provide the buffer to GetSocketData rather than GetSocketData making its own buffer, and then returning a copy.

This topic is closed to new replies.

Advertisement