Files
hawk/hawk-inst.go

80 lines
1.9 KiB
Go
Raw Normal View History

2025-10-26 23:18:23 +09:00
package hawk
/*
#include <hawk.h>
*/
import "C"
import "fmt"
import "sync"
import "weak"
2025-10-26 23:18:23 +09:00
type CPtr interface {
//~*C.hawk_t | ~*C.hawk_rtx_t
*C.hawk_t | *C.hawk_rtx_t
2025-10-26 23:18:23 +09:00
}
type Instance[CT CPtr, GT interface{}] struct {
c CT // c object
g weak.Pointer[GT] // go object
next_free int
}
type InstanceTable[CT CPtr, GT interface{}] struct {
name string
2025-10-26 23:18:23 +09:00
mtx sync.Mutex
insts []Instance[CT, GT]
next_free int
2025-10-26 23:18:23 +09:00
}
type HawkInstance = Instance[*C.hawk_t, Hawk]
type HawkInstanceTable = InstanceTable[*C.hawk_t, Hawk]
type RtxInstance = Instance[*C.hawk_rtx_t, Rtx]
type RtxInstanceTable = InstanceTable[*C.hawk_rtx_t, Rtx]
var inst_table HawkInstanceTable = HawkInstanceTable{ name: "hawk", next_free: -1 }
var rtx_inst_table RtxInstanceTable = RtxInstanceTable{ name: "rtx", next_free: -1 }
func (itab *InstanceTable[CT, GT]) add_instance(c CT, g *GT) int {
2025-10-26 23:18:23 +09:00
itab.mtx.Lock()
defer itab.mtx.Unlock()
if itab.next_free >= 0 {
2025-10-26 23:18:23 +09:00
var slot int
slot = itab.next_free
if slot >= 0 { itab.next_free = itab.insts[slot].next_free }
2025-10-26 23:18:23 +09:00
itab.insts[slot].c = c
itab.insts[slot].g = weak.Make(g)
itab.insts[slot].next_free = -1
2025-10-26 23:18:23 +09:00
return slot
} else { // no free slots
itab.insts = append(itab.insts, Instance[CT, GT]{c: c, g: weak.Make(g), next_free: -1})
return len(itab.insts) - 1
2025-10-26 23:18:23 +09:00
}
}
func (itab *InstanceTable[CT, GT]) delete_instance(slot int) error {
2025-10-26 23:18:23 +09:00
itab.mtx.Lock()
defer itab.mtx.Unlock()
if slot >= len(itab.insts) || slot < 0 {
return fmt.Errorf("index %d out of range", slot)
2025-10-26 23:18:23 +09:00
}
if itab.insts[slot].next_free >= 0 {
return fmt.Errorf("no instance at index %d", slot)
}
itab.insts[slot].c = CT(nil)
itab.insts[slot].g = weak.Make((*GT)(nil)) // this may not even be necessary as it's a weak pointer
itab.insts[slot].next_free = itab.next_free
itab.next_free = slot
2025-10-26 23:18:23 +09:00
return nil
2025-10-26 23:18:23 +09:00
}
func (itab *InstanceTable[CT, GT]) slot_to_instance(slot int) Instance[CT, GT] {
2025-10-26 23:18:23 +09:00
itab.mtx.Lock()
defer itab.mtx.Unlock()
return itab.insts[slot]
}