Skip to content

Commit a9bf981

Browse files
authored
Cgo add cbytes implementation (rebased version of #3318) (#4470)
cgo: added CBytes implementation
1 parent d4729f9 commit a9bf981

File tree

10 files changed

+61
-1
lines changed

10 files changed

+61
-1
lines changed

cgo/cgo.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,13 @@ func __GoBytes(unsafe.Pointer, uintptr) []byte
162162
func GoBytes(ptr unsafe.Pointer, length C.int) []byte {
163163
return C.__GoBytes(ptr, uintptr(length))
164164
}
165+
166+
//go:linkname C.__CBytes runtime.cgo_CBytes
167+
func __CBytes([]byte) unsafe.Pointer
168+
169+
func CBytes(b []byte) unsafe.Pointer {
170+
return C.__CBytes(b)
171+
}
165172
`
166173

167174
// Process extracts `import "C"` statements from the AST, parses the comment
@@ -218,7 +225,7 @@ func Process(files []*ast.File, dir, importPath string, fset *token.FileSet, cfl
218225
switch decl := decl.(type) {
219226
case *ast.FuncDecl:
220227
switch decl.Name.Name {
221-
case "CString", "GoString", "GoStringN", "__GoStringN", "GoBytes", "__GoBytes":
228+
case "CString", "GoString", "GoStringN", "__GoStringN", "GoBytes", "__GoBytes", "CBytes", "__CBytes":
222229
// Adjust the name to have a "C." prefix so it is correctly
223230
// resolved.
224231
decl.Name.Name = "C." + decl.Name.Name

cgo/testdata/basic.out.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ func C.GoBytes(ptr unsafe.Pointer, length C.int) []byte {
2424
return C.__GoBytes(ptr, uintptr(length))
2525
}
2626

27+
//go:linkname C.__CBytes runtime.cgo_CBytes
28+
func C.__CBytes([]byte) unsafe.Pointer
29+
30+
func C.CBytes(b []byte) unsafe.Pointer {
31+
return C.__CBytes(b)
32+
}
33+
2734
type (
2835
C.char uint8
2936
C.schar int8

cgo/testdata/const.out.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ func C.GoBytes(ptr unsafe.Pointer, length C.int) []byte {
2424
return C.__GoBytes(ptr, uintptr(length))
2525
}
2626

27+
//go:linkname C.__CBytes runtime.cgo_CBytes
28+
func C.__CBytes([]byte) unsafe.Pointer
29+
30+
func C.CBytes(b []byte) unsafe.Pointer {
31+
return C.__CBytes(b)
32+
}
33+
2734
type (
2835
C.char uint8
2936
C.schar int8

cgo/testdata/errors.out.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ func C.GoBytes(ptr unsafe.Pointer, length C.int) []byte {
4444
return C.__GoBytes(ptr, uintptr(length))
4545
}
4646

47+
//go:linkname C.__CBytes runtime.cgo_CBytes
48+
func C.__CBytes([]byte) unsafe.Pointer
49+
50+
func C.CBytes(b []byte) unsafe.Pointer {
51+
return C.__CBytes(b)
52+
}
53+
4754
type (
4855
C.char uint8
4956
C.schar int8

cgo/testdata/flags.out.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ func C.GoBytes(ptr unsafe.Pointer, length C.int) []byte {
2929
return C.__GoBytes(ptr, uintptr(length))
3030
}
3131

32+
//go:linkname C.__CBytes runtime.cgo_CBytes
33+
func C.__CBytes([]byte) unsafe.Pointer
34+
35+
func C.CBytes(b []byte) unsafe.Pointer {
36+
return C.__CBytes(b)
37+
}
38+
3239
type (
3340
C.char uint8
3441
C.schar int8

cgo/testdata/symbols.out.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ func C.GoBytes(ptr unsafe.Pointer, length C.int) []byte {
2424
return C.__GoBytes(ptr, uintptr(length))
2525
}
2626

27+
//go:linkname C.__CBytes runtime.cgo_CBytes
28+
func C.__CBytes([]byte) unsafe.Pointer
29+
30+
func C.CBytes(b []byte) unsafe.Pointer {
31+
return C.__CBytes(b)
32+
}
33+
2734
type (
2835
C.char uint8
2936
C.schar int8

cgo/testdata/types.out.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ func C.GoBytes(ptr unsafe.Pointer, length C.int) []byte {
2424
return C.__GoBytes(ptr, uintptr(length))
2525
}
2626

27+
//go:linkname C.__CBytes runtime.cgo_CBytes
28+
func C.__CBytes([]byte) unsafe.Pointer
29+
30+
func C.CBytes(b []byte) unsafe.Pointer {
31+
return C.__CBytes(b)
32+
}
33+
2734
type (
2835
C.char uint8
2936
C.schar int8

src/runtime/string.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,10 @@ func cgo_GoBytes(ptr unsafe.Pointer, length uintptr) []byte {
283283
}
284284
return buf
285285
}
286+
287+
func cgo_CBytes(b []byte) unsafe.Pointer {
288+
p := malloc(uintptr(len(b)))
289+
s := unsafe.Slice((*byte)(p), len(b))
290+
copy(s, b)
291+
return p
292+
}

testdata/cgo/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ func main() {
165165
println("C.GoString(nil):", C.GoString(nil))
166166
println("len(C.GoStringN(nil, 0)):", len(C.GoStringN(nil, 0)))
167167
println("len(C.GoBytes(nil, 0)):", len(C.GoBytes(nil, 0)))
168+
println("len(C.GoBytes(C.CBytes(nil),0)):", len(C.GoBytes(C.CBytes(nil), 0)))
169+
println(`rountrip CBytes:`, C.GoString((*C.char)(C.CBytes([]byte("hello\000")))))
168170

169171
// libc: test whether C functions work at all.
170172
buf1 := []byte("foobar\x00")

testdata/cgo/out.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ C.CStringN: 4 2 0 4 8
7373
C.GoString(nil):
7474
len(C.GoStringN(nil, 0)): 0
7575
len(C.GoBytes(nil, 0)): 0
76+
len(C.GoBytes(C.CBytes(nil),0)): 0
77+
rountrip CBytes: hello
7678
copied string: foobar
7779
CGo sqrt(3): +1.732051e+000
7880
C sqrt(3): +1.732051e+000

0 commit comments

Comments
 (0)