diff --git a/.gitignore b/.gitignore index 845cda6..cfef67e 100644 --- a/.gitignore +++ b/.gitignore @@ -53,3 +53,5 @@ dkms.conf # debug information files *.dwo + +/sc \ No newline at end of file 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/run.sh b/run.sh new file mode 100644 index 0000000..ac5ff80 --- /dev/null +++ b/run.sh @@ -0,0 +1,3 @@ +gcc scope-count.c -o sc + +./sc \ No newline at end of file diff --git a/scope-count.c b/scope-count.c new file mode 100644 index 0000000..9fee5c4 --- /dev/null +++ b/scope-count.c @@ -0,0 +1,54 @@ +#include +#include + +struct ScopeCount +{ + int count; + int is_alias; + void *item; +}; + +struct ScopeCount scope_count(void *item) +{ + struct ScopeCount sc; + sc.count = 0; + sc.is_alias = 0; + sc.item = item; + + return sc; +} + +void end_scope(struct ScopeCount *sc) +{ + if (sc->count == 0) + { + free(sc->item); + } + sc->count--; +} + +struct ScopeCount do_something() +{ + struct ScopeCount sc = scope_count((void *)malloc(sizeof(int))); + + *(int *)sc.item = 67; + + sc.count = 1; + + end_scope(&sc); + + return sc; +} + +int main() +{ + struct ScopeCount sc = do_something(); + + printf("value: %d\n", *(int *)sc.item); + + end_scope(&sc); + + printf("value: %d\n", *(int *)sc.item); + + return 0; +}