| 22 | // Seq is a sequence of machine-dependent encoded values. |
| 23 | // Used by automatically generated language bindings to talk to Go. |
| 24 | public class Seq { |
| 25 | private static Logger log = Logger.getLogger("GoSeq"); |
| 26 | |
| 27 | // also known to bind/seq/ref.go and bind/objc/seq_darwin.m |
| 28 | private static final int NULL_REFNUM = 41; |
| 29 | |
| 30 | // use single Ref for null Object |
| 31 | public static final Ref nullRef = new Ref(NULL_REFNUM, null); |
| 32 | |
| 33 | // The singleton GoRefQueue |
| 34 | private static final GoRefQueue goRefQueue = new GoRefQueue(); |
| 35 | |
| 36 | static { |
| 37 | System.loadLibrary("gojni"); |
| 38 | init(); |
| 39 | Universe.touch(); |
| 40 | } |
| 41 | |
| 42 | // setContext sets the context in the go-library to be used in RunOnJvm. |
| 43 | public static void setContext(Context context) { |
| 44 | setContext((java.lang.Object)context); |
| 45 | } |
| 46 | |
| 47 | private static native void init(); |
| 48 | |
| 49 | // Empty method to run class initializer |
| 50 | public static void touch() {} |
| 51 | |
| 52 | private Seq() { |
| 53 | } |
| 54 | |
| 55 | // ctx is an android.context.Context. |
| 56 | static native void setContext(java.lang.Object ctx); |
| 57 | |
| 58 | public static void incRefnum(int refnum) { |
| 59 | tracker.incRefnum(refnum); |
| 60 | } |
| 61 | |
| 62 | // incRef increments the reference count of Java objects. |
| 63 | // For proxies for Go objects, it calls into the Proxy method |
| 64 | // incRefnum() to make sure the Go reference count is positive |
| 65 | // even if the Proxy is garbage collected and its Ref is finalized. |
| 66 | public static int incRef(Object o) { |
| 67 | return tracker.inc(o); |
| 68 | } |
| 69 | |
| 70 | public static int incGoObjectRef(GoObject o) { |
| 71 | return o.incRefnum(); |
| 72 | } |
| 73 | |
| 74 | // trackGoRef tracks a Go reference and decrements its refcount |
| 75 | // when the given GoObject wrapper is garbage collected. |
| 76 | // |
| 77 | // TODO(crawshaw): We could cut down allocations for frequently |
| 78 | // sent Go objects by maintaining a map to weak references. This |
| 79 | // however, would require allocating two objects per reference |
| 80 | // instead of one. It also introduces weak references, the bane |
| 81 | // of any Java debugging session. |