Vixen
02b45cc4
csharp
public sealed class HandlePool<T>

Stores items in a contiguous table and hands out Handle`1s to them. Removing an item frees its slot for reuse and bumps that slot's generation, so every handle taken before the removal is detectably stale.

No guide page documents this yet — the page shows what the code says about itself.

Remarks

The engine's answer to "how do I refer to a GPU resource". Use-after-free becomes a detected generation mismatch instead of a native crash, and it costs one comparison on the lookup path.

Not thread-safe. The RHI's resource tables are written from one thread and read from many after a barrier; a pool that locked on every lookup would be the wrong trade for that.

Fields and properties (2)

  • public int Count

    How many items are stored.

  • public int Capacity

    How many slots the table holds, live and free together.

Methods (9)

  • public HandlePool(int capacity = 16)

    Creates a pool with room for items before it grows.

  • public Handle<T> Add(T item)

    Stores an item and returns a handle to it.

  • public bool Contains(Handle<T> handle)

    Whether a handle still refers to a stored item.

  • public bool TryGet(Handle<T> handle, out T item)

    Looks up the item a handle refers to.

  • public T Get(Handle<T> handle)

    Looks up the item a handle refers to, or throws.

  • public ref T? GetReference(Handle<T> handle)

    A reference to the stored item, for mutating a large struct in place without copying it out and back.

  • public bool Remove(Handle<T> handle)

    Removes the item a handle refers to and frees its slot.

  • public void Clear()

    Removes everything. Outstanding handles all become stale.

  • public HandlePool<T>.Enumerator GetEnumerator()

    Enumerates the live handles and their items, in slot order.

Used by (6)

  • EnumeratorVixen.Core.Collections
  • GlDeviceVixen.Graphics.OpenGL
  • HandleTestsVixen.Core.Collections.Tests
  • NullDeviceVixen.Graphics.Null
  • VulkanDeviceVixen.Graphics.Vulkan
  • WebGpuDeviceVixen.Graphics.WebGPU