sigar/go_bindings/gotoc/handle.go

66 lines
980 B
Go
Raw Normal View History

2016-01-04 18:23:11 +08:00
package gotoc
import (
"fmt"
"reflect"
"unsafe"
)
/*
#include "../../../../../Include/sigar.h"
*/
import "C"
var sigar *C.sigar_t=nil
func GetSigarHandle()*C.sigar_t{
if(sigar!=nil){
return sigar
}
sigar=new (C.sigar_t)
ret:=C.sigar_open(&sigar)
if(ret!=C.SIGAR_OK){
return nil
}
return sigar
}
func CloseSigarHandle(sigar *C.sigar_t) {
if(sigar!=nil){
C.sigar_close(sigar)
return
}
fmt.Println("Trying to close a nil handel, ignoring")
}
func GetGoSlice(number int, pointer unsafe.Pointer ) reflect.SliceHeader{
var length int=int(number)
cObj := reflect.SliceHeader{
Data: uintptr(pointer),
Len: length,
Cap: length,
}
return cObj
}
func CArr2SlicePtr(length int, cArray interface{}) unsafe.Pointer{
2016-01-04 18:23:11 +08:00
vl := reflect.ValueOf(cArray)
header := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(vl.Pointer())),
Len: length,
Cap: length,
}
2016-01-04 18:23:11 +08:00
return unsafe.Pointer(&header)
2016-01-04 18:23:11 +08:00
}