mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-07-26 06:38:42 +00:00
7c3dc05117
* modernize string cut usage * modernize string cut prefix usage * modernize slices helper usage * modernize min and max usage * modernize loop variable copies * modernize integer range loops * modernize map copy loops * modernize go types iterator usage * modernize empty interface usage * modernize atomic type usage * modernize string builders * modernize string split iteration * modernize remaining loop variable copies * modernize usb cdc min usage * modernize src integer range loops * modernize example empty interface usage * modernize src min and max usage * modernize src integer range loops * modernize src empty interface usage * modernize src atomic type usage * modernize reflect type lookups * modernize review nits
47 lines
1.0 KiB
Go
47 lines
1.0 KiB
Go
package cgo
|
|
|
|
import (
|
|
"sync"
|
|
"unsafe"
|
|
)
|
|
|
|
// #include <stdlib.h>
|
|
import "C"
|
|
|
|
// refMap is a convenient way to store opaque references that can be passed to
|
|
// C. It is useful if an API uses function pointers and you cannot pass a Go
|
|
// pointer but only a C pointer.
|
|
type refMap struct {
|
|
refs map[unsafe.Pointer]any
|
|
lock sync.Mutex
|
|
}
|
|
|
|
// Put stores a value in the map. It can later be retrieved using Get. It must
|
|
// be removed using Remove to avoid memory leaks.
|
|
func (m *refMap) Put(v any) unsafe.Pointer {
|
|
m.lock.Lock()
|
|
defer m.lock.Unlock()
|
|
if m.refs == nil {
|
|
m.refs = make(map[unsafe.Pointer]any, 1)
|
|
}
|
|
ref := C.malloc(1)
|
|
m.refs[ref] = v
|
|
return ref
|
|
}
|
|
|
|
// Get returns a stored value previously inserted with Put. Use the same
|
|
// reference as you got from Put.
|
|
func (m *refMap) Get(ref unsafe.Pointer) any {
|
|
m.lock.Lock()
|
|
defer m.lock.Unlock()
|
|
return m.refs[ref]
|
|
}
|
|
|
|
// Remove deletes a single reference from the map.
|
|
func (m *refMap) Remove(ref unsafe.Pointer) {
|
|
m.lock.Lock()
|
|
defer m.lock.Unlock()
|
|
delete(m.refs, ref)
|
|
C.free(ref)
|
|
}
|