Proof of concept readme

This commit is contained in:
2026-05-13 23:53:12 +02:00
parent a75a898183
commit c3d09ec029
2 changed files with 36 additions and 10 deletions
+31
View File
@@ -1,2 +1,33 @@
# memory-models # memory-models
Memory models in C for researching static compiler analysis 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
```
+5 -10
View File
@@ -8,17 +8,14 @@ struct ScopeCount
void *item; void *item;
}; };
int scope_count(void *item, int is_alias) struct ScopeCount scope_count(void *item)
{ {
struct ScopeCount sc; struct ScopeCount sc;
sc.count = 0; sc.count = 0;
sc.is_alias = 0; sc.is_alias = 0;
sc.item = item; sc.item = item;
sc.count++; return sc;
sc.is_alias = is_alias;
return sc.count;
} }
void end_scope(struct ScopeCount *sc) void end_scope(struct ScopeCount *sc)
@@ -32,14 +29,12 @@ void end_scope(struct ScopeCount *sc)
struct ScopeCount do_something() struct ScopeCount do_something()
{ {
struct ScopeCount sc; struct ScopeCount sc = scope_count((void *)malloc(sizeof(int)));
sc.count = 1;
sc.is_alias = 0;
sc.item = malloc(sizeof(int));
*(int *)sc.item = 67; *(int *)sc.item = 67;
sc.count = 1;
end_scope(&sc); end_scope(&sc);
return sc; return sc;