Skip to content

Instantly share code, notes, and snippets.

@toptensoftware
Last active October 5, 2016 05:56
Show Gist options
  • Select an option

  • Save toptensoftware/eaa7a835b3c763f5b7be45c6c2566c20 to your computer and use it in GitHub Desktop.

Select an option

Save toptensoftware/eaa7a835b3c763f5b7be45c6c2566c20 to your computer and use it in GitHub Desktop.
Win3mu's RangeAllocator
// Manages allocations within an "address space".
public class RangeAllocator<T>
{
// Constructor
public RangeAllocator(int addressSpaceSize);
// Info accessors
public int AddressSpaceSize { get; set; }
public int FreeSpace { get; }
public int EntryCount { get; }
// Defrag the heap, returns true if anything moved
public bool Defrag();
// Allocate
public Range Alloc(int size, bool moveable, bool allowDefrag);
public bool ReAlloc(Range allocation, int newSize, bool moveable, bool allocDefrag);
public bool ReAllocInPlace(Range alloc, int newSize);
public void Free(Range allocation);
// Must be provided to allow move operations
public IMoveListener MoveListener;
// Publicly visible view on an allocation
public abstract class Range
{
public abstract int Position { get; }
public abstract int Size { get; }
public abstract bool Moveable { get; }
public T User;
public int LockCount;
}
// Notify the client that a range is moving
// The local heap listens to this to move the actual data
// behind any allocations.
public interface IMoveListener
{
void Move(Range user, int newPosition);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment