std: mem.Arena 를 구현한다

SPEC R11 은 재귀·그래프 모양 데이터를 아레나가 값을 소유하고 정수 핸들이
가리키는 것으로 답한다. 그 답은 아레나가 실제로 있어야 쓸 수 있다.

핸들은 오프셋이라 아레나가 사는 동안 유효하고, 두 핸들을 비교하는 것은 두 수를
비교하는 것이다. 아레나는 몰래 자라지 않는다 -- 움직인 핸들은 더 이상 아무것도
가리키지 않기 때문이다.

길에서 고친 것 셋:

- Self 가 제네릭 인스턴스에서만 타입으로 묶여 있어서, 평범한 구조체의 Self{..}
  가 안 풀렸다. 이제 모든 메서드에서 묶는다.
- binding.Type.method() 가 식 자리에서 해석되지 않았다.
- 다른 유닛의 비제네릭 구조체 메서드가 lowering 되지 않고 extern 으로만 나갔다.
  파일을 나눌 때 그 가지가 빠졌다.

  handles 0 4 8 / value 65 / full / reset 0 / balanced

exec.py 26/26.
This commit is contained in:
2026-08-17 07:21:40 +09:00
parent c06f5c50c4
commit ae83f5b142
6 changed files with 131 additions and 10 deletions
+39
View File
@@ -0,0 +1,39 @@
// EXIT:0
// OUTPUT:handles 0 4 8
// OUTPUT:value 65
// OUTPUT:full
// OUTPUT:reset 0
// OUTPUT:balanced
unit arena;
import std.io;
import std.mem;
import std.sys;
// SPEC R11: recursive and graph-shaped data is answered by an arena that owns
// the values and integer handles that point into it. This is that shape.
fn run() -> !void {
var a: mem.Arena = try mem.Arena.with_capacity(16);
let first: usize = try a.alloc(4, 4);
let second: usize = try a.alloc(4, 4);
let third: usize = try a.alloc(4, 4);
@print("handles {} {} {}\n", first, second, third);
try a.put(first, 65);
let got: u8 = try a.at(first);
@print("value {}\n", got);
let over: usize = a.alloc(64, 1) catch |e| {
@print("full\n");
a.reset();
@print("reset {}\n", a.size());
return;
};
@print("unexpected room {}\n", over);
return;
}
fn main() -> i32 {
run() catch |e| { @print("failed\n"); return 1; };
if sys.allocs() != sys.frees() { @print("leaked\n"); return 2; }
@print("balanced\n");
return 0;
}