160 likes | 258 Views
Programming with Regions. Chris Durham CSCI 297 June 16 th , 2005. Paper. Language Support for Regions , David Gay and Alex Aiken; UC Berkeley, 2001. Memory Management Issues. Allocation / Deallocation mismatch malloc() / free() Dereference ‘dangling’ pointers
E N D
Programming with Regions Chris Durham CSCI 297 June 16th, 2005
Paper Language Support for Regions, David Gay and Alex Aiken; UC Berkeley, 2001
Memory Management Issues • Allocation / Deallocation mismatch • malloc() / free() • Dereference ‘dangling’ pointers • Heap fragmentation, boundary issues • Sizing, when to allocate • Remembering to free() !
Garbage Collection Questions • When to scan? • How often? • Rearranging? Paper states that they did some work with gcc and the Boehm-Weiser garbage collector
Regions Concept to address some of these issues • All allocations of a particular ‘type’ allocated to same ‘region’ • Reference count kept to the ‘region’ • Can only deallocate region after no references exist; deallocates all ‘objects’ within the region • Best used for complex data structures
Example (1) reference count = 1
Example (2) reference count = 2
Example (3) reference count = 2
Region Benefits • Region can only be de-allocated when reference count == 0; attempts abort program when checking enabled • Rely on region library to optimize heap usage and make garbage collection easier - presumably, implementation of regions is well structured and uses knowledge of allocator • Programmer doesn’t necessarily have to focus on memory management to same degree as with traditional malloc()/free() • In some ways, this concept is similar to resource pooling as in Apache, but is unstructured
RC Compiler A library and compiler (rcc), compiles annotated code to C code. Introduces several annotations http://www.cs.berkeley.edu/~dgay/rc Also: Cyclone has its own region sub-system
Code snippet struct rlist {struct rlist *sameregion next; struct finfo *sameregion data; } *rl, *last = NULL; region r = newregion(); while (…) { rl = ralloc(r,struct rlist); rl->data = ralloc(r,struct rlist); /* fill in data */ rl->next = last; last = rl; } output_rlist(last); deleteregion(r);
Pointer annotations et al • sameregion: in same region • traditional: not in a region at all • parentptr: you can have sub-regions, this means this pointer exists in the region one level higher • struct Foo *sameregion foo; • struct Bar *traditional bar; • struct Parent *parentptr parent; • struct region has a member ‘rc’ for reference count • regionof() to determine what region an identifier is in Also: a constraint inference system that proves some type safety situations was built into rcc, enables them to remove some runtime checks that verify the type constraints to speed things up
Conclusions Regions can help make memory management easier by eliminating the need for the programmer to concentrate on some memory management issues and it even speeds up things a bit over traditional methods As we discussed previously, benchmarks and real life are two different things - no real test of the robustness of the region library itself
Original Problems? / New Problems Of the original problems, which ones do regions address? What new challenges are there for the programmer? What are the security implications here?
References • Language Support for Regions; David Gay and Alex Aiken; UC Berkeley, 2001 • Region-Based Memory Mapping in Cyclone, Dan Grossman, Greg Morrisett, Trevor Jim, Michael Hicks, Yanling Wang, James Cheney; Cornell University 2002