mirror of
https://github.com/yuzu-emu/unicorn.git
synced 2025-06-19 13:17:56 +00:00
Go bindings: add Close() and set as GC finalizer
This commit is contained in:
parent
b41db5abd9
commit
a6ffb71e4c
|
@ -1,6 +1,8 @@
|
||||||
package unicorn
|
package unicorn
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"runtime"
|
||||||
|
"sync"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -37,10 +39,12 @@ type Unicorn interface {
|
||||||
Stop() error
|
Stop() error
|
||||||
HookAdd(htype int, cb interface{}, extra ...uint64) (Hook, error)
|
HookAdd(htype int, cb interface{}, extra ...uint64) (Hook, error)
|
||||||
HookDel(hook Hook) error
|
HookDel(hook Hook) error
|
||||||
|
Close() error
|
||||||
}
|
}
|
||||||
|
|
||||||
type uc struct {
|
type uc struct {
|
||||||
handle *C.uc_engine
|
handle *C.uc_engine
|
||||||
|
final sync.Once
|
||||||
}
|
}
|
||||||
|
|
||||||
type UcOptions struct {
|
type UcOptions struct {
|
||||||
|
@ -57,8 +61,19 @@ func NewUnicorn(arch, mode int) (Unicorn, error) {
|
||||||
if ucerr := C.uc_open(C.uc_arch(arch), C.uc_mode(mode), &handle); ucerr != ERR_OK {
|
if ucerr := C.uc_open(C.uc_arch(arch), C.uc_mode(mode), &handle); ucerr != ERR_OK {
|
||||||
return nil, UcError(ucerr)
|
return nil, UcError(ucerr)
|
||||||
}
|
}
|
||||||
uc := &uc{handle}
|
u := &uc{handle: handle}
|
||||||
return uc, nil
|
runtime.SetFinalizer(u, func(u *uc) { u.Close() })
|
||||||
|
return u, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *uc) Close() (err error) {
|
||||||
|
u.final.Do(func() {
|
||||||
|
if u.handle != nil {
|
||||||
|
err = errReturn(C.uc_close(u.handle))
|
||||||
|
u.handle = nil
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *uc) StartWithOptions(begin, until uint64, options *UcOptions) error {
|
func (u *uc) StartWithOptions(begin, until uint64, options *UcOptions) error {
|
||||||
|
|
|
@ -24,3 +24,16 @@ func TestMemUnmap(t *testing.T) {
|
||||||
t.Fatal(fmt.Errorf("Expected ERR_WRITE_UNMAPPED, got: %v", err))
|
t.Fatal(fmt.Errorf("Expected ERR_WRITE_UNMAPPED, got: %v", err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDoubleClose(t *testing.T) {
|
||||||
|
mu, err := NewUnicorn(ARCH_X86, MODE_32)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := mu.Close(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := mu.Close(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue