[Go] Make sure that arguments for which use memcpy when calling C are

still live after the call.  This ensures that they will not be
collected if the GC runs during the call.
This commit is contained in:
Ian Lance Taylor 2015-05-04 15:11:31 -07:00
commit 48263f4802
4 changed files with 77 additions and 34 deletions

View file

@ -44,14 +44,14 @@ type GoRetStruct struct {
$result.str.assign($input.p, $input.n);
%}
%typemap(out) RetStruct
%typemap(out,fragment="AllocateString") RetStruct
%{
$result = _swig_makegostring($1.str.data(), $1.str.length());
$result = Swig_AllocateString($1.str.data(), $1.str.length());
%}
%typemap(goout) RetStruct
%typemap(goout,fragment="CopyString") RetStruct
%{
$result = GoRetStruct{Str: $input}
$result = GoRetStruct{Str: swigCopyString($input)}
%}
%typemap(godirectorout) RetStruct
@ -81,23 +81,28 @@ type GoRetStruct struct {
}
%}
%typemap(directorin) MyStruct
%typemap(directorin,fragment="AllocateString") MyStruct
%{
$input = _swig_makegostring($1.str.data(), $1.str.length());
$input = Swig_AllocateString($1.str.data(), $1.str.length());
%}
%typemap(out) MyStruct
%typemap(godirectorin,fragment="CopyString") MyStruct
%{
$result = _swig_makegostring($1.str.data(), $1.str.length());
%}
%typemap(godirectorin) MyStruct
%{
if err := json.Unmarshal([]byte($input), &$result); err != nil {
if err := json.Unmarshal([]byte(swigCopyString($input)), &$result); err != nil {
panic(err)
}
%}
%typemap(out,fragment="AllocateString") MyStruct
%{
$result = Swig_AllocateString($1.str.data(), $1.str.length());
%}
%typemap(goout,fragment="CopyString") MyStruct
%{
$result = swigCopyString($input)
%}
%typemap(in) MyStruct
%{
$1.str.assign($input.p, $input.n);

View file

@ -56,14 +56,14 @@ type In json.Marshaler
%typemap(imtype) RetStruct "string"
%typemap(out) RetStruct
%typemap(out,fragment="AllocateString") RetStruct
%{
$result = _swig_makegostring($1.str.data(), $1.str.length());
$result = Swig_AllocateString($1.str.data(), $1.str.length());
%}
%typemap(goout) RetStruct
%typemap(goout,fragment="CopyString") RetStruct
%{
if err := json.Unmarshal([]byte($1), &$result); err != nil {
if err := json.Unmarshal([]byte(swigCopyString($1)), &$result); err != nil {
panic(err)
}
%}
@ -146,7 +146,7 @@ static void putuint64(std::string *s, size_t off, uint64_t v) {
%}
// Pack the vector into a string.
%typemap(argout) MyArray*
%typemap(argout,fragment="AllocateString") MyArray*
%{
{
size_t tot = 8;
@ -164,15 +164,15 @@ static void putuint64(std::string *s, size_t off, uint64_t v) {
str.replace(off, p->size(), *p);
off += p->size();
}
*$input = _swig_makegostring(str.data(), str.size());
*$input = Swig_AllocateString(str.data(), str.size());
}
%}
// Unpack the string into a []string.
%typemap(goargout) MyArray*
%typemap(goargout,fragment="CopyString") MyArray*
%{
{
str := *$input
str := swigCopyString(*$input)
bin := binary.LittleEndian
size := bin.Uint64([]byte(str[:8]))
str = str[8:]