From 3ba5b6536e730baf01efb3203be280c3e61319bd Mon Sep 17 00:00:00 2001 From: Determinant Date: Sun, 16 Jun 2019 23:27:05 -0400 Subject: eliminate the use of Free() --- msg.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'msg.go') diff --git a/msg.go b/msg.go index c3c0811..eb02fc3 100644 --- a/msg.go +++ b/msg.go @@ -3,20 +3,28 @@ package salticidae // #include // #include "salticidae/msg.h" import "C" +import "runtime" -type Msg = *C.struct_msg_t +type CMsg = *C.struct_msg_t +type msg struct { inner CMsg } +type Msg = *msg + +func MsgFromC(ptr *C.struct_msg_t) Msg { return &msg{ inner: ptr } } func NewMsgMovedFromByteArray(opcode Opcode, _moved_payload ByteArray) Msg { - return C.msg_new_moved_from_bytearray(C._opcode_t(opcode), _moved_payload) + res := &msg{ inner: C.msg_new_moved_from_bytearray(C._opcode_t(opcode), _moved_payload.inner) } + runtime.SetFinalizer(res, func(self Msg) { self.free() }) + return res } -func (self Msg) Free() { C.msg_free(self) } +func (self Msg) free() { C.msg_free(self.inner) } func (self Msg) ConsumePayload() DataStream { - return C.msg_consume_payload(self) + res := &dataStream{ inner: C.msg_consume_payload(self.inner) } + runtime.SetFinalizer(res, func(self DataStream) { self.free() }) + return res } func (self Msg) GetOpcode() Opcode { - return Opcode(C.msg_get_opcode(self)) + return Opcode(C.msg_get_opcode(self.inner)) } - -- cgit v1.2.3-70-g09d2