summaryrefslogtreecommitdiff
path: root/lib/btrfs/btrfsitem/item_metadata.go
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/btrfs/btrfsitem/item_metadata.go
parent11ac4fae146e5f599f34a5fafa27e20fecf713a9 (diff)
btrfsitem: Add `Free` and `CloneItem` methods to Items
Diffstat (limited to 'lib/btrfs/btrfsitem/item_metadata.go')
-rw-r--r--lib/btrfs/btrfsitem/item_metadata.go31
1 files changed, 28 insertions, 3 deletions
diff --git a/lib/btrfs/btrfsitem/item_metadata.go b/lib/btrfs/btrfsitem/item_metadata.go
index e90af8d..db2315e 100644
--- a/lib/btrfs/btrfsitem/item_metadata.go
+++ b/lib/btrfs/btrfsitem/item_metadata.go
@@ -1,4 +1,4 @@
-// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
@@ -9,17 +9,42 @@ import (
)
// Metadata is like Extent, but doesn't have .Info.
-type Metadata struct { // METADATA_ITEM=169
+type Metadata struct { // complex METADATA_ITEM=169
Head ExtentHeader
Refs []ExtentInlineRef
}
+func (o *Metadata) Free() {
+ for i := range o.Refs {
+ if o.Refs[i].Body != nil {
+ o.Refs[i].Body.Free()
+ }
+ o.Refs[i] = ExtentInlineRef{}
+ }
+ extentInlineRefPool.Put(o.Refs)
+ *o = Metadata{}
+ metadataPool.Put(o)
+}
+
+func (o Metadata) Clone() Metadata {
+ ret := o
+ ret.Refs = extentInlineRefPool.Get(len(o.Refs))
+ copy(ret.Refs, o.Refs)
+ for i := range ret.Refs {
+ ret.Refs[i].Body = o.Refs[i].Body.CloneItem()
+ }
+ return ret
+}
+
func (o *Metadata) UnmarshalBinary(dat []byte) (int, error) {
+ *o = Metadata{}
n, err := binstruct.Unmarshal(dat, &o.Head)
if err != nil {
return n, err
}
- o.Refs = nil
+ if n < len(dat) {
+ o.Refs = extentInlineRefPool.Get(1)[:0]
+ }
for n < len(dat) {
var ref ExtentInlineRef
_n, err := binstruct.Unmarshal(dat[n:], &ref)