INNER CODE UNIT · Go

ProtoReturnV3

krypton-byte/neonize · goneonize/main.go:387

func ProtoReturnV3(data proto.Message) *C.struct_BytesReturn {
	// Marshal into a pooled buffer to avoid a fresh heap allocation on every
	// call; the bytes are copied onto the C heap below, so the buffer can be
	// returned to the pool immediately afterwards.
	bufPtr := bufPool.Get().(*[]byte)
	data_buf, err := proto.MarshalOptions{}.MarshalAppend((*bufPtr)[:0], data)
	if err != nil {
		bufPool.Put(bufPtr)
		// Return an empty BytesReturn instead of panicking. The Python side
		// will receive an empty protobuf message, which unmarshals as a
		// zero-value struct — the same as a successful call with no data.
		return (*C.struct_BytesReturn)(C.calloc(1, C.size_t(unsafe.Sizeof(C.struct_BytesReturn{}))))
	}
	result := (*C.struct_BytesReturn)(C.malloc(C.size_t(unsafe.Sizeof(C.struct_BytesReturn{}))))
	result.size = C.size_t(len(data_buf))
	if len(data_buf) > 0 {
		result.data = (*C.char)(C.malloc(result.size))
		C.memcpy(unsafe.Pointer(result.data), unsafe.Pointer(&data_buf[0]), result.size)

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…