Skip to content

Instantly share code, notes, and snippets.

@exodist
Created August 6, 2011 07:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save exodist/1129133 to your computer and use it in GitHub Desktop.
Save exodist/1129133 to your computer and use it in GitHub Desktop.
Memory manager with parallel GC
Each thread has it's own memory unit containing a stack and a heap.
Stack:
* contains a region of memory that issues out pointers (called stack-handles)
* can be pushed which marks the last used pointer in the region
* can be popped resetting the pointer to the last pushed position
* is a stack, so the pushed pointers are in a linked list, LIFO
Heap:
* Has region of memory that issues a fixed-width structure with a data pointer and flags
* Has region of memory that holds actual data
* Each thread's heap has a lock that must be acquired before any heap-handle's flags can be modified.
Program run:
* Function push stack before running, pop stack when returning
* When a function needs data it requests a specific size, gets back a stack handle pointing to a heap handle which points to data section of desired size.
* newly allocated items are marked black
* If the heap is full it will be compacted (dead items removed, live items moved towards front of heap)
* Compaction happens in thread that owns the heap
* Compaction knows what is dead and what is live based on flags set by a parallel garbage collector in another thread.
* during compaction the heap-handles will change where they point to in the heap data
* stack-handles never need to change the address to which they point because:
* heap-handles never relocate, free ones are added as nodes in a list to be grabbed when new handles are needed
* When an objects data is accessed it will be moved from white to grey in case the access would cause the GC to miss it in the marking phase
* When an object is actually being used for something it must have it's in-use flag set to true, and removed when it is done.
GC Run:
For each threads memory set:
* mark all non-free heap handles as white
* Lock the stack so that no new stack-handles are allocated
* For each stack-handle mark the heap-handle grey
* unlock stack
* iterate heap-handles looking for greys until no greys are found
* Mark as black
* Mark as in-use (if already in use, free the flag lock and wait (usleep?))
* Mark children grey
* Mark as not in-use
* All heap-handles are white or black, mark whites as free
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment