diff --git a/README.md b/README.md index 75d5554..c1b1bbf 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,33 @@ # memory-models + Memory models in C for researching static compiler analysis + +Many people overhype the rust borrow checker, i'm going to prove how terrible it is by creating a better memory model. + +## Current iteration + +It's called `sc` (scope counting), think of it as a relative counter to the lifetime of a variable. + +```rust +{ + let x = Box::new(10); // sc = 0 + + // sc is 0, drop it +} +``` + +```rust +let r; // sc = 0 + +{ + // sc of r = -1 (relative to current scope) + + let x = Box::new(10); // sc = 0 + + r = &x; // sc of x = -(sc of r) aka 1 + + // sc is 1, don't drop it +} + +// sc is 0, drop it +``` diff --git a/scope-count.c b/scope-count.c index 8d1969e..9fee5c4 100644 --- a/scope-count.c +++ b/scope-count.c @@ -8,17 +8,14 @@ struct ScopeCount void *item; }; -int scope_count(void *item, int is_alias) +struct ScopeCount scope_count(void *item) { struct ScopeCount sc; sc.count = 0; sc.is_alias = 0; sc.item = item; - sc.count++; - sc.is_alias = is_alias; - - return sc.count; + return sc; } void end_scope(struct ScopeCount *sc) @@ -32,14 +29,12 @@ void end_scope(struct ScopeCount *sc) struct ScopeCount do_something() { - struct ScopeCount sc; - sc.count = 1; - sc.is_alias = 0; - - sc.item = malloc(sizeof(int)); + struct ScopeCount sc = scope_count((void *)malloc(sizeof(int))); *(int *)sc.item = 67; + sc.count = 1; + end_scope(&sc); return sc;