美文网首页
cgo example

cgo example

作者: perryn | 来源:发表于2018-05-17 19:00 被阅读49次
  • go access c struct
  // go run cgo.go
  package main

/*
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
typedef struct student {
        int age;
        char *name;
}student;

void student_init(void **ptr,char *name,int age) {
        size_t len = strlen(name);
        student *st = (student *)calloc(1,sizeof(student));
        assert(st!=NULL);
        st->age = age;
        st->name = (char *)calloc(1,len+1);
        memcpy(st->name,name,len);
        *ptr = st;
        fprintf(stdout,"...call student_init...\n");
}
student *student_new(char *name,int age) {
        size_t len = strlen(name);
        student *st = (student *)calloc(1,sizeof(student));
        assert(st!=NULL);
        st->age = age;
        st->name = (char *)calloc(1,len+1);
        memcpy(st->name,name,len);

        fprintf(stdout,"...call student_new...\n");
        return st;
}
void student_destroy(void *ptr) {
        student *st = (student *)ptr;
        if(st !=NULL)
        {
                free(st->name);
                free(st);
                st=NULL;
                fprintf(stdout,"...call student_destroy...\n");
        }
}
void student_print(void  *ptr) {
        student *st = (student *)ptr;
        fprintf(stdout,"student addr=%p,name=%p,age=%p\n",st,st->name,&st->age);
        fprintf(stdout," student {name=%s,age=%d}\n",st->name,st->age);
}
*/
import "C"
import (
        "fmt"
        "unsafe"
)

func main() {
        var st1 unsafe.Pointer
        name := C.CString("perrynzhou")
        C.student_init(&st1, name, 30)
        C.student_print(st1)
        C.student_destroy(st1)
        C.free(unsafe.Pointer(name))

        var st2 *C.student
        name2 := C.CString("hello")
        st2 = C.student_new(name2, 100)
        fmt.Printf("init student st2 {age:%d,name:%s}\n", st2.age, C.GoString(st2.name))
        C.student_print(unsafe.Pointer(st2))

        C.free(unsafe.Pointer(st2.name))
        name3 := C.CString("join")
        st2.name = name3
        st2.age = 67
        fmt.Printf("after change student st2 {age:%d,name:%s}\n", st2.age, C.GoString(st2.name))
        C.student_print(unsafe.Pointer(st2))
        C.student_destroy(unsafe.Pointer(st2))

}
image.png
  • go access c memory
//    go build  -o cgo_test cgo.go 

package main

/*

#include <stdlib.h>

void *alloc() {
        static int count = 0;
        void *d = malloc(sizeof(int));
        *((int *)d) = count++;
        return d;
}
*/
import "C"
import (
        "fmt"
        "runtime"
        "sync"
        "time"
        "unsafe"
)

type CStruct struct {
        sync.Mutex
        name     string
        allocCnt int
        memory   unsafe.Pointer
}

func (cs *CStruct) alloc(name string, id int) {
        cs.name = fmt.Sprintf("CStruct-%s-%d", name, id)
        cs.Lock()
        defer cs.Unlock()
        cs.allocCnt++
        fmt.Printf("%s begin with alloc,count=%d\n", cs.name, cs.allocCnt)
        cs.memory = C.alloc()
        runtime.SetFinalizer(cs, free)
}
func free(cs *CStruct) {
        C.free(unsafe.Pointer(cs.memory))
        cs.Lock()
        defer cs.Unlock()
        cs.allocCnt--
        fmt.Printf("%s end with free count=%d\n", cs.name, cs.allocCnt)
}
func CStructTest(i int) {
        var c1, c2 CStruct
        c1.alloc("c1", i)
        c2.alloc("c2", i)
}
func main() {
        for i := 0; i < 10; i++ {
                CStructTest(i)
                time.Sleep(time.Second)
        }
        runtime.GC()
        time.Sleep(time.Second)
        fmt.Println("done..")
}
image.png

相关文章

  • cgo example

    go access c struct go access c memory

  • CGO使用

    参考文章: http://golang.org/cmd/cgo is the primary cgo docume...

  • CGO 初步认知和基本数据类型转换

    CGO 是什么? CGO 是 GO 语言里面的一个特性,CGO 属于 GOLANG 的高级用法,主要是通过使用 G...

  • golang win10交叉编译

    写了一个bat文件 set CGO_ENABLED=0 关闭CGO

  • CGO编程

    1)什么是CGO编程?2)CGO语法3)相关资料 一、什么是CGO编程?简单说就是GO语言代码中可以编写C代码和调...

  • cgo

    golang与C有着千丝万缕的联系,go代码中可以调用C代码由于项目需要,新使用的golang语言需要调用C语言写...

  • cgo

    三种方式 在头部嵌入c代码 头部引用.h文件,实际调用动态库,需要指定路径(当前项目使用这种形式) 直接引用文件,...

  • cgo

    cgo cgo是用来在Go语言中调用C语言的工具 Go语言调用C语言 简单C语言函数 在Go语言中需要通过impo...

  • cgo

    转载一篇CGO的文章 Go 与 C 的桥梁:cgo 入门,剖析与实践[https://zhuanlan.zhihu...

  • cgo

    https://github.com/chai2010/gopherchina2018-cgo-talk[http...

网友评论

      本文标题:cgo example

      本文链接:https://www.haomeiwen.com/subject/adjidftx.html