1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
package btrfsvol
import (
"fmt"
"git.lukeshu.com/btrfs-progs-ng/lib/fmtutil"
)
type (
PhysicalAddr int64
LogicalAddr int64
AddrDelta int64
)
func formatAddr(addr int64, f fmt.State, verb rune) {
switch verb {
case 'v', 's', 'q':
str := fmt.Sprintf("%#016x", addr)
fmt.Fprintf(f, fmtutil.FmtStateString(f, verb), str)
default:
fmt.Fprintf(f, fmtutil.FmtStateString(f, verb), addr)
}
}
func (a PhysicalAddr) Format(f fmt.State, verb rune) { formatAddr(int64(a), f, verb) }
func (a LogicalAddr) Format(f fmt.State, verb rune) { formatAddr(int64(a), f, verb) }
func (d AddrDelta) Format(f fmt.State, verb rune) { formatAddr(int64(d), f, verb) }
func (a PhysicalAddr) Sub(b PhysicalAddr) AddrDelta { return AddrDelta(a - b) }
func (a LogicalAddr) Sub(b LogicalAddr) AddrDelta { return AddrDelta(a - b) }
func (a PhysicalAddr) Add(b AddrDelta) PhysicalAddr { return a + PhysicalAddr(b) }
func (a LogicalAddr) Add(b AddrDelta) LogicalAddr { return a + LogicalAddr(b) }
type DeviceID uint64
type QualifiedPhysicalAddr struct {
Dev DeviceID
Addr PhysicalAddr
}
func (a QualifiedPhysicalAddr) Add(b AddrDelta) QualifiedPhysicalAddr {
return QualifiedPhysicalAddr{
Dev: a.Dev,
Addr: a.Addr.Add(b),
}
}
func (a QualifiedPhysicalAddr) Cmp(b QualifiedPhysicalAddr) int {
if d := int(a.Dev - b.Dev); d != 0 {
return d
}
return int(a.Addr - b.Addr)
}
type _IntAddr[T any] interface {
~int64
Sub(T) AddrDelta
Add(AddrDelta) T
}
// IntAddr is a type constraint that you can use to accept LogicalAddr
// or PhysicalAddr.
type IntAddr[T _IntAddr[T]] _IntAddr[T]
|