goruntime.swg 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /* ------------------------------------------------------------
  2. * goruntime.swg
  3. *
  4. * Go runtime code for the various generated files.
  5. * ------------------------------------------------------------ */
  6. %inline %{
  7. static void Swig_free(void* p) {
  8. free(p);
  9. }
  10. %}
  11. %insert(runtime) %{
  12. #include <stddef.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <sys/types.h>
  17. %}
  18. #if SWIGGO_CGO
  19. %insert(cgo_comment_typedefs) %{
  20. #include <stdint.h>
  21. %}
  22. #endif
  23. #if SWIGGO_INTGO_SIZE == 32
  24. %insert(runtime) %{
  25. typedef int intgo;
  26. typedef unsigned int uintgo;
  27. %}
  28. #if SWIGGO_CGO
  29. %insert(cgo_comment_typedefs) %{
  30. typedef int intgo;
  31. typedef unsigned int uintgo;
  32. %}
  33. #endif
  34. #elif SWIGGO_INTGO_SIZE == 64
  35. %insert(runtime) %{
  36. typedef long long intgo;
  37. typedef unsigned long long uintgo;
  38. %}
  39. #if SWIGGO_CGO
  40. %insert(cgo_comment_typedefs) %{
  41. typedef long long intgo;
  42. typedef unsigned long long uintgo;
  43. %}
  44. #endif
  45. #else
  46. %insert(runtime) %{
  47. typedef ptrdiff_t intgo;
  48. typedef size_t uintgo;
  49. %}
  50. #if SWIGGO_CGO
  51. %insert(cgo_comment_typedefs) %{
  52. typedef ptrdiff_t intgo;
  53. typedef size_t uintgo;
  54. %}
  55. #endif
  56. #endif
  57. %insert(runtime) %{
  58. typedef struct { char *p; intgo n; } _gostring_;
  59. typedef struct { void* array; intgo len; intgo cap; } _goslice_;
  60. %}
  61. #ifdef SWIGGO_CGO
  62. %insert(cgo_comment_typedefs) %{
  63. typedef struct { char *p; intgo n; } _gostring_;
  64. typedef struct { void* array; intgo len; intgo cap; } _goslice_;
  65. %}
  66. #endif
  67. #ifndef SWIGGO_GCCGO
  68. /* Boilerplate for C/C++ code when using 6g/8g. This code is compiled
  69. with gcc. */
  70. %insert(runtime) %{
  71. #define swiggo_size_assert_eq(x, y, name) typedef char name[(x-y)*(x-y)*-2+1];
  72. #define swiggo_size_assert(t, n) swiggo_size_assert_eq(sizeof(t), n, swiggo_sizeof_##t##_is_not_##n)
  73. swiggo_size_assert(char, 1)
  74. swiggo_size_assert(short, 2)
  75. swiggo_size_assert(int, 4)
  76. typedef long long swiggo_long_long;
  77. swiggo_size_assert(swiggo_long_long, 8)
  78. swiggo_size_assert(float, 4)
  79. swiggo_size_assert(double, 8)
  80. #ifdef __cplusplus
  81. extern "C" {
  82. #endif
  83. extern void crosscall2(void (*fn)(void *, int), void *, int);
  84. extern char* _cgo_topofstack(void) __attribute__ ((weak));
  85. extern void _cgo_allocate(void *, int);
  86. extern void _cgo_panic(void *, int);
  87. #ifdef __cplusplus
  88. }
  89. #endif
  90. static char *_swig_topofstack() {
  91. if (_cgo_topofstack) {
  92. return _cgo_topofstack();
  93. } else {
  94. return 0;
  95. }
  96. }
  97. static void _swig_gopanic(const char *p) {
  98. struct {
  99. const char *p;
  100. } a;
  101. a.p = p;
  102. crosscall2(_cgo_panic, &a, (int) sizeof a);
  103. }
  104. %}
  105. #if !SWIGGO_CGO
  106. /* This is here for backward compatibility, but it will not work
  107. with Go 1.5 or later. Do not use it in new code. */
  108. %insert(runtime) %{
  109. static void *_swig_goallocate(size_t len) {
  110. struct {
  111. size_t len;
  112. void *ret;
  113. } a;
  114. a.len = len;
  115. crosscall2(_cgo_allocate, &a, (int) sizeof a);
  116. return a.ret;
  117. }
  118. %}
  119. #endif
  120. #if !SWIGGO_CGO
  121. /* Boilerplate for C code when using 6g/8g. This code is compiled
  122. with 6c/8c. */
  123. %insert(gc_header) %{
  124. #include "runtime.h"
  125. #include "cgocall.h"
  126. #pragma dataflag 16
  127. static void *cgocall = runtime·cgocall;
  128. #pragma dataflag 16
  129. void *·_cgo_runtime_cgocall = &cgocall;
  130. %}
  131. #endif
  132. #else
  133. /* Boilerplate for C/C++ code when using gccgo. */
  134. %insert(runtime) %{
  135. #define SWIGGO_GCCGO
  136. #ifdef __cplusplus
  137. extern "C" {
  138. #endif
  139. extern void *_cgo_allocate(size_t);
  140. extern void _cgo_panic(const char *);
  141. #ifdef __cplusplus
  142. }
  143. #endif
  144. #define _swig_goallocate _cgo_allocate
  145. #define _swig_gopanic _cgo_panic
  146. %}
  147. #if !SWIGGO_CGO
  148. %insert(runtime) %{
  149. /* Implementations of SwigCgocall and friends for different versions
  150. of gccgo. The Go code will call these functions using C names with
  151. a prefix of the module name. The implementations here call the
  152. routine in libgo. The routines to call vary depending on the gccgo
  153. version. We assume that the version of gcc used to compile this
  154. file is the same as the version of gccgo. */
  155. #ifdef __cplusplus
  156. extern "C" {
  157. #endif
  158. #define SWIG_GCC_VERSION \
  159. (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
  160. #if SWIG_GCC_VERSION < 40700
  161. #define SwigDoCgocall()
  162. #define SwigDoCgocallDone()
  163. #define SwigDoCgocallBack()
  164. #define SwigDoCgocallBackDone()
  165. #elif SWIG_GCC_VERSION == 40700
  166. void SwigDoCgocall(void) __asm__("libgo_syscall.syscall.Entersyscall");
  167. void SwigDoCgocallDone(void) __asm__("libgo_syscall.syscall.Exitsyscall");
  168. void SwigDoCgocallBack(void) __asm__("libgo_syscall.syscall.Exitsyscall");
  169. void SwigDoCgocallBackDone(void) __asm__("libgo_syscall.syscall.Entersyscall");
  170. #else
  171. void SwigDoCgocall(void) __asm__("syscall.Cgocall");
  172. void SwigDoCgocallDone(void) __asm__("syscall.CgocallDone");
  173. void SwigDoCgocallBack(void) __asm__("syscall.CgocallBack");
  174. void SwigDoCgocallBackDone(void) __asm__("syscall.CgocallBackDone");
  175. #endif
  176. #define SWIGSTRINGIFY2(s) #s
  177. #define SWIGSTRINGIFY(s) SWIGSTRINGIFY2(s)
  178. void SwigCgocall()
  179. __asm__(SWIGSTRINGIFY(SWIGGO_PREFIX) ".SwigCgocall");
  180. void SwigCgocall() {
  181. SwigDoCgocall();
  182. }
  183. void SwigCgocallDone()
  184. __asm__(SWIGSTRINGIFY(SWIGGO_PREFIX) ".SwigCgocallDone");
  185. void SwigCgocallDone() {
  186. SwigDoCgocallDone();
  187. }
  188. void SwigCgocallBack()
  189. __asm__(SWIGSTRINGIFY(SWIGGO_PREFIX) ".SwigCgocallBack");
  190. void SwigCgocallBack() {
  191. SwigDoCgocallBack();
  192. }
  193. void SwigCgocallBackDone()
  194. __asm__(SWIGSTRINGIFY(SWIGGO_PREFIX) ".SwigCgocallBackDone");
  195. void SwigCgocallBackDone() {
  196. SwigDoCgocallBackDone();
  197. }
  198. #undef SWIGSTRINGIFY
  199. #undef SWIGSTRINGIFY2
  200. #ifdef __cplusplus
  201. }
  202. #endif
  203. %}
  204. #endif
  205. #endif
  206. #if !SWIGGO_CGO
  207. %insert(runtime) %{
  208. /* This is here for backward compatibility, but it will not work
  209. with Go 1.5 or later. Do not use it in new code. */
  210. static _gostring_ _swig_makegostring(const char *p, size_t l) {
  211. _gostring_ ret;
  212. ret.p = (char*)_swig_goallocate(l + 1);
  213. memcpy(ret.p, p, l);
  214. ret.n = l;
  215. return ret;
  216. }
  217. %}
  218. #endif
  219. %insert(runtime) %{
  220. #define SWIG_contract_assert(expr, msg) \
  221. if (!(expr)) { _swig_gopanic(msg); } else
  222. %}
  223. #ifndef SWIGGO_GCCGO
  224. %go_import("unsafe", _ "runtime/cgo")
  225. #if !SWIGGO_CGO
  226. %insert(go_header) %{
  227. var _cgo_runtime_cgocall func(unsafe.Pointer, uintptr)
  228. %}
  229. #endif
  230. #else
  231. %go_import("syscall", "unsafe")
  232. %insert(go_header) %{
  233. type _ syscall.Sockaddr
  234. %}
  235. #endif
  236. %insert(go_header) %{
  237. type _ unsafe.Pointer
  238. %}
  239. /* Swig_always_false is used to conditionally assign parameters to
  240. Swig_escape_val so that the compiler thinks that they escape. We
  241. only assign them if Swig_always_false is true, which it never is.
  242. We export the variable so that the compiler doesn't realize that it
  243. is never set. */
  244. %insert(go_header) %{
  245. var Swig_escape_always_false bool
  246. var Swig_escape_val interface{}
  247. %}
  248. /* Function pointers are translated by the code in go.cxx into
  249. _swig_fnptr. Member pointers are translated to _swig_memberptr. */
  250. %insert(go_header) %{
  251. type _swig_fnptr *byte
  252. type _swig_memberptr *byte
  253. %}
  254. /* Handle memory management for directors. */
  255. %insert(director) %{
  256. #include <map>
  257. namespace {
  258. struct GCItem {
  259. virtual ~GCItem() {}
  260. };
  261. struct GCItem_var {
  262. GCItem_var(GCItem *item = 0) : _item(item) {
  263. }
  264. GCItem_var& operator=(GCItem *item) {
  265. GCItem *tmp = _item;
  266. _item = item;
  267. delete tmp;
  268. return *this;
  269. }
  270. ~GCItem_var() {
  271. delete _item;
  272. }
  273. GCItem* operator->() {
  274. return _item;
  275. }
  276. private:
  277. GCItem *_item;
  278. };
  279. template <typename Type>
  280. struct GCItem_T : GCItem {
  281. GCItem_T(Type *ptr) : _ptr(ptr) {
  282. }
  283. virtual ~GCItem_T() {
  284. delete _ptr;
  285. }
  286. private:
  287. Type *_ptr;
  288. };
  289. }
  290. class Swig_memory {
  291. public:
  292. template <typename Type>
  293. void swig_acquire_pointer(Type* vptr) {
  294. if (vptr) {
  295. swig_owner[vptr] = new GCItem_T<Type>(vptr);
  296. }
  297. }
  298. private:
  299. typedef std::map<void *, GCItem_var> swig_ownership_map;
  300. swig_ownership_map swig_owner;
  301. };
  302. template <typename Type>
  303. static void swig_acquire_pointer(Swig_memory** pmem, Type* ptr) {
  304. if (!pmem) {
  305. *pmem = new Swig_memory;
  306. }
  307. (*pmem)->swig_acquire_pointer(ptr);
  308. }
  309. %}
  310. /* For directors we need C++ to track a Go pointer. Since we can't
  311. pass a Go pointer into C++, we use a map to track the pointers on
  312. the Go side. */
  313. %go_import("sync")
  314. %insert(go_header) %{
  315. type _ sync.Mutex
  316. %}
  317. %insert(go_director) %{
  318. var swigDirectorTrack struct {
  319. sync.Mutex
  320. m map[int]interface{}
  321. c int
  322. }
  323. func swigDirectorAdd(v interface{}) int {
  324. swigDirectorTrack.Lock()
  325. defer swigDirectorTrack.Unlock()
  326. if swigDirectorTrack.m == nil {
  327. swigDirectorTrack.m = make(map[int]interface{})
  328. }
  329. swigDirectorTrack.c++
  330. ret := swigDirectorTrack.c
  331. swigDirectorTrack.m[ret] = v
  332. return ret
  333. }
  334. func swigDirectorLookup(c int) interface{} {
  335. swigDirectorTrack.Lock()
  336. defer swigDirectorTrack.Unlock()
  337. ret := swigDirectorTrack.m[c]
  338. if ret == nil {
  339. panic("C++ director pointer not found (possible use-after-free)")
  340. }
  341. return ret
  342. }
  343. func swigDirectorDelete(c int) {
  344. swigDirectorTrack.Lock()
  345. defer swigDirectorTrack.Unlock()
  346. if swigDirectorTrack.m[c] == nil {
  347. if c > swigDirectorTrack.c {
  348. panic("C++ director pointer invalid (possible memory corruption")
  349. } else {
  350. panic("C++ director pointer not found (possible use-after-free)")
  351. }
  352. }
  353. delete(swigDirectorTrack.m, c)
  354. }
  355. %}