9

Using Go's runtime/cgo to pass values between C and Go

 1 year ago
source link: https://www.arsenm.dev/blog/runtime-cgo/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

The problem #

Often, I come across a complex problem that I can’t solve myself. This is the purpose of libraries, so just import one and you’re done, right? Well, that works, until you come across a problem for which a library hasn’t been written in your language (Go, in this case). What if you could use a library from another language, such as C, in your Go program.

This is what CGo is for. It is a Foreign Function Interface, meaning it allows you to call functions from another language. CGo calls functions from C. The issue is that, since C is a completely separate language from Go, it has its own rules for how it expects things to be done.

This limitation also means that C doesn’t know how to handle a value from Go. Such values must be converted to values that can be understood by C. For example, if you have a string in Go, you can’t simply pass that to C. It must be converted to a *C.char before doing so, since C doesn’t know about Go’s string type.

Here, we come across some challenges. Let’s say you want your C library to do some processing and then call a method on a struct with the results of the operation. How do you pass a struct to C so that it can call a method? And how do you even call the method when C doesn’t have methods? Well, runtime/cgo is the perfect solution for this.

There are two problems here. First of all, as I said, C doesn’t understand Go’s values and doesn’t even have methods. Second, Go is a garbage-collected language. This means that it’ll automatically clean up unused data (garbage) by deleting (collecting) it. Unfortunately, C has no way to know when this happens and Go has no way to know if C needs the value, so anything passed to C should not be collected until it’s done using it.

How does runtime/cgo help? #

runtime/cgo is a package added in Go 1.17, and its purpose is to solve exactly the problems I discussed above. It has a NewHandle() method that takes any value and returns a Handle, which has the underlying type uintptr. This means it can be converted to a C.uintptr_t, which C can understand.

Internally, NewHandle() creates a unique integer for the value you’ve passed in, and holds a reference to it. This integer is what is returned by the function. Holding a reference to the value means the garbage collector will leave the value alone because it will believe that the value is always in use. So, problem solved, right? Well, kind of. We now have a Handle, which is an integer, but how do we call a method on an integer?

How to use it #

So, first, let’s say we have a Go program like this:

package main

import (
    "runtime/cgo"
    "fmt"
)

// #include <stdint.h>
import "C"

type Result struct {
    value1 int16
    value2 int64
    value3 uint32
}

func (r *Result) Set(val1 int16, val2 int64, val3 uint32) {
    r.value1 = val1
    r.value2 = val2
    r.value3 = val3
}

func (r *Result) String() string {
    return fmt.Sprintf("1: %d, 2: %d, 3: %d", r.value1, r.value2, r.value3)
}

Now, let’s say we want our C library to provide these results, set them, and then print the string returned by *Result.String(). How do we do this with runtime/cgo?

First of all, we need a way for our C library to create a new Result value. We’d do this in Go using a NewResult() function, and we’ll do the same here, but using handles instead of returning the value directly:

//export CNewResult
func CNewResult() C.uintptr_t {
    result := &Result{}
    handle := cgo.NewHandle(result)
    return C.uintptr_t(handle)
}

This function creates a new *Result using &Result{}. Then, it creates a new handle for this value using cgo.NewHandle(). This handle is a uintptr as I mentioned above, so it can be converted to C’s C.uintptr_t and returned.

Now we have a number corresponding to our Result value, but how do we call a method? Since C doesn’t have methods, we’ll need to create functions that call them from Go, but since C also doesn’t have direct access to the value, we’ll have to get it back out from the handle. Since Go is still holding onto the value, we just convert the C.uintptr_t back into a Handle and get its value. It’ll return an interface{}, so we’ll want to use a type assertion to get back the *Result.

//export CResultSet
func CResultSet(handle C.uintptr_t, val1 C.int16_t, val2 C.int64_t, val3 C.uint32_t) {
    // Get the *Result back
    result := cgo.Handle(handle).Value().(*Result)
    // Call the method we wanted to use from C,
    // converting the C values back to Go values.
    result.Set(int16(val1), int64(val2), uint32(val3))
}

//export CResultString
func CResultString(handle C.uintptr_t) *C.char {
    // Get the *Result back
    result := cgo.Handle(handle).Value().(*Result)
    // Call the method we wanted to use from C,
    str := result.String()
    // Since string is a Go type, we'll need to convert to C's *C.char
    // using this function that Go includes for us when we import C.
    cStr := C.CString(str)
    return cStr
}

As you can see, all you need to do is

result := cgo.Handle(handle).Value().(*Result)

and you get the value back from C to do whatever you need.

Now, there’s one more issue. As I mentioned before, Go is a garbage-collected language. What we did with the handles stopped the value from being garbage collected so that C could use it without worrying that it might be collected by the garbage collector. The issue is that since our value is no longer being deleted, if we keep making new ones, they’ll just fill up the computer’s RAM for no reason. To solve this, Handle has a method called Delete(), which removes the reference that runtime/cgo was holding onto, allowing the garbage collector to collect the value again. We need to call this from C so that it can notify us when it’s done with the value.

//export CFreeResult
func CFreeResult(handle C.uintptr_t) {
    cgo.Handle(handle).Delete()
}

That’s it. Using what we have created from C is pretty easy. Simply call the functions we created:

// Go creates this file for us. It contains all the exported functions.
#include "_cgo_export.h"

#include <stdint.h>
#include <stdio.h>

void foo() {
    uintptr_t result = CNewResult();
    CResultSet(result, -1, 123, 456);
    char* str = CResultString(result);
    printf("%s\n", str);
    CFreeResult(result);
}

Calling the foo() function should print:

1: -1, 2: 123, 3: 456

Recommend

  • 87
    • yangxikun.com 6 years ago
    • Cache

    golang cgo 开发小结

  • 37
    • jiajunhuang.com 6 years ago
    • Cache

    CGO简明教程

  • 53
    • studygolang.com 5 years ago
    • Cache

    go cgo 使用总结

    原文地址 CGO 提供了 golang 和 C 语言相互调用的机制。某些第三方库可能只有 C/C++ 的实现,完全用纯 golang 的实现可能工程浩大,这时候 CGO 就派上用场了。可以通 CGO...

  • 8

    Updates (February 28, 2020) Best Student Presentation Award: Jubi Taneja. Best Paper Award: "Testing Static Analyses for Precision and Soundness" by Jubi Taneja, Zhengyang Liu and John Regehr. Test-of-Time Awa...

  • 7

    作者:panhuili,腾讯 IEG 后台开发工程师 Go 作为当下最火的开发语言之一,它的优势不必多说。Go 对于高并发的支持,使得它可以很方便的作为独立模块嵌入业务系统。有鉴于我司大量的 C/C++存量代码,如何将 Go 和 C/C++进行打...

  • 4
    • blog.huoding.com 2 years ago
    • Cache

    实战CGO

    某项目要集成 PDF 文件的 OCR 功能,不过由于此功能技术难度太大,网络上找不到靠谱的开源实现,最终不得不选择 ABBYY FineReader Engine 的付费服务。可惜 ABBYY 只提供了 C++ 和 Java 两种编程语言的 SDK,而我...

  • 2
    • blog.yxwang.me 2 years ago
    • Cache

    CGO 09 一篇关于 DCL 检测的论文

    CGO 09 一篇关于 DCL 检测的论文 Thu, Apr 9, 2009 • Computer System D...

  • 3
    • ijayer.github.io 2 years ago
    • Cache

    Golang | CGO => 基础

    《深入 CGO 编程》 学习笔记 最简单的CGO程序 hello cgo ...

  • 5
    • datastation.multiprocess.io 2 years ago
    • Cache

    SQLite in Go, with and without cgo

    Stay in the loop about new blog posts; subscribe to the mailing list!

  • 2
    • blog.51cto.com 1 year ago
    • Cache

    cgo之#cgo

    cgo之#cgo 精选 原创 zzxiaoma 2022-09-28 08:43:36...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK