gostring.swg 851 B

1234567891011121314151617181920212223242526272829
  1. /* ------------------------------------------------------------
  2. * gostring.swg
  3. *
  4. * Support for returning strings from C to Go.
  5. * ------------------------------------------------------------ */
  6. // C/C++ code to convert a memory buffer into a Go string allocated in
  7. // C/C++ memory.
  8. %fragment("AllocateString", "runtime") %{
  9. static _gostring_ Swig_AllocateString(const char *p, size_t l) {
  10. _gostring_ ret;
  11. ret.p = (char*)malloc(l);
  12. memcpy(ret.p, p, l);
  13. ret.n = l;
  14. return ret;
  15. }
  16. %}
  17. // Go code to convert a string allocated in C++ memory to one
  18. // allocated in Go memory.
  19. %fragment("CopyString", "go_runtime") %{
  20. type swig_gostring struct { p uintptr; n int }
  21. func swigCopyString(s string) string {
  22. p := *(*swig_gostring)(unsafe.Pointer(&s))
  23. r := string((*[0x7fffffff]byte)(unsafe.Pointer(p.p))[:p.n])
  24. Swig_free(p.p)
  25. return r
  26. }
  27. %}