@@ -5,21 +5,22 @@ import (
55 "C"
66 "fmt"
77 "math"
8- "sync"
98 "reflect"
9+ "sync"
1010)
1111
1212type Handle uint64
1313
1414const (
15- ErrorCodeSuccess = iota
15+ ErrorCodeSuccess = iota
1616 ErrorCodeNotFound = - iota
1717 ErrorCodeInternal = - iota
1818)
1919
2020const MessageNotFound string = "object not found"
2121const InvalidHandle Handle = 0
2222const IH uint64 = uint64 (InvalidHandle )
23+
2324var counter Handle = InvalidHandle
2425var opMutex sync.Mutex
2526var registryHandle2Obj map [Handle ]interface {} = map [Handle ]interface {}{}
@@ -43,7 +44,7 @@ func RegisterObject(obj interface{}) Handle {
4344 defer opMutex .Unlock ()
4445 handles , ok := registryObj2Handle [data_ptr ]
4546 if ok {
46- for _ , h := range ( handles ) {
47+ for _ , h := range handles {
4748 other , ok := registryHandle2Obj [h ]
4849 if ! ok {
4950 panic ("Inconsistent internal object mapping state (1)" )
@@ -56,7 +57,7 @@ func RegisterObject(obj interface{}) Handle {
5657 }
5758 }
5859 }
59- handle := getNewHandle ()
60+ handle := getNewHandle ()
6061 registryHandle2Obj [handle ] = obj
6162 registryObj2Handle [data_ptr ] = append (registryObj2Handle [data_ptr ], handle )
6263 if trace {
@@ -67,7 +68,7 @@ func RegisterObject(obj interface{}) Handle {
6768
6869func UnregisterObject (handle Handle ) int {
6970 if trace {
70- fmt .Printf ("UnregisterObject %d\n " , handle )
71+ fmt .Printf ("UnregisterObject %d\n " , handle )
7172 }
7273 if handle == InvalidHandle {
7374 return ErrorCodeNotFound
@@ -83,23 +84,23 @@ func UnregisterObject(handle Handle) int {
8384 other_handles , ok := registryObj2Handle [data_ptr ]
8485 if ! ok {
8586 panic (fmt .Sprintf ("Inconsistent internal object mapping state (2): %d" ,
86- handle ))
87+ handle ))
8788 }
8889 hi := - 1
89- for i , h := range ( other_handles ) {
90+ for i , h := range other_handles {
9091 if h == handle {
9192 hi = i
9293 break
9394 }
9495 }
9596 if hi < 0 {
9697 panic (fmt .Sprintf ("Inconsistent internal object mapping state (3): %d" ,
97- handle ))
98+ handle ))
9899 }
99100 if len (other_handles ) == 1 {
100101 delete (registryObj2Handle , data_ptr )
101102 } else {
102- registryObj2Handle [data_ptr ] = append (other_handles [:hi ], other_handles [hi + 1 :]... )
103+ registryObj2Handle [data_ptr ] = append (other_handles [:hi ], other_handles [hi + 1 :]... )
103104 }
104105 if trace {
105106 c_dump_objects ()
@@ -125,7 +126,7 @@ func GetHandle(obj interface{}) (Handle, bool) {
125126 if ! ok {
126127 return InvalidHandle , false
127128 }
128- for _ , h := range ( handles ) {
129+ for _ , h := range handles {
129130 candidate := registryHandle2Obj [h ]
130131 if candidate == obj {
131132 return h , true
@@ -143,13 +144,13 @@ func CopyString(str string) string {
143144// https://github.com/golang/go/issues/14838
144145func CBytes (bytes []byte ) * C.char {
145146 ptr := C .malloc (C .size_t (len (bytes )))
146- copy ((* [1 << 30 ]byte )(ptr )[:], bytes )
147+ copy ((* [1 << 30 ]byte )(ptr )[:], bytes )
147148 return (* C .char )(ptr )
148149}
149150
150151func SafeIsNil (v reflect.Value ) bool {
151- defer func () { recover () }()
152- return v .IsNil ()
152+ defer func () { recover () }()
153+ return v .IsNil ()
153154}
154155
155156//export c_dispose
@@ -165,17 +166,17 @@ func c_objects_size() int {
165166//export c_dump_objects
166167func c_dump_objects () {
167168 fmt .Printf ("handles (%d):\n " , len (registryHandle2Obj ))
168- for h , obj := range ( registryHandle2Obj ) {
169+ for h , obj := range registryHandle2Obj {
169170 fmt .Printf ("0x%x\t 0x%x %v\n " , h ,
170171 reflect .ValueOf (& obj ).Elem ().InterfaceData ()[1 ], obj )
171172 }
172173 fmt .Println ()
173174 phs := 0
174- for _ , h := range ( registryObj2Handle ) {
175+ for _ , h := range registryObj2Handle {
175176 phs += len (h )
176177 }
177178 fmt .Printf ("pointers (%d):\n " , phs )
178- for ptr , h := range ( registryObj2Handle ) {
179+ for ptr , h := range registryObj2Handle {
179180 fmt .Printf ("0x%x\t %v\n " , ptr , h )
180181 }
181182}
0 commit comments