Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save toptensoftware/d95b7c75058c847195881976ec5f35cf to your computer and use it in GitHub Desktop.
Selector and Allocation classes
// Allocation represents the actual memory behind one or more selectors
public class Allocation
{
public Allocation(uint bytes, ushort flags)
{
this.flags = flags;
buffer = new byte[bytes];
}
public ushort flags; // GlobalAlloc flags
public byte[] buffer; // The actual "memory"!
public LocalHeap localHeap; // Only if this allocation has a local heap
}
// This selector class is very similar to a selector descriptor table
// in a real processor. It maps a 16-bit selector to an actual allocation.
public class Selector
{
public ushort selectorIndex; // Base selector index allocated from RangeAllocator
public Allocation allocation; // The memory behind this selector
public bool isCode; // Can code be executed via this selector?
public bool readOnly; // Read-only or read/write?
// Generate the 16-bit selector used from 16-bit code.
public ushort selector
{
get
{
// Upper 13-bits ar the selector index, lower three bits are ring level
// and descriptor table flag. These lower three bit values seem to match
// what Windows does but aren't really important
return (ushort)((selectorIndex << 3) | (isCode ? 0x02 : 0x03));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment