From a75a89818344b19283b3017f5164a70bc0cbcab0 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Wed, 13 May 2026 22:53:52 +0200 Subject: [PATCH 1/2] Proof of concept --- .gitignore | 2 ++ run.sh | 3 +++ scope-count.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 run.sh create mode 100644 scope-count.c 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/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..8d1969e --- /dev/null +++ b/scope-count.c @@ -0,0 +1,59 @@ +#include +#include + +struct ScopeCount +{ + int count; + int is_alias; + void *item; +}; + +int scope_count(void *item, int is_alias) +{ + struct ScopeCount sc; + sc.count = 0; + sc.is_alias = 0; + sc.item = item; + + sc.count++; + sc.is_alias = is_alias; + + return sc.count; +} + +void end_scope(struct ScopeCount *sc) +{ + if (sc->count == 0) + { + free(sc->item); + } + sc->count--; +} + +struct ScopeCount do_something() +{ + struct ScopeCount sc; + sc.count = 1; + sc.is_alias = 0; + + sc.item = malloc(sizeof(int)); + + *(int *)sc.item = 67; + + 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; +} From c3d09ec0294b13988b968a61cd51c9f851631dde Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Wed, 13 May 2026 23:53:12 +0200 Subject: [PATCH 2/2] Proof of concept readme --- README.md | 31 +++++++++++++++++++++++++++++++ scope-count.c | 15 +++++---------- 2 files changed, 36 insertions(+), 10 deletions(-) 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;