| 152 | static final RefTracker tracker = new RefTracker(); |
| 153 | |
| 154 | static final class RefTracker { |
| 155 | private static final int REF_OFFSET = 42; |
| 156 | |
| 157 | // Next Java object reference number. |
| 158 | // |
| 159 | // Reference numbers are positive for Java objects, |
| 160 | // and start, arbitrarily at a different offset to Go |
| 161 | // to make debugging by reading Seq hex a little easier. |
| 162 | private int next = REF_OFFSET; // next Java object ref |
| 163 | |
| 164 | // Java objects that have been passed to Go. refnum -> Ref |
| 165 | // The Ref obj field is non-null. |
| 166 | // This map pins Java objects so they don't get GCed while the |
| 167 | // only reference to them is held by Go code. |
| 168 | private final RefMap javaObjs = new RefMap(); |
| 169 | |
| 170 | // Java objects to refnum |
| 171 | private final IdentityHashMap<Object, Integer> javaRefs = new IdentityHashMap<>(); |
| 172 | |
| 173 | // inc increments the reference count of a Java object when it |
| 174 | // is sent to Go. inc returns the refnum for the object. |
| 175 | synchronized int inc(Object o) { |
| 176 | if (o == null) { |
| 177 | return NULL_REFNUM; |
| 178 | } |
| 179 | if (o instanceof Proxy) { |
| 180 | return ((Proxy)o).incRefnum(); |
| 181 | } |
| 182 | Integer refnumObj = javaRefs.get(o); |
| 183 | if (refnumObj == null) { |
| 184 | if (next == Integer.MAX_VALUE) { |
| 185 | throw new RuntimeException("createRef overflow for " + o); |
| 186 | } |
| 187 | refnumObj = next++; |
| 188 | javaRefs.put(o, refnumObj); |
| 189 | } |
| 190 | int refnum = refnumObj; |
| 191 | Ref ref = javaObjs.get(refnum); |
| 192 | if (ref == null) { |
| 193 | ref = new Ref(refnum, o); |
| 194 | javaObjs.put(refnum, ref); |
| 195 | } |
| 196 | ref.inc(); |
| 197 | return refnum; |
| 198 | } |
| 199 | |
| 200 | synchronized void incRefnum(int refnum) { |
| 201 | Ref ref = javaObjs.get(refnum); |
| 202 | if (ref == null) { |
| 203 | throw new RuntimeException("referenced Java object is not found: refnum="+refnum); |
| 204 | } |
| 205 | ref.inc(); |
| 206 | } |
| 207 | |
| 208 | // dec decrements the reference count of a Java object when |
| 209 | // Go signals a corresponding proxy object is finalized. |
| 210 | // If the count reaches zero, the Java object is removed |
| 211 | // from the javaObjs map. |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…