From db7047c394edbcc713accda2a15f201a5b400ef7 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Wed, 13 Jul 2022 20:16:46 -0600 Subject: Misc tidy up --- lib/btrfs/internal/misc.go | 8 +++++--- lib/btrfs/internal/objid.go | 28 ++++++++++++++-------------- lib/btrfs/io4_fs.go | 2 +- lib/btrfsprogs/btrfsinspect/print_tree.go | 4 ++-- lib/containers/ordered.go | 21 +++++++-------------- lib/containers/syncmap.go | 8 ++++++-- lib/slices/sliceutil.go | 22 ++++++++++++++-------- lib/util/int.go | 7 ------- 8 files changed, 49 insertions(+), 51 deletions(-) delete mode 100644 lib/util/int.go (limited to 'lib') diff --git a/lib/btrfs/internal/misc.go b/lib/btrfs/internal/misc.go index eb5c287..3bc5402 100644 --- a/lib/btrfs/internal/misc.go +++ b/lib/btrfs/internal/misc.go @@ -26,15 +26,17 @@ func (k Key) String() string { } func (a Key) Cmp(b Key) int { - if d := containers.CmpUint(a.ObjectID, b.ObjectID); d != 0 { + if d := containers.NativeCmp(a.ObjectID, b.ObjectID); d != 0 { return d } - if d := containers.CmpUint(a.ItemType, b.ItemType); d != 0 { + if d := containers.NativeCmp(a.ItemType, b.ItemType); d != 0 { return d } - return containers.CmpUint(a.Offset, b.Offset) + return containers.NativeCmp(a.Offset, b.Offset) } +var _ containers.Ordered[Key] = Key{} + type Time struct { Sec int64 `bin:"off=0x0, siz=0x8"` // Number of seconds since 1970-01-01T00:00:00Z. NSec uint32 `bin:"off=0x8, siz=0x4"` // Number of nanoseconds since the beginning of the second. diff --git a/lib/btrfs/internal/objid.go b/lib/btrfs/internal/objid.go index f1d2a2a..fa9eb4d 100644 --- a/lib/btrfs/internal/objid.go +++ b/lib/btrfs/internal/objid.go @@ -6,12 +6,12 @@ package internal import ( "fmt" - - "git.lukeshu.com/btrfs-progs-ng/lib/util" ) type ObjID uint64 +const maxUint64pp = 0x1_00000000_00000000 + const ( // The IDs of the various trees ROOT_TREE_OBJECTID = ObjID(1) // holds pointers to all of the tree roots @@ -30,21 +30,21 @@ const ( DEV_STATS_OBJECTID = ObjID(0) // device stats in the device tree // ??? - BALANCE_OBJECTID = ObjID(util.MaxUint64pp - 4) // for storing balance parameters in the root tree - ORPHAN_OBJECTID = ObjID(util.MaxUint64pp - 5) // orphan objectid for tracking unlinked/truncated files - TREE_LOG_OBJECTID = ObjID(util.MaxUint64pp - 6) // does write ahead logging to speed up fsyncs - TREE_LOG_FIXUP_OBJECTID = ObjID(util.MaxUint64pp - 7) - TREE_RELOC_OBJECTID = ObjID(util.MaxUint64pp - 8) // space balancing - DATA_RELOC_TREE_OBJECTID = ObjID(util.MaxUint64pp - 9) - EXTENT_CSUM_OBJECTID = ObjID(util.MaxUint64pp - 10) // extent checksums all have this objectid - FREE_SPACE_OBJECTID = ObjID(util.MaxUint64pp - 11) // For storing free space cache - FREE_INO_OBJECTID = ObjID(util.MaxUint64pp - 12) // stores the inode number for the free-ino cache - - MULTIPLE_OBJECTIDS = ObjID(util.MaxUint64pp - 255) // dummy objectid represents multiple objectids + BALANCE_OBJECTID = ObjID(maxUint64pp - 4) // for storing balance parameters in the root tree + ORPHAN_OBJECTID = ObjID(maxUint64pp - 5) // orphan objectid for tracking unlinked/truncated files + TREE_LOG_OBJECTID = ObjID(maxUint64pp - 6) // does write ahead logging to speed up fsyncs + TREE_LOG_FIXUP_OBJECTID = ObjID(maxUint64pp - 7) + TREE_RELOC_OBJECTID = ObjID(maxUint64pp - 8) // space balancing + DATA_RELOC_TREE_OBJECTID = ObjID(maxUint64pp - 9) + EXTENT_CSUM_OBJECTID = ObjID(maxUint64pp - 10) // extent checksums all have this objectid + FREE_SPACE_OBJECTID = ObjID(maxUint64pp - 11) // For storing free space cache + FREE_INO_OBJECTID = ObjID(maxUint64pp - 12) // stores the inode number for the free-ino cache + + MULTIPLE_OBJECTIDS = ObjID(maxUint64pp - 255) // dummy objectid represents multiple objectids // All files have objectids in this range. FIRST_FREE_OBJECTID = ObjID(256) - LAST_FREE_OBJECTID = ObjID(util.MaxUint64pp - 256) + LAST_FREE_OBJECTID = ObjID(maxUint64pp - 256) FIRST_CHUNK_TREE_OBJECTID = ObjID(256) diff --git a/lib/btrfs/io4_fs.go b/lib/btrfs/io4_fs.go index 701230e..b2a2ee1 100644 --- a/lib/btrfs/io4_fs.go +++ b/lib/btrfs/io4_fs.go @@ -135,7 +135,7 @@ func (sv *Subvolume) LoadFullInode(inode ObjID) (*FullInode, error) { }, } items, err := sv.FS.TreeSearchAll(sv.TreeID, func(key Key) int { - return containers.CmpUint(inode, key.ObjectID) + return containers.NativeCmp(inode, key.ObjectID) }) if err != nil { val.Errs = append(val.Errs, err) diff --git a/lib/btrfsprogs/btrfsinspect/print_tree.go b/lib/btrfsprogs/btrfsinspect/print_tree.go index 960dcce..ff53953 100644 --- a/lib/btrfsprogs/btrfsinspect/print_tree.go +++ b/lib/btrfsprogs/btrfsinspect/print_tree.go @@ -8,6 +8,7 @@ import ( "context" "fmt" "io" + "math" "strings" "github.com/datawire/dlib/dlog" @@ -19,7 +20,6 @@ import ( "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol" "git.lukeshu.com/btrfs-progs-ng/lib/diskio" "git.lukeshu.com/btrfs-progs-ng/lib/slices" - "git.lukeshu.com/btrfs-progs-ng/lib/util" ) func DumpTrees(ctx context.Context, out io.Writer, fs *btrfs.FS) { @@ -432,7 +432,7 @@ func fmtKey(key btrfs.Key) string { case btrfsitem.ROOT_ITEM_KEY: fmt.Fprintf(&out, " %v)", btrfs.ObjID(key.Offset)) default: - if key.Offset == util.MaxUint64pp-1 { + if key.Offset == math.MaxUint64 { fmt.Fprintf(&out, " -1)") } else { fmt.Fprintf(&out, " %v)", key.Offset) diff --git a/lib/containers/ordered.go b/lib/containers/ordered.go index c5a3ec5..038edf8 100644 --- a/lib/containers/ordered.go +++ b/lib/containers/ordered.go @@ -8,17 +8,6 @@ import ( "golang.org/x/exp/constraints" ) -func CmpUint[T constraints.Unsigned](a, b T) int { - switch { - case a < b: - return -1 - case a == b: - return 0 - default: - return 1 - } -} - type Ordered[T interface{ Cmp(T) int }] interface { Cmp(T) int } @@ -27,15 +16,19 @@ type NativeOrdered[T constraints.Ordered] struct { Val T } -func (a NativeOrdered[T]) Cmp(b NativeOrdered[T]) int { +func NativeCmp[T constraints.Ordered](a, b T) int { switch { - case a.Val < b.Val: + case a < b: return -1 - case a.Val > b.Val: + case a > b: return 1 default: return 0 } } +func (a NativeOrdered[T]) Cmp(b NativeOrdered[T]) int { + return NativeCmp(a.Val, b.Val) +} + var _ Ordered[NativeOrdered[int]] = NativeOrdered[int]{} diff --git a/lib/containers/syncmap.go b/lib/containers/syncmap.go index 6c26b85..fb7f59b 100644 --- a/lib/containers/syncmap.go +++ b/lib/containers/syncmap.go @@ -12,7 +12,9 @@ type SyncMap[K comparable, V any] struct { inner sync.Map } -func (m *SyncMap[K, V]) Delete(key K) { m.inner.Delete(key) } +func (m *SyncMap[K, V]) Delete(key K) { + m.inner.Delete(key) +} func (m *SyncMap[K, V]) Load(key K) (value V, ok bool) { _value, ok := m.inner.Load(key) if ok { @@ -37,4 +39,6 @@ func (m *SyncMap[K, V]) Range(f func(key K, value V) bool) { return f(key.(K), value.(V)) }) } -func (m *SyncMap[K, V]) Store(key K, value V) { m.inner.Store(key, value) } +func (m *SyncMap[K, V]) Store(key K, value V) { + m.inner.Store(key, value) +} diff --git a/lib/slices/sliceutil.go b/lib/slices/sliceutil.go index 392514f..faaffcb 100644 --- a/lib/slices/sliceutil.go +++ b/lib/slices/sliceutil.go @@ -48,18 +48,24 @@ func Reverse[T any](slice []T) { } } -func Max[T constraints.Ordered](a, b T) T { - if a > b { - return a +func Max[T constraints.Ordered](a T, rest ...T) T { + ret := a + for _, b := range rest { + if b > a { + ret = b + } } - return b + return ret } -func Min[T constraints.Ordered](a, b T) T { - if a < b { - return a +func Min[T constraints.Ordered](a T, rest ...T) T { + ret := a + for _, b := range rest { + if b < a { + ret = b + } } - return b + return ret } func Sort[T constraints.Ordered](slice []T) { diff --git a/lib/util/int.go b/lib/util/int.go deleted file mode 100644 index ea24cf3..0000000 --- a/lib/util/int.go +++ /dev/null @@ -1,7 +0,0 @@ -// Copyright (C) 2022 Luke Shumaker -// -// SPDX-License-Identifier: GPL-2.0-or-later - -package util - -const MaxUint64pp = 0x1_00000000_00000000 -- cgit v1.2.3-2-g168b