Merge pull request #1 from selimaj-dev/proof-of-concept

Proof of concept
This commit is contained in:
2026-05-13 23:54:14 +02:00
committed by GitHub
4 changed files with 90 additions and 0 deletions
+2
View File
@@ -53,3 +53,5 @@ dkms.conf
# debug information files # debug information files
*.dwo *.dwo
/sc
+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
```
+3
View File
@@ -0,0 +1,3 @@
gcc scope-count.c -o sc
./sc
+54
View File
@@ -0,0 +1,54 @@
#include <stdio.h>
#include <stdlib.h>
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;
}