cgocallback.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package runtime
  5. import "unsafe"
  6. // These functions are called from C code via cgo/callbacks.c.
  7. // Allocate memory. This allocates the requested number of bytes in
  8. // memory controlled by the Go runtime. The allocated memory will be
  9. // zeroed. You are responsible for ensuring that the Go garbage
  10. // collector can see a pointer to the allocated memory for as long as
  11. // it is valid, e.g., by storing a pointer in a local variable in your
  12. // C function, or in memory allocated by the Go runtime. If the only
  13. // pointers are in a C global variable or in memory allocated via
  14. // malloc, then the Go garbage collector may collect the memory.
  15. //
  16. // TODO(rsc,iant): This memory is untyped.
  17. // Either we need to add types or we need to stop using it.
  18. func _cgo_allocate_internal(len uintptr) unsafe.Pointer {
  19. if len == 0 {
  20. len = 1
  21. }
  22. ret := unsafe.Pointer(&make([]unsafe.Pointer, (len+ptrSize-1)/ptrSize)[0])
  23. c := new(cgomal)
  24. c.alloc = ret
  25. gp := getg()
  26. c.next = gp.m.cgomal
  27. gp.m.cgomal = c
  28. return ret
  29. }
  30. // Panic.
  31. func _cgo_panic_internal(p *byte) {
  32. panic(gostringnocopy(p))
  33. }