2025-10-26 23:18:23 +09:00
|
|
|
package hawk
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
#include <hawk.h>
|
|
|
|
|
*/
|
|
|
|
|
import "C"
|
2025-11-24 00:17:46 +09:00
|
|
|
import "fmt"
|
2025-10-30 23:47:56 +09:00
|
|
|
import "sync"
|
|
|
|
|
import "weak"
|
2025-10-26 23:18:23 +09:00
|
|
|
|
2025-11-24 00:17:46 +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
|
|
|
}
|
|
|
|
|
|
2025-11-24 00:17:46 +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
|
2025-11-24 00:17:46 +09:00
|
|
|
insts []Instance[CT, GT]
|
|
|
|
|
next_free int
|
2025-10-26 23:18:23 +09:00
|
|
|
}
|
|
|
|
|
|
2025-11-24 00:17:46 +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]
|
2025-10-30 23:47:56 +09:00
|
|
|
|
2025-11-24 00:17:46 +09:00
|
|
|
var inst_table HawkInstanceTable = HawkInstanceTable{ name: "hawk", next_free: -1 }
|
|
|
|
|
var rtx_inst_table RtxInstanceTable = RtxInstanceTable{ name: "rtx", next_free: -1 }
|
2025-10-30 23:47:56 +09:00
|
|
|
|
2025-11-24 00:17:46 +09:00
|
|
|
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()
|
|
|
|
|
|
2025-11-24 00:17:46 +09:00
|
|
|
if itab.next_free >= 0 {
|
2025-10-26 23:18:23 +09:00
|
|
|
var slot int
|
2025-11-24 00:17:46 +09:00
|
|
|
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
|
2025-11-24 00:17:46 +09:00
|
|
|
itab.insts[slot].g = weak.Make(g)
|
|
|
|
|
itab.insts[slot].next_free = -1
|
2025-10-26 23:18:23 +09:00
|
|
|
return slot
|
2025-11-24 00:17:46 +09:00
|
|
|
} 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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-24 00:17:46 +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()
|
|
|
|
|
|
2025-11-24 00:17:46 +09:00
|
|
|
if slot >= len(itab.insts) || slot < 0 {
|
|
|
|
|
return fmt.Errorf("index %d out of range", slot)
|
2025-10-26 23:18:23 +09:00
|
|
|
}
|
2025-11-24 00:17:46 +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
|
|
|
|
2025-11-24 00:17:46 +09:00
|
|
|
return nil
|
2025-10-26 23:18:23 +09:00
|
|
|
}
|
|
|
|
|
|
2025-11-24 00:17:46 +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]
|
|
|
|
|
}
|