summaryrefslogtreecommitdiff
path: root/lib/containers
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-02-03 13:04:17 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-02-12 16:16:53 -0700
commit21e92e5dea4d8efc65403eeaee91b32856b86cb6 (patch)
tree331fc7c8d8765fa11364d4fc28302e829a34e678 /lib/containers
parent11ac4fae146e5f599f34a5fafa27e20fecf713a9 (diff)
btrfsitem: Add `Free` and `CloneItem` methods to Items
Diffstat (limited to 'lib/containers')
-rw-r--r--lib/containers/slicepool.go35
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/containers/slicepool.go b/lib/containers/slicepool.go
new file mode 100644
index 0000000..861691a
--- /dev/null
+++ b/lib/containers/slicepool.go
@@ -0,0 +1,35 @@
+// Copyright (C) 2023 Luke Shumaker <lukeshu@lukeshu.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+package containers
+
+import (
+ "git.lukeshu.com/go/typedsync"
+)
+
+type SlicePool[T any] struct {
+ // TODO(lukeshu): Consider bucketing slices by size, to
+ // increase odds that the `cap(ret) >= size` check passes.
+ inner typedsync.Pool[[]T]
+}
+
+func (p *SlicePool[T]) Get(size int) []T {
+ if size == 0 {
+ return nil
+ }
+ ret, ok := p.inner.Get()
+ if ok && cap(ret) >= size {
+ ret = ret[:size]
+ } else {
+ ret = make([]T, size)
+ }
+ return ret
+}
+
+func (p *SlicePool[T]) Put(slice []T) {
+ if slice == nil {
+ return
+ }
+ p.inner.Put(slice)
+}