Files
mirror-processhacker/trunk/ProcessHacker.Common/Objects/HandleTable.cs
T
wj32 c226494bf4 small HandleTable change
git-svn-id: svn://svn.code.sf.net/p/processhacker/code@1437 21ef857c-d57f-4fe0-8362-d861dc6d29cd
2009-06-20 04:00:45 +00:00

86 lines
2.1 KiB
C#

using System.Collections.Generic;
namespace ProcessHacker.Common.Objects
{
/// <summary>
/// Provides methods for managing handles to objects.
/// </summary>
public class HandleTable : BaseObject
{
private IdGenerator _handleGenerator = new IdGenerator(4, 4);
private Dictionary<int, BaseObject> _handles =
new Dictionary<int, BaseObject>();
protected override void DisposeObject(bool disposing)
{
lock (_handles)
{
foreach (var obj in _handles.Values)
obj.Dereference(disposing);
}
}
public int Allocate(BaseObject obj)
{
int handle = _handleGenerator.Pop();
obj.Reference();
obj.Dispose(); // GC should not own the object.
lock (_handles)
_handles.Add(handle, obj);
return handle;
}
public bool Free(int handle)
{
BaseObject obj;
lock (_handles)
{
if (!_handles.ContainsKey(handle))
return false;
obj = _handles[handle];
_handles.Remove(handle);
}
_handleGenerator.Push(handle);
obj.Dereference();
return true;
}
public BaseObject GetHandleObject(int handle)
{
lock (_handles)
{
if (_handles.ContainsKey(handle))
return _handles[handle];
else
return null;
}
}
public BaseObject ReferenceByHandle(int handle)
{
lock (_handles)
{
if (_handles.ContainsKey(handle))
{
BaseObject obj = _handles[handle];
obj.Reference();
return obj;
}
else
{
return null;
}
}
}
}
}