Skip to content

Instantly share code, notes, and snippets.

@pervognsen pervognsen/dispose.ion
Last active Feb 23, 2019

Embed
What would you like to do?
typedef DisposeFunc = func(void*);
typedef DisposeMark = usize;
struct Disposable {
dispose: DisposeFunc;
mark: DisposeMark;
}
@threadlocal
var disposables: Disposable*[];
func make_disposable(dispose: DisposeFunc): Disposable {
return {dispose, alen(disposables)};
}
func secured(data: void*): bool {
if (!data) {
return false;
}
self: Disposable* = data;
mark := self.mark;
return mark < alen(disposables) && disposables[mark] == self;
}
func secure(data: void*): void* {
if (data && !secured(data)) {
self: Disposable* = data;
self.mark = apush(disposables, self);
}
return data;
}
func unsecure(data: void*): void* {
if (data && secured(data)) {
self: Disposable* = data;
disposables[self.mark] = 0;
self.mark = -1;
}
return data;
}
func dispose(data: void*) {
if (!data) {
return;
}
self: Disposable* = data;
mark := self.mark;
for (i := alen(disposables); i > mark; i--) {
disposable := disposables[i-1];
if (disposable) {
disposable.dispose(disposable);
}
}
asetlen(disposables, mark);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.