2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org
.apache
.arrow
.adapter
.orc
;
20 import java
.util
.concurrent
.atomic
.AtomicInteger
;
22 import org
.apache
.arrow
.memory
.ArrowBuf
;
23 import org
.apache
.arrow
.memory
.BufferAllocator
;
24 import org
.apache
.arrow
.memory
.OwnershipTransferResult
;
25 import org
.apache
.arrow
.memory
.ReferenceManager
;
26 import org
.apache
.arrow
.util
.Preconditions
;
29 * A simple reference manager implementation for memory allocated by native code.
30 * The underlying memory will be released when reference count reach zero.
32 public class OrcReferenceManager
implements ReferenceManager
{
33 private final AtomicInteger bufRefCnt
= new AtomicInteger(0);
35 private OrcMemoryJniWrapper memory
;
37 OrcReferenceManager(OrcMemoryJniWrapper memory
) {
42 public int getRefCount() {
43 return bufRefCnt
.get();
47 public boolean release() {
52 public boolean release(int decrement
) {
53 Preconditions
.checkState(decrement
>= 1,
54 "ref count decrement should be greater than or equal to 1");
55 // decrement the ref count
58 refCnt
= bufRefCnt
.addAndGet(-decrement
);
60 // refcount of this reference manager has dropped to 0
61 // release the underlying memory
65 // the new ref count should be >= 0
66 Preconditions
.checkState(refCnt
>= 0, "RefCnt has gone negative");
71 public void retain() {
76 public void retain(int increment
) {
77 Preconditions
.checkArgument(increment
> 0, "retain(%s) argument is not positive", increment
);
78 bufRefCnt
.addAndGet(increment
);
82 public ArrowBuf
retain(ArrowBuf srcBuffer
, BufferAllocator targetAllocator
) {
88 public ArrowBuf
deriveBuffer(ArrowBuf sourceBuffer
, long index
, long length
) {
89 final long derivedBufferAddress
= sourceBuffer
.memoryAddress() + index
;
91 // create new ArrowBuf
92 final ArrowBuf derivedBuf
= new ArrowBuf(
95 length
, // length (in bytes) in the underlying memory chunk for this new ArrowBuf
96 derivedBufferAddress
// starting byte address in the underlying memory for this new ArrowBuf,
103 public OwnershipTransferResult
transferOwnership(ArrowBuf sourceBuffer
, BufferAllocator targetAllocator
) {
104 throw new UnsupportedOperationException();
108 public BufferAllocator
getAllocator() {
113 public long getSize() {
114 return memory
.getSize();
118 public long getAccountedSize() {