github.com/shijuvar/go@v0.0.0-20141209052335-e8f13700b70c/src/runtime/cgocallback.go (about) 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 5 package runtime 6 7 import "unsafe" 8 9 // These functions are called from C code via cgo/callbacks.c. 10 11 // Allocate memory. This allocates the requested number of bytes in 12 // memory controlled by the Go runtime. The allocated memory will be 13 // zeroed. You are responsible for ensuring that the Go garbage 14 // collector can see a pointer to the allocated memory for as long as 15 // it is valid, e.g., by storing a pointer in a local variable in your 16 // C function, or in memory allocated by the Go runtime. If the only 17 // pointers are in a C global variable or in memory allocated via 18 // malloc, then the Go garbage collector may collect the memory. 19 // 20 // TODO(rsc,iant): This memory is untyped. 21 // Either we need to add types or we need to stop using it. 22 23 func _cgo_allocate_internal(len uintptr) unsafe.Pointer { 24 if len == 0 { 25 len = 1 26 } 27 ret := unsafe.Pointer(&make([]unsafe.Pointer, (len+ptrSize-1)/ptrSize)[0]) 28 c := new(cgomal) 29 c.alloc = ret 30 gp := getg() 31 c.next = gp.m.cgomal 32 gp.m.cgomal = c 33 return ret 34 } 35 36 // Panic. 37 38 func _cgo_panic_internal(p *byte) { 39 panic(gostringnocopy(p)) 40 }