roee88 commented on a change in pull request #11067:
URL: https://github.com/apache/arrow/pull/11067#discussion_r704991017



##########
File path: java/ffi/src/main/java/org/apache/arrow/ffi/FFIReferenceManager.java
##########
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.arrow.ffi;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.arrow.memory.ArrowBuf;
+import org.apache.arrow.memory.BufferAllocator;
+import org.apache.arrow.memory.OwnershipTransferResult;
+import org.apache.arrow.memory.ReferenceManager;
+import org.apache.arrow.util.Preconditions;
+
+/**
+ * A ReferenceManager implementation that holds a {@link 
org.apache.arrow.ffi.BaseStruct}.
+ * <p>
+ * A reference count is maintained and once it reaches zero the struct
+ * is released (as per the C data interface specification) and closed.
+ */
+final class FFIReferenceManager implements ReferenceManager {
+  private final AtomicInteger bufRefCnt = new AtomicInteger(0);
+
+  private final BaseStruct struct;
+
+  FFIReferenceManager(BaseStruct struct) {
+    this.struct = struct;
+  }
+
+  @Override
+  public int getRefCount() {
+    return bufRefCnt.get();
+  }
+
+  @Override
+  public boolean release() {
+    return release(1);
+  }
+
+  @Override
+  public boolean release(int decrement) {
+    Preconditions.checkState(decrement >= 1, "ref count decrement should be 
greater than or equal to 1");
+    // decrement the ref count
+    final int refCnt;
+    synchronized (this) {
+      refCnt = bufRefCnt.addAndGet(-decrement);
+      if (refCnt == 0) {
+        // refcount of this reference manager has dropped to 0
+        // release the underlying memory
+        struct.release();
+        struct.close();
+      }
+    }
+    // the new ref count should be >= 0
+    Preconditions.checkState(refCnt >= 0, "RefCnt has gone negative");
+    return refCnt == 0;
+  }
+
+  @Override
+  public void retain() {
+    retain(1);
+  }
+
+  @Override
+  public void retain(int increment) {
+    Preconditions.checkArgument(increment > 0, "retain(%s) argument is not 
positive", increment);
+    bufRefCnt.addAndGet(increment);
+  }
+
+  @Override
+  public ArrowBuf retain(ArrowBuf srcBuffer, BufferAllocator targetAllocator) {
+    retain();
+    return srcBuffer;
+  }
+
+  @Override
+  public ArrowBuf deriveBuffer(ArrowBuf sourceBuffer, long index, long length) 
{
+    final long derivedBufferAddress = sourceBuffer.memoryAddress() + index;

Review comment:
       It seems like slice is using deriveBuffer so it's not possible




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to