Skip to content

Commit fe72cf4

Browse files
author
Noam Preil
committed
add refcount file
1 parent 88142d4 commit fe72cf4

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

arrow/memory/refcount.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package memory
2+
3+
import (
4+
"sync/atomic"
5+
6+
"github.com/apache/arrow-go/v18/arrow/internal/debug"
7+
)
8+
9+
type Refcount struct {
10+
count atomic.Int64
11+
Dependencies []**Refcount
12+
Buffers []**Buffer
13+
Additional func()
14+
}
15+
16+
func (r *Refcount) Retain() {
17+
r.count.Add(1)
18+
}
19+
20+
func (r *Refcount) Release() {
21+
new := r.count.Add(-1)
22+
if new == 0 {
23+
for _, buffer := range r.Buffers {
24+
(*buffer).Release()
25+
*buffer = nil
26+
}
27+
for _, dependency := range r.Dependencies {
28+
(*dependency).Release()
29+
*dependency = nil
30+
}
31+
r.Buffers = nil
32+
r.Dependencies = nil
33+
if r.Additional != nil {
34+
r.Additional()
35+
}
36+
} else if new < 0 {
37+
// This branch can be optimized out when !debug
38+
// This avoids an unnecessary extra Load
39+
debug.Assert(false, "too many releases")
40+
}
41+
}

0 commit comments

Comments
 (0)