github.com/notti/go-dynamic@v0.0.0-20190619201224-fc443047424c/internal/fakecgo/libcdefs.go (about)

     1  package fakecgo
     2  
     3  import (
     4  	"reflect"
     5  	"unsafe"
     6  )
     7  
     8  // Wrapper functions to provide libc-functions for cgo.go
     9  
    10  // C-types (linux glibc, libc freebsd) we don't need to see what's inside pthread_attr and sigset_t -> byte arrays:
    11  type pthread_attr [56]byte // this is 36 on 386 glibc, 16 on amd64 freebsd - but too big doesn't hurt
    12  type sigset_t [128]byte    // this is 16 on amd64 freebsd - but too big doesn't hurt
    13  type size_t int
    14  type pthread_t int
    15  type timespec struct {
    16  	tv_sec  int
    17  	tv_nsec int
    18  }
    19  
    20  // We could take timespec from syscall - but there it uses int32 and int64 for 32 bit and 64 bit arch, which complicates stuff for us
    21  
    22  // for pthread_sigmask:
    23  
    24  type sighow int32
    25  
    26  const (
    27  	SIG_BLOCK   sighow = 0
    28  	SIG_UNBLOCK sighow = 1
    29  	SIG_SETMASK sighow = 2
    30  )
    31  
    32  // Every wrapper here MUST NOT split stack or have write barriers! (see cgoGlue.go)
    33  
    34  //go:nosplit
    35  func pthread_attr_init(attr *pthread_attr) int32 {
    36  	return int32(libcCall1(libc_pthread_attr_init, uintptr(unsafe.Pointer(attr))))
    37  }
    38  
    39  //go:nosplit
    40  func pthread_attr_getstacksize(attr *pthread_attr, stacksize *size_t) int32 {
    41  	return int32(libcCall2(libc_pthread_attr_getstacksize, uintptr(unsafe.Pointer(attr)), uintptr(unsafe.Pointer(stacksize))))
    42  }
    43  
    44  //go:nosplit
    45  func pthread_attr_destroy(attr *pthread_attr) int32 {
    46  	return int32(libcCall1(libc_pthread_attr_destroy, uintptr(unsafe.Pointer(attr))))
    47  }
    48  
    49  //go:nosplit
    50  func pthread_sigmask(how sighow, set, oldset *sigset_t) int32 {
    51  	return int32(libcCall3(libc_pthread_sigmask, uintptr(how), uintptr(unsafe.Pointer(set)), uintptr(unsafe.Pointer(oldset))))
    52  }
    53  
    54  //go:nosplit
    55  func pthread_create(thread *pthread_t, attr *pthread_attr, start, arg unsafe.Pointer) int32 {
    56  	return int32(libcCall4(libc_pthread_create, uintptr(unsafe.Pointer(thread)), uintptr(unsafe.Pointer(attr)), uintptr(start), uintptr(arg)))
    57  }
    58  
    59  //go:nosplit
    60  func pthread_detach(thread pthread_t) int32 {
    61  	return int32(libcCall1(libc_pthread_detach, uintptr(thread)))
    62  }
    63  
    64  //go:nosplit
    65  func setenv(name, value *byte, overwrite int32) int32 {
    66  	return int32(libcCall3(libc_setenv, uintptr(unsafe.Pointer(name)), uintptr(unsafe.Pointer(value)), uintptr(overwrite)))
    67  }
    68  
    69  //go:nosplit
    70  func unsetenv(name *byte) int32 {
    71  	return int32(libcCall1(libc_unsetenv, uintptr(unsafe.Pointer(name))))
    72  }
    73  
    74  //go:nosplit
    75  func malloc(size uintptr) uintptr {
    76  	return libcCall1(libc_malloc, uintptr(size))
    77  }
    78  
    79  //go:nosplit
    80  func free(ptr unsafe.Pointer) {
    81  	libcCall1(libc_free, uintptr(ptr))
    82  }
    83  
    84  //go:nosplit
    85  func nanosleep(rgtp, rmtp *timespec) int32 {
    86  	return int32(libcCall2(libc_nanosleep, uintptr(unsafe.Pointer(rgtp)), uintptr(unsafe.Pointer(rmtp))))
    87  }
    88  
    89  //go:nosplit
    90  func sigfillset(set *sigset_t) int32 {
    91  	return int32(libcCall1(libc_sigfillset, uintptr(unsafe.Pointer(set))))
    92  }
    93  
    94  //go:nosplit
    95  func abort() {
    96  	libcCall0(libc_abort)
    97  }
    98  
    99  //go:nosplit
   100  func dprintf(fd uintptr, fmt string, arg ...uintptr) int32 {
   101  	var args [4]uintptr
   102  	copy(args[:], arg)
   103  	return int32(libcCall6(libc_dprintf, fd, (*reflect.StringHeader)(unsafe.Pointer(&fmt)).Data, args[0], args[1], args[2], args[3]))
   104  }
   105  
   106  //go:nosplit
   107  func strerror(err int32) uintptr {
   108  	return libcCall1(libc_strerror, uintptr(err))
   109  }