未验证 提交 867cf620 编写于 作者: Y yah01 提交者: GitHub

Add Extract() method to take out C pointer from BytesConverter without releasing it (#15614)

Signed-off-by: Nyah01 <yah2er0ne@outlook.com>
上级 6a48071a
......@@ -56,6 +56,12 @@ func (converter *BytesConverter) UnsafeGoBytes(cbytes *unsafe.Pointer, len int)
}
func (converter *BytesConverter) Release(lease int32) {
p := converter.Extract(lease)
C.free(p)
}
func (converter *BytesConverter) Extract(lease int32) unsafe.Pointer {
pI, ok := converter.pointers.LoadAndDelete(lease)
if !ok {
panic("try to release the resource that doesn't exist")
......@@ -66,7 +72,7 @@ func (converter *BytesConverter) Release(lease int32) {
panic("incorrect value type")
}
C.free(p)
return p
}
// Make sure only the caller own the converter
......@@ -90,4 +96,8 @@ func Release(lease int32) {
globalConverter.Release(lease)
}
func Extract(lease int32) unsafe.Pointer {
return globalConverter.Extract(lease)
}
// DO NOT provide ReleaseAll() method for global converter
......@@ -16,7 +16,6 @@ func TestBytesConverter(t *testing.T) {
defer Release(lease)
equalBytes(t, data, goBytes)
// data = make([]byte, maxByteArrayLen)
v := byte(0x57)
length = maxByteArrayLen
cbytes = mallocCBytes(v, maxByteArrayLen)
......@@ -27,8 +26,6 @@ func TestBytesConverter(t *testing.T) {
if !isAll(goBytes, v) {
t.Errorf("incorrect value, all bytes should be %v", v)
}
// equalBytes(t, data, goBytes)
}
func TestConcurrentBytesConverter(t *testing.T) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册