Skip to content

Commit 84de722

Browse files
cosmo0920edsiper
authored andcommitted
Follow the unsafe.Pointer patterns
It seems that golang/go#41705 (comment) is useful comment for using unsafe.Pointer correctly. Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent 5414969 commit 84de722

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

examples/in_gdummy/in_gdummy.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import "C"
44
import (
55
"fmt"
66
"time"
7+
"reflect"
78
"unsafe"
89

910
"github.com/fluent/fluent-bit-go/input"
@@ -24,8 +25,19 @@ func FLBPluginInit(plugin unsafe.Pointer) int {
2425
return input.FLB_OK
2526
}
2627

28+
func MakePayload(packed []byte) (**C.void, int) {
29+
var payload **C.void
30+
31+
length := len(packed)
32+
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&payload))
33+
hdr.Data = uintptr(unsafe.Pointer(&packed))
34+
hdr.Len = length
35+
36+
return payload, length
37+
}
38+
2739
//export FLBPluginInputCallback
28-
func FLBPluginInputCallback(data *unsafe.Pointer, size *C.size_t) int {
40+
func FLBPluginInputCallback(data **C.void, size *C.size_t) int {
2941
now := time.Now()
3042
flb_time := input.FLBTime{now}
3143
message := map[string]string{"message": "dummy"}
@@ -39,8 +51,10 @@ func FLBPluginInputCallback(data *unsafe.Pointer, size *C.size_t) int {
3951
return input.FLB_ERROR
4052
}
4153

42-
*data = unsafe.Pointer(&packed[0])
43-
*size = C.size_t(len(packed))
54+
payload, length := MakePayload(packed)
55+
56+
*data = *payload
57+
*size = C.size_t(length)
4458
// For emitting interval adjustment.
4559
time.Sleep(1000 * time.Millisecond)
4660

0 commit comments

Comments
 (0)