Jump to content
  • Advertisement
Sign in to follow this  
GeekSharp

Buffer Pool problem...

This topic is 4964 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!