title: Region based memory management
body: <p>I'm designing a high level language, and I want it to have the speed of C++ (it will use LLVM), but be safe and high level like C#. Garbage collection is slow, and new/delete is unsafe. I decided to try to use "region based memory management" (there are a few papers about it on the web, mostly for functional languages). The only "useful" language using it is Cyclone, but that also has GC. Basically, objects are allocated on a lexical stack, and are freed when the block closes. Objects can only refer to other objects in the same region or higher, to prevent dangling references. To make this more flexible, I added parallel regions that can be moved up and down the stack, and retained through loops. The type system would be able to verify assignments in most cases, but low overhead runtime checks would be necessary in some places.</p>
  
  <p>Ex:</p>
  
  <pre><code>region(A) {
      Foo@A x=new Foo(); //x is deleted when this region closes.
      region(B,C) while(x.Y) {
          Bar@B n=new Bar();
          n.D=x; //OK, n is in lower region than x.
          //x.D=n; would cause error: x is in higher region than n.
          n.DoSomething();
          Bar@C m=new Bar();
          //m.D=n; would cause error: m and n are parallel.
          if(m.Y)
              retain(C); //On the next iteration, m is retained.
      }
  }
  </code></pre>
  
  <p>Does this seem practical? Would I need to add non-lexically scoped, reference counted regions? Would I need to add weak variables that can refer to any object, but with a check on region deletion? Can you think of any algorithms that would be hard to use with this system or that would leak?</p>