This repository has been archived by the owner on May 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement SetFinalizer for latest tinygo dev (#5)
- Loading branch information
Showing
4 changed files
with
117 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
//go:build nottinygc_finalizer | ||
|
||
package nottinygc | ||
|
||
/* | ||
void GC_register_finalizer(void* obj, void* fn, void* cd, void** ofn, void** ocn); | ||
void onFinalizer(void* obj, void* fn); | ||
void* malloc(unsigned int long); | ||
void free(void* ptr); | ||
*/ | ||
import "C" | ||
import "unsafe" | ||
|
||
var finalizers = map[uintptr]interface{}{} | ||
|
||
//go:linkname SetFinalizer runtime.SetFinalizer | ||
func SetFinalizer(obj interface{}, finalizer interface{}) { | ||
finKey := uintptr((*_interface)(unsafe.Pointer(&finalizer)).value) | ||
finalizers[finKey] = finalizer | ||
|
||
in := (*_interface)(unsafe.Pointer(&obj)) | ||
|
||
rf := (*registeredFinalizer)(C.malloc(C.ulong(unsafe.Sizeof(registeredFinalizer{})))) | ||
rf.typecode = in.typecode | ||
rf.finKey = finKey | ||
|
||
C.GC_register_finalizer(in.value, C.onFinalizer, unsafe.Pointer(rf), nil, nil) | ||
} | ||
|
||
//export onFinalizer | ||
func onFinalizer(obj unsafe.Pointer, data unsafe.Pointer) { | ||
defer C.free(data) | ||
|
||
rf := (*registeredFinalizer)(data) | ||
finalizer := finalizers[rf.finKey] | ||
delete(finalizers, rf.finKey) | ||
|
||
var in interface{} | ||
inFields := (*_interface)(unsafe.Pointer(&in)) | ||
inFields.typecode = rf.typecode | ||
inFields.value = obj | ||
|
||
switch f := finalizer.(type) { | ||
case func(interface{}): | ||
f(in) | ||
default: | ||
panic("currently only finalizers of the form func(interface{}) are supported") | ||
} | ||
} | ||
|
||
type _interface struct { | ||
typecode uintptr | ||
value unsafe.Pointer | ||
} | ||
|
||
type registeredFinalizer struct { | ||
typecode uintptr | ||
finKey uintptr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
//go:build nottinygc_finalizer | ||
|
||
package nottinygc | ||
|
||
import ( | ||
"runtime" | ||
"testing" | ||
) | ||
|
||
func TestFinalizer(t *testing.T) { | ||
finalized := 0 | ||
allocFinalized(10, func() { | ||
finalized++ | ||
}) | ||
|
||
runtime.GC() | ||
|
||
if finalized == 0 { | ||
t.Errorf("finalizer not called") | ||
} | ||
} | ||
|
||
//go:noinline | ||
func allocFinalized(num int, cb func()) { | ||
f := &finalized{ | ||
a: 100, | ||
b: "foo", | ||
} | ||
|
||
runtime.SetFinalizer(f, func(f interface{}) { | ||
cb() | ||
}) | ||
|
||
// Recurse to create some more stack frames or else the shadow stack | ||
// may still not have been reset, causing GC to find the inaccessible | ||
// pointers. | ||
if num > 1 { | ||
allocFinalized(num-1, cb) | ||
} | ||
} | ||
|
||
type finalized struct { | ||
a int | ||
b string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters