-
Notifications
You must be signed in to change notification settings - Fork 17.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a C.CBytes function to copy a Go byte slice into C memory. This returns an unsafe.Pointer, since that is what needs to be passed to C.free, and the data is often opaque bytes anyway. Fixes #14838 Change-Id: Ic7bc29637eb6f1f5ee409b3898c702a59833a85a Reviewed-on: https://go-review.googlesource.com/20762 Reviewed-by: Ian Lance Taylor <[email protected]>
- Loading branch information
1 parent
3e54ca9
commit 5a34472
Showing
4 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2016 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// Issue 14838. add CBytes function | ||
|
||
package cgotest | ||
|
||
/* | ||
#include <stdlib.h> | ||
int check_cbytes(char *b, size_t l) { | ||
int i; | ||
for (i = 0; i < l; i++) { | ||
if (b[i] != i) { | ||
return 0; | ||
} | ||
} | ||
return 1; | ||
} | ||
*/ | ||
import "C" | ||
|
||
import ( | ||
"testing" | ||
"unsafe" | ||
) | ||
|
||
func test14838(t *testing.T) { | ||
data := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} | ||
cData := C.CBytes(data) | ||
defer C.free(cData) | ||
|
||
if C.check_cbytes((*C.char)(cData), C.size_t(len(data))) == 0 { | ||
t.Fatalf("mismatched data: expected %v, got %v", data, (*(*[10]byte)(unsafe.Pointer(cData)))[:]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters